From 6768763601e332b60eb2452a7d5dd6053192de75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= <gaetan.harter@fu-berlin.de>
Date: Wed, 13 Jun 2018 18:02:14 +0200
Subject: [PATCH] cortexm_common_ldscript: add test for linker FW_ROM_LEN

Compile an elf file with a length equals to half the rom length.
---
 tests/cortexm_common_ldscript/Makefile | 31 ++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/tests/cortexm_common_ldscript/Makefile b/tests/cortexm_common_ldscript/Makefile
index 49973d3f29..337462fdea 100644
--- a/tests/cortexm_common_ldscript/Makefile
+++ b/tests/cortexm_common_ldscript/Makefile
@@ -20,12 +20,12 @@ BOARD_WHITELIST += stm32mindev
 include $(RIOTBASE)/Makefile.include
 
 
-# # # # # # # # # # # # # # # # # # #
-# Compile time tests for ROM_OFFSET #
-# # # # # # # # # # # # # # # # # # #
+# # # # # # # # # # # # # # # # # # # # # # # # # # # #
+# Compile time tests for ROM_OFFSET and FW_ROM_LENGTH #
+# # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
 COMPILE_TESTS  = test-elffile-overflow test-elffile-fw_rom_length
-COMPILE_TESTS += tests-offsets
+COMPILE_TESTS += tests-offsets tests-fw_rom_len
 
 all: compile-tests
 
@@ -98,3 +98,26 @@ $(BINDIR)/$(APPLICATION)_offset_%.elf: ROM_OFFSET=$*
 $(BINDIR)/$(APPLICATION)_offset_%.elf: $(ELFFILES_DEPS)
 	$(Q)$(_LINK) -o $@
 .PRECIOUS: $(BINDIR)/$(APPLICATION)_offset_%.elf
+
+
+# Compile elf files with FW_ROM_LEN and verify the length is taken into account
+#   I arbitrarily do 'half' size because I needed to take a value and
+#   that it is similar to what is required for doing dual firmware in rom ota
+
+tests-fw_rom_len: test-fw_len_half_rom
+
+.PHONY: test-fw_len_half_rom
+test-fw_len_half_rom: $(BINDIR)/$(APPLICATION)_fw_len_half_rom.elf
+	$(Q)echo -n "Test compilation with half ROM length: "
+	$(Q)\
+	TEST_FW_LEN=$$($(PREFIX)readelf --symbols $^ 2>/dev/null | awk '/_fw_rom_length/{printf "0x%s\n", $$2}'); \
+	EXPECT_FW_LEN=$$(printf "0x%08x" $$(( $(ROM_LEN_BYTES) / 2 ))); \
+	if test $${TEST_FW_LEN} != $${EXPECT_FW_LEN}; then \
+	  echo "[ERROR] Linker firmware length not used $${TEST_FW_LEN} != $${EXPECT_FW_LEN}" >&2; \
+	  exit 1;\
+	fi
+	$(Q)echo [OK]
+$(BINDIR)/$(APPLICATION)_fw_len_half_rom.elf: FW_ROM_LEN=$$(($(ROM_LEN_BYTES)/2))
+$(BINDIR)/$(APPLICATION)_fw_len_half_rom.elf: $(ELFFILES_DEPS)
+	$(Q)$(_LINK) -o $@
+.PRECIOUS: $(BINDIR)/$(APPLICATION)_fw_len_half_rom.elf
-- 
GitLab