From 0acffd2923e5629b61e6a93d6c153a3d31cf6ae0 Mon Sep 17 00:00:00 2001 From: James Wainwright Date: Tue, 16 Jan 2024 15:05:44 +0000 Subject: [PATCH 1/2] Set linker script from build.rs file This has two advantages: 1. It adds a `cargo:rerun-if-changed` command to ensure we rebuild crates if the linker script changes. 2. The path to the linker script does not depend on the directory that Cargo is executed from, so `cargo build` can be done from anywhere. Signed-off-by: James Wainwright --- sw/rust/.cargo/config.toml | 3 --- sw/rust/Cargo.toml | 1 + sw/rust/build.rs | 42 +++++++++++++++++++++++++++++ sw/rust/demo/hello_world/Cargo.toml | 2 ++ sw/rust/demo/lcd_hal/Cargo.toml | 2 ++ sw/rust/demo/led/Cargo.toml | 2 ++ sw/rust/demo/led_hal/Cargo.toml | 2 ++ sw/rust/demo/pwm_hal/Cargo.toml | 2 ++ 8 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 sw/rust/build.rs diff --git a/sw/rust/.cargo/config.toml b/sw/rust/.cargo/config.toml index dd461477..60b2a756 100644 --- a/sw/rust/.cargo/config.toml +++ b/sw/rust/.cargo/config.toml @@ -7,9 +7,6 @@ target = "riscv32imc-unknown-none-elf" [target.riscv32imc-unknown-none-elf] runner = "../../util/load_demo_system.sh run" -rustflags = [ - "-C", "link-arg=-T../common/link.ld", -] [unstable] build-std = ["core"] diff --git a/sw/rust/Cargo.toml b/sw/rust/Cargo.toml index 6654239e..ff0fa517 100644 --- a/sw/rust/Cargo.toml +++ b/sw/rust/Cargo.toml @@ -12,6 +12,7 @@ members = [ "demo/lcd_hal", "demo/pwm_hal", ] +resolver = "2" [profile.release] debug = true diff --git a/sw/rust/build.rs b/sw/rust/build.rs new file mode 100644 index 00000000..8d8f5708 --- /dev/null +++ b/sw/rust/build.rs @@ -0,0 +1,42 @@ +// Copyright lowRISC contributors. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 + +//! This Cargo build script locates and uses the `link.ld` linker script. +//! +//! This build script can be used by a crate from its Cargo.toml: +//! +//! ```toml +//! [package] +//! ... +//! build = "../path/to/build.rs" +//! ``` +//! +//! Build scripts are run with their working directory as the root of the +//! crate being built. We must find the path to the Cargo workspace in order +//! to get the correct path to the `link.ld` script. + +use std::path::PathBuf; +use std::process::Command; + +fn main() { + // Ask Cargo for the path of the workspace. + let workspace_path = Command::new(env!("CARGO")) + .arg("locate-project") + .arg("--workspace") + .arg("--message-format=plain") + .output() + .expect("failed to locate project using cargo") + .stdout; + let workspace_path = std::str::from_utf8(&workspace_path).expect("path to workspace not UTF-8"); + let workspace_path = PathBuf::from(workspace_path); + let workspace_path = workspace_path.parent().expect("failed to get parent"); + + // Find the path of the linker script relative to the workspace. + let linker_script_path = workspace_path.join("../common/link.ld"); + let linker_script_path = linker_script_path.display(); + + // Use the linker script and rebuild crates if it changes. + println!("cargo:rerun-if-changed={linker_script_path}"); + println!("cargo:rustc-link-arg=-T{linker_script_path}"); +} diff --git a/sw/rust/demo/hello_world/Cargo.toml b/sw/rust/demo/hello_world/Cargo.toml index a133ade9..90814ae7 100644 --- a/sw/rust/demo/hello_world/Cargo.toml +++ b/sw/rust/demo/hello_world/Cargo.toml @@ -6,6 +6,8 @@ name = "hello_world" version = "0.1.0" edition = "2021" +# This build script configures the linker script to use. +build = "../../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/sw/rust/demo/lcd_hal/Cargo.toml b/sw/rust/demo/lcd_hal/Cargo.toml index 69bda4a8..2a3e0343 100644 --- a/sw/rust/demo/lcd_hal/Cargo.toml +++ b/sw/rust/demo/lcd_hal/Cargo.toml @@ -6,6 +6,8 @@ name = "lcd_hal" version = "0.1.0" edition = "2021" +# This build script configures the linker script to use. +build = "../../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/sw/rust/demo/led/Cargo.toml b/sw/rust/demo/led/Cargo.toml index 78e5d47d..aeaf13b1 100644 --- a/sw/rust/demo/led/Cargo.toml +++ b/sw/rust/demo/led/Cargo.toml @@ -6,6 +6,8 @@ name = "led" version = "0.1.0" edition = "2021" +# This build script configures the linker script to use. +build = "../../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/sw/rust/demo/led_hal/Cargo.toml b/sw/rust/demo/led_hal/Cargo.toml index 0b3e6e08..14c0b039 100644 --- a/sw/rust/demo/led_hal/Cargo.toml +++ b/sw/rust/demo/led_hal/Cargo.toml @@ -6,6 +6,8 @@ name = "led_hal" version = "0.1.0" edition = "2021" +# This build script configures the linker script to use. +build = "../../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/sw/rust/demo/pwm_hal/Cargo.toml b/sw/rust/demo/pwm_hal/Cargo.toml index 51174356..5b6927cb 100644 --- a/sw/rust/demo/pwm_hal/Cargo.toml +++ b/sw/rust/demo/pwm_hal/Cargo.toml @@ -6,6 +6,8 @@ name = "pwm_hal" version = "0.1.0" edition = "2021" +# This build script configures the linker script to use. +build = "../../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From 2d7bfe6a54c907523df307e6ddd7bc9c141e7107 Mon Sep 17 00:00:00 2001 From: James Wainwright Date: Thu, 18 Jan 2024 11:39:42 +0000 Subject: [PATCH 2/2] Enable optimisations for debug builds --- sw/rust/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sw/rust/Cargo.toml b/sw/rust/Cargo.toml index ff0fa517..a68f9d41 100644 --- a/sw/rust/Cargo.toml +++ b/sw/rust/Cargo.toml @@ -16,3 +16,9 @@ resolver = "2" [profile.release] debug = true + +[profile.dev] +opt-level = 1 + +[profile.dev.package.'*'] +opt-level = 2