directive-ifmake.mk revision 1.9 1 1.9 rillig # $NetBSD: directive-ifmake.mk,v 1.9 2022/01/22 16:23:56 rillig Exp $
2 1.1 rillig #
3 1.3 rillig # Tests for the .ifmake directive, which provides a shortcut for asking
4 1.3 rillig # whether a certain target is requested to be made from the command line.
5 1.8 rillig #
6 1.8 rillig # TODO: Describe why the shortcut may be useful (if it's useful at all),
7 1.9 rillig # instead of using the more versatile '.if make(target)'.
8 1.1 rillig
9 1.9 rillig .MAKEFLAGS: first second
10 1.7 rillig
11 1.3 rillig # This is the most basic form.
12 1.3 rillig .ifmake first
13 1.5 rillig . info ok: positive condition works
14 1.3 rillig .else
15 1.5 rillig . warning positive condition fails
16 1.3 rillig .endif
17 1.1 rillig
18 1.9 rillig # The '!' is interpreted as 'not'. A possible alternative interpretation of
19 1.9 rillig # this condition is whether the target named "!first" was requested. To
20 1.9 rillig # distinguish these cases, see the next test.
21 1.3 rillig .ifmake !first
22 1.5 rillig . warning unexpected
23 1.3 rillig .else
24 1.5 rillig . info ok: negation works
25 1.3 rillig .endif
26 1.3 rillig
27 1.3 rillig # See if the exclamation mark really means "not", or if it is just part of
28 1.8 rillig # the target name. Since it means 'not', the two exclamation marks are
29 1.8 rillig # effectively ignored, and 'first' is indeed a requested target. If the
30 1.8 rillig # exclamation mark were part of the name instead, the name would be '!!first',
31 1.8 rillig # and such a target was not requested to be made.
32 1.3 rillig .ifmake !!first
33 1.5 rillig . info ok: double negation works
34 1.3 rillig .else
35 1.5 rillig . warning double negation fails
36 1.3 rillig .endif
37 1.3 rillig
38 1.3 rillig # Multiple targets can be combined using the && and || operators.
39 1.3 rillig .ifmake first && second
40 1.5 rillig . info ok: both mentioned
41 1.3 rillig .else
42 1.5 rillig . warning && does not work as expected
43 1.3 rillig .endif
44 1.3 rillig
45 1.3 rillig # Negation also works in complex conditions.
46 1.3 rillig .ifmake first && !unmentioned
47 1.5 rillig . info ok: only those mentioned
48 1.3 rillig .else
49 1.5 rillig . warning && with ! does not work as expected
50 1.3 rillig .endif
51 1.3 rillig
52 1.4 rillig # Using the .MAKEFLAGS special dependency target, arbitrary command
53 1.4 rillig # line options can be added at parse time. This means that it is
54 1.4 rillig # possible to extend the targets to be made.
55 1.4 rillig .MAKEFLAGS: late-target
56 1.4 rillig .ifmake late-target
57 1.5 rillig . info Targets can even be added at parse time.
58 1.4 rillig .else
59 1.5 rillig . info No, targets cannot be added at parse time anymore.
60 1.4 rillig .endif
61 1.4 rillig
62 1.6 rillig # Numbers are interpreted as numbers, no matter whether the directive is
63 1.6 rillig # a plain .if or an .ifmake.
64 1.6 rillig .ifmake 0
65 1.6 rillig . error
66 1.6 rillig .endif
67 1.6 rillig .ifmake 1
68 1.6 rillig .else
69 1.6 rillig . error
70 1.6 rillig .endif
71 1.6 rillig
72 1.7 rillig # A condition that consists of a variable expression only (without any
73 1.7 rillig # comparison operator) can be used with .if and the other .ifxxx directives.
74 1.7 rillig .ifmake ${:Ufirst}
75 1.7 rillig . info ok
76 1.7 rillig .else
77 1.7 rillig . error
78 1.7 rillig .endif
79 1.7 rillig
80 1.7 rillig
81 1.9 rillig # As an edge case, a target can actually be named "!first" on the command
82 1.9 rillig # line. There is no way to define a target of this name though since in a
83 1.9 rillig # dependency line, a plain '!' is interpreted as a dependency operator.
84 1.9 rillig
85 1.9 rillig .MAKEFLAGS: !edge
86 1.9 rillig .ifmake edge
87 1.9 rillig . error
88 1.9 rillig .endif
89 1.9 rillig
90 1.9 rillig # The '\!edge' in the following condition is parsed as a bare word. For such
91 1.9 rillig # a bare word, there is no escaping mechanism so the backslash passes through.
92 1.9 rillig # Since the condition function 'make' accepts a pattern instead of a plain
93 1.9 rillig # target name, the '\' is finally discarded in Str_Match.
94 1.9 rillig .ifmake \!edge
95 1.9 rillig .else
96 1.9 rillig . error
97 1.9 rillig .endif
98 1.9 rillig
99 1.9 rillig # In a dependency line, a plain '!' is interpreted as a dependency operator
100 1.9 rillig # (the other two are ':' and '::'). If the '!' is escaped by a '\', as
101 1.9 rillig # implemented in ParseDependencyTargetWord, the additional backslash is never
102 1.9 rillig # removed though. The target name thus becomes '\!edge' instead of the
103 1.9 rillig # intended '!edge'. Defining a target whose name contains a '!' will either
104 1.9 rillig # require additional tricks, or it may even be impossible.
105 1.9 rillig
106 1.9 rillig first second unmentioned late-target \!edge:
107 1.3 rillig : $@
108