Skip to content

Commit

Permalink
autocargo/thrift: add support for rust_extra_srcs to fix mononoke OSS…
Browse files Browse the repository at this point in the history
… cargo build

Summary:
Add support for rust_extra_srcs to autocargo and rust shed thrift wrapper.  Copy extra sources to the cargo thrift build location so that the include!() can find them

Mononoke started using them in D63977688, which is causing [github CI Cargo based builds to fail](https://github.com/facebook/sapling/actions/runs/11329919951/job/31506538796) with `error[E0583]: file not found for module 'extra_thrift'`

Mononoke cargo build was also broken on missing scs_errors and scs_methods crate used from async_requests, hence the autocargo mapping update to include it in following diff D64541433

Reviewed By: andreacampi

Differential Revision: D64541434

fbshipit-source-id: 118508175e4168b15e4c99dd63a6eaa34ac10c5e
  • Loading branch information
ahornby authored and facebook-github-bot committed Oct 18, 2024
1 parent 3b6a64b commit 1630d1f
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions shed/thrift_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct Config {
clients_crate: Option<String>,
options: Option<String>,
include_srcs: Vec<PathBuf>,
extra_srcs: Vec<PathBuf>,
}

impl Config {
Expand All @@ -96,6 +97,7 @@ impl Config {
clients_crate: None,
options: None,
include_srcs: vec![],
extra_srcs: vec![],
})
}

Expand Down Expand Up @@ -165,6 +167,13 @@ impl Config {
self
}

/// Set extra srcs to be copied into the generated crate.
pub fn extra_srcs(&mut self, value: impl IntoIterator<Item = impl AsRef<Path>>) -> &mut Self {
self.extra_srcs
.extend(value.into_iter().map(|path| path.as_ref().to_owned()));
self
}

/// Transform a relative path so leading "../"'s are replaced with "_t".
pub fn remap_to_out_dir(&self, path: &Path) -> PathBuf {
let mut rem = path;
Expand Down Expand Up @@ -200,6 +209,15 @@ impl Config {
}
}

for extra_src in &self.extra_srcs {
println!("cargo:rerun-if-changed={}", extra_src.to_string_lossy());
if let GenContext::Types = self.gen_context {
let out_path = self.remap_to_out_dir(extra_src);
fs::create_dir_all(out.join(out_path.parent().unwrap()))?;
fs::copy(extra_src, out.join(out_path))?;
}
}

if let [(_name, file)] = &input[..] {
match self.gen_context {
GenContext::Types => {
Expand Down

0 comments on commit 1630d1f

Please sign in to comment.