TestMakeBase.gmk revision 2518:459d61aa3b54
1#
2# Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# This code is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 only, as
7# published by the Free Software Foundation.  Oracle designates this
8# particular file as subject to the "Classpath" exception as provided
9# by Oracle in the LICENSE file that accompanied this code.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26default: all
27
28include $(SPEC)
29include MakeBase.gmk
30
31THIS_FILE := $(SRC_ROOT)/test/make/TestMakeBase.gmk
32DEPS := $(THIS_FILE) \
33    $(SRC_ROOT)/make/common/MakeBase.gmk \
34    #
35
36# Assert two strings are equal
37# 1 - Tested value
38# 2 - Exepected value
39# 3 - Error message
40define assert-equals
41  ifneq ($$(strip $1),$$(strip $2))
42    $$(error $3 - Expected >$$(strip $2)< - Got >$$(strip $1)<)
43  endif
44endef
45
46# On macosx, file system timestamps only have 1 second resultion so must add
47# sleeps to properly test dependencies.
48ifeq ($(OPENJDK_BUILD_OS), macosx)
49  SLEEP_ON_MAC := sleep 1
50endif
51
52OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/make-base
53$(call MakeDir, $(OUTPUT_DIR))
54
55################################################################################
56# Escape $
57ifneq ($(call EscapeDollar, foo$$bar), foo\$$bar)
58  $(error EscapeDollar failed $(call EscapeDollar, foo$$bar) foo\$$bar)
59endif
60
61ESCAPE_DOLLAR_DIR := $(OUTPUT_DIR)/escape-dollar
62
63$(ESCAPE_DOLLAR_DIR)/_escape_dollar: $(DEPS)
64	$(RM) -r $(@D)
65	$(MKDIR) -p $(@D)
66	$(ECHO) foo\$$bar > $(@D)/file1
67	$(ECHO) $(call EscapeDollar, foo$$bar) > $(@D)/file2
68	$(ECHO) $(call EscapeDollar, foo\$$bar) > $(@D)/file3
69	$(DIFF) $(@D)/file1 $(@D)/file2
70	$(DIFF) $(@D)/file1 $(@D)/file3
71	$(TOUCH) $@
72
73TEST_TARGETS += $(ESCAPE_DOLLAR_DIR)/_escape_dollar
74
75################################################################################
76# Test containing and not-containing
77
78CONT_LIST := foo bar baz foobar foobaz
79
80# Param 1 - string to look for
81# Param 2 - expected result
82define TestContaining
83  value := $$(call containing, $1, $(CONT_LIST))
84  ifneq ($$(value), $2)
85    $$(info (call containing, $1, $(CONT_LIST)))
86    $$(error result >$$(value)<, expected >$2<)
87  endif
88endef
89
90$(eval $(call TestContaining,bar,bar foobar))
91$(eval $(call TestContaining,foo bar,foo bar foobar foobaz))
92
93# Param 1 - string to look for
94# Param 2 - expected result
95define TestNotContaining
96  value := $$(call not-containing, $1, $(CONT_LIST))
97  ifneq ($$(value), $2)
98    $$(info (call not-containing, $1, $(CONT_LIST)))
99    $$(error result >$$(value)<, expected >$2<)
100  endif
101endef
102
103$(eval $(call TestNotContaining,bar,foo baz foobaz))
104$(eval $(call TestNotContaining,foo bar,baz))
105
106################################################################################
107# Test Equals
108
109EQUALS_VALUE1 := value1$(SPACE)
110EQUALS_VALUE2 := value2
111
112ifneq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE2)), )
113  $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE2)< are equal)
114endif
115
116ifeq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE1)), )
117  $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE1)< are not equal)
118endif
119
120################################################################################
121# Test remove-prefixes
122
123$(eval $(call assert-equals, \
124    $(call remove-prefixes, pre, prefix postfix), fix postfix, \
125    Prefixes not properly removed))
126
127$(eval $(call assert-equals, \
128    $(call remove-prefixes, pre post, prefix postfix), fix fix, \
129    Prefixes not properly removed))
130
131################################################################################
132# Test ShellQuote
133
134SHELL_QUOTE_VALUE := foo '""' "''" bar
135SHELL_QUOTE_RESULT := $(shell $(ECHO) $(call ShellQuote, \
136    $(SHELL_QUOTE_VALUE)))
137
138ifneq ($(SHELL_QUOTE_VALUE), $(SHELL_QUOTE_RESULT))
139  $(error Expected: >$(SHELL_QUOTE_VALUE)< - Result: >$(SHELL_QUOTE_RESULT)<)
140endif
141
142################################################################################
143# Test read and write to file
144
145READ_WRITE_FILE := $(OUTPUT_DIR)/read-write
146READ_WRITE_VALUE := foo '""' "''" \t\n\\ bar
147$(call WriteFile, $(READ_WRITE_VALUE), $(READ_WRITE_FILE))
148READ_WRITE_RESULT := $(call ReadFile, $(READ_WRITE_FILE))
149
150ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
151  $(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
152endif
153
154################################################################################
155# Test creating dependencies on make variables
156
157VARDEP_DIR := $(OUTPUT_DIR)/vardep
158VARDEP_SRC_FILE := $(VARDEP_DIR)/src-file
159VARDEP_TARGET_FILE := $(VARDEP_DIR)/target-file
160VARDEP_FLAG_FILE := $(VARDEP_DIR)/flag-file
161
162$(VARDEP_DIR)/src-file:
163	$(MKDIR) -p $(@D)
164	$(ECHO) "some string XXX" > $@
165
166$(VARDEP_TARGET_FILE): $(VARDEP_DIR)/src-file \
167    $(call DependOnVariable, VARDEP_TEST_VAR)
168	$(MKDIR) -p $(@D)
169	$(SED) -e 's/XXX/$(VARDEP_TEST_VAR)/g' $< > $@
170	$(TOUCH) $(VARDEP_FLAG_FILE)
171
172test-vardep:
173	$(RM) $(VARDEP_SRC_FILE) $(VARDEP_TARGET_FILE) $(VARDEP_FLAG_FILE)
174        #
175        # Simply create the target file and verify that it has the correct value
176        #
177	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value1 $(VARDEP_TARGET_FILE)
178	$(PRINTF) "Expecting value1: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
179	test "some string value1" = "`$(CAT) $(VARDEP_DIR)/target-file`"
180	test -e $(VARDEP_FLAG_FILE)
181        #
182        # Make the target file again and verify that the value is updated with
183        # the new value
184        #
185	$(SLEEP_ON_MAC)
186	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
187	$(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
188	test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
189	test -e $(VARDEP_FLAG_FILE)
190        #
191        # Make the target again with the same value and verify that the recipe
192        # was never run by checking that the flag file was not recreated
193        #
194	$(SLEEP_ON_MAC)
195	$(RM) $(VARDEP_FLAG_FILE)
196	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
197	$(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
198	test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
199	test ! -e $(VARDEP_FLAG_FILE)
200        #
201        # Test running with spaces at the end and the middle of the value
202        # and verify that the file isn't rewritten the second time
203        #
204	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3  foo " $(VARDEP_TARGET_FILE)
205	$(RM) $(VARDEP_FLAG_FILE)
206	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3 foo" $(VARDEP_TARGET_FILE)
207	test ! -e $(VARDEP_FLAG_FILE)
208	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=" value3  foo" $(VARDEP_TARGET_FILE)
209	test ! -e $(VARDEP_FLAG_FILE)
210        #
211        # Test including some problematic characters
212	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR='value4 \$$ORIGIN' $(VARDEP_TARGET_FILE)
213	$(RM) $(VARDEP_FLAG_FILE)
214	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR='value4 \$$ORIGIN' $(VARDEP_TARGET_FILE)
215	test ! -e $(VARDEP_FLAG_FILE)
216
217# Test specifying a specific value file to store variable in
218VARDEP_VALUE_FILE := $(VARDEP_DIR)/value-file
219VARDEP_TEST_VAR2 := value3
220
221VARDEP_RETURN_VALUE := $(call DependOnVariable, VARDEP_TEST_VAR2, $(VARDEP_VALUE_FILE))
222$(eval $(call assert-equals, $(VARDEP_RETURN_VALUE), $(VARDEP_VALUE_FILE), \
223    Wrong filename returned))
224-include $(VARDEP_VALUE_FILE)
225$(eval $(call assert-equals, $(VARDEP_TEST_VAR2_old), $(VARDEP_TEST_VAR2), \
226    Wrong contents in vardeps file))
227
228# Test with a variable value containing some problematic characters
229VARDEP_TEST_VAR3 := foo '""' "''" bar \$$ORIGIN &\#x00a9
230VARDEP_VALUE_FILE := $(call DependOnVariable, VARDEP_TEST_VAR3)
231-include $(VARDEP_VALUE_FILE)
232$(eval $(call assert-equals, $(call EscapeHash,$(VARDEP_TEST_VAR3_old)), \
233    $(call EscapeHash,$(VARDEP_TEST_VAR3)), \
234    Wrong contents in vardep file))
235
236TEST_TARGETS += test-vardep
237
238################################################################################
239# Test sequence
240
241ifneq ($(call sequence, 1, 1), 1)
242  $(error Sequence 1, 1 should be "1", but was $(call sequence, 1, 1))
243endif
244
245ifneq ($(call sequence, 2, 3), 2 3)
246  $(error Sequence 2, 3 should be "2 3", but was $(call sequence, 2, 3))
247endif
248
249ifneq ($(call sequence, 4, 9), 4 5 6 7 8 9)
250  $(error Sequence 4, 9 should be "4 5 6 7 8 9", but was $(call sequence, 4, 9))
251endif
252
253ifneq ($(call sequence, 5, 15), 5 6 7 8 9 10 11 12 13 14 15)
254  $(error Sequence 5, 15 should be "5 6 7 8 9 10 11 12 13 14 15", \
255      but was $(call sequence, 5, 15))
256endif
257
258################################################################################
259# Test that PathList is safe when called multiple nested times.
260
261PATHLIST_INPUT := foo bar baz
262
263$(eval $(call assert-equals, \
264    $(call PathList, $(call PathList, $(PATHLIST_INPUT))), \
265    $(call PathList, $(PATHLIST_INPUT)), \
266    PathList call not safe for calling twice))
267
268################################################################################
269# Test FindCommonPathPrefix
270
271$(eval $(call assert-equals, \
272    $(call FindCommonPathPrefix, /foo/bar/baz, /foo/bar/banan), \
273    /foo/bar, \
274    FindCommonPathPrefix, \
275))
276
277$(eval $(call assert-equals, \
278    $(call FindCommonPathPrefix, /foo/bar/baz, /foo/bar), \
279    /foo/bar, \
280    FindCommonPathPrefix, \
281))
282
283$(eval $(call assert-equals, \
284    $(call FindCommonPathPrefix, /foo/bar/baz, /foo/bar/), \
285    /foo/bar, \
286    FindCommonPathPrefix, \
287))
288
289$(eval $(call assert-equals, \
290    $(call FindCommonPathPrefix, foo/bar/baz, foo/bar/banan), \
291    foo/bar, \
292    FindCommonPathPrefix, \
293))
294
295$(eval $(call assert-equals, \
296    $(call FindCommonPathPrefix, foo/bar/baz, /foo/bar/banan), \
297    , \
298    FindCommonPathPrefix, \
299))
300
301################################################################################
302# DirToDotDot
303
304$(eval $(call assert-equals, \
305    $(call DirToDotDot, foo/bar/baz/), \
306    ../../.., \
307    DirToDotDot, \
308))
309
310$(eval $(call assert-equals, \
311    $(call DirToDotDot, foo/bar), \
312    ../.., \
313    DirToDotDot, \
314))
315
316$(eval $(call assert-equals, \
317    $(call DirToDotDot, /foo), \
318    .., \
319    DirToDotDot, \
320))
321
322################################################################################
323# RelativePath
324
325$(eval $(call assert-equals, \
326    $(call RelativePath, foo/bar/baz, foo/bar/banan), \
327    ../baz, \
328    RelativePath, \
329))
330
331$(eval $(call assert-equals, \
332    $(call RelativePath, foo/bar/baz/banan/kung, foo/bar/banan/kung), \
333    ../../baz/banan/kung, \
334    RelativePath, \
335))
336
337$(eval $(call assert-equals, \
338    $(call RelativePath, /foo/bar/baz/banan/kung, /foo/bar/banan/kung/), \
339    ../../baz/banan/kung, \
340    RelativePath, \
341))
342
343################################################################################
344# Test ParseKeywordVariable
345
346KWBASE := APA=banan;GURKA=tomat;COUNT=1%202%203%204%205;SUM=1+2+3+4+5;MANY_WORDS=I have the best words.
347
348$(eval $(call ParseKeywordVariable, KWBASE, \
349    KEYWORDS := APA GURKA SUM, \
350    STRING_KEYWORDS := COUNT MANY_WORDS, \
351))
352
353$(eval $(call assert-equals, \
354    $(KWBASE_APA), \
355    banan, \
356    ParseKeywordVariable failed to parse APA, \
357))
358
359$(eval $(call assert-equals, \
360    $(KWBASE_COUNT), \
361    1 2 3 4 5, \
362    ParseKeywordVariable failed to parse COUNT, \
363))
364
365$(eval $(call assert-equals, \
366    $(KWBASE_SUM), \
367    1+2+3+4+5, \
368    ParseKeywordVariable failed to parse SUM, \
369))
370
371$(eval $(call assert-equals, \
372    $(KWBASE_MANY_WORDS), \
373    I have the best words., \
374    ParseKeywordVariable failed to parse MANY_WORDS, \
375))
376
377# Simulate variable set from command line by using "override"
378override KWBASE_WEIRD_GURKA := paprika
379KWBASE_WEIRD := ;;APA=banan;;;GURKA=apelsin;APA=skansen;;
380
381$(eval $(call ParseKeywordVariable, KWBASE_WEIRD, \
382    KEYWORDS := APA GURKA SUM, \
383    STRING_KEYWORDS := COUNT, \
384))
385
386$(eval $(call assert-equals, \
387    $(KWBASE_WEIRD_APA), \
388    skansen, \
389    ParseKeywordVariable failed to overwrite APA, \
390))
391
392$(eval $(call assert-equals, \
393    $(KWBASE_WEIRD_GURKA), \
394    paprika, \
395    ParseKeywordVariable failed to preserve GURKA, \
396))
397
398################################################################################
399
400all: $(TEST_TARGETS)
401