| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::{ |
| 5 |
collections::BTreeSet, |
| 6 |
fs::{self, read_to_string}, |
| 7 |
io, |
| 8 |
path::Path, |
| 9 |
sync::{Arc, Mutex}, |
| 10 |
}; |
| 11 |
|
| 12 |
use bindgen::callbacks::ItemKind; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const FEATURE_TOKEN_PLACEHOLDER: &str = "<TOKEN_REPLACED_WITH_UNSTABLE_FEATURES>"; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
fn main() { |
| 25 |
let out_dir = std::env::args().nth(1).expect("missing sys dir"); |
| 26 |
let out_dir = Path::new(&out_dir); |
| 27 |
|
| 28 |
let functions = FunctionCallbacks::default(); |
| 29 |
|
| 30 |
gen_bindings( |
| 31 |
"#include <s2n.h>", |
| 32 |
&out_dir.join("lib"), |
| 33 |
functions.with_feature(None), |
| 34 |
) |
| 35 |
.allowlist_type("s2n_.*") |
| 36 |
.allowlist_function("s2n_.*") |
| 37 |
.allowlist_var("s2n_.*") |
| 38 |
.generate() |
| 39 |
.unwrap() |
| 40 |
.write_to_file(out_dir.join("src/api.rs")) |
| 41 |
.unwrap(); |
| 42 |
|
| 43 |
write_feature_bindings( |
| 44 |
out_dir.join("lib/tls/s2n_internal.h"), |
| 45 |
"internal", |
| 46 |
out_dir, |
| 47 |
out_dir.join("src/features/internal.rs"), |
| 48 |
functions.clone(), |
| 49 |
); |
| 50 |
write_feature_bindings( |
| 51 |
out_dir.join("lib/tls/s2n_quic_support.h"), |
| 52 |
"quic", |
| 53 |
out_dir, |
| 54 |
out_dir.join("src/features/quic.rs"), |
| 55 |
functions.clone(), |
| 56 |
); |
| 57 |
|
| 58 |
|
| 59 |
let unstable_api = out_dir.join("lib/api/unstable"); |
| 60 |
let unstable_headers: Vec<(String, fs::DirEntry)> = fs::read_dir(unstable_api) |
| 61 |
.expect("unable to iterate through files in unstable api folder") |
| 62 |
.into_iter() |
| 63 |
.map(|dir_entry| dir_entry.expect("failed to read header")) |
| 64 |
.map(|dir_entry| { |
| 65 |
( |
| 66 |
dir_entry |
| 67 |
.path() |
| 68 |
.file_stem() |
| 69 |
.unwrap() |
| 70 |
.to_str() |
| 71 |
.unwrap() |
| 72 |
.to_owned(), |
| 73 |
dir_entry, |
| 74 |
) |
| 75 |
}) |
| 76 |
.collect(); |
| 77 |
|
| 78 |
|
| 79 |
for (header_name, header) in unstable_headers.iter() { |
| 80 |
let feature_name = format!("unstable-{}", header_name); |
| 81 |
write_feature_bindings( |
| 82 |
header.path(), |
| 83 |
&feature_name, |
| 84 |
out_dir, |
| 85 |
out_dir.join(format!("src/features/{}.rs", header_name)), |
| 86 |
functions.clone(), |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
let mut features_definition_token = unstable_headers |
| 92 |
.iter() |
| 93 |
.map(|(header_name, _header)| format!("unstable-{header_name} = []")) |
| 94 |
.collect::<Vec<String>>(); |
| 95 |
features_definition_token.sort(); |
| 96 |
let cargo_template = out_dir.join("templates/Cargo.template"); |
| 97 |
let cargo_template = read_to_string(cargo_template).expect("unable to read cargo template"); |
| 98 |
let cargo_toml = cargo_template.replace(FEATURE_TOKEN_PLACEHOLDER, &(features_definition_token.join("\n"))); |
| 99 |
fs::write(out_dir.join("Cargo.toml"), cargo_toml).unwrap(); |
| 100 |
|
| 101 |
|
| 102 |
let features_module_token = unstable_headers |
| 103 |
.iter() |
| 104 |
.map(|(header_name, _header)| { |
| 105 |
format!("conditional_module!({header_name}, \"unstable-{header_name}\");") |
| 106 |
}) |
| 107 |
.collect::<Vec<String>>() |
| 108 |
.join("\n"); |
| 109 |
let features_template = out_dir.join("templates/features.template"); |
| 110 |
let features_template = read_to_string(features_template).expect("unable to features template"); |
| 111 |
let features_rs = features_template.replace(FEATURE_TOKEN_PLACEHOLDER, &features_module_token); |
| 112 |
std::fs::write(out_dir.join("src/features.rs"), features_rs).unwrap(); |
| 113 |
|
| 114 |
functions.tests(&out_dir.join("src/tests.rs")).unwrap(); |
| 115 |
|
| 116 |
gen_files(&out_dir.join("lib"), &out_dir.join("files.rs")).unwrap(); |
| 117 |
} |
| 118 |
|
| 119 |
const COPYRIGHT: &str = r#" |
| 120 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 121 |
// SPDX-License-Identifier: Apache-2.0 |
| 122 |
"#; |
| 123 |
|
| 124 |
const PRELUDE: &str = r#" |
| 125 |
#![allow(unused_imports, non_camel_case_types)] |
| 126 |
|
| 127 |
use libc::{iovec, FILE, off_t}; |
| 128 |
// specify that aws-lc-rs is used, so that the rust compiler will link in the appropriate |
| 129 |
// libcrypto artifact. |
| 130 |
#[cfg(not(s2n_tls_external_build))] |
| 131 |
extern crate aws_lc_rs as _; |
| 132 |
"#; |
| 133 |
|
| 134 |
fn base_builder() -> bindgen::Builder { |
| 135 |
bindgen::Builder::default() |
| 136 |
.use_core() |
| 137 |
.layout_tests(true) |
| 138 |
.detect_include_paths(true) |
| 139 |
.size_t_is_usize(true) |
| 140 |
.enable_function_attribute_detection() |
| 141 |
.default_enum_style(bindgen::EnumVariation::ModuleConsts) |
| 142 |
.rust_target(bindgen::RustTarget::Stable_1_47) |
| 143 |
|
| 144 |
|
| 145 |
.blocklist_item("s2n_errno") |
| 146 |
.raw_line(COPYRIGHT) |
| 147 |
.raw_line(PRELUDE) |
| 148 |
.ctypes_prefix("::libc") |
| 149 |
} |
| 150 |
|
| 151 |
fn gen_bindings(entry: &str, s2n_dir: &Path, functions: FunctionCallbacks) -> bindgen::Builder { |
| 152 |
base_builder() |
| 153 |
.header_contents("s2n-sys.h", entry) |
| 154 |
|
| 155 |
.blocklist_type("iovec") |
| 156 |
.blocklist_type("FILE") |
| 157 |
.blocklist_type("_IO_.*") |
| 158 |
.blocklist_type("__.*") |
| 159 |
.blocklist_type("fpos_t") |
| 160 |
.parse_callbacks(Box::new(functions)) |
| 161 |
.clang_arg(format!("-I{}/api", s2n_dir.display())) |
| 162 |
.clang_arg(format!("-I{}", s2n_dir.display())) |
| 163 |
} |
| 164 |
|
| 165 |
fn write_feature_bindings( |
| 166 |
header_path: impl AsRef<Path>, |
| 167 |
feature_flag: &str, |
| 168 |
s2n_tls_sys_dir: &Path, |
| 169 |
output_path: impl AsRef<Path>, |
| 170 |
functions: FunctionCallbacks, |
| 171 |
) { |
| 172 |
let header_path_str = format!("{}", header_path.as_ref().display()); |
| 173 |
let lib_path = s2n_tls_sys_dir.join("lib"); |
| 174 |
base_builder() |
| 175 |
.header(&header_path_str) |
| 176 |
.parse_callbacks(Box::new( |
| 177 |
functions.with_feature(Some(feature_flag.to_owned())), |
| 178 |
)) |
| 179 |
|
| 180 |
.clang_arg(format!("-I{}/api", lib_path.display())) |
| 181 |
.clang_arg(format!("-I{}", lib_path.display())) |
| 182 |
.allowlist_recursively(false) |
| 183 |
.allowlist_file(&header_path_str) |
| 184 |
|
| 185 |
|
| 186 |
.blocklist_type("s2n_connection") |
| 187 |
.blocklist_type("s2n_config") |
| 188 |
.raw_line("use crate::api::*;\n") |
| 189 |
.generate() |
| 190 |
.unwrap() |
| 191 |
.write_to_file(output_path) |
| 192 |
.unwrap(); |
| 193 |
} |
| 194 |
|
| 195 |
fn gen_files(input: &Path, out: &Path) -> io::Result<()> { |
| 196 |
use io::Write; |
| 197 |
|
| 198 |
let mut files = std::fs::File::create(out)?; |
| 199 |
let mut o = io::BufWriter::new(&mut files); |
| 200 |
|
| 201 |
let pattern = format!("{}/**/*.c", input.display()); |
| 202 |
|
| 203 |
writeln!(o, "{}", COPYRIGHT)?; |
| 204 |
writeln!(o, "[")?; |
| 205 |
for file in glob::glob(&pattern).unwrap() { |
| 206 |
let file = file.unwrap(); |
| 207 |
let file = file.strip_prefix(input).unwrap(); |
| 208 |
|
| 209 |
if file.starts_with("tests") { |
| 210 |
continue; |
| 211 |
} |
| 212 |
writeln!(o, " {:?},", Path::new("lib").join(file).display())?; |
| 213 |
} |
| 214 |
writeln!(o, "]")?; |
| 215 |
Ok(()) |
| 216 |
} |
| 217 |
|
| 218 |
type SharedBTreeSet<T> = Arc<Mutex<BTreeSet<T>>>; |
| 219 |
|
| 220 |
#[derive(Clone, Debug, Default)] |
| 221 |
struct FunctionCallbacks { |
| 222 |
|
| 223 |
feature: Arc<Mutex<Option<String>>>, |
| 224 |
|
| 225 |
functions: SharedBTreeSet<(Option<String>, String)>, |
| 226 |
} |
| 227 |
|
| 228 |
impl FunctionCallbacks { |
| 229 |
fn with_feature(&self, feature: Option<String>) -> Self { |
| 230 |
*self.feature.lock().unwrap() = feature; |
| 231 |
self.clone() |
| 232 |
} |
| 233 |
|
| 234 |
fn tests(&self, out: &Path) -> io::Result<()> { |
| 235 |
use io::Write; |
| 236 |
let functions = self.functions.lock().unwrap(); |
| 237 |
let mut tests = std::fs::File::create(out)?; |
| 238 |
let mut o = io::BufWriter::new(&mut tests); |
| 239 |
|
| 240 |
writeln!(o, "{}", COPYRIGHT)?; |
| 241 |
let iter = functions.iter(); |
| 242 |
for (feature, function) in iter { |
| 243 |
|
| 244 |
if feature.is_some() && functions.contains(&(None, function.to_string())) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
writeln!(o, "#[test]")?; |
| 249 |
|
| 250 |
|
| 251 |
if let Some(feature) = feature { |
| 252 |
writeln!(o, "#[cfg(feature = {:?})]", feature)?; |
| 253 |
}; |
| 254 |
|
| 255 |
writeln!(o, "fn {} () {{", function)?; |
| 256 |
writeln!(o, " let ptr = crate::{} as *const ();", function)?; |
| 257 |
writeln!(o, " assert!(!ptr.is_null());")?; |
| 258 |
writeln!(o, "}}")?; |
| 259 |
writeln!(o)?; |
| 260 |
} |
| 261 |
|
| 262 |
Ok(()) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
impl bindgen::callbacks::ParseCallbacks for FunctionCallbacks { |
| 267 |
fn enum_variant_name( |
| 268 |
&self, |
| 269 |
_name: Option<&str>, |
| 270 |
variant_name: &str, |
| 271 |
_variant_value: bindgen::callbacks::EnumVariantValue, |
| 272 |
) -> Option<String> { |
| 273 |
if !variant_name.starts_with("S2N_") { |
| 274 |
return None; |
| 275 |
} |
| 276 |
|
| 277 |
let variant_name = variant_name |
| 278 |
.trim_start_matches("S2N_ERR_T_") |
| 279 |
.trim_start_matches("S2N_EXTENSION_") |
| 280 |
|
| 281 |
.trim_start_matches("S2N_TLS_MAX_FRAG_") |
| 282 |
.trim_start_matches("S2N_ALERT_") |
| 283 |
.trim_start_matches("S2N_CT_SUPPORT_") |
| 284 |
.trim_start_matches("S2N_STATUS_REQUEST_") |
| 285 |
.trim_start_matches("S2N_CERT_AUTH_") |
| 286 |
.trim_start_matches("S2N_CLIENT_HELLO_CB_") |
| 287 |
.trim_start_matches("S2N_TLS_SIGNATURE_") |
| 288 |
.trim_start_matches("S2N_TLS_HASH_") |
| 289 |
.trim_start_matches("S2N_PSK_HMAC_") |
| 290 |
.trim_start_matches("S2N_PSK_MODE_") |
| 291 |
.trim_start_matches("S2N_ASYNC_PKEY_VALIDATION_") |
| 292 |
.trim_start_matches("S2N_ASYNC_") |
| 293 |
.trim_start_matches("S2N_EARLY_DATA_STATUS_") |
| 294 |
|
| 295 |
.trim_start_matches("S2N_"); |
| 296 |
|
| 297 |
Some(variant_name.to_owned()) |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
fn generated_name_override( |
| 304 |
&self, |
| 305 |
item_info: bindgen::callbacks::ItemInfo<'_>, |
| 306 |
) -> Option<String> { |
| 307 |
if !item_info.name.starts_with("s2n_") { |
| 308 |
return None; |
| 309 |
} |
| 310 |
|
| 311 |
if let ItemKind::Function = item_info.kind { |
| 312 |
let feature = self.feature.lock().unwrap().clone(); |
| 313 |
self.functions |
| 314 |
.lock() |
| 315 |
.unwrap() |
| 316 |
.insert((feature, item_info.name.to_owned())); |
| 317 |
} |
| 318 |
None |
| 319 |
} |
| 320 |
} |