Skip to content

Commit 0df5d27

Browse files
Roman Marchenkognu-andrew
authored andcommitted
8301753: AppendFile/WriteFile has differences between make 3.81 and 4+
Reviewed-by: andrew Backport-of: a39cf2e3b242298fbf5fafdb8aa9b5d4562061ef
1 parent 6122785 commit 0df5d27

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

make/common/MakeIO.gmk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ ifeq ($(HAS_FILE_FUNCTION), true)
257257
else
258258
# Use printf to get consistent behavior on all platforms.
259259
WriteFile = \
260-
$(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
260+
$(shell $(PRINTF) "%s\n" $(strip $(call ShellQuote, $1)) > $2)
261261
endif
262262

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

test/make/TestMakeBase.gmk

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,62 @@ ifneq ($(READ_WRITE_VALUE), $(READ_WRITE_RESULT))
158158
$(error Expected: >$(READ_WRITE_VALUE)< - Result: >$(READ_WRITE_RESULT)<)
159159
endif
160160

161+
TEST_STRING_1 := 1234
162+
TEST_STRING_2 := 1234$(NEWLINE)
163+
TEST_STRING_3 := 1234$(NEWLINE)$(NEWLINE)
164+
165+
# Writing a string ending in newline should not add a newline, but if it does
166+
# not, a newline should be added. We check this by verifying that the size of the
167+
# file is 5 characters for both test strings.
168+
TEST_FILE_1 := $(OUTPUT_DIR)/write-file-1
169+
TEST_FILE_2 := $(OUTPUT_DIR)/write-file-2
170+
TEST_FILE_3 := $(OUTPUT_DIR)/write-file-3
171+
172+
$(call WriteFile, $(TEST_STRING_1), $(TEST_FILE_1))
173+
$(call WriteFile, $(TEST_STRING_2), $(TEST_FILE_2))
174+
$(call WriteFile, $(TEST_STRING_3), $(TEST_FILE_3))
175+
176+
TEST_FILE_1_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_1)))
177+
TEST_FILE_2_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_2)))
178+
TEST_FILE_3_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_3)))
179+
180+
ifneq ($(TEST_FILE_1_SIZE), 5)
181+
$(error Expected file size 5 for WriteFile 1, got $(TEST_FILE_1_SIZE))
182+
endif
183+
ifneq ($(TEST_FILE_2_SIZE), 5)
184+
$(error Expected file size 5 for WriteFile 2, got $(TEST_FILE_2_SIZE))
185+
endif
186+
ifneq ($(TEST_FILE_3_SIZE), 5)
187+
$(error Expected file size 5 for WriteFile 3, got $(TEST_FILE_3_SIZE))
188+
endif
189+
190+
# Also test append (assumes WriteFile works as expected)
191+
$(call WriteFile, $(TEST_STRING_1), $(TEST_FILE_1))
192+
$(call AppendFile, $(TEST_STRING_1), $(TEST_FILE_1))
193+
$(call AppendFile, $(TEST_STRING_1), $(TEST_FILE_1))
194+
195+
$(call WriteFile, $(TEST_STRING_2), $(TEST_FILE_2))
196+
$(call AppendFile, $(TEST_STRING_2), $(TEST_FILE_2))
197+
$(call AppendFile, $(TEST_STRING_2), $(TEST_FILE_2))
198+
199+
$(call WriteFile, $(TEST_STRING_3), $(TEST_FILE_3))
200+
$(call AppendFile, $(TEST_STRING_3), $(TEST_FILE_3))
201+
$(call AppendFile, $(TEST_STRING_3), $(TEST_FILE_3))
202+
203+
TEST_FILE_1_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_1)))
204+
TEST_FILE_2_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_2)))
205+
TEST_FILE_3_SIZE := $(strip $(shell $(WC) -c < $(TEST_FILE_3)))
206+
207+
ifneq ($(TEST_FILE_1_SIZE), 15)
208+
$(error Expected file size 15 for AppendFile 1, got $(TEST_FILE_1_SIZE))
209+
endif
210+
ifneq ($(TEST_FILE_2_SIZE), 15)
211+
$(error Expected file size 15 for AppendFile 2, got $(TEST_FILE_2_SIZE))
212+
endif
213+
ifneq ($(TEST_FILE_3_SIZE), 15)
214+
$(error Expected file size 15 for AppendFile 3, got $(TEST_FILE_3_SIZE))
215+
endif
216+
161217
################################################################################
162218
# Test creating dependencies on make variables
163219

0 commit comments

Comments
 (0)