Skip to content

Commit

Permalink
feat: Pre-generate the C header file (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcampbell authored Jun 27, 2024
1 parent 7f40fbc commit c765cf0
Show file tree
Hide file tree
Showing 7 changed files with 2,936 additions and 39 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/c-bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- name: build libraries
run: |
${{ matrix.setup-step || '' }}
cmake -S bindings/c -B build -DACCESSKIT_BUILD_HEADERS=OFF -DRust_CARGO_TARGET=${{ matrix.target }} ${{ matrix.cmake-options || '' }} ${{ !contains(matrix.target, 'msvc') && '-DCMAKE_BUILD_TYPE=Release' || '' }}
cmake -S bindings/c -B build -DRust_CARGO_TARGET=${{ matrix.target }} ${{ matrix.cmake-options || '' }} ${{ !contains(matrix.target, 'msvc') && '-DCMAKE_BUILD_TYPE=Release' || '' }}
cmake --build build ${{ contains(matrix.target, 'msvc') && '--config Release' || '' }}
cmake --install build ${{ contains(matrix.target, 'msvc') && '--config Release' || '' }}
Expand All @@ -75,11 +75,8 @@ jobs:
name: ${{ matrix.target }}
path: bindings/c/lib

generate-headers:
uses: AccessKit/accesskit/.github/workflows/generate-headers.yml@main

publish:
needs: [build-binaries, generate-headers]
needs: [build-binaries]
runs-on: ubuntu-latest
name: Publish
steps:
Expand All @@ -91,9 +88,8 @@ jobs:

- run: |
mkdir -p accesskit_c/lib
mv artifacts/headers accesskit_c/include
cp -r artifacts/*/* accesskit_c/lib
cp -r bindings/c/examples accesskit_c/
cp -r bindings/c/examples bindings/c/include accesskit_c/
cp bindings/c/accesskit*.cmake accesskit_c/
cp bindings/c/*.md accesskit_c/
cp LICENSE* accesskit_c/
Expand Down
17 changes: 15 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,18 @@ jobs:
if: matrix.os == 'windows-2019'
run: cargo test -p accesskit_windows

generate-headers:
uses: AccessKit/accesskit/.github/workflows/generate-headers.yml@main
check-c-header:
runs-on: ubuntu-latest
name: Check committed C header
steps:
- uses: actions/checkout@v4

- name: install nightly toolchain
uses: dtolnay/rust-toolchain@nightly

- uses: dtolnay/install@master
with:
crate: cbindgen
- run: cmake -S bindings/c -B build -DACCESSKIT_BUILD_HEADERS=ON -DACCESSKIT_BUILD_LIBRARIES=OFF
- run: cmake --build build
- run: cmp build/accesskit.h bindings/c/include/accesskit.h
25 changes: 0 additions & 25 deletions .github/workflows/generate-headers.yml

This file was deleted.

1 change: 0 additions & 1 deletion bindings/c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
build
include
lib
2 changes: 1 addition & 1 deletion bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)

project(accesskit_c)

option(ACCESSKIT_BUILD_HEADERS "Whether to build header files" ON)
option(ACCESSKIT_BUILD_HEADERS "Whether to build header files" OFF)
option(ACCESSKIT_BUILD_LIBRARIES "Whether to build libraries" ON)

if (ACCESSKIT_BUILD_LIBRARIES)
Expand Down
19 changes: 16 additions & 3 deletions bindings/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ Prerequisites:

- [Rust](https://rustup.rs/)
- [CMake](https://cmake.org/), version 3.20 or higher
- A nightly Rust toolchain: `rustup install nightly`
- [cbindgen](https://github.com/eqrion/cbindgen): `cargo install cbindgen`
- [clang-format](https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormat.html), version 14 or higher

Once inside the `bindings/c` directory, CMake can be used like this to build the project:

Expand All @@ -55,3 +52,19 @@ On other platforms you will have to specify which Rust target to use, as well as
```bash
cmake -S . -B build -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86 -DRust_CARGO_TARGET=i686-unknown-linux-gnu
```

### Regenerating the header file

If you modify the C bindings, you need to regenerate the header file and commit it. To do this, in addition to the above requirements, you will need:

- A nightly Rust toolchain: `rustup install nightly`
- [cbindgen](https://github.com/mozilla/cbindgen): `cargo install cbindgen`
- [clang-format](https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormat.html), version 14 or higher

Once you have these requirements, the process of regenerating the header file is similar to building and installing from source with CMake, but using different configuration options:

```bash
cmake -S . -B build -DACCESSKIT_BUILD_HEADERS=ON -DACCESSKIT_BUILD_LIBRARIES=OFF
cmake --build build
cmake --install build
```
Loading

0 comments on commit c765cf0

Please sign in to comment.