Skip to content

Commit

Permalink
crate_universe: Allow platform-specific build script env vars (#2139)
Browse files Browse the repository at this point in the history
My use-case here is needing to point at different paths for libclang.so
when using bindgen on different platforms.

This allows specifying build script env vars in annotations like:

```starlark
build_script_env = {
  "BORING_BSSL_PATH": "$(execpath @//third_party/boringssl:gen_dir)",
  "LIBCLANG_PATH": {
      "x86_64-unknown-linux-gnu": "$(execpath @libclang_linux_x86_64//file:libclang.so)",
  },
},
```

which will unconditionally set `$BORING_BSSL_PATH`, but will only set
`$LIBCLANG_PATH` on x86-64 Linux.

We could in theory support this kind of construct for all annotation
fields, but I've only added it where I happened to need it for now.
  • Loading branch information
illicitonion authored Aug 30, 2023
1 parent b55575e commit feb75f1
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 33 deletions.
12 changes: 10 additions & 2 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ impl From<GitReference> for Commitish {
}
}
}
/// A value which may either be a plain String, or a dict of platform triples
/// (or other cfg expressions understood by `crate::context::platforms::resolve_cfg_platforms`) to Strings.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum StringOrSelect {
Value(String),
Select(BTreeMap<String, String>),
}

/// Information representing deterministic identifiers for some remote asset.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
Expand Down Expand Up @@ -234,7 +242,7 @@ pub struct CrateAnnotations {

/// Additional environment variables to pass to a build script's
/// [build_script_env](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-rustc_env) attribute.
pub build_script_env: Option<BTreeMap<String, String>>,
pub build_script_env: Option<BTreeMap<String, StringOrSelect>>,

/// Additional rustc_env flags to pass to a build script's
/// [rustc_env](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-rustc_env) attribute.
Expand Down Expand Up @@ -361,7 +369,7 @@ pub struct AnnotationsProvidedByPackage {
pub rustc_env: Option<BTreeMap<String, String>>,
pub rustc_env_files: Option<BTreeSet<String>>,
pub rustc_flags: Option<Vec<String>>,
pub build_script_env: Option<BTreeMap<String, String>>,
pub build_script_env: Option<BTreeMap<String, StringOrSelect>>,
pub build_script_rustc_env: Option<BTreeMap<String, String>>,
pub additive_build_file_content: Option<String>,
pub extra_aliased_targets: Option<BTreeMap<String, String>>,
Expand Down
21 changes: 19 additions & 2 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::{BTreeMap, BTreeSet};
use cargo_metadata::{Node, Package, PackageId};
use serde::{Deserialize, Serialize};

use crate::config::{CrateId, GenBinaries};
use crate::config::{CrateId, GenBinaries, StringOrSelect};
use crate::metadata::{CrateAnnotation, Dependency, PairredExtras, SourceAnnotation};
use crate::utils::sanitize_module_name;
use crate::utils::starlark::{Glob, SelectList, SelectMap, SelectStringDict, SelectStringList};
Expand Down Expand Up @@ -601,7 +601,24 @@ impl CrateContext {

// Build script env
if let Some(extra) = &crate_extra.build_script_env {
attrs.build_script_env.extend(extra.clone(), None);
for (key, value) in extra {
match value {
StringOrSelect::Value(value) => {
attrs
.build_script_env
.insert(key.clone(), value.clone(), None);
}
StringOrSelect::Select(select) => {
for (select_key, value) in select {
attrs.build_script_env.insert(
key.clone(),
value.clone(),
Some(select_key.clone()),
);
}
}
}
}
}
}

Expand Down
103 changes: 74 additions & 29 deletions crate_universe/src/context/platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::utils::starlark::Select;

/// Walk through all dependencies in a [CrateContext] list for all configuration specific
/// dependencies to produce a mapping of configuration to compatible platform triples.
/// Also adds mappings for all known platform triples.
pub fn resolve_cfg_platforms(
crates: Vec<&CrateContext>,
supported_platform_triples: &BTreeSet<String>,
Expand Down Expand Up @@ -99,6 +100,10 @@ pub fn resolve_cfg_platforms(

Ok((cfg, triples))
})
.chain(supported_platform_triples.iter().filter_map(|triple| {
let target = get_builtin_target_by_triple(triple);
target.map(|target| Ok((triple.clone(), [target.triple.to_string()].into())))
}))
.collect()
}

Expand All @@ -114,26 +119,7 @@ mod test {
fn supported_platform_triples() -> BTreeSet<String> {
BTreeSet::from([
"aarch64-apple-darwin".to_owned(),
"aarch64-apple-ios".to_owned(),
"aarch64-linux-android".to_owned(),
"aarch64-pc-windows-msvc".to_owned(),
"aarch64-unknown-linux-gnu".to_owned(),
"arm-unknown-linux-gnueabi".to_owned(),
"armv7-unknown-linux-gnueabi".to_owned(),
"i686-apple-darwin".to_owned(),
"i686-linux-android".to_owned(),
"i686-pc-windows-msvc".to_owned(),
"i686-unknown-freebsd".to_owned(),
"i686-unknown-linux-gnu".to_owned(),
"powerpc-unknown-linux-gnu".to_owned(),
"s390x-unknown-linux-gnu".to_owned(),
"wasm32-unknown-unknown".to_owned(),
"wasm32-wasi".to_owned(),
"x86_64-apple-darwin".to_owned(),
"x86_64-apple-ios".to_owned(),
"x86_64-linux-android".to_owned(),
"x86_64-pc-windows-msvc".to_owned(),
"x86_64-unknown-freebsd".to_owned(),
"x86_64-unknown-linux-gnu".to_owned(),
])
}
Expand Down Expand Up @@ -163,7 +149,24 @@ mod test {
let configurations =
resolve_cfg_platforms(vec![&context], &supported_platform_triples()).unwrap();

assert_eq!(configurations, BTreeMap::new(),)
assert_eq!(
configurations,
BTreeMap::from([
// All known triples.
(
"aarch64-apple-darwin".to_owned(),
BTreeSet::from(["aarch64-apple-darwin".to_owned()]),
),
(
"i686-apple-darwin".to_owned(),
BTreeSet::from(["i686-apple-darwin".to_owned()]),
),
(
"x86_64-unknown-linux-gnu".to_owned(),
BTreeSet::from(["x86_64-unknown-linux-gnu".to_owned()]),
),
])
)
}

fn mock_resolve_context(configuration: String) -> CrateContext {
Expand Down Expand Up @@ -199,10 +202,7 @@ mod test {
r#"cfg(any(target_os = "macos", target_os = "ios"))"#.to_owned(),
BTreeSet::from([
"aarch64-apple-darwin".to_owned(),
"aarch64-apple-ios".to_owned(),
"i686-apple-darwin".to_owned(),
"x86_64-apple-darwin".to_owned(),
"x86_64-apple-ios".to_owned(),
]),
),
]);
Expand All @@ -215,7 +215,22 @@ mod test {

assert_eq!(
configurations,
BTreeMap::from([(configuration, expectation,)])
BTreeMap::from([
(configuration, expectation,),
// All known triples.
(
"aarch64-apple-darwin".to_owned(),
BTreeSet::from(["aarch64-apple-darwin".to_owned()]),
),
(
"i686-apple-darwin".to_owned(),
BTreeSet::from(["i686-apple-darwin".to_owned()]),
),
(
"x86_64-unknown-linux-gnu".to_owned(),
BTreeSet::from(["x86_64-unknown-linux-gnu".to_owned()]),
),
])
);
})
}
Expand Down Expand Up @@ -248,10 +263,25 @@ mod test {

assert_eq!(
configurations,
BTreeMap::from([(
configuration,
BTreeSet::from(["x86_64-unknown-linux-gnu".to_owned()])
)])
BTreeMap::from([
(
configuration,
BTreeSet::from(["x86_64-unknown-linux-gnu".to_owned()])
),
// All known triples.
(
"aarch64-apple-darwin".to_owned(),
BTreeSet::from(["aarch64-apple-darwin".to_owned()]),
),
(
"i686-apple-darwin".to_owned(),
BTreeSet::from(["i686-apple-darwin".to_owned()]),
),
(
"x86_64-unknown-linux-gnu".to_owned(),
BTreeSet::from(["x86_64-unknown-linux-gnu".to_owned()]),
),
])
);
}

Expand Down Expand Up @@ -283,7 +313,22 @@ mod test {

assert_eq!(
configurations,
BTreeMap::from([(configuration, BTreeSet::new())])
BTreeMap::from([
(configuration, BTreeSet::new()),
// All known triples.
(
"aarch64-apple-darwin".to_owned(),
BTreeSet::from(["aarch64-apple-darwin".to_owned()]),
),
(
"i686-apple-darwin".to_owned(),
BTreeSet::from(["i686-apple-darwin".to_owned()]),
),
(
"x86_64-unknown-linux-gnu".to_owned(),
BTreeSet::from(["x86_64-unknown-linux-gnu".to_owned()]),
),
])
);
}
}
Loading

0 comments on commit feb75f1

Please sign in to comment.