@@ -271,10 +271,28 @@ fn generate_src_bindings(manifest_dir: &Path, prefix: &Option<String>, src_bindi
271
271
& BindingOptions {
272
272
build_prefix : prefix. clone ( ) ,
273
273
include_ssl : false ,
274
- .. Default :: default ( )
274
+ disable_prelude : false ,
275
275
} ,
276
276
)
277
- . write_to_file ( src_bindings_path. join ( format ! ( "{}.rs" , target_platform_prefix( "crypto" ) ) ) )
277
+ . write_to_file ( src_bindings_path)
278
+ . expect ( "write bindings" ) ;
279
+ }
280
+
281
+ #[ allow( unused) ]
282
+ fn external_generate_src_bindings (
283
+ manifest_dir : & Path ,
284
+ prefix : & Option < String > ,
285
+ src_bindings_path : & Path ,
286
+ ) {
287
+ invoke_external_bindgen (
288
+ manifest_dir,
289
+ & BindingOptions {
290
+ build_prefix : prefix. clone ( ) ,
291
+ include_ssl : false ,
292
+ disable_prelude : false ,
293
+ } ,
294
+ src_bindings_path,
295
+ )
278
296
. expect ( "write bindings" ) ;
279
297
}
280
298
@@ -356,7 +374,15 @@ fn initialize() {
356
374
AWS_LC_FIPS_SYS_NO_ASM = env_var_to_bool ( "AWS_LC_FIPS_SYS_NO_ASM" ) . unwrap_or ( false ) ;
357
375
}
358
376
359
- if !is_external_bindgen ( ) && ( is_pregenerating_bindings ( ) || !has_bindgen_feature ( ) ) {
377
+ // The conditions below should prevent use of pregenerated bindings in all cases where the
378
+ // consumer either requires or is requesting bindings generation.
379
+ if ( !is_no_prefix ( )
380
+ && !has_bindgen_feature ( )
381
+ && !is_external_bindgen ( )
382
+ && cfg ! ( not( feature = "ssl" ) ) )
383
+ || is_pregenerating_bindings ( )
384
+ {
385
+ // We only set the PREGENERATED flag when we know pregenerated bindings are available.
360
386
let target = target ( ) ;
361
387
let supported_platform = match target. as_str ( ) {
362
388
"x86_64-unknown-linux-gnu"
@@ -479,12 +505,18 @@ fn main() {
479
505
if is_pregenerating_bindings ( ) {
480
506
#[ cfg( feature = "bindgen" ) ]
481
507
{
508
+ let src_bindings_path = Path :: new ( & manifest_dir)
509
+ . join ( "src" )
510
+ . join ( format ! ( "{}.rs" , target_platform_prefix( "crypto" ) ) ) ;
482
511
emit_warning ( & format ! (
483
- "Generating src bindings. Platform: '{}' Prefix: '{prefix:?}' " ,
484
- target ( )
512
+ "Generating src bindings: {} " ,
513
+ & src_bindings_path . display ( )
485
514
) ) ;
486
- let src_bindings_path = Path :: new ( & manifest_dir) . join ( "src" ) ;
487
- generate_src_bindings ( & manifest_dir, & prefix, & src_bindings_path) ;
515
+ if is_external_bindgen ( ) {
516
+ external_generate_src_bindings ( & manifest_dir, & prefix, & src_bindings_path) ;
517
+ } else {
518
+ generate_src_bindings ( & manifest_dir, & prefix, & src_bindings_path) ;
519
+ }
488
520
bindings_available = true ;
489
521
}
490
522
} else if is_bindgen_required ( ) {
@@ -493,13 +525,14 @@ fn main() {
493
525
bindings_available = true ;
494
526
}
495
527
496
- if !bindings_available && !cfg ! ( feature = "ssl" ) {
497
- emit_warning ( & format ! (
498
- "Generating bindings - external bindgen. Platform: {}" ,
499
- target( )
500
- ) ) ;
528
+ if !bindings_available {
529
+ let options = BindingOptions {
530
+ build_prefix : prefix,
531
+ include_ssl : cfg ! ( feature = "ssl" ) ,
532
+ disable_prelude : true ,
533
+ } ;
501
534
let gen_bindings_path = out_dir ( ) . join ( "bindings.rs" ) ;
502
- let result = invoke_external_bindgen ( & manifest_dir, & prefix , & gen_bindings_path) ;
535
+ let result = invoke_external_bindgen ( & manifest_dir, & options , & gen_bindings_path) ;
503
536
match result {
504
537
Ok ( ( ) ) => {
505
538
emit_rustc_cfg ( "use_bindgen_generated" ) ;
@@ -628,30 +661,69 @@ fn verify_bindgen() -> Result<(), String> {
628
661
Ok ( ( ) )
629
662
}
630
663
664
+ const PRELUDE : & str = r"
665
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
666
+ // SPDX-License-Identifier: Apache-2.0 OR ISC
667
+
668
+ #![allow(
669
+ clippy::cast_lossless,
670
+ clippy::cast_possible_truncation,
671
+ clippy::default_trait_access,
672
+ clippy::must_use_candidate,
673
+ clippy::not_unsafe_ptr_arg_deref,
674
+ clippy::ptr_as_ptr,
675
+ clippy::pub_underscore_fields,
676
+ clippy::semicolon_if_nothing_returned,
677
+ clippy::too_many_lines,
678
+ clippy::unreadable_literal,
679
+ clippy::used_underscore_binding,
680
+ clippy::useless_transmute,
681
+ dead_code,
682
+ improper_ctypes,
683
+ non_camel_case_types,
684
+ non_snake_case,
685
+ non_upper_case_globals,
686
+ unused_imports,
687
+ )]
688
+ " ;
689
+
631
690
fn invoke_external_bindgen (
632
691
manifest_dir : & Path ,
633
- prefix : & Option < String > ,
692
+ options : & BindingOptions ,
634
693
gen_bindings_path : & Path ,
635
694
) -> Result < ( ) , String > {
636
695
verify_bindgen ( ) ?;
696
+ emit_warning ( & format ! (
697
+ "Generating bindings - external bindgen. Platform: '{}' Prefix: '{:?}'" ,
698
+ target( ) ,
699
+ & options. build_prefix
700
+ ) ) ;
637
701
638
- let options = BindingOptions {
639
- // We collect the symbols w/o the prefix added
640
- build_prefix : None ,
641
- include_ssl : false ,
642
- disable_prelude : true ,
643
- } ;
644
-
645
- let clang_args = prepare_clang_args ( manifest_dir, & options) ;
702
+ let mut clang_args = prepare_clang_args (
703
+ manifest_dir,
704
+ & BindingOptions {
705
+ // For external bindgen, we don't want the prefix headers to be included.
706
+ // The bindgen-cli will add prefixes to the symbols to form the correct link name.
707
+ build_prefix : None ,
708
+ include_ssl : options. include_ssl ,
709
+ disable_prelude : options. disable_prelude ,
710
+ } ,
711
+ ) ;
646
712
let header = get_rust_include_path ( manifest_dir)
647
713
. join ( "rust_wrapper.h" )
648
714
. display ( )
649
715
. to_string ( ) ;
650
716
717
+ if options. include_ssl {
718
+ clang_args. extend ( [ String :: from ( "-DAWS_LC_RUST_INCLUDE_SSL" ) ] ) ;
719
+ }
720
+
651
721
let sym_prefix: String ;
652
722
let mut bindgen_params = vec ! [ ] ;
653
- if let Some ( prefix_str) = prefix {
654
- sym_prefix = if target_os ( ) . to_lowercase ( ) == "macos" || target_os ( ) . to_lowercase ( ) == "ios"
723
+ if let Some ( prefix_str) = & options. build_prefix {
724
+ sym_prefix = if target_os ( ) . to_lowercase ( ) == "macos"
725
+ || target_os ( ) . to_lowercase ( ) == "ios"
726
+ || ( target_os ( ) . to_lowercase ( ) == "windows" && target_arch ( ) == "x86" )
655
727
{
656
728
format ! ( "_{prefix_str}_" )
657
729
} else {
@@ -687,8 +759,11 @@ fn invoke_external_bindgen(
687
759
gen_bindings_path. to_str( ) . unwrap( ) ,
688
760
"--formatter" ,
689
761
r"rustfmt" ,
690
- "--" ,
691
762
] ) ;
763
+ if !options. disable_prelude {
764
+ bindgen_params. extend ( [ "--raw-line" , PRELUDE ] ) ;
765
+ }
766
+ bindgen_params. push ( "--" ) ;
692
767
clang_args
693
768
. iter ( )
694
769
. for_each ( |x| bindgen_params. push ( x. as_str ( ) ) ) ;
0 commit comments