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 # On macosx, file system timestamps only have 1 second resultion so must add
  37 # sleeps to properly test dependencies.
  38 ifeq ($(OPENJDK_BUILD_OS), macosx)
  39   SLEEP_ON_MAC := sleep 1
  40 endif
  41 
  42 OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/make-base
  43 $(call MakeDir, $(OUTPUT_DIR))
  44 
  45 ################################################################################
  46 # Escape $
  47 ifneq ($(call EscapeDollar, foo$$bar), foo\$$bar)
  48   $(error EscapeDollar failed $(call EscapeDollar, foo$$bar) foo\$$bar)
  49 endif
  50 
  51 ESCAPE_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 
  63 TEST_TARGETS += $(ESCAPE_DOLLAR_DIR)/_escape_dollar
  64 
  65 ################################################################################
  66 # Test Equals
  67 
  68 EQUALS_VALUE1 := value1$(SPACE)
  69 EQUALS_VALUE2 := value2
  70 
  71 ifneq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE2)), )
  72   $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE2)< are equal)
  73 endif
  74 
  75 ifeq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE1)), )
  76   $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE1)< are not equal)
  77 endif
  78 
  79 ################################################################################
  80 # Test ShellQuote
  81 
  82 SHELL_QUOTE_VALUE := foo '""' "''" bar
  83 SHELL_QUOTE_RESULT := $(shell $(ECHO) $(call ShellQuote, \
  84     $(SHELL_QUOTE_VALUE)))
  85 
  86 ifneq ($(SHELL_QUOTE_VALUE), $(SHELL_QUOTE_RESULT))
  87   $(error Expected: >$(SHELL_QUOTE_VALUE)< - Result: >$(SHELL_QUOTE_RESULT)<)
  88 endif
  89 
  90 ################################################################################
  91 # Test read and write to file
  92 
  93 READ_WRITE_FILE := $(OUTPUT_DIR)/read-write
  94 READ_WRITE_VALUE := foo '""' "''" \t\n\\ bar
  95 $(call WriteFile, $(READ_WRITE_VALUE), $(READ_WRITE_FILE))
  96 READ_WRITE_RESULT := $(call ReadFile, $(READ_WRITE_FILE))
  97 
  98 ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
  99   $(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
 100 endif
 101 
 102 ################################################################################
 103 # Test creating dependencies on make variables
 104 
 105 VARDEP_DIR := $(OUTPUT_DIR)/vardep
 106 VARDEP_SRC_FILE := $(VARDEP_DIR)/src-file
 107 VARDEP_TARGET_FILE := $(VARDEP_DIR)/target-file
 108 VARDEP_FLAG_FILE := $(VARDEP_DIR)/flag-file
 109 
 110 $(VARDEP_DIR)/src-file:
 111         $(MKDIR) -p $(@D)
 112         $(ECHO) "some string XXX" > $@
 113 
 114 $(VARDEP_TARGET_FILE): $(VARDEP_DIR)/src-file \
 115     $(call DependOnVariable, VARDEP_TEST_VAR)
 116         $(MKDIR) -p $(@D)
 117         $(SED) -e 's/XXX/$(VARDEP_TEST_VAR)/g' $< > $@
 118         $(TOUCH) $(VARDEP_FLAG_FILE)
 119 
 120 test-vardep:
 121         $(RM) $(VARDEP_SRC_FILE) $(VARDEP_TARGET_FILE) $(VARDEP_FLAG_FILE)
 122         #
 123         # Simply create the target file and verify that it has the correct value
 124         #
 125         $(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value1 $(VARDEP_TARGET_FILE)
 126         $(PRINTF) "Expecting value1: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
 127         test "some string value1" = "`$(CAT) $(VARDEP_DIR)/target-file`"
 128         test -e $(VARDEP_FLAG_FILE)
 129         #
 130         # Make the target file again and verify that the value is updated with 
 131         # the new value
 132         #
 133         $(SLEEP_ON_MAC)
 134         $(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
 135         $(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
 136         test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
 137         test -e $(VARDEP_FLAG_FILE)
 138         #
 139         # Make the target again with the same value and verify that the recipe
 140         # was never run by checking that the flag file was not recreated
 141         #
 142         $(SLEEP_ON_MAC)
 143         $(RM) $(VARDEP_FLAG_FILE)
 144         $(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
 145         $(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
 146         test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
 147         test ! -e $(VARDEP_FLAG_FILE)
 148         #
 149         # Test running with spaces at the end and the middle of the value
 150         # and verify that the file isn't rewritten the second time
 151         #
 152         $(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3  foo " $(VARDEP_TARGET_FILE)
 153         $(RM) $(VARDEP_FLAG_FILE)
 154         $(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3 foo" $(VARDEP_TARGET_FILE)
 155         test ! -e $(VARDEP_FLAG_FILE)
 156         $(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=" value3  foo" $(VARDEP_TARGET_FILE)
 157         test ! -e $(VARDEP_FLAG_FILE)
 158 
 159 # Test specifying a specific value file to store variable in
 160 VARDEP_VALUE_FILE := $(VARDEP_DIR)/value-file
 161 VARDEP_TEST_VAR2 := value3
 162 
 163 VARDEP_RETURN_VALUE := $(call DependOnVariable, VARDEP_TEST_VAR2, $(VARDEP_VALUE_FILE))
 164 ifneq ($(VARDEP_VALUE_FILE), $(VARDEP_RETURN_VALUE))
 165   $(error Expected: $(VARDEP_VALUE_FILE) - DependOnVariable: $(VARDEP_RETURN_VALUE))
 166 endif
 167 VARDEP_FILE_CONTENTS := $(shell $(CAT) $(VARDEP_VALUE_FILE))
 168 ifneq ($(VARDEP_TEST_VAR2), $(VARDEP_FILE_CONTENTS))
 169   $(error Expected: $(VARDEP_TEST_VAR2) - DependOnVariable file contained: \
 170       $(VARDEP_FILE_CONTENTS))
 171 endif
 172 
 173 # Test with a variable value containing some problematic characters
 174 VARDEP_TEST_VAR3 := foo '""' "''" bar
 175 VARDEP_VALUE_FILE := $(call DependOnVariable, VARDEP_TEST_VAR3)
 176 VARDEP_FILE_CONTENTS := $(shell $(CAT) $(VARDEP_VALUE_FILE))
 177 ifneq ($(VARDEP_TEST_VAR3), $(VARDEP_FILE_CONTENTS))
 178   $(error Expected: >$(VARDEP_TEST_VAR3)< - DependOnVariable file contained: \
 179       >$(VARDEP_FILE_CONTENTS)<)
 180 endif
 181 
 182 TEST_TARGETS += test-vardep
 183 
 184 ################################################################################
 185 # Test sequence
 186 
 187 ifneq ($(call sequence, 1, 1), 1)
 188   $(error Sequence 1, 1 should be "1", but was $(call sequence, 1, 1))
 189 endif
 190 
 191 ifneq ($(call sequence, 2, 3), 2 3)
 192   $(error Sequence 2, 3 should be "2 3", but was $(call sequence, 2, 3))
 193 endif
 194 
 195 ifneq ($(call sequence, 4, 9), 4 5 6 7 8 9)
 196   $(error Sequence 4, 9 should be "4 5 6 7 8 9", but was $(call sequence, 4, 9))
 197 endif
 198 
 199 ifneq ($(call sequence, 5, 15), 5 6 7 8 9 10 11 12 13 14 15)
 200   $(error Sequence 5, 15 should be "5 6 7 8 9 10 11 12 13 14 15", \
 201       but was $(call sequence, 5, 15))
 202 endif
 203 
 204 all: $(TEST_TARGETS)