summaryrefslogtreecommitdiff
blob: 9b3480ea37aa31d96b9a40ad70a7b8ed04fdaf38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/qt4-build-multilib.eclass,v 1.9 2015/04/01 18:45:04 pesa Exp $

# @ECLASS: qt4-build-multilib.eclass
# @MAINTAINER:
# Qt herd <qt@gentoo.org>
# @AUTHOR:
# Davide Pesavento <pesa@gentoo.org>
# @BLURB: Eclass for Qt4 split ebuilds with multilib support.
# @DESCRIPTION:
# This eclass contains various functions that are used when building Qt4.
# Requires EAPI 5.

case ${EAPI} in
	5)	: ;;
	*)	die "qt4-build-multilib.eclass: unsupported EAPI=${EAPI:-0}" ;;
esac

inherit eutils flag-o-matic multilib multilib-minimal toolchain-funcs

HOMEPAGE="https://www.qt.io/"
LICENSE="|| ( LGPL-2.1 GPL-3 )"
SLOT="4"

case ${PV} in
	4.?.9999)
		QT4_BUILD_TYPE="live"
		EGIT_REPO_URI=(
			"git://code.qt.io/qt/qt.git"
			"https://code.qt.io/git/qt/qt.git"
			"https://github.com/qtproject/qt.git"
		)
		EGIT_BRANCH=${PV%.9999}
		inherit git-r3
		;;
	*)
		QT4_BUILD_TYPE="release"
		MY_P=qt-everywhere-opensource-src-${PV/_/-}
		SRC_URI="http://download.qt.io/official_releases/qt/${PV%.*}/${PV}/${MY_P}.tar.gz"
		S=${WORKDIR}/${MY_P}
		;;
esac

if [[ ${PN} != qttranslations ]]; then
	IUSE="aqua debug pch"
	[[ ${PN} != qtxmlpatterns ]] && IUSE+=" +exceptions"
fi

DEPEND="
	dev-lang/perl
	virtual/pkgconfig[${MULTILIB_USEDEP}]
"
RDEPEND="
	dev-qt/qtchooser
	abi_x86_32? ( !app-emulation/emul-linux-x86-qtlibs[-abi_x86_32(-)] )
"


# src_{configure,compile,test,install} are inherited from multilib-minimal
EXPORT_FUNCTIONS src_unpack src_prepare pkg_postinst pkg_postrm

multilib_src_configure()	{ qt4_multilib_src_configure; }
multilib_src_compile()		{ qt4_multilib_src_compile; }
multilib_src_test()		{ qt4_multilib_src_test; }
multilib_src_install()		{ qt4_multilib_src_install; }
multilib_src_install_all()	{ qt4_multilib_src_install_all; }


# @ECLASS-VARIABLE: PATCHES
# @DEFAULT_UNSET
# @DESCRIPTION:
# Array variable containing all the patches to be applied. This variable
# is expected to be defined in the global scope of ebuilds. Make sure to
# specify the full path. This variable is used in src_prepare phase.
#
# Example:
# @CODE
#	PATCHES=(
#		"${FILESDIR}/mypatch.patch"
#		"${FILESDIR}/mypatch2.patch"
#	)
# @CODE

# @ECLASS-VARIABLE: QT4_TARGET_DIRECTORIES
# @DEFAULT_UNSET
# @DESCRIPTION:
# Space-separated list of directories that will be configured,
# compiled, and installed. All paths must be relative to ${S}.

# @ECLASS-VARIABLE: QT4_VERBOSE_BUILD
# @DESCRIPTION:
# Set to false to reduce build output during compilation.
: ${QT4_VERBOSE_BUILD:=true}

# @ECLASS-VARIABLE: QCONFIG_ADD
# @DEFAULT_UNSET
# @DESCRIPTION:
# List of options that must be added to QT_CONFIG in qconfig.pri

# @ECLASS-VARIABLE: QCONFIG_REMOVE
# @DEFAULT_UNSET
# @DESCRIPTION:
# List of options that must be removed from QT_CONFIG in qconfig.pri

# @ECLASS-VARIABLE: QCONFIG_DEFINE
# @DEFAULT_UNSET
# @DESCRIPTION:
# List of macros that must be defined in QtCore/qconfig.h

# @FUNCTION: qt4-build-multilib_src_unpack
# @DESCRIPTION:
# Unpacks the sources.
qt4-build-multilib_src_unpack() {
	if [[ $(gcc-major-version) -lt 4 ]] || [[ $(gcc-major-version) -eq 4 && $(gcc-minor-version) -lt 4 ]]; then
		ewarn
		ewarn "Using a GCC version lower than 4.4 is not supported."
		ewarn
	fi

	if [[ ${PN} == qtwebkit ]]; then
		eshopts_push -s extglob
		if is-flagq '-g?(gdb)?([1-9])'; then
			ewarn
			ewarn "You have enabled debug info (probably have -g or -ggdb in your CFLAGS/CXXFLAGS)."
			ewarn "You may experience really long compilation times and/or increased memory usage."
			ewarn "If compilation fails, please try removing -g/-ggdb before reporting a bug."
			ewarn "For more info check out https://bugs.gentoo.org/307861"
			ewarn
		fi
		eshopts_pop
	fi

	case ${QT4_BUILD_TYPE} in
		live)    git-r3_src_unpack ;;
		release) default ;;
	esac
}

# @FUNCTION: qt4-build-multilib_src_prepare
# @DESCRIPTION:
# Prepare the sources before the configure phase. Strip CFLAGS if necessary, and fix
# the build system in order to respect CFLAGS/CXXFLAGS/LDFLAGS specified in make.conf.
qt4-build-multilib_src_prepare() {
	if [[ ${PN} != qtcore ]]; then
		# avoid unnecessary qmake recompilations
		sed -i -e 's/^if true;/if false;/' configure \
			|| die "sed failed (skip qmake bootstrap)"
	fi

	# skip X11 tests in non-gui packages to avoid spurious dependencies
	if has ${PN} qtbearer qtcore qtdbus qtscript qtsql qttest qttranslations qtxmlpatterns; then
		sed -i -e '/^if.*PLATFORM_X11.*CFG_GUI/,/^fi$/d' configure \
			|| die "sed failed (skip X11 tests)"
	fi

	if use_if_iuse aqua; then
		sed -i \
			-e '/^CONFIG/s:app_bundle::' \
			-e '/^CONFIG/s:plugin_no_soname:plugin_with_soname absolute_library_soname:' \
			mkspecs/$(qt4_get_mkspec)/qmake.conf \
			|| die "sed failed (aqua)"

		# we are crazy and build cocoa + qt3support
		if { ! in_iuse qt3support || use qt3support; } && [[ ${CHOST##*-darwin} -ge 9 ]]; then
			sed -i -e "/case \"\$PLATFORM,\$CFG_MAC_COCOA\" in/,/;;/ s|CFG_QT3SUPPORT=\"no\"|CFG_QT3SUPPORT=\"yes\"|" \
				configure || die "sed failed (cocoa + qt3support)"
		fi
	fi

	if [[ ${PN} == qtcore ]]; then
		# Bug 373061
		# qmake bus errors with -O2 or -O3 but -O1 works
		if [[ ${CHOST} == *86*-apple-darwin* ]]; then
			replace-flags -O[23] -O1
		fi

		# Bug 503500
		# undefined reference with -Os and --as-needed
		if use x86 || use_if_iuse abi_x86_32; then
			replace-flags -Os -O2
		fi
	fi

	# Bug 261632
	if use ppc64; then
		append-flags -mminimal-toc
	fi

	# Bug 417105
	# graphite on gcc 4.7 causes miscompilations
	if [[ $(gcc-version) == "4.7" ]]; then
		filter-flags -fgraphite-identity
	fi

	# Read also AR from the environment
	sed -i -e 's/^SYSTEM_VARIABLES="/&AR /' \
		configure || die "sed SYSTEM_VARIABLES failed"

	# Reset QMAKE_*FLAGS_{RELEASE,DEBUG} variables,
	# or they will override user's flags (.qmake.cache)
	sed -i -e '/^SYSTEM_VARIABLES=/ i \
		QMakeVar set QMAKE_CFLAGS_RELEASE\
		QMakeVar set QMAKE_CFLAGS_DEBUG\
		QMakeVar set QMAKE_CXXFLAGS_RELEASE\
		QMakeVar set QMAKE_CXXFLAGS_DEBUG\
		QMakeVar set QMAKE_LFLAGS_RELEASE\
		QMakeVar set QMAKE_LFLAGS_DEBUG\n' \
		configure || die "sed QMAKE_*FLAGS_{RELEASE,DEBUG} failed"

	# Respect CC, CXX, LINK and *FLAGS in config.tests
	find config.tests/unix -name '*.test' -type f -print0 | xargs -0 \
		sed -i -e "/bin\/qmake/ s: \"\$SRCDIR/: \
			'QMAKE_CC=$(tc-getCC)'    'QMAKE_CXX=$(tc-getCXX)'      'QMAKE_LINK=$(tc-getCXX)' \
			'QMAKE_CFLAGS+=${CFLAGS}' 'QMAKE_CXXFLAGS+=${CXXFLAGS}' 'QMAKE_LFLAGS+=${LDFLAGS}'&:" \
		|| die "sed config.tests failed"

	# Bug 172219
	sed -e 's:/X11R6/:/:' -i mkspecs/$(qt4_get_mkspec)/qmake.conf || die

	if [[ ${CHOST} == *-darwin* ]]; then
		# Set FLAGS and remove -arch, since our gcc-apple is multilib crippled (by design)
		sed -i \
			-e "s:QMAKE_CFLAGS_RELEASE.*=.*:QMAKE_CFLAGS_RELEASE=${CFLAGS}:" \
			-e "s:QMAKE_CXXFLAGS_RELEASE.*=.*:QMAKE_CXXFLAGS_RELEASE=${CXXFLAGS}:" \
			-e "s:QMAKE_LFLAGS_RELEASE.*=.*:QMAKE_LFLAGS_RELEASE=-headerpad_max_install_names ${LDFLAGS}:" \
			-e "s:-arch\s\w*::g" \
			mkspecs/common/g++-macx.conf \
			|| die "sed g++-macx.conf failed"

		# Fix configure's -arch settings that appear in qmake/Makefile and also
		# fix arch handling (automagically duplicates our -arch arg and breaks
		# pch). Additionally disable Xarch support.
		sed -i \
			-e "s:-arch i386::" \
			-e "s:-arch ppc::" \
			-e "s:-arch x86_64::" \
			-e "s:-arch ppc64::" \
			-e "s:-arch \$i::" \
			-e "/if \[ ! -z \"\$NATIVE_64_ARCH\" \]; then/,/fi/ d" \
			-e "s:CFG_MAC_XARCH=yes:CFG_MAC_XARCH=no:g" \
			-e "s:-Xarch_x86_64::g" \
			-e "s:-Xarch_ppc64::g" \
			configure mkspecs/common/gcc-base-macx.conf mkspecs/common/g++-macx.conf \
			|| die "sed -arch/-Xarch failed"

		# On Snow Leopard don't fall back to 10.5 deployment target.
		if [[ ${CHOST} == *-apple-darwin10 ]]; then
			sed -i \
				-e "s:QMakeVar set QMAKE_MACOSX_DEPLOYMENT_TARGET.*:QMakeVar set QMAKE_MACOSX_DEPLOYMENT_TARGET 10.6:g" \
				-e "s:-mmacosx-version-min=10.[0-9]:-mmacosx-version-min=10.6:g" \
				configure mkspecs/common/g++-macx.conf \
				|| die "sed deployment target failed"
		fi
	fi

	# this is needed for all systems with a separate -liconv, except
	# Darwin, for which the sources already cater for -liconv
	if use !elibc_glibc && [[ ${CHOST} != *-darwin* ]]; then
		sed -e 's|mac:\(LIBS += -liconv\)|\1|g' \
			-i config.tests/unix/iconv/iconv.pro \
			|| die "sed iconv.pro failed"
	fi

	# we need some patches for Solaris
	sed -i -e '/^QMAKE_LFLAGS_THREAD/a\QMAKE_LFLAGS_DYNAMIC_LIST = -Wl,--dynamic-list,' \
		mkspecs/$(qt4_get_mkspec)/qmake.conf || die
	# use GCC over SunStudio
	sed -i -e '/PLATFORM=solaris-cc/s/cc/g++/' configure || die
	# do not flirt with non-Prefix stuff, we're quite possessive
	sed -i -e '/^QMAKE_\(LIB\|INC\)DIR\(_X11\|_OPENGL\|\)\t/s/=.*$/=/' \
		mkspecs/$(qt4_get_mkspec)/qmake.conf || die

	# apply patches
	[[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
	epatch_user
}

qt4_multilib_src_configure() {
	qt4_prepare_env

	qt4_symlink_tools_to_build_dir

	# toolchain setup ('local -x' because of bug 532510)
	local -x \
		AR="$(tc-getAR) cqs" \
		CC=$(tc-getCC) \
		CXX=$(tc-getCXX) \
		LD=$(tc-getCXX) \
		OBJCOPY=$(tc-getOBJCOPY) \
		STRIP=$(tc-getSTRIP)

	# convert tc-arch to the values supported by Qt
	local arch=
	case $(tc-arch) in
		amd64|x64-*)		  arch=x86_64 ;;
		ppc*-macos)		  arch=ppc ;;
		ppc*)			  arch=powerpc ;;
		sparc*)			  arch=sparc ;;
		x86-macos)		  arch=x86 ;;
		x86*)			  arch=i386 ;;
		alpha|arm|ia64|mips|s390) arch=$(tc-arch) ;;
		arm64|hppa|sh)		  arch=generic ;;
		*) die "qt4-build-multilib.eclass: unsupported tc-arch '$(tc-arch)'" ;;
	esac

	# configure arguments
	local conf=(
		# installation paths
		-prefix "${QT4_PREFIX}"
		-bindir "${QT4_BINDIR}"
		-libdir "${QT4_LIBDIR}"
		-docdir "${QT4_DOCDIR}"
		-headerdir "${QT4_HEADERDIR}"
		-plugindir "${QT4_PLUGINDIR}"
		-importdir "${QT4_IMPORTDIR}"
		-datadir "${QT4_DATADIR}"
		-translationdir "${QT4_TRANSLATIONDIR}"
		-sysconfdir "${QT4_SYSCONFDIR}"
		-examplesdir "${QT4_EXAMPLESDIR}"
		-demosdir "${QT4_DEMOSDIR}"

		# debug/release
		$(use_if_iuse debug && echo -debug || echo -release)
		-no-separate-debug-info

		# licensing stuff
		-opensource -confirm-license

		# build shared libraries
		-shared

		# skip recursive processing of .pro files at the end of configure
		# (we run qmake by ourselves), thus saving quite a bit of time
		-dont-process

		# always enable large file support
		-largefile

		# exceptions USE flag
		$(in_iuse exceptions && qt_use exceptions || echo -exceptions)

		# build STL support
		-stl

		# architecture/platform (mkspec)
		-arch ${arch}
		-platform $(qt4_get_mkspec)

		# prefer system libraries
		-system-zlib

		# exclude examples and demos from default build
		-nomake examples
		-nomake demos

		# disable rpath on non-prefix (bugs 380415 and 417169)
		$(use prefix || echo -no-rpath)

		# verbosity of the configure and build phases
		-verbose $(${QT4_VERBOSE_BUILD} || echo -silent)

		# precompiled headers don't work on hardened, where the flag is masked
		$(in_iuse pch && qt_use pch || echo -no-pch)

		# enable linker optimizations to reduce relocations, except on Solaris
		# where this flag seems to introduce major breakage to applications,
		# mostly to be seen as a core dump with the message:
		# "QPixmap: Must construct a QApplication before a QPaintDevice"
		$([[ ${CHOST} != *-solaris* ]] && echo -reduce-relocations)

		# this one is needed for all systems with a separate -liconv, apart from
		# Darwin, for which the sources already cater for -liconv
		$(use !elibc_glibc && [[ ${CHOST} != *-darwin* ]] && echo -liconv)
	)

	if use_if_iuse aqua; then
		if [[ ${CHOST##*-darwin} -ge 9 ]]; then
			conf+=(
				# on (snow) leopard use the new (frameworked) cocoa code
				-cocoa -framework
				# add hint for the framework location
				-F"${QT4_LIBDIR}"
			)
		else
			conf+=(-no-framework)
		fi
	fi

	conf+=(
		# module-specific options
		"${myconf[@]}"
	)

	einfo "Configuring with: ${conf[@]}"
	"${S}"/configure "${conf[@]}" || die "configure failed"

	# configure is stupid and assigns QMAKE_LFLAGS twice,
	# thus the previous -rpath-link flag gets overwritten
	# and some packages (e.g. qthelp) fail to link
	sed -i -e '/^QMAKE_LFLAGS =/ s:$: $$QMAKE_LFLAGS:' \
		.qmake.cache || die "sed .qmake.cache failed"

	qt4_qmake
	qt4_foreach_target_subdir qt4_qmake
}

qt4_multilib_src_compile() {
	qt4_prepare_env

	qt4_foreach_target_subdir emake
}

qt4_multilib_src_test() {
	qt4_prepare_env

	qt4_foreach_target_subdir emake -j1 check
}

qt4_multilib_src_install() {
	qt4_prepare_env

	qt4_foreach_target_subdir emake INSTALL_ROOT="${D}" install

	if [[ ${PN} == qtcore ]]; then
		set -- emake INSTALL_ROOT="${D}" install_{mkspecs,qmake}
		einfo "Running $*"
		"$@"

		# install env.d file
		cat > "${T}/44qt4-${CHOST}" <<-_EOF_
			LDPATH="${QT4_LIBDIR}"
		_EOF_
		doenvd "${T}/44qt4-${CHOST}"

		# install qtchooser configuration file
		cat > "${T}/qt4-${CHOST}.conf" <<-_EOF_
			${QT4_BINDIR}
			${QT4_LIBDIR}
		_EOF_

		(
			insinto /etc/xdg/qtchooser
			doins "${T}/qt4-${CHOST}.conf"
		)

		if multilib_is_native_abi; then
			# convenience symlinks
			dosym qt4-"${CHOST}".conf /etc/xdg/qtchooser/4.conf
			dosym qt4-"${CHOST}".conf /etc/xdg/qtchooser/qt4.conf
			# TODO bug 522646: write an eselect module to manage default.conf
			dosym qt4.conf /etc/xdg/qtchooser/default.conf
		fi
	fi

	install_qconfigs
	fix_library_files
	fix_includes
}

qt4_multilib_src_install_all() {
	if [[ ${PN} == qtcore ]]; then
		# include gentoo-qconfig.h at the beginning of Qt{,Core}/qconfig.h
		if use aqua && [[ ${CHOST#*-darwin} -ge 9 ]]; then
			sed -i -e '1i #include <QtCore/Gentoo/gentoo-qconfig.h>\n' \
				"${D}${QT4_LIBDIR}"/QtCore.framework/Headers/qconfig.h \
				|| die "sed failed (qconfig.h)"
			dosym "${QT4_HEADERDIR#${EPREFIX}}"/Gentoo \
				"${QT4_LIBDIR#${EPREFIX}}"/QtCore.framework/Headers/Gentoo
		else
			sed -i -e '1i #include <Gentoo/gentoo-qconfig.h>\n' \
				"${D}${QT4_HEADERDIR}"/Qt{,Core}/qconfig.h \
				|| die "sed failed (qconfig.h)"
		fi

		dodir "${QT4_DATADIR#${EPREFIX}}"/mkspecs/gentoo
		mv "${D}${QT4_DATADIR}"/mkspecs/{qconfig.pri,gentoo/} || die
	fi

	# install private headers of a few modules
	if has ${PN} qtcore qtdeclarative qtgui qtscript; then
		local moduledir=${PN#qt}
		local modulename=Qt$(tr 'a-z' 'A-Z' <<< ${moduledir:0:1})${moduledir:1}
		[[ ${moduledir} == core ]] && moduledir=corelib

		einfo "Installing private headers into ${QT4_HEADERDIR}/${modulename}/private"
		insinto "${QT4_HEADERDIR#${EPREFIX}}"/${modulename}/private
		find "${S}"/src/${moduledir} -type f -name '*_p.h' -exec doins '{}' + || die
	fi

	# remove .la files since we are building only shared libraries
	prune_libtool_files
}

# @FUNCTION: qt4-build-multilib_pkg_postinst
# @DESCRIPTION:
# Regenerate configuration, plus throw a message about possible
# breakages and proposed solutions.
qt4-build-multilib_pkg_postinst() {
	generate_qconfigs
}

# @FUNCTION: qt4-build-multilib_pkg_postrm
# @DESCRIPTION:
# Regenerate configuration when the package is completely removed.
qt4-build-multilib_pkg_postrm() {
	generate_qconfigs
}

# @FUNCTION: qt_use
# @USAGE: <flag> [feature] [enableval]
# @DESCRIPTION:
# Outputs "-${enableval}-${feature}" if <flag> is enabled, "-no-${feature}"
# otherwise. If [feature] is not specified, <flag> is used in its place.
# If [enableval] is not specified, the "-${enableval}" prefix is omitted.
qt_use() {
	use "$1" && echo "${3:+-$3}-${2:-$1}" || echo "-no-${2:-$1}"
}

# @FUNCTION: qt_native_use
# @USAGE: <flag> [feature] [enableval]
# @DESCRIPTION:
# Outputs "-${enableval}-${feature}" if <flag> is enabled and we are currently
# building for the native ABI, "-no-${feature}" otherwise. If [feature] is not
# specified, <flag> is used in its place. If [enableval] is not specified,
# the "-${enableval}" prefix is omitted.
qt_native_use() {
	multilib_is_native_abi && use "$1" && echo "${3:+-$3}-${2:-$1}" || echo "-no-${2:-$1}"
}


######  Internal functions  ######

# @FUNCTION: qt4_prepare_env
# @INTERNAL
# @DESCRIPTION:
# Prepares the environment for building Qt.
qt4_prepare_env() {
	# setup installation directories
	QT4_PREFIX=${EPREFIX}/usr
	QT4_HEADERDIR=${QT4_PREFIX}/include/qt4
	QT4_LIBDIR=${QT4_PREFIX}/$(get_libdir)/qt4
	QT4_PCDIR=${QT4_PREFIX}/$(get_libdir)/pkgconfig
	QT4_BINDIR=${QT4_LIBDIR}/bin
	QT4_PLUGINDIR=${QT4_LIBDIR}/plugins
	QT4_IMPORTDIR=${QT4_LIBDIR}/imports
	QT4_DATADIR=${QT4_PREFIX}/share/qt4
	QT4_DOCDIR=${QT4_PREFIX}/share/doc/qt-${PV}
	QT4_TRANSLATIONDIR=${QT4_DATADIR}/translations
	QT4_EXAMPLESDIR=${QT4_DATADIR}/examples
	QT4_DEMOSDIR=${QT4_DATADIR}/demos
	QT4_SYSCONFDIR=${EPREFIX}/etc/qt4
	QMAKE_LIBDIR_QT=${QT4_LIBDIR}

	export XDG_CONFIG_HOME="${T}"
}

# @FUNCTION: qt4_foreach_target_subdir
# @INTERNAL
# @DESCRIPTION:
# Executes the given command inside each directory listed in QT4_TARGET_DIRECTORIES.
qt4_foreach_target_subdir() {
	local subdir
	for subdir in ${QT4_TARGET_DIRECTORIES}; do
		mkdir -p "${subdir}" || die
		pushd "${subdir}" >/dev/null || die

		einfo "Running $* ${subdir:+in ${subdir}}"
		"$@"

		popd >/dev/null || die
	done
}

# @FUNCTION: qt4_symlink_tools_to_build_dir
# @INTERNAL
# @DESCRIPTION:
# Symlinks qtcore tools to BUILD_DIR,
# so that they can be used when building other modules.
qt4_symlink_tools_to_build_dir() {
	local tool= tools=()
	if [[ ${PN} != qtcore ]]; then
		tools+=(qmake moc rcc uic)
	fi

	mkdir -p "${BUILD_DIR}"/bin || die
	pushd "${BUILD_DIR}"/bin >/dev/null || die

	for tool in "${tools[@]}"; do
		[[ -e ${QT4_BINDIR}/${tool} ]] || continue
		ln -s "${QT4_BINDIR}/${tool}" . || die "failed to symlink ${tool}"
	done

	popd >/dev/null || die
}

# @FUNCTION: qt4_qmake
# @INTERNAL
# @DESCRIPTION:
# Helper function that runs qmake in the current target subdir.
# Intended to be called by qt4_foreach_target_subdir().
qt4_qmake() {
	local projectdir=${PWD/#${BUILD_DIR}/${S}}

	"${BUILD_DIR}"/bin/qmake \
		CONFIG+=nostrip \
		LIBS+=-L"${QT4_LIBDIR}" \
		"${projectdir}" \
		|| die "qmake failed (${projectdir})"
}

# @FUNCTION: install_qconfigs
# @INTERNAL
# @DESCRIPTION:
# Install gentoo-specific mkspecs configurations.
install_qconfigs() {
	local x
	if [[ -n ${QCONFIG_ADD} || -n ${QCONFIG_REMOVE} ]]; then
		for x in QCONFIG_ADD QCONFIG_REMOVE; do
			[[ -n ${!x} ]] && echo ${x}=${!x} >> "${BUILD_DIR}"/${PN}-qconfig.pri
		done
		insinto ${QT4_DATADIR#${EPREFIX}}/mkspecs/gentoo
		doins "${BUILD_DIR}"/${PN}-qconfig.pri
	fi

	if [[ -n ${QCONFIG_DEFINE} ]]; then
		for x in ${QCONFIG_DEFINE}; do
			echo "#define ${x}" >> "${BUILD_DIR}"/gentoo-${PN}-qconfig.h
		done
		insinto ${QT4_HEADERDIR#${EPREFIX}}/Gentoo
		doins "${BUILD_DIR}"/gentoo-${PN}-qconfig.h
	fi
}

# @FUNCTION: generate_qconfigs
# @INTERNAL
# @DESCRIPTION:
# Generates gentoo-specific qconfig.{h,pri}.
generate_qconfigs() {
	if [[ -n ${QCONFIG_ADD} || -n ${QCONFIG_REMOVE} || -n ${QCONFIG_DEFINE} || ${PN} == qtcore ]]; then
		local x qconfig_add qconfig_remove qconfig_new
		for x in "${ROOT}${QT4_DATADIR}"/mkspecs/gentoo/*-qconfig.pri; do
			[[ -f ${x} ]] || continue
			qconfig_add+=" $(sed -n 's/^QCONFIG_ADD=//p' "${x}")"
			qconfig_remove+=" $(sed -n 's/^QCONFIG_REMOVE=//p' "${x}")"
		done

		# these error checks do not use die because dying in pkg_post{inst,rm}
		# just makes things worse.
		if [[ -e "${ROOT}${QT4_DATADIR}"/mkspecs/gentoo/qconfig.pri ]]; then
			# start with the qconfig.pri that qtcore installed
			if ! cp "${ROOT}${QT4_DATADIR}"/mkspecs/gentoo/qconfig.pri \
				"${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri; then
				eerror "cp qconfig failed."
				return 1
			fi

			# generate list of QT_CONFIG entries from the existing list
			# including qconfig_add and excluding qconfig_remove
			for x in $(sed -n 's/^QT_CONFIG +=//p' \
				"${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri) ${qconfig_add}; do
					has ${x} ${qconfig_remove} || qconfig_new+=" ${x}"
			done

			# replace the existing QT_CONFIG list with qconfig_new
			if ! sed -i -e "s/QT_CONFIG +=.*/QT_CONFIG += ${qconfig_new}/" \
				"${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri; then
				eerror "Sed for QT_CONFIG failed"
				return 1
			fi

			# create Gentoo/qconfig.h
			if [[ ! -e ${ROOT}${QT4_HEADERDIR}/Gentoo ]]; then
				if ! mkdir -p "${ROOT}${QT4_HEADERDIR}"/Gentoo; then
					eerror "mkdir ${QT4_HEADERDIR}/Gentoo failed"
					return 1
				fi
			fi
			: > "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-qconfig.h
			for x in "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-*-qconfig.h; do
				[[ -f ${x} ]] || continue
				cat "${x}" >> "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-qconfig.h
			done
		else
			rm -f "${ROOT}${QT4_DATADIR}"/mkspecs/qconfig.pri
			rm -f "${ROOT}${QT4_HEADERDIR}"/Gentoo/gentoo-qconfig.h
			rmdir "${ROOT}${QT4_DATADIR}"/mkspecs \
				"${ROOT}${QT4_DATADIR}" \
				"${ROOT}${QT4_HEADERDIR}"/Gentoo \
				"${ROOT}${QT4_HEADERDIR}" 2>/dev/null
		fi
	fi
}

# @FUNCTION: fix_library_files
# @INTERNAL
# @DESCRIPTION:
# Fixes the paths in *.prl and *.pc, as they are wrong due to sandbox, and
# moves the *.pc files into the pkgconfig directory.
fix_library_files() {
	local libfile
	for libfile in "${D}"/${QT4_LIBDIR}/{*.prl,pkgconfig/*.pc}; do
		if [[ -e ${libfile} ]]; then
			sed -i -e "s:${S}/lib:${QT4_LIBDIR}:g" ${libfile} || die "sed on ${libfile} failed"
		fi
	done

	# pkgconfig files refer to WORKDIR/bin as the moc and uic locations
	for libfile in "${D}"/${QT4_LIBDIR}/pkgconfig/*.pc; do
		if [[ -e ${libfile} ]]; then
			sed -i -e "s:${S}/bin:${QT4_BINDIR}:g" ${libfile} || die "sed on ${libfile} failed"

		# Move .pc files into the pkgconfig directory
		dodir ${QT4_PCDIR#${EPREFIX}}
		mv ${libfile} "${D}"/${QT4_PCDIR}/ || die "moving ${libfile} to ${D}/${QT4_PCDIR}/ failed"
		fi
	done

	# Don't install an empty directory
	rmdir "${D}"/${QT4_LIBDIR}/pkgconfig
}

# @FUNCTION: fix_includes
# @DESCRIPTION:
# For MacOS X we need to add some symlinks when frameworks are
# being used, to avoid complications with some more or less stupid packages.
fix_includes() {
	if use_if_iuse aqua && [[ ${CHOST##*-darwin} -ge 9 ]]; then
		local frw dest f h rdir
		# Some packages tend to include <Qt/...>
		dodir "${QT4_HEADERDIR#${EPREFIX}}"/Qt

		# Fake normal headers when frameworks are installed... eases life later
		# on, make sure we use relative links though, as some ebuilds assume
		# these dirs exist in src_install to add additional files
		f=${QT4_HEADERDIR}
		h=${QT4_LIBDIR}
		while [[ -n ${f} && ${f%%/*} == ${h%%/*} ]] ; do
			f=${f#*/}
			h=${h#*/}
		done
		rdir=${h}
		f="../"
		while [[ ${h} == */* ]] ; do
			f="${f}../"
			h=${h#*/}
		done
		rdir="${f}${rdir}"

		for frw in "${D}${QT4_LIBDIR}"/*.framework; do
			[[ -e "${frw}"/Headers ]] || continue
			f=$(basename ${frw})
			dest="${QT4_HEADERDIR#${EPREFIX}}"/${f%.framework}
			dosym "${rdir}"/${f}/Headers "${dest}"

			# Link normal headers as well.
			for hdr in "${D}/${QT4_LIBDIR}/${f}"/Headers/*; do
				h=$(basename ${hdr})
				dosym "../${rdir}"/${f}/Headers/${h} \
					"${QT4_HEADERDIR#${EPREFIX}}"/Qt/${h}
			done
		done
	fi
}

# @FUNCTION: qt4_get_mkspec
# @INTERNAL
# @DESCRIPTION:
# Returns the right mkspec for the current CHOST/CXX combination.
qt4_get_mkspec() {
	local spec=

	case ${CHOST} in
		*-linux*)
			spec=linux ;;
		*-darwin*)
			use_if_iuse aqua &&
				spec=macx ||   # mac with carbon/cocoa
				spec=darwin ;; # darwin/mac with X11
		*-freebsd*|*-dragonfly*)
			spec=freebsd ;;
		*-netbsd*)
			spec=netbsd ;;
		*-openbsd*)
			spec=openbsd ;;
		*-aix*)
			spec=aix ;;
		hppa*-hpux*)
			spec=hpux ;;
		ia64*-hpux*)
			spec=hpuxi ;;
		*-solaris*)
			spec=solaris ;;
		*)
			die "qt4-build-multilib.eclass: unsupported CHOST '${CHOST}'" ;;
	esac

	case $(tc-getCXX) in
		*g++*)
			spec+=-g++ ;;
		*clang*)
			if [[ -d ${S}/mkspecs/unsupported/${spec}-clang ]]; then
				spec=unsupported/${spec}-clang
			else
				ewarn "${spec}-clang mkspec does not exist, falling back to ${spec}-g++"
				spec+=-g++
			fi ;;
		*icpc*)
			if [[ -d ${S}/mkspecs/${spec}-icc ]]; then
				spec+=-icc
			else
				ewarn "${spec}-icc mkspec does not exist, falling back to ${spec}-g++"
				spec+=-g++
			fi ;;
		*)
			die "qt4-build-multilib.eclass: unsupported compiler '$(tc-getCXX)'" ;;
	esac

	# Add -64 for 64-bit prefix profiles
	if use amd64-linux || use ia64-linux || use ppc64-linux ||
		use x64-macos ||
		use sparc64-freebsd || use x64-freebsd || use x64-openbsd ||
		use ia64-hpux ||
		use sparc64-solaris || use x64-solaris
	then
		[[ -d ${S}/mkspecs/${spec}-64 ]] && spec+=-64
	fi

	echo ${spec}
}