@@ -7,56 +7,61 @@ use std::process::Command;
77use super :: rustc_info:: { get_file_name, get_rustc_path, get_rustc_version} ;
88use super :: utils:: { cargo_command, copy_dir_recursively, spawn_and_wait} ;
99
10+ pub ( crate ) const ABI_CAFE : GitRepo = GitRepo :: github (
11+ "Gankra" ,
12+ "abi-cafe" ,
13+ "4c6dc8c9c687e2b3a760ff2176ce236872b37212" ,
14+ "abi-cafe" ,
15+ ) ;
16+
17+ pub ( crate ) const RAND : GitRepo =
18+ GitRepo :: github ( "rust-random" , "rand" , "0f933f9c7176e53b2a3c7952ded484e1783f0bf1" , "rand" ) ;
19+
20+ pub ( crate ) const REGEX : GitRepo =
21+ GitRepo :: github ( "rust-lang" , "regex" , "341f207c1071f7290e3f228c710817c280c8dca1" , "regex" ) ;
22+
23+ pub ( crate ) const PORTABLE_SIMD : GitRepo = GitRepo :: github (
24+ "rust-lang" ,
25+ "portable-simd" ,
26+ "d5cd4a8112d958bd3a252327e0d069a6363249bd" ,
27+ "portable-simd" ,
28+ ) ;
29+
30+ pub ( crate ) const SIMPLE_RAYTRACER : GitRepo = GitRepo :: github (
31+ "ebobby" ,
32+ "simple-raytracer" ,
33+ "804a7a21b9e673a482797aa289a18ed480e4d813" ,
34+ "<none>" ,
35+ ) ;
36+
1037pub ( crate ) fn prepare ( ) {
38+ if Path :: new ( "download" ) . exists ( ) {
39+ std:: fs:: remove_dir_all ( Path :: new ( "download" ) ) . unwrap ( ) ;
40+ }
41+ std:: fs:: create_dir_all ( Path :: new ( "download" ) ) . unwrap ( ) ;
42+
1143 prepare_sysroot ( ) ;
1244
45+ // FIXME maybe install this only locally?
1346 eprintln ! ( "[INSTALL] hyperfine" ) ;
1447 Command :: new ( "cargo" ) . arg ( "install" ) . arg ( "hyperfine" ) . spawn ( ) . unwrap ( ) . wait ( ) . unwrap ( ) ;
1548
16- clone_repo_shallow_github (
17- "abi-cafe" ,
18- "Gankra" ,
19- "abi-cafe" ,
20- "4c6dc8c9c687e2b3a760ff2176ce236872b37212" ,
21- ) ;
22- apply_patches ( "abi-cafe" , Path :: new ( "abi-cafe" ) ) ;
23-
24- clone_repo_shallow_github (
25- "rand" ,
26- "rust-random" ,
27- "rand" ,
28- "0f933f9c7176e53b2a3c7952ded484e1783f0bf1" ,
29- ) ;
30- apply_patches ( "rand" , Path :: new ( "rand" ) ) ;
31-
32- clone_repo_shallow_github (
33- "regex" ,
34- "rust-lang" ,
35- "regex" ,
36- "341f207c1071f7290e3f228c710817c280c8dca1" ,
37- ) ;
38-
39- clone_repo_shallow_github (
40- "portable-simd" ,
41- "rust-lang" ,
42- "portable-simd" ,
43- "d5cd4a8112d958bd3a252327e0d069a6363249bd" ,
44- ) ;
45- apply_patches ( "portable-simd" , Path :: new ( "portable-simd" ) ) ;
46-
47- clone_repo_shallow_github (
48- "simple-raytracer" ,
49- "ebobby" ,
50- "simple-raytracer" ,
51- "804a7a21b9e673a482797aa289a18ed480e4d813" ,
52- ) ;
49+ ABI_CAFE . fetch ( ) ;
50+ RAND . fetch ( ) ;
51+ REGEX . fetch ( ) ;
52+ PORTABLE_SIMD . fetch ( ) ;
53+ SIMPLE_RAYTRACER . fetch ( ) ;
5354
5455 eprintln ! ( "[LLVM BUILD] simple-raytracer" ) ;
55- let build_cmd = cargo_command ( "cargo" , "build" , None , Path :: new ( "simple-raytracer" ) ) ;
56+ let build_cmd = cargo_command ( "cargo" , "build" , None , & SIMPLE_RAYTRACER . source_dir ( ) ) ;
5657 spawn_and_wait ( build_cmd) ;
5758 fs:: copy (
58- Path :: new ( "simple-raytracer/target/debug" ) . join ( get_file_name ( "main" , "bin" ) ) ,
59- Path :: new ( "simple-raytracer" ) . join ( get_file_name ( "raytracer_cg_llvm" , "bin" ) ) ,
59+ SIMPLE_RAYTRACER
60+ . source_dir ( )
61+ . join ( "target" )
62+ . join ( "debug" )
63+ . join ( get_file_name ( "main" , "bin" ) ) ,
64+ SIMPLE_RAYTRACER . source_dir ( ) . join ( get_file_name ( "raytracer_cg_llvm" , "bin" ) ) ,
6065 )
6166 . unwrap ( ) ;
6267}
@@ -88,38 +93,78 @@ fn prepare_sysroot() {
8893 apply_patches ( "sysroot" , & sysroot_src) ;
8994}
9095
96+ pub ( crate ) struct GitRepo {
97+ url : GitRepoUrl ,
98+ rev : & ' static str ,
99+ patch_name : & ' static str ,
100+ }
101+
102+ enum GitRepoUrl {
103+ Github { user : & ' static str , repo : & ' static str } ,
104+ }
105+
106+ impl GitRepo {
107+ const fn github (
108+ user : & ' static str ,
109+ repo : & ' static str ,
110+ rev : & ' static str ,
111+ patch_name : & ' static str ,
112+ ) -> GitRepo {
113+ GitRepo { url : GitRepoUrl :: Github { user, repo } , rev, patch_name }
114+ }
115+
116+ pub ( crate ) fn source_dir ( & self ) -> PathBuf {
117+ match self . url {
118+ GitRepoUrl :: Github { user : _, repo } => {
119+ std:: env:: current_dir ( ) . unwrap ( ) . join ( "download" ) . join ( repo)
120+ }
121+ }
122+ }
123+
124+ fn fetch ( & self ) {
125+ match self . url {
126+ GitRepoUrl :: Github { user, repo } => {
127+ clone_repo_shallow_github ( & self . source_dir ( ) , user, repo, self . rev ) ;
128+ }
129+ }
130+ apply_patches ( self . patch_name , & self . source_dir ( ) ) ;
131+ }
132+ }
133+
91134#[ allow( dead_code) ]
92- fn clone_repo ( target_dir : & str , repo : & str , rev : & str ) {
135+ fn clone_repo ( download_dir : & Path , repo : & str , rev : & str ) {
93136 eprintln ! ( "[CLONE] {}" , repo) ;
94137 // Ignore exit code as the repo may already have been checked out
95- Command :: new ( "git" ) . arg ( "clone" ) . arg ( repo) . arg ( target_dir ) . spawn ( ) . unwrap ( ) . wait ( ) . unwrap ( ) ;
138+ Command :: new ( "git" ) . arg ( "clone" ) . arg ( repo) . arg ( & download_dir ) . spawn ( ) . unwrap ( ) . wait ( ) . unwrap ( ) ;
96139
97140 let mut clean_cmd = Command :: new ( "git" ) ;
98- clean_cmd. arg ( "checkout" ) . arg ( "--" ) . arg ( "." ) . current_dir ( target_dir ) ;
141+ clean_cmd. arg ( "checkout" ) . arg ( "--" ) . arg ( "." ) . current_dir ( & download_dir ) ;
99142 spawn_and_wait ( clean_cmd) ;
100143
101144 let mut checkout_cmd = Command :: new ( "git" ) ;
102- checkout_cmd. arg ( "checkout" ) . arg ( "-q" ) . arg ( rev) . current_dir ( target_dir ) ;
145+ checkout_cmd. arg ( "checkout" ) . arg ( "-q" ) . arg ( rev) . current_dir ( download_dir ) ;
103146 spawn_and_wait ( checkout_cmd) ;
104147}
105148
106- fn clone_repo_shallow_github ( target_dir : & str , username : & str , repo : & str , rev : & str ) {
149+ fn clone_repo_shallow_github ( download_dir : & Path , user : & str , repo : & str , rev : & str ) {
107150 if cfg ! ( windows) {
108151 // Older windows doesn't have tar or curl by default. Fall back to using git.
109- clone_repo ( target_dir , & format ! ( "https://github.com/{}/{}.git" , username , repo) , rev) ;
152+ clone_repo ( download_dir , & format ! ( "https://github.com/{}/{}.git" , user , repo) , rev) ;
110153 return ;
111154 }
112155
113- let archive_url = format ! ( "https://github.com/{}/{}/archive/{}.tar.gz" , username, repo, rev) ;
114- let archive_file = format ! ( "{}.tar.gz" , rev) ;
115- let archive_dir = format ! ( "{}-{}" , repo, rev) ;
156+ let downloads_dir = std:: env:: current_dir ( ) . unwrap ( ) . join ( "download" ) ;
116157
117- eprintln ! ( "[DOWNLOAD] {}/{} from {}" , username, repo, archive_url) ;
158+ let archive_url = format ! ( "https://github.com/{}/{}/archive/{}.tar.gz" , user, repo, rev) ;
159+ let archive_file = downloads_dir. join ( format ! ( "{}.tar.gz" , rev) ) ;
160+ let archive_dir = downloads_dir. join ( format ! ( "{}-{}" , repo, rev) ) ;
161+
162+ eprintln ! ( "[DOWNLOAD] {}/{} from {}" , user, repo, archive_url) ;
118163
119164 // Remove previous results if they exists
120165 let _ = std:: fs:: remove_file ( & archive_file) ;
121166 let _ = std:: fs:: remove_dir_all ( & archive_dir) ;
122- let _ = std:: fs:: remove_dir_all ( target_dir ) ;
167+ let _ = std:: fs:: remove_dir_all ( & download_dir ) ;
123168
124169 // Download zip archive
125170 let mut download_cmd = Command :: new ( "curl" ) ;
@@ -128,13 +173,13 @@ fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev:
128173
129174 // Unpack tar archive
130175 let mut unpack_cmd = Command :: new ( "tar" ) ;
131- unpack_cmd. arg ( "xf" ) . arg ( & archive_file) ;
176+ unpack_cmd. arg ( "xf" ) . arg ( & archive_file) . current_dir ( downloads_dir ) ;
132177 spawn_and_wait ( unpack_cmd) ;
133178
134179 // Rename unpacked dir to the expected name
135- std:: fs:: rename ( archive_dir, target_dir ) . unwrap ( ) ;
180+ std:: fs:: rename ( archive_dir, & download_dir ) . unwrap ( ) ;
136181
137- init_git_repo ( Path :: new ( target_dir ) ) ;
182+ init_git_repo ( & download_dir ) ;
138183
139184 // Cleanup
140185 std:: fs:: remove_file ( archive_file) . unwrap ( ) ;
@@ -175,6 +220,10 @@ fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
175220}
176221
177222fn apply_patches ( crate_name : & str , target_dir : & Path ) {
223+ if crate_name == "<none>" {
224+ return ;
225+ }
226+
178227 for patch in get_patches ( & std:: env:: current_dir ( ) . unwrap ( ) , crate_name) {
179228 eprintln ! (
180229 "[PATCH] {:?} <- {:?}" ,
0 commit comments