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 
  26 default: all
  27 
  28 include $(SPEC)
  29 include MakeBase.gmk
  30 
  31 THIS_FILE := $(SRC_ROOT)/test/make/TestMakeBase.gmk
  32 DEPS := $(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
  40 define assert-equals
  41   ifneq ($$(strip $1),$$(strip $2))
  42     $$(error $3 - Expected >$$(strip $2)< - Got >$$(strip $1)<)
  43   endif
  44 endef
  45 
  46 # On macosx, file system timestamps only have 1 second resultion so must add
  47 # sleeps to properly test dependencies.
  48 ifeq ($(OPENJDK_BUILD_OS), macosx)
  49   SLEEP_ON_MAC := sleep 1
  50 endif
  51 
  52 OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/make-base
  53 $(call MakeDir, $(OUTPUT_DIR))
  54 
  55 ################################################################################
  56 # Escape $
  57 ifneq ($(call EscapeDollar, foo$$bar), foo\$$bar)
  58   $(error EscapeDollar failed $(call EscapeDollar, foo$$bar) foo\$$bar)
  59 endif
  60 
  61 ESCAPE_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 
  73 TEST_TARGETS += $(ESCAPE_DOLLAR_DIR)/_escape_dollar
  74 
  75 ################################################################################
  76 # Test containing and not-containing
  77 
  78 CONT_LIST := foo bar baz foobar foobaz
  79 
  80 # Param 1 - string to look for
  81 # Param 2 - expected result
  82 define 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
  88 endef
  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
  95 define 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
 101 endef
 102 
 103 $(eval $(call TestNotContaining,bar,foo baz foobaz))
 104 $(eval $(call TestNotContaining,foo bar,baz))
 105 
 106 ################################################################################
 107 # Test Equals
 108 
 109 EQUALS_VALUE1 := value1$(SPACE)
 110 EQUALS_VALUE2 := value2
 111 
 112 ifneq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE2)), )
 113   $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE2)< are equal)
 114 endif
 115 
 116 ifeq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE1)), )
 117   $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE1)< are not equal)
 118 endif
 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 
 134 SHELL_QUOTE_VALUE := foo '""' "''" bar
 135 SHELL_QUOTE_RESULT := $(shell $(ECHO) $(call ShellQuote, \
 136     $(SHELL_QUOTE_VALUE)))
 137 
 138 ifneq ($(SHELL_QUOTE_VALUE), $(SHELL_QUOTE_RESULT))
 139   $(error Expected: >$(SHELL_QUOTE_VALUE)< - Result: >$(SHELL_QUOTE_RESULT)<)
 140 endif
 141 
 142 ################################################################################
 143 # Test read and write to file
 144 
 145 READ_WRITE_FILE := $(OUTPUT_DIR)/read-write
 146 READ_WRITE_VALUE := foo '""' "''" \t\n\\ bar
 147 $(call WriteFile, $(READ_WRITE_VALUE), $(READ_WRITE_FILE))
 148 READ_WRITE_RESULT := $(call ReadFile, $(READ_WRITE_FILE))
 149 
 150 ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
 151   $(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
 152 endif
 153 
 154 ################################################################################
 155 # Test creating dependencies on make variables
 156 
 157 VARDEP_DIR := $(OUTPUT_DIR)/vardep
 158 VARDEP_SRC_FILE := $(VARDEP_DIR)/src-file
 159 VARDEP_TARGET_FILE := $(VARDEP_DIR)/target-file
 160 VARDEP_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 
 172 test-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
 218 VARDEP_VALUE_FILE := $(VARDEP_DIR)/value-file
 219 VARDEP_TEST_VAR2 := value3
 220 
 221 VARDEP_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
 229 VARDEP_TEST_VAR3 := foo '""' "''" bar \$$ORIGIN
 230 VARDEP_VALUE_FILE := $(call DependOnVariable, VARDEP_TEST_VAR3)
 231 -include $(VARDEP_VALUE_FILE)
 232 $(eval $(call assert-equals, $(VARDEP_TEST_VAR3_old), $(VARDEP_TEST_VAR3), \
 233     Wrong contents in vardep file))
 234 
 235 TEST_TARGETS += test-vardep
 236 
 237 ################################################################################
 238 # Test sequence
 239 
 240 ifneq ($(call sequence, 1, 1), 1)
 241   $(error Sequence 1, 1 should be "1", but was $(call sequence, 1, 1))
 242 endif
 243 
 244 ifneq ($(call sequence, 2, 3), 2 3)
 245   $(error Sequence 2, 3 should be "2 3", but was $(call sequence, 2, 3))
 246 endif
 247 
 248 ifneq ($(call sequence, 4, 9), 4 5 6 7 8 9)
 249   $(error Sequence 4, 9 should be "4 5 6 7 8 9", but was $(call sequence, 4, 9))
 250 endif
 251 
 252 ifneq ($(call sequence, 5, 15), 5 6 7 8 9 10 11 12 13 14 15)
 253   $(error Sequence 5, 15 should be "5 6 7 8 9 10 11 12 13 14 15", \
 254       but was $(call sequence, 5, 15))
 255 endif
 256 
 257 all: $(TEST_TARGETS)