diff --git a/aws-lc-fips-sys/builder/main.rs b/aws-lc-fips-sys/builder/main.rs index fcb3759cfb6..a016691c6bc 100644 --- a/aws-lc-fips-sys/builder/main.rs +++ b/aws-lc-fips-sys/builder/main.rs @@ -55,22 +55,32 @@ enum OutputLibType { Dynamic, } +fn env_var_to_bool(name: &str) -> Option { + let build_type_result = env::var(name); + if let Ok(env_var_value) = build_type_result { + eprintln!("{name}={env_var_value}"); + // If the environment variable is set, we ignore every other factor. + let env_var_value = env_var_value.to_lowercase(); + if env_var_value.starts_with('0') + || env_var_value.starts_with('n') + || env_var_value.starts_with("off") + { + return Some(false); + } + // Otherwise, if the variable is set, assume true + return Some(true); + } + return None; +} + 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 { - 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(); - if build_type.starts_with('0') - || build_type.starts_with('n') - || build_type.starts_with("off") - { - // Only dynamic if the value is set and is a "negative" value - return OutputLibType::Dynamic; - } - - return OutputLibType::Static; + if let Some(val) = env_var_to_bool("AWS_LC_FIPS_SYS_STATIC") { + return if val { + OutputLibType::Static + } else { + OutputLibType::Dynamic + }; } if target_os() == "linux" && (target_arch() == "x86_64" || target_arch() == "aarch64") { @@ -185,7 +195,6 @@ fn target_env() -> String { env::var("CARGO_CFG_TARGET_ENV").unwrap() } -#[allow(unused)] fn target_vendor() -> String { env::var("CARGO_CFG_TARGET_VENDOR").unwrap() } @@ -195,6 +204,25 @@ fn target() -> String { env::var("TARGET").unwrap() } +fn get_builder( + prefix: Option, + manifest_dir: &PathBuf, + out_dir: &PathBuf, +) -> Box { + let cmake_builder_builder = || { + Box::new(CmakeBuilder::new( + manifest_dir.clone(), + out_dir.clone(), + prefix.clone(), + OutputLibType::default(), + )) + }; + + let cmake_builder = cmake_builder_builder(); + cmake_builder.check_dependencies().unwrap(); + cmake_builder +} + macro_rules! cfg_bindgen_platform { ($binding:ident, $os:literal, $arch:literal, $env:literal, $additional:expr) => { let $binding = { @@ -214,8 +242,6 @@ trait Builder { } fn main() { - let output_lib_type = OutputLibType::default(); - let mut is_bindgen_required = cfg!(feature = "bindgen"); let is_internal_generate = env::var("AWS_LC_RUST_INTERNAL_BINDGEN") @@ -237,15 +263,7 @@ fn main() { 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, - ); - - builder.check_dependencies().unwrap(); + let builder = get_builder(Some(prefix.clone()), &manifest_dir, &out_dir); #[allow(unused_assignments)] let mut bindings_available = false; @@ -275,9 +293,8 @@ fn main() { assert!( bindings_available, - "aws-lc-fip-sys build failed. Please enable the 'bindgen' feature on aws-lc-rs or aws-lc-fips-sys" + "aws-lc-fips-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/Cargo.toml b/aws-lc-sys/Cargo.toml index ec5b22e76c9..e36df19a2ef 100644 --- a/aws-lc-sys/Cargo.toml +++ b/aws-lc-sys/Cargo.toml @@ -53,6 +53,9 @@ bindgen = ["dep:bindgen"] # Generate the bindings on the targetted platform as a cmake = "0.1.48" dunce = "1.0" fs_extra = "1.3" +serde = { version = "1.0.195", features = ["derive"] } +toml = "0.6.0" +cc = { version = "1.0.83", features = ["parallel"] } [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 } @@ -64,6 +67,5 @@ bindgen = { version = "0.69.2" } [dependencies] libc = "0.2" paste = "1.0.11" - [package.metadata.aws-lc-sys] commit-hash = "c2b14dfb82e1a4d0f2260e05cf07c3a864e52d99" diff --git a/aws-lc-sys/builder/cc/x86_64-apple-darwin.toml b/aws-lc-sys/builder/cc/x86_64-apple-darwin.toml new file mode 100644 index 00000000000..0081eb5c48a --- /dev/null +++ b/aws-lc-sys/builder/cc/x86_64-apple-darwin.toml @@ -0,0 +1,298 @@ +[[Library]] +name = "crypto" +flags = [] +sources = [ + "crypto/asn1/a_bitstr.c", + "crypto/asn1/a_bool.c", + "crypto/asn1/a_d2i_fp.c", + "crypto/asn1/a_dup.c", + "crypto/asn1/a_gentm.c", + "crypto/asn1/a_i2d_fp.c", + "crypto/asn1/a_int.c", + "crypto/asn1/a_mbstr.c", + "crypto/asn1/a_object.c", + "crypto/asn1/a_octet.c", + "crypto/asn1/a_strex.c", + "crypto/asn1/a_strnid.c", + "crypto/asn1/a_time.c", + "crypto/asn1/a_type.c", + "crypto/asn1/a_utctm.c", + "crypto/asn1/a_utf8.c", + "crypto/asn1/asn1_lib.c", + "crypto/asn1/asn1_par.c", + "crypto/asn1/asn_pack.c", + "crypto/asn1/f_int.c", + "crypto/asn1/f_string.c", + "crypto/asn1/tasn_dec.c", + "crypto/asn1/tasn_enc.c", + "crypto/asn1/tasn_fre.c", + "crypto/asn1/tasn_new.c", + "crypto/asn1/tasn_typ.c", + "crypto/asn1/tasn_utl.c", + "crypto/asn1/posix_time.c", + "crypto/base64/base64.c", + "crypto/bio/bio.c", + "crypto/bio/bio_mem.c", + "crypto/bio/connect.c", + "crypto/bio/fd.c", + "crypto/bio/file.c", + "crypto/bio/hexdump.c", + "crypto/bio/pair.c", + "crypto/bio/printf.c", + "crypto/bio/socket.c", + "crypto/bio/socket_helper.c", + "crypto/blake2/blake2.c", + "crypto/bn_extra/bn_asn1.c", + "crypto/bn_extra/convert.c", + "crypto/buf/buf.c", + "crypto/bytestring/asn1_compat.c", + "crypto/bytestring/ber.c", + "crypto/bytestring/cbb.c", + "crypto/bytestring/cbs.c", + "crypto/bytestring/unicode.c", + "crypto/chacha/chacha.c", + "crypto/cipher_extra/cipher_extra.c", + "crypto/cipher_extra/derive_key.c", + "crypto/cipher_extra/e_aesctrhmac.c", + "crypto/cipher_extra/e_aesgcmsiv.c", + "crypto/cipher_extra/e_chacha20poly1305.c", + "crypto/cipher_extra/e_aes_cbc_hmac_sha1.c", + "crypto/cipher_extra/e_aes_cbc_hmac_sha256.c", + "crypto/cipher_extra/e_des.c", + "crypto/cipher_extra/e_null.c", + "crypto/cipher_extra/e_rc2.c", + "crypto/cipher_extra/e_rc4.c", + "crypto/cipher_extra/e_tls.c", + "crypto/cipher_extra/tls_cbc.c", + "crypto/conf/conf.c", + "crypto/crypto.c", + "crypto/curve25519/curve25519.c", + "crypto/curve25519/curve25519_nohw.c", + "crypto/curve25519/curve25519_s2n_bignum_asm.c", + "crypto/curve25519/spake25519.c", + "crypto/des/des.c", + "crypto/dh_extra/params.c", + "crypto/dh_extra/dh_asn1.c", + "crypto/digest_extra/digest_extra.c", + "crypto/dsa/dsa.c", + "crypto/dsa/dsa_asn1.c", + "crypto/ecdh_extra/ecdh_extra.c", + "crypto/ecdsa_extra/ecdsa_asn1.c", + "crypto/ec_extra/ec_asn1.c", + "crypto/ec_extra/ec_derive.c", + "crypto/ec_extra/hash_to_curve.c", + "crypto/err/err.c", + "generated-src/err_data.c", + "crypto/engine/engine.c", + "crypto/evp_extra/evp_asn1.c", + "crypto/evp_extra/p_dsa_asn1.c", + "crypto/evp_extra/p_ec_asn1.c", + "crypto/evp_extra/p_ed25519.c", + "crypto/evp_extra/p_ed25519_asn1.c", + "crypto/evp_extra/p_hmac_asn1.c", + "crypto/evp_extra/p_kem.c", + "crypto/evp_extra/p_kem_asn1.c", + "crypto/evp_extra/p_rsa_asn1.c", + "crypto/evp_extra/p_x25519.c", + "crypto/evp_extra/p_x25519_asn1.c", + "crypto/evp_extra/p_methods.c", + "crypto/evp_extra/print.c", + "crypto/evp_extra/scrypt.c", + "crypto/evp_extra/sign.c", + "crypto/ex_data.c", + "crypto/hpke/hpke.c", + "crypto/hrss/hrss.c", + "crypto/kem/kem.c", + "crypto/kem/kem_methods.c", + "crypto/kyber/kyber512r3_ref.c", + "crypto/kyber/kyber768r3_ref.c", + "crypto/kyber/kyber1024r3_ref.c", + "crypto/kyber/pqcrystals_kyber_ref_common/fips202.c", + "crypto/kyber/kem_kyber.c", + "crypto/lhash/lhash.c", + "crypto/mem.c", + "crypto/obj/obj.c", + "crypto/obj/obj_xref.c", + "crypto/ocsp/ocsp_asn.c", + "crypto/ocsp/ocsp_client.c", + "crypto/ocsp/ocsp_extension.c", + "crypto/ocsp/ocsp_http.c", + "crypto/ocsp/ocsp_lib.c", + "crypto/ocsp/ocsp_print.c", + "crypto/ocsp/ocsp_server.c", + "crypto/ocsp/ocsp_verify.c", + "crypto/pem/pem_all.c", + "crypto/pem/pem_info.c", + "crypto/pem/pem_lib.c", + "crypto/pem/pem_oth.c", + "crypto/pem/pem_pk8.c", + "crypto/pem/pem_pkey.c", + "crypto/pem/pem_x509.c", + "crypto/pem/pem_xaux.c", + "crypto/pkcs7/pkcs7.c", + "crypto/pkcs7/pkcs7_x509.c", + "crypto/pkcs8/pkcs8.c", + "crypto/pkcs8/pkcs8_x509.c", + "crypto/pkcs8/p5_pbev2.c", + "crypto/poly1305/poly1305_vec.c", + "crypto/pool/pool.c", + "crypto/rand_extra/forkunsafe.c", + "crypto/rand_extra/rand_extra.c", + "crypto/rand_extra/pq_custom_randombytes.c", + "crypto/rc4/rc4.c", + "crypto/refcount_lock.c", + "crypto/rsa_extra/rsa_asn1.c", + "crypto/rsa_extra/rsassa_pss_asn1.c", + "crypto/rsa_extra/rsa_print.c", + "crypto/stack/stack.c", + "crypto/siphash/siphash.c", + "crypto/thread.c", + "crypto/thread_pthread.c", + "crypto/trust_token/pmbtoken.c", + "crypto/trust_token/trust_token.c", + "crypto/trust_token/voprf.c", + "crypto/x509/a_digest.c", + "crypto/x509/a_sign.c", + "crypto/x509/a_verify.c", + "crypto/x509/algorithm.c", + "crypto/x509/asn1_gen.c", + "crypto/x509/by_dir.c", + "crypto/x509/by_file.c", + "crypto/x509/i2d_pr.c", + "crypto/x509/name_print.c", + "crypto/x509/policy.c", + "crypto/x509/rsa_pss.c", + "crypto/x509/t_crl.c", + "crypto/x509/t_req.c", + "crypto/x509/t_x509.c", + "crypto/x509/t_x509a.c", + "crypto/x509/x509.c", + "crypto/x509/x509_att.c", + "crypto/x509/x509_cmp.c", + "crypto/x509/x509_d2.c", + "crypto/x509/x509_def.c", + "crypto/x509/x509_ext.c", + "crypto/x509/x509_lu.c", + "crypto/x509/x509_obj.c", + "crypto/x509/x509_req.c", + "crypto/x509/x509_set.c", + "crypto/x509/x509_trs.c", + "crypto/x509/x509_txt.c", + "crypto/x509/x509_v3.c", + "crypto/x509/x509_vfy.c", + "crypto/x509/x509_vpm.c", + "crypto/x509/x509cset.c", + "crypto/x509/x509name.c", + "crypto/x509/x509rset.c", + "crypto/x509/x509spki.c", + "crypto/x509/x_algor.c", + "crypto/x509/x_all.c", + "crypto/x509/x_attrib.c", + "crypto/x509/x_crl.c", + "crypto/x509/x_exten.c", + "crypto/x509/x_info.c", + "crypto/x509/x_name.c", + "crypto/x509/x_pkey.c", + "crypto/x509/x_pubkey.c", + "crypto/x509/x_req.c", + "crypto/x509/x_sig.c", + "crypto/x509/x_spki.c", + "crypto/x509/x_val.c", + "crypto/x509/x_x509.c", + "crypto/x509/x_x509a.c", + "crypto/x509v3/v3_akey.c", + "crypto/x509v3/v3_akeya.c", + "crypto/x509v3/v3_alt.c", + "crypto/x509v3/v3_bcons.c", + "crypto/x509v3/v3_bitst.c", + "crypto/x509v3/v3_conf.c", + "crypto/x509v3/v3_cpols.c", + "crypto/x509v3/v3_crld.c", + "crypto/x509v3/v3_enum.c", + "crypto/x509v3/v3_extku.c", + "crypto/x509v3/v3_genn.c", + "crypto/x509v3/v3_ia5.c", + "crypto/x509v3/v3_info.c", + "crypto/x509v3/v3_int.c", + "crypto/x509v3/v3_lib.c", + "crypto/x509v3/v3_ncons.c", + "crypto/x509v3/v3_ocsp.c", + "crypto/x509v3/v3_pcons.c", + "crypto/x509v3/v3_pmaps.c", + "crypto/x509v3/v3_prn.c", + "crypto/x509v3/v3_purp.c", + "crypto/x509v3/v3_skey.c", + "crypto/x509v3/v3_utl.c", + "crypto/decrepit/bio/base64_bio.c", + "crypto/decrepit/blowfish/blowfish.c", + "crypto/decrepit/cast/cast.c", + "crypto/decrepit/cast/cast_tables.c", + "crypto/decrepit/cfb/cfb.c", + "crypto/decrepit/dh/dh_decrepit.c", + "crypto/decrepit/evp/evp_do_all.c", + "crypto/decrepit/ripemd/ripemd.c", + "crypto/decrepit/rsa/rsa_decrepit.c", + "crypto/decrepit/x509/x509_decrepit.c", + "generated-src/mac-x86_64/crypto/chacha/chacha-x86_64.S", + "generated-src/mac-x86_64/crypto/cipher_extra/chacha20_poly1305_x86_64.S", + "generated-src/mac-x86_64/crypto/cipher_extra/aes128gcmsiv-x86_64.S", + "generated-src/mac-x86_64/crypto/cipher_extra/aesni-sha1-x86_64.S", + "generated-src/mac-x86_64/crypto/cipher_extra/aesni-sha256-x86_64.S", + "generated-src/mac-x86_64/crypto/test/trampoline-x86_64.S", + "crypto/fipsmodule/bcm.c", + "crypto/fipsmodule/cpucap/cpucap.c", + "generated-src/mac-x86_64/crypto/fipsmodule/aesni-gcm-avx512.S", + "generated-src/mac-x86_64/crypto/fipsmodule/aesni-gcm-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/aesni-xts-avx512.S", + "generated-src/mac-x86_64/crypto/fipsmodule/aesni-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/ghash-ssse3-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/ghash-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/md5-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/p256-x86_64-asm.S", + "generated-src/mac-x86_64/crypto/fipsmodule/p256_beeu-x86_64-asm.S", + "generated-src/mac-x86_64/crypto/fipsmodule/rdrand-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/rsaz-avx2.S", + "generated-src/mac-x86_64/crypto/fipsmodule/sha1-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/sha256-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/sha512-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/vpaes-x86_64.S", + "generated-src/mac-x86_64/crypto/fipsmodule/x86_64-mont5.S", + "generated-src/mac-x86_64/crypto/fipsmodule/x86_64-mont.S", + "third_party/s2n-bignum/x86_att/p384/bignum_add_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_sub_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_neg_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_tomont_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_deamont_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_montmul_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_montmul_p384_alt.S", + "third_party/s2n-bignum/x86_att/p384/bignum_montsqr_p384.S", + "third_party/s2n-bignum/x86_att/p384/bignum_montsqr_p384_alt.S", + "third_party/s2n-bignum/x86_att/p384/bignum_nonzero_6.S", + "third_party/s2n-bignum/x86_att/p384/bignum_littleendian_6.S", + "third_party/s2n-bignum/x86_att/p521/bignum_add_p521.S", + "third_party/s2n-bignum/x86_att/p521/bignum_sub_p521.S", + "third_party/s2n-bignum/x86_att/p521/bignum_neg_p521.S", + "third_party/s2n-bignum/x86_att/p521/bignum_mul_p521.S", + "third_party/s2n-bignum/x86_att/p521/bignum_mul_p521_alt.S", + "third_party/s2n-bignum/x86_att/p521/bignum_sqr_p521.S", + "third_party/s2n-bignum/x86_att/p521/bignum_sqr_p521_alt.S", + "third_party/s2n-bignum/x86_att/p521/bignum_tolebytes_p521.S", + "third_party/s2n-bignum/x86_att/p521/bignum_fromlebytes_p521.S", + "third_party/s2n-bignum/x86_att/p384/bignum_tomont_p384_alt.S", + "third_party/s2n-bignum/x86_att/p384/bignum_deamont_p384_alt.S", + "third_party/s2n-bignum/x86_att/curve25519/bignum_madd_n25519.S", + "third_party/s2n-bignum/x86_att/curve25519/bignum_madd_n25519_alt.S", + "third_party/s2n-bignum/x86_att/curve25519/bignum_mod_n25519.S", + "third_party/s2n-bignum/x86_att/curve25519/bignum_neg_p25519.S", + "third_party/s2n-bignum/x86_att/curve25519/curve25519_x25519.S", + "third_party/s2n-bignum/x86_att/curve25519/curve25519_x25519_alt.S", + "third_party/s2n-bignum/x86_att/curve25519/curve25519_x25519base.S", + "third_party/s2n-bignum/x86_att/curve25519/curve25519_x25519base_alt.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_decode.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_decode_alt.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_encode.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_scalarmulbase.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_scalarmulbase_alt.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_scalarmuldouble.S", + "third_party/s2n-bignum/x86_att/curve25519/edwards25519_scalarmuldouble_alt.S", +] diff --git a/aws-lc-sys/builder/cc_builder.rs b/aws-lc-sys/builder/cc_builder.rs new file mode 100644 index 00000000000..41cffcbb1cc --- /dev/null +++ b/aws-lc-sys/builder/cc_builder.rs @@ -0,0 +1,114 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 OR ISC + +use crate::{target, OutputLibType}; +use std::path::PathBuf; + +pub(crate) struct CcBuilder { + manifest_dir: PathBuf, + out_dir: PathBuf, + build_prefix: Option, + output_lib_type: OutputLibType, +} + +use serde::Deserialize; +use std::fs; + +#[derive(Debug, Deserialize)] +struct Config { + #[serde(rename = "Library")] + libraries: Vec, +} + +#[derive(Debug, Deserialize)] +struct Library { + name: String, + flags: Vec, + sources: Vec, +} + +impl CcBuilder { + 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 target_build_config_path(&self) -> PathBuf { + self.manifest_dir + .join("builder") + .join("cc") + .join(format!("{}.toml", target())) + } +} + +impl crate::Builder for CcBuilder { + fn check_dependencies(&self) -> Result<(), String> { + if OutputLibType::Dynamic == self.output_lib_type { + // https://github.com/rust-lang/cc-rs/issues/594 + return Err("CcBuilder only supports static builds".to_string()); + } + + let build_cfg_path = self.target_build_config_path(); + if !build_cfg_path.exists() { + return Err(format!("Platform not supported: {}", target())); + } + Ok(()) + } + + fn build(&self) -> Result<(), String> { + let build_cfg_path = self.target_build_config_path(); + println!("cargo:rerun-if-changed={}", build_cfg_path.display()); + let build_cfg_str = fs::read_to_string(build_cfg_path).map_err(|x| x.to_string())?; + let build_cfg: Config = toml::from_str(&build_cfg_str).unwrap(); + + let entries = build_cfg.libraries; + for entry in &entries { + let lib = entry; + let mut cc_build = cc::Build::default(); + + cc_build + .out_dir(&self.out_dir) + .flag("-std=c99") + .flag("-Wno-unused-parameter") + .cpp(false) + .shared_flag(false) + .static_flag(true) + .include(self.manifest_dir.join("include")) + .include(self.manifest_dir.join("generated-include")) + .include(self.manifest_dir.join("aws-lc").join("include")) + .include( + self.manifest_dir + .join("aws-lc") + .join("third_party") + .join("s2n-bignum") + .join("include"), + ) + .file(self.manifest_dir.join("rust_wrapper.c")); + for source in &lib.sources { + cc_build.file(self.manifest_dir.join("aws-lc").join(source)); + } + + for flag in &lib.flags { + cc_build.flag(flag); + } + + if let Some(prefix) = &self.build_prefix { + cc_build + .define("BORINGSSL_IMPLEMENTATION", "1") + .define("BORINGSSL_PREFIX", prefix.as_str()) + .compile(format!("{}crypto", prefix.as_str()).as_str()); + } else { + cc_build.compile(&lib.name); + } + } + Ok(()) + } +} diff --git a/aws-lc-sys/builder/main.rs b/aws-lc-sys/builder/main.rs index 0b441d064ae..83bba755c2f 100644 --- a/aws-lc-sys/builder/main.rs +++ b/aws-lc-sys/builder/main.rs @@ -8,6 +8,7 @@ use std::ffi::OsStr; use std::path::{Path, PathBuf}; use std::process::Command; +use crate::cc_builder::CcBuilder; use cmake_builder::CmakeBuilder; #[cfg(any( @@ -20,6 +21,7 @@ use cmake_builder::CmakeBuilder; )) ))] mod bindgen; +mod cc_builder; mod cmake_builder; pub(crate) fn get_aws_lc_include_path(manifest_dir: &Path) -> PathBuf { @@ -55,22 +57,32 @@ enum OutputLibType { Dynamic, } +fn env_var_to_bool(name: &str) -> Option { + let build_type_result = env::var(name); + if let Ok(env_var_value) = build_type_result { + eprintln!("{name}={env_var_value}"); + // If the environment variable is set, we ignore every other factor. + let env_var_value = env_var_value.to_lowercase(); + if env_var_value.starts_with('0') + || env_var_value.starts_with('n') + || env_var_value.starts_with("off") + { + return Some(false); + } + // Otherwise, if the variable is set, assume true + return Some(true); + } + None +} + 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 { - 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(); - if build_type.starts_with('0') - || build_type.starts_with('n') - || build_type.starts_with("off") - { - // Only dynamic if the value is set and is a "negative" value - return OutputLibType::Dynamic; - } + if Some(false) == env_var_to_bool("AWS_LC_SYS_STATIC") { + // Only dynamic if the value is set and is a "negative" value + OutputLibType::Dynamic + } else { + OutputLibType::Static } - OutputLibType::Static } } @@ -188,6 +200,45 @@ fn target() -> String { env::var("TARGET").unwrap() } +fn get_builder(prefix: Option, manifest_dir: &Path, out_dir: &Path) -> Box { + let cmake_builder_builder = || { + Box::new(CmakeBuilder::new( + manifest_dir.to_path_buf(), + out_dir.to_path_buf(), + prefix.clone(), + OutputLibType::default(), + )) + }; + + let cc_builder_builder = || { + Box::new(CcBuilder::new( + manifest_dir.to_path_buf(), + out_dir.to_path_buf(), + prefix.clone(), + OutputLibType::default(), + )) + }; + + if let Some(val) = env_var_to_bool("AWS_LC_SYS_CMAKE_BUILDER") { + let builder: Box = if val { + cmake_builder_builder() + } else { + cc_builder_builder() + }; + builder.check_dependencies().unwrap(); + builder + } else { + let cc_builder = cc_builder_builder(); + if cc_builder.check_dependencies().is_ok() { + cc_builder + } else { + let cmake_builder = cmake_builder_builder(); + cmake_builder.check_dependencies().unwrap(); + cmake_builder + } + } +} + macro_rules! cfg_bindgen_platform { ($binding:ident, $os:literal, $arch:literal, $env:literal, $additional:expr) => { let $binding = { @@ -208,7 +259,6 @@ trait Builder { fn main() { 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")) @@ -231,15 +281,7 @@ fn main() { 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, - ); - - builder.check_dependencies().unwrap(); + let builder = get_builder(Some(prefix.clone()), &manifest_dir, &out_dir); #[allow(unused_assignments)] let mut bindings_available = false; @@ -283,6 +325,7 @@ fn main() { println!("cargo:rerun-if-changed=builder/"); println!("cargo:rerun-if-changed=aws-lc/"); println!("cargo:rerun-if-env-changed=AWS_LC_SYS_STATIC"); + println!("cargo:rerun-if-env-changed=AWS_LC_SYS_CMAKE_BUILDER"); } fn setup_include_paths(out_dir: &Path, manifest_dir: &Path) -> PathBuf {