Skip to content

Commit

Permalink
cmake: use cargo capi to build lib
Browse files Browse the repository at this point in the history
This is _much_ easier, provides a real `install` command, and can
provide dynamic linking support that integrates well with `pkg-config`.
  • Loading branch information
cpu committed Nov 26, 2024
1 parent fc7468c commit 4dcd9e5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
44 changes: 42 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- nightly
# MSRV - keep in sync with what rustls and rustls-platform-verifier
# consider MSRV
- 1.71.0
- "1.71"
os: [ ubuntu-latest ]
# Include a few MacOS and cert-compression builds to ensure they're tested without
# bloating the matrix or slowing down CI.
Expand Down Expand Up @@ -59,6 +59,25 @@ jobs:
with:
toolchain: ${{ matrix.rust }}

- name: Install cargo-c (Ubuntu)
if: matrix.os == 'ubuntu-latest'
env:
# Version picked for MSRV compat.
LINK: https://github.com/lu-zero/cargo-c/releases/download/v0.10.0
CARGO_C_FILE: cargo-c-x86_64-unknown-linux-musl.tar.gz
run: |
curl -L $LINK/$CARGO_C_FILE | tar xz -C ~/.cargo/bin
- name: Install cargo-c (macOS)
if: matrix.os == 'macos-latest'
env:
# Version picked for MSRV compat.
LINK: https://github.com/lu-zero/cargo-c/releases/download/v0.10.0
CARGO_C_FILE: cargo-c-macos.zip
run: |
curl -L $LINK/$CARGO_C_FILE -o cargo-c-macos.zip
unzip cargo-c-macos.zip -d ~/.cargo/bin
- name: Setup cmake build
run: |
CC=${{matrix.cc}} \
Expand All @@ -70,6 +89,9 @@ jobs:
${{ matrix.os == 'macos-latest' && '-DCMAKE_OSX_DEPLOYMENT_TARGET=14.5' || '' }} \
-S . -B build
- name: Build
run: cmake --build build

- name: Platform verifier connect test
run: cmake --build build --target connect-test

Expand Down Expand Up @@ -105,6 +127,13 @@ jobs:
- name: Install valgrind
run: sudo apt-get update && sudo apt-get install -y valgrind

- name: Install cargo-c
env:
LINK: https://github.com/lu-zero/cargo-c/releases/latest/download
CARGO_C_FILE: cargo-c-x86_64-unknown-linux-musl.tar.gz
run: |
curl -L $LINK/$CARGO_C_FILE | tar xz -C ~/.cargo/bin
- name: Setup cmake build
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release # No ASAN w/ Valgrind

Expand All @@ -126,6 +155,13 @@ jobs:
- name: Install nightly rust toolchain
uses: dtolnay/rust-toolchain@nightly

- name: Install cargo-c
env:
LINK: https://github.com/lu-zero/cargo-c/releases/latest/download
CARGO_C_FILE: cargo-c-x86_64-unknown-linux-musl.tar.gz
run: |
curl -L $LINK/$CARGO_C_FILE | tar xz -C ~/.cargo/bin
- name: Setup cmake build
run: |
cmake \
Expand Down Expand Up @@ -166,6 +202,10 @@ jobs:
with:
arch: x64

# TODO(@cpu): install pre-built cargo-c similar to other platforms. This is slowww.
- name: Install cargo-c
run: cargo install cargo-c

- name: Configure CMake
run: cmake -DCRYPTO_PROVIDER="${{ matrix.crypto }}" -DCERT_COMPRESSION="${{ matrix.cert_compression }}" -S . -B build

Expand Down Expand Up @@ -247,7 +287,7 @@ jobs:
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.71.0
toolchain: "1.71"
components: rustfmt

- name: Install Gersemi
Expand Down
9 changes: 5 additions & 4 deletions cmake/rust.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ ExternalProject_Add(
rustls-ffi
DOWNLOAD_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
COMMAND
cargo build --locked ${CARGO_FEATURES}
BUILD_COMMAND
cargo capi build --locked ${CARGO_FEATURES}
"$<IF:$<CONFIG:Release>,--release,-->"
# Rely on cargo checking timestamps, rather than tell CMake where every
# output is.
BUILD_ALWAYS true
INSTALL_COMMAND ""
INSTALL_COMMAND
cargo capi install --libdir=lib --prefix=${CMAKE_BINARY_DIR}/rust
--locked ${CARGO_FEATURES} "$<IF:$<CONFIG:Release>,--release,-->"
# Run cargo test with --quiet because msbuild will treat the presence
# of "error" in stdout as an error, and we have some test functions that
# end in "_error". Quiet mode suppresses test names, so this is a
Expand Down
13 changes: 8 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ function(test_binary target_name)
target_sources(${target_name} PRIVATE ${target_name}.c common.c common.h)
add_dependencies(${target_name} rustls-ffi)

target_include_directories(${target_name} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(
${target_name}
PRIVATE ${CMAKE_BINARY_DIR}/rust/include
)

if(WIN32)
target_compile_options(${target_name} PRIVATE ${sanitizer_flags})
target_link_libraries(
${target_name}
debug
"${CMAKE_SOURCE_DIR}/target/debug/rustls_ffi.lib"
"${CMAKE_BINARY_DIR}/rust/lib/rustls.lib"
optimized
"${CMAKE_SOURCE_DIR}/target/release/rustls_ffi.lib"
"${CMAKE_BINARY_DIR}/rust/lib/rustls.lib"
advapi32.lib
bcrypt.lib
crypt32.lib
Expand Down Expand Up @@ -78,9 +81,9 @@ function(test_binary target_name)
target_link_libraries(
${target_name}
debug
"${CMAKE_SOURCE_DIR}/target/debug/librustls_ffi.a"
"${CMAKE_BINARY_DIR}/rust/lib/librustls.a"
optimized
"${CMAKE_SOURCE_DIR}/target/release/librustls_ffi.a"
"${CMAKE_BINARY_DIR}/rust/lib/librustls.a"
)
if(CERT_COMPRESSION)
target_link_libraries(${target_name} m)
Expand Down

0 comments on commit 4dcd9e5

Please sign in to comment.