Skip to content

Commit d69925f

Browse files
committed
Factor out BinDir code into llvm-tools crate
1 parent 3f2c575 commit d69925f

File tree

3 files changed

+32
-78
lines changed

3 files changed

+32
-78
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ version = "0.2.4"
2222
default-features = false
2323
features = ["unicode"]
2424

25+
[build-dependencies]
26+
llvm-tools = "0.1"
27+
2528
[features]
2629
default = []
2730
vga_320x200 = []

build.rs

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ fn main() {
2424
let kernel_out_path = out_dir.join(format!("kernel_bin-{}.o", kernel_file_name));
2525
let kernel_archive_path = out_dir.join(format!("libkernel_bin-{}.a", kernel_file_name));
2626

27+
let llvm_tools = match llvm_tools::LlvmTools::new() {
28+
Ok(tools) => tools,
29+
Err(llvm_tools::Error::NotFound) => {
30+
eprintln!("Error: llvm-tools not found");
31+
eprintln!("Maybe the rustup component `llvm-tools-preview` is missing?");
32+
eprintln!(" Install it through: `rustup component add llvm-tools-preview`");
33+
process::exit(1);
34+
}
35+
Err(err) => {
36+
eprintln!("Failed to retrieve llvm-tools component: {:?}", err);
37+
process::exit(1);
38+
}
39+
};
40+
let objcopy = llvm_tools.tool(&llvm_tools::exe("llvm-objcopy")).expect("llvm-objcopy not found in llvm-tools");
2741

28-
let bin_dir = BinDir::new();
29-
let objcopy = bin_dir.tool(&LlvmTool::tool_name("objcopy")).expect("llvm-objcopy not found in llvm-tools");
30-
31-
let mut cmd = Command::new(objcopy.path());
42+
let mut cmd = Command::new(objcopy);
3243
cmd.arg("-I").arg("binary");
3344
cmd.arg("-O").arg("elf64-x86-64");
3445
cmd.arg("--binary-architecture=i386:x86-64");
@@ -45,8 +56,13 @@ fn main() {
4556
process::exit(1);
4657
}
4758

48-
let ar = bin_dir.tool(&LlvmTool::tool_name("ar")).expect("llvm-ar not found in llvm-tools");
49-
let mut cmd = Command::new(ar.path());
59+
let ar = llvm_tools.tool(&llvm_tools::exe("llvm-ar")).unwrap_or_else(|| {
60+
eprintln!("Failed to retrieve llvm-ar component");
61+
eprint!("This component is available since nightly-XXXX-XX-XX,");
62+
eprintln!("so try updating your toolchain if you're using an older nightly");
63+
process::exit(1);
64+
});
65+
let mut cmd = Command::new(ar);
5066
cmd.arg("crs");
5167
cmd.arg(&kernel_archive_path);
5268
cmd.arg(&kernel_out_path);
@@ -61,75 +77,3 @@ fn main() {
6177
println!("cargo:rustc-link-search=native={}", out_dir.display());
6278
println!("cargo:rustc-link-lib=static=kernel_bin-{}", kernel_file_name);
6379
}
64-
65-
#[derive(Debug)]
66-
struct BinDir {
67-
bin_dir: PathBuf,
68-
}
69-
70-
impl BinDir {
71-
fn new() -> Self {
72-
let example_tool_name = LlvmTool::tool_name("objdump");
73-
let output = Command::new("rustc")
74-
.arg("--print")
75-
.arg("sysroot")
76-
.output()
77-
.expect("failed to print sysroot");
78-
if !output.status.success() {
79-
eprintln!("Failed to execute `rustc --print sysroot`");
80-
eprintln!("Stderr: {}", String::from_utf8(output.stderr).expect("error not valid unicode"));
81-
process::exit(1);
82-
}
83-
84-
let sysroot = PathBuf::from(String::from_utf8(output.stdout).expect("sysroot not valid unicode").trim());
85-
86-
let rustlib = sysroot.join("lib").join("rustlib");
87-
for entry in rustlib.read_dir().expect("read_dir on sysroot dir failed") {
88-
let bin_dir = entry.expect("failed to read sysroot dir entry").path().join("bin");
89-
let tool_path = bin_dir.join(&example_tool_name);
90-
if tool_path.exists() {
91-
return Self { bin_dir };
92-
}
93-
}
94-
95-
eprintln!("Error: llvm-tools not found");
96-
eprintln!("Maybe the rustup component `llvm-tools-preview` is missing?");
97-
eprintln!(" Install it through: `rustup component add llvm-tools-preview`");
98-
process::exit(1);
99-
}
100-
101-
fn tool(&self, tool_name: &str) -> Option<LlvmTool> {
102-
let tool_path = self.bin_dir.join(&tool_name);
103-
104-
if tool_path.exists() {
105-
Some(LlvmTool {
106-
name: tool_name.to_owned(),
107-
path: tool_path,
108-
})
109-
} else {
110-
None
111-
}
112-
}
113-
}
114-
115-
#[derive(Debug)]
116-
struct LlvmTool {
117-
name: String,
118-
path: PathBuf,
119-
}
120-
121-
impl LlvmTool {
122-
fn path(&self) -> &Path {
123-
&self.path
124-
}
125-
126-
#[cfg(target_os = "windows")]
127-
fn tool_name(tool: &str) -> String {
128-
format!("llvm-{}.exe", tool)
129-
}
130-
131-
#[cfg(not(target_os = "windows"))]
132-
fn tool_name(tool: &str) -> String {
133-
format!("llvm-{}", tool)
134-
}
135-
}

0 commit comments

Comments
 (0)