Skip to content

Commit

Permalink
Better align sys-crate builder/main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Apr 8, 2024
1 parent f7f99de commit ee35a34
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 87 deletions.
8 changes: 4 additions & 4 deletions aws-lc-fips-sys/builder/cmake_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_perl_command() -> bool {
fn test_go_command() -> bool {
let result = test_command("go".as_ref(), &["version".as_ref()]);
if !result.status && result.executed {
eprintln!("Go stdout:\n--------\n{}\n--------", result.output);
eprintln!("Go stdout:\n--------\n{}\n--------", result.stdout);
}
result.status
}
Expand Down Expand Up @@ -166,11 +166,11 @@ impl CmakeBuilder {
let script_path = self.manifest_dir.join("builder").join("printenv.bat");
let result = test_command(script_path.as_os_str(), &[]);
if !result.status {
eprintln!("{}", result.output);
eprintln!("{}", result.stdout);
return Err("Failed to run vcvarsall.bat.".to_owned());
}
eprintln!("{}", result.output);
let lines = result.output.lines();
eprintln!("{}", result.stdout);
let lines = result.stdout.lines();
for line in lines {
if let Some((var, val)) = line.split_once('=') {
map.insert(var.to_string(), val.to_string());
Expand Down
154 changes: 85 additions & 69 deletions aws-lc-fips-sys/builder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,36 @@ fn target_platform_prefix(name: &str) -> String {
}

pub(crate) struct TestCommandResult {
output: Box<str>,
status: bool,
#[allow(dead_code)]
stderr: Box<str>,
#[allow(dead_code)]
stdout: Box<str>,
executed: bool,
status: bool,
}

fn test_command(executable: &OsStr, args: &[&OsStr]) -> TestCommandResult {
if let Ok(result) = Command::new(executable).args(args).output() {
let output = String::from_utf8(result.stdout)
if let Ok(mut result) = Command::new(executable).args(args).output() {
result.stderr.truncate(4112);
let stderr = String::from_utf8(result.stderr)
.unwrap_or_default()
.into_boxed_str();
result.stdout.truncate(4112);
let stdout = String::from_utf8(result.stdout)
.unwrap_or_default()
.into_boxed_str();
return TestCommandResult {
output,
status: result.status.success(),
stderr,
stdout,
executed: true,
status: result.status.success(),
};
}
TestCommandResult {
output: String::new().into_boxed_str(),
status: false,
stderr: String::new().into_boxed_str(),
stdout: String::new().into_boxed_str(),
executed: false,
status: false,
}
}

Expand Down Expand Up @@ -242,95 +252,101 @@ fn current_dir() -> PathBuf {
std::env::current_dir().unwrap()
}

macro_rules! cfg_bindgen_platform {
($binding:ident, $target:literal, $additional:expr) => {
let $binding = {
(target() == $target && $additional)
.then(|| {
emit_rustc_cfg(&$target.replace('-', "_"));
true
})
.unwrap_or(false)
};
};
fn get_builder(prefix: &Option<String>, manifest_dir: &Path, out_dir: &Path) -> Box<dyn Builder> {
Box::new(CmakeBuilder::new(
manifest_dir.to_path_buf(),
out_dir.to_path_buf(),
prefix.clone(),
OutputLibType::default(),
))
}

trait Builder {
fn check_dependencies(&self) -> Result<(), String>;
fn build(&self) -> Result<(), String>;
}

#[allow(clippy::too_many_lines)]
fn main() {
let is_internal_no_prefix =
env_var_to_bool("AWS_LC_FIPS_SYS_INTERNAL_NO_PREFIX").unwrap_or(false);
let is_internal_generate = env_var_to_bool("AWS_LC_RUST_INTERNAL_BINDGEN").unwrap_or(false);
let mut is_bindgen_required =
is_internal_no_prefix || is_internal_generate || cfg!(feature = "bindgen");

let pregenerated = !is_bindgen_required || is_internal_generate;

cfg_bindgen_platform!(
x86_64_unknown_linux_gnu,
"x86_64-unknown-linux-gnu",
pregenerated
);
cfg_bindgen_platform!(
aarch64_unknown_linux_gnu,
"aarch64-unknown-linux-gnu",
pregenerated
);
cfg_bindgen_platform!(
x86_64_unknown_linux_musl,
"x86_64-unknown-linux-musl",
pregenerated
);
cfg_bindgen_platform!(
aarch64_unknown_linux_musl,
"aarch64-unknown-linux-musl",
pregenerated
);
cfg_bindgen_platform!(x86_64_apple_darwin, "x86_64-apple-darwin", pregenerated);
cfg_bindgen_platform!(aarch64_apple_darwin, "aarch64-apple-darwin", pregenerated);

if !(x86_64_unknown_linux_gnu
|| aarch64_unknown_linux_gnu
|| x86_64_unknown_linux_musl
|| aarch64_unknown_linux_musl
|| x86_64_apple_darwin
|| aarch64_apple_darwin)
{
is_bindgen_required = true;
static mut PREGENERATED: bool = false;
static mut AWS_LC_SYS_INTERNAL_NO_PREFIX: bool = false;
static mut AWS_LC_RUST_INTERNAL_BINDGEN: bool = false;

fn initialize() {
unsafe {
AWS_LC_SYS_INTERNAL_NO_PREFIX =
env_var_to_bool("AWS_LC_SYS_INTERNAL_NO_PREFIX").unwrap_or(false);
AWS_LC_RUST_INTERNAL_BINDGEN =
env_var_to_bool("AWS_LC_RUST_INTERNAL_BINDGEN").unwrap_or(false);
}

if is_internal_generate() || !has_bindgen_feature() {
let target = target();
let supported_platform = match target.as_str() {
"x86_64-unknown-linux-gnu"
| "aarch64-unknown-linux-gnu"
| "x86_64-unknown-linux-musl"
| "aarch64-unknown-linux-musl"
| "x86_64-apple-darwin"
| "aarch64-apple-darwin" => Some(target),
_ => None,
};
if let Some(platform) = supported_platform {
emit_rustc_cfg(platform.as_str());
unsafe {
PREGENERATED = true;
}
}
}
}

fn is_bindgen_required() -> bool {
is_internal_no_prefix()
|| is_internal_generate()
|| has_bindgen_feature()
|| !has_pregenerated()
}

fn is_internal_no_prefix() -> bool {
unsafe { AWS_LC_SYS_INTERNAL_NO_PREFIX }
}

fn is_internal_generate() -> bool {
unsafe { AWS_LC_RUST_INTERNAL_BINDGEN }
}

fn has_bindgen_feature() -> bool {
cfg!(feature = "bindgen")
}

fn has_pregenerated() -> bool {
unsafe { PREGENERATED }
}

fn main() {
initialize();

let manifest_dir = current_dir();
let manifest_dir = dunce::canonicalize(Path::new(&manifest_dir)).unwrap();
let prefix_str = prefix_string();
let prefix = if is_internal_no_prefix {
let prefix = if is_internal_no_prefix() {
None
} else {
Some(prefix_str)
};

let builder = CmakeBuilder::new(
manifest_dir.clone(),
out_dir(),
prefix.clone(),
OutputLibType::default(),
);
let builder = get_builder(&prefix, &manifest_dir, &out_dir());

builder.check_dependencies().unwrap();

#[allow(unused_assignments)]
let mut bindings_available = false;
if is_internal_generate {
if is_internal_generate() {
#[cfg(feature = "bindgen")]
{
let src_bindings_path = Path::new(&manifest_dir).join("src");
generate_src_bindings(&manifest_dir, prefix, &src_bindings_path);
bindings_available = true;
}
} else if is_bindgen_required {
} else if is_bindgen_required() {
#[cfg(any(
feature = "bindgen",
not(all(
Expand Down
8 changes: 4 additions & 4 deletions aws-lc-sys/builder/cc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ impl CcBuilder {
memcmp_compiler.path(),
memcmp_args.as_slice(),
memcmp_compile_result.executed,
memcmp_compile_result.error,
memcmp_compile_result.output
memcmp_compile_result.stderr,
memcmp_compile_result.stdout
);

if cargo_env("HOST") == target() {
Expand All @@ -239,8 +239,8 @@ impl CcBuilder {
",
memcmp_compiler.path().display(),
memcmp_compile_result.executed,
memcmp_compile_result.error,
memcmp_compile_result.output
memcmp_compile_result.stderr,
memcmp_compile_result.stdout
);
}
let _ = fs::remove_file(exec_path);
Expand Down
22 changes: 12 additions & 10 deletions aws-lc-sys/builder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,33 @@ fn target_platform_prefix(name: &str) -> String {

pub(crate) struct TestCommandResult {
#[allow(dead_code)]
error: Box<str>,
stderr: Box<str>,
#[allow(dead_code)]
output: Box<str>,
stdout: Box<str>,
executed: bool,
status: bool,
}

fn test_command(executable: &OsStr, args: &[&OsStr]) -> TestCommandResult {
if let Ok(result) = Command::new(executable).args(args).output() {
let error = String::from_utf8(result.stderr)
if let Ok(mut result) = Command::new(executable).args(args).output() {
result.stderr.truncate(4112);
let stderr = String::from_utf8(result.stderr)
.unwrap_or_default()
.into_boxed_str();
let output = String::from_utf8(result.stdout)
result.stdout.truncate(4112);
let stdout = String::from_utf8(result.stdout)
.unwrap_or_default()
.into_boxed_str();
return TestCommandResult {
error,
output,
stderr,
stdout,
executed: true,
status: result.status.success(),
};
}
TestCommandResult {
error: String::new().into_boxed_str(),
output: String::new().into_boxed_str(),
stderr: String::new().into_boxed_str(),
stdout: String::new().into_boxed_str(),
executed: false,
status: false,
}
Expand Down Expand Up @@ -228,7 +230,7 @@ fn target_arch() -> String {
cargo_env("CARGO_CFG_TARGET_ARCH")
}

#[allow(dead_code)]
#[allow(unused)]
fn target_env() -> String {
cargo_env("CARGO_CFG_TARGET_ENV")
}
Expand Down

0 comments on commit ee35a34

Please sign in to comment.