Skip to content

Commit 2db4e50

Browse files
committed
Rewrite build.sh in rust
This makes it easier to compile cg_clif on systems that don't support bash shell scripts like Windows
1 parent 0ddb937 commit 2db4e50

File tree

9 files changed

+372
-91
lines changed

9 files changed

+372
-91
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ perf.data
66
perf.data.old
77
*.events
88
*.string*
9+
/y.bin
910
/build
1011
/build_sysroot/sysroot_src
1112
/build_sysroot/compiler-builtins

.vscode/settings.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949
"cfg": [],
5050
},
5151
]
52+
},
53+
{
54+
"roots": ["./y.rs"],
55+
"crates": [
56+
{
57+
"root_module": "./y.rs",
58+
"edition": "2018",
59+
"deps": [{ "crate": 1, "name": "std" }],
60+
"cfg": [],
61+
},
62+
{
63+
"root_module": "./build_sysroot/sysroot_src/library/std/src/lib.rs",
64+
"edition": "2018",
65+
"deps": [],
66+
"cfg": [],
67+
},
68+
]
5269
}
5370
]
5471
}

build.sh

Lines changed: 0 additions & 88 deletions
This file was deleted.

build_system/build_backend.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::env;
2+
use std::process::{self, Command};
3+
4+
pub(crate) fn build_backend(channel: &str) -> String {
5+
let mut cmd = Command::new("cargo");
6+
cmd.arg("build");
7+
8+
match channel {
9+
"debug" => {}
10+
"release" => {
11+
cmd.arg("--release");
12+
}
13+
_ => unreachable!(),
14+
}
15+
16+
if cfg!(unix) {
17+
if cfg!(target_os = "macos") {
18+
cmd.env(
19+
"RUSTFLAGS",
20+
"-Csplit-debuginfo=unpacked \
21+
-Clink-arg=-Wl,-rpath,@loader_path/../lib \
22+
-Zosx-rpath-install-name"
23+
.to_string()
24+
+ env::var("RUSTFLAGS").as_deref().unwrap_or(""),
25+
);
26+
} else {
27+
cmd.env(
28+
"RUSTFLAGS",
29+
"-Clink-arg=-Wl,-rpath=$ORIGIN/../lib ".to_string()
30+
+ env::var("RUSTFLAGS").as_deref().unwrap_or(""),
31+
);
32+
}
33+
}
34+
35+
eprintln!("[BUILD] rustc_codegen_cranelift");
36+
if !cmd.spawn().unwrap().wait().unwrap().success() {
37+
process::exit(1);
38+
}
39+
40+
crate::rustc_info::get_dylib_name("rustc_codegen_cranelift")
41+
}

build_system/build_sysroot.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use crate::{try_hard_link, SysrootKind};
2+
use std::env;
3+
use std::fs;
4+
use std::path::Path;
5+
use std::process::{self, Command};
6+
7+
pub(crate) fn build_sysroot(
8+
channel: &str,
9+
sysroot_kind: SysrootKind,
10+
target_dir: &Path,
11+
cg_clif_dylib: String,
12+
host_triple: &str,
13+
target_triple: &str,
14+
) {
15+
if target_dir.exists() {
16+
fs::remove_dir_all(target_dir).unwrap();
17+
}
18+
fs::create_dir_all(target_dir.join("bin")).unwrap();
19+
fs::create_dir_all(target_dir.join("lib")).unwrap();
20+
21+
// Copy the backend
22+
for file in ["cg_clif", "cg_clif_build_sysroot"] {
23+
try_hard_link(
24+
Path::new("target").join(channel).join(file),
25+
target_dir.join("bin").join(file),
26+
);
27+
}
28+
29+
try_hard_link(
30+
Path::new("target").join(channel).join(&cg_clif_dylib),
31+
target_dir.join("lib").join(cg_clif_dylib),
32+
);
33+
34+
// Copy supporting files
35+
try_hard_link("rust-toolchain", target_dir.join("rust-toolchain"));
36+
try_hard_link("scripts/config.sh", target_dir.join("config.sh"));
37+
try_hard_link("scripts/cargo.sh", target_dir.join("cargo.sh"));
38+
39+
let default_sysroot = crate::rustc_info::get_default_sysroot();
40+
41+
let rustlib = target_dir.join("lib").join("rustlib");
42+
let host_rustlib_lib = rustlib.join(host_triple).join("lib");
43+
let target_rustlib_lib = rustlib.join(target_triple).join("lib");
44+
fs::create_dir_all(&host_rustlib_lib).unwrap();
45+
fs::create_dir_all(&target_rustlib_lib).unwrap();
46+
47+
if target_triple == "x86_64-pc-windows-gnu" {
48+
if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
49+
eprintln!(
50+
"The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
51+
to compile a sysroot for it.",
52+
);
53+
process::exit(1);
54+
}
55+
for file in fs::read_dir(
56+
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
57+
)
58+
.unwrap()
59+
{
60+
let file = file.unwrap().path();
61+
if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
62+
continue; // only copy object files
63+
}
64+
try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
65+
}
66+
}
67+
68+
match sysroot_kind {
69+
SysrootKind::None => {} // Nothing to do
70+
SysrootKind::Llvm => {
71+
for file in fs::read_dir(
72+
default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
73+
)
74+
.unwrap()
75+
{
76+
let file = file.unwrap().path();
77+
let file_name_str = file.file_name().unwrap().to_str().unwrap();
78+
if file_name_str.contains("rustc_")
79+
|| file_name_str.contains("chalk")
80+
|| file_name_str.contains("tracing")
81+
|| file_name_str.contains("regex")
82+
{
83+
// These are large crates that are part of the rustc-dev component and are not
84+
// necessary to run regular programs.
85+
continue;
86+
}
87+
try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
88+
}
89+
90+
if target_triple != host_triple {
91+
for file in fs::read_dir(
92+
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
93+
)
94+
.unwrap()
95+
{
96+
let file = file.unwrap().path();
97+
try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
98+
}
99+
}
100+
}
101+
SysrootKind::Clif => {
102+
let cwd = env::current_dir().unwrap();
103+
104+
let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
105+
cmd.current_dir(target_dir).env("TARGET_TRIPLE", target_triple);
106+
eprintln!("[BUILD] sysroot");
107+
if !cmd.spawn().unwrap().wait().unwrap().success() {
108+
process::exit(1);
109+
}
110+
111+
if host_triple != target_triple {
112+
let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
113+
cmd.current_dir(target_dir).env("TARGET_TRIPLE", host_triple);
114+
eprintln!("[BUILD] sysroot");
115+
if !cmd.spawn().unwrap().wait().unwrap().success() {
116+
process::exit(1);
117+
}
118+
}
119+
120+
for file in fs::read_dir(host_rustlib_lib).unwrap() {
121+
let file = file.unwrap().path();
122+
if file.file_name().unwrap().to_str().unwrap().contains("std-") {
123+
try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
124+
}
125+
}
126+
}
127+
}
128+
}

build_system/rustc_info.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::path::{Path, PathBuf};
2+
use std::process::{Command, Stdio};
3+
4+
pub(crate) fn get_host_triple() -> String {
5+
let version_info =
6+
Command::new("rustc").stderr(Stdio::inherit()).args(&["-vV"]).output().unwrap().stdout;
7+
String::from_utf8(version_info)
8+
.unwrap()
9+
.lines()
10+
.to_owned()
11+
.find(|line| line.starts_with("host"))
12+
.unwrap()
13+
.split(":")
14+
.nth(1)
15+
.unwrap()
16+
.trim()
17+
.to_owned()
18+
}
19+
20+
pub(crate) fn get_default_sysroot() -> PathBuf {
21+
let default_sysroot = Command::new("rustc")
22+
.stderr(Stdio::inherit())
23+
.args(&["--print", "sysroot"])
24+
.output()
25+
.unwrap()
26+
.stdout;
27+
Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
28+
}
29+
30+
pub(crate) fn get_dylib_name(crate_name: &str) -> String {
31+
let dylib_name = Command::new("rustc")
32+
.stderr(Stdio::inherit())
33+
.args(&["--crate-name", crate_name, "--crate-type", "dylib", "--print", "file-names", "-"])
34+
.output()
35+
.unwrap()
36+
.stdout;
37+
let dylib_name = String::from_utf8(dylib_name).unwrap().trim().to_owned();
38+
assert!(!dylib_name.contains('\n'));
39+
assert!(dylib_name.contains(crate_name));
40+
dylib_name
41+
}

scripts/setup_rust_fork.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
./build.sh
4+
./y.rs build
55
source build/config.sh
66

77
echo "[SETUP] Rust fork"

test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
./build.sh --sysroot none "$@"
4+
./y.rs build --sysroot none "$@"
55

66
rm -r target/out || true
77

88
scripts/tests.sh no_sysroot
99

10-
./build.sh "$@"
10+
./y.rs build "$@"
1111

1212
scripts/tests.sh base_sysroot
1313
scripts/tests.sh extended_sysroot

0 commit comments

Comments
 (0)