Merge from vendor branch TNF:
[pkgsrc.git] / mk / bsd.pkg.extract.mk
1 # $NetBSD: bsd.pkg.extract.mk,v 1.12 2005/08/21 07:29:48 rillig Exp $
2 #
3 # This Makefile fragment is included to bsd.pkg.mk and defines the
4 # relevant variables and targets for the "extract" phase.
5 #
6 # The following variables may be set by the package Makefile and
7 # specify how extraction happens:
8 #
9 #    EXTRACT_ONLY is a list of distfiles relative to ${_DISTDIR} to
10 #       extract and defaults to ${DISTFILES}.
11 #
12 #    EXTRACT_SUFX is the suffix for the default distfile to be
13 #       extracted.  The default suffix is ".tar.gz".
14 #
15 #    EXTRACT_CMD is a shell command list that extracts the contents of
16 #       an archive named by the shell variable "extract_file" based on
17 #       the extension of the archive.  The extensions understood by
18 #       EXTRACT_CMD are listed in _EXTRACT_SUFFIXES.
19 #
20 #    EXTRACT_CMD.<sufx> is a shell command list that extracts the
21 #       contents of an archive named by the shell variable "extract_file"
22 #       that ends in <sufx>.
23 #
24 #    EXTRACT_USING specifies the tool used to extract tar/ustar-format
25 #       archives.  The possible values are "gtar", "nbtar", and "pax".
26 #       By default, we use the "nbtar" tool (NetBSD's pax-as-tar).
27 #
28 #    EXTRACT_ELEMENTS is a list of files within the distfile to extract.
29 #       This variable only takes effect for distfiles that are tarballs.
30 #       By default, this is empty, which causes all files within the
31 #       tarball to be extracted.
32 #
33 # The following targets are defined by bsd.pkg.extract.mk:
34 #
35 #    do-extract is the target that causes the actual extraction of
36 #       the distfiles to occur during the "extract" phase.
37 #
38
39 EXTRACT_ONLY?=          ${DISTFILES}
40 EXTRACT_SUFX?=          .tar.gz
41 EXTRACT_USING?=         nbtar
42
43 _EXTRACT_SUFFIXES=      .tar.gz .tgz .tar.bz2 .tbz .tbz2 .tar.Z .tar _tar.gz
44 _EXTRACT_SUFFIXES+=     .shar.gz .shar.bz2 .shar.Z .shar
45 _EXTRACT_SUFFIXES+=     .zip
46 _EXTRACT_SUFFIXES+=     .lha .lzh
47 _EXTRACT_SUFFIXES+=     .Z .bz2 .gz
48 _EXTRACT_SUFFIXES+=     .zoo
49 _EXTRACT_SUFFIXES+=     .bin
50 _EXTRACT_SUFFIXES+=     .rar
51
52 _EXTRACT_PATTERNS=      ${EXTRACT_ONLY} ${EXTRACT_SUFX}
53
54 .if !empty(_EXTRACT_PATTERNS:M*.tar) || \
55     !empty(_EXTRACT_PATTERNS:M*.tar.*) || \
56     !empty(_EXTRACT_PATTERNS:M*.tbz) || \
57     !empty(_EXTRACT_PATTERNS:M*.tbz2) || \
58     !empty(_EXTRACT_PATTERNS:M*.tgz) || \
59     !empty(_EXTRACT_PATTERNS:M*_tar.gz)
60
61 .  if !empty(EXTRACT_USING:Mgtar)
62 USE_TOOLS+=     gtar
63 .  elif !empty(EXTRACT_USING:Mnbtar)
64 USE_TOOLS+=     tar
65 .  else
66 USE_TOOLS+=     pax
67 .  endif
68 .endif
69 .if !empty(_EXTRACT_PATTERNS:M*.bz2) || \
70     !empty(_EXTRACT_PATTERNS:M*.tbz) || \
71     !empty(_EXTRACT_PATTERNS:M*.tbz2)
72 USE_TOOLS+=     bzcat
73 .endif
74 .if !empty(_EXTRACT_PATTERNS:M*.zip)
75 USE_TOOLS+=     unzip
76 .endif
77 .if !empty(_EXTRACT_PATTERNS:M*.lzh) || \
78     !empty(_EXTRACT_PATTERNS:M*.lha)
79 USE_TOOLS+=     lha
80 .endif
81 .if !empty(_EXTRACT_PATTERNS:M*.gz) || \
82     !empty(_EXTRACT_PATTERNS:M*.tgz) || \
83     !empty(_EXTRACT_PATTERNS:M*.Z)
84 USE_TOOLS+=     gzcat
85 .endif
86 .if !empty(_EXTRACT_PATTERNS:M*.zoo)
87 USE_TOOLS+=     unzoo
88 .endif
89 .if !empty(_EXTRACT_PATTERNS:M*.rar)
90 USE_TOOLS+=     unrar
91 .endif
92
93 DECOMPRESS_CMD.tar.gz?=         ${GZCAT}
94 DECOMPRESS_CMD.tgz?=            ${DECOMPRESS_CMD.tar.gz}
95 DECOMPRESS_CMD.tar.bz2?=        ${BZCAT}
96 DECOMPRESS_CMD.tbz?=            ${DECOMPRESS_CMD.tar.bz2}
97 DECOMPRESS_CMD.tbz2?=           ${DECOMPRESS_CMD.tar.bz2}
98 DECOMPRESS_CMD.tar.Z?=          ${GZCAT}
99 DECOMPRESS_CMD.tar?=            ${CAT}
100
101 DECOMPRESS_CMD.shar.gz?=        ${GZCAT}
102 DECOMPRESS_CMD.shar.bz2?=       ${BZCAT}
103 DECOMPRESS_CMD.shar.Z?=         ${GZCAT}
104 DECOMPRESS_CMD.shar?=           ${CAT}
105
106 DECOMPRESS_CMD.Z?=              ${GZCAT}
107 DECOMPRESS_CMD.bz2?=            ${BZCAT}
108 DECOMPRESS_CMD.gz?=             ${GZCAT}
109
110 DECOMPRESS_CMD?=                ${GZCAT}
111 .for __suffix__ in ${_EXTRACT_SUFFIXES}
112 .  if !defined(DECOMPRESS_CMD${__suffix__})
113 DECOMPRESS_CMD${__suffix__}?=   ${DECOMPRESS_CMD}
114 .  endif
115 .endfor
116
117 # If this is empty, then everything gets extracted.
118 EXTRACT_ELEMENTS?=      # empty
119
120 DOWNLOADED_DISTFILE=    $${extract_file}
121
122 EXTRACT_CMD.zip?=       ${UNZIP} ${EXTRACT_CMD_OPTS.zip} $${extract_file}
123 EXTRACT_CMD_OPTS.zip?=  -Laqo
124 EXTRACT_CMD.lha?=       ${LHA} ${EXTRACT_CMD_OPTS.lha} $${extract_file}
125 EXTRACT_CMD_OPTS.lha?=  xq
126 EXTRACT_CMD.lzh?=       ${EXTRACT_CMD.lha}
127 EXTRACT_CMD_OPTS.lzh?=  ${EXTRACT_CMD_OPTS.lha}
128 EXTRACT_CMD.zoo?=       ${UNZOO} ${EXTRACT_CMD_OPTS.zoo} $${extract_file}
129 EXTRACT_CMD_OPTS.zoo?=  -x
130 EXTRACT_CMD.rar?=       ${UNRAR} ${EXTRACT_CMD_OPTS.rar} $${extract_file}
131 EXTRACT_CMD_OPTS.rar?=  x -inul
132 EXTRACT_ENV.bin?=       # empty
133 EXTRACT_CMD.bin?=       ${ECHO} yes | ${SETENV} ${EXTRACT_ENV.bin} $${extract_file} ${EXTRACT_CMD_OPTS.bin} >/dev/null
134
135 .for __suffix__ in .gz .bz2 .Z
136 EXTRACT_CMD${__suffix__}?=      ${DECOMPRESS_CMD${__suffix__}} $${extract_file} > `${BASENAME} $${extract_file} ${__suffix__}`
137 .endfor
138
139 .for __suffix__ in .shar.gz .shar.bz2 .shar.Z .shar
140 EXTRACT_CMD${__suffix__}?=      ${DECOMPRESS_CMD${__suffix__}} $${extract_file} | ${SH}
141 .endfor
142
143 .if !empty(EXTRACT_USING:Mgtar)
144 _DFLT_EXTRACT_CMD?=     ${DECOMPRESS_CMD} $${extract_file} | ${GTAR} -xf - ${EXTRACT_ELEMENTS}
145 .elif !empty(EXTRACT_USING:Mnbtar)
146 _DFLT_EXTRACT_CMD?=     ${DECOMPRESS_CMD} $${extract_file} | ${TAR} -xf - ${EXTRACT_ELEMENTS}
147 .else
148 _DFLT_EXTRACT_CMD?=     ${DECOMPRESS_CMD} $${extract_file} | ${PAX} -O -r ${EXTRACT_ELEMENTS}
149 .endif
150
151 .for __suffix__ in ${_EXTRACT_SUFFIXES}
152 .  if !defined(EXTRACT_CMD${__suffix__})
153 .    if !empty(EXTRACT_USING:Mgtar)
154 EXTRACT_CMD${__suffix__}?=      ${DECOMPRESS_CMD${__suffix__}} $${extract_file} | ${GTAR} ${EXTRACT_CMD_OPTS${__suffix__}} -xf - ${EXTRACT_ELEMENTS}
155 .    elif !empty(EXTRACT_USING:Mnbtar)
156 EXTRACT_CMD${__suffix__}?=      ${DECOMPRESS_CMD${__suffix__}} $${extract_file} | ${TAR} ${EXTRACT_CMD_OPTS${__suffix__}} -xf - ${EXTRACT_ELEMENTS}
157 .    else
158 EXTRACT_CMD${__suffix__}?=      ${DECOMPRESS_CMD${__suffix__}} $${extract_file} | ${PAX} ${EXTRACT_CMD_OPTS${__suffix__}} -O -r ${EXTRACT_ELEMENTS}
159 .    endif
160 .  endif
161 .endfor
162
163 # _SHELL_EXTRACT is a "subroutine" for extracting an archive.  It extracts
164 # the contents of archive named by the shell variable "extract_file" based
165 # on the file extension of the archive.
166 #
167 _SHELL_EXTRACT=         case $${extract_file} in
168 .for __suffix__ in ${_EXTRACT_SUFFIXES}
169 _SHELL_EXTRACT+=        *${__suffix__}) ${EXTRACT_CMD${__suffix__}} ;;
170 .endfor
171 _SHELL_EXTRACT+=        *)              ${_DFLT_EXTRACT_CMD} ;;
172 _SHELL_EXTRACT+=        esac
173
174 EXTRACT_CMD?=           ${_SHELL_EXTRACT}
175
176 .PHONY: do-extract
177 .if !target(do-extract)
178 do-extract: ${WRKDIR}
179 .  for __file__ in ${EXTRACT_ONLY}
180         ${_PKG_SILENT}${_PKG_DEBUG}                                     \
181         extract_file="${_DISTDIR}/${__file__}"; export extract_file;    \
182         cd ${WRKDIR}; ${EXTRACT_CMD}
183 .  endfor
184 .endif