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

De-couple Python and bootstrap slightly #76544

Merged
merged 5 commits into from
Sep 21, 2020

Conversation

Mark-Simulacrum
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum commented Sep 9, 2020

This revises rustbuild's entry points from Python to rely less on magic environment variables, preferring to use Cargo-provided environment variables where feasible.

Notably, BUILD_DIR and BOOTSTRAP_CONFIG are not moved, because both more-or-less have some non-trivial discovery logic and replicating it in rustbuild seems unfortunate; if it moved to Cargo that would be a different story.

Best reviewed by-commit.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 9, 2020
@Mark-Simulacrum
Copy link
Member Author

cc @jyn514

@jyn514 jyn514 added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Sep 9, 2020
Copy link
Member

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

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

src/bootstrap/config.rs Outdated Show resolved Hide resolved
src/bootstrap/config.rs Show resolved Hide resolved
@mati865
Copy link
Contributor

mati865 commented Sep 10, 2020

How crazy would it be to move all the logic to Rust and leave x.py only to download stage0 compiler that will be used to build bootstrap?

Something like this should allow updating submodules: rust-lang/cargo#1248 (comment)

@Mark-Simulacrum
Copy link
Member Author

AFAICT, there is no way for Cargo to resolve the workspace without submodules being checked out -- the manifest for Cargo (src/tools/Cargo/Cargo.toml) is just not available. We also cannot easily make rustbuild its own workspace, because that prevents the top-level workspace from using cargo run with default-run appropriately configured to point at bootstrap.

If we want to move the logic, I think we will need to stop trying to build bootstrap locally on everyone's computer and instead download the binaries from CI -- we already know that bootstrap is only useful if your build triple has Rust (as we need a stage 0 compiler), so this is not a painful requirement I suspect. I would prefer to hold off on doing that, though, and the duplication in this PR is not major; most of the code it duplicates has been largely unchanged since it was introduced.

I plan to explore moving our submodule dependencies to Cargo-based git dependencies, or similar, where possible -- that would let Cargo resolve everything normally and, hopefully, avoid some of the current pain points with submodules. I suspect this should be possible for all of the crates we need for workspace resolution, but have not checked. IMO asking people to do a git submodule update --init as necessary is already not too bad.

@jyn514
Copy link
Member

jyn514 commented Sep 10, 2020

I plan to explore moving our submodule dependencies to Cargo-based git dependencies, or similar, where possible -- that would let Cargo resolve everything normally and, hopefully, avoid some of the current pain points with submodules.

This sounds really amazing!

Maybe to avoid the duplication we could land Cargo-based git dependencies first? I don't feel super strongly about that but I do think submodules are much more painful than python x.py. This is from my biased linux viewpoint though.

@Mark-Simulacrum
Copy link
Member Author

I do not yet know whether the submodules are even feasible to remove, it's something I do want to investigate. In any case, it seems like x.py is a hard blocker for some developers and submodules are likely not that (I've never really needed more than some git submodule update --init and git -C src/tools/cargo reset --hard). The duplication again is pretty minor.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 10, 2020
@Mark-Simulacrum
Copy link
Member Author

Moving this over to waiting-on-author until I fix CI here, it looks like there's going to be a number of bugs in various components that were relying on the environment variables set for just rustbuild's consumption.

@Mark-Simulacrum
Copy link
Member Author

Okay, this is now ready for review, marking as such.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 11, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 11, 2020

What's the example usage for this? Can you run cargo run -- build --stage 0 from the top level directory?

@Mark-Simulacrum
Copy link
Member Author

Yes, and no need for -- in many cases.

@bors

This comment has been minimized.

@Mark-Simulacrum
Copy link
Member Author

Okay, rebased.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

I've left some comments inline, but overall I'm a little confused by the purpose of this PR. Is there a vision for where the build system is going to end up? I'm not sure what the end goal is myself and it feels like this is still pretty far from it but it's already bending over quite a lot and contorting various pieces here and there to try to make something work. I'd generally want to make sure that we know where this is all going to land before landing some of the weirder parts of this.

There's a couple of pieces though that seem good to land as it's just moving some stuff around and generally more into Rust than Python.

}

let build_rustc = build.rustc;
config.initial_rustc = env::var_os("RUSTC")
Copy link
Member

Choose a reason for hiding this comment

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

Could this unconditionally be whatever rustc compiled the build script? (the build script should have a RUSTC env var)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think there's probably a cargo bug (or maybe rustup?), but for me that environment variable gets set to just "rustc" when compiling through rustup/cargo, even with +beta, which would mean that we don't use the right rustc. Not a problem with x.py, because that sets RUSTC in the environment which fixes things I believe.

I'm down for removing this whole section and trying to fix the upstream bug, wherever that is, though.

Copy link
Member

Choose a reason for hiding this comment

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

Oh that's probably because Cargo just executes rustc itself, in that case could the build script read RUSTC and find it in PATH?

// Make sure that our variables, pulled from Cargo, are equivalent to
// configuration.
if let Some(cargo) = build.cargo.map(PathBuf::from) {
assert_eq!(config.initial_cargo, cargo.canonicalize().expect("cargo exists"));
Copy link
Member

Choose a reason for hiding this comment

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

Could initial_cargo be set to the CARGO env var used to compile the build script?

Copy link
Member Author

Choose a reason for hiding this comment

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

We do that here -- https://github.com/rust-lang/rust/pull/76544/files#diff-1731657fe6a8025cbe7a498b30e93890R478 -- this is just checking that if you've done that and also configured initial-cargo in the toml file they match. Maybe we shouldn't bother though and just treat the command line as an override?

Copy link
Member

Choose a reason for hiding this comment

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

Ah yeah in that case I'd just treat this as an override.

src/bootstrap/config.rs Outdated Show resolved Hide resolved
.arg("build")
.arg("--manifest-path")
.stdout(std::process::Stdio::null())
.arg(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"));
Copy link
Member

Choose a reason for hiding this comment

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

I must admit, I don't really understand the motivation of this PR, but blocks like this seem pretty weird. This seems like it's bending over really far backwards to try to get cargo run to work. Cargo was never intended to be a full-fledged general purpose build system though, and building the compiler is just compilcated due to how much stuff needs to get built in various ways. This isn't the end of the world to have this here but this feels ripe for bugs and other issues.

(and this is just the beginning of trying to get cargo run to work...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Much of this discussion has been happening on Zulip. This PR probably doesn't make sense without having followed along there.

I will say that, regardless of where Cargo started or what was intended back in the day, it needs to become a general build system eventually, or at least become far closer to being one. Because Cargo is how every single Rust programmer is told to interact with their Rust projects. So when Cargo can't handle a task, it's suddenly astoundingly painful to do that task with a Rust project. This doesn't need to happen today or tomorrow, but it needs to be a long term goal.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Much of this discussion has been happening on Zulip

Ah ok, thanks for the heads up. For reviewing this though I don't have time to read up on the whole stream, so I'm gonna stick to what's in this PR.

it needs to become a general build system eventually

This PR is probably not the best location to push this idea.

.out
.components()
.flat_map(|component| {
if component.as_os_str() == "$ROOT" {
Copy link
Member

Choose a reason for hiding this comment

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

This... seems like a misfeature. I guess it's supported in the python code though...

Copy link
Member Author

Choose a reason for hiding this comment

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

This was added in #71994, but yeah I'm not entirely 100% sure it's something we truly need (vs. forcing absolute paths or whatever in configuration).

Copy link
Member

Choose a reason for hiding this comment

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

I'd be ok with removing this. What I'd really like is for the build directory to always be relative to the source directory, not the current directory, but I don't think anyone is using $ROOT.

@Mark-Simulacrum
Copy link
Member Author

FWIW, with this PR locally I have cargo run check and cargo run build and such working, so this PR is enough to get it working for most use cases, modulo submodule management/stage0 version verification/vendoring.

So in that sense while I agree that this is definitely bending over backwards in some places it's not that much and does put us in a place where people who get pushed away by the Python entry point (I am told that on Windows, Python usage is a red flag by @Lokathor, though I don't personally have experience there to expand on that).

I'm happy to drop some parts of this (or extract others into separate PRs), though -- I'm not personally feeling strongly that this set of commits and the duplication/contortions they introduce are all that desirable. Though admittedly I have a hard time evaluating whether it's worth it when I haven't myself experienced the Windows Python issues...

@jyn514
Copy link
Member

jyn514 commented Sep 14, 2020

cc also @Kixiron and @RDambrosio016 (#75156)

@Lokathor
Copy link
Contributor

@Mark-Simulacrum With simply proper documentation of the expected build environment, the python usage on Windows would be much less of a red flag to potential newcomers.

However, I think that in (say) the next 5 years or so, moving towards the compiler and standard library working with just cargo and not an external tool would be very valuable.

But I'm totally willing to believe that even with that as a goal, "it's not yet time for those changes".

@Kixiron
Copy link
Member

Kixiron commented Sep 15, 2020

Python has the tendency to be "finicky" on windows, to say the least. Even past the issues I've encountered (and recognize are pretty fringe), having the build system in Rust is great since it lowers the barrier of entry since everyone contributing to the Rust compiler probably knows Rust to some capacity

@Mark-Simulacrum
Copy link
Member Author

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Sep 20, 2020

📌 Commit cf33aad has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2020
@Mark-Simulacrum
Copy link
Member Author

@bors rollup=never (prone to failures on various builders)

@bors
Copy link
Contributor

bors commented Sep 20, 2020

⌛ Testing commit cf33aad with merge 7467d17...

@bors
Copy link
Contributor

bors commented Sep 21, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: alexcrichton
Pushing 7467d17 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 21, 2020
@bors bors merged commit 7467d17 into rust-lang:master Sep 21, 2020
@rustbot rustbot added this to the 1.48.0 milestone Sep 21, 2020
Mark-Simulacrum pushed a commit to jyn514/rust that referenced this pull request Mar 7, 2022
Same rationale as rust-lang#76544;
it would be nice to make python entirely optional at some point.

This also removes $ROOT as an option for the build directory; I haven't been using it, and like Alex
said in rust-lang#76544 (comment) it seems like a
misfeature.

This allows running `cargo run` from src/bootstrap, although that still gives
lots of compile errors if you don't use the beta toolchain.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 8, 2022
…lacrum

Move some more bootstrap logic from python to rust

Same rationale as rust-lang#76544; it would be nice to make python entirely optional at some point.

This also removes $ROOT as an option for the build directory; I haven't been using it, and like Alex
said in rust-lang#76544 (comment) it seems like a misfeature.

This allows running `cargo run` from src/bootstrap, although that still gives
lots of compile errors if you don't use the beta toolchain. It's not exactly the same as using `x.py`, since it won't have `BOOTSTRAP_DOWNLOAD_RUSTC` set, but it's pretty close. Doing this from the top-level directory requires rust-lang/cargo#7290 to be fixed, or using `cargo run -p bootstrap`.

The next steps for making python optional are to move download-ci-llvm and download-rustc support into rustbuild, likely be shelling out as the python scripts do today.

It would also be nice (although not required) to move submodule support there, but that would require taking bootstrap out of the workspace to avoid errors from crates that haven't been cloned yet.

r? `@Mark-Simulacrum`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants