Improve the name-version regex
[gentoo.git] / eclass / autotools-utils.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: autotools-utils.eclass
5 # @MAINTAINER:
6 # Maciej Mrozowski <reavertm@gentoo.org>
7 # Michał Górny <mgorny@gentoo.org>
8 # @SUPPORTED_EAPIS: 4 5
9 # @BLURB: common ebuild functions for autotools-based packages
10 # @DESCRIPTION:
11 # autotools-utils.eclass is autotools.eclass(5) and base.eclass(5) wrapper
12 # providing all inherited features along with econf arguments as Bash array,
13 # out of source build with overridable build dir location, static archives
14 # handling, libtool files removal.
15 #
16 # Please note that autotools-utils does not support mixing of its phase
17 # functions with regular econf/emake calls. If necessary, please call
18 # autotools-utils_src_compile instead of the latter.
19 #
20 # @EXAMPLE:
21 # Typical ebuild using autotools-utils.eclass:
22 #
23 # @CODE
24 # EAPI="2"
25 #
26 # inherit autotools-utils
27 #
28 # DESCRIPTION="Foo bar application"
29 # HOMEPAGE="http://example.org/foo/"
30 # SRC_URI="mirror://sourceforge/foo/${P}.tar.bz2"
31 #
32 # LICENSE="LGPL-2.1"
33 # KEYWORDS=""
34 # SLOT="0"
35 # IUSE="debug doc examples qt4 static-libs tiff"
36 #
37 # CDEPEND="
38 #       media-libs/libpng:0
39 #       qt4? (
40 #               dev-qt/qtcore:4
41 #               dev-qt/qtgui:4
42 #       )
43 #       tiff? ( media-libs/tiff:0 )
44 # "
45 # RDEPEND="${CDEPEND}
46 #       !media-gfx/bar
47 # "
48 # DEPEND="${CDEPEND}
49 #       doc? ( app-doc/doxygen )
50 # "
51 #
52 # # bug 123456
53 # AUTOTOOLS_IN_SOURCE_BUILD=1
54 #
55 # DOCS=(AUTHORS ChangeLog README "Read me.txt" TODO)
56 #
57 # PATCHES=(
58 #       "${FILESDIR}/${P}-gcc44.patch" # bug 123458
59 #       "${FILESDIR}/${P}-as-needed.patch"
60 #       "${FILESDIR}/${P}-unbundle_libpng.patch"
61 # )
62 #
63 # src_configure() {
64 #       local myeconfargs=(
65 #               $(use_enable debug)
66 #               $(use_with qt4)
67 #               $(use_enable threads multithreading)
68 #               $(use_with tiff)
69 #       )
70 #       autotools-utils_src_configure
71 # }
72 #
73 # src_compile() {
74 #       autotools-utils_src_compile
75 #       use doc && autotools-utils_src_compile docs
76 # }
77 #
78 # src_install() {
79 #       use doc && HTML_DOCS=("${BUILD_DIR}/apidocs/html/")
80 #       autotools-utils_src_install
81 #       if use examples; then
82 #               dobin "${BUILD_DIR}"/foo_example{1,2,3} \\
83 #                       || die 'dobin examples failed'
84 #       fi
85 # }
86 #
87 # @CODE
88
89 # Keep variable names synced with cmake-utils and the other way around!
90
91 case ${EAPI:-0} in
92         6) die "${ECLASS}.eclass is banned in EAPI ${EAPI}";;
93         4|5) ;;
94         *) die "EAPI=${EAPI} is not supported" ;;
95 esac
96
97 # @ECLASS-VARIABLE: AUTOTOOLS_AUTORECONF
98 # @DEFAULT_UNSET
99 # @DESCRIPTION:
100 # Set to a non-empty value before calling inherit to enable running autoreconf
101 # in src_prepare() and adding autotools dependencies.
102 #
103 # This is usually necessary when using live sources or applying patches
104 # modifying configure.ac or Makefile.am files. Note that in the latter case
105 # setting this variable is obligatory even though the eclass will work without
106 # it (to add the necessary dependencies).
107 #
108 # The eclass will try to determine the correct autotools to run including a few
109 # external tools: gettext, glib-gettext, intltool, gtk-doc, gnome-doc-prepare.
110 # If your tool is not supported, please open a bug and we'll add support for it.
111 #
112 # Note that dependencies are added for autoconf, automake and libtool only.
113 # If your package needs one of the external tools listed above, you need to add
114 # appropriate packages to DEPEND yourself.
115 [[ ${AUTOTOOLS_AUTORECONF} ]] || : ${AUTOTOOLS_AUTO_DEPEND:=no}
116
117 # eutils for eqawarn, path_exists
118 inherit autotools epatch eutils libtool ltprune
119
120 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_install src_test
121
122 # @ECLASS-VARIABLE: BUILD_DIR
123 # @DEFAULT_UNSET
124 # @DESCRIPTION:
125 # Build directory, location where all autotools generated files should be
126 # placed. For out of source builds it defaults to ${WORKDIR}/${P}_build.
127 #
128 # This variable has been called AUTOTOOLS_BUILD_DIR formerly.
129 # It is set under that name for compatibility.
130
131 # @ECLASS-VARIABLE: AUTOTOOLS_IN_SOURCE_BUILD
132 # @DEFAULT_UNSET
133 # @DESCRIPTION:
134 # Set to enable in-source build.
135
136 # @ECLASS-VARIABLE: ECONF_SOURCE
137 # @DEFAULT_UNSET
138 # @DESCRIPTION:
139 # Specify location of autotools' configure script. By default it uses ${S}.
140
141 # @ECLASS-VARIABLE: DOCS
142 # @DEFAULT_UNSET
143 # @DESCRIPTION:
144 # Array containing documents passed to dodoc command.
145 #
146 # In EAPIs 4+, can list directories as well.
147 #
148 # Example:
149 # @CODE
150 # DOCS=( NEWS README )
151 # @CODE
152
153 # @ECLASS-VARIABLE: HTML_DOCS
154 # @DEFAULT_UNSET
155 # @DESCRIPTION:
156 # Array containing documents passed to dohtml command.
157 #
158 # Example:
159 # @CODE
160 # HTML_DOCS=( doc/html/ )
161 # @CODE
162
163 # @ECLASS-VARIABLE: PATCHES
164 # @DEFAULT_UNSET
165 # @DESCRIPTION:
166 # PATCHES array variable containing all various patches to be applied.
167 #
168 # Example:
169 # @CODE
170 # PATCHES=( "${FILESDIR}"/${P}-mypatch.patch )
171 # @CODE
172
173 # @ECLASS-VARIABLE: AUTOTOOLS_PRUNE_LIBTOOL_FILES
174 # @DEFAULT_UNSET
175 # @DESCRIPTION:
176 # Sets the mode of pruning libtool files. The values correspond to
177 # prune_libtool_files parameters, with leading dashes stripped.
178 #
179 # Defaults to pruning the libtool files when static libraries are not
180 # installed or can be linked properly without them. Libtool files
181 # for modules (plugins) will be kept in case plugin loader needs them.
182 #
183 # If set to 'modules', the .la files for modules will be removed
184 # as well. This is often the preferred option.
185 #
186 # If set to 'all', all .la files will be removed unconditionally. This
187 # option is discouraged and shall be used only if 'modules' does not
188 # remove the files.
189 #
190 # If set to 'none', no .la files will be pruned ever. Use in corner
191 # cases only.
192
193 # Determine using IN or OUT source build
194 _check_build_dir() {
195         : ${ECONF_SOURCE:=${S}}
196         # Respect both the old variable and the new one, depending
197         # on which one was set by the ebuild.
198         if [[ ! ${BUILD_DIR} && ${AUTOTOOLS_BUILD_DIR} ]]; then
199                 eqawarn "The AUTOTOOLS_BUILD_DIR variable has been renamed to BUILD_DIR."
200                 eqawarn "Please migrate the ebuild to use the new one."
201
202                 # In the next call, both variables will be set already
203                 # and we'd have to know which one takes precedence.
204                 _RESPECT_AUTOTOOLS_BUILD_DIR=1
205         fi
206
207         if [[ ${_RESPECT_AUTOTOOLS_BUILD_DIR} ]]; then
208                 BUILD_DIR=${AUTOTOOLS_BUILD_DIR:-${WORKDIR}/${P}_build}
209         else
210                 if [[ -n ${AUTOTOOLS_IN_SOURCE_BUILD} ]]; then
211                         : ${BUILD_DIR:=${ECONF_SOURCE}}
212                 else
213                         : ${BUILD_DIR:=${WORKDIR}/${P}_build}
214                 fi
215         fi
216
217         # Backwards compatibility for getting the value.
218         AUTOTOOLS_BUILD_DIR=${BUILD_DIR}
219         echo ">>> Working in BUILD_DIR: \"${BUILD_DIR}\""
220 }
221
222 # @FUNCTION: autotools-utils_src_prepare
223 # @DESCRIPTION:
224 # The src_prepare function.
225 #
226 # Supporting PATCHES array and user patches. See base.eclass(5) for reference.
227 autotools-utils_src_prepare() {
228         debug-print-function ${FUNCNAME} "$@"
229
230         local want_autoreconf=${AUTOTOOLS_AUTORECONF}
231
232         [[ ${PATCHES} ]] && epatch "${PATCHES[@]}"
233
234         at_checksum() {
235                 find '(' -name 'Makefile.am' \
236                         -o -name 'configure.ac' \
237                         -o -name 'configure.in' ')' \
238                         -exec cksum {} + | sort -k2
239         }
240
241         [[ ! ${want_autoreconf} ]] && local checksum=$(at_checksum)
242         epatch_user
243         if [[ ! ${want_autoreconf} ]]; then
244                 if [[ ${checksum} != $(at_checksum) ]]; then
245                         einfo 'Will autoreconfigure due to user patches applied.'
246                         want_autoreconf=yep
247                 fi
248         fi
249
250         [[ ${want_autoreconf} ]] && eautoreconf
251         elibtoolize --patch-only
252 }
253
254 # @FUNCTION: autotools-utils_src_configure
255 # @DESCRIPTION:
256 # The src_configure function. For out of source build it creates build
257 # directory and runs econf there. Configuration parameters defined
258 # in myeconfargs are passed here to econf. Additionally following USE
259 # flags are known:
260 #
261 # IUSE="static-libs" passes --enable-shared and either --disable-static/--enable-static
262 # to econf respectively.
263
264 # @VARIABLE: myeconfargs
265 # @DEFAULT_UNSET
266 # @DESCRIPTION:
267 # Optional econf arguments as Bash array. Should be defined before calling src_configure.
268 # @CODE
269 # src_configure() {
270 #       local myeconfargs=(
271 #               --disable-readline
272 #               --with-confdir="/etc/nasty foo confdir/"
273 #               $(use_enable debug cnddebug)
274 #               $(use_enable threads multithreading)
275 #       )
276 #       autotools-utils_src_configure
277 # }
278 # @CODE
279 autotools-utils_src_configure() {
280         debug-print-function ${FUNCNAME} "$@"
281
282         [[ -z ${myeconfargs+1} || $(declare -p myeconfargs) == 'declare -a'* ]] \
283                 || die 'autotools-utils.eclass: myeconfargs has to be an array.'
284
285         # Common args
286         local econfargs=()
287
288         _check_build_dir
289         if "${ECONF_SOURCE}"/configure --help 2>&1 | grep -q '^ *--docdir='; then
290                 econfargs+=(
291                         --docdir="${EPREFIX}"/usr/share/doc/${PF}
292                 )
293         fi
294
295         # Handle static-libs found in IUSE, disable them by default
296         if in_iuse static-libs; then
297                 econfargs+=(
298                         --enable-shared
299                         $(use_enable static-libs static)
300                 )
301         fi
302
303         # Append user args
304         econfargs+=("${myeconfargs[@]}")
305
306         mkdir -p "${BUILD_DIR}" || die
307         pushd "${BUILD_DIR}" > /dev/null || die
308         econf "${econfargs[@]}" "$@"
309         popd > /dev/null || die
310 }
311
312 # @FUNCTION: autotools-utils_src_compile
313 # @DESCRIPTION:
314 # The autotools src_compile function, invokes emake in specified BUILD_DIR.
315 autotools-utils_src_compile() {
316         debug-print-function ${FUNCNAME} "$@"
317
318         _check_build_dir
319         pushd "${BUILD_DIR}" > /dev/null || die
320         emake "$@" || die 'emake failed'
321         popd > /dev/null || die
322 }
323
324 # @FUNCTION: autotools-utils_src_install
325 # @DESCRIPTION:
326 # The autotools src_install function. Runs emake install, unconditionally
327 # removes unnecessary static libs (based on shouldnotlink libtool property)
328 # and removes unnecessary libtool files when static-libs USE flag is defined
329 # and unset.
330 #
331 # DOCS and HTML_DOCS arrays are supported. See base.eclass(5) for reference.
332 autotools-utils_src_install() {
333         debug-print-function ${FUNCNAME} "$@"
334
335         _check_build_dir
336         pushd "${BUILD_DIR}" > /dev/null || die
337         emake DESTDIR="${D}" "$@" install || die "emake install failed"
338         popd > /dev/null || die
339
340         # XXX: support installing them from builddir as well?
341         if declare -p DOCS &>/dev/null; then
342                 # an empty list == don't install anything
343                 if [[ ${DOCS[@]} ]]; then
344                         # dies by itself
345                         dodoc -r "${DOCS[@]}"
346                 fi
347         else
348                 local f
349                 # same list as in PMS
350                 for f in README* ChangeLog AUTHORS NEWS TODO CHANGES \
351                                 THANKS BUGS FAQ CREDITS CHANGELOG; do
352                         if [[ -s ${f} ]]; then
353                                 dodoc "${f}" || die "(default) dodoc ${f} failed"
354                         fi
355                 done
356         fi
357         if [[ ${HTML_DOCS} ]]; then
358                 dohtml -r "${HTML_DOCS[@]}" || die "dohtml failed"
359         fi
360
361         # Remove libtool files and unnecessary static libs
362         local prune_ltfiles=${AUTOTOOLS_PRUNE_LIBTOOL_FILES}
363         if [[ ${prune_ltfiles} != none ]]; then
364                 prune_libtool_files ${prune_ltfiles:+--${prune_ltfiles}}
365         fi
366 }
367
368 # @FUNCTION: autotools-utils_src_test
369 # @DESCRIPTION:
370 # The autotools src_test function. Runs emake check in build directory.
371 autotools-utils_src_test() {
372         debug-print-function ${FUNCNAME} "$@"
373
374         _check_build_dir
375         pushd "${BUILD_DIR}" > /dev/null || die
376
377         if make -ni check "${@}" &>/dev/null; then
378                 emake check "${@}" || die 'emake check failed.'
379         elif make -ni test "${@}" &>/dev/null; then
380                 emake test "${@}" || die 'emake test failed.'
381         fi
382
383         popd > /dev/null || die
384 }