Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sw: Add target-aware linking for BMPs #168

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/um/sw.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ To build a baremetal program (here `sw/tests/helloworld.c`) executing from the S
make sw/tests/helloworld.spm.elf
```

To create the same program executing from DRAM, `sw/tests/helloworld.spm.dram` can instead be built from the same source. Depending on their assumptions and behavior, not all programs may be built to execute from both locations.
To create the same program executing from DRAM, `sw/tests/helloworld.dram.elf` can instead be built from the same source.

Not all BMPs may be designed to be linked in multiple ways like `helloworld` above is. Linking is controlled by the linker scripts in `sw/link`. By convention, BMP main source files with a suffix are intended to only be linked with the corresponding script; for example, `sw/tests/dma_2d.spm.c` should only be linked against SPM using `sw/link/spm.ld`.

When building BMPs, the toolchain will strip any suffixes corresponding to existing linker scripts. When running `make sw-all`, tests in `sw/tests` will be built only for their intended linking targets. Tests without suffixes, like `helloworld`, are assumed to be agnostic and will be built for all linking targets.

## Boot Flow

Expand Down
36 changes: 28 additions & 8 deletions sw/sw.mk
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,21 @@ CHS_SW_GEN_HDRS += $(OTPROOT)/.generated
%.o: %.S $(CHS_SW_GEN_HDRS)
$(CHS_SW_CC) $(CHS_SW_INCLUDES) $(CHS_SW_CCFLAGS) -c $< -o $@

# Programs may specify a linking mode in their name, e.g. `helloworld.spm.c`.
# Tests with such infixes are built only for one linking mode, tests without them for all
define chs_sw_ld_elf_rule
.PRECIOUS: %.$(1).elf

%.$(1).elf: $$(CHS_SW_LD_DIR)/$(1).ld %.o $$(CHS_SW_LIBS)
$$(CHS_SW_CC) $$(CHS_SW_INCLUDES) -T$$< $$(CHS_SW_LDFLAGS) -o $$@ $$*.o $$(CHS_SW_LIBS)

%.$(1).elf: $$(CHS_SW_LD_DIR)/$(1).ld %.$(1).o $$(CHS_SW_LIBS)
$$(CHS_SW_CC) $$(CHS_SW_INCLUDES) -T$$< $$(CHS_SW_LDFLAGS) -o $$@ $$*.$(1).o $$(CHS_SW_LIBS)
endef

$(foreach link,$(patsubst $(CHS_SW_LD_DIR)/%.ld,%,$(wildcard $(CHS_SW_LD_DIR)/*.ld)),$(eval $(call chs_sw_ld_elf_rule,$(link))))
CHS_SW_LINK_MODES ?= $(patsubst $(CHS_SW_LD_DIR)/%.ld,%,$(wildcard $(CHS_SW_LD_DIR)/*.ld))

$(foreach link,$(CHS_SW_LINK_MODES),$(eval $(call chs_sw_ld_elf_rule,$(link))))

%.dump: %.elf
$(CHS_SW_OBJDUMP) -d -S $< > $@
Expand Down Expand Up @@ -158,11 +165,24 @@ $(CHS_SW_DIR)/boot/linux.%.gpt.bin: $(CHS_SW_DIR)/boot/zsl.rom.bin $(CHS_SW_DIR)
# Tests #
#########

CHS_SW_TEST_SRCS_S = $(wildcard $(CHS_SW_DIR)/tests/*.S)
CHS_SW_TEST_SRCS_C = $(wildcard $(CHS_SW_DIR)/tests/*.c)
CHS_SW_TEST_DRAM_DUMP = $(CHS_SW_TEST_SRCS_S:.S=.dram.dump) $(CHS_SW_TEST_SRCS_C:.c=.dram.dump)
CHS_SW_TEST_SPM_DUMP = $(CHS_SW_TEST_SRCS_S:.S=.spm.dump) $(CHS_SW_TEST_SRCS_C:.c=.spm.dump)
CHS_SW_TEST_SPM_ROMH = $(CHS_SW_TEST_SRCS_S:.S=.rom.memh) $(CHS_SW_TEST_SRCS_C:.c=.rom.memh)
CHS_SW_TEST_SPM_GPTH = $(CHS_SW_TEST_SRCS_S:.S=.gpt.memh) $(CHS_SW_TEST_SRCS_C:.c=.gpt.memh)
# Accumulate single-link-mode sources and corresponding .dump targets
define chs_sw_tests_add_rule
BLA += $(wildcard $(2)/*.$(1).c)
CHS_SW_TEST_LONE += $(wildcard $(2)/*.$(1).c) $(wildcard $(2)/*.$(1).S)
CHS_SW_TEST_DUMP += $(patsubst %.c,%.dump,$(wildcard $(2)/*.$(1).c)) $(patsubst %.S,%.dump,$(wildcard $(2)/*.$(1).S))
endef

# Accumulate tests for all link modes
$(foreach link,$(CHS_SW_LINK_MODES),$(eval $(call chs_sw_tests_add_rule,$(link),$(CHS_SW_DIR)/tests)))

# Collect mode-agnostic tests, which should be build for all modes, and their .dump targets
CHS_SW_TEST_C_LALL = $(filter-out $(CHS_SW_TEST_LONE), $(wildcard $(CHS_SW_DIR)/tests/*.c))
CHS_SW_TEST_S_LALL = $(filter-out $(CHS_SW_TEST_LONE), $(wildcard $(CHS_SW_DIR)/tests/*.S))
$(foreach link,$(CHS_SW_LINK_MODES),$(eval CHS_SW_TEST_DUMP += $(CHS_SW_TEST_C_LALL:.c=.$(link).dump) $(CHS_SW_TEST_S_LALL:.S=.$(link).dump)))

# Generate .memh targets for ROM-linked tests
CHS_SW_TEST_ROM_DUMP = $(filter %.rom.dump,$(CHS_SW_TEST_DUMP))
CHS_SW_TESTS += $(CHS_SW_TEST_ROM_DUMP:.rom.dump=.rom.memh) $(CHS_SW_TEST_ROM_DUMP:.rom.dump=.gpt.memh)

CHS_SW_TESTS = $(CHS_SW_TEST_DRAM_DUMP) $(CHS_SW_TEST_SPM_DUMP) $(CHS_SW_TEST_SPM_ROMH) $(CHS_SW_TEST_SPM_GPTH)
# Add all dumps to test build
CHS_SW_TESTS += $(CHS_SW_TEST_DUMP)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading