Skip to content

Commit 28e70ec

Browse files
authored
Merge pull request #21 from Serial-IO/feat/ship-ffi-api-metadata
Generate and ship FFI metadata with ASTrein
2 parents 12e5240 + 3c1ece5 commit 28e70ec

5 files changed

Lines changed: 137 additions & 10 deletions

File tree

‎.github/workflows/build_binary.yml‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ jobs:
1515
build-binary:
1616
name: 'Build binary'
1717
runs-on: ubuntu-latest
18+
env:
19+
ASTREIN_VERSION: '1.1.0'
20+
ASTREIN_SHA256: 'd5964319ea5a3742e4d7223ee5963d6aa3b24c8f67ef75c61d591c635f413497'
1821
permissions:
1922
contents: write
2023

@@ -30,6 +33,19 @@ jobs:
3033
version: '16'
3134
platform: 'x64'
3235

36+
- name: 'Download ASTrein'
37+
run: |
38+
curl --fail --location \
39+
--output "${RUNNER_TEMP}/astrein-linux-x86_64.tar.gz" \
40+
"https://github.com/Katze719/ASTrein/releases/download/v${ASTREIN_VERSION}/astrein-linux-x86_64.tar.gz"
41+
echo "${ASTREIN_SHA256} ${RUNNER_TEMP}/astrein-linux-x86_64.tar.gz" \
42+
| sha256sum --check -
43+
mkdir -p "${RUNNER_TEMP}/astrein"
44+
tar -xzf "${RUNNER_TEMP}/astrein-linux-x86_64.tar.gz" \
45+
-C "${RUNNER_TEMP}/astrein" \
46+
--strip-components=1
47+
"${RUNNER_TEMP}/astrein/bin/astrein" --version
48+
3349
- name: 'Setup CMake'
3450
uses: jwlawson/actions-setup-cmake@v2
3551
with:
@@ -39,13 +55,16 @@ jobs:
3955
run: |
4056
cmake --preset linux-gcc-release \
4157
-DCMAKE_C_COMPILER=gcc-16 \
42-
-DCMAKE_CXX_COMPILER=g++-16
58+
-DCMAKE_CXX_COMPILER=g++-16 \
59+
-DCPP_BINDINGS_LINUX_ENABLE_FFI_JSON_EXPORT=ON \
60+
-DCPP_BINDINGS_LINUX_ASTREIN_EXECUTABLE="${RUNNER_TEMP}/astrein/bin/astrein"
4361
4462
- name: 'Build'
4563
run: |
46-
cmake --build --preset linux-gcc-release --target cpp_bindings_linux
64+
cmake --build --preset linux-gcc-release \
65+
--target cpp_bindings_linux cpp_bindings_linux_ffi_json
4766
48-
- name: 'Set PACKAGE_VERSION from env.bat'
67+
- name: 'Set PACKAGE_VERSION from env.sh'
4968
id: version
5069
run: |
5170
. build/env.sh
@@ -70,7 +89,9 @@ jobs:
7089
with:
7190
if-no-files-found: error
7291
name: libcpp_bindings_linux
73-
path: build/libcpp_bindings_linux.so
92+
path: |
93+
build/libcpp_bindings_linux.so
94+
build/cpp_bindings_linux.ffi.json
7495
7596
- name: 'Create GitHub Release'
7697
if: github.ref_type == 'tag' && steps.check-tag.outputs.IS_VALID_PACKAGE_VERSION == 'true'
@@ -79,7 +100,9 @@ jobs:
79100
name: 'v${{ steps.version.outputs.PACKAGE_VERSION }}'
80101
tag_name: ${{ github.ref_name }}
81102
generate_release_notes: true
82-
files: build/out/libcpp_bindings_windows.dll
103+
files: |
104+
build/libcpp_bindings_windows.so
105+
build/cpp_bindings_linux.ffi.json
83106
84107
outputs:
85108
package_version: ${{ steps.version.outputs.PACKAGE_VERSION }}

‎.github/workflows/publish_jsr.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
run: |
5151
mkdir -p ./jsr/bin
5252
cp ./artifacts/libcpp_bindings_linux.so ./jsr/bin/x86_64.so
53+
cp ./artifacts/cpp_bindings_linux.ffi.json ./jsr/bin/x86_64.ffi.json
5354
5455
./jsr/scripts/binary_to_json.sh \
5556
./artifacts/libcpp_bindings_linux.so \
@@ -71,4 +72,3 @@ jobs:
7172
working-directory: jsr
7273
run: |
7374
deno publish --allow-dirty --dry-run
74-

‎CMakeLists.txt‎

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,116 @@ set(CMAKE_CXX_EXTENSIONS OFF)
3434
set(CMAKE_CXX_MODULE_STD 26)
3535
set(CMAKE_CXX_MODULE_EXTENSIONS OFF)
3636

37+
option(
38+
CPP_BINDINGS_LINUX_ENABLE_FFI_JSON_EXPORT
39+
"Enable ASTrein JSON export for the cpp-core FFI headers"
40+
OFF
41+
)
42+
set(
43+
CPP_BINDINGS_LINUX_ASTREIN_EXECUTABLE
44+
""
45+
CACHE FILEPATH
46+
"Path to the ASTrein executable used for FFI JSON export"
47+
)
48+
set(
49+
CPP_BINDINGS_LINUX_FFI_JSON_OUTPUT
50+
"${CMAKE_BINARY_DIR}/cpp_bindings_linux.ffi.json"
51+
CACHE FILEPATH
52+
"Output path for the generated cpp-core FFI API metadata"
53+
)
54+
3755
CPMAddPackage(
3856
NAME cpp_core
3957
GITHUB_REPOSITORY Serial-IO/cpp-core
40-
GIT_TAG v2.0.0
58+
GIT_TAG v2.0.1
4159
OPTIONS
4260
"CMAKE_EXPORT_COMPILE_COMMANDS OFF"
43-
"CPP_CORE_ENABLE_AST_EXPORT ON"
4461
)
4562

63+
if(CPP_BINDINGS_LINUX_ENABLE_FFI_JSON_EXPORT)
64+
if(CPP_BINDINGS_LINUX_ASTREIN_EXECUTABLE)
65+
set(_cpp_bindings_linux_astrein "${CPP_BINDINGS_LINUX_ASTREIN_EXECUTABLE}")
66+
else()
67+
find_program(_cpp_bindings_linux_astrein NAMES astrein)
68+
endif()
69+
70+
if(NOT _cpp_bindings_linux_astrein)
71+
message(
72+
FATAL_ERROR
73+
"CPP_BINDINGS_LINUX_ENABLE_FFI_JSON_EXPORT=ON requires ASTrein. "
74+
"Install astrein or set CPP_BINDINGS_LINUX_ASTREIN_EXECUTABLE."
75+
)
76+
endif()
77+
78+
file(
79+
GLOB_RECURSE _cpp_bindings_linux_ffi_headers
80+
CONFIGURE_DEPENDS
81+
"${cpp_core_SOURCE_DIR}/include/*.h"
82+
"${cpp_core_SOURCE_DIR}/include/*.hpp"
83+
)
84+
set(
85+
_cpp_bindings_linux_ffi_wrapper
86+
"${CMAKE_BINARY_DIR}/ffi.cpp"
87+
)
88+
get_filename_component(
89+
_cpp_bindings_linux_ffi_output_dir
90+
"${CPP_BINDINGS_LINUX_FFI_JSON_OUTPUT}"
91+
DIRECTORY
92+
)
93+
94+
file(
95+
GENERATE
96+
OUTPUT "${_cpp_bindings_linux_ffi_wrapper}"
97+
CONTENT "#include <cpp_core/serial.h>\n"
98+
)
99+
100+
add_library(
101+
cpp_bindings_linux_ffi_ast_context
102+
OBJECT
103+
EXCLUDE_FROM_ALL
104+
"${_cpp_bindings_linux_ffi_wrapper}"
105+
)
106+
target_include_directories(
107+
cpp_bindings_linux_ffi_ast_context
108+
PRIVATE
109+
"${cpp_core_SOURCE_DIR}/include"
110+
)
111+
target_compile_features(
112+
cpp_bindings_linux_ffi_ast_context
113+
PRIVATE
114+
cxx_std_26
115+
)
116+
117+
add_custom_command(
118+
OUTPUT "${CPP_BINDINGS_LINUX_FFI_JSON_OUTPUT}"
119+
COMMAND
120+
${CMAKE_COMMAND} -E make_directory
121+
"${_cpp_bindings_linux_ffi_output_dir}"
122+
COMMAND
123+
"${_cpp_bindings_linux_astrein}"
124+
--ffi
125+
--compile-commands "${CMAKE_BINARY_DIR}/compile_commands.json"
126+
--require-c-linkage
127+
--require-default-visibility
128+
--public-header "cpp_core/serial.h"
129+
--api-root "${cpp_core_SOURCE_DIR}/include"
130+
--output "${CPP_BINDINGS_LINUX_FFI_JSON_OUTPUT}"
131+
"${_cpp_bindings_linux_ffi_wrapper}"
132+
DEPENDS
133+
"${_cpp_bindings_linux_astrein}"
134+
"${CMAKE_BINARY_DIR}/compile_commands.json"
135+
"${_cpp_bindings_linux_ffi_wrapper}"
136+
${_cpp_bindings_linux_ffi_headers}
137+
COMMENT "Exporting cpp-core FFI API metadata with ASTrein"
138+
VERBATIM
139+
)
140+
141+
add_custom_target(
142+
cpp_bindings_linux_ffi_json
143+
DEPENDS "${CPP_BINDINGS_LINUX_FFI_JSON_OUTPUT}"
144+
)
145+
endif()
146+
46147
CPMAddPackage(
47148
NAME GTest
48149
GITHUB_REPOSITORY google/googletest

‎jsr/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
Binaries are provided as a [package on JSR](https://jsr.io/@serial/cpp-bindings-linux). They are serialized as a base64 string inside the JSON file.
77

8+
The package also includes cpp-core FFI API metadata generated with
9+
[ASTrein](https://github.com/Katze719/ASTrein) at `bin/x86_64.ffi.json`.
10+
It describes the exported C symbols, parameter and return types, callbacks,
11+
default values, and API documentation used by downstream FFI adapter generators.
12+
813
This package is primarily intended as a dependency for [`@serial/serial`](https://jsr.io/@serial/serial). However, it can also be used independently.
914

1015

‎jsr/jsr.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@
1919
]
2020
}
2121
}
22-
23-

0 commit comments

Comments
 (0)