- Enhance .POSIX to set -e when invoking shells, as demanded by a
[make.git] / tests / scripts / variables / define
blobf91519e3276fa5c7178cabd5aa0c572c78167978
1 #                                                                    -*-perl-*-
3 $description = "Test define/endef variable assignments.";
5 $details = "";
7 # TEST 0: old-style basic define/endef
9 run_make_test('
10 define multi
11 @echo hi
12 echo there
13 endef
15 all: ; $(multi)
17               '', "hi\necho there\nthere\n");
19 # TEST 1: Various new-style define/endef
21 run_make_test('
22 FOO = foo
24 define multi =
25 echo hi
26 @echo $(FOO)
27 endef # this is the end
29 define simple :=
30 @echo $(FOO)
31 endef
33 append = @echo a
35 define append +=
37 @echo b
38 endef
40 define cond ?= # this is a conditional
41 @echo first
42 endef
44 define cond ?=
45 @echo second
46 endef
48 FOO = there
50 all: ; $(multi)
51         $(simple)
52         $(append)
53         $(cond)
55               '', "echo hi\nhi\nthere\nfoo\na\nb\nfirst\n");
57 # TEST 2: define in true section of conditional (containing conditional)
59 run_make_test('
60 FOO = foo
61 NAME = def
62 def =
63 ifdef BOGUS
64  define  $(subst e,e,$(NAME))     =
65   ifeq (1,1)
66    FOO = bar
67   endif
68  endef
69 endif
71 $(eval $(def))
72 all: ; @echo $(FOO)
74               'BOGUS=1', "bar\n");
76 # TEST 3: define in false section of conditional (containing conditional)
78 run_make_test(undef, '', "foo\n");
80 # TEST 4: nested define (supported?)
82 run_make_test('
83 define outer
84  define inner
85   A = B
86  endef
87 endef
89 $(eval $(outer))
91 outer: ; @echo $(inner)
93               '', "A = B\n");
95 # TEST 5: NEGATIVE: Missing variable name
97 run_make_test('
98 NAME =
99 define $(NAME)  =
100 ouch
101 endef
102 all: ; @echo ouch
104               '', "#MAKEFILE#:3: *** empty variable name.  Stop.\n", 512);
106 # TEST 6: NEGATIVE: extra text after define
108 run_make_test('
109 NAME =
110 define NAME = $(NAME)
111 ouch
112 endef
113 all: ; @echo ok
115               '', "#MAKEFILE#:3: extraneous text after `define' directive\nok\n");
117 # TEST 7: NEGATIVE: extra text after endef
119 run_make_test('
120 NAME =
121 define NAME =
122 ouch
123 endef $(NAME)
124 all: ; @echo ok
126               '', "#MAKEFILE#:5: extraneous text after `endef' directive\nok\n");
128 # TEST 8: NEGATIVE: missing endef
130 run_make_test('
131 NAME =
132 all: ; @echo ok
133 define NAME =
134 ouch
135 endef$(NAME)
137               '', "#MAKEFILE#:4: *** missing `endef', unterminated `define'.  Stop.\n", 512);
139 # -------------------------
140 # Make sure that prefix characters apply properly to define/endef values.
142 # There's a bit of oddness here if you try to use a variable to hold the
143 # prefix character for a define.  Even though something like this:
145 #       define foo
146 #       echo bar
147 #       endef
149 #       all: ; $(V)$(foo)
151 # (where V=@) can be seen by the user to be obviously different than this:
153 #       define foo
154 #       $(V)echo bar
155 #       endef
157 #       all: ; $(foo)
159 # and the user thinks it should behave the same as when the "@" is literal
160 # instead of in a variable, that can't happen because by the time make
161 # expands the variables for the command line and sees it begins with a "@" it
162 # can't know anymore whether the prefix character came before the variable
163 # reference or was included in the first line of the variable reference.
165 # TEST #5
166 # -------
168 run_make_test('
169 define FOO
170 $(V1)echo hello
171 $(V2)echo world
172 endef
173 all: ; @$(FOO)
174 ', '', 'hello
175 world');
177 # TEST #6
178 # -------
180 run_make_test(undef, 'V1=@ V2=@', 'hello
181 world');
183 # TEST #7
184 # -------
186 run_make_test('
187 define FOO
188 $(V1)echo hello
189 $(V2)echo world
190 endef
191 all: ; $(FOO)
192 ', 'V1=@', 'hello
193 echo world
194 world');
196 # TEST #8
197 # -------
199 run_make_test(undef, 'V2=@', 'echo hello
200 hello
201 world');
203 # TEST #9
204 # -------
206 run_make_test(undef, 'V1=@ V2=@', 'hello
207 world');
209 # TEST #10
210 # -------
211 # Test the basics; a "@" internally to the variable applies to only one line.
212 # A "@" before the variable applies to the entire variable.
214 run_make_test('
215 define FOO
216 @echo hello
217 echo world
218 endef
219 define BAR
220 echo hello
221 echo world
222 endef
224 all: foo bar
225 foo: ; $(FOO)
226 bar: ; @$(BAR)
227 ', '', 'hello
228 echo world
229 world
230 hello
231 world