Skip to content

Commit e6b83a3

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 3acc5bb commit e6b83a3

File tree

9 files changed

+44
-41
lines changed

9 files changed

+44
-41
lines changed

.github/workflows/ci.yml

+5-18
Original file line numberDiff line numberDiff line change
@@ -308,24 +308,14 @@ jobs:
308308
make install_sw
309309
;;
310310
"boringssl")
311-
sed -i rust/CMakeLists.txt -e '1s%^%include_directories(../include)\n%'
312-
cpu=`echo ${{ matrix.target }} | cut -d - -f 1`
313-
echo "set(CMAKE_SYSTEM_NAME Linux)" > toolchain.cmake
314-
echo "set(CMAKE_SYSTEM_PROCESSOR $cpu)" >> toolchain.cmake
315-
echo "set(triple ${{ matrix.target }})" >> toolchain.cmake
316-
echo 'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} '$OS_FLAGS '" CACHE STRING "c++ flags")' >> toolchain.cmake
317-
echo 'set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} '$OS_FLAGS '" CACHE STRING "c flags")' >> toolchain.cmake
318-
echo 'set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} '$OS_FLAGS '" CACHE STRING "asm flags")' >> toolchain.cmake
319-
cmake -DRUST_BINDINGS="${{ matrix.target }}" -B $OPENSSL_DIR -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake
320-
make -C $OPENSSL_DIR
311+
mkdir build
312+
cd build
313+
cmake .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DRUST_BINDINGS="${{ matrix.target }}" -DCMAKE_INSTALL_PREFIX="${OPENSSL_DIR}"
314+
make -j "$(nproc)"
315+
make install
321316
esac
322317
323318
if: matrix.library.version != 'vendored' && !steps.openssl-cache.outputs.cache-hit
324-
- run: |
325-
mkdir -p .cargo
326-
echo '[patch.crates-io]' > .cargo/config.toml
327-
echo 'bssl-sys = { path = "'$OPENSSL_DIR'/rust" }' >> .cargo/config.toml
328-
if: matrix.library.name == 'boringssl'
329319
- uses: actions/cache@v1
330320
with:
331321
path: ~/.cargo/registry/index
@@ -354,9 +344,6 @@ jobs:
354344
if: matrix.library.name != 'boringssl'
355345
- name: Test openssl
356346
run: |
357-
if [[ "${{ matrix.library.name }}" == "boringssl" ]]; then
358-
features="--features unstable_boringssl"
359-
fi
360347
if [[ "${{ matrix.library.version }}" == "vendored" ]]; then
361348
features="--features vendored"
362349
fi

openssl-sys/build/main.rs

+14-13
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

+11-2
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

-1
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

+1-1
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

+2-2
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

+1-1
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

+5-3
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,17 @@ impl fmt::Display for Error {
301301
write!(fmt, "error:{:08X}", self.code())?;
302302
match self.library() {
303303
Some(l) => write!(fmt, ":{}", l)?,
304-
None => write!(fmt, ":lib({})", ffi::ERR_GET_LIB(self.code()))?,
304+
None => write!(fmt, ":lib({})", unsafe { ffi::ERR_GET_LIB(self.code()) })?,
305305
}
306306
match self.function() {
307307
Some(f) => write!(fmt, ":{}", f)?,
308-
None => write!(fmt, ":func({})", ffi::ERR_GET_FUNC(self.code()))?,
308+
None => write!(fmt, ":func({})", unsafe { ffi::ERR_GET_FUNC(self.code()) })?,
309309
}
310310
match self.reason() {
311311
Some(r) => write!(fmt, ":{}", r)?,
312-
None => write!(fmt, ":reason({})", ffi::ERR_GET_REASON(self.code()))?,
312+
None => write!(fmt, ":reason({})", unsafe {
313+
ffi::ERR_GET_REASON(self.code())
314+
})?,
313315
}
314316
write!(
315317
fmt,

openssl/src/lib.rs

+5
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)