diff options
| author | Jan Špaček <[email protected]> | 2024-05-30 19:59:06 +0200 |
|---|---|---|
| committer | Jan Špaček <[email protected]> | 2024-05-31 08:07:51 +0200 |
| commit | bfb380e8ca8c220036739e6046df79ac784d935b (patch) | |
| tree | b5954fe04970b5080f6ab079cd4b56ab93f788a0 /embassy-hal-internal | |
| parent | 368893c9cb1b192c9e0d45440cacb271d1039c29 (diff) | |
Copy build_common.rs into each crate, to make cargo publish happy
Diffstat (limited to 'embassy-hal-internal')
| -rw-r--r-- | embassy-hal-internal/build.rs | 2 | ||||
| -rw-r--r-- | embassy-hal-internal/build_common.rs | 109 |
2 files changed, 110 insertions, 1 deletions
diff --git a/embassy-hal-internal/build.rs b/embassy-hal-internal/build.rs index 2649f5d37..ecd2c0c9f 100644 --- a/embassy-hal-internal/build.rs +++ b/embassy-hal-internal/build.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | #[path = "../build_common.rs"] | 1 | #[path = "./build_common.rs"] |
| 2 | mod common; | 2 | mod common; |
| 3 | 3 | ||
| 4 | fn main() { | 4 | fn main() { |
diff --git a/embassy-hal-internal/build_common.rs b/embassy-hal-internal/build_common.rs new file mode 100644 index 000000000..2c65f8529 --- /dev/null +++ b/embassy-hal-internal/build_common.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | // NOTE: this file is copy-pasted between several Embassy crates, because there is no | ||
| 2 | // straightforward way to share this code: | ||
| 3 | // - it cannot be placed into the root of the repo and linked from each build.rs using `#[path = | ||
| 4 | // "../build_common.rs"]`, because `cargo publish` requires that all files published with a crate | ||
| 5 | // reside in the crate's directory, | ||
| 6 | // - it cannot be symlinked from `embassy-xxx/build_common.rs` to `../build_common.rs`, because | ||
| 7 | // symlinks don't work on Windows. | ||
| 8 | |||
| 9 | use std::collections::HashSet; | ||
| 10 | use std::env; | ||
| 11 | use std::ffi::OsString; | ||
| 12 | use std::process::Command; | ||
| 13 | |||
| 14 | /// Helper for emitting cargo instruction for enabling configs (`cargo:rustc-cfg=X`) and declaring | ||
| 15 | /// them (`cargo:rust-check-cfg=cfg(X)`). | ||
| 16 | #[derive(Debug)] | ||
| 17 | pub struct CfgSet { | ||
| 18 | enabled: HashSet<String>, | ||
| 19 | declared: HashSet<String>, | ||
| 20 | emit_declared: bool, | ||
| 21 | } | ||
| 22 | |||
| 23 | impl CfgSet { | ||
| 24 | pub fn new() -> Self { | ||
| 25 | Self { | ||
| 26 | enabled: HashSet::new(), | ||
| 27 | declared: HashSet::new(), | ||
| 28 | emit_declared: is_rustc_nightly(), | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | /// Enable a config, which can then be used in `#[cfg(...)]` for conditional compilation. | ||
| 33 | /// | ||
| 34 | /// All configs that can potentially be enabled should be unconditionally declared using | ||
| 35 | /// [`Self::declare()`]. | ||
| 36 | pub fn enable(&mut self, cfg: impl AsRef<str>) { | ||
| 37 | if self.enabled.insert(cfg.as_ref().to_owned()) { | ||
| 38 | println!("cargo:rustc-cfg={}", cfg.as_ref()); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | pub fn enable_all(&mut self, cfgs: &[impl AsRef<str>]) { | ||
| 43 | for cfg in cfgs.iter() { | ||
| 44 | self.enable(cfg.as_ref()); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | /// Declare a valid config for conditional compilation, without enabling it. | ||
| 49 | /// | ||
| 50 | /// This enables rustc to check that the configs in `#[cfg(...)]` attributes are valid. | ||
| 51 | pub fn declare(&mut self, cfg: impl AsRef<str>) { | ||
| 52 | if self.declared.insert(cfg.as_ref().to_owned()) && self.emit_declared { | ||
| 53 | println!("cargo:rustc-check-cfg=cfg({})", cfg.as_ref()); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | pub fn declare_all(&mut self, cfgs: &[impl AsRef<str>]) { | ||
| 58 | for cfg in cfgs.iter() { | ||
| 59 | self.declare(cfg.as_ref()); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | pub fn set(&mut self, cfg: impl Into<String>, enable: bool) { | ||
| 64 | let cfg = cfg.into(); | ||
| 65 | if enable { | ||
| 66 | self.enable(cfg.clone()); | ||
| 67 | } | ||
| 68 | self.declare(cfg); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | fn is_rustc_nightly() -> bool { | ||
| 73 | let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); | ||
| 74 | |||
| 75 | let output = Command::new(rustc) | ||
| 76 | .arg("--version") | ||
| 77 | .output() | ||
| 78 | .expect("failed to run `rustc --version`"); | ||
| 79 | |||
| 80 | String::from_utf8_lossy(&output.stdout).contains("nightly") | ||
| 81 | } | ||
| 82 | |||
| 83 | /// Sets configs that describe the target platform. | ||
| 84 | pub fn set_target_cfgs(cfgs: &mut CfgSet) { | ||
| 85 | let target = env::var("TARGET").unwrap(); | ||
| 86 | |||
| 87 | if target.starts_with("thumbv6m-") { | ||
| 88 | cfgs.enable_all(&["cortex_m", "armv6m"]); | ||
| 89 | } else if target.starts_with("thumbv7m-") { | ||
| 90 | cfgs.enable_all(&["cortex_m", "armv7m"]); | ||
| 91 | } else if target.starts_with("thumbv7em-") { | ||
| 92 | cfgs.enable_all(&["cortex_m", "armv7m", "armv7em"]); | ||
| 93 | } else if target.starts_with("thumbv8m.base") { | ||
| 94 | cfgs.enable_all(&["cortex_m", "armv8m", "armv8m_base"]); | ||
| 95 | } else if target.starts_with("thumbv8m.main") { | ||
| 96 | cfgs.enable_all(&["cortex_m", "armv8m", "armv8m_main"]); | ||
| 97 | } | ||
| 98 | cfgs.declare_all(&[ | ||
| 99 | "cortex_m", | ||
| 100 | "armv6m", | ||
| 101 | "armv7m", | ||
| 102 | "armv7em", | ||
| 103 | "armv8m", | ||
| 104 | "armv8m_base", | ||
| 105 | "armv8m_main", | ||
| 106 | ]); | ||
| 107 | |||
| 108 | cfgs.set("has_fpu", target.ends_with("-eabihf")); | ||
| 109 | } | ||
