Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
12 changes: 10 additions & 2 deletions cinterop-c/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# we build the cc_static library bundled with all dependencies
build --experimental_cc_static_library --experimental_platform_in_output_dir

build:release --compilation_mode=opt --strip=always
build:release --compilation_mode=opt --strip=always

build:macos_arm64 --platforms=@build_bazel_apple_support//platforms:macos_arm64 --apple_platform_type=macos
build:macos_x64 --platforms=@build_bazel_apple_support//platforms:macos_x86_64 --apple_platform_type=macos
build:ios_arm64 --platforms=@build_bazel_apple_support//platforms:ios_arm64 --apple_platform_type=ios
build:ios_simulator_arm64 --platforms=@build_bazel_apple_support//platforms:ios_sim_arm64 --apple_platform_type=ios
build:ios_x64 --platforms=@build_bazel_apple_support//platforms:ios_x86_64 --apple_platform_type=ios

build:linux_arm64 --platforms=//platforms:linux_arm64
build:linux_x64 --platforms=//platforms:linux_x86_64
11 changes: 8 additions & 3 deletions cinterop-c/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
cc_static_library(
name = "kgrpc",
deps = [
":kgrpc_lib",
],
)

cc_library(
name = "kgrpc_lib",
srcs = ["src/kgrpc.cpp"],
hdrs = glob(["include/kgrpc.h"]),
copts = ["-std=c++20"],
includes = ["include"],
visibility = ["//visibility:public"],
deps = [
Expand All @@ -24,7 +30,6 @@ cc_library(
name = "protowire",
srcs = ["src/protowire.cpp"],
hdrs = glob(["include/protowire.h"]),
copts = ["-std=c++20"],
includes = ["include"],
visibility = ["//visibility:public"],
deps = [
Expand Down
13 changes: 8 additions & 5 deletions cinterop-c/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ module(
)

# rules_cc for cc_library support
bazel_dep(
name = "rules_cc",
version = "0.1.1",
)
bazel_dep(name = "rules_cc", version = "0.2.0")

# required to build for apple targets (like iOS)
bazel_dep(name = "apple_support", version = "1.22.1", repo_name = "build_bazel_apple_support")
Expand All @@ -24,9 +21,15 @@ bazel_dep(
GRPC_VERSION = "1.74.1"

## gRPC source dependency

bazel_dep(
name = "grpc",
version = GRPC_VERSION,
repo_name = "com_github_grpc_grpc",
)

# Linux toolchain setup (cross-compile)

register_toolchains(
"//toolchain:toolchain_linux_x64_konan",
"//toolchain:toolchain_linux_arm64_konan",
)
4 changes: 3 additions & 1 deletion cinterop-c/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions cinterop-c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
This subdirectory contains the C sources required by native targets.
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
```bash
bazel build :protowire_fat --config=ios_arm64 --config=release
```

### KgRPC

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
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
```

### Compiling for Linux

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
```
16 changes: 9 additions & 7 deletions cinterop-c/build_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@ trap 'echo "ERROR: Build failed at ${BASH_SOURCE}:${LINENO}" >&2' ERR
# Builds a static library for a specific platform (os/arch).
#
# Usage:
# ./build_target.sh //path:libtarget out_dir <platform> <short> <os>
# ./build_target.sh //path:libtarget dest konan_target konan_home
# Example:
# ./build_target.sh :protowire_static out @build_bazel_apple_support//platforms:ios_arm64 ios_arm64 ios
# ./build_target.sh :protowire_static out/libprotowire_static.ios_arm64.a ios_arm64 \
# $HOME/.konan/kotlin-native-prebuilt-macos-aarch64-2.2.10
#
# The example will produce ./out/libprotowire_static.ios_arm64.a

LABEL="${1:?need bazel target label}"
DST="${2:?need output destination}"
PLATFORM="${3:?need a platform for bazel build command}"
OS="${4:?need the operating system of the target platform}"
KONAN_TARGET="${3:?need the konan_target name}"
KONAN_HOME="${4:?need the konan_home path}"

CONFIG=release

mkdir -p $(dirname "$DST")
mkdir -p "$(dirname "$DST")"

echo "==> Building $LABEL to $DST" >&2
bazel build "$LABEL" --platforms="$PLATFORM" --apple_platform_type="$OS" --config="$CONFIG" --announce_rc >/dev/null
KONAN_DEP="--define=KONAN_DEPS=/Users/johannes.zottele/.konan//dependencies"
bazel build "$LABEL" --config="$KONAN_TARGET" --config="$CONFIG" $KONAN_DEP "--define=KONAN_HOME=$KONAN_HOME" >/dev/null

# Ask Bazel what file(s) this target produced under this platform
out="$(bazel cquery "$LABEL" --platforms="$PLATFORM" --apple_platform_type="$OS" --config="$CONFIG" --output=files | head -n1)"
out="$(bazel cquery "$LABEL" --config="$KONAN_TARGET" --config="$CONFIG" $KONAN_DEP "--define=KONAN_HOME=$KONAN_HOME" --output=files | head -n1)"
[[ -n "$out" ]] || { echo "No output for $LABEL ($SHORT)"; exit 1; }

cp -f "$out" "$DST"
Expand Down
15 changes: 15 additions & 0 deletions cinterop-c/platforms/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
platform(
name = "linux_x86_64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)

platform(
name = "linux_arm64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5eab3ed3bebb20944f053cacf34c755173832ab65e1c7f199a5d70fb3f1e7d55
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
511c10750585f1a7c7cd8801acaab10cf21cd8e098c381a07102496e3569b085
67 changes: 67 additions & 0 deletions cinterop-c/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
load(":cc_toolchain_config.bzl", "cc_toolchain_config")

cc_toolchain_config(
name = "cfg_linux_x64_konan",
target = "linux_x64",
)

cc_toolchain_config(
name = "cfg_linux_arm64_konan",
target = "linux_arm64",
)

filegroup(
name = "wrappers",
srcs = [
"run_konan",
"run_konan_ar.sh",
"run_konan_clang.sh",
"run_konan_clangxx.sh",
],
)

cc_toolchain(
name = "tc_linux_x64_konan",
all_files = ":wrappers",
ar_files = ":wrappers",
compiler_files = ":wrappers",
dwp_files = ":wrappers",
linker_files = ":wrappers",
objcopy_files = ":wrappers",
strip_files = ":wrappers",
toolchain_config = ":cfg_linux_x64_konan",
toolchain_identifier = "konan_linux_x64",
)

cc_toolchain(
name = "tc_linux_arm64_konan",
all_files = ":wrappers",
ar_files = ":wrappers",
compiler_files = ":wrappers",
dwp_files = ":wrappers",
linker_files = ":wrappers",
objcopy_files = ":wrappers",
strip_files = ":wrappers",
toolchain_config = ":cfg_linux_arm64_konan",
toolchain_identifier = "konan_linux_arm64",
)

toolchain(
name = "toolchain_linux_x64_konan",
target_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
toolchain = ":tc_linux_x64_konan",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

toolchain(
name = "toolchain_linux_arm64_konan",
target_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:arm64",
],
toolchain = ":tc_linux_arm64_konan",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
Loading
Loading