Fix to be able to use a target specification JSON file and document the process

This commit is contained in:
Antoni Boucher 2023-10-24 17:40:45 -04:00
parent 16b3da81f3
commit 0b6e1332b1
3 changed files with 43 additions and 9 deletions

View File

@ -322,7 +322,12 @@ generate it in [gimple.md](./doc/gimple.md).
* Set the path to the cross-compiling libgccjit in `gcc_path`. * Set the path to the cross-compiling libgccjit in `gcc_path`.
* 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`. * 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`.
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target m68k-unknown-linux-gnu`. * Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target m68k-unknown-linux-gnu`.
* 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).
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).
Then, you can use it the following way:
* 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`
* 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`.
If you get the following error: If you get the following error:

View File

@ -1,4 +1,4 @@
use crate::config::set_config; use crate::config::{set_config, ConfigInfo};
use crate::utils::{ use crate::utils::{
get_gcc_path, run_command, run_command_with_output_and_env, walk_dir, get_gcc_path, run_command, run_command_with_output_and_env, walk_dir,
}; };
@ -55,6 +55,15 @@ impl BuildArg {
); );
} }
} }
"--target" => {
if args.next().is_some() {
// Handled in config.rs.
} else {
return Err(
"Expected a value after `--target`, found nothing".to_string()
);
}
}
arg => return Err(format!("Unknown argument `{}`", arg)), arg => return Err(format!("Unknown argument `{}`", arg)),
} }
} }
@ -80,7 +89,7 @@ impl BuildArg {
fn build_sysroot( fn build_sysroot(
env: &mut HashMap<String, String>, env: &mut HashMap<String, String>,
release_mode: bool, release_mode: bool,
target_triple: &str, config: &ConfigInfo,
) -> Result<(), String> { ) -> Result<(), String> {
std::env::set_current_dir("build_sysroot") std::env::set_current_dir("build_sysroot")
.map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?; .map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?;
@ -143,7 +152,7 @@ fn build_sysroot(
&"cargo", &"cargo",
&"build", &"build",
&"--target", &"--target",
&target_triple, &config.target,
&"--release", &"--release",
], ],
None, None,
@ -156,7 +165,7 @@ fn build_sysroot(
&"cargo", &"cargo",
&"build", &"build",
&"--target", &"--target",
&target_triple, &config.target,
], ],
None, None,
Some(env), Some(env),
@ -165,14 +174,14 @@ fn build_sysroot(
}; };
// Copy files to sysroot // Copy files to sysroot
let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", target_triple); let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", config.target_triple);
fs::create_dir_all(&sysroot_path) fs::create_dir_all(&sysroot_path)
.map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?; .map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?;
let copier = |dir_to_copy: &Path| { let copier = |dir_to_copy: &Path| {
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ()) run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
}; };
walk_dir( walk_dir(
&format!("target/{}/{}/deps", target_triple, channel), &format!("target/{}/{}/deps", config.target_triple, channel),
copier, copier,
copier, copier,
)?; )?;
@ -216,7 +225,7 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
build_sysroot( build_sysroot(
&mut env, &mut env,
args.sysroot_release_channel, args.sysroot_release_channel,
&config.target_triple, &config,
)?; )?;
Ok(()) Ok(())
} }

View File

@ -3,6 +3,7 @@ use std::collections::HashMap;
use std::env as std_env; use std::env as std_env;
pub struct ConfigInfo { pub struct ConfigInfo {
pub target: String,
pub target_triple: String, pub target_triple: String,
pub rustc_command: Vec<String>, pub rustc_command: Vec<String>,
} }
@ -30,25 +31,43 @@ pub fn set_config(
let host_triple = get_rustc_host_triple()?; let host_triple = get_rustc_host_triple()?;
let mut linker = None; let mut linker = None;
let mut target_triple = host_triple.clone(); let mut target_triple = host_triple.clone();
let mut target = target_triple.clone();
// We skip binary name and the command. // We skip binary name and the command.
let mut args = std::env::args().skip(2); let mut args = std::env::args().skip(2);
let mut set_target_triple = false;
let mut set_target = false;
while let Some(arg) = args.next() { while let Some(arg) = args.next() {
match arg.as_str() { match arg.as_str() {
"--target-triple" => { "--target-triple" => {
if let Some(arg) = args.next() { if let Some(arg) = args.next() {
target_triple = arg; target_triple = arg;
set_target_triple = true;
} else { } else {
return Err( return Err(
"Expected a value after `--target-triple`, found nothing".to_string() "Expected a value after `--target-triple`, found nothing".to_string()
); );
} }
}, },
"--target" => {
if let Some(arg) = args.next() {
target = arg;
set_target = true;
} else {
return Err(
"Expected a value after `--target`, found nothing".to_string()
);
}
},
_ => (), _ => (),
} }
} }
if set_target_triple && !set_target {
target = target_triple.clone();
}
if host_triple != target_triple { if host_triple != target_triple {
linker = Some(format!("-Clinker={}-gcc", target_triple)); linker = Some(format!("-Clinker={}-gcc", target_triple));
} }
@ -123,7 +142,8 @@ pub fn set_config(
"target/out".to_string(), "target/out".to_string(),
]); ]);
Ok(ConfigInfo { Ok(ConfigInfo {
target_triple: target_triple.to_string(), target,
target_triple,
rustc_command, rustc_command,
}) })
} }