From 72bfef36c7512b6b83aa33339d65a0869099bdb6 Mon Sep 17 00:00:00 2001 From: Justin Smith Date: Thu, 18 Jan 2024 14:54:37 -0500 Subject: [PATCH] * Realign aws-lc-sys build with aws-lc-fips-sys * cleanup --- aws-lc-fips-sys/CMakeLists.txt | 11 ++++------- aws-lc-fips-sys/builder/cmake_builder.rs | 10 +++++----- aws-lc-fips-sys/builder/main.rs | 7 ++----- aws-lc-sys/CMakeLists.txt | 4 ++++ aws-lc-sys/Cargo.toml | 1 + aws-lc-sys/builder/cmake_builder.rs | 15 ++++++--------- aws-lc-sys/builder/main.rs | 12 +----------- aws-lc-sys/include/rust_wrapper.h | 9 ++++++--- 8 files changed, 29 insertions(+), 40 deletions(-) diff --git a/aws-lc-fips-sys/CMakeLists.txt b/aws-lc-fips-sys/CMakeLists.txt index 952ba1240fb..0f087c31a30 100644 --- a/aws-lc-fips-sys/CMakeLists.txt +++ b/aws-lc-fips-sys/CMakeLists.txt @@ -18,9 +18,10 @@ if (BUILD_SHARED_LIBS AND FIPS) # as cmake crate will postfix the C/CXX flags after our disablement nullifying them. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections") -endif () +endif() add_subdirectory(aws-lc aws-lc EXCLUDE_FROM_ALL) + if (BUILD_LIBSSL) add_definitions(-DAWS_LC_RUST_INCLUDE_SSL) endif() @@ -38,6 +39,7 @@ set(FINAL_ARTIFACTS_DIRECTORY ${CMAKE_BINARY_DIR}/artifacts) # location to find the artifacts cross-platform. set_my_target_properties( ARCHIVE_OUTPUT_DIRECTORY ${FINAL_ARTIFACTS_DIRECTORY} + RUNTIME_OUTPUT_DIRECTORY ${FINAL_ARTIFACTS_DIRECTORY} LIBRARY_OUTPUT_DIRECTORY ${FINAL_ARTIFACTS_DIRECTORY}) # Based on https://stackoverflow.com/a/7750816 as some generators, like MSVC, will try to prefix the output directory @@ -46,6 +48,7 @@ foreach (OUT_NAME ${CMAKE_CONFIGURATION_TYPES}) string(TOUPPER ${OUT_NAME} OUT_NAME) set_my_target_properties( ARCHIVE_OUTPUT_DIRECTORY_${OUT_NAME} ${FINAL_ARTIFACTS_DIRECTORY} + RUNTIME_OUTPUT_DIRECTORY_${OUT_NAME} ${FINAL_ARTIFACTS_DIRECTORY} LIBRARY_OUTPUT_DIRECTORY_${OUT_NAME} ${FINAL_ARTIFACTS_DIRECTORY}) endforeach () @@ -58,9 +61,3 @@ if (BORINGSSL_PREFIX) endif() set_my_target_properties(PREFIX ${TARGET_PREFIX}) endif () - -if (BUILD_LIBSSL) - install(TARGETS rust_wrapper crypto ssl DESTINATION .) -else() - install(TARGETS rust_wrapper crypto DESTINATION .) -endif() diff --git a/aws-lc-fips-sys/builder/cmake_builder.rs b/aws-lc-fips-sys/builder/cmake_builder.rs index e39c3fd4a55..04fd7a45603 100644 --- a/aws-lc-fips-sys/builder/cmake_builder.rs +++ b/aws-lc-fips-sys/builder/cmake_builder.rs @@ -94,18 +94,20 @@ impl CmakeBuilder { // The Windows/FIPS build rejects "debug" profile // https://github.com/aws/aws-lc/blob/main/CMakeLists.txt#L656 cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo"); + } else { + cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); + } + + if target_os() == "windows" { cmake_cfg.generator("Ninja"); let env_map = self .collect_vcvarsall_bat() .map_err(|x| panic!("{}", x)) .unwrap(); - println!("Setting environment!"); for (key, value) in env_map { println!("ENV-{}={}", key.as_str(), value.as_str()); cmake_cfg.env(key, value); } - } else { - cmake_cfg.define("CMAKE_BUILD_TYPE", "debug"); } if let Some(prefix) = &self.build_prefix { @@ -145,7 +147,6 @@ impl CmakeBuilder { cmake_cfg.define("ASAN", "1"); } - cmake_cfg.define("CMAKE_INSTALL_PREFIX", self.artifact_output_dir()); cmake_cfg } @@ -153,7 +154,6 @@ impl CmakeBuilder { fn build_rust_wrapper(&self) -> PathBuf { self.prepare_cmake_build() .configure_arg("--no-warn-unused-cli") - .build_target("install") .build() } diff --git a/aws-lc-fips-sys/builder/main.rs b/aws-lc-fips-sys/builder/main.rs index 8baf2a15d77..03520dbe8d9 100644 --- a/aws-lc-fips-sys/builder/main.rs +++ b/aws-lc-fips-sys/builder/main.rs @@ -57,8 +57,7 @@ enum OutputLibType { impl Default for OutputLibType { fn default() -> Self { - let build_type_result = env::var("AWS_LC_FIPS_SYS_STATIC"); - if let Ok(build_type) = build_type_result { + if let Ok(build_type) = env::var("AWS_LC_FIPS_SYS_STATIC") { eprintln!("AWS_LC_FIPS_SYS_STATIC={build_type}"); // If the environment variable is set, we ignore every other factor. let build_type = build_type.to_lowercase(); @@ -228,9 +227,8 @@ trait Builder { } fn main() { - let output_lib_type = OutputLibType::default(); - let mut is_bindgen_required = cfg!(feature = "bindgen"); + let output_lib_type = OutputLibType::default(); let is_internal_generate = env::var("AWS_LC_RUST_INTERNAL_BINDGEN") .unwrap_or_else(|_| String::from("0")) @@ -291,7 +289,6 @@ fn main() { bindings_available, "aws-lc-fip-sys build failed. Please enable the 'bindgen' feature on aws-lc-rs or aws-lc-fips-sys" ); - builder.build().unwrap(); println!( diff --git a/aws-lc-sys/CMakeLists.txt b/aws-lc-sys/CMakeLists.txt index c19fd82c1fe..19e5bb29b10 100644 --- a/aws-lc-sys/CMakeLists.txt +++ b/aws-lc-sys/CMakeLists.txt @@ -18,6 +18,7 @@ add_subdirectory(aws-lc aws-lc EXCLUDE_FROM_ALL) if (BUILD_LIBSSL) add_definitions(-DAWS_LC_RUST_INCLUDE_SSL) endif() + add_library(rust_wrapper rust_wrapper.c) target_include_directories(rust_wrapper PRIVATE include) target_link_libraries(rust_wrapper PUBLIC crypto) @@ -31,6 +32,7 @@ set(FINAL_ARTIFACTS_DIRECTORY ${CMAKE_BINARY_DIR}/artifacts) # location to find the artifacts cross-platform. set_my_target_properties( ARCHIVE_OUTPUT_DIRECTORY ${FINAL_ARTIFACTS_DIRECTORY} + RUNTIME_OUTPUT_DIRECTORY ${FINAL_ARTIFACTS_DIRECTORY} LIBRARY_OUTPUT_DIRECTORY ${FINAL_ARTIFACTS_DIRECTORY}) # Based on https://stackoverflow.com/a/7750816 as some generators, like MSVC, will try to prefix the output directory @@ -39,12 +41,14 @@ foreach (OUT_NAME ${CMAKE_CONFIGURATION_TYPES}) string(TOUPPER ${OUT_NAME} OUT_NAME) set_my_target_properties( ARCHIVE_OUTPUT_DIRECTORY_${OUT_NAME} ${FINAL_ARTIFACTS_DIRECTORY} + RUNTIME_OUTPUT_DIRECTORY_${OUT_NAME} ${FINAL_ARTIFACTS_DIRECTORY} LIBRARY_OUTPUT_DIRECTORY_${OUT_NAME} ${FINAL_ARTIFACTS_DIRECTORY}) endforeach () if (BORINGSSL_PREFIX) if (MSVC) set(TARGET_PREFIX "${BORINGSSL_PREFIX}") + set_my_target_properties(IMPORT_PREFIX ${TARGET_PREFIX}) else() set(TARGET_PREFIX "lib${BORINGSSL_PREFIX}") endif() diff --git a/aws-lc-sys/Cargo.toml b/aws-lc-sys/Cargo.toml index 166e6b42770..ebd0c7decf6 100644 --- a/aws-lc-sys/Cargo.toml +++ b/aws-lc-sys/Cargo.toml @@ -53,6 +53,7 @@ bindgen = ["dep:bindgen"] # Generate the bindings on the targetted platform as a cmake = "0.1.48" dunce = "1.0" fs_extra = "1.3" +which = "5.0" [target.'cfg(any(all(target_os = "macos", target_arch = "x86_64"), all(target_os = "linux", target_arch = "x86", target_env="gnu"), all(target_os = "linux", target_arch = "x86_64", target_env="gnu"), all(target_os = "linux", target_arch = "aarch64", target_env="gnu")))'.build-dependencies] bindgen = { version = "0.69.1", optional = true } diff --git a/aws-lc-sys/builder/cmake_builder.rs b/aws-lc-sys/builder/cmake_builder.rs index c95e8ff468f..043e57ccc8b 100644 --- a/aws-lc-sys/builder/cmake_builder.rs +++ b/aws-lc-sys/builder/cmake_builder.rs @@ -2,10 +2,10 @@ // 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 crate::{target, target_arch, target_os, target_vendor, OutputLibType}; use std::env; -use std::ffi::OsStr; use std::path::PathBuf; +use which::which; pub(crate) struct CmakeBuilder { manifest_dir: PathBuf, @@ -14,14 +14,11 @@ pub(crate) struct CmakeBuilder { 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 find_cmake_command() -> Option { + if let Ok(path) = which("cmake").or_else(|_| which("cmake3")) { + return Some(path); } + None } fn get_platform_output_path() -> PathBuf { diff --git a/aws-lc-sys/builder/main.rs b/aws-lc-sys/builder/main.rs index 0b441d064ae..768074d2c12 100644 --- a/aws-lc-sys/builder/main.rs +++ b/aws-lc-sys/builder/main.rs @@ -4,9 +4,7 @@ // SPDX-License-Identifier: Apache-2.0 OR ISC use std::env; -use std::ffi::OsStr; use std::path::{Path, PathBuf}; -use std::process::Command; use cmake_builder::CmakeBuilder; @@ -57,8 +55,7 @@ enum OutputLibType { impl Default for OutputLibType { fn default() -> Self { - let build_type_result = env::var("AWS_LC_SYS_STATIC"); - if let Ok(build_type) = build_type_result { + if let Ok(build_type) = env::var("AWS_LC_SYS_STATIC") { eprintln!("AWS_LC_SYS_STATIC={build_type}"); // If the environment variable is set, we ignore every other factor. let build_type = build_type.to_lowercase(); @@ -109,13 +106,6 @@ fn target_platform_prefix(name: &str) -> String { format!("{}_{}_{}", env::consts::OS, env::consts::ARCH, name) } -fn test_command(executable: &OsStr, args: &[&OsStr]) -> bool { - if let Ok(output) = Command::new(executable).args(args).output() { - return output.status.success(); - } - false -} - #[cfg(any( feature = "bindgen", not(any( diff --git a/aws-lc-sys/include/rust_wrapper.h b/aws-lc-sys/include/rust_wrapper.h index 5e6cbbef798..24a5f455d82 100644 --- a/aws-lc-sys/include/rust_wrapper.h +++ b/aws-lc-sys/include/rust_wrapper.h @@ -8,6 +8,9 @@ #ifdef _WIN32 #define BORINGSSL_SHARED_LIBRARY +#define AWS_LC_FIPS_SYS_EXPORT __declspec(dllexport) +#else +#define AWS_LC_FIPS_SYS_EXPORT __attribute__((visibility("default"))) #endif #include @@ -20,9 +23,9 @@ extern "C" { // The following functions are wrappers over inline functions and macros in // BoringSSL, which bindgen cannot currently correctly bind. These wrappers // ensure changes to the functions remain in lockstep with the Rust versions. -int ERR_GET_LIB_RUST(uint32_t packed_error); -int ERR_GET_REASON_RUST(uint32_t packed_error); -int ERR_GET_FUNC_RUST(uint32_t packed_error); +AWS_LC_FIPS_SYS_EXPORT int ERR_GET_LIB_RUST(uint32_t packed_error); +AWS_LC_FIPS_SYS_EXPORT int ERR_GET_REASON_RUST(uint32_t packed_error); +AWS_LC_FIPS_SYS_EXPORT int ERR_GET_FUNC_RUST(uint32_t packed_error); #if defined(__cplusplus)