@@ -24,11 +24,22 @@ fn main() {
24
24
let kernel_out_path = out_dir. join ( format ! ( "kernel_bin-{}.o" , kernel_file_name) ) ;
25
25
let kernel_archive_path = out_dir. join ( format ! ( "libkernel_bin-{}.a" , kernel_file_name) ) ;
26
26
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" ) ;
27
41
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) ;
32
43
cmd. arg ( "-I" ) . arg ( "binary" ) ;
33
44
cmd. arg ( "-O" ) . arg ( "elf64-x86-64" ) ;
34
45
cmd. arg ( "--binary-architecture=i386:x86-64" ) ;
@@ -45,8 +56,13 @@ fn main() {
45
56
process:: exit ( 1 ) ;
46
57
}
47
58
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) ;
50
66
cmd. arg ( "crs" ) ;
51
67
cmd. arg ( & kernel_archive_path) ;
52
68
cmd. arg ( & kernel_out_path) ;
@@ -61,75 +77,3 @@ fn main() {
61
77
println ! ( "cargo:rustc-link-search=native={}" , out_dir. display( ) ) ;
62
78
println ! ( "cargo:rustc-link-lib=static=kernel_bin-{}" , kernel_file_name) ;
63
79
}
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