Skip to content

Commit 95a0390

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 11797d9 commit 95a0390

File tree

10 files changed

+45
-41
lines changed

10 files changed

+45
-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/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ readme = "README.md"
1212
categories = ["cryptography", "external-ffi-bindings"]
1313
links = "openssl"
1414
build = "build/main.rs"
15+
edition = "2018"
1516

1617
[features]
1718
vendored = ['openssl-src']

openssl-sys/build/main.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum Version {
2828
Openssl11x,
2929
Openssl10x,
3030
Libressl,
31+
Boringssl,
3132
}
3233

3334
fn env_inner(name: &str) -> Option<OsString> {
@@ -60,21 +61,9 @@ fn find_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) {
6061
find_normal::get_openssl(target)
6162
}
6263

63-
fn check_ssl_kind() {
64-
if cfg!(feature = "unstable_boringssl") {
65-
println!("cargo:rustc-cfg=boringssl");
66-
// BoringSSL does not have any build logic, exit early
67-
std::process::exit(0);
68-
} else {
69-
println!("cargo:rustc-cfg=openssl");
70-
}
71-
}
72-
7364
fn main() {
7465
check_rustc_versions();
7566

76-
check_ssl_kind();
77-
7867
let target = env::var("TARGET").unwrap();
7968

8069
let (lib_dirs, include_dir) = find_openssl(&target);
@@ -231,9 +220,21 @@ See rust-openssl documentation for more information:
231220
}
232221

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

237+
println!("cargo:rustc-cfg=openssl");
237238
for enabled in &enabled {
238239
println!("cargo:rustc-cfg=osslconf=\"{}\"", enabled);
239240
}

openssl-sys/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ extern crate libc;
1616
pub use libc::*;
1717

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

2332
#[cfg(openssl)]
2433
#[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
@@ -7,7 +7,7 @@ fn main() {
77
println!("cargo:rustc-cfg=libressl");
88
}
99

10-
if env::var("CARGO_FEATURE_UNSTABLE_BORINGSSL").is_ok() {
10+
if env::var("DEP_OPENSSL_BORINGSSL").is_ok() {
1111
println!("cargo:rustc-cfg=boringssl");
1212
return;
1313
}

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
@@ -189,6 +189,11 @@ type LenType = libc::size_t;
189189
#[cfg(not(boringssl))]
190190
type LenType = libc::c_int;
191191

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

0 commit comments

Comments
 (0)