diff --git a/boards/apollo3/lora_things_plus/Makefile b/boards/apollo3/lora_things_plus/Makefile index 3d023c939a..1bda4b1082 100644 --- a/boards/apollo3/lora_things_plus/Makefile +++ b/boards/apollo3/lora_things_plus/Makefile @@ -44,10 +44,8 @@ flash-app: .PHONY: test test: - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ $(Q)OBJCOPY=${OBJCOPY} PORT=$(PORT) $(CARGO) test $(NO_RUN) --bin $(PLATFORM) --release .PHONY: test-atecc508a test-atecc508a: - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ $(Q)OBJCOPY=${OBJCOPY} PORT=$(PORT) $(CARGO) test $(NO_RUN) --bin $(PLATFORM) --release --features atecc508a diff --git a/boards/apollo3/redboard_artemis_atp/Makefile b/boards/apollo3/redboard_artemis_atp/Makefile index 2a6c2ff0ff..a698dd8e79 100644 --- a/boards/apollo3/redboard_artemis_atp/Makefile +++ b/boards/apollo3/redboard_artemis_atp/Makefile @@ -44,5 +44,4 @@ flash-app: .PHONY: test test: - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ $(Q)OBJCOPY=${OBJCOPY} PORT=$(PORT) $(CARGO) test $(NO_RUN) --bin $(PLATFORM) --release diff --git a/boards/apollo3/redboard_artemis_nano/Makefile b/boards/apollo3/redboard_artemis_nano/Makefile index de8d303edc..a7e5912434 100644 --- a/boards/apollo3/redboard_artemis_nano/Makefile +++ b/boards/apollo3/redboard_artemis_nano/Makefile @@ -44,5 +44,4 @@ flash-app: .PHONY: test test: - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ $(Q)OBJCOPY=${OBJCOPY} PORT=$(PORT) $(CARGO) test $(NO_RUN) --bin $(PLATFORM) --release diff --git a/boards/build_scripts/src/default.rs b/boards/build_scripts/src/default.rs index 1332458f2b..402d532a5f 100644 --- a/boards/build_scripts/src/default.rs +++ b/boards/build_scripts/src/default.rs @@ -25,6 +25,43 @@ pub fn default_linker_script() { panic!("Boards must provide a `layout.ld` link script file"); } + rustflags_check(); + + include_tock_kernel_layout(); + + add_board_dir_to_linker_search_path(); + + set_and_track_linker_script(LINKER_SCRIPT); +} + +/// Include the folder where the board's Cargo.toml is in the linker file +/// search path. +pub fn add_board_dir_to_linker_search_path() { + // Note this is a different path than the one returned by + // `std::env!("CARGO_MANIFEST_DIR")` in `include_tock_kernel_layout()`, + // since that is evaluated at compile + // time while this `std::env::var("CARGO_MANIFEST_DIR")` is evaluated at runtime. + println!( + "cargo:rustc-link-arg=-L{}", + std::env::var("CARGO_MANIFEST_DIR").unwrap() + ); +} + +/// Include the folder where this build_script crate's Cargo.toml is in the +/// linker file search path for `tock_kernel_layout.ld`, and instruct cargo +/// to rebuild if that linker script is changed. +pub fn include_tock_kernel_layout() { + println!("cargo:rustc-link-arg=-L{}", std::env!("CARGO_MANIFEST_DIR")); + // Directive to rebuild if the linker script in this crate is changed. + println!( + "cargo:rerun-if-changed={}", + Path::new(std::env!("CARGO_MANIFEST_DIR")) + .join("tock_kernel_layout.ld") + .to_string_lossy() + ); +} + +pub fn rustflags_check() { // The `RUSTFLAGS` that the Tock config files set can be easily overridden // by command line flags. The build will still succeed but the resulting // binary may be invalid as it was not built with the intended flags. This @@ -50,33 +87,18 @@ pub fn default_linker_script() { ); } } +} - // Include the folder where this build_script crate's Cargo.toml is in the - // linker file search path for `tock_kernel_layout.ld`. - println!("cargo:rustc-link-arg=-L{}", std::env!("CARGO_MANIFEST_DIR")); - // Directive to rebuild if the linker script in this crate is changed. - println!( - "cargo:rerun-if-changed={}", - Path::new(std::env!("CARGO_MANIFEST_DIR")) - .join("tock_kernel_layout.ld") - .to_string_lossy() - ); - - // Include the folder where the board's Cargo.toml is in the linker file - // search path. - println!( - "cargo:rustc-link-arg=-L{}", - std::env::var("CARGO_MANIFEST_DIR").unwrap() - ); - // `-Tlayout.ld`: Use the linker script `layout.ld` all boards must provide. - println!("cargo:rustc-link-arg=-T{}", LINKER_SCRIPT); - - track_linker_script(LINKER_SCRIPT); +/// Pass the given linker script to cargo, and track it and all of its `INCLUDE`s +pub fn set_and_track_linker_script + ToString>(path: P) { + // Use the passed linker script + println!("cargo:rustc-link-arg=-T{}", path.to_string()); + track_linker_script(path); } /// Track the given linker script and all of its `INCLUDE`s so that the build /// is rerun when any of them change. -fn track_linker_script>(path: P) { +pub fn track_linker_script>(path: P) { let path = path.as_ref(); // Skip the default Tock linker script as we have manually added the diff --git a/boards/build_scripts/src/lib.rs b/boards/build_scripts/src/lib.rs index a7233780cc..b861129dd0 100644 --- a/boards/build_scripts/src/lib.rs +++ b/boards/build_scripts/src/lib.rs @@ -2,6 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT // Copyright Tock Contributors 2024. -mod default; +pub mod default; pub use default::default_linker_script; diff --git a/boards/esp32-c3-devkitM-1/Makefile b/boards/esp32-c3-devkitM-1/Makefile index 0d9e8e2a7e..f0a45f7829 100644 --- a/boards/esp32-c3-devkitM-1/Makefile +++ b/boards/esp32-c3-devkitM-1/Makefile @@ -25,5 +25,4 @@ endif esptool.py --port /dev/ttyUSB0 --chip esp32c3 write_flash --flash_mode dio --flash_size detect --flash_freq 80m 0x0 binary.hex test: - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ $(Q)$(CARGO) test $(NO_RUN) --bin $(PLATFORM) --release diff --git a/boards/opentitan/earlgrey-cw310/Cargo.toml b/boards/opentitan/earlgrey-cw310/Cargo.toml index 9c1d00711c..b9053343d0 100644 --- a/boards/opentitan/earlgrey-cw310/Cargo.toml +++ b/boards/opentitan/earlgrey-cw310/Cargo.toml @@ -6,7 +6,7 @@ name = "earlgrey-cw310" version.workspace = true authors.workspace = true -build = "../../build.rs" +build = "build.rs" edition.workspace = true [dependencies] diff --git a/boards/opentitan/earlgrey-cw310/Makefile b/boards/opentitan/earlgrey-cw310/Makefile index 2be8982928..e528c96a23 100644 --- a/boards/opentitan/earlgrey-cw310/Makefile +++ b/boards/opentitan/earlgrey-cw310/Makefile @@ -108,16 +108,10 @@ test: ifneq ($(OPENTITAN_TREE),) $(error "Running on QEMU, use test-hardware to run on hardware") endif - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ - $(Q)cp test_layout.ld $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/layout.ld - $(Q)TOCK_ROOT_DIRECTORY=${TOCK_ROOT_DIRECTORY} QEMU_ENTRY_POINT=${QEMU_ENTRY_POINT} TARGET=${TARGET} $(CARGO) test $(CARGO_FLAGS) $(NO_RUN) --bin $(PLATFORM) --release + $(Q)TOCK_ROOT_DIRECTORY=${TOCK_ROOT_DIRECTORY} QEMU_ENTRY_POINT=${QEMU_ENTRY_POINT} TARGET=${TARGET} LINKER_SCRIPT_OVERRIDE=test_layout.ld $(CARGO) test $(CARGO_FLAGS) $(NO_RUN) --bin $(PLATFORM) --release test-hardware: ot-check - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ - $(Q)cp test_layout.ld $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/layout.ld - $(Q)OBJCOPY=$(RISC_PREFIX)-objcopy TOCK_ROOT_DIRECTORY=${TOCK_ROOT_DIRECTORY} TARGET=${TARGET} $(CARGO) test $(CARGO_FLAGS) $(NO_RUN) --bin $(PLATFORM) --release --features=hardware_tests + $(Q)OBJCOPY=$(RISC_PREFIX)-objcopy TOCK_ROOT_DIRECTORY=${TOCK_ROOT_DIRECTORY} TARGET=${TARGET} LINKER_SCRIPT_OVERRIDE=test_layout.ld $(CARGO) test $(CARGO_FLAGS) $(NO_RUN) --bin $(PLATFORM) --release --features=hardware_tests test-verilator: ot-check - mkdir -p $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/ - $(Q)cp test_layout.ld $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/deps/layout.ld - $(Q)VERILATOR="yes" OBJCOPY=$(RISC_PREFIX)-objcopy TOCK_ROOT_DIRECTORY=${TOCK_ROOT_DIRECTORY} TARGET=${TARGET} $(CARGO) test $(CARGO_FLAGS) $(NO_RUN) --bin $(PLATFORM) --release --features=hardware_tests,sim_verilator + $(Q)VERILATOR="yes" OBJCOPY=$(RISC_PREFIX)-objcopy TOCK_ROOT_DIRECTORY=${TOCK_ROOT_DIRECTORY} TARGET=${TARGET} LINKER_SCRIPT_OVERRIDE=test_layout.ld $(CARGO) test $(CARGO_FLAGS) $(NO_RUN) --bin $(PLATFORM) --release --features=hardware_tests,sim_verilator diff --git a/boards/opentitan/earlgrey-cw310/build.rs b/boards/opentitan/earlgrey-cw310/build.rs new file mode 100644 index 0000000000..e0a7bc2ffb --- /dev/null +++ b/boards/opentitan/earlgrey-cw310/build.rs @@ -0,0 +1,29 @@ +// Licensed under the Apache License, Version 2.0 or the MIT License. +// SPDX-License-Identifier: Apache-2.0 OR MIT +// Copyright Tock Contributors 2024. + +//! This board uses a custom build script to enable selecting a different layout +//! file for tests, which require a different layout than normal kernel builds. +//! The script is lightly adapted from the `default_linker_script` in +//! `tock_build_scripts`, and uses the functions provided by that crate. + +use std::path::Path; + +const LINKER_SCRIPT_OVERRIDE_ENV: &str = "LINKER_SCRIPT_OVERRIDE"; + +fn main() { + let linker_script = + std::env::var(LINKER_SCRIPT_OVERRIDE_ENV).unwrap_or("layout.ld".to_string()); + println!("cargo:rerun-if-env-changed={}", LINKER_SCRIPT_OVERRIDE_ENV); + + if !Path::new(&linker_script).exists() { + panic!( + "Boards must provide a linker script file; path does not exist: {:?}", + linker_script + ); + } + tock_build_scripts::default::rustflags_check(); + tock_build_scripts::default::include_tock_kernel_layout(); + tock_build_scripts::default::add_board_dir_to_linker_search_path(); + tock_build_scripts::default::set_and_track_linker_script(linker_script); +} diff --git a/boards/opentitan/earlgrey-cw310/run.sh b/boards/opentitan/earlgrey-cw310/run.sh index 18e62fd976..4b5d51cc06 100755 --- a/boards/opentitan/earlgrey-cw310/run.sh +++ b/boards/opentitan/earlgrey-cw310/run.sh @@ -6,9 +6,6 @@ BUILD_DIR="verilator_build/" -# Preemptively cleanup layout (incase this was a test) so that following apps (non-tests) load the correct layout. -rm $TOCK_ROOT_DIRECTORY/target/$TARGET/release/deps/layout.ld - if [[ "${VERILATOR}" == "yes" ]]; then if [ -d "$BUILD_DIR" ]; then # Cleanup before we build again