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

Add default search path to Target::search() #83800

Merged
merged 3 commits into from
May 10, 2021

Conversation

xobs
Copy link
Contributor

@xobs xobs commented Apr 3, 2021

The function Target::search() accepts a target triple and returns a Target struct defining the requested target.

There is a // FIXME 16351: add a sane default search path? comment that indicates it is desirable to include some sort of default. This was raised in #16351 which was closed without any resolution.

#31117 was proposed, however that has platform-specific logic that is unsuitable for systems without /etc/.

This patch implements the suggestion raised in #16351 (comment) where a target.json file may be placed in $(rustc --print sysroot)/lib/rustlib/<target-triple>/target.json. This allows shipping a toolchain distribution as a single file that gets extracted to the sysroot.

This enables placing a `target.json` file into the rust sysroot under
the target-specific directory.

Signed-off-by: Sean Cross <sean@xobs.io>
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @davidtwco (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 3, 2021
@rust-log-analyzer

This comment has been minimized.

This fixes a build issue with formatting as part of rust-lang#83800.

Signed-off-by: Sean Cross <sean@xobs.io>
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Implementation looks good to me. Going to reassign to another team member just to confirm that this is behaviour that we want to support.

@davidtwco
Copy link
Member

r? @nagisa

@rust-highfive rust-highfive assigned nagisa and unassigned davidtwco Apr 5, 2021
@nagisa nagisa added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-nominated labels Apr 6, 2021
@nagisa
Copy link
Member

nagisa commented Apr 6, 2021

Nominating for a brief look at the T-compiler meeting. It isn't entirely obvious to me if we are comfortable with enable further ability to transparently use adhoc target json specs given how unstable they are in practice.

OTOH fetching them from sysroot sounds like a pretty good middle-ground here.

@xobs
Copy link
Contributor Author

xobs commented Apr 7, 2021

An earlier version of this patch didn't look for $(rustc --print sysroot)/lib/rustlib/<target-triple>/target.json, but instead looked for $(rustc --print sysroot)/<target-triple>.json, but I changed that after reading #16351 (comment)

I could always change it back if desired.

@nagisa
Copy link
Member

nagisa commented Apr 10, 2021

This was briefly discussed during the compiler meeting this Thursday. and then further in this thread.

In summary the consensus seems to be that there's missing context motivating this change. Neither the TODO comment nor the linked issue really provides it. It would be great to gather some. I looked at couple other places. Here's what I found so far:

  1. The original RFC introducing the target mechanism used today specified RUST_TARGET_PATH would default to some directory. This has been since removed;
  2. The PR removing this default from the RFC says such an addition would require much more scrutiny than what was afforded to the RFC in the original place;
  3. There isn't any discussion about the default or the environment variable in the RFC issue or meeting minutes discussing this RFC… or anywhere else really;
  4. So far 7 years have passed and there weren't really any major complaints.

While we probably won't make anybody write any RFCs for a change like this, this will at least involve an FCP vote on this PR, and having a motivation documented is a great way to set such a vote up for success.

@xobs do you have any motivating use-cases that made you submit this PR?

@xobs
Copy link
Contributor Author

xobs commented Apr 11, 2021

My motivation is the same as the one in #16351 (comment) -- to be able to distribute a custom toolchain for a new platform.

I have a new operating system I'm working on, and this includes a libstd. This is bundled as patches on top of this repository, with the goal of porting them forward with each new version of Rust. I don't want to force users to use nightly, because many crates detect if they're building on nightly and enable special features. Owing to the shifting nature of the nightly compiler, this means that as soon as we require nightly we bind ourselves to a specific version of the compiler. stable fixes this problem thanks to the amazing work of the Rust team.

The current approach is to distribute a .tar.gz with the root, and have users do export RUST_TARGET_PATH=$(rustc --print sysroot). This allows them to compile software with cargo build --target riscv32imac-unknown-xous-elf. Without setting this environment variable, a hash is appended to the compiled target name and I become unable to distribute the toolchain.

Alternatives to this approach are to patch rustc for every host to include the toolchain file, which requires me to maintain both libstd as well as rustc itself. I can also force users to use nightly, which would open us up to the version mismatches I described above.

My current approach is described at https://github.com/betrusted-io/rust#rust-stable-for-xous. I feel like I'm breaking quite a few rules here, and would welcome descriptions of alternate approaches I could take.

@nagisa nagisa added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 11, 2021
@nagisa
Copy link
Member

nagisa commented Apr 11, 2021

Do note that the target files are an implementation detail and don't confer any stability guarantees, regardless of whether it is a stable rustc or not.

Anyway, given the motivation above:

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Apr 11, 2021

Team member @nagisa has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Apr 11, 2021
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
// as a fallback.
let p =
sysroot.join("lib").join("rustlib").join(&target_triple).join("target.json");
Copy link
Member

Choose a reason for hiding this comment

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

This should likely consider the possibility of lib being defined differently, perhaps by using the filesearch facilities in rustc_session (though that may be painful, as the two crates already have a dependency edge in the wrong direction, so some amount of code duplication or movement would be necessary).

Copy link
Member

Choose a reason for hiding this comment

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

This concern seems to have gone unaddressed, and to my knowledge distros are precisely the case we typically see nonstandard lib paths. I would at least like to see an issue filed.

@Mark-Simulacrum
Copy link
Member

It seems reasonable to support this, but I am wondering if there has been some background research on what other compilers do, particularly those that like rustc are always capable of cross-compiling. This solution seems reasonable, but it seems plausible that there may be pitfalls others have encountered that we have not addressed here. While I believe we are not committing to this being the behavior indefinitely (after all, target.json's contents are unstable), changing the search path will still be relatively disruptive, so doing some research first seems good.

@petrochenkov
Copy link
Contributor

Seems fine to keep a target spec file inside the directory corresponding to the target's "rustup component", ticked the box.

@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@bors
Copy link
Contributor

bors commented Apr 24, 2021

☔ The latest upstream changes (presumably #84501) made this pull request unmergeable. Please resolve the merge conflicts.

@rfcbot
Copy link

rfcbot commented Apr 24, 2021

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Apr 24, 2021
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels May 4, 2021
@rfcbot
Copy link

rfcbot commented May 4, 2021

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label May 6, 2021
@nagisa
Copy link
Member

nagisa commented May 9, 2021

@bors r+

@bors
Copy link
Contributor

bors commented May 9, 2021

📌 Commit f9d390d has been approved by nagisa

@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-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2021
@nagisa
Copy link
Member

nagisa commented May 9, 2021

Note that all of the concerns raised above are still valid. I, however, don't believe that this functionality will be utilized widely enough that adjusting or removing it will be disruptive enough to prevent changes. Among other things sysroot layout is also an unstable detail, so changing this in the future seems fairly plausible.

@bors
Copy link
Contributor

bors commented May 9, 2021

⌛ Testing commit f9d390d with merge 6d1405bc0d4d2018f18744d522c2749c52ed714a...

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request May 9, 2021
Add default search path to `Target::search()`

The function `Target::search()` accepts a target triple and returns a `Target` struct defining the requested target.

There is a `// FIXME 16351: add a sane default search path?` comment that indicates it is desirable to include some sort of default. This was raised in rust-lang#16351 which was closed without any resolution.

rust-lang#31117 was proposed, however that has platform-specific logic that is unsuitable for systems without `/etc/`.

This patch implements the suggestion raised in rust-lang#16351 (comment) where a `target.json` file may be placed in `$(rustc --print sysroot)/lib/rustlib/<target-triple>/target.json`. This allows shipping a toolchain distribution as a single file that gets extracted to the sysroot.
@Dylan-DPC-zz
Copy link

@bors retry yield

@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors
Copy link
Contributor

bors commented May 9, 2021

⌛ Testing commit f9d390d with merge c55c26c...

@bors
Copy link
Contributor

bors commented May 10, 2021

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing c55c26c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 10, 2021
@bors bors merged commit c55c26c into rust-lang:master May 10, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 10, 2021
@Mark-Simulacrum Mark-Simulacrum added the relnotes Marks issues that should be documented in the release notes of the next release. label May 10, 2021
@xobs xobs deleted the impl-16351-nightly branch May 10, 2021 03:25
nagisa added a commit to nagisa/rust that referenced this pull request May 10, 2021
With this the concerns expressed in rust-lang#83800 should be addressed.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 10, 2021
…k-Simulacrum

Adjust target search algorithm for rustlib path

With this the concerns expressed in rust-lang#83800 should be addressed.

r? `@Mark-Simulacrum`
wip-sync pushed a commit to NetBSD/pkgsrc-wip that referenced this pull request Aug 12, 2021
Pkgsrc changes:
 * Bump bootstrap requirements to 1.53.0.
 * Adjust patches, adapt to upstream changes, adjust cargo checksums
 * If using an external llvm, require >= 10.0

Upsteream changes:

Version 1.54.0 (2021-07-29)
============================

Language
-----------------------

- [You can now use macros for values in built-in attribute macros.][83366]
  While a seemingly minor addition on its own, this enables a lot of
  powerful functionality when combined correctly. Most notably you can
  now include external documentation in your crate by writing the following.
  ```rust
  #![doc = include_str!("README.md")]
  ```
  You can also use this to include auto-generated modules:
  ```rust
  #[path = concat!(env!("OUT_DIR"), "/generated.rs")]
  mod generated;
  ```

- [You can now cast between unsized slice types (and types which contain
  unsized slices) in `const fn`.][85078]
- [You can now use multiple generic lifetimes with `impl Trait` where the
   lifetimes don't explicitly outlive another.][84701] In code this means
   that you can now have `impl Trait<'a, 'b>` where as before you could
   only have `impl Trait<'a, 'b> where 'b: 'a`.

Compiler
-----------------------

- [Rustc will now search for custom JSON targets in
  `/lib/rustlib/<target-triple>/target.json` where `/` is the "sysroot"
  directory.][83800] You can find your sysroot directory by running
  `rustc --print sysroot`.
- [Added `wasm` as a `target_family` for WebAssembly platforms.][84072]
- [You can now use `#[target_feature]` on safe functions when targeting
  WebAssembly platforms.][84988]
- [Improved debugger output for enums on Windows MSVC platforms.][85292]
- [Added tier 3\* support for `bpfel-unknown-none`
   and `bpfeb-unknown-none`.][79608]

\* Refer to Rust's [platform support page][platform-support-doc] for more
   information on Rust's tiered platform support.

Libraries
-----------------------

- [`panic::panic_any` will now `#[track_caller]`.][85745]
- [Added `OutOfMemory` as a variant of `io::ErrorKind`.][84744]
- [ `proc_macro::Literal` now implements `FromStr`.][84717]
- [The implementations of vendor intrinsics in core::arch have been
   significantly refactored.][83278] The main user-visible changes are
   a 50% reduction in the size of libcore.rlib and stricter validation
   of constant operands passed to intrinsics. The latter is technically
   a breaking change, but allows Rust to more closely match the C vendor
   intrinsics API.

Stabilized APIs
---------------

- [`BTreeMap::into_keys`]
- [`BTreeMap::into_values`]
- [`HashMap::into_keys`]
- [`HashMap::into_values`]
- [`arch::wasm32`]
- [`VecDeque::binary_search`]
- [`VecDeque::binary_search_by`]
- [`VecDeque::binary_search_by_key`]
- [`VecDeque::partition_point`]

Cargo
-----

- [Added the `--prune <spec>` option to `cargo-tree` to remove a package from
  the dependency graph.][cargo/9520]
- [Added the `--depth` option to `cargo-tree` to print only to a certain depth
  in the tree ][cargo/9499]
- [Added the `no-proc-macro` value to `cargo-tree --edges` to hide procedural
  macro dependencies.][cargo/9488]
- [A new environment variable named `CARGO_TARGET_TMPDIR` is
  available.][cargo/9375]
  This variable points to a directory that integration tests and
  benches can use as a "scratchpad" for testing filesystem operations.

Compatibility Notes
-------------------
- [Mixing Option and Result via `?` is no longer permitted in
  closures for inferred types.][86831]
- [Previously unsound code is no longer permitted where different
  constructors in branches could require different lifetimes.][85574]
- As previously mentioned the [`std::arch` instrinsics now uses
  stricter const checking][83278] than before and may reject some
  previously accepted code.
- [`i128` multiplication on Cortex M0+ platforms currently
  unconditionally causes overflow when compiled with `codegen-units
  = 1`.][86063]

[85574]: rust-lang/rust#85574
[86831]: rust-lang/rust#86831
[86063]: rust-lang/rust#86063
[86831]: rust-lang/rust#86831
[79608]: rust-lang/rust#79608
[84988]: rust-lang/rust#84988
[84701]: rust-lang/rust#84701
[84072]: rust-lang/rust#84072
[85745]: rust-lang/rust#85745
[84744]: rust-lang/rust#84744
[85078]: rust-lang/rust#85078
[84717]: rust-lang/rust#84717
[83800]: rust-lang/rust#83800
[83366]: rust-lang/rust#83366
[83278]: rust-lang/rust#83278
[85292]: rust-lang/rust#85292
[cargo/9520]: rust-lang/cargo#9520
[cargo/9499]: rust-lang/cargo#9499
[cargo/9488]: rust-lang/cargo#9488
[cargo/9375]: rust-lang/cargo#9375
[`BTreeMap::into_keys`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#method.into_keys
[`BTreeMap::into_values`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#method.into_values
[`HashMap::into_keys`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.into_keys
[`HashMap::into_values`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.into_values
[`arch::wasm32`]: https://doc.rust-lang.org/core/arch/wasm32/index.html
[`VecDeque::binary_search`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html#method.binary_search
[`VecDeque::binary_search_by`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html#method.binary_search_by
[`VecDeque::binary_search_by_key`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html#method.binary_search_by_key
[`VecDeque::partition_point`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html#method.partition_point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.