Skip to content

Commit 46887f2

Browse files
authored
Merge pull request #364 from rust-lang/doc/target-spec
Fix to be able to use a target specification JSON file and document the process
2 parents 16b3da8 + 0b6e133 commit 46887f2

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

Diff for: Readme.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,12 @@ generate it in [gimple.md](./doc/gimple.md).
322322
* Set the path to the cross-compiling libgccjit in `gcc_path`.
323323
* Make sure you have the linker for your target (for instance `m68k-unknown-linux-gnu-gcc`) in your `$PATH`. Currently, the linker name is hardcoded as being `$TARGET-gcc`. Specify the target when building the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu`.
324324
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target m68k-unknown-linux-gnu`.
325-
* If the target is not yet supported by the Rust compiler, create a [target specification file](https://docs.rust-embedded.org/embedonomicon/custom-target.html) (note that the `arch` specified in this file must be supported by the rust compiler).
325+
326+
If the target is not yet supported by the Rust compiler, create a [target specification file](https://docs.rust-embedded.org/embedonomicon/custom-target.html) (note that the `arch` specified in this file must be supported by the rust compiler).
327+
Then, you can use it the following way:
328+
329+
* Add the target specification file using `--target` as an **absolute** path to build the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu --target $(pwd)/m68k-unknown-linux-gnu.json`
330+
* Build your project by specifying the target specification file: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target path/to/m68k-unknown-linux-gnu.json`.
326331

327332
If you get the following error:
328333

Diff for: build_system/src/build.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::set_config;
1+
use crate::config::{set_config, ConfigInfo};
22
use crate::utils::{
33
get_gcc_path, run_command, run_command_with_output_and_env, walk_dir,
44
};
@@ -55,6 +55,15 @@ impl BuildArg {
5555
);
5656
}
5757
}
58+
"--target" => {
59+
if args.next().is_some() {
60+
// Handled in config.rs.
61+
} else {
62+
return Err(
63+
"Expected a value after `--target`, found nothing".to_string()
64+
);
65+
}
66+
}
5867
arg => return Err(format!("Unknown argument `{}`", arg)),
5968
}
6069
}
@@ -80,7 +89,7 @@ impl BuildArg {
8089
fn build_sysroot(
8190
env: &mut HashMap<String, String>,
8291
release_mode: bool,
83-
target_triple: &str,
92+
config: &ConfigInfo,
8493
) -> Result<(), String> {
8594
std::env::set_current_dir("build_sysroot")
8695
.map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?;
@@ -143,7 +152,7 @@ fn build_sysroot(
143152
&"cargo",
144153
&"build",
145154
&"--target",
146-
&target_triple,
155+
&config.target,
147156
&"--release",
148157
],
149158
None,
@@ -156,7 +165,7 @@ fn build_sysroot(
156165
&"cargo",
157166
&"build",
158167
&"--target",
159-
&target_triple,
168+
&config.target,
160169
],
161170
None,
162171
Some(env),
@@ -165,14 +174,14 @@ fn build_sysroot(
165174
};
166175

167176
// Copy files to sysroot
168-
let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", target_triple);
177+
let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", config.target_triple);
169178
fs::create_dir_all(&sysroot_path)
170179
.map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?;
171180
let copier = |dir_to_copy: &Path| {
172181
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
173182
};
174183
walk_dir(
175-
&format!("target/{}/{}/deps", target_triple, channel),
184+
&format!("target/{}/{}/deps", config.target_triple, channel),
176185
copier,
177186
copier,
178187
)?;
@@ -216,7 +225,7 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
216225
build_sysroot(
217226
&mut env,
218227
args.sysroot_release_channel,
219-
&config.target_triple,
228+
&config,
220229
)?;
221230
Ok(())
222231
}

Diff for: build_system/src/config.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashMap;
33
use std::env as std_env;
44

55
pub struct ConfigInfo {
6+
pub target: String,
67
pub target_triple: String,
78
pub rustc_command: Vec<String>,
89
}
@@ -30,25 +31,43 @@ pub fn set_config(
3031
let host_triple = get_rustc_host_triple()?;
3132
let mut linker = None;
3233
let mut target_triple = host_triple.clone();
34+
let mut target = target_triple.clone();
3335

3436
// We skip binary name and the command.
3537
let mut args = std::env::args().skip(2);
3638

39+
let mut set_target_triple = false;
40+
let mut set_target = false;
3741
while let Some(arg) = args.next() {
3842
match arg.as_str() {
3943
"--target-triple" => {
4044
if let Some(arg) = args.next() {
4145
target_triple = arg;
46+
set_target_triple = true;
4247
} else {
4348
return Err(
4449
"Expected a value after `--target-triple`, found nothing".to_string()
4550
);
4651
}
4752
},
53+
"--target" => {
54+
if let Some(arg) = args.next() {
55+
target = arg;
56+
set_target = true;
57+
} else {
58+
return Err(
59+
"Expected a value after `--target`, found nothing".to_string()
60+
);
61+
}
62+
},
4863
_ => (),
4964
}
5065
}
5166

67+
if set_target_triple && !set_target {
68+
target = target_triple.clone();
69+
}
70+
5271
if host_triple != target_triple {
5372
linker = Some(format!("-Clinker={}-gcc", target_triple));
5473
}
@@ -123,7 +142,8 @@ pub fn set_config(
123142
"target/out".to_string(),
124143
]);
125144
Ok(ConfigInfo {
126-
target_triple: target_triple.to_string(),
145+
target,
146+
target_triple,
127147
rustc_command,
128148
})
129149
}

0 commit comments

Comments
 (0)