chore: cleanup build.rs, remove unnecessary prefix

This commit is contained in:
Evan Huff 2024-04-08 17:11:30 -04:00 committed by June
parent 9174cb5543
commit 24ba28ca8c

194
build.rs
View file

@ -1,132 +1,112 @@
use std::{ use std::{
env::{self, current_dir}, env::{self, current_dir},
path::Path, path::Path,
process::Command, process::Command,
}; };
/// If submodules were not synced, sync them to actually build hardened_malloc /// If submodules were not synced, sync them to actually build hardened_malloc
fn update_submodules() { fn update_submodules() {
let program = "git"; let program = "git";
let args = ["submodule", "update", "--init", "--recursive"]; let args = ["submodule", "update", "--init", "--recursive"];
println!( println!(
"[hardened_malloc-sys]: Running command: \"{} {}\" in directory: {:?}", "Running command: \"{} {}\" in directory: {:?}",
program, program,
args.join(" "), args.join(" "),
current_dir(), current_dir(),
); );
let ret = Command::new(program).args(args).status(); let ret = Command::new(program).args(args).status();
match ret.map(|status| (status.success(), status.code())) { match ret.map(|status| (status.success(), status.code())) {
Ok((true, _)) => println!("[hardened_malloc-sys]: Updating submodules exited successfully"), Ok((true, _)) => println!("updating submodules exited successfully"),
Ok((false, Some(exit_code))) => panic!( Ok((false, Some(exit_code))) => panic!("updating submodules failed with error code {}", exit_code),
"[hardened_malloc-sys]: Updating submodules failed with error code {}", Ok((false, None)) => {
exit_code panic!("updating submodules exited with no error code, possibly killed by system, exiting.")
), },
Ok((false, None)) => panic!( Err(e) => panic!("updating submodules failed with error: {}", e),
"[hardened_malloc-sys]: Updating submodules exited with no error code, possibly killed by system, exiting." }
),
Err(e) => panic!("[hardened_malloc-sys]: Updating submodules failed with error: {}", e),
}
} }
fn check_compiler(compiler: &str) { fn check_compiler(compiler: &'static str) -> &'static str {
let args = "-v"; let args = "-v";
println!( println!("checking if compiler {} exists", compiler);
"[hardened_malloc-sys]: Checking if compiler {} exists",
compiler
);
let ret = Command::new(compiler).arg(args).status(); let ret = Command::new(compiler).arg(args).status();
match ret.map(|status| (status.success(), status.code())) { match ret.map(|status| (status.success(), status.code())) {
Ok((true, _)) => println!("[hardened_malloc-sys]: Compiler check exited successfully"), Ok((true, _)) => println!("compiler check exited successfully"),
Ok((false, Some(exit_code))) => panic!( Ok((false, Some(exit_code))) => panic!("compiler check failed with error code {}", exit_code),
"[hardened_malloc-sys]: Compiler check failed with error code {}", Ok((false, None)) => panic!("compiler check exited with no error code, possibly killed by system"),
exit_code Err(e) => panic!("compiler check failed with error: {}", e),
), }
Ok((false, None)) => panic!( compiler
"[hardened_malloc-sys]: Compiler check exited with no error code, possibly killed by system"
),
Err(e) => panic!("[hardened_malloc-sys]: Compiler check failed with error: {}", e),
}
} }
fn main() { fn main() {
#[cfg(all(feature = "gcc", feature="clang"))] #[cfg(all(feature = "gcc", feature = "clang"))]
compile_error!("gcc OR clang must be enabled, not both."); compile_error!("gcc OR clang must be enabled, not both.");
println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/hardened_malloc/"); println!("cargo:rerun-if-changed=src/hardened_malloc/");
println!("cargo:rerun-if-changed=src/hardened_malloc/.git"); println!("cargo:rerun-if-changed=src/hardened_malloc/.git");
println!("cargo:rerun-if-changed=src/hardened_malloc/.git/HEAD"); println!("cargo:rerun-if-changed=src/hardened_malloc/.git/HEAD");
println!("cargo:rerun-if-changed=src/hardened_malloc/.git/index"); println!("cargo:rerun-if-changed=src/hardened_malloc/.git/index");
println!("cargo:rerun-if-changed=src/hardened_malloc/.git/refs/tags"); println!("cargo:rerun-if-changed=src/hardened_malloc/.git/refs/tags");
let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = env::var("OUT_DIR").unwrap();
let current_working_directory = current_dir().unwrap();
if !Path::new("src/hardened_malloc/Makefile").exists() { if !Path::new("src/hardened_malloc/Makefile").exists() {
println!("src/hardened_malloc/Makefile does not exist, running submodule sync"); println!("src/hardened_malloc/Makefile does not exist, running submodule sync");
update_submodules(); update_submodules();
} }
let compiler = if cfg!(feature = "gcc") { let compiler = if cfg!(feature = "gcc") {
check_compiler("gcc"); check_compiler("gcc")
"gcc" } else {
} else { check_compiler("clang")
check_compiler("clang"); };
"clang"
};
let variant = if cfg!(feature = "light") { let variant = if cfg!(feature = "light") {
"light" "light"
} else { } else {
"default" // "default" is hardened_malloc's default.mk. this crate's feature uses "standard" for "default" "default" // "default" is hardened_malloc's default.mk. this crate's feature
}; // uses "standard" for "default"
};
let build_args = [ let build_args = [
"VARIANT=".to_owned() + variant, format!("VARIANT={}", variant),
"V=".to_owned() + "1", format!("V={}", "1"),
"OUT=".to_owned() + &out_dir, format!("OUT={}", &out_dir),
"CC=".to_owned() + compiler, format!("CC={}", compiler),
]; ];
//TODO: handle support for explicit make flags like N_ARENA=1 and such //TODO: handle support for explicit make flags like N_ARENA=1 and such
let mut make_command = Command::new("make");
let mut make_command = Command::new("make"); println!("running {:?} with args {:?}", make_command, build_args);
println!("running {:?} with args {:?}", make_command, build_args); let make_output = make_command
.current_dir("src/hardened_malloc/")
.args(build_args)
.output()
.unwrap_or_else(|error| {
panic!("failed to run 'make {}': ", error);
});
let make_output = make_command if !make_output.status.success() {
.current_dir("src/hardened_malloc/") panic!(
.args(build_args) "building hardened_malloc failed:\n{:?}\n{}\n{}",
.output() make_command,
.unwrap_or_else(|error| { String::from_utf8_lossy(&make_output.stdout),
panic!("[hardened_malloc-sys]: Failed to run 'make {}': ", error); String::from_utf8_lossy(&make_output.stderr)
}); );
}
if !make_output.status.success() { if cfg!(feature = "light") {
panic!( println!("cargo:rustc-link-lib=dylib=hardened_malloc-light");
"[hardened_malloc-sys]: building hardened_malloc failed:\n{:?}\n{}\n{}", println!("cargo:rustc-link-search={}", out_dir);
make_command, } else {
String::from_utf8_lossy(&make_output.stdout), println!("cargo:rustc-link-lib=dylib=hardened_malloc");
String::from_utf8_lossy(&make_output.stderr) println!("cargo:rustc-link-search={}", out_dir);
); }
} }
println!(
"[hardened_malloc-sys]: current working directory: {}",
current_working_directory.display()
);
println!("[hardened_malloc-sys]: OUT_DIR={}", out_dir);
if cfg!(feature = "light") {
println!("cargo:rustc-link-lib=dylib=hardened_malloc-light");
println!("cargo:rustc-link-search={}", out_dir);
} else {
println!("cargo:rustc-link-lib=dylib=hardened_malloc");
println!("cargo:rustc-link-search={}", out_dir);
}
}