8301753: AppendFile/WriteFile has differences between make 3.81 and 4+

Backport-of: a39cf2e3b242298fbf5fafdb8aa9b5d4562061ef
This commit is contained in:
Roman Marchenko 2024-04-26 09:00:04 +00:00 committed by Aleksey Shipilev
parent 3f5e5369c8
commit 54e012001e
2 changed files with 58 additions and 2 deletions

View File

@ -257,7 +257,7 @@ ifeq ($(HAS_FILE_FUNCTION), true)
else
# Use printf to get consistent behavior on all platforms.
WriteFile = \
$(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
$(shell $(PRINTF) "%s\n" $(strip $(call ShellQuote, $1)) > $2)
endif
# Param 1 - Text to write
@ -268,5 +268,5 @@ ifeq ($(HAS_FILE_FUNCTION), true)
else
# Use printf to get consistent behavior on all platforms.
AppendFile = \
$(shell $(PRINTF) "%s" $(call ShellQuote, $1) >> $2)
$(shell $(PRINTF) "%s\n" $(strip $(call ShellQuote, $1)) >> $2)
endif

View File

@ -171,6 +171,62 @@ ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
$(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
endif
TEST_STRING_1 := 1234
TEST_STRING_2 := 1234$(NEWLINE)
TEST_STRING_3 := 1234$(NEWLINE)$(NEWLINE)
# Writing a string ending in newline should not add a newline, but if it does
# not, a newline should be added. We check this by verifying that the size of the
# file is 5 characters for both test strings.
TEST_FILE_1 := $(OUTPUT_DIR)/write-file-1
TEST_FILE_2 := $(OUTPUT_DIR)/write-file-2
TEST_FILE_3 := $(OUTPUT_DIR)/write-file-3
$(call WriteFile, $(TEST_STRING_1), $(TEST_FILE_1))
$(call WriteFile, $(TEST_STRING_2), $(TEST_FILE_2))
$(call WriteFile, $(TEST_STRING_3), $(TEST_FILE_3))
TEST_FILE_1_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_1)))
TEST_FILE_2_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_2)))
TEST_FILE_3_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_3)))
ifneq ($(TEST_FILE_1_SIZE), 5)
$(error Expected file size 5 for WriteFile 1, got $(TEST_FILE_1_SIZE))
endif
ifneq ($(TEST_FILE_2_SIZE), 5)
$(error Expected file size 5 for WriteFile 2, got $(TEST_FILE_2_SIZE))
endif
ifneq ($(TEST_FILE_3_SIZE), 5)
$(error Expected file size 5 for WriteFile 3, got $(TEST_FILE_3_SIZE))
endif
# Also test append (assumes WriteFile works as expected)
$(call WriteFile, $(TEST_STRING_1), $(TEST_FILE_1))
$(call AppendFile, $(TEST_STRING_1), $(TEST_FILE_1))
$(call AppendFile, $(TEST_STRING_1), $(TEST_FILE_1))
$(call WriteFile, $(TEST_STRING_2), $(TEST_FILE_2))
$(call AppendFile, $(TEST_STRING_2), $(TEST_FILE_2))
$(call AppendFile, $(TEST_STRING_2), $(TEST_FILE_2))
$(call WriteFile, $(TEST_STRING_3), $(TEST_FILE_3))
$(call AppendFile, $(TEST_STRING_3), $(TEST_FILE_3))
$(call AppendFile, $(TEST_STRING_3), $(TEST_FILE_3))
TEST_FILE_1_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_1)))
TEST_FILE_2_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_2)))
TEST_FILE_3_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_3)))
ifneq ($(TEST_FILE_1_SIZE), 15)
$(error Expected file size 15 for AppendFile 1, got $(TEST_FILE_1_SIZE))
endif
ifneq ($(TEST_FILE_2_SIZE), 15)
$(error Expected file size 15 for AppendFile 2, got $(TEST_FILE_2_SIZE))
endif
ifneq ($(TEST_FILE_3_SIZE), 15)
$(error Expected file size 15 for AppendFile 3, got $(TEST_FILE_3_SIZE))
endif
################################################################################
# Test creating dependencies on make variables