Skip to content

Commit

Permalink
Factor custom flags management to one function
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed May 18, 2022
1 parent 276c6d1 commit f2a1e43
Showing 1 changed file with 32 additions and 42 deletions.
74 changes: 32 additions & 42 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
)?;
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
}
add_custom_env(&mut rustc, &script_outputs, script_metadata);
add_custom_flags(&mut rustc, &script_outputs, script_metadata)?;
}

for output in outputs.iter() {
Expand Down Expand Up @@ -408,15 +408,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
}

if key.0 == current_id {
for cfg in &output.cfgs {
rustc.arg("--cfg").arg(cfg);
}
if !output.check_cfgs.is_empty() {
rustc.arg("-Zunstable-options");
for check_cfg in &output.check_cfgs {
rustc.arg("--check-cfg").arg(check_cfg);
}
}
if pass_l_flag {
for name in output.library_links.iter() {
rustc.arg("-l").arg(name);
Expand All @@ -437,22 +428,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
}
Ok(())
}

// Add all custom environment variables present in `state` (after they've
// been put there by one of the `build_scripts`) to the command provided.
fn add_custom_env(
rustc: &mut ProcessBuilder,
build_script_outputs: &BuildScriptOutputs,
metadata: Option<Metadata>,
) {
if let Some(metadata) = metadata {
if let Some(output) = build_script_outputs.get(metadata) {
for &(ref name, ref value) in output.env.iter() {
rustc.env(name, value);
}
}
}
}
}

/// Link the compiled target (often of form `foo-{metadata_hash}`) to the
Expand Down Expand Up @@ -719,22 +694,11 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
let mut output_options = OutputOptions::new(cx, unit);
let script_metadata = cx.find_build_script_metadata(unit);
Ok(Work::new(move |state| {
if let Some(script_metadata) = script_metadata {
if let Some(output) = build_script_outputs.lock().unwrap().get(script_metadata) {
for cfg in output.cfgs.iter() {
rustdoc.arg("--cfg").arg(cfg);
}
if !output.check_cfgs.is_empty() {
rustdoc.arg("-Zunstable-options");
for check_cfg in &output.check_cfgs {
rustdoc.arg("--check-cfg").arg(check_cfg);
}
}
for &(ref name, ref value) in output.env.iter() {
rustdoc.env(name, value);
}
}
}
add_custom_flags(
&mut rustdoc,
&build_script_outputs.lock().unwrap(),
script_metadata,
)?;
let crate_dir = doc_dir.join(&crate_name);
if crate_dir.exists() {
// Remove output from a previous build. This ensures that stale
Expand Down Expand Up @@ -1196,6 +1160,32 @@ fn build_deps_args(
Ok(())
}

/// Add custom flags from the output a of build-script to a `ProcessBuilder`
fn add_custom_flags(
cmd: &mut ProcessBuilder,
build_script_outputs: &BuildScriptOutputs,
metadata: Option<Metadata>,
) -> CargoResult<()> {
if let Some(metadata) = metadata {
if let Some(output) = build_script_outputs.get(metadata) {
for cfg in output.cfgs.iter() {
cmd.arg("--cfg").arg(cfg);
}
if !output.check_cfgs.is_empty() {
cmd.arg("-Zunstable-options");
for check_cfg in &output.check_cfgs {
cmd.arg("--check-cfg").arg(check_cfg);
}
}
for &(ref name, ref value) in output.env.iter() {
cmd.env(name, value);
}
}
}

Ok(())
}

/// Generates a list of `--extern` arguments.
pub fn extern_args(
cx: &Context<'_, '_>,
Expand Down

0 comments on commit f2a1e43

Please sign in to comment.