Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions cinterop-c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ It uses the Bazel build system and contains two libraries: protowire and kgrpc.

### Protowire

Is a thin layer over the CodedStream implementation of the C++ protobuf library.
To build (e.g. ios_arm64) run
Is a thin layer over the CodedStream implementation of the C++ protobuf library.
To build (e.g. ios_arm64) run

```bash
bazel build :protowire_fat --config=ios_arm64 --config=release
```
Expand All @@ -15,9 +16,10 @@ We are using the gRPC-core library, which already exports its API with a C ABI.
Therefore, the KgRPC library is almost empty primarily used for convenient functions
or API that is not exposed by the C API.

Because the gRPC takes a while to build when compiling for multiple targets, we store
Because the gRPC takes a while to build when compiling for multiple targets, we store
it as a prebuilt static (fat) in `prebuilt-deps/grpc_fat`.
The binary can be updated by running

```bash
./gradlew :grpc:grpc-core:buildDependencyCLibGrpc_fat_iosArm64
```
Expand All @@ -27,6 +29,32 @@ The binary can be updated by running
To produce K/N compatible static libraries, we use the Konan toolchain for compilation.
The Bazel toolchain is specified in `toolchain/` and requires the user to specify the
`KONAN_HOME` variable like

```bash
bazel build //:protowire --config=linux_arm64 --define=KONAN_HOME=$HOME/.konan/kotlin-native-prebuilt-macos-aarch64-2.2.10
```
```

#### Upgrading the Kotlin Compiler Version

When we upgrade the project's Kotlin compiler version, compilation for Linux will fail.
Bazel will throw an error like

```
[1,351 / 1,353] Compiling src/kgrpc.cpp; 1s darwin-sandbox
ERROR: /Users/jozott/development/jetbrains/kotlinx-rpc/cinterop-c/BUILD.bazel:11:11: Compiling src/kgrpc.cpp failed: absolute path inclusion(s) found in rule '//:kgrpc_lib':
the source file 'src/kgrpc.cpp' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain):
/Users/jozott/development/jetbrains/kotlinx-rpc/cinterop-c/BUILD.bazel:11:11: Compiling src/kgrpc.cpp failed: absolute path inclusion(s) found in rule '//:kgrpc_lib':

/Users/jozott/.konan/dependencies/llvm-19-aarch64-macos-essentials-79/bin/clang ... -c src/kgrpc.cpp -o bazel-out/linux_arm64-opt/bin/_objs/kgrpc_lib/kgrpc.o
'/Users/jozott/.konan/dependencies/llvm-19-aarch64-macos-essentials-79/lib/clang/19/include/stdbool.h'
'/Users/jozott/.konan/dependencies/llvm-19-aarch64-macos-essentials-79/lib/clang/19/include/stdint.h'
...
```

To fix this, we need to adjust the clang built-in include paths defined in the `toolchain/cc_toolchain_config.bzl` file.
In the case above, the path at `cxx_builtin_include_directories` must be replaced by
`deps + "llvm-19-aarch64-macos-essentials-79/lib/clang/19/include"`.
This must be done for aarch64 and x86_64 separately.
The current LLVM toolchain bundle paths can be found in
[kotlin/kotlin-native/gradle.properties](https://github.com/JetBrains/kotlin/blob/master/kotlin-native/gradle.properties)
of the Kotlin repository.
7 changes: 7 additions & 0 deletions cinterop-c/toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ def _impl(ctx):
tool_paths = tool_paths,
features = features,
cxx_builtin_include_directories = [
# Built-in include directories from the toolchain.
# Only one will be available on the host (x64 or arm64).
# These paths must be updated after each upgrade of the project's Kotlin compiler version.
# After upgrading the Kotlin compiler, the C compiler will throw an error that certain includes are not
# are not part of the built-in include paths. In this case, replace the below path with the printed one.
# See the cinterop-c/README.md for more details.
deps + "/llvm-19-aarch64-macos-essentials-75/lib/clang/19/include",
deps + "/llvm-19-x86_64-macos-essentials-103/lib/clang/19/include",
] + includes,
)

Expand Down
Loading