xref: /wasmtime-44.0.1/crates/wasmtime/build.rs (revision fe6f7a40)
1 use std::str;
2 
main()3 fn main() {
4     println!("cargo:rerun-if-changed=build.rs");
5 
6     // NB: duplicating a workaround in the wasmtime-fiber build script.
7     custom_cfg("asan", cfg_is("sanitize", "address"));
8 
9     let unix = cfg("unix");
10     let windows = cfg("windows");
11     let miri = cfg("miri");
12 
13     // A boolean indicating whether there's a `sys` module for this platform.
14     // This is true for `unix` or `windows`, but both of those require the `std`
15     // feature to also be active so check that too.
16     let supported_os = (unix || windows) && cfg!(feature = "std");
17 
18     // Determine if the current host architecture is supported by Cranelift
19     // meaning that we might be executing native code.
20     let has_host_compiler_backend = match std::env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
21         "x86_64" | "riscv64" | "s390x" | "aarch64" => true,
22         _ => false,
23     };
24 
25     let has_native_signals = !miri
26         && (supported_os || cfg!(feature = "custom-native-signals"))
27         && has_host_compiler_backend;
28     let has_virtual_memory = supported_os || cfg!(feature = "custom-virtual-memory");
29     let has_custom_sync = !cfg!(feature = "std")
30         && cfg!(feature = "custom-sync-primitives")
31         && cfg!(feature = "runtime");
32 
33     custom_cfg("has_native_signals", has_native_signals);
34     custom_cfg("has_virtual_memory", has_virtual_memory);
35     custom_cfg("has_custom_sync", has_custom_sync);
36     custom_cfg("has_host_compiler_backend", has_host_compiler_backend);
37 
38     // If this OS isn't supported and no debug-builtins or if Cranelift doesn't support
39     // the host or there's no need to build these helpers.
40     #[cfg(feature = "runtime")]
41     if has_host_compiler_backend && (supported_os || cfg!(feature = "debug-builtins")) {
42         build_c_helpers();
43     }
44 
45     // Figure out what to do about Pulley.
46     //
47     // If the target platform does not have any Cranelift support then Pulley
48     // will be used by default. That means that the pulley feature is "enabled"
49     // here and the default target is pulley. Note that by enabling the feature
50     // here it doesn't actually enable the Cargo feature, it just passes a cfg
51     // to rustc. That means that conditional dependencies enabled in
52     // `Cargo.toml` (or other features) by `pulley` aren't activated, which is
53     // why the `pulley` feature of this crate depends on nothing else.
54     let default_target_pulley = !has_host_compiler_backend || miri;
55     custom_cfg("default_target_pulley", default_target_pulley);
56     if default_target_pulley {
57         println!("cargo:rustc-cfg=feature=\"pulley\"");
58     }
59 }
60 
cfg(key: &str) -> bool61 fn cfg(key: &str) -> bool {
62     std::env::var(&format!("CARGO_CFG_{}", key.to_uppercase())).is_ok()
63 }
64 
cfg_is(key: &str, val: &str) -> bool65 fn cfg_is(key: &str, val: &str) -> bool {
66     std::env::var(&format!("CARGO_CFG_{}", key.to_uppercase()))
67         .ok()
68         .as_deref()
69         == Some(val)
70 }
71 
custom_cfg(key: &str, enabled: bool)72 fn custom_cfg(key: &str, enabled: bool) {
73     println!("cargo:rustc-check-cfg=cfg({key})");
74     if enabled {
75         println!("cargo:rustc-cfg={key}");
76     }
77 }
78 
79 #[cfg(feature = "runtime")]
build_c_helpers()80 fn build_c_helpers() {
81     use wasmtime_versioned_export_macros::versioned_suffix;
82 
83     let mut build = cc::Build::new();
84     build.warnings(true);
85     let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
86     let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
87     build.define(&format!("CFG_TARGET_OS_{os}"), None);
88     build.define(&format!("CFG_TARGET_ARCH_{arch}"), None);
89     build.define("VERSIONED_SUFFIX", Some(versioned_suffix!()));
90     if std::env::var("CARGO_FEATURE_DEBUG_BUILTINS").is_ok() {
91         build.define("FEATURE_DEBUG_BUILTINS", None);
92     } else if cfg("windows") {
93         // If debug builtins are disabled and this target is for Windows then
94         // there's no need to build the C helpers file.
95         //
96         // TODO: should skip this on Unix targets as well but needs a solution
97         // for `wasmtime_using_libunwind`.
98         return;
99     }
100 
101     println!("cargo:rerun-if-changed=src/runtime/vm/helpers.c");
102     build.file("src/runtime/vm/helpers.c");
103     build.compile("wasmtime-helpers");
104 }
105