Skip to content

Commit 1b01ba5

Browse files
committed
Support env_flags attribute for cargo.rust_library/cargo.rust_binary
* When `buildscript.run = true`, set `env_flags` attribute like `env_flags = ["@$(location :my_crate-0.0.0-build-script-run[env_flags])"],` Signed-off-by: Yuxuan Dai <yxdai@smail.nju.edu.cn>
1 parent 09c8683 commit 1b01ba5

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/buck.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ pub struct PlatformRustCommon {
557557
pub deps: Selectable<UniverseName, BTreeSet<RuleRef>>,
558558
pub named_deps: Selectable<UniverseName, BTreeMap<String, RuleRef>>,
559559
pub env: Selectable<UniverseName, BTreeMap<String, StringOrPath>>,
560+
pub env_flags: BTreeSet<String>,
560561

561562
// This isn't really "common" (Binaries only), but does need to be platform
562563
pub link_style: Option<String>,
@@ -575,6 +576,7 @@ impl Serialize for PlatformRustCommon {
575576
deps,
576577
named_deps,
577578
env,
579+
env_flags,
578580
link_style,
579581
linker_flags,
580582
preferred_linkage,
@@ -586,6 +588,9 @@ impl Serialize for PlatformRustCommon {
586588
if !env.is_empty() {
587589
map.serialize_entry("env", env)?;
588590
}
591+
if !env_flags.is_empty() {
592+
map.serialize_entry("env_flags", env_flags)?;
593+
}
589594
if !features.is_empty() {
590595
map.serialize_entry("features", features)?;
591596
}
@@ -711,6 +716,7 @@ impl Serialize for RustLibrary {
711716
deps,
712717
named_deps,
713718
env,
719+
env_flags,
714720
link_style,
715721
linker_flags,
716722
preferred_linkage,
@@ -739,6 +745,9 @@ impl Serialize for RustLibrary {
739745
if !env.is_empty() {
740746
map.serialize_entry("env", env)?;
741747
}
748+
if !env_flags.is_empty() {
749+
map.serialize_entry("env_flags", env_flags)?;
750+
}
742751
if !features.is_empty() {
743752
map.serialize_entry("features", features)?;
744753
}
@@ -812,6 +821,7 @@ impl Serialize for RustBinary {
812821
deps,
813822
named_deps,
814823
env,
824+
env_flags,
815825
link_style,
816826
linker_flags,
817827
preferred_linkage,
@@ -833,6 +843,9 @@ impl Serialize for RustBinary {
833843
if !env.is_empty() {
834844
map.serialize_entry("env", env)?;
835845
}
846+
if !env_flags.is_empty() {
847+
map.serialize_entry("env_flags", env_flags)?;
848+
}
836849
if !features.is_empty() {
837850
map.serialize_entry("features", features)?;
838851
}

src/buckify.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,23 @@ fn generate_target_rules<'scope>(
657657
)
658658
.context("env")?;
659659

660+
unzip_platform(
661+
config,
662+
&mut base,
663+
&mut perplat,
664+
|rule, flags| {
665+
log::debug!(
666+
"pkg {} target {}: adding env_flags: {:?}",
667+
pkg,
668+
tgt.name,
669+
flags,
670+
);
671+
rule.env_flags.extend(flags);
672+
},
673+
fixups.buildscript_env_flags(),
674+
)
675+
.context("env_flags")?;
676+
660677
// Compute set of dependencies any rule we generate here will need. They will only
661678
// be emitted if we actually emit some rules below.
662679
let mut dep_pkgs = Vec::new();

src/fixups.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,26 @@ impl<'meta> Fixups<'meta> {
954954
Ok(Some(value))
955955
}
956956

957+
/// Additional environment variable settings that are passed through flags
958+
pub fn buildscript_env_flags(&self) -> Vec<(Option<PlatformExpr>, BTreeSet<String>)> {
959+
let mut ret = vec![];
960+
if self.buildscript_target().is_none() {
961+
return ret; // no buildscript
962+
}
963+
964+
for (platform, config) in self.fixup_config.configs(&self.package.version) {
965+
if !self.target.kind_custom_build() && config.buildscript.run.is_some() {
966+
let flags = BTreeSet::from_iter([format!(
967+
"@$(location :{}[env_flags])",
968+
self.buildscript_genrule_name()
969+
)]);
970+
ret.push((platform.cloned(), flags));
971+
}
972+
}
973+
974+
ret
975+
}
976+
957977
/// Given a glob for the srcs, walk the filesystem to get the full set.
958978
/// `srcs` is the normal source glob rooted at the package's manifest dir.
959979
pub fn compute_srcs(

0 commit comments

Comments
 (0)