Skip to content

Commit 8c98bbe

Browse files
Ericson2314yhql
authored andcommitted
Use #38 in build.rs and structure
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 #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 #38 the device name is used for the OS field instead, and so we are robust to confusing behavior around names.
1 parent a534ef4 commit 8c98bbe

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

build.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,30 @@ fn main() -> Result<(), Box<dyn Error>> {
182182
.flag("-Wno-unused-command-line-argument")
183183
.clone();
184184

185-
// determine target
186-
let target = env::var_os("TARGET");
187-
let cx_makefile = match target.clone().unwrap().to_str().unwrap() {
188-
"nanos" => finalize_nanos_configuration(&mut command, &bolos_sdk),
189-
"nanox" => finalize_nanox_configuration(&mut command, &bolos_sdk),
190-
"nanosplus" => finalize_nanosplus_configuration(&mut command, &bolos_sdk),
185+
enum Device {
186+
NanoS,
187+
NanoSPlus,
188+
NanoX,
189+
}
190+
use Device::*;
191+
192+
// determine device
193+
let device = match env::var_os("CARGO_CFG_TARGET_OS").unwrap().to_str().unwrap() {
194+
"nanos" => NanoS,
195+
"nanosplus" => NanoSPlus,
196+
"nanox" => NanoX,
191197
target_name => panic!(
192198
"invalid target `{}`, expected one of `nanos`, `nanox`, `nanosplus`. Run with `-Z build-std=core --target=./<target name>.json`",
193199
target_name
194200
),
195201
};
196202

203+
let cx_makefile = match NanoS {
204+
NanoS => finalize_nanos_configuration(&mut command, &bolos_sdk),
205+
NanoX => finalize_nanox_configuration(&mut command, &bolos_sdk),
206+
NanoSPlus => finalize_nanosplus_configuration(&mut command, &bolos_sdk),
207+
};
208+
197209
// all 'finalize_...' functions also declare a new 'cfg' variable corresponding
198210
// to the name of the target (as #[cfg(target = "nanox")] does not work, for example)
199211
// this allows code to easily import things depending on the target
@@ -225,11 +237,10 @@ fn main() -> Result<(), Box<dyn Error>> {
225237
// extend the library search path
226238
println!("cargo:rustc-link-search={}", out_dir.display());
227239
// copy
228-
let linkerscript = match target.unwrap().to_str().unwrap() {
229-
"nanos" => "nanos_layout.ld",
230-
"nanox" => "nanox_layout.ld",
231-
"nanosplus" => "nanosplus_layout.ld",
232-
_ => "",
240+
let linkerscript = match device {
241+
NanoS => "nanos_layout.ld",
242+
NanoX => "nanox_layout.ld",
243+
NanoSPlus => "nanosplus_layout.ld",
233244
};
234245
std::fs::copy(linkerscript, out_dir.join(linkerscript))?;
235246
std::fs::copy("link.ld", out_dir.join("link.ld"))?;

0 commit comments

Comments
 (0)