1
1
use std:: ffi:: OsStr ;
2
2
use std:: ffi:: OsString ;
3
3
use std:: fs;
4
+ use std:: path:: Path ;
5
+ use std:: path:: PathBuf ;
4
6
use std:: process:: Command ;
5
7
8
+ use crate :: rustc_info:: get_rustc_path;
9
+ use crate :: utils:: copy_dir_recursively;
6
10
use crate :: utils:: spawn_and_wait;
7
11
8
12
pub ( crate ) fn prepare ( ) {
9
- // FIXME implement in rust
10
- let prepare_sysroot_cmd = Command :: new ( "./build_sysroot/prepare_sysroot_src.sh" ) ;
11
- spawn_and_wait ( prepare_sysroot_cmd) ;
13
+ prepare_sysroot ( ) ;
12
14
13
15
eprintln ! ( "[INSTALL] hyperfine" ) ;
14
16
Command :: new ( "cargo" ) . arg ( "install" ) . arg ( "hyperfine" ) . spawn ( ) . unwrap ( ) . wait ( ) . unwrap ( ) ;
@@ -18,15 +20,7 @@ pub(crate) fn prepare() {
18
20
"https://github.com/rust-random/rand.git" ,
19
21
"0f933f9c7176e53b2a3c7952ded484e1783f0bf1" ,
20
22
) ;
21
-
22
- eprintln ! ( "[PATCH] rand" ) ;
23
- for patch in get_patches ( "crate_patches" , "rand" ) {
24
- let mut patch_arg = OsString :: from ( "../crate_patches/" ) ;
25
- patch_arg. push ( patch) ;
26
- let mut apply_patch_cmd = Command :: new ( "git" ) ;
27
- apply_patch_cmd. arg ( "am" ) . arg ( patch_arg) . current_dir ( "rand" ) ;
28
- spawn_and_wait ( apply_patch_cmd) ;
29
- }
23
+ apply_patches ( "crate_patches" , "rand" , Path :: new ( "rand" ) ) ;
30
24
31
25
clone_repo (
32
26
"regex" ,
@@ -47,17 +41,64 @@ pub(crate) fn prepare() {
47
41
fs:: copy ( "simple-raytracer/target/debug/main" , "simple-raytracer/raytracer_cg_llvm" ) . unwrap ( ) ;
48
42
}
49
43
50
- fn clone_repo ( name : & str , repo : & str , commit : & str ) {
44
+ fn prepare_sysroot ( ) {
45
+ let rustc_path = get_rustc_path ( ) ;
46
+ let sysroot_src_orig = rustc_path. parent ( ) . unwrap ( ) . join ( "../lib/rustlib/src/rust" ) ;
47
+ let sysroot_src = PathBuf :: from ( "build_sysroot" ) . canonicalize ( ) . unwrap ( ) . join ( "sysroot_src" ) ;
48
+
49
+ assert ! ( sysroot_src_orig. exists( ) ) ;
50
+
51
+ if sysroot_src. exists ( ) {
52
+ fs:: remove_dir_all ( & sysroot_src) . unwrap ( ) ;
53
+ }
54
+ fs:: create_dir_all ( sysroot_src. join ( "library" ) ) . unwrap ( ) ;
55
+ eprintln ! ( "[COPY] sysroot src" ) ;
56
+ copy_dir_recursively ( & sysroot_src_orig. join ( "library" ) , & sysroot_src. join ( "library" ) ) ;
57
+
58
+ eprintln ! ( "[GIT] init" ) ;
59
+ let mut git_init_cmd = Command :: new ( "git" ) ;
60
+ git_init_cmd. arg ( "init" ) . arg ( "-q" ) . current_dir ( & sysroot_src) ;
61
+ spawn_and_wait ( git_init_cmd) ;
62
+
63
+ let mut git_add_cmd = Command :: new ( "git" ) ;
64
+ git_add_cmd. arg ( "add" ) . arg ( "." ) . current_dir ( & sysroot_src) ;
65
+ spawn_and_wait ( git_add_cmd) ;
66
+
67
+ let mut git_commit_cmd = Command :: new ( "git" ) ;
68
+ git_commit_cmd
69
+ . arg ( "commit" )
70
+ . arg ( "-m" )
71
+ . arg ( "Initial commit" )
72
+ . arg ( "-q" )
73
+ . current_dir ( & sysroot_src) ;
74
+ spawn_and_wait ( git_commit_cmd) ;
75
+
76
+ apply_patches ( "patches" , "sysroot" , & sysroot_src) ;
77
+
78
+ clone_repo (
79
+ "build_sysroot/compiler-builtins" ,
80
+ "https://github.com/rust-lang/compiler-builtins.git" ,
81
+ "0.1.45" ,
82
+ ) ;
83
+
84
+ apply_patches (
85
+ "crate_patches" ,
86
+ "compiler-builtins" ,
87
+ Path :: new ( "build_sysroot/compiler-builtins" ) ,
88
+ ) ;
89
+ }
90
+
91
+ fn clone_repo ( target_dir : & str , repo : & str , rev : & str ) {
51
92
eprintln ! ( "[CLONE] {}" , repo) ;
52
93
// Ignore exit code as the repo may already have been checked out
53
- Command :: new ( "git" ) . arg ( "clone" ) . arg ( repo) . spawn ( ) . unwrap ( ) . wait ( ) . unwrap ( ) ;
94
+ Command :: new ( "git" ) . arg ( "clone" ) . arg ( repo) . arg ( target_dir ) . spawn ( ) . unwrap ( ) . wait ( ) . unwrap ( ) ;
54
95
55
96
let mut clean_cmd = Command :: new ( "git" ) ;
56
- clean_cmd. arg ( "checkout" ) . arg ( "--" ) . arg ( "." ) . current_dir ( name ) ;
97
+ clean_cmd. arg ( "checkout" ) . arg ( "--" ) . arg ( "." ) . current_dir ( target_dir ) ;
57
98
spawn_and_wait ( clean_cmd) ;
58
99
59
100
let mut checkout_cmd = Command :: new ( "git" ) ;
60
- checkout_cmd. arg ( "checkout" ) . arg ( commit ) . current_dir ( name ) ;
101
+ checkout_cmd. arg ( "checkout" ) . arg ( "-q" ) . arg ( rev ) . current_dir ( target_dir ) ;
61
102
spawn_and_wait ( checkout_cmd) ;
62
103
}
63
104
@@ -67,8 +108,20 @@ fn get_patches(patch_dir: &str, crate_name: &str) -> Vec<OsString> {
67
108
. map ( |entry| entry. unwrap ( ) . path ( ) )
68
109
. filter ( |path| path. extension ( ) == Some ( OsStr :: new ( "patch" ) ) )
69
110
. map ( |path| path. file_name ( ) . unwrap ( ) . to_owned ( ) )
70
- . filter ( |file_name| file_name. to_str ( ) . unwrap ( ) . split ( "-" ) . nth ( 1 ) . unwrap ( ) == crate_name)
111
+ . filter ( |file_name| {
112
+ file_name. to_str ( ) . unwrap ( ) . split_once ( "-" ) . unwrap ( ) . 1 . starts_with ( crate_name)
113
+ } )
71
114
. collect ( ) ;
72
115
patches. sort ( ) ;
73
116
patches
74
117
}
118
+
119
+ fn apply_patches ( patch_dir : & str , crate_name : & str , target_dir : & Path ) {
120
+ for patch in get_patches ( patch_dir, crate_name) {
121
+ eprintln ! ( "[PATCH] {:?} <- {:?}" , target_dir. file_name( ) . unwrap( ) , patch) ;
122
+ let patch_arg = Path :: new ( patch_dir) . join ( patch) . canonicalize ( ) . unwrap ( ) ;
123
+ let mut apply_patch_cmd = Command :: new ( "git" ) ;
124
+ apply_patch_cmd. arg ( "am" ) . arg ( patch_arg) . arg ( "-q" ) . current_dir ( target_dir) ;
125
+ spawn_and_wait ( apply_patch_cmd) ;
126
+ }
127
+ }
0 commit comments