| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
// NOTE: This module is intended to produce an equivalent "libcrypto" static library to the one |
| 5 |
// produced by the CMake. Changes to CMake relating to compiler checks and/or special build flags |
| 6 |
// may require modifications to the logic in this module. |
| 7 |
|
| 8 |
mod apple_aarch64; |
| 9 |
mod apple_x86_64; |
| 10 |
mod linux_aarch64; |
| 11 |
mod linux_arm; |
| 12 |
mod linux_ppc64le; |
| 13 |
mod linux_x86; |
| 14 |
mod linux_x86_64; |
| 15 |
mod universal; |
| 16 |
mod win_aarch64; |
| 17 |
mod win_x86; |
| 18 |
mod win_x86_64; |
| 19 |
|
| 20 |
use crate::nasm_builder::NasmBuilder; |
| 21 |
use crate::{ |
| 22 |
cargo_env, emit_warning, env_var_to_bool, execute_command, find_clang_cl, get_crate_cc, |
| 23 |
get_crate_cflags, get_crate_cxx, is_no_asm, out_dir, requested_c_std, set_env_for_target, |
| 24 |
should_build_jitter_entropy, target, target_arch, target_env, target_os, target_vendor, |
| 25 |
CStdRequested, EnvGuard, OutputLibType, |
| 26 |
}; |
| 27 |
use std::cell::Cell; |
| 28 |
use std::collections::HashMap; |
| 29 |
use std::env; |
| 30 |
use std::path::PathBuf; |
| 31 |
|
| 32 |
#[non_exhaustive] |
| 33 |
#[derive(PartialEq, Eq)] |
| 34 |
pub(crate) enum CompilerFeature { |
| 35 |
NeonSha3, |
| 36 |
} |
| 37 |
|
| 38 |
pub(crate) struct CcBuilder { |
| 39 |
manifest_dir: PathBuf, |
| 40 |
out_dir: PathBuf, |
| 41 |
build_prefix: Option<String>, |
| 42 |
output_lib_type: OutputLibType, |
| 43 |
compiler_features: Cell<Vec<CompilerFeature>>, |
| 44 |
} |
| 45 |
|
| 46 |
use std::fs; |
| 47 |
|
| 48 |
fn identify_sources() -> Vec<&'static str> { |
| 49 |
let mut source_files: Vec<&'static str> = vec![]; |
| 50 |
source_files.append(&mut Vec::from(universal::CRYPTO_LIBRARY)); |
| 51 |
let mut target_specific_source_found = true; |
| 52 |
if target_os() == "windows" { |
| 53 |
if target_arch() == "x86_64" { |
| 54 |
source_files.append(&mut Vec::from(win_x86_64::CRYPTO_LIBRARY)); |
| 55 |
} else if target_arch() == "aarch64" { |
| 56 |
source_files.append(&mut Vec::from(win_aarch64::CRYPTO_LIBRARY)); |
| 57 |
} else if target_arch() == "x86" { |
| 58 |
source_files.append(&mut Vec::from(win_x86::CRYPTO_LIBRARY)); |
| 59 |
} else { |
| 60 |
target_specific_source_found = false; |
| 61 |
} |
| 62 |
} else if target_vendor() == "apple" { |
| 63 |
if target_arch() == "x86_64" { |
| 64 |
source_files.append(&mut Vec::from(apple_x86_64::CRYPTO_LIBRARY)); |
| 65 |
} else if target_arch() == "aarch64" { |
| 66 |
source_files.append(&mut Vec::from(apple_aarch64::CRYPTO_LIBRARY)); |
| 67 |
} else { |
| 68 |
target_specific_source_found = false; |
| 69 |
} |
| 70 |
} else if target_arch() == "x86_64" { |
| 71 |
source_files.append(&mut Vec::from(linux_x86_64::CRYPTO_LIBRARY)); |
| 72 |
} else if target_arch() == "aarch64" { |
| 73 |
source_files.append(&mut Vec::from(linux_aarch64::CRYPTO_LIBRARY)); |
| 74 |
} else if target_arch() == "arm" { |
| 75 |
source_files.append(&mut Vec::from(linux_arm::CRYPTO_LIBRARY)); |
| 76 |
} else if target_arch() == "x86" { |
| 77 |
source_files.append(&mut Vec::from(linux_x86::CRYPTO_LIBRARY)); |
| 78 |
} else if target_arch() == "powerpc64" { |
| 79 |
source_files.append(&mut Vec::from(linux_ppc64le::CRYPTO_LIBRARY)); |
| 80 |
} else { |
| 81 |
target_specific_source_found = false; |
| 82 |
} |
| 83 |
if !target_specific_source_found { |
| 84 |
emit_warning(format!( |
| 85 |
"No target-specific source found: {}-{}", |
| 86 |
target_os(), |
| 87 |
target_arch() |
| 88 |
)); |
| 89 |
} |
| 90 |
|
| 91 |
source_files |
| 92 |
} |
| 93 |
|
| 94 |
#[allow(clippy::upper_case_acronyms)] |
| 95 |
pub(crate) enum BuildOption { |
| 96 |
STD(String), |
| 97 |
FLAG(String), |
| 98 |
DEFINE(String, String), |
| 99 |
INCLUDE(PathBuf), |
| 100 |
} |
| 101 |
impl BuildOption { |
| 102 |
fn std<T: ToString + ?Sized>(val: &T) -> Self { |
| 103 |
Self::STD(val.to_string()) |
| 104 |
} |
| 105 |
fn flag<T: ToString + ?Sized>(val: &T) -> Self { |
| 106 |
Self::FLAG(val.to_string()) |
| 107 |
} |
| 108 |
fn flag_if_supported<T: ToString + ?Sized>(cc_build: &cc::Build, flag: &T) -> Option<Self> { |
| 109 |
if let Ok(true) = cc_build.is_flag_supported(flag.to_string()) { |
| 110 |
Some(Self::FLAG(flag.to_string())) |
| 111 |
} else { |
| 112 |
None |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
fn define<K: ToString + ?Sized, V: ToString + ?Sized>(key: &K, val: &V) -> Self { |
| 117 |
Self::DEFINE(key.to_string(), val.to_string()) |
| 118 |
} |
| 119 |
|
| 120 |
fn include<P: Into<PathBuf>>(path: P) -> Self { |
| 121 |
Self::INCLUDE(path.into()) |
| 122 |
} |
| 123 |
|
| 124 |
fn apply_cc<'a>(&self, cc_build: &'a mut cc::Build) -> &'a mut cc::Build { |
| 125 |
match self { |
| 126 |
BuildOption::STD(val) => cc_build.std(val), |
| 127 |
BuildOption::FLAG(val) => cc_build.flag(val), |
| 128 |
BuildOption::DEFINE(key, val) => cc_build.define(key, Some(val.as_str())), |
| 129 |
BuildOption::INCLUDE(path) => cc_build.include(path.as_path()), |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
pub(crate) fn apply_cmake<'a>( |
| 134 |
&self, |
| 135 |
cmake_cfg: &'a mut cmake::Config, |
| 136 |
is_like_msvc: bool, |
| 137 |
) -> &'a mut cmake::Config { |
| 138 |
if is_like_msvc { |
| 139 |
match self { |
| 140 |
BuildOption::STD(val) => cmake_cfg.define( |
| 141 |
"CMAKE_C_STANDARD", |
| 142 |
val.to_ascii_lowercase().strip_prefix('c').unwrap_or("11"), |
| 143 |
), |
| 144 |
BuildOption::FLAG(val) => cmake_cfg.cflag(val), |
| 145 |
BuildOption::DEFINE(key, val) => cmake_cfg.cflag(format!("/D{key}={val}")), |
| 146 |
BuildOption::INCLUDE(path) => cmake_cfg.cflag(format!("/I{}", path.display())), |
| 147 |
} |
| 148 |
} else { |
| 149 |
match self { |
| 150 |
BuildOption::STD(val) => cmake_cfg.define( |
| 151 |
"CMAKE_C_STANDARD", |
| 152 |
val.to_ascii_lowercase().strip_prefix('c').unwrap_or("11"), |
| 153 |
), |
| 154 |
BuildOption::FLAG(val) => cmake_cfg.cflag(val), |
| 155 |
BuildOption::DEFINE(key, val) => cmake_cfg.cflag(format!("-D{key}={val}")), |
| 156 |
BuildOption::INCLUDE(path) => cmake_cfg.cflag(format!("-I{}", path.display())), |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
pub(crate) fn apply_nasm<'a>(&self, nasm_builder: &'a mut NasmBuilder) -> &'a mut NasmBuilder { |
| 162 |
match self { |
| 163 |
BuildOption::FLAG(val) => nasm_builder.flag(val), |
| 164 |
BuildOption::DEFINE(key, val) => nasm_builder.define(key, Some(val.as_str())), |
| 165 |
BuildOption::INCLUDE(path) => nasm_builder.include(path.as_path()), |
| 166 |
BuildOption::STD(_) => nasm_builder, // STD ignored for NASM |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
impl CcBuilder { |
| 172 |
pub(crate) fn new( |
| 173 |
manifest_dir: PathBuf, |
| 174 |
out_dir: PathBuf, |
| 175 |
build_prefix: Option<String>, |
| 176 |
output_lib_type: OutputLibType, |
| 177 |
) -> Self { |
| 178 |
Self { |
| 179 |
manifest_dir, |
| 180 |
out_dir, |
| 181 |
build_prefix, |
| 182 |
output_lib_type, |
| 183 |
compiler_features: Cell::new(vec![]), |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
pub(crate) fn collect_universal_build_options( |
| 188 |
&self, |
| 189 |
cc_build: &cc::Build, |
| 190 |
do_quote_paths: bool, |
| 191 |
) -> (bool, Vec<BuildOption>) { |
| 192 |
let mut build_options: Vec<BuildOption> = Vec::new(); |
| 193 |
|
| 194 |
let compiler_is_msvc = { |
| 195 |
let compiler = cc_build.get_compiler(); |
| 196 |
!compiler.is_like_gnu() && !compiler.is_like_clang() |
| 197 |
}; |
| 198 |
|
| 199 |
match requested_c_std() { |
| 200 |
CStdRequested::C99 => { |
| 201 |
build_options.push(BuildOption::std("c99")); |
| 202 |
} |
| 203 |
CStdRequested::C11 => { |
| 204 |
build_options.push(BuildOption::std("c11")); |
| 205 |
} |
| 206 |
CStdRequested::None => { |
| 207 |
if !compiler_is_msvc { |
| 208 |
if self.compiler_check("c11", Vec::<String>::new()) { |
| 209 |
build_options.push(BuildOption::std("c11")); |
| 210 |
} else { |
| 211 |
build_options.push(BuildOption::std("c99")); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if let Some(cc) = get_crate_cc() { |
| 218 |
set_env_for_target("CC", &cc); |
| 219 |
} |
| 220 |
if let Some(cxx) = get_crate_cxx() { |
| 221 |
set_env_for_target("CXX", &cxx); |
| 222 |
} |
| 223 |
|
| 224 |
if target_arch() == "x86" && !compiler_is_msvc { |
| 225 |
if let Some(option) = BuildOption::flag_if_supported(cc_build, "-msse2") { |
| 226 |
build_options.push(option); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if target_os() == "macos" || target_os() == "darwin" { |
| 231 |
// Certain MacOS system headers are guarded by _POSIX_C_SOURCE and _DARWIN_C_SOURCE |
| 232 |
build_options.push(BuildOption::define("_DARWIN_C_SOURCE", "1")); |
| 233 |
} |
| 234 |
|
| 235 |
let opt_level = cargo_env("OPT_LEVEL"); |
| 236 |
match opt_level.as_str() { |
| 237 |
"0" | "1" | "2" => { |
| 238 |
if is_no_asm() { |
| 239 |
emit_warning("AWS_LC_SYS_NO_ASM found. Disabling assembly code usage."); |
| 240 |
build_options.push(BuildOption::define("OPENSSL_NO_ASM", "1")); |
| 241 |
} |
| 242 |
} |
| 243 |
_ => { |
| 244 |
assert!( |
| 245 |
!is_no_asm(), |
| 246 |
"AWS_LC_SYS_NO_ASM only allowed for debug builds!" |
| 247 |
); |
| 248 |
if !compiler_is_msvc { |
| 249 |
let path_str = if do_quote_paths { |
| 250 |
format!("\"{}\"", self.manifest_dir.display()) |
| 251 |
} else { |
| 252 |
format!("{}", self.manifest_dir.display()) |
| 253 |
}; |
| 254 |
|
| 255 |
let flag = format!("-ffile-prefix-map={path_str}="); |
| 256 |
if let Ok(true) = cc_build.is_flag_supported(&flag) { |
| 257 |
emit_warning(format!("Using flag: {flag}")); |
| 258 |
build_options.push(BuildOption::flag(&flag)); |
| 259 |
} else { |
| 260 |
emit_warning("NOTICE: Build environment source paths might be visible in release binary."); |
| 261 |
let flag = format!("-fdebug-prefix-map={path_str}="); |
| 262 |
if let Ok(true) = cc_build.is_flag_supported(&flag) { |
| 263 |
emit_warning(format!("Using flag: {flag}")); |
| 264 |
build_options.push(BuildOption::flag(&flag)); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if target_os() == "macos" { |
| 272 |
// This compiler error has only been seen on MacOS x86_64: |
| 273 |
// ``` |
| 274 |
// clang: error: overriding '-mmacosx-version-min=13.7' option with '--target=x86_64-apple-macosx14.2' [-Werror,-Woverriding-t-option] |
| 275 |
// ``` |
| 276 |
if let Some(option) = |
| 277 |
BuildOption::flag_if_supported(cc_build, "-Wno-overriding-t-option") |
| 278 |
{ |
| 279 |
build_options.push(option); |
| 280 |
} |
| 281 |
if let Some(option) = BuildOption::flag_if_supported(cc_build, "-Wno-overriding-option") |
| 282 |
{ |
| 283 |
build_options.push(option); |
| 284 |
} |
| 285 |
} |
| 286 |
(compiler_is_msvc, build_options) |
| 287 |
} |
| 288 |
|
| 289 |
pub fn collect_cc_only_build_options(&self, cc_build: &cc::Build) -> Vec<BuildOption> { |
| 290 |
let mut build_options: Vec<BuildOption> = Vec::new(); |
| 291 |
let is_like_msvc = { |
| 292 |
let compiler = cc_build.get_compiler(); |
| 293 |
!compiler.is_like_gnu() && !compiler.is_like_clang() |
| 294 |
}; |
| 295 |
if !is_like_msvc { |
| 296 |
build_options.push(BuildOption::flag("-Wno-unused-parameter")); |
| 297 |
build_options.push(BuildOption::flag("-pthread")); |
| 298 |
if target_os() == "linux" { |
| 299 |
build_options.push(BuildOption::define("_XOPEN_SOURCE", "700")); |
| 300 |
} else if target_vendor() != "apple" { |
| 301 |
// Needed by illumos |
| 302 |
build_options.push(BuildOption::define("__EXTENSIONS__", "1")); |
| 303 |
} |
| 304 |
} |
| 305 |
if !should_build_jitter_entropy() { |
| 306 |
build_options.push(BuildOption::define("DISABLE_CPU_JITTER_ENTROPY", "1")); |
| 307 |
} |
| 308 |
self.add_includes(&mut build_options); |
| 309 |
self.add_defines(&mut build_options, is_like_msvc); |
| 310 |
|
| 311 |
build_options |
| 312 |
} |
| 313 |
|
| 314 |
fn add_includes(&self, build_options: &mut Vec<BuildOption>) { |
| 315 |
// The order of includes matters |
| 316 |
if let Some(prefix) = &self.build_prefix { |
| 317 |
build_options.push(BuildOption::define("BORINGSSL_IMPLEMENTATION", "1")); |
| 318 |
build_options.push(BuildOption::define("BORINGSSL_PREFIX", prefix.as_str())); |
| 319 |
build_options.push(BuildOption::include( |
| 320 |
self.manifest_dir.join("generated-include"), |
| 321 |
)); |
| 322 |
} |
| 323 |
build_options.push(BuildOption::include(self.manifest_dir.join("include"))); |
| 324 |
build_options.push(BuildOption::include( |
| 325 |
self.manifest_dir.join("aws-lc").join("include"), |
| 326 |
)); |
| 327 |
build_options.push(BuildOption::include( |
| 328 |
self.manifest_dir |
| 329 |
.join("aws-lc") |
| 330 |
.join("third_party") |
| 331 |
.join("s2n-bignum") |
| 332 |
.join("include"), |
| 333 |
)); |
| 334 |
build_options.push(BuildOption::include( |
| 335 |
self.manifest_dir |
| 336 |
.join("aws-lc") |
| 337 |
.join("third_party") |
| 338 |
.join("s2n-bignum") |
| 339 |
.join("s2n-bignum-imported") |
| 340 |
.join("include"), |
| 341 |
)); |
| 342 |
|
| 343 |
if should_build_jitter_entropy() { |
| 344 |
let jitterentropy_path = self |
| 345 |
.manifest_dir |
| 346 |
.join("aws-lc") |
| 347 |
.join("third_party") |
| 348 |
.join("jitterentropy") |
| 349 |
.join("jitterentropy-library"); |
| 350 |
|
| 351 |
build_options.push(BuildOption::include(&jitterentropy_path)); |
| 352 |
build_options.push(BuildOption::include(jitterentropy_path.join("src"))); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
pub fn create_builder(&self) -> cc::Build { |
| 357 |
let mut cc_build = cc::Build::new(); |
| 358 |
let build_options = self.collect_cc_only_build_options(&cc_build); |
| 359 |
for option in build_options { |
| 360 |
option.apply_cc(&mut cc_build); |
| 361 |
} |
| 362 |
cc_build |
| 363 |
} |
| 364 |
|
| 365 |
pub fn prepare_builder(&self) -> cc::Build { |
| 366 |
if let Some(cflags) = get_crate_cflags() { |
| 367 |
set_env_for_target("CFLAGS", cflags); |
| 368 |
} |
| 369 |
|
| 370 |
let mut cc_build = self.create_builder(); |
| 371 |
let (_, build_options) = self.collect_universal_build_options(&cc_build, false); |
| 372 |
for option in build_options { |
| 373 |
option.apply_cc(&mut cc_build); |
| 374 |
} |
| 375 |
|
| 376 |
// Add --noexecstack flag for assembly files to prevent executable stacks |
| 377 |
// This matches the behavior of AWS-LC's CMake build which uses -Wa,--noexecstack |
| 378 |
// See: https://github.com/aws/aws-lc/blob/main/crypto/CMakeLists.txt#L77 |
| 379 |
if target_os() == "linux" || target_os().ends_with("bsd") { |
| 380 |
cc_build.asm_flag("-Wa,--noexecstack"); |
| 381 |
} |
| 382 |
|
| 383 |
cc_build |
| 384 |
} |
| 385 |
|
| 386 |
#[allow(clippy::zero_sized_map_values)] |
| 387 |
fn build_s2n_bignum_source_feature_map() -> HashMap<String, CompilerFeature> { |
| 388 |
let mut source_feature_map: HashMap<String, CompilerFeature> = HashMap::new(); |
| 389 |
source_feature_map.insert("sha3_keccak_f1600_alt.S".into(), CompilerFeature::NeonSha3); |
| 390 |
source_feature_map.insert("sha3_keccak2_f1600.S".into(), CompilerFeature::NeonSha3); |
| 391 |
source_feature_map.insert( |
| 392 |
"sha3_keccak4_f1600_alt2.S".into(), |
| 393 |
CompilerFeature::NeonSha3, |
| 394 |
); |
| 395 |
source_feature_map |
| 396 |
} |
| 397 |
|
| 398 |
#[allow(clippy::unused_self)] |
| 399 |
fn add_defines(&self, build_options: &mut Vec<BuildOption>, is_like_msvc: bool) { |
| 400 |
// WIN32_LEAN_AND_MEAN and NOMINMAX are needed for all Windows targets to avoid |
| 401 |
// header type definition errors, no matter the compiler. This matches the behavior |
| 402 |
// in aws-lc/CMakeLists.txt, which defines these for all WIN32 targets |
| 403 |
if target_os() == "windows" { |
| 404 |
build_options.push(BuildOption::define("WIN32_LEAN_AND_MEAN", "")); |
| 405 |
build_options.push(BuildOption::define("NOMINMAX", "")); |
| 406 |
} |
| 407 |
|
| 408 |
if is_like_msvc { |
| 409 |
build_options.push(BuildOption::define("_HAS_EXCEPTIONS", "0")); |
| 410 |
build_options.push(BuildOption::define("_CRT_SECURE_NO_WARNINGS", "0")); |
| 411 |
build_options.push(BuildOption::define( |
| 412 |
"_STL_EXTRA_DISABLED_WARNINGS", |
| 413 |
"4774 4987", |
| 414 |
)); |
| 415 |
} |
| 416 |
|
| 417 |
// Target Windows 7 (0x0601 == _WIN32_WINNT_WIN7) for any win7 target triple. |
| 418 |
if target().contains("-win7-windows-") { |
| 419 |
build_options.push(BuildOption::define("_WIN32_WINNT", "0x0601")); |
| 420 |
emit_warning(format!( |
| 421 |
"Setting _WIN32_WINNT to _WIN32_WINNT_WIN7 for {} target", |
| 422 |
target() |
| 423 |
)); |
| 424 |
|
| 425 |
// Additional workaround for MinGW: the upstream C source |
| 426 |
// (crypto/rand_extra/windows.c) gates the Win7 compat path |
| 427 |
// (BCryptGenRandom) with `!defined(__MINGW32__)`, which prevents MinGW |
| 428 |
// from using it even when `_WIN32_WINNT` targets Win7. We define |
| 429 |
// AWSLC_WINDOWS_7_COMPAT directly to bypass that guard until the |
| 430 |
// upstream fix lands: https://github.com/aws/aws-lc/pull/3239 |
| 431 |
if !is_like_msvc { |
| 432 |
build_options.push(BuildOption::define("AWSLC_WINDOWS_7_COMPAT", "")); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
fn prepare_jitter_entropy_builder(&self, is_like_msvc: bool) -> cc::Build { |
| 438 |
// See: https://github.com/aws/aws-lc/blob/2294510cd0ecb2d5946461e3dbb038363b7b94cb/third_party/jitterentropy/CMakeLists.txt#L19-L35 |
| 439 |
let mut build_options: Vec<BuildOption> = Vec::new(); |
| 440 |
self.add_includes(&mut build_options); |
| 441 |
self.add_defines(&mut build_options, is_like_msvc); |
| 442 |
|
| 443 |
let mut je_builder = cc::Build::new(); |
| 444 |
for option in build_options { |
| 445 |
option.apply_cc(&mut je_builder); |
| 446 |
} |
| 447 |
|
| 448 |
je_builder.define("AWSLC", "1"); |
| 449 |
if target_os() == "macos" || target_os() == "darwin" { |
| 450 |
// Certain MacOS system headers are guarded by _POSIX_C_SOURCE and _DARWIN_C_SOURCE |
| 451 |
je_builder.define("_DARWIN_C_SOURCE", "1"); |
| 452 |
} |
| 453 |
// Only enable PIC on non-Windows targets. Windows doesn't support -fPIC. |
| 454 |
if target_os() != "windows" { |
| 455 |
je_builder.pic(true); |
| 456 |
} |
| 457 |
if is_like_msvc { |
| 458 |
je_builder.flag("-Od").flag("-W4").flag("-DYNAMICBASE"); |
| 459 |
} else { |
| 460 |
je_builder |
| 461 |
.flag("-fwrapv") |
| 462 |
.flag("--param") |
| 463 |
.flag("ssp-buffer-size=4") |
| 464 |
.flag("-fvisibility=hidden") |
| 465 |
.flag("-Wcast-align") |
| 466 |
.flag("-Wmissing-field-initializers") |
| 467 |
.flag("-Wshadow") |
| 468 |
.flag("-Wswitch-enum") |
| 469 |
.flag("-Wextra") |
| 470 |
.flag("-Wall") |
| 471 |
.flag("-pedantic") |
| 472 |
// Compilation will fail if optimizations are enabled. |
| 473 |
.flag("-O0") |
| 474 |
.flag("-fwrapv") |
| 475 |
.flag("-Wconversion"); |
| 476 |
} |
| 477 |
je_builder |
| 478 |
} |
| 479 |
|
| 480 |
/// The cc crate appends CFLAGS at the end of the compiler command line, |
| 481 |
/// which means CFLAGS optimization flags override build script flags. |
| 482 |
/// Jitterentropy MUST be compiled with -O0, so we temporarily override |
| 483 |
/// CFLAGS to replace any optimization flags with -O0. |
| 484 |
/// |
| 485 |
/// The cc crate collects flags from ALL matching CFLAGS env vars (not just |
| 486 |
/// the highest-priority one), so we must filter every variable it checks: |
| 487 |
/// 1. `CFLAGS_{target}` (e.g. `CFLAGS_x86_64_unknown_freebsd`) |
| 488 |
/// 2. `HOST_CFLAGS` or `TARGET_CFLAGS` |
| 489 |
/// 3. `CFLAGS` |
| 490 |
fn jitter_entropy_cflags_guards(is_like_msvc: bool) -> Vec<EnvGuard> { |
| 491 |
let target_u = target().to_lowercase().replace('-', "_"); |
| 492 |
|
| 493 |
let cflags_env_names: Vec<String> = vec![ |
| 494 |
format!("CFLAGS_{target_u}"), |
| 495 |
"HOST_CFLAGS".to_string(), |
| 496 |
"TARGET_CFLAGS".to_string(), |
| 497 |
"CFLAGS".to_string(), |
| 498 |
]; |
| 499 |
|
| 500 |
let filter_cflags = |value: &str| -> String { |
| 501 |
let filtered: String = value |
| 502 |
.split_whitespace() |
| 503 |
.filter(|flag| !flag.starts_with("-O") && !flag.starts_with("/O")) |
| 504 |
.collect::<Vec<_>>() |
| 505 |
.join(" "); |
| 506 |
if is_like_msvc { |
| 507 |
format!("{filtered} -Od").trim().to_string() |
| 508 |
} else { |
| 509 |
format!("{filtered} -O0 -U_FORTIFY_SOURCE") |
| 510 |
.trim() |
| 511 |
.to_string() |
| 512 |
} |
| 513 |
}; |
| 514 |
|
| 515 |
cflags_env_names |
| 516 |
.into_iter() |
| 517 |
.filter_map(|name| { |
| 518 |
let value = env::var(&name).ok()?; |
| 519 |
Some(EnvGuard::new(&name, filter_cflags(&value))) |
| 520 |
}) |
| 521 |
.collect() |
| 522 |
} |
| 523 |
|
| 524 |
fn add_all_files(&self, sources: &[&'static str], cc_build: &mut cc::Build) { |
| 525 |
let compiler = cc_build.get_compiler(); |
| 526 |
|
| 527 |
let force_include_option = if compiler.is_like_msvc() { |
| 528 |
"/FI" |
| 529 |
} else { |
| 530 |
"--include=" |
| 531 |
}; |
| 532 |
// s2n-bignum is compiled separately due to needing extra flags |
| 533 |
let mut s2n_bignum_builder = cc_build.clone(); |
| 534 |
s2n_bignum_builder.flag(format!( |
| 535 |
"{}{}", |
| 536 |
force_include_option, |
| 537 |
self.manifest_dir |
| 538 |
.join("generated-include") |
| 539 |
.join("openssl") |
| 540 |
.join("boringssl_prefix_symbols_asm.h") |
| 541 |
.display() |
| 542 |
)); |
| 543 |
s2n_bignum_builder.define("S2N_BN_HIDE_SYMBOLS", "1"); |
| 544 |
|
| 545 |
// CPU Jitter Entropy is compiled separately due to needing specific flags |
| 546 |
let mut jitter_entropy_builder = |
| 547 |
self.prepare_jitter_entropy_builder(compiler.is_like_msvc()); |
| 548 |
jitter_entropy_builder.flag(format!( |
| 549 |
"{}{}", |
| 550 |
force_include_option, |
| 551 |
self.manifest_dir |
| 552 |
.join("generated-include") |
| 553 |
.join("openssl") |
| 554 |
.join("boringssl_prefix_symbols.h") |
| 555 |
.display() |
| 556 |
)); |
| 557 |
|
| 558 |
let mut build_options = vec![]; |
| 559 |
self.add_includes(&mut build_options); |
| 560 |
let mut nasm_builder = NasmBuilder::new(self.manifest_dir.clone(), self.out_dir.clone()); |
| 561 |
|
| 562 |
for option in &build_options { |
| 563 |
option.apply_nasm(&mut nasm_builder); |
| 564 |
} |
| 565 |
|
| 566 |
let s2n_bignum_source_feature_map = Self::build_s2n_bignum_source_feature_map(); |
| 567 |
let compiler_features = self.compiler_features.take(); |
| 568 |
for source in sources { |
| 569 |
let source_path = self.manifest_dir.join("aws-lc").join(source); |
| 570 |
let is_s2n_bignum = std::path::Path::new(source).starts_with("third_party/s2n-bignum"); |
| 571 |
let is_jitter_entropy = |
| 572 |
std::path::Path::new(source).starts_with("third_party/jitterentropy"); |
| 573 |
|
| 574 |
if !source_path.is_file() { |
| 575 |
emit_warning(format!("Not a file: {:?}", source_path.as_os_str())); |
| 576 |
continue; |
| 577 |
} |
| 578 |
if is_s2n_bignum { |
| 579 |
let filename: String = source_path |
| 580 |
.file_name() |
| 581 |
.unwrap() |
| 582 |
.to_str() |
| 583 |
.unwrap() |
| 584 |
.to_string(); |
| 585 |
|
| 586 |
if let Some(compiler_feature) = s2n_bignum_source_feature_map.get(&filename) { |
| 587 |
if compiler_features.contains(compiler_feature) { |
| 588 |
s2n_bignum_builder.file(source_path); |
| 589 |
} else { |
| 590 |
emit_warning(format!( |
| 591 |
"Skipping due to missing compiler features: {:?}", |
| 592 |
source_path.as_os_str() |
| 593 |
)); |
| 594 |
} |
| 595 |
} else { |
| 596 |
s2n_bignum_builder.file(source_path); |
| 597 |
} |
| 598 |
} else if is_jitter_entropy { |
| 599 |
// Only compile if not disabled. |
| 600 |
if should_build_jitter_entropy() { |
| 601 |
jitter_entropy_builder.file(source_path); |
| 602 |
} |
| 603 |
} else if source_path.extension() == Some("asm".as_ref()) { |
| 604 |
nasm_builder.file(source_path); |
| 605 |
} else { |
| 606 |
cc_build.file(source_path); |
| 607 |
} |
| 608 |
} |
| 609 |
self.compiler_features.set(compiler_features); |
| 610 |
let s2n_bignum_object_files = s2n_bignum_builder.compile_intermediates(); |
| 611 |
for object in s2n_bignum_object_files { |
| 612 |
cc_build.object(object); |
| 613 |
} |
| 614 |
if should_build_jitter_entropy() { |
| 615 |
let _je_cflags_guards = Self::jitter_entropy_cflags_guards(compiler.is_like_msvc()); |
| 616 |
let jitter_entropy_object_files = jitter_entropy_builder.compile_intermediates(); |
| 617 |
for object in jitter_entropy_object_files { |
| 618 |
cc_build.object(object); |
| 619 |
} |
| 620 |
} |
| 621 |
let nasm_object_files = nasm_builder.compile_intermediates(); |
| 622 |
for object in nasm_object_files { |
| 623 |
cc_build.object(object); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
fn build_library(&self, sources: &[&'static str]) { |
| 628 |
let mut cc_build = self.prepare_builder(); |
| 629 |
self.run_compiler_checks(&mut cc_build); |
| 630 |
|
| 631 |
self.add_all_files(sources, &mut cc_build); |
| 632 |
if let Some(prefix) = &self.build_prefix { |
| 633 |
cc_build.compile(format!("{}_crypto", prefix.as_str()).as_str()); |
| 634 |
} else { |
| 635 |
cc_build.compile("crypto"); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
// This performs basic checks of compiler capabilities and sets an appropriate flag on success. |
| 640 |
// This should be kept in alignment with the checks performed by AWS-LC's CMake build. |
| 641 |
// See: https://github.com/search?q=repo%3Aaws%2Faws-lc%20check_compiler&type=code |
| 642 |
fn compiler_check<T, S>(&self, basename: &str, extra_flags: T) -> bool |
| 643 |
where |
| 644 |
T: IntoIterator<Item = S>, |
| 645 |
S: AsRef<str>, |
| 646 |
{ |
| 647 |
let mut ret_val = false; |
| 648 |
let output_dir = self.out_dir.join(format!("out-{basename}")); |
| 649 |
let source_file = self |
| 650 |
.manifest_dir |
| 651 |
.join("aws-lc") |
| 652 |
.join("tests") |
| 653 |
.join("compiler_features_tests") |
| 654 |
.join(format!("{basename}.c")); |
| 655 |
if !source_file.exists() { |
| 656 |
emit_warning("######"); |
| 657 |
emit_warning("###### WARNING: MISSING GIT SUBMODULE ######"); |
| 658 |
emit_warning(format!( |
| 659 |
" -- Did you initialize the repo's git submodules? Unable to find source file: {}.", |
| 660 |
source_file.display() |
| 661 |
)); |
| 662 |
emit_warning(" -- run 'git submodule update --init --recursive' to initialize."); |
| 663 |
emit_warning("######"); |
| 664 |
emit_warning("######"); |
| 665 |
} |
| 666 |
let mut cc_build = cc::Build::default(); |
| 667 |
cc_build |
| 668 |
.file(source_file) |
| 669 |
.warnings_into_errors(true) |
| 670 |
.out_dir(&output_dir); |
| 671 |
for flag in extra_flags { |
| 672 |
let flag = flag.as_ref(); |
| 673 |
cc_build.flag(flag); |
| 674 |
} |
| 675 |
|
| 676 |
let compiler = cc_build.get_compiler(); |
| 677 |
if compiler.is_like_gnu() || compiler.is_like_clang() { |
| 678 |
cc_build.flag("-Wno-unused-parameter"); |
| 679 |
} |
| 680 |
let result = cc_build.try_compile_intermediates(); |
| 681 |
|
| 682 |
if result.is_ok() { |
| 683 |
ret_val = true; |
| 684 |
} |
| 685 |
if fs::remove_dir_all(&output_dir).is_err() { |
| 686 |
emit_warning(format!("Failed to remove {}", output_dir.display())); |
| 687 |
} |
| 688 |
emit_warning(format!( |
| 689 |
"Compilation of '{basename}.c' {} - {:?}.", |
| 690 |
if ret_val { "succeeded" } else { "failed" }, |
| 691 |
result |
| 692 |
)); |
| 693 |
ret_val |
| 694 |
} |
| 695 |
|
| 696 |
// This checks whether the compiler contains a critical bug that causes `memcmp` to erroneously |
| 697 |
// consider two regions of memory to be equal when they're not. |
| 698 |
// See GCC bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189 |
| 699 |
// This should be kept in alignment with the same check performed by the CMake build. |
| 700 |
// See: https://github.com/search?q=repo%3Aaws%2Faws-lc%20check_run&type=code |
| 701 |
fn memcmp_check(&self) { |
| 702 |
// This check compiles, links, and executes a test program. When cross-compiling |
| 703 |
// (HOST != TARGET), we cannot execute the resulting binary, so we skip this check. |
| 704 |
// This also avoids linker configuration issues with cross-compilation toolchains |
| 705 |
// (e.g., cross-rs Darwin toolchains that set invalid -fuse-ld= flags in CFLAGS). |
| 706 |
if cargo_env("HOST") != target() { |
| 707 |
return; |
| 708 |
} |
| 709 |
|
| 710 |
let basename = "memcmp_invalid_stripped_check"; |
| 711 |
let exec_path = out_dir().join(basename); |
| 712 |
let memcmp_build = cc::Build::default(); |
| 713 |
let memcmp_compiler = memcmp_build.get_compiler(); |
| 714 |
if !memcmp_compiler.is_like_clang() && !memcmp_compiler.is_like_gnu() { |
| 715 |
// The logic below assumes a Clang or GCC compiler is in use |
| 716 |
return; |
| 717 |
} |
| 718 |
let mut memcmp_compile_args = Vec::from(memcmp_compiler.args()); |
| 719 |
|
| 720 |
// This check invokes the compiled executable and hence needs to link |
| 721 |
// it. CMake handles this via LDFLAGS but `cc` doesn't. In setups with |
| 722 |
// custom linker setups this could lead to a mismatch between the |
| 723 |
// expected and the actually used linker. Explicitly respecting LDFLAGS |
| 724 |
// here brings us back to parity with CMake. |
| 725 |
if let Ok(ldflags) = std::env::var("LDFLAGS") { |
| 726 |
for flag in ldflags.split_whitespace() { |
| 727 |
memcmp_compile_args.push(flag.into()); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
memcmp_compile_args.push( |
| 732 |
self.manifest_dir |
| 733 |
.join("aws-lc") |
| 734 |
.join("tests") |
| 735 |
.join("compiler_features_tests") |
| 736 |
.join(format!("{basename}.c")) |
| 737 |
.into_os_string(), |
| 738 |
); |
| 739 |
memcmp_compile_args.push("-Wno-unused-parameter".into()); |
| 740 |
memcmp_compile_args.push("-o".into()); |
| 741 |
memcmp_compile_args.push(exec_path.clone().into_os_string()); |
| 742 |
let memcmp_args: Vec<_> = memcmp_compile_args |
| 743 |
.iter() |
| 744 |
.map(std::ffi::OsString::as_os_str) |
| 745 |
.collect(); |
| 746 |
let memcmp_compile_result = |
| 747 |
execute_command(memcmp_compiler.path().as_os_str(), memcmp_args.as_slice()); |
| 748 |
assert!( |
| 749 |
memcmp_compile_result.status, |
| 750 |
"COMPILER: {}\ |
| 751 |
ARGS: {:?}\ |
| 752 |
EXECUTED: {}\ |
| 753 |
ERROR: {}\ |
| 754 |
OUTPUT: {}\ |
| 755 |
Failed to compile {basename} |
| 756 |
", |
| 757 |
memcmp_compiler.path().display(), |
| 758 |
memcmp_args.as_slice(), |
| 759 |
memcmp_compile_result.executed, |
| 760 |
memcmp_compile_result.stderr, |
| 761 |
memcmp_compile_result.stdout |
| 762 |
); |
| 763 |
|
| 764 |
let result = execute_command(exec_path.as_os_str(), &[]); |
| 765 |
assert!( |
| 766 |
result.status, |
| 767 |
"### COMPILER BUG DETECTED ###\nYour compiler ({}) is not supported due to a memcmp related bug reported in \ |
| 768 |
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189. \ |
| 769 |
We strongly recommend against using this compiler. \n\ |
| 770 |
EXECUTED: {}\n\ |
| 771 |
ERROR: {}\n\ |
| 772 |
OUTPUT: {}\n\ |
| 773 |
", |
| 774 |
memcmp_compiler.path().display(), |
| 775 |
memcmp_compile_result.executed, |
| 776 |
memcmp_compile_result.stderr, |
| 777 |
memcmp_compile_result.stdout |
| 778 |
); |
| 779 |
let _ = fs::remove_file(exec_path); |
| 780 |
} |
| 781 |
fn run_compiler_checks(&self, cc_build: &mut cc::Build) { |
| 782 |
if self.compiler_check("stdalign_check", Vec::<&'static str>::new()) { |
| 783 |
cc_build.define("AWS_LC_STDALIGN_AVAILABLE", Some("1")); |
| 784 |
} |
| 785 |
// Only run builtin_swap_check for GCC/Clang (matching CMake). On MSVC, |
| 786 |
// try_compile_intermediates succeeds but __builtin_bswap* are unresolved at |
| 787 |
// link time. Without this guard the define is set and crypto/internal.h skips |
| 788 |
// the correct _MSC_VER path that uses _byteswap_* intrinsics. |
| 789 |
if target_env() != "msvc" |
| 790 |
&& self.compiler_check("builtin_swap_check", Vec::<&'static str>::new()) |
| 791 |
{ |
| 792 |
cc_build.define("AWS_LC_BUILTIN_SWAP_SUPPORTED", Some("1")); |
| 793 |
} |
| 794 |
if target_arch() == "aarch64" |
| 795 |
&& self.compiler_check("neon_sha3_check", vec!["-march=armv8.4-a+sha3"]) |
| 796 |
{ |
| 797 |
let mut compiler_features = self.compiler_features.take(); |
| 798 |
compiler_features.push(CompilerFeature::NeonSha3); |
| 799 |
self.compiler_features.set(compiler_features); |
| 800 |
cc_build.define("MY_ASSEMBLER_SUPPORTS_NEON_SHA3_EXTENSION", Some("1")); |
| 801 |
} |
| 802 |
if target_os() == "linux" || target_os() == "android" { |
| 803 |
if self.compiler_check("linux_random_h", Vec::<&'static str>::new()) { |
| 804 |
cc_build.define("HAVE_LINUX_RANDOM_H", Some("1")); |
| 805 |
} else if self.compiler_check("linux_random_h", vec!["-DDEFINE_U32"]) { |
| 806 |
cc_build.define("HAVE_LINUX_RANDOM_H", Some("1")); |
| 807 |
cc_build.define("AWS_LC_URANDOM_NEEDS_U32", Some("1")); |
| 808 |
} |
| 809 |
} |
| 810 |
self.memcmp_check(); |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
impl crate::Builder for CcBuilder { |
| 815 |
fn check_dependencies(&self) -> Result<(), String> { |
| 816 |
if OutputLibType::Dynamic == self.output_lib_type { |
| 817 |
// https://github.com/rust-lang/cc-rs/issues/594 |
| 818 |
return Err("CcBuilder only supports static builds".to_string()); |
| 819 |
} |
| 820 |
|
| 821 |
if target_env() == "ohos" { |
| 822 |
return Err("OpenHarmony targets must be built with CMake.".to_string()); |
| 823 |
} |
| 824 |
|
| 825 |
if Some(true) == env_var_to_bool("CARGO_FEATURE_SSL") { |
| 826 |
return Err("cc_builder for libssl not supported".to_string()); |
| 827 |
} |
| 828 |
|
| 829 |
Ok(()) |
| 830 |
} |
| 831 |
|
| 832 |
fn build(&self) -> Result<(), String> { |
| 833 |
if target_os() == "windows" |
| 834 |
&& target_arch() == "aarch64" |
| 835 |
&& target_env() == "msvc" |
| 836 |
&& get_crate_cc().is_none() |
| 837 |
{ |
| 838 |
if let Some(clang_cl) = find_clang_cl() { |
| 839 |
set_env_for_target("CC", clang_cl); |
| 840 |
} else { |
| 841 |
emit_warning( |
| 842 |
"Windows ARM64 (aarch64-pc-windows-msvc) requires clang-cl. \ |
| 843 |
Install the 'C++ Clang Compiler for Windows' component in \ |
| 844 |
Visual Studio Build Tools, or set CC to a working clang-cl. \ |
| 845 |
See User Guide: https://aws.github.io/aws-lc-rs/index.html", |
| 846 |
); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
println!("cargo:root={}", self.out_dir.display()); |
| 851 |
let sources = crate::cc_builder::identify_sources(); |
| 852 |
self.build_library(sources.as_slice()); |
| 853 |
Ok(()) |
| 854 |
} |
| 855 |
|
| 856 |
fn name(&self) -> &'static str { |
| 857 |
"CC" |
| 858 |
} |
| 859 |
} |