aboutsummaryrefslogtreecommitdiff
blob: dd6369ad5c4dac83bb11fc37f7b08b3333542be1 (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
# Copyright: 2011-2012 Brian Harring <ferringb@gmail.com>
# license GPL2/BSD 3

use() {
	# EAPI 5 and up
	if ${PKGCORE_PROFILE_IUSE_INJECTION} && \
			[[ ! ${1#!} =~ ${PKGCORE_IUSE_EFFECTIVE} ]]; then
		die "USE flag '${1#!}' not in IUSE for ${CATEGORY}/${PF}"
	fi

	# Ensure USE is split on normal IFS.
	local IFS=$' \t\n'

	if [[ ${1:0:1} == "!" ]]; then
		! __safe_has "${1#!}" ${USE}
	else
		__safe_has "$1" ${USE}
	fi
}

usev() {
	if use "$1"; then
		echo "${1#!}"
	fi
}

useq() {
	use "$@"
}

use_with() {
	if [[ -z $1 ]]; then
		echo "!!! use_with() called without a parameter." >&2
		echo "!!! use_with <USEFLAG> [<flagname> [value]]" >&2
		return
	fi

	local uw_suffix=""
	if __safe_has "${EAPI:-0}" 0 1 2 3; then
		uw_suffix=${3:+=$3}
	else
		uw_suffix=${3+=$3}
	fi

	local uword=$2
	if [[ -z ${uword} ]]; then
		uword=$1
	fi

	if use $1; then
		echo "--with-${uword}${uw_suffix}"
		return 0
	fi
	echo "--without-${uword}"
	return 1
}

use_enable() {
	if [[ -z $1 ]]; then
		echo "!!! use_enable() called without a parameter." >&2
		echo "!!! use_enable <USEFLAG> [<flagname> [value]]" >&2
		return
	fi

	local ue_suffix=""
	if __safe_has "${EAPI:-0}" 0 1 2 3; then
		ue_suffix=${3:+=$3}
	else
		ue_suffix=${3+=$3}
	fi

	local uword=$2
	if [[ -z ${uword} ]]; then
		uword=$1
	fi

	if use "$1"; then
		echo "--enable-${uword}${ue_suffix}"
		return 0
	fi
	echo "--disable-${uword}"
	return 1
}

econf() {
	local ret
	ECONF_SOURCE=${ECONF_SOURCE:-.}
	if [[ ! -x ${ECONF_SOURCE}/configure ]]; then
		[[ -f ${ECONF_SOURCE}/configure ]] && die "configure script isn't executable"
		die "no configure script found"
	fi

	if [[ -d /usr/share/gnuconfig ]]; then
		local x
		find "${WORKDIR}" -type f \( -name config.guess -o -name config.sub \) | \
			while read x; do
			echo "econf: replacing ${x} with /usr/share/gnuconfig/${x##*/}"
			cp -f "/usr/share/gnuconfig/${x##*/}" "${x}"
		done
	fi

	# if the profile defines a location to install libs to aside from default, pass it on.
	# if the ebuild passes in --libdir, they're responsible for the conf_libdir fun.
	local CONF_LIBDIR=$(__get_libdir)
	if [[ -n ${CONF_LIBDIR} && $* != *"--libdir="* ]]; then
		if [[ $* == *"--exec-prefix="* ]]; then
			local args=$(echo $*)
			local -a prefix=( $(echo ${args/*--exec-prefix[= ]}) )
			CONF_PREFIX=${prefix/--*}
			[[ ${CONF_PREFIX} != /* ]] && CONF_PREFIX=/${CONF_PREFIX}
		elif [[ $* == *"--prefix="* ]]; then
			local args=$(echo $*)
			local -a pref=( $(echo ${args/*--prefix[= ]}) )
			CONF_PREFIX=${prefix/--*}
			[[ ${CONF_PREFIX} != /* ]] && CONF_PREFIX=/${CONF_PREFIX}
		else
			CONF_PREFIX=/usr
		fi
		export CONF_PREFIX
		[[ ${CONF_LIBDIR} != /* ]] && CONF_LIBDIR=/${CONF_LIBDIR}
		set -- --libdir="$(__strip_duplicate_slashes "${CONF_PREFIX}${CONF_LIBDIR}")" "$@"
	fi

	if ${PKGCORE_ECONF_DISABLE_DEPENDENCY_TRACKING} || ${PKGCORE_ECONF_DISABLE_SILENT_RULES}; then
		local help_text=$("${ECONF_SOURCE}/configure" --help 2> /dev/null)
		local extra_args=()

		# EAPI 4 and up.
		if ${PKGCORE_ECONF_DISABLE_DEPENDENCY_TRACKING} && \
				[[ ${help_text} =~ "--disable-dependency-tracking" ]]; then
			extra_args+=( --disable-dependency-tracking )
		fi

		# EAPI 5 and up.
		if ${PKGCORE_ECONF_DISABLE_SILENT_RULES} && \
				[[ ${help_text} =~ "--disable-silent-rules" ]]; then
			extra_args+=( --disable-silent-rules )
		fi
		set -- "${extra_args[@]}" "$@"
		unset extra_args
		unset help_text
	fi

	# Reset IFS since we're interpretting user supplied EXTRA_ECONF.
	local IFS=$' \t\n'
	set -- "${ECONF_SOURCE}/configure" \
		--prefix=/usr \
		${CBUILD:+--build="${CBUILD}"} \
		--host="${CHOST}" \
		${CTARGET:+--target="${CTARGET}"} \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--datadir=/usr/share \
		--sysconfdir=/etc \
		--localstatedir=/var/lib \
		"$@" \
		${EXTRA_ECONF}

	echo "$@"

	if ! "$@"; then
		if [[ -s config.log ]]; then
			echo
			echo "!!! Please attach the config.log to your bug report:"
			echo "!!! ${PWD}/config.log"
		fi
		die "econf failed"
	fi
	return $?
}

# debug-print() gets called from many places with verbose status information useful
# for tracking down problems. The output is in ${T}/eclass-debug.log.
# You can set ECLASS_DEBUG_OUTPUT to redirect the output somewhere else as well.
# The special "on" setting echoes the information, mixing it with the rest of the
# emerge output.
# You can override the setting by exporting a new one from the console, or you can
# set a new default in make.*. Here the default is "" or unset.

# in the future might use e* from /etc/init.d/functions.sh if i feel like it
debug-print() {
	if __safe_has ${EBUILD_PHASE} depend nofetch config info postinst; then
		return
	fi
	# if ${T} isn't defined, we're in dep calculation mode and
	# shouldn't do anything
	[[ -z ${T} ]] && return 0

	local _item
	for _item in "$@"; do
		# extra user-configurable targets
		if [[ ${ECLASS_DEBUG_OUTPUT} == "on" ]]; then
			echo "debug: ${_item}"
		elif [[ -n ${ECLASS_DEBUG_OUTPUT} ]]; then
			echo "debug: ${_item}" >> "${ECLASS_DEBUG_OUTPUT}"
		fi

		# default target
		echo "${_item}" >> "${T}"/eclass-debug.log
		chmod g+w "${T}"/eclass-debug.log &> /dev/null
	done
	# let the portage user own/write to this file
}

# The following 2 functions are debug-print() wrappers

debug-print-function() {
	str="$1: entering function"
	shift
	debug-print "${str}, parameters: $*"
}

debug-print-section() {
	debug-print "now in section $*"
}

einstall() {
	${PKGCORE_PREFIX_SUPPORT} || local ED=${D}
	# CONF_PREFIX is only set if they didn't pass in libdir above
	local LOCAL_EXTRA_EINSTALL=( ${EXTRA_EINSTALL} )
	local CONF_LIBDIR=$(__get_libdir)
	if [[ -n ${CONF_LIBDIR} && ${CONF_PREFIX:-unset} != "unset" ]]; then
		EI_DESTLIBDIR=${ED%%/}/${CONF_PREFIX%%/}/${CONF_LIBDIR%%/}/
		LOCAL_EXTRA_EINSTALL+=( libdir=${EI_DESTLIBDIR} )
		unset EI_DESTLIBDIR
	fi

	if ! [[ -f Makefile || -f GNUmakefile || -f makefile ]]; then
		die "no Makefile found"
	fi

	# Reset IFS for LOCAL_EXTRA_EINSTALL, should users be up to something.
	local IFS=$' \t\n'
	set -- \
		${MAKE:-make} \
		prefix="${ED}/usr" \
		datadir="${ED}/usr/share" \
		infodir="${ED}/usr/share/info" \
		localstatedir="${ED}/var/lib" \
		mandir="${ED}/usr/share/man" \
		sysconfdir="${ED}/etc" \
		${LOCAL_EXTRA_EINSTALL[@]} \
		"$@" install
	[[ ${PKGCORE_DEBUG} != 0 ]] && "$@" -n
	"$@" || die "einstall failed"
}

__get_libdir() {
	local libdir=$1 libdir_var="LIBDIR_${ABI}"
	[[ -n ${ABI} && -n ${!libdir_var} ]] && libdir=${!libdir_var}
	echo "${libdir}"
}

__phase_common_pkg_nofetch() {
	[[ -z ${SRC_URI} ]] && return

	echo "!!! The following are listed in SRC_URI for ${PN}:"
	local fp
	__shopt_push -f
	for fp in ${SRC_URI}; do
		echo "!!! ${fp}"
	done
	__shopt_pop
}

__phase_common_src_unpack() {
	if [[ -n ${A} ]]; then
		unpack ${A}
	fi
}

__phase_common_src_compile() {
	# only eapi 0/1 invoke configure...
	if __safe_has "${EAPI:-0}" 0 1; then
		if [[ ${EAPI:-0} == 0 ]]; then
			[[ -x ./configure ]] && econf
		elif [[ -x ${ECONF_SOURCE:-.}/configure ]]; then
			econf
		fi
	fi
	if [[ -f Makefile || -f GNUmakefile || -f makefile ]]; then
		emake || die "emake failed"
	fi
}

__phase_common_src_test() {
	addpredict /
	local extra_args=( ${EXTRA_EMAKE} )
	${PKGCORE_ALLOW_PARALLEL_SRC_TEST} || extra_args+=( -j1 )
	if make check -n &> /dev/null; then
		echo ">>> Test phase [check]: ${CATEGORY}/${PF}"
		emake "${extra_args[@]}" check || die "Make check failed. See above for details."
	elif make test -n &> /dev/null; then
		emake "${extra_args[@]}" test || die "Make test failed. See above for details."
	else
		echo ">>> Test phase [none]: ${CATEGORY}/${PF}"
	fi
	SANDBOX_PREDICT=${SANDBOX_PREDICT%:/}
}

into() {
	${PKGCORE_PREFIX_SUPPORT} || local ED=${D}
	if [[ $1 == "/" ]]; then
		export DESTTREE=""
	else
		export DESTTREE=$1
		if [[ ! -d ${ED}${DESTTREE} ]]; then
			install -d "${ED}${DESTTREE}"
		fi
	fi
}

insinto() {
	${PKGCORE_PREFIX_SUPPORT} || local ED=${D}
	if [[ $1 == "/" ]]; then
		export INSDESTTREE=""
	else
		export INSDESTTREE=$1
		if [[ ! -d ${ED}${INSDESTTREE} ]]; then
			install -d "${ED}${INSDESTTREE}"
		fi
	fi
}

exeinto() {
	${PKGCORE_PREFIX_SUPPORT} || local ED=${D}
	if [[ $1 == "/" ]]; then
		export PKGCORE_EXEDESTTREE=""
	else
		export PKGCORE_EXEDESTTREE=$1
		if [[ ! -d ${ED}${PKGCORE_EXEDESTTREE} ]]; then
			install -d "${ED}${PKGCORE_EXEDESTTREE}"
		fi
	fi
}

docinto() {
	${PKGCORE_PREFIX_SUPPORT} || local ED=${D}
	if [[ $1 == "/" ]]; then
		export PKGCORE_DOCDESTTREE=""
	else
		export PKGCORE_DOCDESTTREE=$1
		if [[ ! -d ${ED}usr/share/doc/${PF}/${PKGCORE_DOCDESTTREE} ]]; then
			install -d "${ED}usr/share/doc/${PF}/${PKGCORE_DOCDESTTREE}"
		fi
	fi
}

__phase_common_pre_phase() {
	if [[ -d ${S} ]]; then
		cd "${S}"
	elif __safe_has "${EAPI}" 0 1 2 3; then
		cd "${WORKDIR}"
	elif [[ -n ${A} ]]; then
		die "source directory '${S}' doesn't exist, but \${A} isn't empty (see S-WORKDIR-FALLBACK in PMS)"
	else
		local phase
		# eapi4 blatant idiocy...
		for phase in unpack prepare configure compile install; do
			[[ ${phase} == ${EBUILD_PHASE} ]] && break
			__is_function src_${phase} || continue
			# to reach here means that (for example), we're doing src_install, and src_compile was defined
			# but S doesn't exist.
			die "source directory '${S}' doesn't exist, \${A} is defined, and there was a defined " \
				"phase function '${phase}' prior to '${EBUILD_PHASE}'; please see S-WORKDIR-FALLBACK " \
				"in pms for the details of what is allowed for eapi4 and later"
		done
		cd "${WORKDIR}"
	fi
}

__phase_default_pre_src_unpack() {
	export PORTAGE_GZIP_COMMAND=${PORTAGE_GZIP_COMMAND:-gzip}
	export PORTAGE_BZIP2_COMMAND=${PORTAGE_BZIP2_COMMAND:-bzip2}
	export PORTAGE_XZ_COMMAND=${PORTAGE_XZ_COMMAND:-xz}
	export S
	cd "${WORKDIR}"
}

__phase_default_pre_src_prepare() { __phase_common_pre_phase; }
__phase_default_pre_src_test()    { __phase_common_pre_phase; }

__phase_default_pre_src_configure() {
	local var
	for var in C{BUILD,HOST,TARGET,C,XX} {AS,LD,{,LIB}C{,XX}}FLAGS; do
		[[ -n ${!var+set} ]] && export ${var}="${!var}"
	done
	__phase_common_pre_phase
}

__phase_default_pre_src_compile() {
	# just reuse the default_pre_src_configure; this means we don't have to care
	# if the eapi has configure or not.
	__phase_default_pre_src_configure

	if __feature_is_enabled distcc; then
		[[ -n ${DISTCC_DIR} ]] && addwrite "${DISTCC_DIR}"
		if __feature_is_enabled distcc-pump; then
			eval $(pump --startup) || echo "Warning: Failed starting pump" >&2
			trap 'pump --shutdown' EXIT
		fi
	fi
}

__phase_default_post_src_compile() {
	if __feature_is_enabled distcc && __feature_is_enabled distcc-pump; then
		pump --shutdown
		trap - EXIT
	fi
}

__phase_default_pre_src_install() {
	export DESTTREE=/usr INSDESTTREE='' PKGCORE_EXEDESTTREE='' PKGCORE_DOCDESTTREE=''
	export INSOPTIONS="-m0644" EXEOPTIONS="-m0755"
	export LIBOPTIONS="-m0644" DIROPTIONS="-m0755"
	export PORTAGE_COMPRESS=${PORTAGE_COMPRESS:-bzip2}
	export PORTAGE_COMPRESS_FLAGS=${PORTAGE_COMPRESS_FLAGS:--9}
	export MOPREFIX=${PN}
	export D
	rm -rf "${D}"
	if ${PKGCORE_PREFIX_SUPPORT}; then
		[[ -n ${ED+set} ]] || \
			die "variable ED is unset, but prefix mode is enabled.	internal error?"
		export ED=${ED}
		mkdir -p "${ED}"
	else
		mkdir "${D}"
	fi
	__phase_common_pre_phase
}

__inject_phase_funcs() {
	local prefix=$1 func
	shift

	for func in "$@"; do
		if ! __is_function "${func}"; then
			eval "__phase_default_${func}() { ${prefix}_${func}; }";
		fi

		# define default_${phase} funcs for EAPI 2 and up
		if ! __safe_has "${EAPI:-0}" 0 1 ; then
			eval "default_${func}() { ${prefix}_${func}; }";
		fi
	done
}

__inject_common_phase_funcs() {
	__inject_phase_funcs __phase_common pkg_nofetch src_{unpack,compile,test}
}

unpack() {
	local file filename myfail srcdir taropts tar_subdir
	taropts='--no-same-owner'

	[[ $# -eq 0 ]] && die "Nothing passed to the 'unpack' command"

	for file in "$@"; do
		echo ">>> Unpacking ${file} to ${PWD}"
		myfail="failure unpacking ${file}"
		if [[ ${file} == "./"* ]]; then
			srcdir=""
		else
			srcdir=${DISTDIR}
		fi

		[[ ! -e ${srcdir}${file} ]] && die "${myfail}: file doesn't exist"
		[[ ! -s ${srcdir}${file} ]] && die "${myfail}: empty file"
		[[ ${file} == ${DISTDIR%%/}/* ]] && \
			die "Arguments to unpack() must not begin with \${DISTDIR}."
		[[ ${file} == /* ]] &&
			die "Arguments to unpack() must not be absolute paths."

		filename=${file##*/}

		case ${file} in
			*.tar)
				tar xf "${srcdir}${file}" ${taropts} || die "${myfail}"
				;;
			*.tar.gz|*.tgz|*.tar.Z)
				tar xf "${srcdir}${file}" -I"${PORTAGE_GZIP_COMMAND}" ${taropts} || die "${myfail}"
				;;
			*.tar.bz2|*.tbz2|*.tbz)
				tar xf "${srcdir}${file}" -I"${PORTAGE_BZIP2_COMMAND}" ${taropts} || die "${myfail}"
				;;
			*.tar.lzma)
				tar xf "${srcdir}${file}" -Ilzma ${taropts} || die "${myfail}"
				;;
			*.tar.xz)
				if __safe_has "${EAPI}" 0 1 2; then
					echo "xz is a supported extension in EAPI 3 and above only" >&2
					continue;
				fi
				tar xf "${srcdir}${file}" -I"${PORTAGE_XZ_COMMAND}" ${taropts} || die "${myfail}"
				;;
			*.ZIP|*.zip|*.jar)
				{ set +x; while :; do echo n || break; done } | \
					unzip -qo "${srcdir}${file}" || die "${myfail}"
				;;
			*.gz|*.Z|*.z)
				${PORTAGE_GUNZIP_COMMAND:-${PORTAGE_GZIP_COMMAND} -d} -c "${srcdir}${file}" > ${filename%.*} || die "${myfail}"
				;;
			*.bz2|*.bz)
				${PORTAGE_BUNZIP2_COMMAND:-${PORTAGE_BZIP2_COMMAND} -d} -c "${srcdir}${file}" > ${filename%.*} || die "${myfail}"
				;;
			*.xz)
				if __safe_has "${EAPI}" 0 1 2; then
					echo "xz is a supported extension in EAPI 3 and above only" >&2
					continue;
				fi
				${PORTAGE_UNXZ_COMMAND:-${PORTAGE_XZ_COMMAND} -d} < "${srcdir}${file}" > ${filename%.*} || die "${myfail}"
				;;
			*.7Z|*.7z)
				local my_output
				my_output=$(7z x -y "${srcdir}${file}")
				if [[ $? -ne 0 ]]; then
					echo "${my_output}" >&2
					die "${myfail}"
				fi
				;;
			*.RAR|*.rar)
				unrar x -idq -o+ "${srcdir}${file}" || die "${myfail}"
				;;
			*.LHa|*.LHA|*.lha|*.lzh)
				lha xfq "${srcdir}${file}" || die "${myfail}"
				;;
			*.a|*.deb)
				ar x "${srcdir}${file}" || die "${myfail}"
				;;
			*.lzma)
				lzma -dc "${srcdir}${file}" > ${filename%.*} || die "${myfail}"
				;;
			*)
				echo "unpack ${file}: file format not recognized. Ignoring."
				;;
		esac
	done
	find . -mindepth 1 -maxdepth 1 ! -type l -print0 | \
		${XARGS} -0 chmod -fR a+rX,u+w,g-w,o-w
}

diropts() {
	export DIROPTIONS=$@
}

insopts() {
	{ has -s "$@" || has --strip "$@"; } && \
		ewarn "insopts shouldn't be given -s; stripping should be left to the manager."
	export INSOPTIONS=$@
}

exeopts() {
	{ has -s "$@" || has --strip "$@"; } && \
		ewarn "exeopts shouldn't be given -s; stripping should be left to the manager."
	export EXEOPTIONS=$@
}

libopts() {
	{ has -s "$@" || has --strip "$@"; } && \
		ewarn "libopts shouldn't be given -s; stripping should be left to the manager."
	export LIBOPTIONS=$@
}

portageq() {
	if [[ ${EBUILD_PHASE} == "depend" ]]; then
		die "portageq calls in depends phase aren't allowed"
	fi
	local command=$1
	shift
	# suppress sandbox for the invocation; do this to avoid things like .pyc generation
	# being snagged by the sandbox
	local portageq_str="portageq"
	${PKGCORE_DISABLE_COMPAT-false} && portageq_str="query"
	SANDBOX_ON=0 PYTHONPATH="${PKGCORE_PYTHONPATH}" "${PKGCORE_PYTHON_BINARY}" \
		$(type -P pinspect) ${portageq_str} "${command}" \
		--eapi "${EAPI:--1}" --use "${USE}" "$@"
	local ret=$?
	[[ ${ret} == 127 ]] && die "pinspect couldn't be found; broken pkgcore installation?"
	return $(( ret ))
}

has_version() {
	PKGCORE_DISABLE_COMPAT=true portageq 'has_version' "$1"
}

best_version() {
	PKGCORE_DISABLE_COMPAT=true portageq 'best_version' "$1"
}

: