Skip to content

Commit c7f445b

Browse files
authored
grpc-native: Add linux support (#466)
* grpc-native: Add linux support * grpc-native: Add support for linux Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Half working with konan (only kgrpc is not compiling) * grpc-native: Working protobuf konan compile * grpc-native: Little fix * grpc-native: Little fix * grpc-native: Not working state * grpc-native: Working linux arm64 and x64 * grpc-native: Address PR comments Signed-off-by: Johannes Zottele <[email protected]> --------- Signed-off-by: Johannes Zottele <[email protected]>
1 parent c5f0daa commit c7f445b

24 files changed

+500
-74
lines changed

cinterop-c/.bazelrc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
# we build the cc_static library bundled with all dependencies
21
build --experimental_cc_static_library --experimental_platform_in_output_dir
32

4-
build:release --compilation_mode=opt --strip=always
3+
build:release --compilation_mode=opt --strip=always
4+
5+
build:macos_arm64 --platforms=@build_bazel_apple_support//platforms:macos_arm64 --apple_platform_type=macos
6+
build:macos_x64 --platforms=@build_bazel_apple_support//platforms:macos_x86_64 --apple_platform_type=macos
7+
build:ios_arm64 --platforms=@build_bazel_apple_support//platforms:ios_arm64 --apple_platform_type=ios
8+
build:ios_simulator_arm64 --platforms=@build_bazel_apple_support//platforms:ios_sim_arm64 --apple_platform_type=ios
9+
build:ios_x64 --platforms=@build_bazel_apple_support//platforms:ios_x86_64 --apple_platform_type=ios
10+
11+
build:linux_arm64 --platforms=//platforms:linux_arm64
12+
build:linux_x64 --platforms=//platforms:linux_x86_64

cinterop-c/BUILD.bazel

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
22
load("@rules_cc//cc:defs.bzl", "cc_library")
33

4-
cc_library(
4+
cc_static_library(
55
name = "kgrpc",
6+
deps = [
7+
":kgrpc_lib",
8+
],
9+
)
10+
11+
cc_library(
12+
name = "kgrpc_lib",
613
srcs = ["src/kgrpc.cpp"],
714
hdrs = glob(["include/kgrpc.h"]),
8-
copts = ["-std=c++20"],
915
includes = ["include"],
1016
visibility = ["//visibility:public"],
1117
deps = [
@@ -24,7 +30,6 @@ cc_library(
2430
name = "protowire",
2531
srcs = ["src/protowire.cpp"],
2632
hdrs = glob(["include/protowire.h"]),
27-
copts = ["-std=c++20"],
2833
includes = ["include"],
2934
visibility = ["//visibility:public"],
3035
deps = [

cinterop-c/MODULE.bazel

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ module(
44
)
55

66
# rules_cc for cc_library support
7-
bazel_dep(
8-
name = "rules_cc",
9-
version = "0.1.1",
10-
)
7+
bazel_dep(name = "rules_cc", version = "0.2.0")
118

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

2623
## gRPC source dependency
27-
2824
bazel_dep(
2925
name = "grpc",
3026
version = GRPC_VERSION,
3127
repo_name = "com_github_grpc_grpc",
3228
)
29+
30+
# Linux toolchain setup (cross-compile)
31+
32+
register_toolchains(
33+
"//toolchain:toolchain_linux_x64_konan",
34+
"//toolchain:toolchain_linux_arm64_konan",
35+
)

cinterop-c/MODULE.bazel.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cinterop-c/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
This subdirectory contains the C sources required by native targets.
2+
It uses the Bazel build system and contains two libraries: protowire and kgrpc.
3+
4+
### Protowire
5+
6+
Is a thin layer over the CodedStream implementation of the C++ protobuf library.
7+
To build (e.g. ios_arm64) run
8+
```bash
9+
bazel build :protowire_fat --config=ios_arm64 --config=release
10+
```
11+
12+
### KgRPC
13+
14+
We are using the gRPC-core library, which already exports its API with a C ABI.
15+
Therefore, the KgRPC library is almost empty primarily used for convenient functions
16+
or API that is not exposed by the C API.
17+
18+
Because the gRPC takes a while to build when compiling for multiple targets, we store
19+
it as a prebuilt static (fat) in `prebuilt-deps/grpc_fat`.
20+
The binary can be updated by running
21+
```bash
22+
./gradlew :grpc:grpc-core:buildDependencyCLibGrpc_fat_iosArm64
23+
```
24+
25+
### Compiling for Linux
26+
27+
To produce K/N compatible static libraries, we use the Konan toolchain for compilation.
28+
The Bazel toolchain is specified in `toolchain/` and requires the user to specify the
29+
`KONAN_HOME` variable like
30+
```bash
31+
bazel build //:protowire --config=linux_arm64 --define=KONAN_HOME=$HOME/.konan/kotlin-native-prebuilt-macos-aarch64-2.2.10
32+
```

cinterop-c/build_target.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ trap 'echo "ERROR: Build failed at ${BASH_SOURCE}:${LINENO}" >&2' ERR
99
# Builds a static library for a specific platform (os/arch).
1010
#
1111
# Usage:
12-
# ./build_target.sh //path:libtarget out_dir <platform> <short> <os>
12+
# ./build_target.sh //path:libtarget dest konan_target konan_home
1313
# Example:
14-
# ./build_target.sh :protowire_static out @build_bazel_apple_support//platforms:ios_arm64 ios_arm64 ios
14+
# ./build_target.sh :protowire_static out/libprotowire_static.ios_arm64.a ios_arm64 \
15+
# $HOME/.konan/kotlin-native-prebuilt-macos-aarch64-2.2.10
1516
#
1617
# The example will produce ./out/libprotowire_static.ios_arm64.a
1718

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

2324
CONFIG=release
2425

25-
mkdir -p $(dirname "$DST")
26+
mkdir -p "$(dirname "$DST")"
2627

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

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

3436
cp -f "$out" "$DST"

cinterop-c/platforms/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
platform(
2+
name = "linux_x86_64",
3+
constraint_values = [
4+
"@platforms//os:linux",
5+
"@platforms//cpu:x86_64",
6+
],
7+
)
8+
9+
platform(
10+
name = "linux_arm64",
11+
constraint_values = [
12+
"@platforms//os:linux",
13+
"@platforms//cpu:aarch64",
14+
],
15+
)
52.7 MB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5eab3ed3bebb20944f053cacf34c755173832ab65e1c7f199a5d70fb3f1e7d55
50.4 MB
Binary file not shown.

0 commit comments

Comments
 (0)