aboutsummaryrefslogtreecommitdiff
blob: cb818a29d3cf17f38a2880c18dfeb83f9d2862a2 (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
<?xml version="1.0" encoding="UTF-8"?>
<guide self="ebuild-writing/use-conditional-code/">
<chapter>
<title>USE flag conditional code</title>

<body>
<p>
Often a particular block of code must only be executed if a given USE flag is
set (or unset). For large blocks, <c>if use foo</c> is best, or for inverse tests
either <c>if ! use foo</c> or <c>if use !foo</c> can be used (the former is more
common and is recommended for readability). For single-statement conditions, the
<c>use foo &amp;&amp; blah</c> (or <c>use foo || blah</c> for negatives) form is often more
readable.
</p>

<p>
The <c>if [ "`use foo`" ]</c> and <c>if [ -n "`use foo`" ]</c> forms which are
occasionally seen in older code must <b>not</b> be used. This is because, since Portage 2.1, the 'use' Portage helper does not produce any output when the use flag is enabled or disabled so the [ "`use foo`" ] statement is pretty much identical to [ "" ] which always evaluates to false.
</p>

<note>
<c>die</c> will not work as expected within a subshell, so code in the
form <c>use foo &amp;&amp; ( blah ; blah )</c> should be avoided in favour of a proper if
statement. See <uri link="::ebuild-writing/error-handling/#die and subshells"/>.
</note>

<codesample lang="ebuild">
	# USE conditional blocks...
	if use livecd ; then
		# remove some extra files for a small livecd install
		rm -fr "${vimfiles}"/{compiler,doc,ftplugin,indent} || die
	fi

	# Inverse USE conditional blocks...
	if ! use cscope ; then
		# the --disable-cscope configure arg doesn't quite work properly,
		# so sed it out of feature.h if we're not USEing cscope.
		sed -i -e '/# define FEAT_CSCOPE/d' src/feature.h || die "couldn't disable cscope"
	fi

	# USE conditional statements...
	use ssl &amp;&amp; eapply "${FILESDIR}/${P}-ssl.patch"
	use sparc &amp;&amp; filter-flags -fomit-frame-pointer

	# Inverse USE conditional statements...
	use ncurses || eapply "${FILESDIR}/${P}-no-ncurses.patch"
</codesample>

<p>
For echoing content based upon a USE flag, there is often a better helper
function available.
</p>

<p>
It is guaranteed that <c>use</c> produces no output. If you need output
displayed, use the <c>usev</c> function; it echoes the USE flag's name
if the condition is met.
The <c>useq</c> function is a deprecated synonym for <c>use</c>, don't
use it in new code.
</p>
</body>

</chapter>
</guide>