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