-
Notifications
You must be signed in to change notification settings - Fork 78
Rustify build.sh script #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,233 @@ | ||
use crate::config::set_config; | ||
use crate::utils::{ | ||
get_gcc_path, run_command, run_command_with_env, run_command_with_output_and_env, walk_dir, | ||
}; | ||
use std::collections::HashMap; | ||
use std::ffi::OsStr; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
#[derive(Default)] | ||
struct BuildArg { | ||
codegen_release_channel: bool, | ||
sysroot_release_channel: bool, | ||
features: Vec<String>, | ||
gcc_path: String, | ||
} | ||
|
||
impl BuildArg { | ||
fn new() -> Result<Option<Self>, String> { | ||
let gcc_path = get_gcc_path()?; | ||
let mut build_arg = Self { | ||
gcc_path, | ||
..Default::default() | ||
}; | ||
// We skip binary name and the `build` command. | ||
let mut args = std::env::args().skip(2); | ||
|
||
while let Some(arg) = args.next() { | ||
match arg.as_str() { | ||
"--release" => build_arg.codegen_release_channel = true, | ||
"--release-sysroot" => build_arg.sysroot_release_channel = true, | ||
"--no-default-features" => { | ||
build_arg.features.push("--no-default-features".to_string()); | ||
} | ||
"--features" => { | ||
if let Some(arg) = args.next() { | ||
build_arg.features.push("--features".to_string()); | ||
build_arg.features.push(arg.as_str().into()); | ||
GuillaumeGomez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
return Err( | ||
"Expected a value after `--features`, found nothing".to_string() | ||
); | ||
} | ||
} | ||
"--help" => { | ||
Self::usage(); | ||
return Ok(None); | ||
} | ||
arg => return Err(format!("Unknown argument `{}`", arg)), | ||
} | ||
} | ||
Ok(Some(build_arg)) | ||
} | ||
|
||
fn usage() { | ||
println!( | ||
r#" | ||
`build` command help: | ||
|
||
--release : Build codegen in release mode | ||
--release-sysroot : Build sysroot in release mode | ||
--no-default-features : Add `--no-default-features` flag | ||
--features [arg] : Add a new feature [arg] | ||
--help : Show this help | ||
"# | ||
) | ||
} | ||
} | ||
|
||
fn build_sysroot( | ||
env: &mut HashMap<String, String>, | ||
release_mode: bool, | ||
target_triple: &str, | ||
) -> Result<(), String> { | ||
std::env::set_current_dir("build_sysroot") | ||
.map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?; | ||
// Cleanup for previous run | ||
// Clean target dir except for build scripts and incremental cache | ||
let _ = walk_dir( | ||
"target", | ||
|dir: &Path| { | ||
for top in &["debug", "release"] { | ||
let _ = fs::remove_dir_all(dir.join(top).join("build")); | ||
let _ = fs::remove_dir_all(dir.join(top).join("deps")); | ||
let _ = fs::remove_dir_all(dir.join(top).join("examples")); | ||
let _ = fs::remove_dir_all(dir.join(top).join("native")); | ||
|
||
let _ = walk_dir( | ||
dir.join(top), | ||
|sub_dir: &Path| { | ||
if sub_dir | ||
.file_name() | ||
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot")) | ||
.unwrap_or(false) | ||
{ | ||
let _ = fs::remove_dir_all(sub_dir); | ||
} | ||
Ok(()) | ||
}, | ||
|file: &Path| { | ||
if file | ||
.file_name() | ||
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot")) | ||
.unwrap_or(false) | ||
{ | ||
let _ = fs::remove_file(file); | ||
} | ||
Ok(()) | ||
}, | ||
); | ||
} | ||
Ok(()) | ||
}, | ||
|_| Ok(()), | ||
); | ||
|
||
let _ = fs::remove_file("Cargo.lock"); | ||
let _ = fs::remove_file("test_target/Cargo.lock"); | ||
let _ = fs::remove_dir_all("sysroot"); | ||
|
||
// Builds libs | ||
let channel = if release_mode { | ||
let rustflags = env | ||
.get("RUSTFLAGS") | ||
.cloned() | ||
.unwrap_or_default(); | ||
env.insert( | ||
"RUSTFLAGS".to_string(), | ||
format!("{} -Zmir-opt-level=3", rustflags), | ||
); | ||
run_command_with_output_and_env( | ||
&[ | ||
&"cargo", | ||
&"build", | ||
&"--target", | ||
&target_triple, | ||
&"--release", | ||
], | ||
None, | ||
Some(&env), | ||
)?; | ||
"release" | ||
} else { | ||
run_command_with_output_and_env( | ||
&[ | ||
&"cargo", | ||
&"build", | ||
&"--target", | ||
&target_triple, | ||
&"--features", | ||
&"compiler_builtins/c", | ||
], | ||
None, | ||
Some(env), | ||
)?; | ||
"debug" | ||
}; | ||
|
||
// Copy files to sysroot | ||
let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", target_triple); | ||
fs::create_dir_all(&sysroot_path) | ||
.map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?; | ||
let copier = |dir_to_copy: &Path| { | ||
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ()) | ||
}; | ||
walk_dir( | ||
&format!("target/{}/{}/deps", target_triple, channel), | ||
copier, | ||
copier, | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn build_codegen(args: &BuildArg) -> Result<(), String> { | ||
let mut env = HashMap::new(); | ||
|
||
let current_dir = | ||
std::env::current_dir().map_err(|error| format!("`current_dir` failed: {:?}", error))?; | ||
if let Ok(rt_root) = std::env::var("RUST_COMPILER_RT_ROOT") { | ||
env.insert("RUST_COMPILER_RT_ROOT".to_string(), rt_root); | ||
} else { | ||
env.insert( | ||
"RUST_COMPILER_RT_ROOT".to_string(), | ||
format!("{}", current_dir.join("llvm/compiler-rt").display()), | ||
); | ||
} | ||
env.insert("LD_LIBRARY_PATH".to_string(), args.gcc_path.clone()); | ||
env.insert("LIBRARY_PATH".to_string(), args.gcc_path.clone()); | ||
|
||
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &"rustc"]; | ||
if args.codegen_release_channel { | ||
command.push(&"--release"); | ||
env.insert("CHANNEL".to_string(), "release".to_string()); | ||
env.insert("CARGO_INCREMENTAL".to_string(), "1".to_string()); | ||
} else { | ||
env.insert("CHANNEL".to_string(), "debug".to_string()); | ||
} | ||
let ref_features = args.features.iter().map(|s| s.as_str()).collect::<Vec<_>>(); | ||
antoyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for feature in &ref_features { | ||
command.push(feature); | ||
} | ||
run_command_with_env(&command, None, Some(&env))?; | ||
|
||
let config = set_config(&mut env, &[], Some(&args.gcc_path))?; | ||
|
||
// We voluntarily ignore the error. | ||
let _ = fs::remove_dir_all("target/out"); | ||
let gccjit_target = "target/out/gccjit"; | ||
fs::create_dir_all(gccjit_target).map_err(|error| { | ||
format!( | ||
"Failed to create directory `{}`: {:?}", | ||
gccjit_target, error | ||
) | ||
})?; | ||
|
||
println!("[BUILD] sysroot"); | ||
build_sysroot( | ||
&mut env, | ||
args.sysroot_release_channel, | ||
&config.target_triple, | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn run() -> Result<(), String> { | ||
let args = match BuildArg::new()? { | ||
Some(args) => args, | ||
None => return Ok(()), | ||
}; | ||
build_codegen(&args)?; | ||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.