Skip to content

Commit fdf289e

Browse files
committed
split build.rs into runtime-specific and root
In runtime we need to get the linker scripts somewhere that the linker can use them. We _always_ need libtock_layout.ld, and we may want the board-specific ld scripts. In the new crate for build.rs, we specify which linker script we actually want.
1 parent a051206 commit fdf289e

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

libtock_runtime_build/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ pub fn auto_layout() {
2727
"Build path contains a newline, which is unsupported"
2828
);
2929

30-
// Point the linker to the generic libtock-rs linker file.
31-
let generic_ld_dir: PathBuf = ["runtime"].iter().collect();
32-
println!("cargo:rustc-link-search={}", generic_ld_dir.display());
33-
3430
// Choose the linker file we are going to use for this build. That can be
3531
// specified by choosing a platform, where the linker file will be selected
3632
// from `runtime/layouts`, or by explicitly setting the flash and RAM
@@ -47,16 +43,7 @@ pub fn auto_layout() {
4743
if let Ok(platform) = platform {
4844
// Point the linker to the correct platform-specific linker file.
4945
let platform_ld_name = format!("{}.ld", platform);
50-
let platform_ld_dir: PathBuf = ["runtime", "layouts"].iter().collect();
51-
let platform_ld_path: PathBuf = ["runtime", "layouts", &platform_ld_name].iter().collect();
52-
53-
assert!(platform_ld_path.exists(), "Unknown platform {}", platform);
54-
55-
println!("cargo:rerun-if-changed={}", platform_ld_path.display());
5646
println!("cargo:rustc-link-arg=-T{}", platform_ld_name);
57-
58-
// Tell rustc where to search for the layout file.
59-
println!("cargo:rustc-link-search={}", platform_ld_dir.display());
6047
} else if let (Ok(linker_flash), Ok(linker_ram)) = (linker_flash, linker_ram) {
6148
// Create a valid linker file with the specified flash and ram locations.
6249
//

runtime/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ libtock_platform = { path = "../platform" }
1414

1515
[features]
1616

17-
# By default, libtock_runtime looks for the LIBTOCK_PLATFORM variable to decide
18-
# what layout file to use. If you are providing your own linker script, set
19-
# no_auto_layout to disable the layout file logic.
20-
no_auto_layout = []
21-
2217
# By default, libtock_runtime calls Memop to tell the Tock kernel where the
2318
# stack and heap begin. The kernel uses those addresses to specify the stack and
2419
# heap address ranges if the process faults. Those calls cost 22 bytes on ARM

runtime/build.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! This build.rs makes common linker scripts available when linking libtock-rs
2+
//! apps.
3+
4+
fn main() {
5+
use std::fs::copy;
6+
use std::fs::read_dir;
7+
use std::path::PathBuf;
8+
9+
const LAYOUT_GENERIC_FILENAME: &str = "libtock_layout.ld";
10+
11+
// Note: cargo fails if run in a path that is not valid Unicode, so this
12+
// script doesn't need to handle non-Unicode paths. Also, OUT_DIR cannot be
13+
// in a location with a newline in it, or we have no way to pass
14+
// rustc-link-search to cargo.
15+
let out_dir = &std::env::var("OUT_DIR").expect("Unable to read OUT_DIR");
16+
assert!(
17+
!out_dir.contains('\n'),
18+
"Build path contains a newline, which is unsupported"
19+
);
20+
21+
// Copy all platform linker scripts to a directory where the linker can find
22+
// them.
23+
let paths = read_dir("layouts").unwrap();
24+
for path in paths {
25+
let ld_path = path.as_ref().unwrap().path();
26+
let ld_name = path.as_ref().unwrap().file_name().into_string().unwrap();
27+
let out_ld_path: PathBuf = [out_dir, &ld_name].iter().collect();
28+
copy(ld_path, out_ld_path).expect("Unable to copy platform layout into OUT_DIR");
29+
}
30+
31+
// Copy the generic layout file into OUT_DIR.
32+
let out_layout_generic: PathBuf = [out_dir, LAYOUT_GENERIC_FILENAME].iter().collect();
33+
println!("cargo:rerun-if-changed={}", LAYOUT_GENERIC_FILENAME);
34+
copy(LAYOUT_GENERIC_FILENAME, out_layout_generic)
35+
.expect("Unable to copy layout_generic.ld into OUT_DIR");
36+
37+
// Tell the linker that it can find linker scripts in the out directory of
38+
// the libtock_runtime crate.
39+
println!("cargo:rustc-link-search={}", out_dir);
40+
}

0 commit comments

Comments
 (0)