kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / qt4-build-multilib.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: qt4-build-multilib.eclass
5 # @MAINTAINER:
6 # qt@gentoo.org
7 # @AUTHOR:
8 # Davide Pesavento <pesa@gentoo.org>
9 # @BLURB: Eclass for Qt4 split ebuilds with multilib support.
10 # @DESCRIPTION:
11 # This eclass contains various functions that are used when building Qt4.
12 # Requires EAPI 5.
13
14 case ${EAPI} in
15         5)      : ;;
16         *)      die "qt4-build-multilib.eclass: unsupported EAPI=${EAPI:-0}" ;;
17 esac
18
19 inherit eutils flag-o-matic multilib multilib-minimal toolchain-funcs
20
21 HOMEPAGE="https://www.qt.io/"
22 LICENSE="|| ( LGPL-2.1 LGPL-3 GPL-3 ) FDL-1.3"
23 SLOT="4"
24
25 case ${PV} in
26         4.?.9999)
27                 # git stable branch
28                 QT4_BUILD_TYPE="live"
29                 EGIT_BRANCH=${PV%.9999}
30                 ;;
31         *)
32                 # official stable release
33                 QT4_BUILD_TYPE="release"
34                 MY_P=qt-everywhere-opensource-src-${PV/_/-}
35                 SRC_URI="http://download.qt.io/official_releases/qt/${PV%.*}/${PV}/${MY_P}.tar.gz"
36                 S=${WORKDIR}/${MY_P}
37                 ;;
38 esac
39
40 EGIT_REPO_URI=(
41         "git://code.qt.io/qt/qt.git"
42         "https://code.qt.io/git/qt/qt.git"
43         "https://github.com/qtproject/qt.git"
44 )
45 [[ ${QT4_BUILD_TYPE} == live ]] && inherit git-r3
46
47 if [[ ${PN} != qttranslations ]]; then
48         IUSE="aqua debug pch"
49         [[ ${PN} != qtxmlpatterns ]] && IUSE+=" +exceptions"
50 fi
51
52 DEPEND="
53         dev-lang/perl
54         virtual/pkgconfig[${MULTILIB_USEDEP}]
55 "
56 RDEPEND="
57         dev-qt/qtchooser
58         abi_x86_32? ( !app-emulation/emul-linux-x86-qtlibs[-abi_x86_32(-)] )
59 "
60
61
62 # src_{configure,compile,test,install} are inherited from multilib-minimal
63 EXPORT_FUNCTIONS src_unpack src_prepare pkg_postinst pkg_postrm
64
65 multilib_src_configure()        { qt4_multilib_src_configure; }
66 multilib_src_compile()          { qt4_multilib_src_compile; }
67 multilib_src_test()             { qt4_multilib_src_test; }
68 multilib_src_install()          { qt4_multilib_src_install; }
69 multilib_src_install_all()      { qt4_multilib_src_install_all; }
70
71
72 # @ECLASS-VARIABLE: PATCHES
73 # @DEFAULT_UNSET
74 # @DESCRIPTION:
75 # Array variable containing all the patches to be applied. This variable
76 # is expected to be defined in the global scope of ebuilds. Make sure to
77 # specify the full path. This variable is used in src_prepare phase.
78 #
79 # Example:
80 # @CODE
81 #       PATCHES=(
82 #               "${FILESDIR}/mypatch.patch"
83 #               "${FILESDIR}/mypatch2.patch"
84 #       )
85 # @CODE
86
87 # @ECLASS-VARIABLE: QT4_TARGET_DIRECTORIES
88 # @DEFAULT_UNSET
89 # @DESCRIPTION:
90 # Space-separated list of directories that will be configured,
91 # compiled, and installed. All paths must be relative to ${S}.
92
93 # @ECLASS-VARIABLE: QCONFIG_ADD
94 # @DEFAULT_UNSET
95 # @DESCRIPTION:
96 # List of options that must be added to QT_CONFIG in qconfig.pri
97
98 # @ECLASS-VARIABLE: QCONFIG_REMOVE
99 # @DEFAULT_UNSET
100 # @DESCRIPTION:
101 # List of options that must be removed from QT_CONFIG in qconfig.pri
102
103 # @ECLASS-VARIABLE: QCONFIG_DEFINE
104 # @DEFAULT_UNSET
105 # @DESCRIPTION:
106 # List of macros that must be defined in QtCore/qconfig.h
107
108
109 ######  Phase functions  ######
110
111 # @FUNCTION: qt4-build-multilib_src_unpack
112 # @DESCRIPTION:
113 # Unpacks the sources.
114 qt4-build-multilib_src_unpack() {
115         if tc-is-gcc; then
116                 if [[ $(gcc-major-version) -lt 4 ]] || \
117                    [[ $(gcc-major-version) -eq 4 && $(gcc-minor-version) -lt 4 ]]; then
118                         ewarn
119                         ewarn "Using a GCC version lower than 4.4 is not supported"
120                         ewarn
121                 fi
122         fi
123
124         if [[ ${PN} == qtwebkit ]]; then
125                 eshopts_push -s extglob
126                 if is-flagq '-g?(gdb)?([1-9])'; then
127                         ewarn
128                         ewarn "You have enabled debug info (probably have -g or -ggdb in your CFLAGS/CXXFLAGS)."
129                         ewarn "You may experience really long compilation times and/or increased memory usage."
130                         ewarn "If compilation fails, please try removing -g/-ggdb before reporting a bug."
131                         ewarn "For more info check out https://bugs.gentoo.org/307861"
132                         ewarn
133                 fi
134                 eshopts_pop
135         fi
136
137         case ${QT4_BUILD_TYPE} in
138                 live)    git-r3_src_unpack ;;
139                 release) default ;;
140         esac
141 }
142
143 # @FUNCTION: qt4-build-multilib_src_prepare
144 # @DESCRIPTION:
145 # Prepare the sources before the configure phase. Strip CFLAGS if necessary, and fix
146 # the build system in order to respect CFLAGS/CXXFLAGS/LDFLAGS specified in make.conf.
147 qt4-build-multilib_src_prepare() {
148         if [[ ${PN} != qtcore ]]; then
149                 # avoid unnecessary qmake recompilations
150                 sed -i -e 's/^if true;/if false;/' configure \
151                         || die "sed failed (skip qmake bootstrap)"
152         fi
153
154         # skip X11 tests in non-gui packages to avoid spurious dependencies
155         if has ${PN} qtbearer qtcore qtdbus qtscript qtsql qttest qttranslations qtxmlpatterns; then
156                 sed -i -e '/^if.*PLATFORM_X11.*CFG_GUI/,/^fi$/d' configure \
157                         || die "sed failed (skip X11 tests)"
158         fi
159
160         # Qt4 is not safe to build with C++14 (the new gcc-6 default).
161         # Upstream has addressed this for Qt5, but while we continue to
162         # support Qt4, we need to ensure everything is built in C++98 mode.
163         # See bugs 582522, 582618, 583662, 583744.
164         append-cxxflags -std=gnu++98
165
166         if [[ ${PN} == qtcore ]]; then
167                 # Bug 373061
168                 # qmake bus errors with -O2 or -O3 but -O1 works
169                 if [[ ${CHOST} == *86*-apple-darwin* ]]; then
170                         replace-flags -O[23] -O1
171                 fi
172
173                 # Bug 503500
174                 # undefined reference with -Os and --as-needed
175                 if use x86 || use_if_iuse abi_x86_32; then
176                         replace-flags -Os -O2
177                 fi
178         fi
179
180         if [[ ${PN} == qtdeclarative ]]; then
181                 # Bug 551560
182                 # gcc-4.8 ICE with -Os, fixed in 4.9
183                 if use x86 && tc-is-gcc && [[ $(gcc-version) == 4.8 ]]; then
184                         replace-flags -Os -O2
185                 fi
186         fi
187
188         if [[ ${PN} == qtwebkit ]]; then
189                 # Bug 550780
190                 # various ICEs with graphite-related flags, gcc-5 works
191                 if [[ $(gcc-major-version) -lt 5 ]]; then
192                         filter-flags -fgraphite-identity -floop-strip-mine
193                 fi
194         fi
195
196         # Bug 261632
197         if use ppc64; then
198                 append-flags -mminimal-toc
199         fi
200
201         # Teach configure about gcc-6 and later
202         sed -i -e 's:5\*|:[5-9]*|:' \
203                 configure || die "sed gcc version failed"
204
205         # Read also AR from the environment
206         sed -i -e 's/^SYSTEM_VARIABLES="/&AR /' \
207                 configure || die "sed SYSTEM_VARIABLES failed"
208
209         # Reset QMAKE_*FLAGS_{RELEASE,DEBUG} variables,
210         # or they will override the user's flags (via .qmake.cache)
211         sed -i -e '/^SYSTEM_VARIABLES=/ i \
212                 QMakeVar set QMAKE_CFLAGS_RELEASE\
213                 QMakeVar set QMAKE_CFLAGS_DEBUG\
214                 QMakeVar set QMAKE_CXXFLAGS_RELEASE\
215                 QMakeVar set QMAKE_CXXFLAGS_DEBUG\
216                 QMakeVar set QMAKE_LFLAGS_RELEASE\
217                 QMakeVar set QMAKE_LFLAGS_DEBUG\n' \
218                 configure || die "sed QMAKE_*FLAGS_{RELEASE,DEBUG} failed"
219
220         # Drop -nocache from qmake invocation in all configure tests, to ensure that the
221         # correct toolchain and build flags are picked up from config.tests/.qmake.cache
222         find config.tests/unix -name '*.test' -type f -execdir \
223                 sed -i -e '/bin\/qmake/s/-nocache//' '{}' + || die "sed -nocache failed"
224
225         # compile.test needs additional patching so that it doesn't create another cache file
226         # inside the test subdir, which would incorrectly override config.tests/.qmake.cache
227         sed -i -e '/echo.*QT_BUILD_TREE.*\.qmake\.cache/d' \
228                 -e '/bin\/qmake/s/ "$SRCDIR/ "QT_BUILD_TREE=$OUTDIR"&/' \
229                 config.tests/unix/compile.test || die "sed compile.test failed"
230
231         # Delete references to the obsolete /usr/X11R6 directory
232         # On prefix, this also prevents looking at non-prefix stuff
233         sed -i -re '/^QMAKE_(LIB|INC)DIR(_X11|_OPENGL|)\s+/ s/=.*/=/' \
234                 mkspecs/common/linux.conf \
235                 mkspecs/$(qt4_get_mkspec)/qmake.conf \
236                 || die "sed QMAKE_(LIB|INC)DIR failed"
237
238         if use_if_iuse aqua; then
239                 sed -i \
240                         -e '/^CONFIG/s:app_bundle::' \
241                         -e '/^CONFIG/s:plugin_no_soname:plugin_with_soname absolute_library_soname:' \
242                         mkspecs/$(qt4_get_mkspec)/qmake.conf \
243                         || die "sed failed (aqua)"
244
245                 # we are crazy and build cocoa + qt3support
246                 if { ! in_iuse qt3support || use qt3support; } && [[ ${CHOST##*-darwin} -ge 9 ]]; then
247                         sed -i -e "/case \"\$PLATFORM,\$CFG_MAC_COCOA\" in/,/;;/ s|CFG_QT3SUPPORT=\"no\"|CFG_QT3SUPPORT=\"yes\"|" \
248                                 configure || die "sed failed (cocoa + qt3support)"
249                 fi
250         fi
251
252         if [[ ${CHOST} == *-darwin* ]]; then
253                 # Set FLAGS and remove -arch, since our gcc-apple is multilib crippled (by design)
254                 sed -i \
255                         -e "s:QMAKE_CFLAGS_RELEASE.*=.*:QMAKE_CFLAGS_RELEASE=${CFLAGS}:" \
256                         -e "s:QMAKE_CXXFLAGS_RELEASE.*=.*:QMAKE_CXXFLAGS_RELEASE=${CXXFLAGS}:" \
257                         -e "s:QMAKE_LFLAGS_RELEASE.*=.*:QMAKE_LFLAGS_RELEASE=-headerpad_max_install_names ${LDFLAGS}:" \
258                         -e "s:-arch\s\w*::g" \
259                         mkspecs/common/g++-macx.conf \
260                         || die "sed g++-macx.conf failed"
261
262                 # Fix configure's -arch settings that appear in qmake/Makefile and also
263                 # fix arch handling (automagically duplicates our -arch arg and breaks
264                 # pch). Additionally disable Xarch support.
265                 sed -i \
266                         -e "s:-arch i386::" \
267                         -e "s:-arch ppc::" \
268                         -e "s:-arch x86_64::" \
269                         -e "s:-arch ppc64::" \
270                         -e "s:-arch \$i::" \
271                         -e "/if \[ ! -z \"\$NATIVE_64_ARCH\" \]; then/,/fi/ d" \
272                         -e "s:CFG_MAC_XARCH=yes:CFG_MAC_XARCH=no:g" \
273                         -e "s:-Xarch_x86_64::g" \
274                         -e "s:-Xarch_ppc64::g" \
275                         configure mkspecs/common/gcc-base-macx.conf mkspecs/common/g++-macx.conf \
276                         || die "sed -arch/-Xarch failed"
277
278                 # On Snow Leopard don't fall back to 10.5 deployment target.
279                 if [[ ${CHOST} == *-apple-darwin10 ]]; then
280                         sed -i \
281                                 -e "s:QMakeVar set QMAKE_MACOSX_DEPLOYMENT_TARGET.*:QMakeVar set QMAKE_MACOSX_DEPLOYMENT_TARGET 10.6:g" \
282                                 -e "s:-mmacosx-version-min=10.[0-9]:-mmacosx-version-min=10.6:g" \
283                                 configure mkspecs/common/g++-macx.conf \
284                                 || die "sed deployment target failed"
285                 fi
286         fi
287
288         if [[ ${CHOST} == *-solaris* ]]; then
289                 sed -i -e '/^QMAKE_LFLAGS_THREAD/a QMAKE_LFLAGS_DYNAMIC_LIST = -Wl,--dynamic-list,' \
290                         mkspecs/$(qt4_get_mkspec)/qmake.conf || die
291         fi
292
293         # apply patches
294         [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
295         epatch_user
296 }
297
298 qt4_multilib_src_configure() {
299         qt4_prepare_env
300
301         qt4_symlink_tools_to_build_dir
302
303         # toolchain setup ('local -x' because of bug 532510)
304         local -x \
305                 AR="$(tc-getAR) cqs" \
306                 CC=$(tc-getCC) \
307                 CXX=$(tc-getCXX) \
308                 LD=$(tc-getCXX) \
309                 MAKEFLAGS=${MAKEOPTS} \
310                 OBJCOPY=$(tc-getOBJCOPY) \
311                 OBJDUMP=$(tc-getOBJDUMP) \
312                 STRIP=$(tc-getSTRIP)
313
314         # convert tc-arch to the values supported by Qt
315         local arch=$(tc-arch)
316         case ${arch} in
317                 amd64|x64-*)    arch=x86_64 ;;
318                 arm64|hppa)     arch=generic ;;
319                 ppc*-macos)     arch=ppc ;;
320                 ppc*)           arch=powerpc ;;
321                 sparc*)         arch=sparc ;;
322                 x86-macos)      arch=x86 ;;
323                 x86*)           arch=i386 ;;
324         esac
325
326         # configure arguments
327         local conf=(
328                 # installation paths
329                 -prefix "${QT4_PREFIX}"
330                 -bindir "${QT4_BINDIR}"
331                 -libdir "${QT4_LIBDIR}"
332                 -docdir "${QT4_DOCDIR}"
333                 -headerdir "${QT4_HEADERDIR}"
334                 -plugindir "${QT4_PLUGINDIR}"
335                 -importdir "${QT4_IMPORTDIR}"
336                 -datadir "${QT4_DATADIR}"
337                 -translationdir "${QT4_TRANSLATIONDIR}"
338                 -sysconfdir "${QT4_SYSCONFDIR}"
339                 -examplesdir "${QT4_EXAMPLESDIR}"
340                 -demosdir "${QT4_DEMOSDIR}"
341
342                 # debug/release
343                 $(use_if_iuse debug && echo -debug || echo -release)
344                 -no-separate-debug-info
345
346                 # licensing stuff
347                 -opensource -confirm-license
348
349                 # build shared libraries
350                 -shared
351
352                 # skip recursive processing of .pro files at the end of configure
353                 # (we run qmake by ourselves), thus saving quite a bit of time
354                 -dont-process
355
356                 # always enable large file support
357                 -largefile
358
359                 # exceptions USE flag
360                 $(in_iuse exceptions && qt_use exceptions || echo -exceptions)
361
362                 # build STL support
363                 -stl
364
365                 # architecture/platform (mkspec)
366                 -arch ${arch}
367                 -platform $(qt4_get_mkspec)
368
369                 # instruction set support
370                 $(is-flagq -mno-mmx     && echo -no-mmx)
371                 $(is-flagq -mno-3dnow   && echo -no-3dnow)
372                 $(is-flagq -mno-sse     && echo -no-sse)
373                 $(is-flagq -mno-sse2    && echo -no-sse2)
374                 $(is-flagq -mno-sse3    && echo -no-sse3)
375                 $(is-flagq -mno-ssse3   && echo -no-ssse3)
376                 $(is-flagq -mno-sse4.1  && echo -no-sse4.1)
377                 $(is-flagq -mno-sse4.2  && echo -no-sse4.2)
378                 $(is-flagq -mno-avx     && echo -no-avx)
379                 $(is-flagq -mfpu=*      && ! is-flagq -mfpu=*neon* && echo -no-neon)
380
381                 # bug 367045
382                 $([[ ${CHOST} == *86*-apple-darwin* ]] && echo -no-ssse3)
383
384                 # prefer system libraries
385                 -system-zlib
386
387                 # exclude examples and demos from default build
388                 -nomake examples
389                 -nomake demos
390
391                 # disable rpath on non-prefix (bugs 380415 and 417169)
392                 $(usex prefix '' -no-rpath)
393
394                 # print verbose information about each configure test
395                 -verbose
396
397                 # precompiled headers don't work on hardened, where the flag is masked
398                 $(in_iuse pch && qt_use pch || echo -no-pch)
399
400                 # enable linker optimizations to reduce relocations, except on Solaris
401                 # where this flag seems to introduce major breakage to applications,
402                 # mostly to be seen as a core dump with the message:
403                 # "QPixmap: Must construct a QApplication before a QPaintDevice"
404                 $([[ ${CHOST} != *-solaris* ]] && echo -reduce-relocations)
405         )
406
407         if use_if_iuse aqua; then
408                 if [[ ${CHOST##*-darwin} -ge 9 ]]; then
409                         conf+=(
410                                 # on (snow) leopard use the new (frameworked) cocoa code
411                                 -cocoa -framework
412                                 # add hint for the framework location
413                                 -F"${QT4_LIBDIR}"
414                         )
415                 else
416                         conf+=(-no-framework)
417                 fi
418         fi
419
420         conf+=(
421                 # module-specific options
422                 "${myconf[@]}"
423         )
424
425         einfo "Configuring with: ${conf[@]}"
426         "${S}"/configure "${conf[@]}" || die "configure failed"
427
428         # configure is stupid and assigns QMAKE_LFLAGS twice,
429         # thus the previous -rpath-link flag gets overwritten
430         # and some packages (e.g. qthelp) fail to link
431         sed -i -e '/^QMAKE_LFLAGS =/ s:$: $$QMAKE_LFLAGS:' \
432                 .qmake.cache || die "sed .qmake.cache failed"
433
434         qt4_qmake
435         qt4_foreach_target_subdir qt4_qmake
436 }
437
438 qt4_multilib_src_compile() {
439         qt4_prepare_env
440
441         qt4_foreach_target_subdir emake
442 }
443
444 qt4_multilib_src_test() {
445         qt4_prepare_env
446
447         qt4_foreach_target_subdir emake -j1 check
448 }
449
450 qt4_multilib_src_install() {
451         qt4_prepare_env
452
453         qt4_foreach_target_subdir emake INSTALL_ROOT="${D}" install
454
455         if [[ ${PN} == qtcore ]]; then
456                 set -- emake INSTALL_ROOT="${D}" install_{mkspecs,qmake}
457                 einfo "Running $*"
458                 "$@"
459
460                 # install env.d file
461                 cat > "${T}/44qt4-${CHOST}" <<-_EOF_
462                         LDPATH="${QT4_LIBDIR}"
463                 _EOF_
464                 doenvd "${T}/44qt4-${CHOST}"
465
466                 # install qtchooser configuration file
467                 cat > "${T}/qt4-${CHOST}.conf" <<-_EOF_
468                         ${QT4_BINDIR}
469                         ${QT4_LIBDIR}
470                 _EOF_
471
472                 (
473                         insinto /etc/xdg/qtchooser
474                         doins "${T}/qt4-${CHOST}.conf"
475                 )
476
477                 if multilib_is_native_abi; then
478                         # convenience symlinks
479                         dosym qt4-"${CHOST}".conf /etc/xdg/qtchooser/4.conf
480                         dosym qt4-"${CHOST}".conf /etc/xdg/qtchooser/qt4.conf
481                         # TODO bug 522646: write an eselect module to manage default.conf
482                         dosym qt4.conf /etc/xdg/qtchooser/default.conf
483                 fi
484         fi
485
486         # move pkgconfig directory to the correct location
487         if [[ -d ${D}${QT4_LIBDIR}/pkgconfig ]]; then
488                 mv "${D}${QT4_LIBDIR}"/pkgconfig "${ED}usr/$(get_libdir)" || die
489         fi
490
491         qt4_install_module_qconfigs
492         qt4_symlink_framework_headers
493 }
494
495 qt4_multilib_src_install_all() {
496         if [[ ${PN} == qtcore ]]; then
497                 # include gentoo-qconfig.h at the beginning of Qt{,Core}/qconfig.h
498                 if use aqua && [[ ${CHOST#*-darwin} -ge 9 ]]; then
499                         sed -i -e '1i #include <QtCore/Gentoo/gentoo-qconfig.h>\n' \
500                                 "${D}${QT4_LIBDIR}"/QtCore.framework/Headers/qconfig.h \
501                                 || die "sed failed (qconfig.h)"
502                         dosym "${QT4_HEADERDIR#${EPREFIX}}"/Gentoo \
503                                 "${QT4_LIBDIR#${EPREFIX}}"/QtCore.framework/Headers/Gentoo
504                 else
505                         sed -i -e '1i #include <Gentoo/gentoo-qconfig.h>\n' \
506                                 "${D}${QT4_HEADERDIR}"/Qt{,Core}/qconfig.h \
507                                 || die "sed failed (qconfig.h)"
508                 fi
509
510                 dodir "${QT4_DATADIR#${EPREFIX}}"/mkspecs/gentoo
511                 mv "${D}${QT4_DATADIR}"/mkspecs/{qconfig.pri,gentoo/} || die
512         fi
513
514         # install private headers of a few modules
515         if has ${PN} qtcore qtdeclarative qtgui qtscript; then
516                 local moduledir=${PN#qt}
517                 local modulename=Qt$(tr 'a-z' 'A-Z' <<< ${moduledir:0:1})${moduledir:1}
518                 [[ ${moduledir} == core ]] && moduledir=corelib
519
520                 einfo "Installing private headers into ${QT4_HEADERDIR}/${modulename}/private"
521                 insinto "${QT4_HEADERDIR#${EPREFIX}}"/${modulename}/private
522                 find "${S}"/src/${moduledir} -type f -name '*_p.h' -exec doins '{}' + || die
523         fi
524
525         prune_libtool_files
526 }
527
528 # @FUNCTION: qt4-build-multilib_pkg_postinst
529 # @DESCRIPTION:
530 # Regenerate configuration after installation or upgrade/downgrade.
531 qt4-build-multilib_pkg_postinst() {
532         qt4_regenerate_global_qconfigs
533 }
534
535 # @FUNCTION: qt4-build-multilib_pkg_postrm
536 # @DESCRIPTION:
537 # Regenerate configuration when a module is completely removed.
538 qt4-build-multilib_pkg_postrm() {
539         qt4_regenerate_global_qconfigs
540 }
541
542
543 ######  Public helpers  ######
544
545 # @FUNCTION: qt_use
546 # @USAGE: <flag> [feature] [enableval]
547 # @DESCRIPTION:
548 # <flag> is the name of a flag in IUSE.
549 #
550 # Outputs "-${enableval}-${feature}" if <flag> is enabled, "-no-${feature}"
551 # otherwise. If [feature] is not specified, <flag> is used in its place.
552 # If [enableval] is not specified, the "-${enableval}" prefix is omitted.
553 qt_use() {
554         [[ $# -ge 1 ]] || die "${FUNCNAME}() requires at least one argument"
555
556         usex "$1" "${3:+-$3}-${2:-$1}" "-no-${2:-$1}"
557 }
558
559 # @FUNCTION: qt_native_use
560 # @USAGE: <flag> [feature] [enableval]
561 # @DESCRIPTION:
562 # <flag> is the name of a flag in IUSE.
563 #
564 # Outputs "-${enableval}-${feature}" if <flag> is enabled and we are currently
565 # building for the native ABI, "-no-${feature}" otherwise. If [feature] is not
566 # specified, <flag> is used in its place. If [enableval] is not specified,
567 # the "-${enableval}" prefix is omitted.
568 qt_native_use() {
569         [[ $# -ge 1 ]] || die "${FUNCNAME}() requires at least one argument"
570
571         multilib_is_native_abi && qt_use "$@" || echo "-no-${2:-$1}"
572 }
573
574
575 ######  Internal functions  ######
576
577 # @FUNCTION: qt4_prepare_env
578 # @INTERNAL
579 # @DESCRIPTION:
580 # Prepares the environment for building Qt.
581 qt4_prepare_env() {
582         # setup installation directories
583         # note: keep paths in sync with qmake-utils.eclass
584         QT4_PREFIX=${EPREFIX}/usr
585         QT4_HEADERDIR=${QT4_PREFIX}/include/qt4
586         QT4_LIBDIR=${QT4_PREFIX}/$(get_libdir)/qt4
587         QT4_BINDIR=${QT4_LIBDIR}/bin
588         QT4_PLUGINDIR=${QT4_LIBDIR}/plugins
589         QT4_IMPORTDIR=${QT4_LIBDIR}/imports
590         QT4_DATADIR=${QT4_PREFIX}/share/qt4
591         QT4_DOCDIR=${QT4_PREFIX}/share/doc/qt-${PV}
592         QT4_TRANSLATIONDIR=${QT4_DATADIR}/translations
593         QT4_EXAMPLESDIR=${QT4_DATADIR}/examples
594         QT4_DEMOSDIR=${QT4_DATADIR}/demos
595         QT4_SYSCONFDIR=${EPREFIX}/etc/qt4
596
597         # are these still needed?
598         QMAKE_LIBDIR_QT=${QT4_LIBDIR}
599         export XDG_CONFIG_HOME="${T}"
600
601         # can confuse qmake if set (bug 583352)
602         unset QMAKESPEC
603 }
604
605 # @FUNCTION: qt4_foreach_target_subdir
606 # @INTERNAL
607 # @DESCRIPTION:
608 # Executes the given command inside each directory listed in QT4_TARGET_DIRECTORIES.
609 qt4_foreach_target_subdir() {
610         local ret=0 subdir=
611         for subdir in ${QT4_TARGET_DIRECTORIES}; do
612                 mkdir -p "${subdir}" || die
613                 pushd "${subdir}" >/dev/null || die
614
615                 einfo "Running $* ${subdir:+in ${subdir}}"
616                 "$@"
617                 ((ret+=$?))
618
619                 popd >/dev/null || die
620         done
621
622         return ${ret}
623 }
624
625 # @FUNCTION: qt4_symlink_tools_to_build_dir
626 # @INTERNAL
627 # @DESCRIPTION:
628 # Symlinks qtcore tools to BUILD_DIR,
629 # so that they can be used when building other modules.
630 qt4_symlink_tools_to_build_dir() {
631         local tool= tools=()
632         if [[ ${PN} != qtcore ]]; then
633                 tools+=(qmake moc rcc uic)
634         fi
635
636         mkdir -p "${BUILD_DIR}"/bin || die
637         pushd "${BUILD_DIR}"/bin >/dev/null || die
638
639         for tool in "${tools[@]}"; do
640                 [[ -e ${QT4_BINDIR}/${tool} ]] || continue
641                 ln -s "${QT4_BINDIR}/${tool}" . || die "failed to symlink ${tool}"
642         done
643
644         popd >/dev/null || die
645 }
646
647 # @FUNCTION: qt4_qmake
648 # @INTERNAL
649 # @DESCRIPTION:
650 # Helper function that runs qmake in the current target subdir.
651 # Intended to be called by qt4_foreach_target_subdir().
652 qt4_qmake() {
653         local projectdir=${PWD/#${BUILD_DIR}/${S}}
654
655         "${BUILD_DIR}"/bin/qmake \
656                 "${projectdir}" \
657                 CONFIG+=nostrip \
658                 LIBS+=-L"${QT4_LIBDIR}" \
659                 "${myqmakeargs[@]}" \
660                 || die "qmake failed (${projectdir#${S}/})"
661 }
662
663 # @FUNCTION: qt4_install_module_qconfigs
664 # @INTERNAL
665 # @DESCRIPTION:
666 # Creates and installs gentoo-specific ${PN}-qconfig.{h,pri} files.
667 qt4_install_module_qconfigs() {
668         local x
669         if [[ -n ${QCONFIG_ADD} || -n ${QCONFIG_REMOVE} ]]; then
670                 for x in QCONFIG_ADD QCONFIG_REMOVE; do
671                         [[ -n ${!x} ]] && echo ${x}=${!x} >> "${BUILD_DIR}"/${PN}-qconfig.pri
672                 done
673                 insinto ${QT4_DATADIR#${EPREFIX}}/mkspecs/gentoo
674                 doins "${BUILD_DIR}"/${PN}-qconfig.pri
675         fi
676
677         if [[ -n ${QCONFIG_DEFINE} ]]; then
678                 for x in ${QCONFIG_DEFINE}; do
679                         echo "#define ${x}" >> "${BUILD_DIR}"/gentoo-${PN}-qconfig.h
680                 done
681                 insinto ${QT4_HEADERDIR#${EPREFIX}}/Gentoo
682                 doins "${BUILD_DIR}"/gentoo-${PN}-qconfig.h
683         fi
684 }
685
686 # @FUNCTION: qt4_regenerate_global_qconfigs
687 # @INTERNAL
688 # @DESCRIPTION:
689 # Generates Gentoo-specific qconfig.{h,pri} according to the build configuration.
690 # Don't call die here because dying in pkg_post{inst,rm} only makes things worse.
691 qt4_regenerate_global_qconfigs() {
692         if [[ -n ${QCONFIG_ADD} || -n ${QCONFIG_REMOVE} || -n ${QCONFIG_DEFINE} || ${PN} == qtcore ]]; then
693                 local x qconfig_add qconfig_remove qconfig_new
694                 for x in "${ROOT}${QT4_DATADIR}"/mkspecs/gentoo/*-qconfig.pri; do
695                         [[ -f ${x} ]] || continue
696                         qconfig_add+=" $(sed -n 's/^QCONFIG_ADD=//p' "${x}")"
697                         qconfig_remove+=" $(sed -n 's/^QCONFIG_REMOVE=//p' "${x}")"
698                 done
699
700                 if [[ -e "${ROOT}${QT4_DATADIR}"/mkspecs/gentoo/qconfig.pri ]]; then
701                         # start with the qconfig.pri that qtcore installed
702                         if ! cp "${ROOT}${QT4_DATADIR}"/mkspecs/gentoo/qconfig.pri \
703                                 "${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri; then
704                                 eerror "cp qconfig failed."
705                                 return 1
706                         fi
707
708                         # generate list of QT_CONFIG entries from the existing list
709                         # including qconfig_add and excluding qconfig_remove
710                         for x in $(sed -n 's/^QT_CONFIG +=//p' \
711                                 "${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri) ${qconfig_add}; do
712                                         has ${x} ${qconfig_remove} || qconfig_new+=" ${x}"
713                         done
714
715                         # replace the existing QT_CONFIG list with qconfig_new
716                         if ! sed -i -e "s/QT_CONFIG +=.*/QT_CONFIG += ${qconfig_new}/" \
717                                 "${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri; then
718                                 eerror "Sed for QT_CONFIG failed"
719                                 return 1
720                         fi
721
722                         # create Gentoo/qconfig.h
723                         if [[ ! -e ${ROOT}${QT4_HEADERDIR}/Gentoo ]]; then
724                                 if ! mkdir -p "${ROOT}${QT4_HEADERDIR}"/Gentoo; then
725                                         eerror "mkdir ${QT4_HEADERDIR}/Gentoo failed"
726                                         return 1
727                                 fi
728                         fi
729                         : > "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-qconfig.h
730                         for x in "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-*-qconfig.h; do
731                                 [[ -f ${x} ]] || continue
732                                 cat "${x}" >> "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-qconfig.h
733                         done
734                 else
735                         rm -f "${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri
736                         rm -f "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-qconfig.h
737                         rmdir "${ROOT}${QT4_DATADIR}"/mkspecs \
738                                 "${ROOT}${QT4_DATADIR}" \
739                                 "${ROOT}${QT4_HEADERDIR}"/Gentoo \
740                                 "${ROOT}${QT4_HEADERDIR}" 2>/dev/null
741                 fi
742         fi
743 }
744
745 # @FUNCTION: qt4_symlink_framework_headers
746 # @DESCRIPTION:
747 # On OS X we need to add some symlinks when frameworks are being
748 # used, to avoid complications with some more or less stupid packages.
749 qt4_symlink_framework_headers() {
750         if use_if_iuse aqua && [[ ${CHOST##*-darwin} -ge 9 ]]; then
751                 local frw dest f h rdir
752                 # Some packages tend to include <Qt/...>
753                 dodir "${QT4_HEADERDIR#${EPREFIX}}"/Qt
754
755                 # Fake normal headers when frameworks are installed... eases life later
756                 # on, make sure we use relative links though, as some ebuilds assume
757                 # these dirs exist in src_install to add additional files
758                 f=${QT4_HEADERDIR}
759                 h=${QT4_LIBDIR}
760                 while [[ -n ${f} && ${f%%/*} == ${h%%/*} ]] ; do
761                         f=${f#*/}
762                         h=${h#*/}
763                 done
764                 rdir=${h}
765                 f="../"
766                 while [[ ${h} == */* ]] ; do
767                         f="${f}../"
768                         h=${h#*/}
769                 done
770                 rdir="${f}${rdir}"
771
772                 for frw in "${D}${QT4_LIBDIR}"/*.framework; do
773                         [[ -e "${frw}"/Headers ]] || continue
774                         f=$(basename ${frw})
775                         dest="${QT4_HEADERDIR#${EPREFIX}}"/${f%.framework}
776                         dosym "${rdir}"/${f}/Headers "${dest}"
777
778                         # Link normal headers as well.
779                         for hdr in "${D}${QT4_LIBDIR}/${f}"/Headers/*; do
780                                 h=$(basename ${hdr})
781                                 dosym "../${rdir}"/${f}/Headers/${h} \
782                                         "${QT4_HEADERDIR#${EPREFIX}}"/Qt/${h}
783                         done
784                 done
785         fi
786 }
787
788 # @FUNCTION: qt4_get_mkspec
789 # @INTERNAL
790 # @DESCRIPTION:
791 # Returns the right mkspec for the current CHOST/CXX combination.
792 qt4_get_mkspec() {
793         local spec=
794
795         case ${CHOST} in
796                 *-linux*)
797                         spec=linux ;;
798                 *-darwin*)
799                         use_if_iuse aqua &&
800                                 spec=macx ||   # mac with carbon/cocoa
801                                 spec=darwin ;; # darwin/mac with X11
802                 *-freebsd*|*-dragonfly*)
803                         spec=freebsd ;;
804                 *-netbsd*)
805                         spec=netbsd ;;
806                 *-openbsd*)
807                         spec=openbsd ;;
808                 *-aix*)
809                         spec=aix ;;
810                 hppa*-hpux*)
811                         spec=hpux ;;
812                 ia64*-hpux*)
813                         spec=hpuxi ;;
814                 *-solaris*)
815                         spec=solaris ;;
816                 *)
817                         die "qt4-build-multilib.eclass: unsupported CHOST '${CHOST}'" ;;
818         esac
819
820         case $(tc-getCXX) in
821                 *g++*)
822                         spec+=-g++ ;;
823                 *clang*)
824                         if [[ -d ${S}/mkspecs/unsupported/${spec}-clang ]]; then
825                                 spec=unsupported/${spec}-clang
826                         else
827                                 ewarn "${spec}-clang mkspec does not exist, falling back to ${spec}-g++"
828                                 spec+=-g++
829                         fi ;;
830                 *icpc*)
831                         if [[ -d ${S}/mkspecs/${spec}-icc ]]; then
832                                 spec+=-icc
833                         else
834                                 ewarn "${spec}-icc mkspec does not exist, falling back to ${spec}-g++"
835                                 spec+=-g++
836                         fi ;;
837                 *)
838                         die "qt4-build-multilib.eclass: unsupported compiler '$(tc-getCXX)'" ;;
839         esac
840
841         # Add -64 for 64-bit prefix profiles
842         if use amd64-linux || use ppc64-linux ||
843                 use x64-macos ||
844                 use sparc64-solaris || use x64-solaris
845         then
846                 [[ -d ${S}/mkspecs/${spec}-64 ]] && spec+=-64
847         fi
848
849         echo ${spec}
850 }