Debian Codegraph @

debian/rust-vendor/aws-lc-sys/builder/sys_bindgen.rs

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 use crate::{
5 effective_target, emit_warning, get_rust_include_path, is_all_bindings, BindingOptions,
6 EnvGuard, COPYRIGHT, PRELUDE,
7 };
8 use bindgen::callbacks::{ItemInfo, ParseCallbacks};
9 use std::fmt::Debug;
10 use std::path::Path;
11
12 #[derive(Debug)]
13 struct StripPrefixCallback {
14 remove_prefix: Option<String>,
15 }
16
17 impl StripPrefixCallback {
18 fn new(prefix: &str) -> StripPrefixCallback {
19 StripPrefixCallback {
20 remove_prefix: Some(prefix.to_string()),
21 }
22 }
23 }
24
25 impl ParseCallbacks for StripPrefixCallback {
26 fn generated_name_override(&self, item_info: ItemInfo<'_>) -> Option<String> {
27 self.remove_prefix.as_ref().and_then(|s| {
28 let prefix = format!("{s}_");
29 item_info
30 .name
31 .strip_prefix(prefix.as_str())
32 .map(String::from)
33 })
34 }
35 }
36
37 const ALLOWED_HEADERS: [&str; 30] = [
38 "aead.h",
39 "aes.h",
40 "base.h",
41 "bn.h",
42 "boringssl_prefix_symbols.h",
43 "boringssl_prefix_symbols_asm.h",
44 "boringssl_prefix_symbols_nasm.inc",
45 "bytestring.h",
46 "chacha.h",
47 "cipher.h",
48 "cmac.h",
49 "crypto.h",
50 "curve25519.h",
51 "digest.h",
52 "ec.h",
53 "ec_key.h",
54 "ecdh.h",
55 "ecdsa.h",
56 "err.h",
57 "evp.h",
58 "hkdf.h",
59 "hmac.h",
60 "is_awslc.h",
61 "kdf.h",
62 "mem.h",
63 "nid.h",
64 "poly1305.h",
65 "rand.h",
66 "rsa.h",
67 "sha.h",
68 ];
69
70 // These functions have parameters using a blocked type
71 const BLOCKED_FUNCTIONS: [&str; 5] = [
72 "BN_print_fp",
73 "CBS_parse_generalized_time",
74 "CBS_parse_utc_time",
75 "ERR_print_errors_fp",
76 "RSA_print_fp",
77 ];
78
79 // These types might vary by platform. Ensure that they do not appear in the universal bindings.
80 const BLOCKED_TYPES: [&str; 4] = ["FILE", "fpos_t", "tm", "__sFILE"];
81
82 fn prepare_bindings_builder(manifest_dir: &Path, options: &BindingOptions) -> bindgen::Builder {
83 let clang_args = crate::prepare_clang_args(manifest_dir, options);
84
85 let mut builder = bindgen::Builder::default()
86 .derive_copy(true)
87 .derive_debug(true)
88 .derive_default(true)
89 .derive_eq(true)
90 .allowlist_file(r".*(/|\\)rust_wrapper\.h")
91 .rustified_enum(r"point_conversion_form_t")
92 .rust_target(bindgen::RustTarget::stable(70, 0).unwrap())
93 .default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
94 .generate_comments(true)
95 .fit_macro_constants(false)
96 .size_t_is_usize(true)
97 .layout_tests(true)
98 .prepend_enum_name(true)
99 .formatter(bindgen::Formatter::Rustfmt)
100 .clang_args(clang_args)
101 .raw_line(COPYRIGHT)
102 .header(
103 get_rust_include_path(manifest_dir)
104 .join("rust_wrapper.h")
105 .display()
106 .to_string(),
107 );
108
109 if is_all_bindings() {
110 builder = builder.allowlist_file(r".*(/|\\)openssl((/|\\)[^/\\]+)+\.h");
111 } else {
112 for header in ALLOWED_HEADERS {
113 emit_warning(format!("Allowed header: {header}").as_str());
114 builder = builder.allowlist_file(format!("{}{}", r".*(/|\\)openssl(/|\\)", header));
115 }
116 for function in BLOCKED_FUNCTIONS {
117 emit_warning(format!("Blocked function: {function}").as_str());
118 builder = builder.blocklist_function(function);
119 }
120 for tipe in BLOCKED_TYPES {
121 emit_warning(format!("Opaque type: {tipe}").as_str());
122 builder = builder.blocklist_type(tipe);
123 }
124 }
125
126 if !options.disable_prelude {
127 builder = builder.raw_line(PRELUDE);
128 }
129
130 if options.include_ssl {
131 builder = builder.clang_arg("-DAWS_LC_RUST_INCLUDE_SSL");
132 }
133 if let Some(prefix) = &options.build_prefix {
134 let callbacks = StripPrefixCallback::new(prefix.as_str());
135 builder = builder.parse_callbacks(Box::new(callbacks));
136 }
137
138 builder
139 }
140
141 pub(crate) fn generate_bindings(
142 manifest_dir: &Path,
143 options: &BindingOptions,
144 ) -> bindgen::Bindings {
145 let _guard_target = EnvGuard::new("TARGET", effective_target());
146 prepare_bindings_builder(manifest_dir, options)
147 .generate()
148 .expect("Unable to generate bindings.")
149 }