From 806535fad81adaf35f7a0044bc6828686499b5ee Mon Sep 17 00:00:00 2001 From: Justin W Smith <103147162+justsmth@users.noreply.github.com> Date: Mon, 18 Dec 2023 12:32:07 -0500 Subject: [PATCH 1/3] Refactor aws-lc-sys CMake build logic --- aws-lc-fips-sys/builder/cmake_builder.rs | 181 +++++++++++++++++++++++ aws-lc-sys/builder/cmake_builder.rs | 181 +++++++++++++++++++++++ aws-lc-sys/builder/main.rs | 167 +++------------------ 3 files changed, 382 insertions(+), 147 deletions(-) create mode 100644 aws-lc-fips-sys/builder/cmake_builder.rs create mode 100644 aws-lc-sys/builder/cmake_builder.rs diff --git a/aws-lc-fips-sys/builder/cmake_builder.rs b/aws-lc-fips-sys/builder/cmake_builder.rs new file mode 100644 index 00000000000..b6ec60f0ed2 --- /dev/null +++ b/aws-lc-fips-sys/builder/cmake_builder.rs @@ -0,0 +1,181 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 OR ISC + +use crate::OutputLib::{Crypto, RustWrapper, Ssl}; +use crate::{target, target_arch, target_os, target_vendor, test_command, OutputLibType}; +use std::env; +use std::ffi::OsStr; +use std::path::PathBuf; + +pub(crate) struct CmakeBuilder { + manifest_dir: PathBuf, + out_dir: PathBuf, + build_prefix: Option, + output_lib_type: OutputLibType, +} + +fn find_cmake_command() -> Option<&'static OsStr> { + if test_command("cmake3".as_ref(), &["--version".as_ref()]) { + Some("cmake3".as_ref()) + } else if test_command("cmake".as_ref(), &["--version".as_ref()]) { + Some("cmake".as_ref()) + } else { + None + } +} + +fn get_platform_output_path() -> PathBuf { + PathBuf::new() +} + +impl CmakeBuilder { + pub(crate) fn new( + manifest_dir: PathBuf, + out_dir: PathBuf, + build_prefix: Option, + output_lib_type: OutputLibType, + ) -> Self { + Self { + manifest_dir, + out_dir, + build_prefix, + output_lib_type, + } + } + + fn artifact_output_dir(&self) -> PathBuf { + self.out_dir + .join("build") + .join("artifacts") + .join(get_platform_output_path()) + } + + fn get_cmake_config(&self) -> cmake::Config { + cmake::Config::new(&self.manifest_dir) + } + + fn prepare_cmake_build(&self) -> cmake::Config { + let mut cmake_cfg = self.get_cmake_config(); + + if ["powerpc64", "powerpc"] + .iter() + .any(|arch| target_arch().eq_ignore_ascii_case(arch)) + { + cmake_cfg.define("ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT", "1"); + } + + if OutputLibType::default() == OutputLibType::Dynamic { + cmake_cfg.define("BUILD_SHARED_LIBS", "1"); + } else { + cmake_cfg.define("BUILD_SHARED_LIBS", "0"); + } + + let opt_level = env::var("OPT_LEVEL").unwrap_or_else(|_| "0".to_string()); + if opt_level.ne("0") { + if opt_level.eq("1") || opt_level.eq("2") { + cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo"); + } else { + cmake_cfg.define("CMAKE_BUILD_TYPE", "release"); + } + } else { + cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); + } + + if let Some(prefix) = &self.build_prefix { + cmake_cfg.define("BORINGSSL_PREFIX", format!("{prefix}_")); + let include_path = self.manifest_dir.join("generated-include"); + cmake_cfg.define( + "BORINGSSL_PREFIX_HEADERS", + include_path.display().to_string(), + ); + } + + // Build flags that minimize our crate size. + cmake_cfg.define("BUILD_TESTING", "OFF"); + if cfg!(feature = "ssl") { + cmake_cfg.define("BUILD_LIBSSL", "ON"); + } else { + cmake_cfg.define("BUILD_LIBSSL", "OFF"); + } + // Build flags that minimize our dependencies. + cmake_cfg.define("DISABLE_PERL", "ON"); + cmake_cfg.define("DISABLE_GO", "ON"); + + if target_vendor() == "apple" { + if target_os().trim() == "ios" { + cmake_cfg.define("CMAKE_SYSTEM_NAME", "iOS"); + if target().trim().ends_with("-ios-sim") { + cmake_cfg.define("CMAKE_OSX_SYSROOT", "iphonesimulator"); + } + } + if target_arch().trim() == "aarch64" { + cmake_cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64"); + } + } + + if cfg!(feature = "asan") { + env::set_var("CC", "/usr/bin/clang"); + env::set_var("CXX", "/usr/bin/clang++"); + env::set_var("ASM", "/usr/bin/clang"); + + cmake_cfg.define("ASAN", "1"); + } + + cmake_cfg + } + + fn build_rust_wrapper(&self) -> PathBuf { + self.prepare_cmake_build() + .configure_arg("--no-warn-unused-cli") + .build() + } +} + +impl crate::Builder for CmakeBuilder { + fn check_dependencies(&self) -> Result<(), String> { + let mut missing_dependency = false; + + if let Some(cmake_cmd) = find_cmake_command() { + env::set_var("CMAKE", cmake_cmd); + } else { + eprintln!("Missing dependency: cmake"); + missing_dependency = true; + }; + + if missing_dependency { + return Err("Required build dependency is missing. Halting build.".to_owned()); + } + + Ok(()) + } + fn build(&self) -> Result<(), String> { + self.build_rust_wrapper(); + + println!( + "cargo:rustc-link-search=native={}", + self.artifact_output_dir().display() + ); + + println!( + "cargo:rustc-link-lib={}={}", + self.output_lib_type.rust_lib_type(), + Crypto.libname(&self.build_prefix) + ); + + if cfg!(feature = "ssl") { + println!( + "cargo:rustc-link-lib={}={}", + self.output_lib_type.rust_lib_type(), + Ssl.libname(&self.build_prefix) + ); + } + + println!( + "cargo:rustc-link-lib={}={}", + self.output_lib_type.rust_lib_type(), + RustWrapper.libname(&self.build_prefix) + ); + + Ok(()) + } +} diff --git a/aws-lc-sys/builder/cmake_builder.rs b/aws-lc-sys/builder/cmake_builder.rs new file mode 100644 index 00000000000..b6ec60f0ed2 --- /dev/null +++ b/aws-lc-sys/builder/cmake_builder.rs @@ -0,0 +1,181 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 OR ISC + +use crate::OutputLib::{Crypto, RustWrapper, Ssl}; +use crate::{target, target_arch, target_os, target_vendor, test_command, OutputLibType}; +use std::env; +use std::ffi::OsStr; +use std::path::PathBuf; + +pub(crate) struct CmakeBuilder { + manifest_dir: PathBuf, + out_dir: PathBuf, + build_prefix: Option, + output_lib_type: OutputLibType, +} + +fn find_cmake_command() -> Option<&'static OsStr> { + if test_command("cmake3".as_ref(), &["--version".as_ref()]) { + Some("cmake3".as_ref()) + } else if test_command("cmake".as_ref(), &["--version".as_ref()]) { + Some("cmake".as_ref()) + } else { + None + } +} + +fn get_platform_output_path() -> PathBuf { + PathBuf::new() +} + +impl CmakeBuilder { + pub(crate) fn new( + manifest_dir: PathBuf, + out_dir: PathBuf, + build_prefix: Option, + output_lib_type: OutputLibType, + ) -> Self { + Self { + manifest_dir, + out_dir, + build_prefix, + output_lib_type, + } + } + + fn artifact_output_dir(&self) -> PathBuf { + self.out_dir + .join("build") + .join("artifacts") + .join(get_platform_output_path()) + } + + fn get_cmake_config(&self) -> cmake::Config { + cmake::Config::new(&self.manifest_dir) + } + + fn prepare_cmake_build(&self) -> cmake::Config { + let mut cmake_cfg = self.get_cmake_config(); + + if ["powerpc64", "powerpc"] + .iter() + .any(|arch| target_arch().eq_ignore_ascii_case(arch)) + { + cmake_cfg.define("ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT", "1"); + } + + if OutputLibType::default() == OutputLibType::Dynamic { + cmake_cfg.define("BUILD_SHARED_LIBS", "1"); + } else { + cmake_cfg.define("BUILD_SHARED_LIBS", "0"); + } + + let opt_level = env::var("OPT_LEVEL").unwrap_or_else(|_| "0".to_string()); + if opt_level.ne("0") { + if opt_level.eq("1") || opt_level.eq("2") { + cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo"); + } else { + cmake_cfg.define("CMAKE_BUILD_TYPE", "release"); + } + } else { + cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); + } + + if let Some(prefix) = &self.build_prefix { + cmake_cfg.define("BORINGSSL_PREFIX", format!("{prefix}_")); + let include_path = self.manifest_dir.join("generated-include"); + cmake_cfg.define( + "BORINGSSL_PREFIX_HEADERS", + include_path.display().to_string(), + ); + } + + // Build flags that minimize our crate size. + cmake_cfg.define("BUILD_TESTING", "OFF"); + if cfg!(feature = "ssl") { + cmake_cfg.define("BUILD_LIBSSL", "ON"); + } else { + cmake_cfg.define("BUILD_LIBSSL", "OFF"); + } + // Build flags that minimize our dependencies. + cmake_cfg.define("DISABLE_PERL", "ON"); + cmake_cfg.define("DISABLE_GO", "ON"); + + if target_vendor() == "apple" { + if target_os().trim() == "ios" { + cmake_cfg.define("CMAKE_SYSTEM_NAME", "iOS"); + if target().trim().ends_with("-ios-sim") { + cmake_cfg.define("CMAKE_OSX_SYSROOT", "iphonesimulator"); + } + } + if target_arch().trim() == "aarch64" { + cmake_cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64"); + } + } + + if cfg!(feature = "asan") { + env::set_var("CC", "/usr/bin/clang"); + env::set_var("CXX", "/usr/bin/clang++"); + env::set_var("ASM", "/usr/bin/clang"); + + cmake_cfg.define("ASAN", "1"); + } + + cmake_cfg + } + + fn build_rust_wrapper(&self) -> PathBuf { + self.prepare_cmake_build() + .configure_arg("--no-warn-unused-cli") + .build() + } +} + +impl crate::Builder for CmakeBuilder { + fn check_dependencies(&self) -> Result<(), String> { + let mut missing_dependency = false; + + if let Some(cmake_cmd) = find_cmake_command() { + env::set_var("CMAKE", cmake_cmd); + } else { + eprintln!("Missing dependency: cmake"); + missing_dependency = true; + }; + + if missing_dependency { + return Err("Required build dependency is missing. Halting build.".to_owned()); + } + + Ok(()) + } + fn build(&self) -> Result<(), String> { + self.build_rust_wrapper(); + + println!( + "cargo:rustc-link-search=native={}", + self.artifact_output_dir().display() + ); + + println!( + "cargo:rustc-link-lib={}={}", + self.output_lib_type.rust_lib_type(), + Crypto.libname(&self.build_prefix) + ); + + if cfg!(feature = "ssl") { + println!( + "cargo:rustc-link-lib={}={}", + self.output_lib_type.rust_lib_type(), + Ssl.libname(&self.build_prefix) + ); + } + + println!( + "cargo:rustc-link-lib={}={}", + self.output_lib_type.rust_lib_type(), + RustWrapper.libname(&self.build_prefix) + ); + + Ok(()) + } +} diff --git a/aws-lc-sys/builder/main.rs b/aws-lc-sys/builder/main.rs index 2bb931aa7c2..0b441d064ae 100644 --- a/aws-lc-sys/builder/main.rs +++ b/aws-lc-sys/builder/main.rs @@ -8,6 +8,8 @@ use std::ffi::OsStr; use std::path::{Path, PathBuf}; use std::process::Command; +use cmake_builder::CmakeBuilder; + #[cfg(any( feature = "bindgen", not(any( @@ -18,6 +20,7 @@ use std::process::Command; )) ))] mod bindgen; +mod cmake_builder; pub(crate) fn get_aws_lc_include_path(manifest_dir: &Path) -> PathBuf { manifest_dir.join("aws-lc").join("include") @@ -66,8 +69,6 @@ impl Default for OutputLibType { // Only dynamic if the value is set and is a "negative" value return OutputLibType::Dynamic; } - - return OutputLibType::Static; } OutputLibType::Static } @@ -83,7 +84,7 @@ impl OutputLibType { } impl OutputLib { - fn libname(self, prefix: Option<&str>) -> String { + fn libname(self, prefix: &Option) -> String { let name = match self { OutputLib::Crypto => "crypto", OutputLib::Ssl => "ssl", @@ -97,16 +98,6 @@ impl OutputLib { } } -fn artifact_output_dir(path: &Path) -> PathBuf { - path.join("build") - .join("artifacts") - .join(get_platform_output_path()) -} - -fn get_platform_output_path() -> PathBuf { - PathBuf::new() -} - const VERSION: &str = env!("CARGO_PKG_VERSION"); fn prefix_string() -> String { @@ -125,94 +116,6 @@ fn test_command(executable: &OsStr, args: &[&OsStr]) -> bool { false } -fn find_cmake_command() -> Option<&'static OsStr> { - if test_command("cmake3".as_ref(), &["--version".as_ref()]) { - Some("cmake3".as_ref()) - } else if test_command("cmake".as_ref(), &["--version".as_ref()]) { - Some("cmake".as_ref()) - } else { - None - } -} - -fn get_cmake_config(manifest_dir: &PathBuf) -> cmake::Config { - cmake::Config::new(manifest_dir) -} - -fn prepare_cmake_build(manifest_dir: &PathBuf, build_prefix: String) -> cmake::Config { - let mut cmake_cfg = get_cmake_config(manifest_dir); - - if ["powerpc64", "powerpc"] - .iter() - .any(|arch| target_arch().eq_ignore_ascii_case(arch)) - { - cmake_cfg.define("ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT", "1"); - } - - if OutputLibType::default() == OutputLibType::Dynamic { - cmake_cfg.define("BUILD_SHARED_LIBS", "1"); - } else { - cmake_cfg.define("BUILD_SHARED_LIBS", "0"); - } - - let opt_level = env::var("OPT_LEVEL").unwrap_or_else(|_| "0".to_string()); - if opt_level.ne("0") { - if opt_level.eq("1") || opt_level.eq("2") { - cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo"); - } else { - cmake_cfg.define("CMAKE_BUILD_TYPE", "release"); - } - } else { - cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); - } - - cmake_cfg.define("BORINGSSL_PREFIX", build_prefix); - let include_path = manifest_dir.join("generated-include"); - cmake_cfg.define( - "BORINGSSL_PREFIX_HEADERS", - include_path.display().to_string(), - ); - - // Build flags that minimize our crate size. - cmake_cfg.define("BUILD_TESTING", "OFF"); - if cfg!(feature = "ssl") { - cmake_cfg.define("BUILD_LIBSSL", "ON"); - } else { - cmake_cfg.define("BUILD_LIBSSL", "OFF"); - } - // Build flags that minimize our dependencies. - cmake_cfg.define("DISABLE_PERL", "ON"); - cmake_cfg.define("DISABLE_GO", "ON"); - - if target_vendor() == "apple" { - if target_os().trim() == "ios" { - cmake_cfg.define("CMAKE_SYSTEM_NAME", "iOS"); - if target().trim().ends_with("-ios-sim") { - cmake_cfg.define("CMAKE_OSX_SYSROOT", "iphonesimulator"); - } - } - if target_arch().trim() == "aarch64" { - cmake_cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64"); - } - } - - if cfg!(feature = "asan") { - env::set_var("CC", "/usr/bin/clang"); - env::set_var("CXX", "/usr/bin/clang++"); - env::set_var("ASM", "/usr/bin/clang"); - - cmake_cfg.define("ASAN", "1"); - } - - cmake_cfg -} - -fn build_rust_wrapper(manifest_dir: &PathBuf) -> PathBuf { - prepare_cmake_build(manifest_dir, prefix_string() + "_") - .configure_arg("--no-warn-unused-cli") - .build() -} - #[cfg(any( feature = "bindgen", not(any( @@ -298,9 +201,12 @@ macro_rules! cfg_bindgen_platform { }; } -fn main() { - use crate::OutputLib::{Crypto, RustWrapper, Ssl}; +trait Builder { + fn check_dependencies(&self) -> Result<(), String>; + fn build(&self) -> Result<(), String>; +} +fn main() { let mut is_bindgen_required = cfg!(feature = "bindgen"); let output_lib_type = OutputLibType::default(); @@ -320,13 +226,20 @@ fn main() { is_bindgen_required = true; } - check_dependencies(); - let manifest_dir = env::current_dir().unwrap(); let manifest_dir = dunce::canonicalize(Path::new(&manifest_dir)).unwrap(); let prefix = prefix_string(); + let out_dir_str = env::var("OUT_DIR").unwrap(); + let out_dir = Path::new(out_dir_str.as_str()).to_path_buf(); + + let builder = CmakeBuilder::new( + manifest_dir.clone(), + out_dir.clone(), + Some(prefix.clone()), + output_lib_type, + ); - let out_dir = build_rust_wrapper(&manifest_dir); + builder.check_dependencies().unwrap(); #[allow(unused_assignments)] let mut bindings_available = false; @@ -360,31 +273,7 @@ fn main() { bindings_available, "aws-lc-sys build failed. Please enable the 'bindgen' feature on aws-lc-rs or aws-lc-sys" ); - - println!( - "cargo:rustc-link-search=native={}", - artifact_output_dir(&out_dir).display() - ); - - println!( - "cargo:rustc-link-lib={}={}", - output_lib_type.rust_lib_type(), - Crypto.libname(Some(&prefix)) - ); - - if cfg!(feature = "ssl") { - println!( - "cargo:rustc-link-lib={}={}", - output_lib_type.rust_lib_type(), - Ssl.libname(Some(&prefix)) - ); - } - - println!( - "cargo:rustc-link-lib={}={}", - output_lib_type.rust_lib_type(), - RustWrapper.libname(Some(&prefix)) - ); + builder.build().unwrap(); println!( "cargo:include={}", @@ -396,22 +285,6 @@ fn main() { println!("cargo:rerun-if-env-changed=AWS_LC_SYS_STATIC"); } -fn check_dependencies() { - let mut missing_dependency = false; - - if let Some(cmake_cmd) = find_cmake_command() { - env::set_var("CMAKE", cmake_cmd); - } else { - eprintln!("Missing dependency: cmake"); - missing_dependency = true; - }; - - assert!( - !missing_dependency, - "Required build dependency is missing. Halting build." - ); -} - fn setup_include_paths(out_dir: &Path, manifest_dir: &Path) -> PathBuf { let mut include_paths = vec![ get_rust_include_path(manifest_dir), From f14a3b7fedb2212a8b5ecfd4887c929cd274e135 Mon Sep 17 00:00:00 2001 From: Justin W Smith <103147162+justsmth@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:15:54 -0500 Subject: [PATCH 2/3] Refactor aws-lc-fips-sys CMake build logic --- aws-lc-fips-sys/builder/cmake_builder.rs | 25 +++- aws-lc-fips-sys/builder/main.rs | 160 +++-------------------- 2 files changed, 40 insertions(+), 145 deletions(-) diff --git a/aws-lc-fips-sys/builder/cmake_builder.rs b/aws-lc-fips-sys/builder/cmake_builder.rs index b6ec60f0ed2..59cd4e48569 100644 --- a/aws-lc-fips-sys/builder/cmake_builder.rs +++ b/aws-lc-fips-sys/builder/cmake_builder.rs @@ -14,6 +14,14 @@ pub(crate) struct CmakeBuilder { output_lib_type: OutputLibType, } +fn test_perl_command() -> bool { + test_command("perl".as_ref(), &["--version".as_ref()]) +} + +fn test_go_command() -> bool { + test_command("go".as_ref(), &["version".as_ref()]) +} + fn find_cmake_command() -> Option<&'static OsStr> { if test_command("cmake3".as_ref(), &["--version".as_ref()]) { Some("cmake3".as_ref()) @@ -77,7 +85,9 @@ impl CmakeBuilder { } else { cmake_cfg.define("CMAKE_BUILD_TYPE", "release"); } - } else { + } else if target_os() != "windows" { + // The Windows/FIPS build rejects "debug" profile + // https://github.com/aws/aws-lc/blob/main/CMakeLists.txt#L656 cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); } @@ -97,9 +107,6 @@ impl CmakeBuilder { } else { cmake_cfg.define("BUILD_LIBSSL", "OFF"); } - // Build flags that minimize our dependencies. - cmake_cfg.define("DISABLE_PERL", "ON"); - cmake_cfg.define("DISABLE_GO", "ON"); if target_vendor() == "apple" { if target_os().trim() == "ios" { @@ -112,6 +119,7 @@ impl CmakeBuilder { cmake_cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64"); } } + cmake_cfg.define("FIPS", "1"); if cfg!(feature = "asan") { env::set_var("CC", "/usr/bin/clang"); @@ -134,7 +142,14 @@ impl CmakeBuilder { impl crate::Builder for CmakeBuilder { fn check_dependencies(&self) -> Result<(), String> { let mut missing_dependency = false; - + if !test_go_command() { + eprintln!("Missing dependency: Go is required for FIPS."); + missing_dependency = true; + } + if !test_perl_command() { + eprintln!("Missing dependency: perl is required for FIPS."); + missing_dependency = true; + } if let Some(cmake_cmd) = find_cmake_command() { env::set_var("CMAKE", cmake_cmd); } else { diff --git a/aws-lc-fips-sys/builder/main.rs b/aws-lc-fips-sys/builder/main.rs index 56a4e589490..fcb3759cfb6 100644 --- a/aws-lc-fips-sys/builder/main.rs +++ b/aws-lc-fips-sys/builder/main.rs @@ -10,6 +10,8 @@ use std::ffi::OsStr; use std::path::{Path, PathBuf}; use std::process::Command; +use cmake_builder::CmakeBuilder; + #[cfg(any( feature = "bindgen", not(any( @@ -18,6 +20,7 @@ use std::process::Command; )) ))] mod bindgen; +mod cmake_builder; pub(crate) fn get_aws_lc_include_path(manifest_dir: &Path) -> PathBuf { manifest_dir.join("aws-lc").join("include") @@ -88,7 +91,7 @@ impl OutputLibType { } impl OutputLib { - fn libname(self, prefix: Option<&str>) -> String { + fn libname(self, prefix: &Option) -> String { let name = match self { OutputLib::Crypto => "crypto", OutputLib::Ssl => "ssl", @@ -102,30 +105,12 @@ impl OutputLib { } } -fn artifact_output_dir(path: &Path) -> PathBuf { - path.join("build") - .join("artifacts") - .join(get_platform_output_path()) -} - -fn get_platform_output_path() -> PathBuf { - PathBuf::new() -} - const VERSION: &str = env!("CARGO_PKG_VERSION"); fn prefix_string() -> String { format!("aws_lc_fips_{}", VERSION.to_string().replace('.', "_")) } -fn test_perl_command() -> bool { - test_command("perl".as_ref(), &["--version".as_ref()]) -} - -fn test_go_command() -> bool { - test_command("go".as_ref(), &["version".as_ref()]) -} - #[cfg(feature = "bindgen")] fn target_platform_prefix(name: &str) -> String { format!("{}_{}_{}", env::consts::OS, env::consts::ARCH, name) @@ -138,75 +123,6 @@ fn test_command(executable: &OsStr, args: &[&OsStr]) -> bool { false } -fn find_cmake_command() -> Option<&'static OsStr> { - if test_command("cmake3".as_ref(), &["--version".as_ref()]) { - Some("cmake3".as_ref()) - } else if test_command("cmake".as_ref(), &["--version".as_ref()]) { - Some("cmake".as_ref()) - } else { - None - } -} - -fn get_cmake_config(manifest_dir: &PathBuf) -> cmake::Config { - cmake::Config::new(manifest_dir) -} - -fn prepare_cmake_build(manifest_dir: &PathBuf, build_prefix: String) -> cmake::Config { - let mut cmake_cfg = get_cmake_config(manifest_dir); - - if OutputLibType::default() == OutputLibType::Dynamic { - cmake_cfg.define("BUILD_SHARED_LIBS", "1"); - } else { - cmake_cfg.define("BUILD_SHARED_LIBS", "0"); - } - - let opt_level = env::var("OPT_LEVEL").unwrap_or_else(|_| "0".to_string()); - if opt_level.ne("0") { - if opt_level.eq("1") || opt_level.eq("2") { - cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo"); - } else { - cmake_cfg.define("CMAKE_BUILD_TYPE", "release"); - } - } else if target_os() != "windows" { - // The Windows/FIPS build rejects "debug" profile - // https://github.com/aws/aws-lc/blob/main/CMakeLists.txt#L656 - cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); - } - - cmake_cfg.define("BORINGSSL_PREFIX", build_prefix); - let include_path = manifest_dir.join("generated-include"); - cmake_cfg.define( - "BORINGSSL_PREFIX_HEADERS", - include_path.display().to_string(), - ); - - // Build flags that minimize our crate size. - cmake_cfg.define("BUILD_TESTING", "OFF"); - if cfg!(feature = "ssl") { - cmake_cfg.define("BUILD_LIBSSL", "ON"); - } else { - cmake_cfg.define("BUILD_LIBSSL", "OFF"); - } - cmake_cfg.define("FIPS", "1"); - - if cfg!(feature = "asan") { - env::set_var("CC", "/usr/bin/clang"); - env::set_var("CXX", "/usr/bin/clang++"); - env::set_var("ASM", "/usr/bin/clang"); - - cmake_cfg.define("ASAN", "1"); - } - - cmake_cfg -} - -fn build_rust_wrapper(manifest_dir: &PathBuf) -> PathBuf { - prepare_cmake_build(manifest_dir, prefix_string() + "_") - .configure_arg("--no-warn-unused-cli") - .build() -} - #[cfg(any( feature = "bindgen", not(any( @@ -292,9 +208,12 @@ macro_rules! cfg_bindgen_platform { }; } -fn main() { - use crate::OutputLib::{Crypto, RustWrapper, Ssl}; +trait Builder { + fn check_dependencies(&self) -> Result<(), String>; + fn build(&self) -> Result<(), String>; +} +fn main() { let output_lib_type = OutputLibType::default(); let mut is_bindgen_required = cfg!(feature = "bindgen"); @@ -313,13 +232,20 @@ fn main() { is_bindgen_required = true; } - check_dependencies(); - let manifest_dir = env::current_dir().unwrap(); let manifest_dir = dunce::canonicalize(Path::new(&manifest_dir)).unwrap(); let prefix = prefix_string(); + let out_dir_str = env::var("OUT_DIR").unwrap(); + let out_dir = Path::new(out_dir_str.as_str()).to_path_buf(); + + let builder = CmakeBuilder::new( + manifest_dir.clone(), + out_dir.clone(), + Some(prefix.clone()), + output_lib_type, + ); - let out_dir = build_rust_wrapper(&manifest_dir); + builder.check_dependencies().unwrap(); #[allow(unused_assignments)] let mut bindings_available = false; @@ -352,30 +278,7 @@ fn main() { "aws-lc-fip-sys build failed. Please enable the 'bindgen' feature on aws-lc-rs or aws-lc-fips-sys" ); - println!( - "cargo:rustc-link-search=native={}", - artifact_output_dir(&out_dir).display() - ); - - println!( - "cargo:rustc-link-lib={}={}", - output_lib_type.rust_lib_type(), - Crypto.libname(Some(&prefix)) - ); - - if cfg!(feature = "ssl") { - println!( - "cargo:rustc-link-lib={}={}", - output_lib_type.rust_lib_type(), - Ssl.libname(Some(&prefix)) - ); - } - - println!( - "cargo:rustc-link-lib={}={}", - output_lib_type.rust_lib_type(), - RustWrapper.libname(Some(&prefix)) - ); + builder.build().unwrap(); println!( "cargo:include={}", @@ -387,29 +290,6 @@ fn main() { println!("cargo:rerun-if-env-changed=AWS_LC_FIPS_SYS_STATIC"); } -fn check_dependencies() { - let mut missing_dependency = false; - if !test_go_command() { - eprintln!("Missing dependency: Go is required for FIPS."); - missing_dependency = true; - } - if !test_perl_command() { - eprintln!("Missing dependency: perl is required for FIPS."); - missing_dependency = true; - } - if let Some(cmake_cmd) = find_cmake_command() { - env::set_var("CMAKE", cmake_cmd); - } else { - eprintln!("Missing dependency: cmake"); - missing_dependency = true; - }; - - assert!( - !missing_dependency, - "Required build dependency is missing. Halting build." - ); -} - fn setup_include_paths(out_dir: &Path, manifest_dir: &Path) -> PathBuf { let mut include_paths = vec![ get_rust_include_path(manifest_dir), From 9c0a2d7b794bb66332825d1cc45932d08027632f Mon Sep 17 00:00:00 2001 From: Justin W Smith <103147162+justsmth@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:25:56 -0500 Subject: [PATCH 3/3] no hardcoded clang paths --- aws-lc-fips-sys/builder/cmake_builder.rs | 6 +++--- aws-lc-sys/builder/cmake_builder.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/aws-lc-fips-sys/builder/cmake_builder.rs b/aws-lc-fips-sys/builder/cmake_builder.rs index 59cd4e48569..a60e0e2c39c 100644 --- a/aws-lc-fips-sys/builder/cmake_builder.rs +++ b/aws-lc-fips-sys/builder/cmake_builder.rs @@ -122,9 +122,9 @@ impl CmakeBuilder { cmake_cfg.define("FIPS", "1"); if cfg!(feature = "asan") { - env::set_var("CC", "/usr/bin/clang"); - env::set_var("CXX", "/usr/bin/clang++"); - env::set_var("ASM", "/usr/bin/clang"); + env::set_var("CC", "clang"); + env::set_var("CXX", "clang++"); + env::set_var("ASM", "clang"); cmake_cfg.define("ASAN", "1"); } diff --git a/aws-lc-sys/builder/cmake_builder.rs b/aws-lc-sys/builder/cmake_builder.rs index b6ec60f0ed2..c95e8ff468f 100644 --- a/aws-lc-sys/builder/cmake_builder.rs +++ b/aws-lc-sys/builder/cmake_builder.rs @@ -114,9 +114,9 @@ impl CmakeBuilder { } if cfg!(feature = "asan") { - env::set_var("CC", "/usr/bin/clang"); - env::set_var("CXX", "/usr/bin/clang++"); - env::set_var("ASM", "/usr/bin/clang"); + env::set_var("CC", "clang"); + env::set_var("CXX", "clang++"); + env::set_var("ASM", "clang"); cmake_cfg.define("ASAN", "1"); }