Skip to content

Commit 1519749

Browse files
C library: add script to download header files and update binding #12
1 parent 96e984c commit 1519749

File tree

3 files changed

+79
-14
lines changed

3 files changed

+79
-14
lines changed

dev-doc/updating-c-library.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ Dart won't error on C function signature mismatch, leading to obscure memory bug
55

66
## C libraries
77

8-
For Dart Native and unit tests and for Flutter (`flutter_libs` and `sync_flutter_libs` plugins)
9-
on Linux and Windows:
8+
For Dart Native and unit tests ([install.sh](../install.sh)),
9+
for the binding update script (see below) and
10+
for Flutter (`flutter_libs` and `sync_flutter_libs` plugins) on Linux and Windows:
1011
```
1112
./tool/set-c-version.sh 0.20.0
1213
```
@@ -24,18 +25,18 @@ For the Flutter plugins on iOS/macOS ([view releases](https://github.com/objectb
2425
For each, add an entry (see previous releases) to the [CHANGELOG](../objectbox/CHANGELOG.md).
2526

2627
## Dart C API bindings
27-
Download source code of an [objectbox-c release version](https://github.com/objectbox/objectbox-c/releases).
28-
- Update [objectbox.h](../objectbox/lib/src/native/bindings/objectbox.h)
29-
- Update [objectbox-dart.h](../objectbox/lib/src/native/bindings/objectbox-dart.h)
30-
- Update [objectbox-sync.h](../objectbox/lib/src/native/bindings/objectbox-sync.h)
31-
- Replace `const void*` by `const uint8_t*` in all objectbox*.h files
32-
(see ffigen note in [pubspec.yaml](../objectbox/pubspec.yaml)).
33-
- Execute `dart run ffigen` in the `objectbox` directory. This requires LLVM libraries
34-
(see [ffigen docs](https://pub.dev/packages/ffigen#installing-llvm)
35-
and the ffigen section in [pubspec.yaml](../objectbox/pubspec.yaml)).
36-
- Copy/update enums that need to be exposed to users from [objectbox_c.dart](../objectbox/lib/src/native/bindings/objectbox_c.dart)
28+
To download the C library header files and generate bindings with ffigen (requires LLVM libraries,
29+
see [ffigen docs](https://pub.dev/packages/ffigen#installing-llvm)
30+
and the ffigen section in [pubspec.yaml](../objectbox/pubspec.yaml)):
31+
```
32+
./tool/update-c-binding.sh
33+
```
34+
35+
Then manually:
36+
- Copy/update enums that need to be exposed to users
37+
from [objectbox_c.dart](../objectbox/lib/src/native/bindings/objectbox_c.dart)
3738
to [enums.dart](../objectbox/lib/src/modelinfo/enums.dart).
38-
- Have a look at the changed files to see if some call sites need to be updated.
39+
- Check the changed files, make any required changes in the Dart library (like method signature changes).
3940
- ⚠️ Update minimum C library and core version and notes as needed in [bindings.dart](../objectbox/lib/src/native/bindings/bindings.dart).
4041

4142
Note: the embedded C library and core version can be looked up

tool/set-c-version.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
. "$(dirname "$0")"/common.sh
33

44
# Changes the C library version used in
5-
# - the install script (for Dart Native and unit tests) and
5+
# - the install script (for Dart Native and unit tests),
6+
# - the binding update script and
67
# - both Flutter plugins (for Flutter on Linux, Windows).
78

89
if [[ "$#" -ne "1" ]]; then
@@ -17,6 +18,7 @@ echo "Setting cLibVersion and OBJECTBOX_VERSION version: $version"
1718

1819
versionExpr="s/cLibVersion=[0-9]\+\.[0-9]\+\.[0-9]\+/cLibVersion=${version}/g"
1920
update install.sh "${versionExpr}"
21+
update tool/update-c-binding.sh "${versionExpr}"
2022

2123
versionExpr="s/OBJECTBOX_VERSION [0-9]\+\.[0-9]\+\.[0-9]\+/OBJECTBOX_VERSION ${version}/g"
2224
update flutter_libs/linux/CMakeLists.txt "${versionExpr}"

tool/update-c-binding.sh

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
. "$(dirname "$0")"/common.sh
3+
4+
# Downloads the C library source files of a specific release from GitHub,
5+
# copies the header files, makes some required modifications
6+
# and runs the ffigen binding generator on them.
7+
8+
cLibVersion=0.20.0
9+
echo "Downloading C library source files from GitHub..."
10+
11+
# Note: the release archives do not contain objectbox-dart.h, so get the full sources.
12+
archiveExt="zip"
13+
downloadUrl="https://github.com/objectbox/objectbox-c/archive/refs/tags/v${cLibVersion}.${archiveExt}"
14+
echo "Download URL: ${downloadUrl}"
15+
16+
targetDir="objectbox/download"
17+
archiveFile="${targetDir}/objectbox-c-${cLibVersion}.${archiveExt}"
18+
mkdir -p "$(dirname "${archiveFile}")"
19+
20+
# Support both curl and wget because their availability is platform dependent
21+
if [ -x "$(command -v curl)" ]; then
22+
curl --location --fail --output "${archiveFile}" "${downloadUrl}"
23+
else
24+
wget --no-verbose --output-document="${archiveFile}" "${downloadUrl}"
25+
fi
26+
27+
if [[ ! -s ${archiveFile} ]]; then
28+
echo "Error: download failed (file ${archiveFile} does not exist or is empty)"
29+
exit 1
30+
fi
31+
32+
echo
33+
echo "Downloaded:"
34+
du -h "${archiveFile}"
35+
36+
echo
37+
echo "Extracting into ${targetDir}..."
38+
unzip "${archiveFile}" -d "${targetDir}"
39+
40+
headerBuildDir="objectbox/lib/src/native/bindings"
41+
echo
42+
echo "Copying to ${headerBuildDir}..."
43+
mkdir -p "${headerBuildDir}"
44+
cp "${targetDir}/objectbox-c-${cLibVersion}"/include/*.h "${headerBuildDir}"
45+
ls -l "${headerBuildDir}"
46+
47+
# Replace `const void*` by `const uint8_t*` in all objectbox*.h files
48+
# (see ffigen note in ../objectbox/pubspec.yaml).
49+
echo
50+
echo "Replacing 'const void*' by 'const uint8_t*'..."
51+
replaceVoidExpr="s/const void\*/const uint8_t*/g"
52+
update objectbox/lib/src/native/bindings/objectbox.h "${replaceVoidExpr}"
53+
update objectbox/lib/src/native/bindings/objectbox-dart.h "${replaceVoidExpr}"
54+
update objectbox/lib/src/native/bindings/objectbox-sync.h "${replaceVoidExpr}"
55+
56+
# This requires LLVM libraries
57+
# (see ffigen docs https://pub.dev/packages/ffigen#installing-llvm
58+
# and the ffigen section in ../objectbox/pubspec.yaml).
59+
echo
60+
echo "Generating bindings with ffigen (requires LLVM libraries)..."
61+
cd objectbox
62+
dart run ffigen

0 commit comments

Comments
 (0)