Skip to content

Commit 206e3fe

Browse files
authored
Implement host-target substitution (#15838)
### What does this PR try to resolve? This PR resolves: #13051 Namely, it allows users to invoke cargo subcommands that accept a `--target` directive to specify the `host` target, which is later substituted in the command processing layer by the host's real target triple (for instance, on most Linux distributions, `cargo build --target host` would effectively run `cargo build --target x86_64-unknown-linux-gnu`). This additionally applies to usage within `config.toml`, like so: ```toml # .cargo/config.toml [build] target = "host" ```
2 parents 71eb84f + d5509eb commit 206e3fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+731
-221
lines changed

src/cargo/core/compiler/compile_kind.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,27 @@ impl CompileKind {
8484
fallback: CompileKindFallback,
8585
) -> CargoResult<Vec<CompileKind>> {
8686
let dedup = |targets: &[String]| {
87-
Ok(targets
87+
let deduplicated_targets = targets
8888
.iter()
89-
.map(|value| Ok(CompileKind::Target(CompileTarget::new(value)?)))
89+
.map(|value| {
90+
// This neatly substitutes the manually-specified `host` target directive
91+
// with the compiling machine's target triple.
92+
93+
if value.as_str() == "host" {
94+
let host_triple = env!("RUST_HOST_TARGET");
95+
Ok(CompileKind::Target(CompileTarget::new(host_triple)?))
96+
} else {
97+
Ok(CompileKind::Target(CompileTarget::new(value.as_str())?))
98+
}
99+
})
90100
// First collect into a set to deduplicate any `--target` passed
91101
// more than once...
92102
.collect::<CargoResult<BTreeSet<_>>>()?
93103
// ... then generate a flat list for everything else to use.
94104
.into_iter()
95-
.collect())
105+
.collect();
106+
107+
Ok(deduplicated_targets)
96108
};
97109

98110
if !targets.is_empty() {

src/cargo/util/command_prelude.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,11 @@ fn get_target_triples() -> Vec<clap_complete::CompletionCandidate> {
12571257
}
12581258
}
12591259

1260+
// Allow tab-completion for `host` as the desired target.
1261+
candidates.push(clap_complete::CompletionCandidate::new("host").help(Some(
1262+
concat!("alias for: ", env!("RUST_HOST_TARGET")).into(),
1263+
)));
1264+
12601265
candidates
12611266
}
12621267

src/doc/man/generated_txt/cargo-bench.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,24 @@ OPTIONS
220220

221221
Compilation Options
222222
--target triple
223-
Benchmark for the given architecture. The default is the host
224-
architecture. The general format of the triple is
225-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
226-
a list of supported targets. This flag may be specified multiple
227-
times.
223+
Benchmark for the specified target architecture. Flag may be
224+
specified multiple times. The default is the host architecture. The
225+
general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
226+
227+
Possible values:
228+
229+
o Any supported target in rustc --print target-list.
230+
231+
o "host", which will internally be substituted by the host’s
232+
target. This can be particularly useful if you’re
233+
cross-compiling some crates, and don’t want to specify your
234+
host’s machine as a target (for instance, an xtask in a shared
235+
project that may be worked on by many hosts).
236+
237+
o A path to a custom target specification. See Custom Target Lookup
238+
Path
239+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
240+
for more information.
228241

229242
This may also be specified with the build.target config value
230243
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-build.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,24 @@ OPTIONS
137137

138138
Compilation Options
139139
--target triple
140-
Build for the given architecture. The default is the host
141-
architecture. The general format of the triple is
142-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
143-
a list of supported targets. This flag may be specified multiple
144-
times.
140+
Build for the specified target architecture. Flag may be specified
141+
multiple times. The default is the host architecture. The general
142+
format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
143+
144+
Possible values:
145+
146+
o Any supported target in rustc --print target-list.
147+
148+
o "host", which will internally be substituted by the host’s
149+
target. This can be particularly useful if you’re
150+
cross-compiling some crates, and don’t want to specify your
151+
host’s machine as a target (for instance, an xtask in a shared
152+
project that may be worked on by many hosts).
153+
154+
o A path to a custom target specification. See Custom Target Lookup
155+
Path
156+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
157+
for more information.
145158

146159
This may also be specified with the build.target config value
147160
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-check.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,24 @@ OPTIONS
134134

135135
Compilation Options
136136
--target triple
137-
Check for the given architecture. The default is the host
138-
architecture. The general format of the triple is
139-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
140-
a list of supported targets. This flag may be specified multiple
141-
times.
137+
Check for the specified target architecture. Flag may be specified
138+
multiple times. The default is the host architecture. The general
139+
format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
140+
141+
Possible values:
142+
143+
o Any supported target in rustc --print target-list.
144+
145+
o "host", which will internally be substituted by the host’s
146+
target. This can be particularly useful if you’re
147+
cross-compiling some crates, and don’t want to specify your
148+
host’s machine as a target (for instance, an xtask in a shared
149+
project that may be worked on by many hosts).
150+
151+
o A path to a custom target specification. See Custom Target Lookup
152+
Path
153+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
154+
for more information.
142155

143156
This may also be specified with the build.target config value
144157
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-clean.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,24 @@ OPTIONS
4545
target in the root of the workspace.
4646

4747
--target triple
48-
Clean for the given architecture. The default is the host
49-
architecture. The general format of the triple is
50-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
51-
a list of supported targets. This flag may be specified multiple
52-
times.
48+
Clean for the specified target architecture. Flag may be specified
49+
multiple times. The default is the host architecture. The general
50+
format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
51+
52+
Possible values:
53+
54+
o Any supported target in rustc --print target-list.
55+
56+
o "host", which will internally be substituted by the host’s
57+
target. This can be particularly useful if you’re
58+
cross-compiling some crates, and don’t want to specify your
59+
host’s machine as a target (for instance, an xtask in a shared
60+
project that may be worked on by many hosts).
61+
62+
o A path to a custom target specification. See Custom Target Lookup
63+
Path
64+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
65+
for more information.
5366

5467
This may also be specified with the build.target config value
5568
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-doc.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,24 @@ OPTIONS
116116

117117
Compilation Options
118118
--target triple
119-
Document for the given architecture. The default is the host
120-
architecture. The general format of the triple is
121-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
122-
a list of supported targets. This flag may be specified multiple
123-
times.
119+
Document for the specified target architecture. Flag may be
120+
specified multiple times. The default is the host architecture. The
121+
general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
122+
123+
Possible values:
124+
125+
o Any supported target in rustc --print target-list.
126+
127+
o "host", which will internally be substituted by the host’s
128+
target. This can be particularly useful if you’re
129+
cross-compiling some crates, and don’t want to specify your
130+
host’s machine as a target (for instance, an xtask in a shared
131+
project that may be worked on by many hosts).
132+
133+
o A path to a custom target specification. See Custom Target Lookup
134+
Path
135+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
136+
for more information.
124137

125138
This may also be specified with the build.target config value
126139
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-fetch.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,24 @@ DESCRIPTION
2525
OPTIONS
2626
Fetch options
2727
--target triple
28-
Fetch for the given architecture. The default is all architectures.
29-
The general format of the triple is
30-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
31-
a list of supported targets. This flag may be specified multiple
32-
times.
28+
Fetch for the specified target architecture. Flag may be specified
29+
multiple times. The default is all architectures. The general format
30+
of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
31+
32+
Possible values:
33+
34+
o Any supported target in rustc --print target-list.
35+
36+
o "host", which will internally be substituted by the host’s
37+
target. This can be particularly useful if you’re
38+
cross-compiling some crates, and don’t want to specify your
39+
host’s machine as a target (for instance, an xtask in a shared
40+
project that may be worked on by many hosts).
41+
42+
o A path to a custom target specification. See Custom Target Lookup
43+
Path
44+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
45+
for more information.
3346

3447
This may also be specified with the build.target config value
3548
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-fix.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,24 @@ OPTIONS
208208

209209
Compilation Options
210210
--target triple
211-
Fix for the given architecture. The default is the host
212-
architecture. The general format of the triple is
213-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
214-
a list of supported targets. This flag may be specified multiple
215-
times.
211+
Fix for the specified target architecture. Flag may be specified
212+
multiple times. The default is the host architecture. The general
213+
format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.
214+
215+
Possible values:
216+
217+
o Any supported target in rustc --print target-list.
218+
219+
o "host", which will internally be substituted by the host’s
220+
target. This can be particularly useful if you’re
221+
cross-compiling some crates, and don’t want to specify your
222+
host’s machine as a target (for instance, an xtask in a shared
223+
project that may be worked on by many hosts).
224+
225+
o A path to a custom target specification. See Custom Target Lookup
226+
Path
227+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
228+
for more information.
216229

217230
This may also be specified with the build.target config value
218231
<https://doc.rust-lang.org/cargo/reference/config.html>.

src/doc/man/generated_txt/cargo-install.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,24 @@ OPTIONS
186186

187187
Compilation Options
188188
--target triple
189-
Install for the given architecture. The default is the host
190-
architecture. The general format of the triple is
191-
<arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
192-
a list of supported targets.
189+
Install for the specified target architecture. The default is the
190+
host architecture. The general format of the triple is
191+
<arch><sub>-<vendor>-<sys>-<abi>.
192+
193+
Possible values:
194+
195+
o Any supported target in rustc --print target-list.
196+
197+
o "host", which will internally be substituted by the host’s
198+
target. This can be particularly useful if you’re
199+
cross-compiling some crates, and don’t want to specify your
200+
host’s machine as a target (for instance, an xtask in a shared
201+
project that may be worked on by many hosts).
202+
203+
o A path to a custom target specification. See Custom Target Lookup
204+
Path
205+
<https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path>
206+
for more information.
193207

194208
This may also be specified with the build.target config value
195209
<https://doc.rust-lang.org/cargo/reference/config.html>.

0 commit comments

Comments
 (0)