@@ -19,13 +19,22 @@ use crate::{
1919 is_no_asm, optional_env_optional_crate_target, out_dir, requested_c_std, set_env_for_target,
2020 target, target_arch, target_env, target_os, CStdRequested , OutputLibType ,
2121} ;
22+ use std:: cell:: Cell ;
23+ use std:: collections:: HashMap ;
2224use std:: path:: PathBuf ;
2325
26+ #[ non_exhaustive]
27+ #[ derive( PartialEq , Eq ) ]
28+ pub ( crate ) enum CompilerFeature {
29+ NeonSha3 ,
30+ }
31+
2432pub ( crate ) struct CcBuilder {
2533 manifest_dir : PathBuf ,
2634 out_dir : PathBuf ,
2735 build_prefix : Option < String > ,
2836 output_lib_type : OutputLibType ,
37+ compiler_features : Cell < Vec < CompilerFeature > > ,
2938}
3039
3140use std:: fs;
@@ -168,6 +177,7 @@ impl CcBuilder {
168177 out_dir,
169178 build_prefix,
170179 output_lib_type,
180+ compiler_features : Cell :: new ( vec ! [ ] ) ,
171181 }
172182 }
173183
@@ -190,7 +200,7 @@ impl CcBuilder {
190200 build_options. push ( BuildOption :: std ( "c11" ) ) ;
191201 }
192202 CStdRequested :: None => {
193- if self . compiler_check ( "c11" ) {
203+ if self . compiler_check ( "c11" , Vec :: < String > :: new ( ) ) {
194204 build_options. push ( BuildOption :: std ( "c11" ) ) ;
195205 } else {
196206 build_options. push ( BuildOption :: std ( "c99" ) ) ;
@@ -341,6 +351,18 @@ impl CcBuilder {
341351 cc_build
342352 }
343353
354+ #[ allow( clippy:: zero_sized_map_values) ]
355+ fn build_s2n_bignum_source_feature_map ( ) -> HashMap < String , CompilerFeature > {
356+ let mut source_feature_map: HashMap < String , CompilerFeature > = HashMap :: new ( ) ;
357+ source_feature_map. insert ( "sha3_keccak_f1600_alt.S" . into ( ) , CompilerFeature :: NeonSha3 ) ;
358+ source_feature_map. insert ( "sha3_keccak2_f1600.S" . into ( ) , CompilerFeature :: NeonSha3 ) ;
359+ source_feature_map. insert (
360+ "sha3_keccak4_f1600_alt2.S" . into ( ) ,
361+ CompilerFeature :: NeonSha3 ,
362+ ) ;
363+ source_feature_map
364+ }
365+
344366 fn add_all_files ( & self , lib : & Library , cc_build : & mut cc:: Build ) {
345367 use core:: str:: FromStr ;
346368
@@ -371,20 +393,45 @@ impl CcBuilder {
371393 // conditioned on the target OS.
372394 jitter_entropy_builder. flag ( "-DAWSLC -fwrapv --param ssp-buffer-size=4 -fvisibility=hidden -Wcast-align -Wmissing-field-initializers -Wshadow -Wswitch-enum -Wextra -Wall -pedantic -O0 -fwrapv -Wconversion" ) ;
373395
396+ let s2n_bignum_source_feature_map = Self :: build_s2n_bignum_source_feature_map ( ) ;
397+ let compiler_features = self . compiler_features . take ( ) ;
374398 for source in lib. sources {
375399 let source_path = self . manifest_dir . join ( "aws-lc" ) . join ( source) ;
376400 let is_s2n_bignum = std:: path:: Path :: new ( source) . starts_with ( "third_party/s2n-bignum" ) ;
377401 let is_jitter_entropy =
378402 std:: path:: Path :: new ( source) . starts_with ( "third_party/jitterentropy" ) ;
379403
404+ if !source_path. is_file ( ) {
405+ emit_warning ( & format ! ( "Not a file: {:?}" , source_path. as_os_str( ) ) ) ;
406+ continue ;
407+ }
380408 if is_s2n_bignum {
381- s2n_bignum_builder. file ( source_path) ;
409+ let filename: String = source_path
410+ . file_name ( )
411+ . unwrap ( )
412+ . to_str ( )
413+ . unwrap ( )
414+ . to_string ( ) ;
415+
416+ if let Some ( compiler_feature) = s2n_bignum_source_feature_map. get ( & filename) {
417+ if compiler_features. contains ( compiler_feature) {
418+ s2n_bignum_builder. file ( source_path) ;
419+ } else {
420+ emit_warning ( & format ! (
421+ "Skipping due to missing compiler features: {:?}" ,
422+ source_path. as_os_str( )
423+ ) ) ;
424+ }
425+ } else {
426+ s2n_bignum_builder. file ( source_path) ;
427+ }
382428 } else if is_jitter_entropy {
383429 jitter_entropy_builder. file ( source_path) ;
384430 } else {
385431 cc_build. file ( source_path) ;
386432 }
387433 }
434+ self . compiler_features . set ( compiler_features) ;
388435 let s2n_bignum_object_files = s2n_bignum_builder. compile_intermediates ( ) ;
389436 for object in s2n_bignum_object_files {
390437 cc_build. object ( object) ;
@@ -414,7 +461,11 @@ impl CcBuilder {
414461 // This performs basic checks of compiler capabilities and sets an appropriate flag on success.
415462 // This should be kept in alignment with the checks performed by AWS-LC's CMake build.
416463 // See: https://github.com/search?q=repo%3Aaws%2Faws-lc%20check_compiler&type=code
417- fn compiler_check ( & self , basename : & str ) -> bool {
464+ fn compiler_check < T , S > ( & self , basename : & str , extra_flags : T ) -> bool
465+ where
466+ T : IntoIterator < Item = S > ,
467+ S : AsRef < str > ,
468+ {
418469 let mut ret_val = false ;
419470 let output_dir = self . out_dir . join ( format ! ( "out-{basename}" ) ) ;
420471 let source_file = self
@@ -439,6 +490,10 @@ impl CcBuilder {
439490 . file ( source_file)
440491 . warnings_into_errors ( true )
441492 . out_dir ( & output_dir) ;
493+ for flag in extra_flags {
494+ let flag = flag. as_ref ( ) ;
495+ cc_build. flag ( flag) ;
496+ }
442497
443498 let compiler = cc_build. get_compiler ( ) ;
444499 if compiler. is_like_gnu ( ) || compiler. is_like_clang ( ) {
@@ -529,12 +584,20 @@ impl CcBuilder {
529584 let _ = fs:: remove_file ( exec_path) ;
530585 }
531586 fn run_compiler_checks ( & self , cc_build : & mut cc:: Build ) {
532- if self . compiler_check ( "stdalign_check" ) {
587+ if self . compiler_check ( "stdalign_check" , Vec :: < & ' static str > :: new ( ) ) {
533588 cc_build. define ( "AWS_LC_STDALIGN_AVAILABLE" , Some ( "1" ) ) ;
534589 }
535- if self . compiler_check ( "builtin_swap_check" ) {
590+ if self . compiler_check ( "builtin_swap_check" , Vec :: < & ' static str > :: new ( ) ) {
536591 cc_build. define ( "AWS_LC_BUILTIN_SWAP_SUPPORTED" , Some ( "1" ) ) ;
537592 }
593+ if target_arch ( ) == "aarch64"
594+ && self . compiler_check ( "neon_sha3_check" , vec ! [ "-march=armv8.4-a+sha3" ] )
595+ {
596+ let mut compiler_features = self . compiler_features . take ( ) ;
597+ compiler_features. push ( CompilerFeature :: NeonSha3 ) ;
598+ self . compiler_features . set ( compiler_features) ;
599+ cc_build. define ( "MY_ASSEMBLER_SUPPORTS_NEON_SHA3_EXTENSION" , Some ( "1" ) ) ;
600+ }
538601 self . memcmp_check ( ) ;
539602 }
540603}
0 commit comments