diff options
Diffstat (limited to 'mk/extract')
-rw-r--r-- | mk/extract/bsd.extract-vars.mk | 63 | ||||
-rw-r--r-- | mk/extract/bsd.extract.mk | 15 | ||||
-rwxr-xr-x | mk/extract/extract | 306 | ||||
-rw-r--r-- | mk/extract/extract.mk | 189 |
4 files changed, 573 insertions, 0 deletions
diff --git a/mk/extract/bsd.extract-vars.mk b/mk/extract/bsd.extract-vars.mk new file mode 100644 index 00000000000..3ff08de0e9b --- /dev/null +++ b/mk/extract/bsd.extract-vars.mk @@ -0,0 +1,63 @@ +# $NetBSD: bsd.extract-vars.mk,v 1.1 2006/06/06 03:05:48 jlam Exp $ +# +# This Makefile fragment is included to bsd.prefs.mk and defines some +# variables which must be defined earlier than where bsd.extract.mk +# is included. +# +# The following variables may be set by the package Makefile and +# specify how extraction happens: +# +# EXTRACT_ONLY is a list of distfiles relative to ${_DISTDIR} to +# extract and defaults to ${DISTFILES}. +# +# EXTRACT_SUFX is the suffix for the default distfile to be +# extracted. The default suffix is ".tar.gz". +# + +EXTRACT_ONLY?= ${DISTFILES} +EXTRACT_SUFX?= .tar.gz + +### +### Discover which tools we need based on the file extensions of the +### distfiles. +### +_EXTRACT_PATTERNS= ${EXTRACT_ONLY} ${EXTRACT_SUFX} + +.if !empty(_EXTRACT_PATTERNS:M*.tar) || \ + !empty(_EXTRACT_PATTERNS:M*.tar.*) || \ + !empty(_EXTRACT_PATTERNS:M*.tbz) || \ + !empty(_EXTRACT_PATTERNS:M*.tbz2) || \ + !empty(_EXTRACT_PATTERNS:M*.tgz) || \ + !empty(_EXTRACT_PATTERNS:M*-tar.gz) || \ + !empty(_EXTRACT_PATTERNS:M*_tar.gz) +. if !empty(EXTRACT_USING:Mgtar) +USE_TOOLS+= gtar +. elif !empty(EXTRACT_USING:Mnbtar) +USE_TOOLS+= tar +. else +USE_TOOLS+= pax +. endif +.endif +.if !empty(_EXTRACT_PATTERNS:M*.bz2) || \ + !empty(_EXTRACT_PATTERNS:M*.tbz) || \ + !empty(_EXTRACT_PATTERNS:M*.tbz2) +USE_TOOLS+= bzcat +.endif +.if !empty(_EXTRACT_PATTERNS:M*.zip) +USE_TOOLS+= unzip +.endif +.if !empty(_EXTRACT_PATTERNS:M*.lzh) || \ + !empty(_EXTRACT_PATTERNS:M*.lha) +USE_TOOLS+= lha +.endif +.if !empty(_EXTRACT_PATTERNS:M*.gz) || \ + !empty(_EXTRACT_PATTERNS:M*.tgz) || \ + !empty(_EXTRACT_PATTERNS:M*.Z) +USE_TOOLS+= gzcat +.endif +.if !empty(_EXTRACT_PATTERNS:M*.zoo) +USE_TOOLS+= unzoo +.endif +.if !empty(_EXTRACT_PATTERNS:M*.rar) +USE_TOOLS+= unrar +.endif diff --git a/mk/extract/bsd.extract.mk b/mk/extract/bsd.extract.mk new file mode 100644 index 00000000000..3c837c7afbe --- /dev/null +++ b/mk/extract/bsd.extract.mk @@ -0,0 +1,15 @@ +# $NetBSD: bsd.extract.mk,v 1.1 2006/06/06 03:05:48 jlam Exp $ +# +# This Makefile fragment is included to bsd.pkg.mk and defines the +# relevant variables and targets for the "extract" phase. +# +# The following are the "public" targets provided by this module: +# +# extract +# +# The following targets may be overridden in a package Makefile: +# +# pre-extract, do-extract, post-extract +# + +.include "${PKGSRCDIR}/mk/extract/extract.mk" diff --git a/mk/extract/extract b/mk/extract/extract new file mode 100755 index 00000000000..b8590dcb8ad --- /dev/null +++ b/mk/extract/extract @@ -0,0 +1,306 @@ +#!/bin/sh +# +# $NetBSD: extract,v 1.1 2006/06/06 03:05:48 jlam Exp $ +# +# Copyright (c) 2006 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Johnny C. Lam. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by the NetBSD +# Foundation, Inc. and its contributors. +# 4. Neither the name of The NetBSD Foundation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +###################################################################### +# +# NAME +# extract -- extract distfile, regardless of format +# +# SYNOPSIS +# extract [options] distfile [file ...] +# +# DESCRIPTION +# extract will unpack the contents of the named distfile into the +# current working directory. If any optional files are specified then +# only they will be extracted from the distfile, provided that the +# underlying tool supports this ability. If the distfile's file +# extension doesn't match any known archive format's, then the +# distfile is simply copied into the current working directory. If +# "-" is given as the distfile, then standard input is used as the +# contents of the archive, provided that the underlying tool supports +# this ability. +# +# OPTIONS +# -d dir Extract the files into the specified dir instead +# of the current working directory. If the directory +# doesn't exist, then it is created along with any +# intermediate directories using the current umask. +# +# -f format Force interpretation of the distfile's archive +# format to be the specified format. +# +# -t tarprog This specifies the tool to use to extract tar/ustar +# archives, and may be either "tar" or "pax", or the +# full path to the program. +# +# -X excludefile excludefile is a list of file patterns to exclude +# from extraction. If the -X option is specified +# then any optional files listed on the command line +# are ignored. +# +# -x This causes the optional files listed on the +# command line to be excluded from extraction, +# provided the underlying tool supports this +# ability. +# +# ENVIRONMENT +# EXTRACT_OPTS_BIN +# EXTRACT_OPTS_LHA +# EXTRACT_OPTS_PAX +# EXTRACT_OPTS_RAR +# EXTRACT_OPTS_TAR +# EXTRACT_OPTS_ZIP +# EXTRACT_OPTS_ZOO +# These variables set additional arguments passed to the +# underlying extraction tool to unpack their respective +# archive formats. +# +###################################################################### + +set -e # exit on errors +set -u # treat undefined variables as errors + +: ${BZCAT:=bzcat} +: ${CAT:=cat} +: ${CP:=cp} +: ${ECHO:=echo} +: ${GZCAT:=gzcat} +: ${LHA:=lha} +: ${MKDIR:=mkdir} +: ${PAX:=pax} +: ${RM:=rm} +: ${SH:=sh} +: ${TAR:=tar} +: ${TEST:=test} +: ${UNRAR:=unrar} +: ${UNZIP_CMD:=unzip} +: ${UNZOO:=unzoo} + +: ${TMPDIR:=/tmp} + +self="${0##*/}" + +usage() { + ${ECHO} 1>&2 "usage: $self [-d dir] [-f format] [-t tarprog] [-X excludefile | -x] distfile [file ...]" +} + +exclude=no +exclude_file= +exclude_flag= +extract_dir=. +extract_using=tar +format= + +# Process optional arguments +while ${TEST} $# -gt 0; do + case "$1" in + -d) extract_dir="$2"; shift 2 ;; + -f) format="$2"; shift 2 ;; + -t) extract_using="$2"; shift 2 ;; + -X) exclude_file="$2"; shift 2 ;; + -x) exclude=yes; shift ;; + --) shift; break ;; + -?*) ${ECHO} 1>&2 "$self: unknown option -- ${1#-}" + usage + exit 1 + ;; + *) break ;; + esac +done + +case "$extract_using" in +/*tar|/*pax) tarprog="$extract_using" ;; +*tar) tarprog="${TAR}" ;; +*pax) tarprog="${PAX}" ;; +*) tarprog="${TAR}" ;; +esac + +if ${TEST} -n "$exclude_file" -a ! -f "$exclude_file"; then + ${ECHO} 1>&2 "$self: exclude file missing: $exclude_file" + exit 1 +fi + +# Process required arguments +${TEST} $# -gt 0 || { usage; exit 1; } +distfile="$1"; shift + +# Make distfile an absolute path, because we will change the current +# directory soon. +case "$distfile" in +/*) ;; +*) distfile=`exec pwd`/"$distfile" + ;; +esac + +# Set the command to decompress the file and write the contents to stdout. +case "$distfile" in +*.gz|*.tgz|*.z) decompress_cat="${GZCAT}" ;; +*.bz2|*.tbz|*.tbz2|*.bz) decompress_cat="${BZCAT}" ;; +*.Z) decompress_cat="${GZCAT}" ;; +*) decompress_cat="${CAT}" ;; +esac + +# Derive the format of the archive based on the file extension. +case "$distfile" in +*.tar.gz|*.tgz|*-tar.gz|*_tar.gz|*.tar.bz2|*.tbz|*.tbz2|*.tar.Z|*.tar.z|*.tar|*.tar.bz) + _format=tar ;; +*.shar.gz|*.shar.bz2|*.shar.Z|*.shar|*.shr.gz|*.shr.bz2|*.shr.Z|*.shr) + _format=shar ;; +*.zip|*.ZIP) _format=zip ;; +*.lha|*.lzh) _format=lha ;; +*.Z|*.bz2|*.gz|*.z) + _format=compressed ;; +*.zoo) _format=zoo ;; +*.rar) _format=rar ;; +*.bin) _format=jre-bin ;; +*) _format=none ;; +esac +${TEST} -n "$format" || format="$_format" + +case "$format" in +tar|shar) ;; +*) if ${TEST} "$distfile" = "-"; then + ${ECHO} 1>&2 "$self: archive format cannot be given on standard input -- $format" + exit 1 + fi + ;; +esac + +${TEST} -d "$extract_dir" || ${MKDIR} -p "$extract_dir" +cd "$extract_dir" + +# Use the correct tool and extraction procedure to perform the extraction +# based on the archive format. +# +case "$format" in +tar) + case "$extract_using" in + *pax) + : ${EXTRACT_OPTS_PAX=} + case "$extract_using" in + /*) paxprog="$extract_using" ;; + *) paxprog="${PAX}" ;; + esac + if ${TEST} -n "$exclude_file"; then + exclude=yes + set -- dummy `${CAT} "$exclude_file"`; shift + fi + ${TEST} "$exclude" = no || exclude_flag="-c" + $decompress_cat "$distfile" | + $paxprog ${EXTRACT_OPTS_PAX} $exclude_flag -O -r ${1+"$@"} + ;; + *tar) + : ${EXTRACT_OPTS_TAR=} + case "$extract_using" in + /*) tarprog="$extract_using" ;; + *) tarprog="${TAR}" ;; + esac + tmpfile= + if ${TEST} "$exclude" = "yes"; then + tmpfile="${TMPDIR}/$self.$$" + ${RM} -f "$tmpfile" + trap "\${RM} -f \"\$tmpfile\"" 0 + for i in ${1+"$@"}; do + ${ECHO} "$i" >> "$tmpfile" + done + exclude_file="$tmpfile" + fi + if ${TEST} -n "$exclude_file"; then + exclude_flag="-X $exclude_file" + set -- dummy; shift + fi + $decompress_cat "$distfile" | + $tarprog ${EXTRACT_OPTS_TAR} $exclude_flag -xf - ${1+"$@"} + ;; + *) + ${ECHO} 1>&2 "$self: unknown tar program: $extract_using" + exit 1 + esac + ;; + +shar) + $decompress_cat "$distfile" | ${SH} + ;; + +zip) + : ${EXTRACT_OPTS_ZIP=-Laqo} + ${TEST} "$exclude" = "no" || exclude_flag="-x" + if ${TEST} -n "$exclude_file"; then + set -- dummy `${CAT} "$exclude_file"`; shift + fi + ${UNZIP_CMD} ${EXTRACT_OPTS_ZIP} "$distfile" $exclude_flag ${1+"$@"} + ;; + +lha) + : ${EXTRACT_OPTS_LHA=q} + ${LHA} x${EXTRACT_OPTS_LHA} "$distfile" ${1+"$@"} + ;; + +compressed) + target="${distfile##*/}"; target="${target%.*}" + $decompress_cat "$distfile" > "$target" + ;; + +zoo) + : ${EXTRACT_OPTS_ZOO=} + ${UNZOO} -x ${EXTRACT_OPTS_ZOO} "$distfile" ${1+"$@"} + ;; + +rar) + : ${EXTRACT_OPTS_RAR=-inul} + ${UNRAR} x ${EXTRACT_OPTS_RAR} "$distfile" ${1+"$@"} + ;; + +jre-bin) + : ${EXTRACT_OPTS_BIN=} + ${ECHO} yes | "$distfile" ${EXTRACT_OPTS_BIN} >/dev/null + ;; + +none) + # By default, copy the distfile over to the current working directory. + ${CP} "$distfile" . + ;; + +*) + ${ECHO} 1>&2 "$self: archive format not recognized -- $format" + exit 1 + ;; +esac + +exit 0 diff --git a/mk/extract/extract.mk b/mk/extract/extract.mk new file mode 100644 index 00000000000..f4c7aac4947 --- /dev/null +++ b/mk/extract/extract.mk @@ -0,0 +1,189 @@ +# $NetBSD: extract.mk,v 1.1 2006/06/06 03:05:48 jlam Exp $ +# +# The following variables may be set by the package Makefile and +# specify how extraction happens: +# +# EXTRACT_CMD is a shell command list that extracts the contents of +# an archive named by the variable ${DOWNLOADED_DISTFILE} to the +# current working directory. The default is ${EXTRACT_CMD_DEFAULT}. +# +# EXTRACT_CMD_DEFAULT uses the "extract" script to unpack archives. The +# precise manner in which extraction occurs may be tweaked by setting +# EXTRACT_OPTS, EXTRACT_USING and EXTRACT_ELEMENTS. +# +# EXTRACT_OPTS is a list of options to pass to the "extract" script +# when using EXTRACT_CMD_DEFAULT. See the comments at the head of +# the "extract" script for a definitive list of the available +# options. The default list is empty. +# +# EXTRACT_USING specifies the tool used to extract tar/ustar-format +# archives when using EXTRACT_CMD_DEFAULT. The possible values are +# "gtar", "nbtar", and "pax". By default, we use the "nbtar" tool +# (pkgsrc's pax-as-tar). +# +# EXTRACT_ELEMENTS is a list of files within the distfile to extract +# when using EXTRACT_CMD_DEFAULT. By default, this is empty, which +# causes all files within the archive to be extracted. +# +# The following are read-only variables that may be used within a package +# Makefile: +# +# DOWNLOADED_DISTFILE represents the path to the distfile that is +# currently being extracted, and may be used in custom EXTRACT_CMD +# overrides, e.g. +# +# EXTRACT_CMD= ${TAIL} +25 ${DOWNLOADED_DISTFILE} > foo.pl +# + +_EXTRACT_COOKIE= ${WRKDIR}/.extract_done + +###################################################################### +### extract (PUBLIC) +###################################################################### +### extract is a public target to perform extraction. +### +_EXTRACT_TARGETS+= checksum +_EXTRACT_TARGETS+= ${WRKDIR} +_EXTRACT_TARGETS+= depends +_EXTRACT_TARGETS+= acquire-extract-lock +_EXTRACT_TARGETS+= ${_EXTRACT_COOKIE} +_EXTRACT_TARGETS+= release-extract-lock + +.PHONY: extract +.if !target(extract) +extract: ${_EXTRACT_TARGETS} +.endif + +.PHONY: acquire-extract-lock release-extract-lock +acquire-extract-lock: acquire-lock +release-extract-lock: release-lock + +.if !exists(${_EXTRACT_COOKIE}) +${_EXTRACT_COOKIE}: real-extract +.else +${_EXTRACT_COOKIE}: + @${DO_NADA} +.endif + +###################################################################### +### real-extract (PRIVATE) +###################################################################### +### real-extract is a helper target onto which one can hook all of the +### targets that do the actual extraction work. +### +_REAL_EXTRACT_TARGETS+= extract-check-interactive +_REAL_EXTRACT_TARGETS+= extract-message +_REAL_EXTRACT_TARGETS+= extract-vars +_REAL_EXTRACT_TARGETS+= pre-extract +_REAL_EXTRACT_TARGETS+= do-extract +_REAL_EXTRACT_TARGETS+= post-extract +_REAL_EXTRACT_TARGETS+= extract-cookie + +.PHONY: real-extract +real-extract: ${_REAL_EXTRACT_TARGETS} + +.PHONY: extract-message +extract-message: + @${PHASE_MSG} "Extracting for ${PKGNAME}" + +###################################################################### +### extract-check-interactive (PRIVATE) +###################################################################### +### extract-check-interactive checks whether we can do an interactive +### extraction or not. +### +extract-check-interactive: +.if !empty(INTERACTIVE_STAGE:Mextract) && defined(BATCH) + @${ERROR_MSG} "The extract stage of this package requires user interaction" + @${ERROR_MSG} "Please extract manually with:" + @${ERROR_MSG} " \"cd ${PKGSRCDIR}/${PKGPATH} && ${MAKE} extract\"" + @${TOUCH} ${_INTERACTIVE_COOKIE} + @${FALSE} +.else + @${DO_NADA} +.endif + +###################################################################### +### extract-cookie (PRIVATE) +###################################################################### +### extract-cookie creates the "extract" cookie file. The contents +### are the name of the package. +### +.PHONY: extract-cookie +extract-cookie: + ${_PKG_SILENT}${_PKG_DEBUG}${MKDIR} ${_EXTRACT_COOKIE:H} + ${_PKG_SILENT}${_PKG_DEBUG}${ECHO} ${PKGNAME} >> ${_EXTRACT_COOKIE} + +###################################################################### +### pre-extract, do-extract, post-extract (PUBLIC, override) +###################################################################### +### {pre,do,post}-extract are the heart of the package-customizable +### extract targets, and may be overridden within a package Makefile. +### +.PHONY: pre-extract do-extract post-extract + +EXTRACT_USING?= nbtar +EXTRACT_ELEMENTS?= # empty + +### +### Build the default extraction command +### +_EXTRACT_ENV+= ${EXTRACT_OPTS_BIN:D EXTRACT_OPTS_BIN=${EXTRACT_OPTS_BIN:Q}} +_EXTRACT_ENV+= ${EXTRACT_OPTS_LHA:D EXTRACT_OPTS_LHA=${EXTRACT_OPTS_LHA:Q}} +_EXTRACT_ENV+= ${EXTRACT_OPTS_PAX:D EXTRACT_OPTS_PAX=${EXTRACT_OPTS_PAX:Q}} +_EXTRACT_ENV+= ${EXTRACT_OPTS_RAR:D EXTRACT_OPTS_RAR=${EXTRACT_OPTS_RAR:Q}} +_EXTRACT_ENV+= ${EXTRACT_OPTS_TAR:D EXTRACT_OPTS_TAR=${EXTRACT_OPTS_TAR:Q}} +_EXTRACT_ENV+= ${EXTRACT_OPTS_ZIP:D EXTRACT_OPTS_ZIP=${EXTRACT_OPTS_ZIP:Q}} +_EXTRACT_ENV+= ${EXTRACT_OPTS_ZOO:D EXTRACT_OPTS_ZOO=${EXTRACT_OPTS_ZOO:Q}} +_EXTRACT_ENV+= ${TOOLS_BZCAT:D BZCAT=${TOOLS_BZCAT:Q}} +_EXTRACT_ENV+= ${TOOLS_CAT:D CAT=${TOOLS_CAT:Q}} +_EXTRACT_ENV+= ${TOOLS_CP:D CP=${TOOLS_CP:Q}} +_EXTRACT_ENV+= ${TOOLS_ECHO:D ECHO=${TOOLS_ECHO:Q}} +_EXTRACT_ENV+= ${TOOLS_CMDLINE.gzcat:D GZCAT=${TOOLS_CMDLINE.gzcat:Q}} +_EXTRACT_ENV+= ${TOOLS_LHA:D LHA=${TOOLS_LHA:Q}} +_EXTRACT_ENV+= ${TOOLS_MKDIR:D MKDIR=${TOOLS_MKDIR:Q}} +_EXTRACT_ENV+= ${TOOLS_RM:D RM=${TOOLS_RM:Q}} +_EXTRACT_ENV+= ${TOOLS_PAX:D PAX=${TOOLS_PAX:Q}} +_EXTRACT_ENV+= ${TOOLS_SH:D SH=${TOOLS_SH:Q}} +_EXTRACT_ENV+= ${TOOLS_TAR:D TAR=${TOOLS_TAR:Q}} +_EXTRACT_ENV+= ${TOOLS_TEST:D TEST=${TOOLS_TEST:Q}} +_EXTRACT_ENV+= ${TOOLS_UNRAR:D UNRAR=${TOOLS_UNRAR:Q}} +_EXTRACT_ENV+= ${TOOLS_UNZIP_CMD:D UNZIP_CMD=${TOOLS_UNZIP_CMD:Q}} +_EXTRACT_ENV+= ${TOOLS_UNZOO:D UNZOO=${TOOLS_UNZOO:Q}} +_EXTRACT_ENV+= ${EXTRACT_ENV} + +.if !empty(EXTRACT_USING:Mgtar) +EXTRACT_OPTS+= ${TOOLS_GTAR:D -t ${TOOLS_GTAR}} +.elif !empty(EXTRACT_USING:Mnbtar) +EXTRACT_OPTS+= ${TOOLS_TAR:D -t ${TOOLS_TAR}} +.else +EXTRACT_OPTS+= ${TOOLS_PAX:D -t ${TOOLS_PAX}} +.endif + +EXTRACT_CMD_DEFAULT= \ + ${SETENV} ${_EXTRACT_ENV} \ + ${SH} ${PKGSRCDIR}//mk/extract/extract \ + ${EXTRACT_OPTS} \ + ${DOWNLOADED_DISTFILE} ${EXTRACT_ELEMENTS} + +EXTRACT_CMD?= ${EXTRACT_CMD_DEFAULT} + +DOWNLOADED_DISTFILE= $${extract_file} + +.if !target(do-extract) +do-extract: ${WRKDIR} +. for __file__ in ${EXTRACT_ONLY} + ${_PKG_SILENT}${_PKG_DEBUG} \ + extract_file=${_DISTDIR:Q}/${__file__:Q}; export extract_file; \ + cd ${WRKDIR} && ${EXTRACT_CMD} +. endfor +.endif + +.if !target(pre-extract) +pre-extract: + @${DO_NADA} +.endif +.if !target(post-extract) +post-extract: + @${DO_NADA} +.endif |