Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: use runtime OUT_DIR #3100

Closed
wants to merge 1 commit into from
Closed

Conversation

coffinmatician
Copy link

This helps building on systems where they may be different (bazel, buck2, etc.)

@adamreichold
Copy link
Member

This helps building on systems where they may be different (bazel, buck2, etc.)

Could you mention this rationale (build systems other than Cargo) in the news fragment? Thanks!

@kngwyu
Copy link
Member

kngwyu commented Apr 13, 2023

I'm also a bit confused. If this PR is meant to handle the case when 'OUT_DIR' is not set, the PR title and changelog need to be revised. I don't have any experience using another build system than cargo to compile Rust crate, but do you mean that some of them don't set OUT_DIR?

@adamreichold
Copy link
Member

I'd say this is basically about replacing env!("OUT_DIR") (evaluated when build.rs is compiled) by env_os("OUT_DIR") (evaluated when it is executed).

@kngwyu
Copy link
Member

kngwyu commented Apr 13, 2023

Ah, I see, thank you.

path.push("pyo3-build-config.txt");
path
env::var_os("TARGET").and_then(|target| {
std::env::var_os("OUT_DIR").map(|out_dir| {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a link to the documentation of e.g. bazel or buck2 that says how they set OUT_DIR? otherwise it will be hard to determine in the future why it's written in this way and how you may or may not change it

Copy link
Author

@coffinmatician coffinmatician Apr 13, 2023

Choose a reason for hiding this comment

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

Uhh, I don't think it is documented anywhere explicitly. That is just a design decision of some implementations such as cargo-raze. (I'm guessing they don't document it as that sort of design decision is everywhere in Bazel and so would be obvious to someone who has used it for a while (all actions run in different environments and you can't rely on the old OUT_DIR being the new OUT_DIR)).

Copy link
Member

Choose a reason for hiding this comment

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

I also think this strictly an improvement as we never needed compile-time access to OUT_DIR in the first place, i.e. we could have written it this way from the start. But let's wait what @konstin thinks w.r.t. your answer before merging.

@coffinmatician
Copy link
Author

I'm also a bit confused. If this PR is meant to handle the case when 'OUT_DIR' is not set, the PR title and changelog need to be revised. I don't have any experience using another build system than cargo to compile Rust crate, but do you mean that some of them don't set OUT_DIR?

I'd say this is basically about replacing env!("OUT_DIR") (evaluated when build.rs is compiled) by env_os("OUT_DIR") (evaluated when it is executed).

It is as adamreichold says.

path.push("pyo3-build-config.txt");
path
env::var_os("TARGET").and_then(|target| {
std::env::var_os("OUT_DIR").map(|out_dir| {
Copy link
Member

Choose a reason for hiding this comment

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

I also think this strictly an improvement as we never needed compile-time access to OUT_DIR in the first place, i.e. we could have written it this way from the start. But let's wait what @konstin thinks w.r.t. your answer before merging.

@adamreichold
Copy link
Member

@coffinmatician I know I am to blame because I asked for the removal of Path::new, but it seems like you need to fix the unused import warning triggered by that as well.

This helps building on systems where they may be different (bazel, buck2, etc.)
@coffinmatician
Copy link
Author

Hmm, I think we may want to wait on this patch a bit longer. A coworker is suspicious that I have broken things subtly for other targets. I probably should have tested it more in traditional cargo to make sure it was working as I expected, but I assumed that the CI here would test some cross compilation.

@adamreichold
Copy link
Member

but I assumed that the CI here would test some cross compilation.

I want to say that we do, e.g. https://github.com/PyO3/pyo3/actions/runs/4692932437/jobs/8319284869, but it seems we are missing the CARGO_BUILD_TARGET environment variable for clippy CI jobs that we set for the build ones? cc @messense

@davidhewitt
Copy link
Member

Hmm, I think we may want to wait on this patch a bit longer. A coworker is suspicious that I have broken things subtly for other targets. I probably should have tested it more in traditional cargo to make sure it was working as I expected, but I assumed that the CI here would test some cross compilation.

Sorry I didn't get to commenting on this thread sooner.

If I recall correctly, I made the choice to bake-in the OUT_DIR at build time because this code can get evaluated from multiple different build scripts and I wasn't sure that it would be the same each time.

I think #1758 is the right PR with the original context. At the time we needed to access the config from our proc macros. In #2243 that access was removed.

It's very possible there's a solution I didn't think of, or something enabled by newer cargo functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants