Skip to content

Commit

Permalink
Use LedgerHQ#38 in build.rs and structure
Browse files Browse the repository at this point in the history
The first change is to parse once, and then use an enum for the device.
This is hopefully a straightforward improvement.

The second change is to case on the OS name (leveraging LedgerHQ#38) instead of
the target name. The target "name" isn't so structured, and it is
unclear to what extent it should be anm exposed part of the target. (See
rust-lang/rust#98225 for example, where the
contents rather than json file path were used as a a key.)

With LedgerHQ#38 the device name is used for the OS field instead, and so we are
robust to confusing behavior around names.
  • Loading branch information
Ericson2314 committed Aug 25, 2022
1 parent 1b9a32e commit 1f6d8db
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,30 @@ fn main() -> Result<(), Box<dyn Error>> {
.flag("-Wno-unused-command-line-argument")
.clone();

// determine target
let target = env::var_os("TARGET");
let cx_makefile = match target.clone().unwrap().to_str().unwrap() {
"nanos" => finalize_nanos_configuration(&mut command, &bolos_sdk),
"nanox" => finalize_nanox_configuration(&mut command, &bolos_sdk),
"nanosplus" => finalize_nanosplus_configuration(&mut command, &bolos_sdk),
enum Device {
NanoS,
NanoSPlus,
NanoX,
}
use Device::*;

// determine device
let device = match env::var_os("CARGO_CFG_TARGET_OS").unwrap().to_str().unwrap() {
"nanos" => NanoS,
"nanosplus" => NanoSPlus,
"nanox" => NanoX,
target_name => panic!(
"invalid target `{}`, expected one of `nanos`, `nanox`, `nanosplus`. Run with `-Z build-std=core --target=./<target name>.json`",
target_name
),
};

let cx_makefile = match NanoS {
NanoS => finalize_nanos_configuration(&mut command, &bolos_sdk),
NanoX => finalize_nanox_configuration(&mut command, &bolos_sdk),
NanoSPlus => finalize_nanosplus_configuration(&mut command, &bolos_sdk),
};

// all 'finalize_...' functions also declare a new 'cfg' variable corresponding
// to the name of the target (as #[cfg(target = "nanox")] does not work, for example)
// this allows code to easily import things depending on the target
Expand Down Expand Up @@ -225,11 +237,10 @@ fn main() -> Result<(), Box<dyn Error>> {
// extend the library search path
println!("cargo:rustc-link-search={}", out_dir.display());
// copy
let linkerscript = match target.unwrap().to_str().unwrap() {
"nanos" => "nanos_layout.ld",
"nanox" => "nanox_layout.ld",
"nanosplus" => "nanosplus_layout.ld",
_ => "",
let linkerscript = match device {
NanoS => "nanos_layout.ld",
NanoX => "nanox_layout.ld",
NanoSPlus => "nanosplus_layout.ld",
};
std::fs::copy(linkerscript, out_dir.join(linkerscript))?;
std::fs::copy("link.ld", out_dir.join("link.ld"))?;
Expand Down

0 comments on commit 1f6d8db

Please sign in to comment.