Skip to content

Commit e4ec997

Browse files
committed
fixes sfackler#1768 -- integrate boringssl into the build process more naturally
This PR uses the in-development bindgen support for static inline functions (rust-lang/rust-bindgen#2335) + an in-development boringssl patch (https://boringssl-review.googlesource.com/c/boringssl/+/56505) to allow using boringssl with rust-openssl without needing a .cargo/config override
1 parent 611405a commit e4ec997

File tree

9 files changed

+47
-41
lines changed

9 files changed

+47
-41
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,24 +311,14 @@ jobs:
311311
make install_sw
312312
;;
313313
"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
314+
mkdir build
315+
cd build
316+
cmake .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DRUST_BINDINGS="${{ matrix.target }}" -DCMAKE_INSTALL_PREFIX="${OPENSSL_DIR}"
317+
make -j "$(nproc)"
318+
make install
324319
esac
325320
326321
if: matrix.library.version != 'vendored' && !steps.openssl-cache.outputs.cache-hit
327-
- run: |
328-
mkdir -p .cargo
329-
echo '[patch.crates-io]' > .cargo/config.toml
330-
echo 'bssl-sys = { path = "'$OPENSSL_DIR'/rust" }' >> .cargo/config.toml
331-
if: matrix.library.name == 'boringssl'
332322
- uses: actions/cache@v1
333323
with:
334324
path: ~/.cargo/registry/index
@@ -357,9 +347,6 @@ jobs:
357347
if: matrix.library.name != 'boringssl'
358348
- name: Test openssl
359349
run: |
360-
if [[ "${{ matrix.library.name }}" == "boringssl" ]]; then
361-
features="--features unstable_boringssl"
362-
fi
363350
if [[ "${{ matrix.library.version }}" == "vendored" ]]; then
364351
features="--features vendored"
365352
fi

openssl-sys/build/main.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum Version {
3232
Openssl11x,
3333
Openssl10x,
3434
Libressl,
35+
Boringssl,
3536
}
3637

3738
fn env_inner(name: &str) -> Option<OsString> {
@@ -64,21 +65,9 @@ fn find_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) {
6465
find_normal::get_openssl(target)
6566
}
6667

67-
fn check_ssl_kind() {
68-
if cfg!(feature = "unstable_boringssl") {
69-
println!("cargo:rustc-cfg=boringssl");
70-
// BoringSSL does not have any build logic, exit early
71-
std::process::exit(0);
72-
} else {
73-
println!("cargo:rustc-cfg=openssl");
74-
}
75-
}
76-
7768
fn main() {
7869
check_rustc_versions();
7970

80-
check_ssl_kind();
81-
8271
let target = env::var("TARGET").unwrap();
8372

8473
let (lib_dirs, include_dir) = find_openssl(&target);
@@ -235,9 +224,21 @@ See rust-openssl documentation for more information:
235224
}
236225

237226
if is_boringssl {
238-
panic!("BoringSSL detected, but `unstable_boringssl` feature wasn't specified.")
227+
let rust_dir = include_dirs[0].join("..").join("rust");
228+
println!("cargo:rustc-cfg=boringssl");
229+
println!("cargo:boringssl=true");
230+
println!(
231+
"cargo:rustc-env=BORINGSSL_RUST_WRAPPER={}/wrapper_{}.rs",
232+
rust_dir.display(),
233+
env::var("TARGET").unwrap()
234+
);
235+
println!("cargo:rustc-link-search=native={}", rust_dir.display());
236+
println!("cargo:rustc-link-lib=static=rust_wrapper");
237+
// BoringSSL does not have any additional build logic, exit early
238+
return Version::Boringssl;
239239
}
240240

241+
println!("cargo:rustc-cfg=openssl");
241242
for enabled in &enabled {
242243
println!("cargo:rustc-cfg=osslconf=\"{}\"", enabled);
243244
}

openssl-sys/src/lib.rs

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

1919
#[cfg(boringssl)]
20-
extern crate bssl_sys;
20+
#[path = "."]
21+
mod boringssl {
22+
include!(env!("BORINGSSL_RUST_WRAPPER"));
23+
24+
pub fn init() {
25+
unsafe {
26+
CRYPTO_library_init();
27+
}
28+
}
29+
}
2130
#[cfg(boringssl)]
22-
pub use bssl_sys::*;
31+
pub use boringssl::*;
2332

2433
#[cfg(openssl)]
2534
#[path = "."]

openssl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ v111 = []
1919

2020
vendored = ['ffi/vendored']
2121
bindgen = ['ffi/bindgen']
22-
unstable_boringssl = ["ffi/unstable_boringssl"]
2322
default = []
2423

2524
[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)