about summary refs log tree commit diff
path: root/compiler/rustc_codegen_gcc/build_system/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_codegen_gcc/build_system/src')
-rw-r--r--compiler/rustc_codegen_gcc/build_system/src/build.rs93
-rw-r--r--compiler/rustc_codegen_gcc/build_system/src/config.rs58
-rw-r--r--compiler/rustc_codegen_gcc/build_system/src/main.rs5
-rw-r--r--compiler/rustc_codegen_gcc/build_system/src/prepare.rs38
-rw-r--r--compiler/rustc_codegen_gcc/build_system/src/test.rs15
5 files changed, 148 insertions, 61 deletions
diff --git a/compiler/rustc_codegen_gcc/build_system/src/build.rs b/compiler/rustc_codegen_gcc/build_system/src/build.rs
index e2819c37ad9..eaca7a987d6 100644
--- a/compiler/rustc_codegen_gcc/build_system/src/build.rs
+++ b/compiler/rustc_codegen_gcc/build_system/src/build.rs
@@ -1,6 +1,6 @@
-use crate::config::set_config;
+use crate::config::{set_config, ConfigInfo};
 use crate::utils::{
-    get_gcc_path, run_command, run_command_with_env, run_command_with_output_and_env, walk_dir,
+    get_gcc_path, run_command, run_command_with_output_and_env, walk_dir,
 };
 use std::collections::HashMap;
 use std::ffi::OsStr;
@@ -11,7 +11,8 @@ use std::path::Path;
 struct BuildArg {
     codegen_release_channel: bool,
     sysroot_release_channel: bool,
-    features: Vec<String>,
+    sysroot_panic_abort: bool,
+    flags: Vec<String>,
     gcc_path: String,
 }
 
@@ -30,12 +31,15 @@ impl BuildArg {
                 "--release" => build_arg.codegen_release_channel = true,
                 "--release-sysroot" => build_arg.sysroot_release_channel = true,
                 "--no-default-features" => {
-                    build_arg.features.push("--no-default-features".to_string());
+                    build_arg.flags.push("--no-default-features".to_string());
                 }
+                "--sysroot-panic-abort" => {
+                    build_arg.sysroot_panic_abort = true;
+                },
                 "--features" => {
                     if let Some(arg) = args.next() {
-                        build_arg.features.push("--features".to_string());
-                        build_arg.features.push(arg.as_str().into());
+                        build_arg.flags.push("--features".to_string());
+                        build_arg.flags.push(arg.as_str().into());
                     } else {
                         return Err(
                             "Expected a value after `--features`, found nothing".to_string()
@@ -46,6 +50,24 @@ impl BuildArg {
                     Self::usage();
                     return Ok(None);
                 }
+                "--target-triple" => {
+                    if args.next().is_some() {
+                        // Handled in config.rs.
+                    } else {
+                        return Err(
+                            "Expected a value after `--target-triple`, found nothing".to_string()
+                        );
+                    }
+                }
+                "--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)),
             }
         }
@@ -59,8 +81,10 @@ impl BuildArg {
 
     --release              : Build codegen in release mode
     --release-sysroot      : Build sysroot in release mode
+    --sysroot-panic-abort  : Build the sysroot without unwinding support.
     --no-default-features  : Add `--no-default-features` flag
     --features [arg]       : Add a new feature [arg]
+    --target-triple [arg]  : Set the target triple to [arg]
     --help                 : Show this help
 "#
         )
@@ -69,8 +93,8 @@ impl BuildArg {
 
 fn build_sysroot(
     env: &mut HashMap<String, String>,
-    release_mode: bool,
-    target_triple: &str,
+    args: &BuildArg,
+    config: &ConfigInfo,
 ) -> Result<(), String> {
     std::env::set_current_dir("build_sysroot")
         .map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?;
@@ -119,21 +143,24 @@ fn build_sysroot(
     let _ = fs::remove_dir_all("sysroot");
 
     // Builds libs
-    let channel = if release_mode {
-        let rustflags = env
-            .get("RUSTFLAGS")
-            .cloned()
-            .unwrap_or_default();
-        env.insert(
-            "RUSTFLAGS".to_string(),
-            format!("{} -Zmir-opt-level=3", rustflags),
-        );
+    let mut rustflags = env
+        .get("RUSTFLAGS")
+        .cloned()
+        .unwrap_or_default();
+    if args.sysroot_panic_abort {
+        rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests");
+    }
+    env.insert(
+        "RUSTFLAGS".to_string(),
+        format!("{} -Zmir-opt-level=3", rustflags),
+    );
+    let channel = if args.sysroot_release_channel {
         run_command_with_output_and_env(
             &[
                 &"cargo",
                 &"build",
                 &"--target",
-                &target_triple,
+                &config.target,
                 &"--release",
             ],
             None,
@@ -146,9 +173,7 @@ fn build_sysroot(
                 &"cargo",
                 &"build",
                 &"--target",
-                &target_triple,
-                &"--features",
-                &"compiler_builtins/c",
+                &config.target,
             ],
             None,
             Some(env),
@@ -157,14 +182,14 @@ fn build_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)
         .map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?;
     let copier = |dir_to_copy: &Path| {
         run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
     };
     walk_dir(
-        &format!("target/{}/{}/deps", target_triple, channel),
+        &format!("target/{}/{}/deps", config.target_triple, channel),
         copier,
         copier,
     )?;
@@ -175,16 +200,6 @@ fn build_sysroot(
 fn build_codegen(args: &BuildArg) -> Result<(), String> {
     let mut env = HashMap::new();
 
-    let current_dir =
-        std::env::current_dir().map_err(|error| format!("`current_dir` failed: {:?}", error))?;
-    if let Ok(rt_root) = std::env::var("RUST_COMPILER_RT_ROOT") {
-        env.insert("RUST_COMPILER_RT_ROOT".to_string(), rt_root);
-    } else {
-        env.insert(
-            "RUST_COMPILER_RT_ROOT".to_string(),
-            format!("{}", current_dir.join("llvm/compiler-rt").display()),
-        );
-    }
     env.insert("LD_LIBRARY_PATH".to_string(), args.gcc_path.clone());
     env.insert("LIBRARY_PATH".to_string(), args.gcc_path.clone());
 
@@ -196,11 +211,11 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
     } else {
         env.insert("CHANNEL".to_string(), "debug".to_string());
     }
-    let ref_features = args.features.iter().map(|s| s.as_str()).collect::<Vec<_>>();
-    for feature in &ref_features {
-        command.push(feature);
+    let flags = args.flags.iter().map(|s| s.as_str()).collect::<Vec<_>>();
+    for flag in &flags {
+        command.push(flag);
     }
-    run_command_with_env(&command, None, Some(&env))?;
+    run_command_with_output_and_env(&command, None, Some(&env))?;
 
     let config = set_config(&mut env, &[], Some(&args.gcc_path))?;
 
@@ -217,8 +232,8 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
     println!("[BUILD] sysroot");
     build_sysroot(
         &mut env,
-        args.sysroot_release_channel,
-        &config.target_triple,
+        args,
+        &config,
     )?;
     Ok(())
 }
diff --git a/compiler/rustc_codegen_gcc/build_system/src/config.rs b/compiler/rustc_codegen_gcc/build_system/src/config.rs
index 4f2e33f0f99..64d9bd73e01 100644
--- a/compiler/rustc_codegen_gcc/build_system/src/config.rs
+++ b/compiler/rustc_codegen_gcc/build_system/src/config.rs
@@ -3,9 +3,9 @@ use std::collections::HashMap;
 use std::env as std_env;
 
 pub struct ConfigInfo {
+    pub target: String,
     pub target_triple: String,
     pub rustc_command: Vec<String>,
-    pub run_wrapper: Option<&'static str>,
 }
 
 // Returns the beginning for the command line of rustc.
@@ -30,23 +30,47 @@ pub fn set_config(
     };
     let host_triple = get_rustc_host_triple()?;
     let mut linker = None;
-    let mut target_triple = host_triple.as_str();
-    let mut run_wrapper = None;
-    // FIXME: handle this with a command line flag?
-    // let mut target_triple = "m68k-unknown-linux-gnu";
+    let mut target_triple = host_triple.clone();
+    let mut target = target_triple.clone();
 
-    if host_triple != target_triple {
-        if target_triple == "m68k-unknown-linux-gnu" {
-            target_triple = "mips-unknown-linux-gnu";
-            linker = Some("-Clinker=m68k-linux-gcc");
-        } else if target_triple == "aarch64-unknown-linux-gnu" {
-            // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
-            linker = Some("-Clinker=aarch64-linux-gnu-gcc");
-            run_wrapper = Some("qemu-aarch64 -L /usr/aarch64-linux-gnu");
-        } else {
-            return Err(format!("unknown non-native platform `{}`", target_triple));
+    // We skip binary name and the command.
+    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() {
+        match arg.as_str() {
+            "--target-triple" => {
+                if let Some(arg) = args.next() {
+                    target_triple = arg;
+                    set_target_triple = true;
+                } else {
+                    return Err(
+                        "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 {
+        linker = Some(format!("-Clinker={}-gcc", target_triple));
+    }
     let current_dir =
         std_env::current_dir().map_err(|error| format!("`current_dir` failed: {:?}", error))?;
     let channel = if let Some(channel) = env.get("CHANNEL") {
@@ -118,8 +142,8 @@ pub fn set_config(
         "target/out".to_string(),
     ]);
     Ok(ConfigInfo {
-        target_triple: target_triple.to_string(),
+        target,
+        target_triple,
         rustc_command,
-        run_wrapper,
     })
 }
diff --git a/compiler/rustc_codegen_gcc/build_system/src/main.rs b/compiler/rustc_codegen_gcc/build_system/src/main.rs
index 332a14ff0a2..bff82b6e3e5 100644
--- a/compiler/rustc_codegen_gcc/build_system/src/main.rs
+++ b/compiler/rustc_codegen_gcc/build_system/src/main.rs
@@ -5,6 +5,7 @@ mod build;
 mod config;
 mod prepare;
 mod rustc_info;
+mod test;
 mod utils;
 
 macro_rules! arg_error {
@@ -23,6 +24,7 @@ Available commands for build_system:
 
     prepare  : Run prepare command
     build    : Run build command
+    test     : Run test command
     --help   : Show this message"
     );
 }
@@ -30,6 +32,7 @@ Available commands for build_system:
 pub enum Command {
     Prepare,
     Build,
+    Test,
 }
 
 fn main() {
@@ -40,6 +43,7 @@ fn main() {
     let command = match env::args().nth(1).as_deref() {
         Some("prepare") => Command::Prepare,
         Some("build") => Command::Build,
+        Some("test") => Command::Test,
         Some("--help") => {
             usage();
             process::exit(0);
@@ -55,6 +59,7 @@ fn main() {
     if let Err(e) = match command {
         Command::Prepare => prepare::run(),
         Command::Build => build::run(),
+        Command::Test => test::run(),
     } {
         eprintln!("Command failed to run: {e:?}");
         process::exit(1);
diff --git a/compiler/rustc_codegen_gcc/build_system/src/prepare.rs b/compiler/rustc_codegen_gcc/build_system/src/prepare.rs
index b258ddf3664..6c7c8586834 100644
--- a/compiler/rustc_codegen_gcc/build_system/src/prepare.rs
+++ b/compiler/rustc_codegen_gcc/build_system/src/prepare.rs
@@ -4,7 +4,7 @@ use crate::utils::{cargo_install, git_clone, run_command, run_command_with_outpu
 use std::fs;
 use std::path::Path;
 
-fn prepare_libcore(sysroot_path: &Path) -> Result<(), String> {
+fn prepare_libcore(sysroot_path: &Path, libgccjit12_patches: bool, cross_compile: bool) -> Result<(), String> {
     let rustc_path = match get_rustc_path() {
         Some(path) => path,
         None => return Err("`rustc` path not found".to_string()),
@@ -87,6 +87,22 @@ fn prepare_libcore(sysroot_path: &Path) -> Result<(), String> {
             Ok(())
         },
     )?;
+    if cross_compile {
+        walk_dir("cross_patches", |_| Ok(()), |file_path: &Path| {
+            patches.push(file_path.to_path_buf());
+            Ok(())
+        })?;
+    }
+    if libgccjit12_patches {
+        walk_dir(
+            "patches/libgccjit12",
+            |_| Ok(()),
+            |file_path: &Path| {
+                patches.push(file_path.to_path_buf());
+                Ok(())
+            },
+        )?;
+    }
     patches.sort();
     for file_path in patches {
         println!("[GIT] apply `{}`", file_path.display());
@@ -156,16 +172,22 @@ where
 }
 
 struct PrepareArg {
+    cross_compile: bool,
     only_libcore: bool,
+    libgccjit12_patches: bool,
 }
 
 impl PrepareArg {
     fn new() -> Result<Option<Self>, String> {
         let mut only_libcore = false;
+        let mut cross_compile = false;
+        let mut libgccjit12_patches = false;
 
         for arg in std::env::args().skip(2) {
             match arg.as_str() {
                 "--only-libcore" => only_libcore = true,
+                "--cross" => cross_compile = true,
+                "--libgccjit12-patches" => libgccjit12_patches = true,
                 "--help" => {
                     Self::usage();
                     return Ok(None);
@@ -173,7 +195,11 @@ impl PrepareArg {
                 a => return Err(format!("Unknown argument `{a}`")),
             }
         }
-        Ok(Some(Self { only_libcore }))
+        Ok(Some(Self {
+            cross_compile,
+            only_libcore,
+            libgccjit12_patches,
+        }))
     }
 
     fn usage() {
@@ -181,8 +207,10 @@ impl PrepareArg {
             r#"
 `prepare` command help:
 
-    --only-libcore  : Only setup libcore and don't clone other repositories
-    --help          : Show this help
+    --only-libcore           : Only setup libcore and don't clone other repositories
+    --cross                  : Apply the patches needed to do cross-compilation
+    --libgccjit12-patches    : Apply patches needed for libgccjit12
+    --help                   : Show this help
 "#
         )
     }
@@ -194,7 +222,7 @@ pub fn run() -> Result<(), String> {
         None => return Ok(()),
     };
     let sysroot_path = Path::new("build_sysroot");
-    prepare_libcore(sysroot_path)?;
+    prepare_libcore(sysroot_path, args.libgccjit12_patches, args.cross_compile)?;
 
     if !args.only_libcore {
         cargo_install("hyperfine")?;
diff --git a/compiler/rustc_codegen_gcc/build_system/src/test.rs b/compiler/rustc_codegen_gcc/build_system/src/test.rs
new file mode 100644
index 00000000000..4c8c63e59ab
--- /dev/null
+++ b/compiler/rustc_codegen_gcc/build_system/src/test.rs
@@ -0,0 +1,15 @@
+use crate::utils::run_command_with_output;
+
+fn get_args<'a>(args: &mut Vec<&'a dyn AsRef<std::ffi::OsStr>>, extra_args: &'a Vec<String>) {
+    for extra_arg in extra_args {
+        args.push(extra_arg);
+    }
+}
+
+pub fn run() -> Result<(), String> {
+    let mut args: Vec<&dyn AsRef<std::ffi::OsStr>> = vec![&"bash", &"test.sh"];
+    let extra_args = std::env::args().skip(2).collect::<Vec<_>>();
+    get_args(&mut args, &extra_args);
+    let current_dir = std::env::current_dir().map_err(|error| format!("`current_dir` failed: {:?}", error))?;
+    run_command_with_output(args.as_slice(), Some(&current_dir))
+}