Skip to content

Commit c5f0daa

Browse files
authored
grpc-native: Pre-Built gRPC library to avoid long compilation times (#461)
* grpc-native: Build cache, not working Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Build cache within repository Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Add gRPC includes Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Cleanup Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Small fixes Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Use header sources from dependency instead of copy (bazel) Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Extract headers on demand Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Remove copy of include directory Signed-off-by: Johannes Zottele <[email protected]> * grpc-native: Remove unnecessary source dir * grpc-native: Address PR comments --------- Signed-off-by: Johannes Zottele <[email protected]>
1 parent ce62f16 commit c5f0daa

32 files changed

+557
-98
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Verify gRPC CLib Checksum is Up-to-Date
2+
3+
on:
4+
pull_request:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
verify-platforms-table:
11+
name: Run Verification
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Sources
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
- name: Setup Gradle
19+
uses: gradle/actions/setup-gradle@v4
20+
- name: Run gRPC Checksum Generation
21+
run: ./gradlew grpc:grpc-core:computeSha256AllTargetsForCLibGrpc_fat --info --stacktrace
22+
- name: Check if prebuilt gRPC library checksum is up-to-date
23+
run: |
24+
if [[ -n "$(git status --porcelain | grep cinterop-c/prebuilt-deps/grpc_fat/)" ]]; then
25+
echo "Prebuilt gRPC library checksum is not up to date. Please run './gradlew grpc:grpc-core:computeSha256AllTargetsForCLibGrpc_fat' and commit changes"
26+
exit 1
27+
fi

cinterop-c/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@
1212
/.aswb/
1313
/.clwb/
1414
.idea/
15-
16-
out/

cinterop-c/BUILD.bazel

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
12
load("@rules_cc//cc:defs.bzl", "cc_library")
23

3-
cc_static_library(
4-
name = "kgrpc_static",
5-
deps = [
6-
":kgrpc",
7-
],
8-
)
9-
104
cc_library(
115
name = "kgrpc",
126
srcs = ["src/kgrpc.cpp"],
@@ -15,12 +9,12 @@ cc_library(
159
includes = ["include"],
1610
visibility = ["//visibility:public"],
1711
deps = [
18-
"@com_github_grpc_grpc//:grpc",
12+
"//prebuilt-deps/grpc_fat:grpc_core_prebuilt",
1913
],
2014
)
2115

2216
cc_static_library(
23-
name = "protowire_static",
17+
name = "protowire_fat",
2418
deps = [
2519
":protowire",
2620
],
@@ -34,6 +28,6 @@ cc_library(
3428
includes = ["include"],
3529
visibility = ["//visibility:public"],
3630
deps = [
37-
"@com_google_protobuf//:protobuf_lite",
31+
"@com_github_protobuffers_protobuf//:protobuf_lite",
3832
],
3933
)

cinterop-c/MODULE.bazel

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ bazel_dep(
1111

1212
# required to build for apple targets (like iOS)
1313
bazel_dep(name = "apple_support", version = "1.22.1", repo_name = "build_bazel_apple_support")
14+
bazel_dep(name = "platforms", version = "1.0.0")
1415

1516
# Protobuf
1617
bazel_dep(
1718
name = "protobuf",
1819
version = "31.1",
19-
repo_name = "com_google_protobuf",
20+
repo_name = "com_github_protobuffers_protobuf",
2021
)
2122

2223
# gRPC library
24+
GRPC_VERSION = "1.74.1"
25+
26+
## gRPC source dependency
27+
2328
bazel_dep(
2429
name = "grpc",
25-
version = "1.74.1",
30+
version = GRPC_VERSION,
2631
repo_name = "com_github_grpc_grpc",
2732
)

cinterop-c/MODULE.bazel.lock

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

cinterop-c/build_target.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ out="$(bazel cquery "$LABEL" --platforms="$PLATFORM" --apple_platform_type="$OS"
3333

3434
cp -f "$out" "$DST"
3535

36-
echo "Done. Binary written to: $DST"
36+
echo "Done. Binary written to: $DST" >&2

cinterop-c/extract_include_dir.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
4+
#
5+
6+
set -Eeuo pipefail
7+
trap 'echo "ERROR: Build failed at ${BASH_SOURCE}:${LINENO}" >&2' ERR
8+
9+
# Extract all headers in the /include directory of the given target.
10+
#
11+
# Usage:
12+
# ./extract_include_dir.sh <target> <output-directory>
13+
# Example:
14+
# ./extract_include_dir.sh //prebuilt-deps/grpc_fat:grpc_fat prebuilt-deps/grpc_fat
15+
#
16+
# The example will produce the prebuilt-deps/grpc_fat/include directory with all headers.
17+
18+
LABEL="${1:?need bazel target label}"
19+
DST="${2:?need output destination}"
20+
21+
CONFIG=release
22+
23+
mkdir -p $(dirname "$DST")
24+
25+
bazel build "$LABEL" >/dev/null
26+
27+
# Ask Bazel what file(s) this target produced
28+
out="$(bazel cquery "$LABEL" --output=files | head -n1)"
29+
[[ -n "$out/include" ]] || { echo "No output for $LABEL"; exit 1; }
30+
31+
rm -rf "$DST/include"
32+
cp -rLf "$out/include" "$DST/include"
33+
chmod -R u+w "$DST"

cinterop-c/include/kgrpc.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*
2-
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
1+
// Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
42

53
/*
64
* Helper functions required for gRPC Core cinterop.
@@ -25,10 +23,11 @@ typedef struct {
2523
void *user_data;
2624
} kgrpc_cb_tag;
2725

28-
/*
26+
27+
/*
2928
* Call to grpc_iomgr_run_in_background(), which is not exposed as extern "C" and therefore must be wrapped.
3029
*/
31-
bool kgrpc_iomgr_run_in_background();
30+
bool kgrpc_iomgr_run_in_background();
3231

3332
#ifdef __cplusplus
3433
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
2+
load("//:tools/collect_headers.bzl", "cc_headers_only", "include_dir")
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
#### Compile gRPC from scratch ####
7+
8+
cc_static_library(
9+
name = "grpc_fat",
10+
deps = [
11+
"@com_github_grpc_grpc//:grpc",
12+
],
13+
)
14+
15+
include_dir(
16+
name = "grpc_include_dir",
17+
target = "@com_github_grpc_grpc//:grpc",
18+
)
19+
20+
#### Targets for pre-built dependency ####
21+
22+
# extract only the header files from grpc without compiling it
23+
cc_headers_only(
24+
name = "grpc_hdrs_ccinfo",
25+
dep = "@com_github_grpc_grpc//:grpc",
26+
)
27+
28+
cc_library(
29+
name = "grpc_core_prebuilt",
30+
visibility = ["//visibility:public"],
31+
deps = [":grpc_hdrs_ccinfo"] +
32+
# pick the right prebuilt .a:
33+
select({
34+
":ios_device_arm64": [":grpc_core_ios_arm64"],
35+
":ios_simulator_arm64": [":grpc_core_ios_sim_arm64"],
36+
":ios_simulator_x64": [":grpc_core_ios_x64"],
37+
":macos_arm64": [":grpc_core_macos_arm64"],
38+
"//conditions:default": [],
39+
}),
40+
)
41+
42+
config_setting(
43+
name = "ios_device_arm64",
44+
constraint_values = [
45+
"@platforms//os:ios",
46+
"@platforms//cpu:arm64",
47+
"@build_bazel_apple_support//constraints:device",
48+
],
49+
)
50+
51+
config_setting(
52+
name = "ios_simulator_arm64",
53+
constraint_values = [
54+
"@platforms//os:ios",
55+
"@platforms//cpu:arm64",
56+
"@build_bazel_apple_support//constraints:simulator",
57+
],
58+
)
59+
60+
config_setting(
61+
name = "ios_simulator_x64",
62+
constraint_values = [
63+
"@platforms//os:ios",
64+
"@platforms//cpu:x86_64",
65+
"@build_bazel_apple_support//constraints:simulator",
66+
],
67+
)
68+
69+
config_setting(
70+
name = "macos_arm64",
71+
constraint_values = [
72+
"@platforms//os:macos",
73+
"@platforms//cpu:arm64",
74+
],
75+
)
76+
77+
cc_import(
78+
name = "grpc_core_ios_arm64",
79+
static_library = ":libgrpc_fat.ios_arm64.a",
80+
visibility = ["//visibility:public"],
81+
)
82+
83+
cc_import(
84+
name = "grpc_core_ios_sim_arm64",
85+
static_library = ":libgrpc_fat.ios_simulator_arm64.a",
86+
visibility = ["//visibility:public"],
87+
)
88+
89+
cc_import(
90+
name = "grpc_core_ios_x64",
91+
static_library = ":libgrpc_fat.ios_x64.a",
92+
visibility = ["//visibility:public"],
93+
)
94+
95+
cc_import(
96+
name = "grpc_core_macos_arm64",
97+
static_library = ":libgrpc_fat.macos_arm64.a",
98+
visibility = ["//visibility:public"],
99+
)
23 MB
Binary file not shown.

0 commit comments

Comments
 (0)