Skip to content

Commit 3f556c9

Browse files
feat: Add CMake support to C bindings (#247)
Co-authored-by: Matt Campbell <mattcampbell@pobox.com>
1 parent 022ef04 commit 3f556c9

File tree

16 files changed

+287
-129
lines changed

16 files changed

+287
-129
lines changed

.clang-format

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Language: Cpp
33
# BasedOnStyle: Google
44
AccessModifierOffset: -1
55
AlignAfterOpenBracket: Align
6-
AlignArrayOfStructures: None
76
AlignConsecutiveMacros: None
87
AlignConsecutiveAssignments: None
98
AlignConsecutiveBitFields: None
@@ -12,6 +11,7 @@ AlignEscapedNewlines: Left
1211
AlignOperands: Align
1312
AlignTrailingComments: true
1413
AllowAllArgumentsOnNextLine: true
14+
AllowAllConstructorInitializersOnNextLine: true
1515
AllowAllParametersOfDeclarationOnNextLine: true
1616
AllowShortEnumsOnASingleLine: true
1717
AllowShortBlocksOnASingleLine: Never
@@ -59,28 +59,23 @@ BreakAfterJavaFieldAnnotations: false
5959
BreakStringLiterals: true
6060
ColumnLimit: 80
6161
CommentPragmas: '^ IWYU pragma:'
62-
QualifierAlignment: Leave
6362
CompactNamespaces: false
63+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
6464
ConstructorInitializerIndentWidth: 4
6565
ContinuationIndentWidth: 4
6666
Cpp11BracedListStyle: true
6767
DeriveLineEnding: true
6868
DerivePointerAlignment: true
6969
DisableFormat: false
70-
EmptyLineAfterAccessModifier: Never
7170
EmptyLineBeforeAccessModifier: LogicalBlock
7271
ExperimentalAutoDetectBinPacking: false
73-
PackConstructorInitializers: NextLine
74-
BasedOnStyle: ''
75-
ConstructorInitializerAllOnOneLineOrOnePerLine: false
76-
AllowAllConstructorInitializersOnNextLine: true
7772
FixNamespaceComments: true
7873
ForEachMacros:
7974
- foreach
8075
- Q_FOREACH
8176
- BOOST_FOREACH
82-
IfMacros:
83-
- KJ_IF_MAYBE
77+
StatementAttributeLikeMacros:
78+
- Q_EMIT
8479
IncludeBlocks: Regroup
8580
IncludeCategories:
8681
- Regex: '^<ext/.*\.h>'
@@ -101,7 +96,6 @@ IncludeCategories:
10196
CaseSensitive: false
10297
IncludeIsMainRegex: '([-_](test|unittest))?$'
10398
IncludeIsMainSourceRegex: ''
104-
IndentAccessModifiers: false
10599
IndentCaseLabels: true
106100
IndentCaseBlocks: false
107101
IndentGotoLabels: true
@@ -114,7 +108,6 @@ InsertTrailingCommas: None
114108
JavaScriptQuotes: Leave
115109
JavaScriptWrapImports: true
116110
KeepEmptyLinesAtTheStartOfBlocks: false
117-
LambdaBodyIndentation: Signature
118111
MacroBlockBegin: ''
119112
MacroBlockEnd: ''
120113
MaxEmptyLinesToKeep: 1
@@ -128,14 +121,12 @@ PenaltyBreakAssignment: 2
128121
PenaltyBreakBeforeFirstCallParameter: 1
129122
PenaltyBreakComment: 300
130123
PenaltyBreakFirstLessLess: 120
131-
PenaltyBreakOpenParenthesis: 0
132124
PenaltyBreakString: 1000
133125
PenaltyBreakTemplateDeclaration: 10
134126
PenaltyExcessCharacter: 1000000
135127
PenaltyReturnTypeOnItsOwnLine: 200
136128
PenaltyIndentedWhitespace: 0
137129
PointerAlignment: Left
138-
PPIndentWidth: -1
139130
RawStringFormats:
140131
- Language: Cpp
141132
Delimiters:
@@ -164,14 +155,10 @@ RawStringFormats:
164155
- ParseTextProtoOrDie
165156
- ParseTestProto
166157
- ParsePartialTestProto
167-
CanonicalDelimiter: pb
158+
CanonicalDelimiter: ''
168159
BasedOnStyle: google
169-
ReferenceAlignment: Pointer
170160
ReflowComments: true
171-
RemoveBracesLLVM: false
172-
SeparateDefinitionBlocks: Leave
173-
ShortNamespaceLines: 1
174-
SortIncludes: CaseSensitive
161+
SortIncludes: true
175162
SortJavaStaticImport: Before
176163
SortUsingDeclarations: true
177164
SpaceAfterCStyleCast: false
@@ -183,33 +170,20 @@ SpaceBeforeCpp11BracedList: false
183170
SpaceBeforeCtorInitializerColon: true
184171
SpaceBeforeInheritanceColon: true
185172
SpaceBeforeParens: ControlStatements
186-
SpaceBeforeParensOptions:
187-
AfterControlStatements: true
188-
AfterForeachMacros: true
189-
AfterFunctionDefinitionName: false
190-
AfterFunctionDeclarationName: false
191-
AfterIfMacros: true
192-
AfterOverloadedOperator: false
193-
BeforeNonEmptyParentheses: false
194173
SpaceAroundPointerQualifiers: Default
195174
SpaceBeforeRangeBasedForLoopColon: true
196175
SpaceInEmptyBlock: false
197176
SpaceInEmptyParentheses: false
198177
SpacesBeforeTrailingComments: 2
199-
SpacesInAngles: Never
178+
SpacesInAngles: false
200179
SpacesInConditionalStatement: false
201180
SpacesInContainerLiterals: true
202181
SpacesInCStyleCastParentheses: false
203-
SpacesInLineCommentPrefix:
204-
Minimum: 1
205-
Maximum: -1
206182
SpacesInParentheses: false
207183
SpacesInSquareBrackets: false
208184
SpaceBeforeSquareBrackets: false
209185
BitFieldColonSpacing: Both
210186
Standard: Auto
211-
StatementAttributeLikeMacros:
212-
- Q_EMIT
213187
StatementMacros:
214188
- Q_UNUSED
215189
- QT_REQUIRE_VERSION
@@ -223,3 +197,4 @@ WhitespaceSensitiveMacros:
223197
- NS_SWIFT_NAME
224198
- CF_SWIFT_NAME
225199
...
200+

.github/workflows/bindings.yml

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,44 @@ jobs:
1212
include:
1313
- os: macOS-latest
1414
target: aarch64-apple-darwin
15+
cmake-options: -DCMAKE_OSX_ARCHITECTURES=arm64
1516
path: macos/arm64
16-
shared-ext: dylib
17-
static-ext: a
1817
- os: macOS-latest
1918
target: x86_64-apple-darwin
2019
path: macos/x86_64
21-
shared-ext: dylib
22-
static-ext: a
2320
- os: windows-2019
2421
target: aarch64-pc-windows-msvc
22+
setup-step: 'cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsamd64_x86.bat" `& powershell'
23+
cmake-options: -A ARM64
2524
path: windows/arm64/msvc
26-
shared-ext: dll
27-
static-ext: lib
2825
- os: windows-2019
2926
target: i686-pc-windows-msvc
27+
setup-step: 'cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars32.bat" `& powershell'
28+
cmake-options: -A Win32
3029
path: windows/x86/msvc
31-
shared-ext: dll
32-
static-ext: lib
3330
- os: windows-2019
3431
target: x86_64-pc-windows-msvc
32+
setup-step: 'cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" `& powershell'
3533
path: windows/x86_64/msvc
36-
shared-ext: dll
37-
static-ext: lib
3834
- os: ubuntu-latest
3935
target: i686-pc-windows-gnu
40-
use-cross: true
36+
setup-step: sudo apt update && sudo apt install -y mingw-w64
37+
cmake-options: -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=x86 -DCMAKE_C_COMPILER=i686-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=i686-w64-mingw32-g++
4138
path: windows/x86/mingw
42-
shared-ext: dll
43-
static-ext: a
4439
- os: ubuntu-latest
4540
target: x86_64-pc-windows-gnu
46-
use-cross: true
41+
setup-step: sudo apt update && sudo apt install -y mingw-w64
42+
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++
4743
path: windows/x86_64/mingw
48-
shared-ext: dll
49-
static-ext: a
5044
- os: ubuntu-latest
5145
target: i686-unknown-linux-gnu
52-
use-cross: true
46+
setup-step: sudo apt update && sudo apt install -y gcc-multilib g++-multilib
47+
cmake-options: -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86
5348
path: linux/x86
54-
shared-ext: so
55-
static-ext: a
5649
- os: ubuntu-latest
5750
target: x86_64-unknown-linux-gnu
5851
path: linux/x86_64
59-
shared-ext: so
60-
static-ext: a
61-
name: cargo build
52+
name: Build
6253
steps:
6354
- uses: actions/checkout@v3
6455

@@ -70,34 +61,18 @@ jobs:
7061
profile: minimal
7162
override: true
7263

73-
- name: cargo build
74-
uses: actions-rs/cargo@v1
75-
with:
76-
command: build
77-
use-cross: ${{ matrix.use-cross || false }}
78-
args: --package accesskit_c --release --target ${{ matrix.target }}
79-
80-
- shell: bash
81-
run: |
82-
mkdir -p artifacts/${{ matrix.path }}/shared
83-
mkdir -p artifacts/${{ matrix.path }}/static
84-
85-
- if: startsWith(matrix.os, 'windows')
64+
- name: build libraries
8665
run: |
87-
mv target/${{ matrix.target }}/release/accesskit.dll.lib artifacts/${{ matrix.path }}/shared/accesskit.lib
88-
mv target/${{ matrix.target }}/release/*.pdb artifacts/${{ matrix.path }}/shared
89-
- if: contains(matrix.path, 'mingw')
90-
run: mv target/${{ matrix.target }}/release/libaccesskit.dll.a artifacts/${{ matrix.path }}/shared/libaccesskit.a
91-
92-
- run: |
93-
mv target/${{ matrix.target }}/release/*.${{ matrix.shared-ext }} artifacts/${{ matrix.path }}/shared
94-
mv target/${{ matrix.target }}/release/*.${{ matrix.static-ext }} artifacts/${{ matrix.path }}/static
66+
${{ matrix.setup-step || '' }}
67+
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' || '' }}
68+
cmake --build build ${{ contains(matrix.target, 'msvc') && '--config Release' || '' }}
69+
cmake --install build ${{ contains(matrix.target, 'msvc') && '--config Release' || '' }}
9570
9671
- name: Upload binaries
9772
uses: actions/upload-artifact@v3
9873
with:
9974
name: ${{ matrix.target }}
100-
path: artifacts
75+
path: bindings/c/lib
10176

10277
generate-headers:
10378
if: startsWith(github.ref_name, 'accesskit_c-v')
@@ -119,6 +94,7 @@ jobs:
11994
mv artifacts/headers accesskit_c/include
12095
cp -r artifacts/*/* accesskit_c/lib
12196
cp -r bindings/c/examples accesskit_c/
97+
cp bindings/c/accesskit*.cmake accesskit_c/
12298
cp bindings/c/*.md accesskit_c/
12399
cp LICENSE* accesskit_c/
124100
mv accesskit_c ${{ github.ref_name }}

.github/workflows/generate-headers.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ jobs:
1818
- uses: dtolnay/install@master
1919
with:
2020
crate: cbindgen
21-
- run: make -C bindings/c include/accesskit.h
21+
- run: cmake -S bindings/c -B build -DACCESSKIT_BUILD_LIBRARIES=OFF
22+
- run: cmake --build build
23+
- run: cmake --install build
2224

2325
- name: Upload header file
2426
uses: actions/upload-artifact@v3

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ default-members = [
1212
"common",
1313
"consumer",
1414
"platforms/winit",
15+
"bindings/c",
1516
]
1617

1718
[profile.release]

bindings/c/.cargo/config.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[target.aarch64-apple-darwin]
2+
rustflags = [
3+
"-C", "link-arg=-Wl,-install_name,@rpath/libaccesskit.dylib",
4+
]
5+
6+
[target.x86_64-apple-darwin]
7+
rustflags = [
8+
"-C", "link-arg=-Wl,-install_name,@rpath/libaccesskit.dylib",
9+
]

bindings/c/.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
2-
include
1+
build
2+
include
3+
lib

bindings/c/CMakeLists.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(accesskit_c)
4+
5+
option(ACCESSKIT_BUILD_HEADERS "Whether to build header files" ON)
6+
option(ACCESSKIT_BUILD_LIBRARIES "Whether to build libraries" ON)
7+
8+
if (ACCESSKIT_BUILD_LIBRARIES)
9+
include(FetchContent)
10+
11+
FetchContent_Declare(
12+
Corrosion
13+
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
14+
GIT_TAG v0.3.5
15+
)
16+
FetchContent_MakeAvailable(Corrosion)
17+
18+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
19+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
20+
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
21+
corrosion_import_crate(MANIFEST_PATH Cargo.toml)
22+
endif()
23+
24+
if (ACCESSKIT_BUILD_HEADERS)
25+
find_program(RUSTUP rustup)
26+
find_program(CBINDGEN cbindgen)
27+
find_program(CLANG_FORMAT clang-format)
28+
29+
add_custom_target(headers ALL
30+
COMMAND ${RUSTUP} run nightly ${CBINDGEN} --crate accesskit_c --output accesskit.hpp "${CMAKE_SOURCE_DIR}"
31+
COMMAND ${CLANG_FORMAT} -i accesskit.hpp
32+
COMMAND ${CMAKE_COMMAND} -E rename accesskit.hpp accesskit.h
33+
BYPRODUCTS accesskit.h
34+
)
35+
36+
if (ACCESSKIT_BUILD_LIBRARIES)
37+
add_dependencies(cargo-prebuild_accesskit headers)
38+
endif()
39+
endif()
40+
41+
include("accesskit.cmake")
42+
43+
if (ACCESSKIT_BUILD_HEADERS)
44+
install(FILES
45+
"${CMAKE_CURRENT_BINARY_DIR}/accesskit.h"
46+
DESTINATION "${ACCESSKIT_INCLUDE_DIR}"
47+
)
48+
endif()
49+
50+
if (ACCESSKIT_BUILD_LIBRARIES)
51+
install(FILES
52+
"$<TARGET_PROPERTY:accesskit-static,IMPORTED_LOCATION>"
53+
DESTINATION "${ACCESSKIT_LIBRARIES_DIR}/static"
54+
)
55+
56+
install(FILES
57+
"$<TARGET_PROPERTY:accesskit-shared,IMPORTED_LOCATION>"
58+
"$<$<STREQUAL:${_accesskit_toolchain},msvc>:${CMAKE_CURRENT_BINARY_DIR}/accesskit.pdb>"
59+
DESTINATION "${ACCESSKIT_LIBRARIES_DIR}/shared"
60+
)
61+
install(FILES
62+
"$<$<NOT:$<STREQUAL:$<TARGET_PROPERTY:accesskit-shared,IMPORTED_IMPLIB>,>>:$<TARGET_PROPERTY:accesskit-shared,IMPORTED_IMPLIB>>"
63+
RENAME "libaccesskit.a"
64+
DESTINATION "${ACCESSKIT_LIBRARIES_DIR}/shared"
65+
)
66+
endif()

bindings/c/Makefile

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)