Skip to content

Commit

Permalink
Improve alignment of CC and CMake builds
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Sep 23, 2024
1 parent 5565584 commit edee779
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 58 deletions.
79 changes: 49 additions & 30 deletions aws-lc-sys/builder/cc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ mod x86_64_unknown_linux_gnu;
mod x86_64_unknown_linux_musl;

use crate::{
cargo_env, emit_warning, env_var_to_bool, execute_command, get_cflags, out_dir,
requested_c_std, target, target_arch, target_os, target_vendor, CStdRequested, OutputLibType,
cargo_env, emit_warning, env_var_to_bool, execute_command, get_cflags, is_no_asm, option_env,
out_dir, requested_c_std, target, target_arch, target_env, target_os, target_vendor,
CStdRequested, OutputLibType,
};
use std::path::PathBuf;

Expand Down Expand Up @@ -96,25 +97,35 @@ impl CcBuilder {
}
}

fn apply_c_std(cc_build: &mut cc::Build) {
pub(crate) fn create_builder(&self) -> cc::Build {
let mut cc_build = cc::Build::default();
cc_build.out_dir(&self.out_dir).cpp(false);

let compiler = cc_build.get_compiler();
if compiler.is_like_gnu() || compiler.is_like_clang() {
cc_build.flag("-Wno-unused-parameter");
if target_os() == "linux" || target_env() == "gnu" {
cc_build.define("_XOPEN_SOURCE", "700").flag("-lpthread");
}
}

match requested_c_std() {
CStdRequested::C99 => cc_build.std("c99"),
_ => cc_build.std("c11"),
CStdRequested::C99 => {
cc_build.std("c99");
}
CStdRequested::C11 => {
cc_build.std("c11");
}
CStdRequested::None => {} // Use compiler default
};
}

fn create_builder(&self) -> cc::Build {
let mut cc_build = cc::Build::default();
cc_build
.out_dir(&self.out_dir)
.flag("-Wno-unused-parameter")
.cpp(false)
.shared_flag(false)
.static_flag(true);
CcBuilder::apply_c_std(&mut cc_build);
if target_os() == "linux" {
cc_build.define("_XOPEN_SOURCE", "700").flag("-lpthread");
if let Some(cc) = option_env("CC") {
emit_warning(&format!("CC environment variable set: {}", cc.clone()));
}
if let Some(cxx) = option_env("CXX") {
emit_warning(&format!("CXX environment variable set: {}", cxx.clone()));
}

if let Some(prefix) = &self.build_prefix {
cc_build
.define("BORINGSSL_IMPLEMENTATION", "1")
Expand All @@ -128,26 +139,34 @@ impl CcBuilder {

let opt_level = cargo_env("OPT_LEVEL");
match opt_level.as_str() {
"0" | "1" | "2" => {}
"0" | "1" | "2" => {
if is_no_asm() {
cc_build.define("OPENSSL_NO_ASM", "1");
}
}
_ => {
let file_prefix_map_option =
format!("-ffile-prefix-map={}=", self.manifest_dir.display());
if let Ok(true) = cc_build.is_flag_supported(&file_prefix_map_option) {
cc_build.flag(file_prefix_map_option);
} else {
cc_build.flag_if_supported(format!(
"-fdebug-prefix-map={}=",
self.manifest_dir.display()
));
assert!(
!is_no_asm(),
"AWS_LC_SYS_NO_ASM only allowed for debug builds!"
);
if compiler.is_like_gnu() || compiler.is_like_clang() {
let file_prefix_map_option =
format!("-ffile-prefix-map={}=", self.manifest_dir.display());
if let Ok(true) = cc_build.is_flag_supported(&file_prefix_map_option) {
cc_build.flag(file_prefix_map_option);
} else {
cc_build.flag_if_supported(format!(
"-fdebug-prefix-map={}=",
self.manifest_dir.display()
));
}
}
}
}

if !get_cflags().is_empty() {
let cflags = get_cflags();
emit_warning(&format!(
"AWS_LC_SYS_CFLAGS found. Setting CFLAGS: '{cflags}'"
));
emit_warning(&format!("AWS_LC_SYS_CFLAGS found. CFLAGS: '{cflags:?}'"));
env::set_var("CFLAGS", cflags);
}

Expand Down
48 changes: 20 additions & 28 deletions aws-lc-sys/builder/cmake_builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC

use crate::cc_builder::CcBuilder;
use crate::OutputLib::{Crypto, RustWrapper, Ssl};
use crate::{
allow_prebuilt_nasm, cargo_env, emit_warning, execute_command, get_cflags, is_crt_static,
is_no_asm, option_env, requested_c_std, target, target_arch, target_env, target_family,
target_os, target_underscored, target_vendor, test_nasm_command, use_prebuilt_nasm,
CStdRequested, OutputLibType,
is_no_asm, option_env, requested_c_std, target, target_arch, target_env, target_os,
target_underscored, target_vendor, test_nasm_command, use_prebuilt_nasm, CStdRequested,
OutputLibType,
};
use std::env;
use std::ffi::OsString;
Expand Down Expand Up @@ -89,19 +90,27 @@ impl CmakeBuilder {
cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo");
} else {
cmake_cfg.define("CMAKE_BUILD_TYPE", "release");
if target_family() == "unix" || target_env() == "gnu" {
// This flag is not supported on GCC < v8.1
// TODO: re-enable this
// cmake_cfg.cflag(format!(
// "-ffile-prefix-map={}=",
// self.manifest_dir.display()
// ));
}
}
} else {
cmake_cfg.define("CMAKE_BUILD_TYPE", "debug");
}

// Use the compiler options identified by CcBuilder
let cc_builder = CcBuilder::new(
self.manifest_dir.clone(),
self.out_dir.clone(),
self.build_prefix.clone(),
self.output_lib_type,
);
let mut cflags = OsString::new();
cflags.push(cc_builder.create_builder().get_compiler().cflags_env());
if !get_cflags().is_empty() {
cflags.push(" ");
cflags.push(get_cflags());
}
emit_warning(&format!("Setting CFLAGS: {cflags:?}"));
env::set_var("CFLAGS", cflags);

if let Some(prefix) = &self.build_prefix {
cmake_cfg.define("BORINGSSL_PREFIX", format!("{prefix}_"));
let include_path = self.manifest_dir.join("generated-include");
Expand Down Expand Up @@ -148,14 +157,6 @@ impl CmakeBuilder {
CStdRequested::None => {}
}

if !get_cflags().is_empty() {
let cflags = get_cflags();
emit_warning(&format!(
"AWS_LC_SYS_CFLAGS found. Setting CFLAGS: '{cflags}'"
));
env::set_var("CFLAGS", cflags);
}

// Allow environment to specify CMake toolchain.
if let Some(toolchain) = option_env("CMAKE_TOOLCHAIN_FILE").or(option_env(format!(
"CMAKE_TOOLCHAIN_FILE_{}",
Expand All @@ -167,15 +168,6 @@ impl CmakeBuilder {
return cmake_cfg;
}

if let Some(cc) = option_env("CC") {
emit_warning(&format!("CC environment variable set: {}", cc.clone()));
cmake_cfg.define("CMAKE_C_COMPILER", cc);
}
if let Some(cxx) = option_env("CXX") {
emit_warning(&format!("CXX environment variable set: {}", cxx.clone()));
cmake_cfg.define("CMAKE_CXX_COMPILER", cxx);
}

// See issue: https://github.com/aws/aws-lc-rs/issues/453
if target_os() == "windows" {
self.configure_windows(&mut cmake_cfg);
Expand Down
2 changes: 2 additions & 0 deletions aws-lc-sys/builder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ fn emit_warning(message: &str) {
println!("cargo:warning={message}");
}

#[allow(dead_code)]
fn target_family() -> String {
cargo_env("CARGO_CFG_TARGET_FAMILY")
}
Expand Down Expand Up @@ -424,6 +425,7 @@ fn is_no_asm() -> bool {
unsafe { AWS_LC_SYS_NO_ASM }
}

#[allow(static_mut_refs)]
fn get_cflags() -> &'static str {
unsafe { AWS_LC_SYS_CFLAGS.as_str() }
}
Expand Down

0 comments on commit edee779

Please sign in to comment.