Skip to content

Commit 58ba5b2

Browse files
committed
Add support for aws-lc prefixed installs using OPENSSL_DIR
1 parent a299e5c commit 58ba5b2

File tree

6 files changed

+59
-33
lines changed

6 files changed

+59
-33
lines changed

openssl-sys/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ rust-version = "1.63.0"
1919
vendored = ['openssl-src']
2020
unstable_boringssl = ['bssl-sys']
2121
aws-lc = ['aws-lc-sys']
22-
aws-lc-fips = ['aws-lc-fips-sys']
2322

2423
[dependencies]
2524
libc = "0.2"
2625
bssl-sys = { version = "0.1.0", optional = true }
2726
aws-lc-sys = { version = "0", features = ["ssl"], optional = true }
28-
aws-lc-fips-sys = { version = "0", features = ["ssl", "bindgen"], optional = true }
2927

3028
[build-dependencies]
3129
bindgen = { version = "0.69.0", optional = true, features = ["experimental"] }

openssl-sys/build/expando.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,9 @@ RUST_CONF_OPENSSL_NO_SEED
146146
#ifdef OPENSSL_NO_SCRYPT
147147
RUST_CONF_OPENSSL_NO_SCRYPT
148148
#endif
149+
150+
#define SYMBOL_PREFIX2(X) RUST_BINDGEN_SYMBOL_PREFIX_##X##_
151+
#define SYMBOL_PREFIX(X) SYMBOL_PREFIX2(X)
152+
#if defined(OPENSSL_IS_AWSLC) && defined(BORINGSSL_PREFIX)
153+
SYMBOL_PREFIX(BORINGSSL_PREFIX)
154+
#endif

openssl-sys/build/main.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,24 @@ fn check_ssl_kind() {
7474
}
7575

7676
let is_aws_lc = cfg!(feature = "aws-lc");
77-
let is_aws_lc_fips = cfg!(feature = "aws-lc-fips");
7877

79-
if is_aws_lc || is_aws_lc_fips {
78+
if is_aws_lc {
8079
println!("cargo:rustc-cfg=awslc");
8180
println!("cargo:awslc=true");
8281

83-
// The aws-lc-sys and aws-lc-fips-sys crate use a link name that embeds
82+
// The aws-lc-sys crate uses a link name that embeds
8483
// the version number of crate. Examples (crate-name => links name):
8584
// * aws-lc-sys => aws_lc_0_26_0
86-
// * aws-lc-fips-sys => aws_lc_fips_0_13_3
8785
// This is done to avoid issues if the cargo dependency graph for an application
8886
// were to resolve to multiple versions for the same crate.
8987
//
9088
// Due to this we need to determine what version of the AWS-LC has been selected (fips or non-fips)
9189
// and then need to parse out the pieces we are interested in ignoring the version componenet of the name.
92-
let env_var_prefix = match (is_aws_lc, is_aws_lc_fips) {
93-
(true, false) => "DEP_AWS_LC_",
94-
(false, true) => "DEP_AWS_LC_FIPS_",
95-
_ => {
96-
panic!("aws-lc and aws-lc-fips are mutually exclusive features!");
97-
}
98-
};
90+
const AWS_LC_ENV_VAR_PREFIX: &str = "DEP_AWS_LC_";
9991

10092
let mut version = None;
10193
for (name, _) in std::env::vars() {
102-
if let Some(name) = name.strip_prefix(env_var_prefix) {
94+
if let Some(name) = name.strip_prefix(AWS_LC_ENV_VAR_PREFIX) {
10395
if let Some(name) = name.strip_suffix("_INCLUDE") {
10496
version = Some(name.to_owned());
10597
break;
@@ -109,7 +101,7 @@ fn check_ssl_kind() {
109101
let version = version.expect("aws-lc version detected");
110102

111103
// Read the OpenSSL configuration statements and emit rust-cfg for each.
112-
if let Ok(vars) = std::env::var(format!("{env_var_prefix}{version}_CONF")) {
104+
if let Ok(vars) = std::env::var(format!("{AWS_LC_ENV_VAR_PREFIX}{version}_CONF")) {
113105
for var in vars.split(',') {
114106
println!("cargo:rustc-cfg=osslconf=\"{var}\"");
115107
}
@@ -118,7 +110,7 @@ fn check_ssl_kind() {
118110

119111
// Emit the include header directory from the aws-lc(-fips)-sys crate so that it can be used if needed
120112
// by crates consuming openssl-sys.
121-
if let Ok(val) = std::env::var(format!("{env_var_prefix}{version}_INCLUDE")) {
113+
if let Ok(val) = std::env::var(format!("{AWS_LC_ENV_VAR_PREFIX}{version}_INCLUDE")) {
122114
println!("cargo:include={val}");
123115
}
124116

@@ -355,6 +347,7 @@ See rust-openssl documentation for more information:
355347
let mut libressl_version = None;
356348
let mut is_boringssl = false;
357349
let mut is_awslc = false;
350+
let mut bindgen_symbol_prefix: Option<String> = None;
358351
for line in expanded.lines() {
359352
let line = line.trim();
360353

@@ -364,6 +357,7 @@ See rust-openssl documentation for more information:
364357
let boringssl_prefix = "RUST_OPENSSL_IS_BORINGSSL";
365358
let awslc_prefix = "RUST_OPENSSL_IS_AWSLC";
366359
let conf_prefix = "RUST_CONF_";
360+
let symbol_prefix = "RUST_BINDGEN_SYMBOL_PREFIX_";
367361
if let Some(version) = line.strip_prefix(openssl_prefix) {
368362
openssl_version = Some(parse_version(version));
369363
} else if let Some(version) = line.strip_prefix(new_openssl_prefix) {
@@ -376,6 +370,9 @@ See rust-openssl documentation for more information:
376370
is_boringssl = true;
377371
} else if line.starts_with(awslc_prefix) {
378372
is_awslc = true;
373+
} else if line.starts_with(symbol_prefix) {
374+
let sym_prefix = String::from(line.strip_prefix(symbol_prefix).unwrap());
375+
bindgen_symbol_prefix = Some(sym_prefix);
379376
}
380377
}
381378

@@ -394,7 +391,7 @@ See rust-openssl documentation for more information:
394391
if is_awslc {
395392
println!("cargo:rustc-cfg=awslc");
396393
println!("cargo:awslc=true");
397-
run_bindgen::run_awslc(include_dirs);
394+
run_bindgen::run_awslc(include_dirs, bindgen_symbol_prefix);
398395
return Version::AwsLc;
399396
}
400397

openssl-sys/build/run_bindgen.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,33 @@ pub fn run_boringssl(include_dirs: &[PathBuf]) {
222222
}
223223

224224
#[cfg(feature = "bindgen")]
225-
pub fn run_awslc(include_dirs: &[PathBuf]) {
225+
mod bindgen_options {
226+
use bindgen::callbacks::{ItemInfo, ParseCallbacks};
227+
228+
#[derive(Debug)]
229+
pub struct StripPrefixCallback {
230+
remove_prefix: Option<String>,
231+
}
232+
233+
impl StripPrefixCallback {
234+
pub fn new(prefix: &str) -> StripPrefixCallback {
235+
StripPrefixCallback {
236+
remove_prefix: Some(prefix.to_string()),
237+
}
238+
}
239+
}
240+
241+
impl ParseCallbacks for StripPrefixCallback {
242+
fn generated_name_override(&self, item_info: ItemInfo<'_>) -> Option<String> {
243+
self.remove_prefix
244+
.as_ref()
245+
.and_then(|s| item_info.name.strip_prefix(s.as_str()).map(String::from))
246+
}
247+
}
248+
}
249+
250+
#[cfg(feature = "bindgen")]
251+
pub fn run_awslc(include_dirs: &[PathBuf], symbol_prefix: Option<String>) {
226252
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
227253

228254
fs::File::create(out_dir.join("awslc_static_wrapper.h"))
@@ -242,12 +268,13 @@ pub fn run_awslc(include_dirs: &[PathBuf]) {
242268
.wrap_static_fns(true)
243269
.wrap_static_fns_path(out_dir.join("awslc_static_wrapper").display().to_string())
244270
.layout_tests(false)
245-
.header(
246-
out_dir
247-
.join("awslc_static_wrapper.h")
248-
.display()
249-
.to_string(),
250-
);
271+
.header(out_dir.join("awslc_static_wrapper.h").display().to_string());
272+
273+
if let Some(prefix) = symbol_prefix {
274+
use bindgen_options::StripPrefixCallback;
275+
let callback = StripPrefixCallback::new(prefix.as_str());
276+
builder = builder.parse_callbacks(Box::from(callback));
277+
}
251278

252279
for include_dir in include_dirs {
253280
builder = builder
@@ -268,7 +295,12 @@ pub fn run_awslc(include_dirs: &[PathBuf]) {
268295
}
269296

270297
#[cfg(not(feature = "bindgen"))]
271-
pub fn run_awslc(include_dirs: &[PathBuf]) {
298+
pub fn run_awslc(include_dirs: &[PathBuf], symbol_prefix: Option<String>) {
299+
if symbol_prefix.is_some() {
300+
panic!("aws-lc installation has prefixed symbols, but bindgen-cli does not support removing prefixes. \
301+
Enable the bindgen crate feature to support this installation.")
302+
}
303+
272304
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
273305

274306
fs::File::create(out_dir.join("awslc_static_wrapper.h"))
@@ -290,7 +322,6 @@ pub fn run_awslc(include_dirs: &[PathBuf]) {
290322
.arg("--default-macro-constant-type=signed")
291323
.arg("--rustified-enum=point_conversion_form_t")
292324
.arg(r"--allowlist-file=.*(/|\\)openssl((/|\\)[^/\\]+)+\.h")
293-
.arg("--experimental")
294325
.arg("--wrap-static-fns")
295326
.arg("--wrap-static-fns-path")
296327
.arg(out_dir.join("awslc_static_wrapper").display().to_string())

openssl-sys/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ mod boringssl {
2929
#[cfg(boringssl)]
3030
pub use boringssl::*;
3131

32-
#[cfg(feature = "aws-lc-fips")]
33-
extern crate aws_lc_fips_sys;
3432
#[cfg(feature = "aws-lc")]
3533
extern crate aws_lc_sys;
3634

@@ -40,10 +38,7 @@ mod aws_lc {
4038
#[cfg(feature = "aws-lc")]
4139
pub use aws_lc_sys::*;
4240

43-
#[cfg(feature = "aws-lc-fips-sys")]
44-
pub use aws_lc_fips_sys::*;
45-
46-
#[cfg(not(any(feature = "aws-lc", feature = "aws-lc-fips-sys")))]
41+
#[cfg(not(feature = "aws-lc"))]
4742
include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
4843

4944
// AWS-LC does not require initialization.

openssl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ vendored = ['ffi/vendored']
2222
bindgen = ['ffi/bindgen']
2323
unstable_boringssl = ["ffi/unstable_boringssl"]
2424
aws-lc = ["ffi/aws-lc"]
25-
aws-lc-fips = ["ffi/aws-lc-fips"]
2625
default = []
2726

2827
[dependencies]

0 commit comments

Comments
 (0)