summaryrefslogtreecommitdiff
blob: 284f3ce7b4ae27a5455738b96333baa024339657 (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
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/multilib.eclass,v 1.30 2005/07/11 15:08:06 swegener Exp $
#
# Author: Jeremy Huddleston <eradicator@gentoo.org>
#
# This eclass is for all functions pertaining to handling multilib.
# configurations.


DESCRIPTION="Based on the ${ECLASS} eclass"

# has_multilib_profile:
# Return true if the current profile is a multilib profile and lists more than
# one abi in ${MULTILIB_ABIS}.  You might want to use this like
# 'use multilib || has_multilib_profile' until all profiles utilizing the
# 'multilib' use flag are removed from portage

# is_final_abi:
# Return true if ${ABI} is the final abi to be installed (and thus we are
# on our last run through a src_* function.

# number_abis:
# echo the number of ABIs we will be installing for

# get_install_abis:
# Return a list of the ABIs we want to install for with
# the last one in the list being the default.

# get_all_abis:
# Return a list of the ABIs supported by this profile.
# the last one in the list being the default.

# get_all_libdirs:
# Returns a list of all the libdirs used by this profile.  This includes
# those that might not be touched by the current ebuild and always includes
# "lib".

# get_libdir:
# Returns the libdir for the selected ABI.  This is backwards compatible
# and simply calls get_abi_LIBDIR() on newer profiles.  You should use this
# to determine where to install shared objects (ex: /usr/$(get_libdir))

# get_abi_var <VAR> [<ABI>]:
# returns the value of ${<VAR>_<ABI>} which should be set in make.defaults
#
# get_abi_CFLAGS:
# get_abi_CDEFINE:
# get_abi_LIBDIR:
# Aliases for 'get_abi_var CFLAGS', etc.

# get_ml_incdir [<include dir> [<ABI>]]
# include dir defaults to /usr/include
# ABI defaults to ${ABI} or ${DEFAULT_ABI}
#
# If a multilib include dir is associated with the passed include dir, then
# we return it, otherwise, we just echo back the include dir.  This is
# neccessary when a built script greps header files rather than testing them
# via #include (like perl) to figure out features.

# prep_ml_includes:
# Some includes (include/asm, glibc, etc) are ABI dependent.  In this case,
# We can install them in different locations for each ABI and create a common
# header which includes the right one based on CDEFINE_${ABI}.  If your
# package installs ABI-specific headers, just add 'prep_ml_includes' to the
# end of your src_install().  It takes a list of directories that include
# files are installed in (default is /usr/include if none are passed).
#
# Example:
# src_install() {
#    ...
#    prep_ml_includes /usr/qt/3/include
# }

# create_ml_includes <include dir> <symbol 1>:<dir 1> [<symbol 2>:<dir 2> ...]
# If you need more control than prep_ml_includes can offer (like linux-headers
# for the asm-* dirs, then use create_ml_includes.  The firs argument is the
# common dir.  The remaining args are of the form <symbol>:<dir> where
# <symbol> is what is put in the #ifdef for choosing that dir.
#
# Ideas for this code came from debian's sparc-linux headers package.
#
# Example:
# create_ml_includes /usr/include/asm __sparc__:/usr/include/asm-sparc __sparc64__:/usr/include/asm-sparc64
# create_ml_includes /usr/include/asm __i386__:/usr/include/asm-i386 __x86_64__:/usr/include/asm-x86_64

### END DOCUMENTATION ###

# Defaults:
export MULTILIB_ABIS=${MULTILIB_ABIS:-"default"}
export DEFAULT_ABI=${DEFAULT_ABI:-"default"}
# This causes econf to set --libdir=/usr/lib where it didn't before
#export ABI=${ABI:-"default"}
export CFLAGS_default
export LDFLAGS_default
export CHOST_default=${CHOST_default:-${CHOST}}
export LIBDIR_default=${CONF_LIBDIR:-"lib"}
export CDEFINE_default="__unix__"

# has_multilib_profile()
has_multilib_profile() {
	[ -n "${MULTILIB_ABIS}" -a "${MULTILIB_ABIS}" != "${MULTILIB_ABIS/ /}" ]
}

# This function simply returns the desired lib directory. With portage
# 2.0.51, we now have support for installing libraries to lib32/lib64
# to accomidate the needs of multilib systems. It's no longer a good idea
# to assume all libraries will end up in lib. Replace any (sane) instances
# where lib is named directly with $(get_libdir) if possible.
#
# Travis Tilley <lv@gentoo.org> (24 Aug 2004)
#
# Jeremy Huddleston <eradicator@gentoo.org> (23 Dec 2004):
#   Added support for ${ABI} and ${DEFAULT_ABI}.  If they're both not set,
#   fall back on old behavior.  Any profile that has these set should also
#   depend on a newer version of portage (not yet released) which uses these
#   over CONF_LIBDIR in econf, dolib, etc...
get_libdir() {
	local CONF_LIBDIR
	if [ -n  "${CONF_LIBDIR_OVERRIDE}" ] ; then
		# if there is an override, we want to use that... always.
		echo ${CONF_LIBDIR_OVERRIDE}
	else
		get_abi_LIBDIR
	fi
}

get_multilibdir() {
	if has_multilib_profile; then
		eerror "get_multilibdir called, but it shouldn't be needed with the new multilib approach.  Please file a bug at http://bugs.gentoo.org and assign it to eradicator@gentoo.org"
		exit 1
	fi
	echo ${CONF_MULTILIBDIR:=lib32}
}

# Sometimes you need to override the value returned by get_libdir. A good
# example of this is xorg-x11, where lib32 isnt a supported configuration,
# and where lib64 -must- be used on amd64 (for applications that need lib
# to be 32bit, such as adobe acrobat). Note that this override also bypasses
# portage version sanity checking.
# get_libdir_override expects one argument, the result get_libdir should
# return:
#
#   get_libdir_override lib64
#
# Travis Tilley <lv@gentoo.org> (31 Aug 2004)
get_libdir_override() {
	if has_multilib_profile; then
		eerror "get_libdir_override called, but it shouldn't be needed with the new multilib approach.  Please file a bug at http://bugs.gentoo.org and assign it to eradicator@gentoo.org"
		exit 1
	fi
	CONF_LIBDIR="$1"
	CONF_LIBDIR_OVERRIDE="$1"
	LIBDIR_default="$1"
}

# get_abi_var <VAR> [<ABI>]
# returns the value of ${<VAR>_<ABI>} which should be set in make.defaults
#
# ex:
# CFLAGS=$(get_abi_var CFLAGS sparc32) # CFLAGS=-m32
#
# Note that the prefered method is to set CC="$(tc-getCC) $(get_abi_CFLAGS)"
# This will hopefully be added to portage soon...
#
# If <ABI> is not specified, ${ABI} is used.
# If <ABI> is not specified and ${ABI} is not defined, ${DEFAULT_ABI} is used.
# If <ABI> is not specified and ${ABI} and ${DEFAULT_ABI} are not defined, we return an empty string.
#
# Jeremy Huddleston <eradicator@gentoo.org>
get_abi_var() {
	local flag=${1}
	local abi
	if [ $# -gt 1 ]; then
		abi=${2}
	elif [ -n "${ABI}" ]; then
		abi=${ABI}
	elif [ -n "${DEFAULT_ABI}" ]; then
		abi=${DEFAULT_ABI}
	else
		abi="default"
	fi

	local var="${flag}_${abi}"
	echo ${!var}
}

get_abi_CFLAGS() { get_abi_var CFLAGS "${@}"; }
get_abi_LDFLAGS() { get_abi_var LDFLAGS "${@}"; }
get_abi_CHOST() { get_abi_var CHOST "${@}"; }
get_abi_FAKE_TARGETS() { get_abi_var FAKE_TARGETS "${@}"; }
get_abi_CDEFINE() { get_abi_var CDEFINE "${@}"; }
get_abi_LIBDIR() { get_abi_var LIBDIR "${@}"; }

# Return a list of the ABIs we want to install for with
# the last one in the list being the default.
get_install_abis() {
	local order=""

	if [ -z "${MULTILIB_ABIS}" ]; then
		echo "default"
		return 0
	fi

	if hasq multilib-pkg-force ${RESTRICT} ||
	   { hasq multilib-pkg ${FEATURES} && hasq multilib-pkg ${RESTRICT}; }; then
		for x in ${MULTILIB_ABIS}; do
			if [ "${x}" != "${DEFAULT_ABI}" ]; then
				hasq ${x} ${ABI_DENY} || ordera="${ordera} ${x}"
			fi
		done
		hasq ${DEFAULT_ABI} ${ABI_DENY} || order="${ordera} ${DEFAULT_ABI}"

		if [ -n "${ABI_ALLOW}" ]; then
			local ordera=""
			for x in ${order}; do
				if hasq ${x} ${ABI_ALLOW}; then
					ordera="${ordera} ${x}"
				fi
			done
			order="${ordera}"
		fi
	else
		order="${DEFAULT_ABI}"
	fi

	if [ -z "${order}" ]; then
		die "The ABI list is empty.  Are you using a proper multilib profile?  Perhaps your USE flags or MULTILIB_ABIS are too restrictive for this package."
	fi

	echo ${order}
	return 0
}

# Return a list of the ABIs supported by this profile.
# the last one in the list being the default.
get_all_abis() {
	local order=""

	if [ -z "${MULTILIB_ABIS}" ]; then
		echo "default"
		return 0
	fi

	for x in ${MULTILIB_ABIS}; do
		if [ "${x}" != "${DEFAULT_ABI}" ]; then
			order="${order:+${order }}${x}"
		fi
	done
	order="${order:+${order} }${DEFAULT_ABI}"

	echo ${order}
	return 0
}

# get_all_libdirs()
# Returns a list of all the libdirs used by this profile.  This includes
# those that might not be touched by the current ebuild.
get_all_libdirs() {
	local libdirs="lib"
	local abi
	local dir

	if has_multilib_profile; then
		for abi in ${MULTILIB_ABIS}; do
			[ "$(get_abi_LIBDIR ${abi})" != "lib" ] && libdirs="${libdirs} $(get_abi_LIBDIR ${abi})"
		done
	elif [ -n "${CONF_LIBDIR}" ]; then
		for dir in ${CONF_LIBDIR} ${CONF_MULTILIBDIR:-lib32}; do
			[ "${dir}" != "lib" ] && libdirs="${libdirs} ${dir}"
		done
	fi

	echo "${libdirs}"
}

# Return true if ${ABI} is the last ABI on our list (or if we're not
# using the new multilib configuration.  This can be used to determine
# if we're in the last (or only) run through src_{unpack,compile,install}
is_final_abi() {
	has_multilib_profile || return 0
	local ALL_ABIS=$(get_install_abis)
	local LAST_ABI=${ALL_ABIS/* /}
	[[ ${LAST_ABI} == ${ABI} ]]
}

# echo the number of ABIs we will be installing for
number_abis() {
	get_install_abis | wc -w
}

# get_ml_incdir [<include dir> [<ABI>]]
# include dir defaults to /usr/include
# ABI defaults to ${ABI} or ${DEFAULT_ABI}
get_ml_incdir() {
	local dir=/usr/include

	if [[ ${#} -gt 0 ]]; then
		incdir=${1}
		shift
	fi

	if [[ -z "${MULTILIB_ABIS}" ]]; then
		echo ${incdir}
		return 0
	fi

	local abi=${ABI-${DEFAULT_ABI}}
	if [[ ${#} -gt 0 ]]; then
		abi=${1}
		shift
	fi

	if [[ -d "${dir}/gentoo-multilib/${abi}" ]]; then
		echo ${dir}/gentoo-multilib/${abi}
	else
		echo ${dir}
	fi
}

# prep_ml_includes:
#
# Some includes (include/asm, glibc, etc) are ABI dependent.  In this case,
# We can install them in different locations for each ABI and create a common
# header which includes the right one based on CDEFINE_${ABI}.  If your
# package installs ABI-specific headers, just add 'prep_ml_includes' to the
# end of your src_install().  It takes a list of directories that include
# files are installed in (default is /usr/include if none are passed).
#
# Example:
# src_install() {
#    ...
#    prep_ml_includes /usr/qt/3/include
# }

prep_ml_includes() {
	if [ $(number_abis) -gt 1 ]; then
		local dir
		local dirs
		local base

		if [ ${#} -eq 0 ]; then
			dirs="/usr/include"
		else
			dirs="${@}"
		fi

		for dir in ${dirs}; do
			base=${T}/gentoo-multilib/${dir}/gentoo-multilib
			mkdir -p ${base}
			[ -d ${base}/${ABI} ] && rm -rf ${base}/${ABI}
			mv ${D}/${dir} ${base}/${ABI}
		done

		if is_final_abi; then
			base=${T}/gentoo-multilib
			pushd ${base}
			find . | tar -c -T - -f - | tar -x --no-same-owner -f - -C ${D}
			popd

			for dir in ${dirs}; do
				local args=${dir}
				local abi
				for abi in $(get_install_abis); do
					args="${args} $(get_abi_CDEFINE ${abi}):${dir}/gentoo-multilib/${abi}"
				done
				create_ml_includes ${args}
			done
		fi
	fi
}

# If you need more control than prep_ml_includes can offer (like linux-headers
# for the asm-* dirs, then use create_ml_includes.  The firs argument is the
# common dir.  The remaining args are of the form <symbol>:<dir> where
# <symbol> is what is put in the #ifdef for choosing that dir.
#
# Ideas for this code came from debian's sparc-linux headers package.
#
# Example:
# create_ml_includes /usr/include/asm __sparc__:/usr/include/asm-sparc __sparc64__:/usr/include/asm-sparc64
# create_ml_includes /usr/include/asm __i386__:/usr/include/asm-i386 __x86_64__:/usr/include/asm-x86_64
create_ml_includes() {
	local dest="${1}"
	shift
	local mlinfo="${@}"
	local basedirs=$(create_ml_includes-listdirs ${mlinfo})

	create_ml_includes-makedestdirs ${dest} ${basedirs}

	local file
	for file in $(create_ml_includes-allfiles ${basedirs}); do
		local name="$(echo $file | tr a-z A-Z | sed 's:[^A-Z]:_:g')"
		{
			echo "/* Common header file autogenerated by create_ml_includes in multilib.eclass */"

			local dir
			for dir in ${basedirs}; do
				if [ -f "${D}/${dir}/${file}" ]; then
					local sym=$(create_ml_includes-sym_for_dir ${dir} ${mlinfo})
					if [[ ${sym::1} == "!" ]]; then
						echo "#ifndef ${sym:1}"
					else
						echo "#ifdef ${sym}"
					fi
					echo "#include <$(create_ml_includes-absolute ${dir}/${file})>"
					echo "#endif /* ${sym} */"
					echo ""
				fi
			done

			#echo "#endif /* __CREATE_ML_INCLUDES_STUB_${name}__ */"
		} > ${D}/${dest}/${file}
	done
}

# Helper function for create_ml_includes
create_ml_includes-absolute() {
	local dst="$(create_ml_includes-tidy_path ${1})"

	dst=(${dst//\// })

	local i
	for ((i=0; i<${#dst[*]}; i++)); do
		[ "${dst[i]}" == "include" ] && break
	done

	local strip_upto=$i

	for ((i=strip_upto+1; i<${#dst[*]}-1; i++)); do
		echo -n ${dst[i]}/
	done

	echo -n ${dst[i]}
}

# Helper function for create_ml_includes
create_ml_includes-tidy_path() {
	local removed="${1}"

	if [ -n "${removed}" ]; then
		# Remove multiple slashes
		while [ "${removed}" != "${removed/\/\//\/}" ]; do
			removed=${removed/\/\//\/}
		done

		# Remove . directories
		while [ "${removed}" != "${removed//\/.\//\/}" ]; do
			removed=${removed//\/.\//\/}
		done
		[ "${removed##*/}" = "." ] && removed=${removed%/*}

		# Removed .. directories
		while [ "${removed}" != "${removed//\/..\/}" ]; do
			local p1="${removed%%\/..\/*}"
			local p2="${removed#*\/..\/}"

			removed="${p1%\/*}/${p2}"
		done

		# Remove trailing ..
		[ "${removed##*/}" = ".." ] && removed=${removed%/*/*}

		# Remove trailing /
		[ "${removed##*/}" = "" ] && removed=${removed%/*}

		echo ${removed}
	fi
}

# Helper function for create_ml_includes
create_ml_includes-listdirs() {
	local dirs
	local data
	for data in ${@}; do
		dirs="${dirs} ${data/*:/}"
	done
	echo ${dirs:1}
}

# Helper function for create_ml_includes
create_ml_includes-makedestdirs() {
	local dest=${1}
	shift
	local basedirs=${@}

	dodir ${dest}

	local basedir
	for basedir in ${basedirs}; do
		local dir
		for dir in $(find ${D}/${basedir} -type d); do
			dodir ${dest}/${dir/${D}\/${basedir}/}
		done
	done
}

# Helper function for create_ml_includes
create_ml_includes-allfiles() {
	local basedirs=${@}

	local basedir
	for basedir in ${basedirs}; do
		local file
		for file in $(find ${D}/${basedir} -type f); do
			echo ${file/${D}\/${basedir}\//}
		done
	done | sort | uniq
}

# Helper function for create_ml_includes
create_ml_includes-sym_for_dir() {
	local dir="${1}"
	shift
	local data
	for data in ${@}; do
		if [ "${dir}" = "${data/*:/}" ]; then
			echo ${data/:*/}
			return 0
		fi
	done
	echo "Shouldn't be here -- create_ml_includes-sym_for_dir ${1} ${@}"
	# exit because we'll likely be called from a subshell
	exit 1
}