Skip to content

Commit

Permalink
Add build guards for invalid build configs (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Jan 15, 2020
1 parent 3fde267 commit 8bb9d88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ serde_yaml = { version = "0.8.3", optional = true }
slab = "0.4"
stdweb = { version = "0.4.20", optional = true }
toml = { version = "0.5", optional = true }
wasm-bindgen = { version = "0.2.58", optional = true }
yew-macro = { version = "0.11.1", path = "crates/macro" }

[dependencies.web-sys]
Expand Down Expand Up @@ -91,9 +92,7 @@ features = [
"WorkerOptions",
]

[target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dependencies]
wasm-bindgen = "0.2.58"

# Changes here must be reflected in `build.rs`
[target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dev-dependencies]
wasm-bindgen-test = "0.3.4"

Expand All @@ -108,7 +107,7 @@ rustversion = "1.0"
[features]
default = ["services", "agent"]
std_web = ["stdweb"]
web_sys = ["console_error_panic_hook", "gloo", "js-sys", "web-sys"]
web_sys = ["console_error_panic_hook", "gloo", "js-sys", "web-sys", "wasm-bindgen"]
doc_test = []
web_test = []
wasm_test = []
Expand Down
20 changes: 16 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ use std::env;

pub fn main() {
if cfg!(all(feature = "web_sys", feature = "std_web")) {
panic!("the `web_sys` and `std_web` cargo features cannot be used simultaneously")
panic!("Yew does not allow the `web_sys` and `std_web` cargo features to be used simultaneously");
} else if cfg!(not(any(feature = "web_sys", feature = "std_web"))) {
panic!("please select either the `web_sys` or `std_web` cargo feature")
panic!("Yew requires selecting either the `web_sys` or `std_web` cargo feature");
}

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
let using_wasi = target_os == "wasi";

let cargo_web = env::var("COMPILING_UNDER_CARGO_WEB").unwrap_or_default();
if target_arch == "wasm32" && cargo_web != "1" {
let using_cargo_web = cargo_web == "1";
if using_cargo_web && cfg!(feature = "web_sys") {
panic!("cargo-web is not compatible with web-sys");
}

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let using_wasm_bindgen = target_arch == "wasm32" && !using_cargo_web && !using_wasi;
if using_wasm_bindgen {
println!("cargo:rustc-cfg=feature=\"wasm_bindgen_test\"");
} else if cfg!(all(feature = "web_sys", not(feature = "doc_test"))) {
let target = env::var("TARGET").unwrap_or_default();
panic!("Selected target `{}` is not compatible with web-sys", target);
}
}

0 comments on commit 8bb9d88

Please sign in to comment.