Skip to content

Commit

Permalink
feat: Add CMake support to C bindings (#247)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Campbell <mattcampbell@pobox.com>
  • Loading branch information
DataTriny and mwcampbell authored Jun 24, 2023
1 parent 022ef04 commit 3f556c9
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 129 deletions.
41 changes: 8 additions & 33 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
Expand All @@ -12,6 +11,7 @@ AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
Expand Down Expand Up @@ -59,28 +59,23 @@ BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: NextLine
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
StatementAttributeLikeMacros:
- Q_EMIT
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Expand All @@ -101,7 +96,6 @@ IncludeCategories:
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
Expand All @@ -114,7 +108,6 @@ InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
Expand All @@ -128,14 +121,12 @@ PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PenaltyIndentedWhitespace: 0
PointerAlignment: Left
PPIndentWidth: -1
RawStringFormats:
- Language: Cpp
Delimiters:
Expand Down Expand Up @@ -164,14 +155,10 @@ RawStringFormats:
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
CanonicalDelimiter: ''
BasedOnStyle: google
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortIncludes: true
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
Expand All @@ -183,33 +170,20 @@ SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
Expand All @@ -223,3 +197,4 @@ WhitespaceSensitiveMacros:
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

64 changes: 20 additions & 44 deletions .github/workflows/bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,44 @@ jobs:
include:
- os: macOS-latest
target: aarch64-apple-darwin
cmake-options: -DCMAKE_OSX_ARCHITECTURES=arm64
path: macos/arm64
shared-ext: dylib
static-ext: a
- os: macOS-latest
target: x86_64-apple-darwin
path: macos/x86_64
shared-ext: dylib
static-ext: a
- os: windows-2019
target: aarch64-pc-windows-msvc
setup-step: 'cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsamd64_x86.bat" `& powershell'
cmake-options: -A ARM64
path: windows/arm64/msvc
shared-ext: dll
static-ext: lib
- os: windows-2019
target: i686-pc-windows-msvc
setup-step: 'cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars32.bat" `& powershell'
cmake-options: -A Win32
path: windows/x86/msvc
shared-ext: dll
static-ext: lib
- os: windows-2019
target: x86_64-pc-windows-msvc
setup-step: 'cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" `& powershell'
path: windows/x86_64/msvc
shared-ext: dll
static-ext: lib
- os: ubuntu-latest
target: i686-pc-windows-gnu
use-cross: true
setup-step: sudo apt update && sudo apt install -y mingw-w64
cmake-options: -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=x86 -DCMAKE_C_COMPILER=i686-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=i686-w64-mingw32-g++
path: windows/x86/mingw
shared-ext: dll
static-ext: a
- os: ubuntu-latest
target: x86_64-pc-windows-gnu
use-cross: true
setup-step: sudo apt update && sudo apt install -y mingw-w64
cmake-options: -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=x86_64 -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++
path: windows/x86_64/mingw
shared-ext: dll
static-ext: a
- os: ubuntu-latest
target: i686-unknown-linux-gnu
use-cross: true
setup-step: sudo apt update && sudo apt install -y gcc-multilib g++-multilib
cmake-options: -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86
path: linux/x86
shared-ext: so
static-ext: a
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
path: linux/x86_64
shared-ext: so
static-ext: a
name: cargo build
name: Build
steps:
- uses: actions/checkout@v3

Expand All @@ -70,34 +61,18 @@ jobs:
profile: minimal
override: true

- name: cargo build
uses: actions-rs/cargo@v1
with:
command: build
use-cross: ${{ matrix.use-cross || false }}
args: --package accesskit_c --release --target ${{ matrix.target }}

- shell: bash
run: |
mkdir -p artifacts/${{ matrix.path }}/shared
mkdir -p artifacts/${{ matrix.path }}/static
- if: startsWith(matrix.os, 'windows')
- name: build libraries
run: |
mv target/${{ matrix.target }}/release/accesskit.dll.lib artifacts/${{ matrix.path }}/shared/accesskit.lib
mv target/${{ matrix.target }}/release/*.pdb artifacts/${{ matrix.path }}/shared
- if: contains(matrix.path, 'mingw')
run: mv target/${{ matrix.target }}/release/libaccesskit.dll.a artifacts/${{ matrix.path }}/shared/libaccesskit.a

- run: |
mv target/${{ matrix.target }}/release/*.${{ matrix.shared-ext }} artifacts/${{ matrix.path }}/shared
mv target/${{ matrix.target }}/release/*.${{ matrix.static-ext }} artifacts/${{ matrix.path }}/static
${{ 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 --build build ${{ contains(matrix.target, 'msvc') && '--config Release' || '' }}
cmake --install build ${{ contains(matrix.target, 'msvc') && '--config Release' || '' }}
- name: Upload binaries
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.target }}
path: artifacts
path: bindings/c/lib

generate-headers:
if: startsWith(github.ref_name, 'accesskit_c-v')
Expand All @@ -119,6 +94,7 @@ jobs:
mv artifacts/headers accesskit_c/include
cp -r artifacts/*/* accesskit_c/lib
cp -r bindings/c/examples accesskit_c/
cp bindings/c/accesskit*.cmake accesskit_c/
cp bindings/c/*.md accesskit_c/
cp LICENSE* accesskit_c/
mv accesskit_c ${{ github.ref_name }}
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/generate-headers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
- uses: dtolnay/install@master
with:
crate: cbindgen
- run: make -C bindings/c include/accesskit.h
- run: cmake -S bindings/c -B build -DACCESSKIT_BUILD_LIBRARIES=OFF
- run: cmake --build build
- run: cmake --install build

- name: Upload header file
uses: actions/upload-artifact@v3
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default-members = [
"common",
"consumer",
"platforms/winit",
"bindings/c",
]

[profile.release]
Expand Down
9 changes: 9 additions & 0 deletions bindings/c/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[target.aarch64-apple-darwin]
rustflags = [
"-C", "link-arg=-Wl,-install_name,@rpath/libaccesskit.dylib",
]

[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-Wl,-install_name,@rpath/libaccesskit.dylib",
]
5 changes: 3 additions & 2 deletions bindings/c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

include
build
include
lib
66 changes: 66 additions & 0 deletions bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
cmake_minimum_required(VERSION 3.20)

project(accesskit_c)

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

if (ACCESSKIT_BUILD_LIBRARIES)
include(FetchContent)

FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.3.5
)
FetchContent_MakeAvailable(Corrosion)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
corrosion_import_crate(MANIFEST_PATH Cargo.toml)
endif()

if (ACCESSKIT_BUILD_HEADERS)
find_program(RUSTUP rustup)
find_program(CBINDGEN cbindgen)
find_program(CLANG_FORMAT clang-format)

add_custom_target(headers ALL
COMMAND ${RUSTUP} run nightly ${CBINDGEN} --crate accesskit_c --output accesskit.hpp "${CMAKE_SOURCE_DIR}"
COMMAND ${CLANG_FORMAT} -i accesskit.hpp
COMMAND ${CMAKE_COMMAND} -E rename accesskit.hpp accesskit.h
BYPRODUCTS accesskit.h
)

if (ACCESSKIT_BUILD_LIBRARIES)
add_dependencies(cargo-prebuild_accesskit headers)
endif()
endif()

include("accesskit.cmake")

if (ACCESSKIT_BUILD_HEADERS)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/accesskit.h"
DESTINATION "${ACCESSKIT_INCLUDE_DIR}"
)
endif()

if (ACCESSKIT_BUILD_LIBRARIES)
install(FILES
"$<TARGET_PROPERTY:accesskit-static,IMPORTED_LOCATION>"
DESTINATION "${ACCESSKIT_LIBRARIES_DIR}/static"
)

install(FILES
"$<TARGET_PROPERTY:accesskit-shared,IMPORTED_LOCATION>"
"$<$<STREQUAL:${_accesskit_toolchain},msvc>:${CMAKE_CURRENT_BINARY_DIR}/accesskit.pdb>"
DESTINATION "${ACCESSKIT_LIBRARIES_DIR}/shared"
)
install(FILES
"$<$<NOT:$<STREQUAL:$<TARGET_PROPERTY:accesskit-shared,IMPORTED_IMPLIB>,>>:$<TARGET_PROPERTY:accesskit-shared,IMPORTED_IMPLIB>>"
RENAME "libaccesskit.a"
DESTINATION "${ACCESSKIT_LIBRARIES_DIR}/shared"
)
endif()
36 changes: 0 additions & 36 deletions bindings/c/Makefile

This file was deleted.

Loading

0 comments on commit 3f556c9

Please sign in to comment.