8000 ot/earlgrey-cw310: custom build.rs that supports alternative linker script for tests by hudson-ayers · Pull Request #4190 · tock/tock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ot/earlgrey-cw310: custom build.rs that supports alternative linker script for tests #4190

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

Merged
merged 1 commit into from
Oct 9, 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
2 changes: 0 additions & 2 deletions boards/apollo3/lora_things_plus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion boards/apollo3/redboard_artemis_atp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion boards/apollo3/redboard_artemis_nano/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 44 additions & 22 deletions boards/build_scripts/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<P: AsRef<Path> + 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<P: AsRef<Path>>(path: P) {
pub fn track_linker_script<P: AsRef<Path>>(path: P) {
let path = path.as_ref();

// Skip the default Tock linker script as we have manually added the
Expand Down
2 changes: 1 addition & 1 deletion boards/build_scripts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

mod default;
pub mod default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to expose this? We could namespace new functionality in the future, but what we have seems like the normal APIs we can just expose directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you -- I am fine to re-export all of the functions inside default, but it seems easier to just have default be pub and then we can still hide functions inside the file by making individual functions not pub


pub use default::default_linker_script;
1 change: 0 additions & 1 deletion boards/esp32-c3-devkitM-1/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion boards/opentitan/earlgrey-cw310/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name = "earlgrey-cw310"
version.workspace = true
authors.workspace = true
build = "../../build.rs"
build = "build.rs"
edition.workspace = true

[dependencies]
Expand Down
12 changes: 3 additions & 9 deletions boards/opentitan/earlgrey-cw310/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions boards/opentitan/earlgrey-cw310/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 0 additions & 3 deletions boards/opentitan/earlgrey-cw310/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
0