Skip to content

Conversation

namanlp
Copy link
Contributor

@namanlp namanlp commented Aug 28, 2025

This PR is continuation of my work on GSoC Project : Build Script Delegation

What does this PR try to resolve?

Through this PR, I want the user to be able to access each build script's OUT_DIR.

How to test and review this PR?

There is a feature gate multiple-build-scripts that can be passed via cargo-features in Cargo.toml. So, you have to add

cargo-features = ["multiple-build-scripts"]

Preferably on the top of the Cargo.toml and use nightly toolchain to use the feature

Then, you can access the OUT_DIR of given build script by accessing it like a compile time environment variable, for example:

const BUILD1_OUT_DIR: &'static str = env!("BUILD1_OUT_DIR");
const BUILD2_OUT_DIR: &'static str = env!("BUILD2_OUT_DIR");

fn main() {
    println!("{}", BUILD1_OUT_DIR);
    println!("{}", BUILD2_OUT_DIR);
}

@namanlp namanlp force-pushed the multiple-build-scripts-4 branch from 75ff71c to 0d5b7b0 Compare August 28, 2025 14:50
@rustbot rustbot added the A-build-scripts Area: build.rs scripts label Aug 28, 2025
@namanlp namanlp force-pushed the multiple-build-scripts-4 branch 3 times, most recently from c0d23d2 to 6c8bc08 Compare September 7, 2025 12:37
@rustbot rustbot added the A-build-execution Area: anything dealing with executing the compiler label Sep 7, 2025
@namanlp namanlp force-pushed the multiple-build-scripts-4 branch 4 times, most recently from 5581004 to c9e3081 Compare September 11, 2025 20:34
@namanlp namanlp force-pushed the multiple-build-scripts-4 branch 2 times, most recently from 65b6c7f to 4117277 Compare September 15, 2025 21:11
@namanlp namanlp force-pushed the multiple-build-scripts-4 branch 3 times, most recently from 16fb171 to 8f929ff Compare September 23, 2025 20:48
@namanlp namanlp force-pushed the multiple-build-scripts-4 branch from 8f929ff to 1ab5374 Compare September 29, 2025 19:34
@namanlp namanlp force-pushed the multiple-build-scripts-4 branch from 1ab5374 to b5150ef Compare September 29, 2025 20:29
@rustbot rustbot added the A-documenting-cargo-itself Area: Cargo's documentation label Sep 29, 2025
@namanlp namanlp marked this pull request as ready for review September 29, 2025 20:29
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 29, 2025

r? @weihanglo

rustbot has assigned @weihanglo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@namanlp
Copy link
Contributor Author

namanlp commented Sep 29, 2025

r? @epage

@rustbot rustbot assigned epage and unassigned weihanglo Sep 29, 2025
@epage epage changed the title [WIP] Accessing each build script's OUT_DIR Accessing each build script's OUT_DIR Sep 29, 2025
Comment on lines +259 to +261
* `<script-name>_OUT_DIR` --- If the package has build script(s), this is set to the folder where the
build script should place its output. The `<script-name>` is the file-stem of the build script, exactly as-is.
For example, `bar_OUT_DIR` for script at `foo/bar.rs`. (Only set during compilation.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be on unstable.md

Comment on lines +1716 to +1722
let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
let target_name = dep.unit.target.name();
let out_dir_prefix = target_name
.strip_prefix("build-script-")
.unwrap_or(target_name);
let out_dir_name = format!("{out_dir_prefix}_OUT_DIR");
cmd.env(&out_dir_name, &out_dir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't is need a feature gate?

Comment on lines +921 to +925
err_msg += &format!(
"\nBuild Scripts : {} have the same file stem, that is {}",
paths.join(", "),
stem
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err_msg += &format!(
"\nBuild Scripts : {} have the same file stem, that is {}",
paths.join(", "),
stem
);
write!(&mut err_msg, " for stem `{stem}`: {}", paths.join(", "))?;
  • generally better to use write! to append formatted text
  • duplicating the error message
  • needs indentation to set apart from the rest of the message

}

#[cargo_test]
fn multiple_out_dirs() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this test be added before the validation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-execution Area: anything dealing with executing the compiler A-build-scripts Area: build.rs scripts A-documenting-cargo-itself Area: Cargo's documentation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants