rust-num / num-traits

Numeric traits for generic mathematics in Rust
Apache License 2.0
694 stars 131 forks source link

[Build-script] unable to determine target details for custom target-spec #329

Open boozook opened 2 months ago

boozook commented 2 months ago

There is autocfg can't determine target details for custom target-spec (json file).

Error:

[num-traits 0.2.19] error: Error loading target specification: Could not find specification for target "my-custom-target". Run `rustc --print target-list` for a list of built-in targets
[num-traits 0.2.19] warning: autocfg could not probe for `std`
boozook commented 2 months ago

As an ugly workaround I could suggest something like this:

// save original to restore later:
let orig_target = std::env::var_os("TARGET")?;
// choose one of "override" or "by cargo":
let target = std::env::var_os("AUTOCONF_TARGET").or_else(|| env::var_os("TARGET"))?;
// override target for current use for autocfg:
std::env::set_var("TARGET", target);
let mut ac = autocfg::new();
ac.emit_expression_cfg("1f64.total_cmp(&2f64)", "has_total_cmp"); // 1.62
// do some needed...
autocfg::rerun_path("build.rs");
// restore original target for future use:
std::env::set_var("TARGET", orig_target);

And so we can override the target passed to autoconf with env AUTOCONF_TARGET.