test/make/TestMakeBase.gmk
author duke
Wed, 05 Jul 2017 20:32:43 +0200
changeset 30411 22902dfebf97
parent 30095 0034766ccb09
child 32553 418e3b5ff477
permissions -rw-r--r--
Merge

#
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

default: all

include $(SPEC)
include MakeBase.gmk

THIS_FILE := $(SRC_ROOT)/test/make/TestMakeBase.gmk
DEPS := $(THIS_FILE) \
    $(SRC_ROOT)/make/common/MakeBase.gmk \
    #

# Assert two strings are equal
# 1 - Tested value
# 2 - Exepected value
# 3 - Error message
define assert-equals
  ifneq ($$(strip $1),$$(strip $2))
    $$(error $3 - Expected >$$(strip $2)< - Got >$$(strip $1)<)
  endif
endef

# On macosx, file system timestamps only have 1 second resultion so must add
# sleeps to properly test dependencies.
ifeq ($(OPENJDK_BUILD_OS), macosx)
  SLEEP_ON_MAC := sleep 1
endif

OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/make-base
$(call MakeDir, $(OUTPUT_DIR))

################################################################################
# Escape $
ifneq ($(call EscapeDollar, foo$$bar), foo\$$bar)
  $(error EscapeDollar failed $(call EscapeDollar, foo$$bar) foo\$$bar)
endif

ESCAPE_DOLLAR_DIR := $(OUTPUT_DIR)/escape-dollar

$(ESCAPE_DOLLAR_DIR)/_escape_dollar: $(DEPS)
	$(RM) -r $(@D)
	$(MKDIR) -p $(@D)
	$(ECHO) foo\$$bar > $(@D)/file1
	$(ECHO) $(call EscapeDollar, foo$$bar) > $(@D)/file2
	$(ECHO) $(call EscapeDollar, foo\$$bar) > $(@D)/file3
	$(DIFF) $(@D)/file1 $(@D)/file2
	$(DIFF) $(@D)/file1 $(@D)/file3
	$(TOUCH) $@

TEST_TARGETS += $(ESCAPE_DOLLAR_DIR)/_escape_dollar

################################################################################
# Test Equals

EQUALS_VALUE1 := value1$(SPACE)
EQUALS_VALUE2 := value2

ifneq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE2)), )
  $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE2)< are equal)
endif

ifeq ($(call equals, $(EQUALS_VALUE1), $(EQUALS_VALUE1)), )
  $(error The strings >$(EQUALS_VALUE1)< and >$(EQUALS_VALUE1)< are not equal)
endif

################################################################################
# Test remove-prefixes

$(eval $(call assert-equals, \
    $(call remove-prefixes, pre, prefix postfix), fix postfix, \
    Prefixes not properly removed))

$(eval $(call assert-equals, \
    $(call remove-prefixes, pre post, prefix postfix), fix fix, \
    Prefixes not properly removed))

################################################################################
# Test ShellQuote

SHELL_QUOTE_VALUE := foo '""' "''" bar
SHELL_QUOTE_RESULT := $(shell $(ECHO) $(call ShellQuote, \
    $(SHELL_QUOTE_VALUE)))

ifneq ($(SHELL_QUOTE_VALUE), $(SHELL_QUOTE_RESULT))
  $(error Expected: >$(SHELL_QUOTE_VALUE)< - Result: >$(SHELL_QUOTE_RESULT)<)
endif

################################################################################
# Test read and write to file

READ_WRITE_FILE := $(OUTPUT_DIR)/read-write
READ_WRITE_VALUE := foo '""' "''" \t\n\\ bar
$(call WriteFile, $(READ_WRITE_VALUE), $(READ_WRITE_FILE))
READ_WRITE_RESULT := $(call ReadFile, $(READ_WRITE_FILE))

ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
  $(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
endif

################################################################################
# Test creating dependencies on make variables

VARDEP_DIR := $(OUTPUT_DIR)/vardep
VARDEP_SRC_FILE := $(VARDEP_DIR)/src-file
VARDEP_TARGET_FILE := $(VARDEP_DIR)/target-file
VARDEP_FLAG_FILE := $(VARDEP_DIR)/flag-file

$(VARDEP_DIR)/src-file:
	$(MKDIR) -p $(@D)
	$(ECHO) "some string XXX" > $@

$(VARDEP_TARGET_FILE): $(VARDEP_DIR)/src-file \
    $(call DependOnVariable, VARDEP_TEST_VAR)
	$(MKDIR) -p $(@D)
	$(SED) -e 's/XXX/$(VARDEP_TEST_VAR)/g' $< > $@
	$(TOUCH) $(VARDEP_FLAG_FILE)

test-vardep:
	$(RM) $(VARDEP_SRC_FILE) $(VARDEP_TARGET_FILE) $(VARDEP_FLAG_FILE)
        #
        # Simply create the target file and verify that it has the correct value
        #
	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value1 $(VARDEP_TARGET_FILE)
	$(PRINTF) "Expecting value1: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
	test "some string value1" = "`$(CAT) $(VARDEP_DIR)/target-file`"
	test -e $(VARDEP_FLAG_FILE)
        #
        # Make the target file again and verify that the value is updated with 
        # the new value
        #
	$(SLEEP_ON_MAC)
	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
	$(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
	test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
	test -e $(VARDEP_FLAG_FILE)
        #
        # Make the target again with the same value and verify that the recipe
        # was never run by checking that the flag file was not recreated
        #
	$(SLEEP_ON_MAC)
	$(RM) $(VARDEP_FLAG_FILE)
	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=value2 $(VARDEP_TARGET_FILE)
	$(PRINTF) "Expecting value2: %s\n" "`$(CAT) $(VARDEP_DIR)/target-file`"
	test "some string value2" = "`$(CAT) $(VARDEP_DIR)/target-file`"
	test ! -e $(VARDEP_FLAG_FILE)
        #
        # Test running with spaces at the end and the middle of the value
        # and verify that the file isn't rewritten the second time
        #
	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3  foo " $(VARDEP_TARGET_FILE)
	$(RM) $(VARDEP_FLAG_FILE)
	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR="value3 foo" $(VARDEP_TARGET_FILE)
	test ! -e $(VARDEP_FLAG_FILE)
	$(MAKE) -f $(THIS_FILE) VARDEP_TEST_VAR=" value3  foo" $(VARDEP_TARGET_FILE)
	test ! -e $(VARDEP_FLAG_FILE)

# Test specifying a specific value file to store variable in
VARDEP_VALUE_FILE := $(VARDEP_DIR)/value-file
VARDEP_TEST_VAR2 := value3

VARDEP_RETURN_VALUE := $(call DependOnVariable, VARDEP_TEST_VAR2, $(VARDEP_VALUE_FILE))
ifneq ($(VARDEP_VALUE_FILE), $(VARDEP_RETURN_VALUE))
  $(error Expected: $(VARDEP_VALUE_FILE) - DependOnVariable: $(VARDEP_RETURN_VALUE))
endif
VARDEP_FILE_CONTENTS := $(shell $(CAT) $(VARDEP_VALUE_FILE))
ifneq ($(VARDEP_TEST_VAR2), $(VARDEP_FILE_CONTENTS))
  $(error Expected: $(VARDEP_TEST_VAR2) - DependOnVariable file contained: \
      $(VARDEP_FILE_CONTENTS))
endif

# Test with a variable value containing some problematic characters
VARDEP_TEST_VAR3 := foo '""' "''" bar
VARDEP_VALUE_FILE := $(call DependOnVariable, VARDEP_TEST_VAR3)
VARDEP_FILE_CONTENTS := $(shell $(CAT) $(VARDEP_VALUE_FILE))
ifneq ($(VARDEP_TEST_VAR3), $(VARDEP_FILE_CONTENTS))
  $(error Expected: >$(VARDEP_TEST_VAR3)< - DependOnVariable file contained: \
      >$(VARDEP_FILE_CONTENTS)<)
endif

TEST_TARGETS += test-vardep

################################################################################
# Test sequence

ifneq ($(call sequence, 1, 1), 1)
  $(error Sequence 1, 1 should be "1", but was $(call sequence, 1, 1))
endif

ifneq ($(call sequence, 2, 3), 2 3)
  $(error Sequence 2, 3 should be "2 3", but was $(call sequence, 2, 3))
endif

ifneq ($(call sequence, 4, 9), 4 5 6 7 8 9)
  $(error Sequence 4, 9 should be "4 5 6 7 8 9", but was $(call sequence, 4, 9))
endif

ifneq ($(call sequence, 5, 15), 5 6 7 8 9 10 11 12 13 14 15)
  $(error Sequence 5, 15 should be "5 6 7 8 9 10 11 12 13 14 15", \
      but was $(call sequence, 5, 15))
endif

all: $(TEST_TARGETS)