TestMakeBase.gmk revision 1299:8720fa7696cb
1#
2# Copyright (c) 2014, 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# On macosx, file system timestamps only have 1 second resultion so must add
37# sleeps to properly test dependencies.
38ifeq ($(OPENJDK_BUILD_OS), macosx)
39  SLEEP_ON_MAC := sleep 1
40endif
41
42OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/make-base
43$(call MakeDir, $(OUTPUT_DIR))
44
45################################################################################
46# Escape $
47ifneq ($(call EscapeDollar, foo$$bar), foo\$$bar)
48  $(error EscapeDollar failed $(call EscapeDollar, foo$$bar) foo\$$bar)
49endif
50
51ESCAPE_DOLLAR_DIR := $(OUTPUT_DIR)/escape-dollar
52
53$(ESCAPE_DOLLAR_DIR)/_escape_dollar: $(DEPS)
54	$(RM) -r $(@D)
55	$(MKDIR) -p $(@D)
56	$(ECHO) foo\$$bar > $(@D)/file1
57	$(ECHO) $(call EscapeDollar, foo$$bar) > $(@D)/file2
58	$(ECHO) $(call EscapeDollar, foo\$$bar) > $(@D)/file3
59	$(DIFF) $(@D)/file1 $(@D)/file2
60	$(DIFF) $(@D)/file1 $(@D)/file3
61	$(TOUCH) $@
62
63TEST_TARGETS += $(ESCAPE_DOLLAR_DIR)/_escape_dollar
64
65################################################################################
66# Test ShellQuote
67
68SHELL_QUOTE_VALUE := foo '""' "''" bar
69SHELL_QUOTE_RESULT := $(shell $(ECHO) $(call ShellQuote, \
70    $(SHELL_QUOTE_VALUE)))
71
72ifneq ($(SHELL_QUOTE_VALUE), $(SHELL_QUOTE_RESULT))
73  $(error Expected: >$(SHELL_QUOTE_VALUE)< - Result: >$(SHELL_QUOTE_RESULT)<)
74endif
75
76################################################################################
77# Test read and write to file
78
79READ_WRITE_FILE := $(OUTPUT_DIR)/read-write
80READ_WRITE_VALUE := foo '""' "''" \t\n\\ bar
81$(call WriteFile, $(READ_WRITE_VALUE), $(READ_WRITE_FILE))
82READ_WRITE_RESULT := $(call ReadFile, $(READ_WRITE_FILE))
83
84ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
85  $(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
86endif
87
88################################################################################
89# Test creating dependencies on make variables
90
91VARDEP_DIR := $(OUTPUT_DIR)/vardep
92VARDEP_SRC_FILE := $(VARDEP_DIR)/src-file
93VARDEP_TARGET_FILE := $(VARDEP_DIR)/target-file
94VARDEP_FLAG_FILE := $(VARDEP_DIR)/flag-file
95
96$(VARDEP_DIR)/src-file:
97	$(MKDIR) -p $(@D)
98	$(ECHO) "some string XXX" > $@
99
100$(VARDEP_TARGET_FILE): $(VARDEP_DIR)/src-file \
101    $(call DependOnVariable, VARDEP_TEST_VAR)
102	$(MKDIR) -p $(@D)
103	$(SED) -e 's/XXX/$(VARDEP_TEST_VAR)/g' $< > $@
104	$(TOUCH) $(VARDEP_FLAG_FILE)
105
106test-vardep:
107	$(RM) $(VARDEP_SRC_FILE) $(VARDEP_TARGET_FILE) $(VARDEP_FLAG_FILE)
108        #
109        # Simply create the target file and verify that it has the correct value
110        #
111	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value1 $(VARDEP_TARGET_FILE)
112	$(PRINTF) "Expecting value1: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
113	test "some string value1" = "`$(CAT) $(VARDEP_DIR)/target-file`"
114	test -e $(VARDEP_FLAG_FILE)
115        #
116        # Make the target file again and verify that the value is updated with 
117        # the new value
118        #
119	$(SLEEP_ON_MAC)
120	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
121	$(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
122	test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
123	test -e $(VARDEP_FLAG_FILE)
124        #
125        # Make the target again with the same value and verify that the recipe
126        # was never run by checking that the flag file was not recreated
127        #
128	$(SLEEP_ON_MAC)
129	$(RM) $(VARDEP_FLAG_FILE)
130	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
131	$(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
132	test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
133	test ! -e $(VARDEP_FLAG_FILE)
134        #
135        # Test running with spaces at the end and the middle of the value
136        # and verify that the file isn't rewritten the second time
137        #
138	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3  foo " $(VARDEP_TARGET_FILE)
139	$(RM) $(VARDEP_FLAG_FILE)
140	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3 foo" $(VARDEP_TARGET_FILE)
141	test ! -e $(VARDEP_FLAG_FILE)
142	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=" value3  foo" $(VARDEP_TARGET_FILE)
143	test ! -e $(VARDEP_FLAG_FILE)
144
145# Test specifying a specific value file to store variable in
146VARDEP_VALUE_FILE := $(VARDEP_DIR)/value-file
147VARDEP_TEST_VAR2 := value3
148
149VARDEP_RETURN_VALUE := $(call DependOnVariable, VARDEP_TEST_VAR2, $(VARDEP_VALUE_FILE))
150ifneq ($(VARDEP_VALUE_FILE), $(VARDEP_RETURN_VALUE))
151  $(error Expected: $(VARDEP_VALUE_FILE) - DependOnVariable: $(VARDEP_RETURN_VALUE))
152endif
153VARDEP_FILE_CONTENTS := $(shell $(CAT) $(VARDEP_VALUE_FILE))
154ifneq ($(VARDEP_TEST_VAR2), $(VARDEP_FILE_CONTENTS))
155  $(error Expected: $(VARDEP_TEST_VAR2) - DependOnVariable file contained: \
156      $(VARDEP_FILE_CONTENTS))
157endif
158
159# Test with a variable value containing some problematic characters
160VARDEP_TEST_VAR3 := foo '""' "''" bar
161VARDEP_VALUE_FILE := $(call DependOnVariable, VARDEP_TEST_VAR3)
162VARDEP_FILE_CONTENTS := $(shell $(CAT) $(VARDEP_VALUE_FILE))
163ifneq ($(VARDEP_TEST_VAR3), $(VARDEP_FILE_CONTENTS))
164  $(error Expected: >$(VARDEP_TEST_VAR3)< - DependOnVariable file contained: \
165      >$(VARDEP_FILE_CONTENTS)<)
166endif
167
168TEST_TARGETS += test-vardep
169
170################################################################################
171
172all: $(TEST_TARGETS)
173