summaryrefslogtreecommitdiffstats
path: root/src/kmk/tests/scripts/variables/define
blob: 7324cbc7825820e65e4fced53c32de54a5f276ed (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
#                                                                    -*-perl-*-

$description = "Test define/endef variable assignments.";

$details = "";

# TEST 0: old-style basic define/endef

run_make_test('
define multi
@echo hi
echo there
endef

all: ; $(multi)
',
              '', "hi\necho there\nthere\n");

# TEST 1: Various new-style define/endef

run_make_test('
FOO = foo

define multi =
echo hi
@echo $(FOO)
endef # this is the end

define simple :=
@echo $(FOO)
endef

define posix ::=
@echo $(FOO)
endef

append = @echo a

define append +=

@echo b
endef

define cond ?= # this is a conditional
@echo first
endef

define cond ?=
@echo second
endef

FOO = there

all: ; $(multi)
	$(simple)
	$(posix)
	$(append)
	$(cond)
',
              '', "echo hi\nhi\nthere\nfoo\nfoo\na\nb\nfirst\n");

# TEST 1a: Various new-style define/endef, with no spaces

run_make_test('
FOO = foo

define multi=
echo hi
@echo $(FOO)
endef # this is the end

define simple:=
@echo $(FOO)
endef

define posix::=
@echo $(FOO)
endef

append = @echo a

define append+=

@echo b
endef

define cond?= # this is a conditional
@echo first
endef

define cond?=
@echo second
endef

FOO = there

all: ; $(multi)
	$(simple)
	$(posix)
	$(append)
	$(cond)
',
              '', "echo hi\nhi\nthere\nfoo\nfoo\na\nb\nfirst\n");

# TEST 2: define in true section of conditional (containing conditional)

run_make_test('
FOO = foo
NAME = def
def =
ifdef BOGUS
 define  $(subst e,e,$(NAME))     =
  ifeq (1,1)
   FOO = bar
  endif
 endef
endif

$(eval $(def))
all: ; @echo $(FOO)
',
              'BOGUS=1', "bar\n");

# TEST 3: define in false section of conditional (containing conditional)

run_make_test(undef, '', "foo\n");

# TEST 4: nested define (supported?)

run_make_test('
define outer
 define inner
  A = B
 endef
endef

$(eval $(outer))

outer: ; @echo $(inner)
',
              '', "A = B\n");

# TEST 5: NEGATIVE: Missing variable name

run_make_test('
NAME =
define $(NAME)  =
ouch
endef
all: ; @echo ouch
',
              '', "#MAKEFILE#:3: *** empty variable name.  Stop.\n", 512);

# TEST 6: NEGATIVE: extra text after define

run_make_test('
NAME =
define NAME = $(NAME)
ouch
endef
all: ; @echo ok
',
              '', "#MAKEFILE#:3: extraneous text after 'define' directive\nok\n");

# TEST 7: NEGATIVE: extra text after endef

run_make_test('
NAME =
define NAME =
ouch
endef $(NAME)
all: ; @echo ok
',
              '', "#MAKEFILE#:5: extraneous text after 'endef' directive\nok\n");

# TEST 8: NEGATIVE: missing endef

run_make_test('
NAME =
all: ; @echo ok
define NAME =
ouch
endef$(NAME)
',
              '', "#MAKEFILE#:4: *** missing 'endef', unterminated 'define'.  Stop.\n", 512);

# -------------------------
# Make sure that prefix characters apply properly to define/endef values.
#
# There's a bit of oddness here if you try to use a variable to hold the
# prefix character for a define.  Even though something like this:
#
#       define foo
#       echo bar
#       endef
#
#       all: ; $(V)$(foo)
#
# (where V=@) can be seen by the user to be obviously different than this:
#
#       define foo
#       $(V)echo bar
#       endef
#
#       all: ; $(foo)
#
# and the user thinks it should behave the same as when the "@" is literal
# instead of in a variable, that can't happen because by the time make
# expands the variables for the command line and sees it begins with a "@" it
# can't know anymore whether the prefix character came before the variable
# reference or was included in the first line of the variable reference.

# TEST #5
# -------

run_make_test('
define FOO
$(V1)echo hello
$(V2)echo world
endef
all: ; @$(FOO)
', '', 'hello
world');

# TEST #6
# -------

run_make_test(undef, 'V1=@ V2=@', 'hello
world');

# TEST #7
# -------

run_make_test('
define FOO
$(V1)echo hello
$(V2)echo world
endef
all: ; $(FOO)
', 'V1=@', 'hello
echo world
world');

# TEST #8
# -------

run_make_test(undef, 'V2=@', 'echo hello
hello
world');

# TEST #9
# -------

run_make_test(undef, 'V1=@ V2=@', 'hello
world');

# TEST #10
# -------
# Test the basics; a "@" internally to the variable applies to only one line.
# A "@" before the variable applies to the entire variable.

run_make_test('
define FOO
@echo hello
echo world
endef
define BAR
echo hello
echo world
endef

all: foo bar
foo: ; $(FOO)
bar: ; @$(BAR)
', '', 'hello
echo world
world
hello
world
');

1;