Skip to content

Commit 99bf95f

Browse files
committed
Add support for OPENSSL_DIR
1 parent 7d835d0 commit 99bf95f

File tree

4 files changed

+138
-17
lines changed

4 files changed

+138
-17
lines changed

openssl-sys/build/expando.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ VERSION(OPENSSL, OPENSSL_VERSION_NUMBER)
1919
RUST_OPENSSL_IS_BORINGSSL
2020
#endif
2121

22+
#ifdef OPENSSL_IS_AWSLC
23+
RUST_OPENSSL_IS_AWSLC
24+
#endif
25+
2226
#ifdef OPENSSL_NO_BF
2327
RUST_CONF_OPENSSL_NO_BF
2428
#endif

openssl-sys/build/main.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum Version {
2424
Openssl10x,
2525
Libressl,
2626
Boringssl,
27+
AwsLc,
2728
}
2829

2930
fn env_inner(name: &str) -> Option<OsString> {
@@ -255,7 +256,10 @@ fn main() {
255256
// try to match the behavior for common platforms. For a more robust option,
256257
// this likely needs to be deferred to the caller with an environment
257258
// variable.
258-
if version == Version::Boringssl && kind == "static" && env::var("CARGO_CFG_UNIX").is_ok() {
259+
if (version == Version::Boringssl || version == Version::AwsLc)
260+
&& kind == "static"
261+
&& env::var("CARGO_CFG_UNIX").is_ok()
262+
{
259263
let cpp_lib = match env::var("CARGO_CFG_TARGET_OS").unwrap().as_ref() {
260264
"macos" => "c++",
261265
_ => "stdc++",
@@ -285,8 +289,8 @@ fn main() {
285289
fn postprocess(include_dirs: &[PathBuf]) -> Version {
286290
let version = validate_headers(include_dirs);
287291

288-
// Never run bindgen for BoringSSL, if it was needed we already ran it.
289-
if version != Version::Boringssl {
292+
// Never run bindgen for BoringSSL or AWS-LC, if it was needed we already ran it.
293+
if !(version == Version::Boringssl || version == Version::AwsLc) {
290294
#[cfg(feature = "bindgen")]
291295
run_bindgen::run(&include_dirs);
292296
}
@@ -350,13 +354,15 @@ See rust-openssl documentation for more information:
350354
let mut openssl_version = None;
351355
let mut libressl_version = None;
352356
let mut is_boringssl = false;
357+
let mut is_awslc = false;
353358
for line in expanded.lines() {
354359
let line = line.trim();
355360

356361
let openssl_prefix = "RUST_VERSION_OPENSSL_";
357362
let new_openssl_prefix = "RUST_VERSION_NEW_OPENSSL_";
358363
let libressl_prefix = "RUST_VERSION_LIBRESSL_";
359364
let boringssl_prefix = "RUST_OPENSSL_IS_BORINGSSL";
365+
let awslc_prefix = "RUST_OPENSSL_IS_AWSLC";
360366
let conf_prefix = "RUST_CONF_";
361367
if let Some(version) = line.strip_prefix(openssl_prefix) {
362368
openssl_version = Some(parse_version(version));
@@ -368,6 +374,8 @@ See rust-openssl documentation for more information:
368374
enabled.push(conf);
369375
} else if line.starts_with(boringssl_prefix) {
370376
is_boringssl = true;
377+
} else if line.starts_with(awslc_prefix) {
378+
is_awslc = true;
371379
}
372380
}
373381

@@ -383,6 +391,13 @@ See rust-openssl documentation for more information:
383391
return Version::Boringssl;
384392
}
385393

394+
if is_awslc {
395+
println!("cargo:rustc-cfg=awslc");
396+
println!("cargo:awslc=true");
397+
run_bindgen::run_awslc(include_dirs);
398+
return Version::AwsLc;
399+
}
400+
386401
// We set this for any non-BoringSSL lib.
387402
println!("cargo:rustc-cfg=openssl");
388403

openssl-sys/build/run_bindgen.rs

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,20 @@ const INCLUDES: &str = "
3636
#include <openssl/x509_vfy.h>
3737
#include <openssl/x509v3.h>
3838
39+
#if !defined(OPENSSL_IS_AWSLC)
3940
// this must be included after ssl.h for libressl!
4041
#include <openssl/srtp.h>
42+
#endif
4143
42-
#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
43-
#include <openssl/cms.h>
44+
#if !(defined(LIBRESSL_VERSION_NUMBER) || defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC))
45+
#include <openssl/cmsrc/ssl/mod.rss.h>
4446
#endif
4547
46-
#if !defined(OPENSSL_IS_BORINGSSL)
48+
#if !(defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC))
4749
#include <openssl/comp.h>
50+
#endif
51+
52+
#if !defined(OPENSSL_IS_BORINGSSL)
4853
#include <openssl/ocsp.h>
4954
#endif
5055
@@ -60,7 +65,7 @@ const INCLUDES: &str = "
6065
#include <openssl/quic.h>
6166
#endif
6267
63-
#if defined(LIBRESSL_VERSION_NUMBER) || defined(OPENSSL_IS_BORINGSSL)
68+
#if defined(LIBRESSL_VERSION_NUMBER) || defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
6469
#include <openssl/poly1305.h>
6570
#endif
6671
@@ -216,6 +221,96 @@ pub fn run_boringssl(include_dirs: &[PathBuf]) {
216221
.compile("boring_static_wrapper");
217222
}
218223

224+
#[cfg(feature = "bindgen")]
225+
pub fn run_awslc(include_dirs: &[PathBuf]) {
226+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
227+
228+
fs::File::create(out_dir.join("awslc_static_wrapper.h"))
229+
.expect("Failed to create awslc_static_wrapper.h")
230+
.write_all(INCLUDES.as_bytes())
231+
.expect("Failed to write contents to awslc_static_wrapper.h");
232+
233+
let mut builder = bindgen::builder()
234+
.rust_target(RustTarget::Stable_1_47)
235+
.ctypes_prefix("::libc")
236+
.raw_line("use libc::*;")
237+
.derive_default(false)
238+
.enable_function_attribute_detection()
239+
.default_macro_constant_type(MacroTypeVariation::Signed)
240+
.rustified_enum("point_conversion_form_t")
241+
.allowlist_file(r".*(/|\\)openssl((/|\\)[^/\\]+)+\.h")
242+
.wrap_static_fns(true)
243+
.wrap_static_fns_path(out_dir.join("awslc_static_wrapper").display().to_string())
244+
.layout_tests(false)
245+
.header(
246+
out_dir
247+
.join("awslc_static_wrapper.h")
248+
.display()
249+
.to_string(),
250+
);
251+
252+
for include_dir in include_dirs {
253+
builder = builder
254+
.clang_arg("-I")
255+
.clang_arg(include_dir.display().to_string());
256+
}
257+
258+
builder
259+
.generate()
260+
.unwrap()
261+
.write_to_file(out_dir.join("bindgen.rs"))
262+
.unwrap();
263+
264+
cc::Build::new()
265+
.file(out_dir.join("awslc_static_wrapper.c"))
266+
.includes(include_dirs)
267+
.compile("awslc_static_wrapper");
268+
}
269+
270+
#[cfg(not(feature = "bindgen"))]
271+
pub fn run_awslc(include_dirs: &[PathBuf]) {
272+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
273+
274+
fs::File::create(out_dir.join("awslc_static_wrapper.h"))
275+
.expect("Failed to create awslc_static_wrapper.h")
276+
.write_all(INCLUDES.as_bytes())
277+
.expect("Failed to write contents to awslc_static_wrapper.h");
278+
279+
let mut bindgen_cmd = process::Command::new("bindgen");
280+
bindgen_cmd
281+
.arg("-o")
282+
.arg(out_dir.join("bindgen.rs"))
283+
// Must be a valid version from
284+
// https://docs.rs/bindgen/latest/bindgen/enum.RustTarget.html
285+
.arg("--rust-target=1.47")
286+
.arg("--ctypes-prefix=::libc")
287+
.arg("--raw-line=use libc::*;")
288+
.arg("--no-derive-default")
289+
.arg("--enable-function-attribute-detection")
290+
.arg("--default-macro-constant-type=signed")
291+
.arg("--rustified-enum=point_conversion_form_t")
292+
.arg(r"--allowlist-file=.*(/|\\)openssl((/|\\)[^/\\]+)+\.h")
293+
.arg("--experimental")
294+
.arg("--wrap-static-fns")
295+
.arg("--wrap-static-fns-path")
296+
.arg(out_dir.join("awslc_static_wrapper").display().to_string())
297+
.arg(out_dir.join("awslc_static_wrapper.h"))
298+
.arg("--")
299+
.arg(format!("--target={}", env::var("TARGET").unwrap()));
300+
301+
for include_dir in include_dirs {
302+
bindgen_cmd.arg("-I").arg(include_dir.display().to_string());
303+
}
304+
305+
let result = bindgen_cmd.status().expect("bindgen failed to execute");
306+
assert!(result.success());
307+
308+
cc::Build::new()
309+
.file(out_dir.join("awslc_static_wrapper.c"))
310+
.includes(include_dirs)
311+
.compile("awslc_static_wrapper");
312+
}
313+
219314
#[cfg(feature = "bindgen")]
220315
#[derive(Debug)]
221316
struct OpensslCallbacks;

openssl-sys/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,27 @@ mod boringssl {
2929
#[cfg(boringssl)]
3030
pub use boringssl::*;
3131

32-
#[cfg(any(feature = "aws-lc", feature = "aws-lc-fips-sys"))]
32+
#[cfg(feature = "aws-lc-fips")]
33+
extern crate aws_lc_fips_sys;
34+
#[cfg(feature = "aws-lc")]
35+
extern crate aws_lc_sys;
36+
37+
#[cfg(awslc)]
38+
#[path = "."]
3339
mod aws_lc {
34-
#[cfg(feature = "aws-lc-fips")]
35-
extern crate aws_lc_fips_sys as aws_lc;
3640
#[cfg(feature = "aws-lc")]
37-
extern crate aws_lc_sys as aws_lc;
38-
pub use aws_lc::*;
41+
pub use aws_lc_sys::*;
3942

40-
// TODO: AWS-LC doesn't currently expose this in it's public headers
41-
extern "C" {
42-
pub fn OCSP_ONEREQ_free(r: *mut OCSP_ONEREQ);
43-
}
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")))]
47+
include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
48+
49+
// AWS-LC does not require initialization.
50+
pub fn init() {}
4451
}
45-
#[cfg(any(feature = "aws-lc", feature = "aws-lc-fips-sys"))]
52+
#[cfg(awslc)]
4653
pub use aws_lc::*;
4754

4855
#[cfg(openssl)]

0 commit comments

Comments
 (0)