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

Discussion: Reconsider target parsing #1317

Open
madsmtm opened this issue Dec 6, 2024 · 10 comments
Open

Discussion: Reconsider target parsing #1317

madsmtm opened this issue Dec 6, 2024 · 10 comments

Comments

@madsmtm
Copy link
Collaborator

madsmtm commented Dec 6, 2024

I'm beginning to regret the decision we took in #1225 (comment) to pre-generate a static list of target information from the current nightly, instead of trying to guess it from the target name.

The gist of the issue is that static data doesn't help us much when using a custom target - but when using custom targets, we usually still need to default to some value (example: the LLVM target). Also, I don't like that you can sometimes use cc in a build script, but not outside.

And yeah, rustc target names are a mess, and they're impossible to parse correctly in general, but we can do a fairly good job at it (that's what I implemented in that PR initially) - especially if we have CI that verifies the parsing code against the latest rustc specs. Of course, manual parsing code is more tedious to update, i.e. the automated GitHub PRs would've instead been turned into a CI failure that we'd have to fix.

Idk., still don't know how I feel about it, but I do know that I was unhappy with #1315, as I felt I kinda had to get the default relocation model guessing correct, and once it was correct, there wasn't any point in the pre-generated data.

Related issues:

@NobodyXu
Copy link
Collaborator

NobodyXu commented Dec 6, 2024

The gist of the issue is that static data doesn't help us much when using a custom target - but when using custom targets, we usually still need to default to some value (example: the LLVM target).

Maybe we can require environment variables to be set for customs?

We could use cc::Build::envs to avoid unsafe set_var

@tronical
Copy link
Contributor

tronical commented Dec 6, 2024

As one of the affected parties, I think what surprised me most is that this came in a minor version and broke existing builds (#1297). I think the change itself makes a lot of sense, but it would have been a lot easier if this was in a new major version so that the transition can be controlled.

Just my two cents. Absolutely love the crate and how responsive you all are as maintainers.

@lasiotus
Copy link

lasiotus commented Dec 14, 2024

Yes, please reconsider target parsing!

Context: Motor OS is a custom Rust target at the moment (not an official Tier-3 target), so it is not part of the pre-generated list of "targets known to rustc". The switch to pre-generated targets keeps us on an older Rust version, which is somewhat unfortunate.

Maybe do target parsing for targets not found in the static/pregenerated list?

Related issue: moturus/motor-os#18

@NobodyXu
Copy link
Collaborator

NobodyXu commented Dec 15, 2024

Maybe do target parsing for targets not found in the static/pregenerated list?

I think that'd be reasonable, but shouldn't cargo already pass environment variables that we could use https://docs.rs/cc/latest/src/cc/target/parser.rs.html

cc @lasiotus

@lasiotus
Copy link

[...] shouldn't cargo already pass environment variables that we could use https://docs.rs/cc/latest/src/cc/target/parser.rs.html

At the moment rustc/stdlib bootstrap calls get_compiler() which kills the process if the target is not recognized.

Essentially we have here a circular dependency: Rust's target bootstrap depends on cc recognizing the target, which does so only for "targets recognized by rustc", if I understand the logic here correctly.

So unofficial targets are now a bit more painful to maintain/work on.

@NobodyXu
Copy link
Collaborator

At the moment rustc/stdlib bootstrap calls get_compiler() which kills the process if the target is not recognized.

Aha so it's not a build-script issue...

Maybe you could set CARGO_* manually?

https://docs.rs/cc/latest/src/cc/target/parser.rs.html

@lasiotus
Copy link

Maybe you could set CARGO_* manually?

It seems this should work - I'll try a bit later; thanks for the suggestion!

@lasiotus
Copy link

Maybe you could set CARGO_* manually?

It seems this should work - I'll try a bit later; thanks for the suggestion!

Doesn't work: rustc bootstrap code sets Build::target string triple prior to calling get_compiler(); but get_compiler() -> get_target() parses cargo env vars only if the target string is not set. The latest cc-rs version even explicitly advises forking itself for unrecognized targets. So now new targets need to fork both rust and cc-rs. Again, seems like a circular dependency...

Probably the bootstrap code can be hacked to not set the target string triple before calling get_compiler(), but this is becoming increasingly fragile...

@NobodyXu
Copy link
Collaborator

Doesn't work: rustc bootstrap code sets Build::target string triple prior to calling get_compiler();

As long as value set by Build::target matches the env variable TARGET it would take the CARGO_* env parsing step

@lasiotus
Copy link

lasiotus commented Dec 15, 2024

As long as value set by Build::target matches the env variable TARGET it would take the CARGO_* env parsing step

That worked, thanks!

As it works only at head, and not at 1.2.0 to which Rust bootstrap is pinned, I created a PR to bump the pin to 1.2.4.

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

No branches or pull requests

4 participants