Skip to content

Commit fdc67f9

Browse files
committed
Added support for building boringssl with bindgen
This allows building it without the bssl-sys crate. This is an alternative approach to fixing sfackler#1768 (in contrast to sfackler#1806). This maintains support for using the bssl-sys crate.
1 parent 278279f commit fdc67f9

File tree

11 files changed

+132
-46
lines changed

11 files changed

+132
-46
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ jobs:
153153
- false
154154
library:
155155
- name: boringssl
156-
version: 5697a9202615925696f8dc7f4e286d44d474769e
156+
version: 93e8d4463d59d671e9c5c6171226341f04b07907
157157
- name: openssl
158158
version: vendored
159159
- name: openssl
@@ -215,10 +215,6 @@ jobs:
215215
library:
216216
name: libressl
217217
version: 3.7.0
218-
exclude:
219-
- library:
220-
name: boringssl
221-
bindgen: true
222218
name: ${{ matrix.target }}-${{ matrix.library.name }}-${{ matrix.library.version }}-${{ matrix.bindgen }}
223219
runs-on: ubuntu-latest
224220
env:
@@ -311,24 +307,20 @@ jobs:
311307
make install_sw
312308
;;
313309
"boringssl")
314-
sed -i rust/CMakeLists.txt -e '1s%^%include_directories(../include)\n%'
315-
cpu=`echo ${{ matrix.target }} | cut -d - -f 1`
316-
echo "set(CMAKE_SYSTEM_NAME Linux)" > toolchain.cmake
317-
echo "set(CMAKE_SYSTEM_PROCESSOR $cpu)" >> toolchain.cmake
318-
echo "set(triple ${{ matrix.target }})" >> toolchain.cmake
319-
echo 'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} '$OS_FLAGS '" CACHE STRING "c++ flags")' >> toolchain.cmake
320-
echo 'set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} '$OS_FLAGS '" CACHE STRING "c flags")' >> toolchain.cmake
321-
echo 'set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} '$OS_FLAGS '" CACHE STRING "asm flags")' >> toolchain.cmake
322-
cmake -DRUST_BINDINGS="${{ matrix.target }}" -B $OPENSSL_DIR -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake
323-
make -C $OPENSSL_DIR
310+
mkdir build
311+
cd build
312+
cmake .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DRUST_BINDINGS="${{ matrix.target }}" -DCMAKE_INSTALL_PREFIX="${OPENSSL_DIR}"
313+
make -j "$(nproc)"
314+
make install
315+
cp -r rust/ "$OPENSSL_DIR/rust"
324316
esac
325317
326318
if: matrix.library.version != 'vendored' && !steps.openssl-cache.outputs.cache-hit
327319
- run: |
328320
mkdir -p .cargo
329321
echo '[patch.crates-io]' > .cargo/config.toml
330322
echo 'bssl-sys = { path = "'$OPENSSL_DIR'/rust" }' >> .cargo/config.toml
331-
if: matrix.library.name == 'boringssl'
323+
if: matrix.library.name == 'boringssl' && !matrix.bindgen
332324
- uses: actions/cache@v1
333325
with:
334326
path: ~/.cargo/registry/index
@@ -350,21 +342,25 @@ jobs:
350342
if [[ "${{ matrix.library.version }}" == "vendored" ]]; then
351343
features="--features vendored"
352344
fi
353-
if [[ "${{ matrix.bindgen }}" == "true" ]]; then
345+
if [[ "${{ matrix.bindgen }}" == "true" && "${{ matrix.library.name }}" != "boringssl" ]]; then
354346
features="$features --features bindgen"
355347
fi
356348
cargo run --manifest-path=systest/Cargo.toml --target ${{ matrix.target }} $features
357349
if: matrix.library.name != 'boringssl'
358350
- name: Test openssl
359351
run: |
360-
if [[ "${{ matrix.library.name }}" == "boringssl" ]]; then
361-
features="--features unstable_boringssl"
362-
fi
363-
if [[ "${{ matrix.library.version }}" == "vendored" ]]; then
364-
features="--features vendored"
365-
fi
366-
if [[ "${{ matrix.bindgen }}" == "true" ]]; then
367-
features="$features --features bindgen"
352+
if [[ "${{ matrix.library.name }}" == "boringssl" && "${{ matrix.bindgen }}" == "true" ]]; then
353+
features="--features unstable_boringssl_bindgen"
354+
else
355+
if [[ "${{ matrix.library.name }}" == "boringssl" ]]; then
356+
features="--features unstable_boringssl"
357+
fi
358+
if [[ "${{ matrix.library.version }}" == "vendored" ]]; then
359+
features="--features vendored"
360+
fi
361+
if [[ "${{ matrix.bindgen }}" == "true" ]]; then
362+
features="$features --features bindgen"
363+
fi
368364
fi
369365
cargo test --manifest-path=openssl/Cargo.toml --target ${{ matrix.target }} $features
370366
- name: Test openssl-errors

openssl-sys/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ edition = "2018"
1717
[features]
1818
vendored = ['openssl-src']
1919
unstable_boringssl = ['bssl-sys']
20+
unstable_boringssl_bindgen = ['bindgen']
2021

2122
[dependencies]
2223
libc = "0.2"
2324
bssl-sys = { version = "0.1.0", optional = true }
2425

2526
[build-dependencies]
26-
bindgen = { version = "0.60.1", optional = true }
27+
bindgen = { version = "0.64.0", optional = true, features = ["experimental"] }
2728
cc = "1.0"
2829
openssl-src = { version = "111", optional = true }
2930
pkg-config = "0.3.9"

openssl-sys/build/main.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod cfgs;
2323
mod find_normal;
2424
#[cfg(feature = "vendored")]
2525
mod find_vendored;
26-
#[cfg(feature = "bindgen")]
26+
#[cfg(any(feature = "bindgen", feature = "unstable_boringssl_bindgen"))]
2727
mod run_bindgen;
2828

2929
#[derive(PartialEq)]
@@ -32,6 +32,7 @@ enum Version {
3232
Openssl11x,
3333
Openssl10x,
3434
Libressl,
35+
Boringssl,
3536
}
3637

3738
fn env_inner(name: &str) -> Option<OsString> {
@@ -69,8 +70,6 @@ fn check_ssl_kind() {
6970
println!("cargo:rustc-cfg=boringssl");
7071
// BoringSSL does not have any build logic, exit early
7172
std::process::exit(0);
72-
} else {
73-
println!("cargo:rustc-cfg=openssl");
7473
}
7574
}
7675

@@ -146,8 +145,12 @@ fn check_rustc_versions() {
146145
#[allow(clippy::let_and_return)]
147146
fn postprocess(include_dirs: &[PathBuf]) -> Version {
148147
let version = validate_headers(include_dirs);
149-
#[cfg(feature = "bindgen")]
150-
run_bindgen::run(&include_dirs);
148+
149+
// Never run bindgen for BoringSSL, if it was needed we already ran it.
150+
if version != Version::Boringssl {
151+
#[cfg(feature = "bindgen")]
152+
run_bindgen::run(&include_dirs);
153+
}
151154

152155
version
153156
}
@@ -235,9 +238,20 @@ See rust-openssl documentation for more information:
235238
}
236239

237240
if is_boringssl {
238-
panic!("BoringSSL detected, but `unstable_boringssl` feature wasn't specified.")
241+
if !cfg!(feature = "unstable_boringssl_bindgen") {
242+
panic!("BoringSSL detected, neither the `unstable_boringssl` nor `unstable_boringssl_bindgen` features were specified.")
243+
}
244+
println!("cargo:rustc-cfg=boringssl");
245+
println!("cargo:boringssl=true");
246+
// Necessary for the compiler
247+
#[cfg(feature = "unstable_boringssl_bindgen")]
248+
run_bindgen::run_boringssl(include_dirs);
249+
return Version::Boringssl;
239250
}
240251

252+
// We set this for any non-BoringSSL lib.
253+
println!("cargo:rustc-cfg=openssl");
254+
241255
for enabled in &enabled {
242256
println!("cargo:rustc-cfg=osslconf=\"{}\"", enabled);
243257
}

openssl-sys/build/run_bindgen.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
2-
use bindgen::RustTarget;
3-
use std::env;
2+
use bindgen::{MacroTypeVariation, RustTarget};
3+
use std::io::Write;
44
use std::path::PathBuf;
5+
use std::{env, fs};
56

67
const INCLUDES: &str = "
78
#include <openssl/aes.h>
89
#include <openssl/asn1.h>
910
#include <openssl/bio.h>
10-
#include <openssl/comp.h>
1111
#include <openssl/conf.h>
1212
#include <openssl/crypto.h>
1313
#include <openssl/dh.h>
@@ -17,7 +17,6 @@ const INCLUDES: &str = "
1717
#include <openssl/evp.h>
1818
#include <openssl/hmac.h>
1919
#include <openssl/objects.h>
20-
#include <openssl/ocsp.h>
2120
#include <openssl/opensslv.h>
2221
#include <openssl/pem.h>
2322
#include <openssl/pkcs12.h>
@@ -35,10 +34,15 @@ const INCLUDES: &str = "
3534
// this must be included after ssl.h for libressl!
3635
#include <openssl/srtp.h>
3736
38-
#if !defined(LIBRESSL_VERSION_NUMBER)
37+
#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
3938
#include <openssl/cms.h>
4039
#endif
4140
41+
#if !defined(OPENSSL_IS_BORINGSSL)
42+
#include <openssl/comp.h>
43+
#include <openssl/ocsp.h>
44+
#endif
45+
4246
#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
4347
#include <openssl/kdf.h>
4448
#endif
@@ -94,6 +98,52 @@ pub fn run(include_dirs: &[PathBuf]) {
9498
.unwrap();
9599
}
96100

101+
pub fn run_boringssl(include_dirs: &[PathBuf]) {
102+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
103+
let mut builder = bindgen::builder()
104+
.rust_target(RustTarget::Stable_1_47)
105+
.ctypes_prefix("::libc")
106+
.derive_default(false)
107+
.enable_function_attribute_detection()
108+
.size_t_is_usize(true)
109+
.default_macro_constant_type(MacroTypeVariation::Signed)
110+
.rustified_enum("point_conversion_form_t")
111+
.allowlist_file(".*/openssl/[^/]+\\.h")
112+
.wrap_static_fns(true)
113+
.wrap_static_fns_path(out_dir.join("boring_static_wrapper").display().to_string())
114+
.layout_tests(false)
115+
.header_contents("includes.h", INCLUDES);
116+
117+
for include_dir in include_dirs {
118+
builder = builder
119+
.clang_arg("-I")
120+
.clang_arg(include_dir.display().to_string());
121+
}
122+
123+
builder
124+
.generate()
125+
.unwrap()
126+
.write_to_file(out_dir.join("bindgen.rs"))
127+
.unwrap();
128+
129+
fs::File::create(out_dir.join("boring_static_wrapper.h"))
130+
.expect("Failed to create boring_static_wrapper.h")
131+
.write_all(INCLUDES.as_bytes())
132+
.expect("Failed to write contents to boring_static_wrapper.h");
133+
134+
cc::Build::new()
135+
.file(out_dir.join("boring_static_wrapper.c"))
136+
.includes(include_dirs)
137+
.flag("-include")
138+
.flag(
139+
&out_dir
140+
.join("boring_static_wrapper.h")
141+
.display()
142+
.to_string(),
143+
)
144+
.compile("boring_static_wrapper");
145+
}
146+
97147
#[derive(Debug)]
98148
struct OpensslCallbacks;
99149

openssl-sys/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@
1616
extern crate libc;
1717
pub use libc::*;
1818

19-
#[cfg(boringssl)]
19+
#[cfg(feature = "unstable_boringssl")]
2020
extern crate bssl_sys;
21-
#[cfg(boringssl)]
21+
#[cfg(feature = "unstable_boringssl")]
2222
pub use bssl_sys::*;
2323

24+
#[cfg(all(feature = "unstable_boringssl_bindgen", boringssl))]
25+
#[path = "."]
26+
mod boringssl {
27+
include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
28+
29+
pub fn init() {
30+
unsafe {
31+
CRYPTO_library_init();
32+
}
33+
}
34+
}
35+
#[cfg(all(feature = "unstable_boringssl_bindgen", boringssl))]
36+
pub use boringssl::*;
37+
2438
#[cfg(openssl)]
2539
#[path = "."]
2640
mod openssl {

openssl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ v111 = []
2020
vendored = ['ffi/vendored']
2121
bindgen = ['ffi/bindgen']
2222
unstable_boringssl = ["ffi/unstable_boringssl"]
23+
unstable_boringssl_bindgen = ["ffi/unstable_boringssl_bindgen"]
2324
default = []
2425

2526
[dependencies]

openssl/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
println!("cargo:rustc-cfg=libressl");
1212
}
1313

14-
if env::var("CARGO_FEATURE_UNSTABLE_BORINGSSL").is_ok() {
14+
if env::var("DEP_OPENSSL_BORINGSSL").is_ok() {
1515
println!("cargo:rustc-cfg=boringssl");
1616
return;
1717
}

openssl/src/bio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a> MemBioSlice<'a> {
2525
let bio = unsafe {
2626
cvt_p(BIO_new_mem_buf(
2727
buf.as_ptr() as *const _,
28-
buf.len() as c_int,
28+
buf.len() as crate::SLenType,
2929
))?
3030
};
3131

@@ -74,7 +74,7 @@ impl MemBio {
7474
}
7575

7676
cfg_if! {
77-
if #[cfg(ossl102)] {
77+
if #[cfg(any(ossl102, boringssl))] {
7878
use ffi::BIO_new_mem_buf;
7979
} else {
8080
#[allow(bad_style)]

openssl/src/dh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ where
239239
}
240240

241241
cfg_if! {
242-
if #[cfg(any(ossl110, libressl270))] {
242+
if #[cfg(any(ossl110, libressl270, boringssl))] {
243243
use ffi::{DH_set0_pqg, DH_get0_pqg, DH_get0_key, DH_set0_key};
244244
} else {
245245
#[allow(bad_style)]

openssl/src/error.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,24 @@ impl fmt::Debug for Error {
297297
}
298298

299299
impl fmt::Display for Error {
300+
// On BoringSSL ERR_GET_{LIB,FUNC,REASON} are `unsafe`, but on
301+
// OpenSSL/LibreSSL they're safe.
302+
#[allow(unused_unsafe)]
300303
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
301304
write!(fmt, "error:{:08X}", self.code())?;
302305
match self.library() {
303306
Some(l) => write!(fmt, ":{}", l)?,
304-
None => write!(fmt, ":lib({})", ffi::ERR_GET_LIB(self.code()))?,
307+
None => write!(fmt, ":lib({})", unsafe { ffi::ERR_GET_LIB(self.code()) })?,
305308
}
306309
match self.function() {
307310
Some(f) => write!(fmt, ":{}", f)?,
308-
None => write!(fmt, ":func({})", ffi::ERR_GET_FUNC(self.code()))?,
311+
None => write!(fmt, ":func({})", unsafe { ffi::ERR_GET_FUNC(self.code()) })?,
309312
}
310313
match self.reason() {
311314
Some(r) => write!(fmt, ":{}", r)?,
312-
None => write!(fmt, ":reason({})", ffi::ERR_GET_REASON(self.code()))?,
315+
None => write!(fmt, ":reason({})", unsafe {
316+
ffi::ERR_GET_REASON(self.code())
317+
})?,
313318
}
314319
write!(
315320
fmt,

openssl/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ type LenType = libc::size_t;
190190
#[cfg(not(boringssl))]
191191
type LenType = libc::c_int;
192192

193+
#[cfg(boringssl)]
194+
type SLenType = libc::ssize_t;
195+
#[cfg(not(boringssl))]
196+
type SLenType = libc::c_int;
197+
193198
#[inline]
194199
fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
195200
if r.is_null() {

0 commit comments

Comments
 (0)