-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <james.wainwright@lowrisc.org>
- Loading branch information
Showing
8 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ members = [ | |
"demo/lcd_hal", | ||
"demo/pwm_hal", | ||
] | ||
resolver = "2" | ||
|
||
[profile.release] | ||
debug = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters