-
Notifications
You must be signed in to change notification settings - Fork 40
grpc-native: Pre-Built gRPC library to avoid long compilation times #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c3e364
grpc-native: Build cache, not working
Jozott00 65cc9cd
grpc-native: Build cache within repository
Jozott00 6da5041
grpc-native: Add gRPC includes
Jozott00 70e0566
grpc-native: Cleanup
Jozott00 ec296fb
grpc-native: Small fixes
Jozott00 1e2d1e4
grpc-native: Use header sources from dependency instead of copy (bazel)
Jozott00 107eba0
grpc-native: Extract headers on demand
Jozott00 9dcc7a8
grpc-native: Remove copy of include directory
Jozott00 c1e39c3
grpc-native: Remove unnecessary source dir
Jozott00 2946a71
grpc-native: Address PR comments
Jozott00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Verify gRPC CLib Checksum is Up-to-Date | ||
|
||
on: | ||
pull_request: | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
verify-platforms-table: | ||
name: Run Verification | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Sources | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- name: Setup Gradle | ||
uses: gradle/actions/setup-gradle@v4 | ||
- name: Run gRPC Checksum Generation | ||
run: ./gradlew grpc:grpc-core:computeSha256AllTargetsForCLibGrpc_fat --info --stacktrace | ||
- name: Check if prebuilt gRPC library checksum is up-to-date | ||
run: | | ||
if [[ -n "$(git status --porcelain | grep cinterop-c/prebuilt-deps/grpc_fat/)" ]]; then | ||
echo "Prebuilt gRPC library checksum is not up to date. Please run './gradlew grpc:grpc-core:computeSha256AllTargetsForCLibGrpc_fat' and commit changes" | ||
exit 1 | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,3 @@ | |
/.aswb/ | ||
/.clwb/ | ||
.idea/ | ||
|
||
out/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
# | ||
|
||
set -Eeuo pipefail | ||
trap 'echo "ERROR: Build failed at ${BASH_SOURCE}:${LINENO}" >&2' ERR | ||
|
||
# Extract all headers in the /include directory of the given target. | ||
# | ||
# Usage: | ||
# ./extract_include_dir.sh <target> <output-directory> | ||
# Example: | ||
# ./extract_include_dir.sh //prebuilt-deps/grpc_fat:grpc_fat prebuilt-deps/grpc_fat | ||
# | ||
# The example will produce the prebuilt-deps/grpc_fat/include directory with all headers. | ||
|
||
LABEL="${1:?need bazel target label}" | ||
DST="${2:?need output destination}" | ||
|
||
CONFIG=release | ||
|
||
mkdir -p $(dirname "$DST") | ||
|
||
bazel build "$LABEL" >/dev/null | ||
|
||
# Ask Bazel what file(s) this target produced | ||
out="$(bazel cquery "$LABEL" --output=files | head -n1)" | ||
[[ -n "$out/include" ]] || { echo "No output for $LABEL"; exit 1; } | ||
|
||
rm -rf "$DST/include" | ||
cp -rLf "$out/include" "$DST/include" | ||
chmod -R u+w "$DST" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library") | ||
load("//:tools/collect_headers.bzl", "cc_headers_only", "include_dir") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
#### Compile gRPC from scratch #### | ||
|
||
cc_static_library( | ||
name = "grpc_fat", | ||
deps = [ | ||
"@com_github_grpc_grpc//:grpc", | ||
], | ||
) | ||
|
||
include_dir( | ||
name = "grpc_include_dir", | ||
target = "@com_github_grpc_grpc//:grpc", | ||
) | ||
|
||
#### Targets for pre-built dependency #### | ||
|
||
# extract only the header files from grpc without compiling it | ||
cc_headers_only( | ||
name = "grpc_hdrs_ccinfo", | ||
dep = "@com_github_grpc_grpc//:grpc", | ||
) | ||
|
||
cc_library( | ||
name = "grpc_core_prebuilt", | ||
visibility = ["//visibility:public"], | ||
deps = [":grpc_hdrs_ccinfo"] + | ||
# pick the right prebuilt .a: | ||
select({ | ||
":ios_device_arm64": [":grpc_core_ios_arm64"], | ||
":ios_simulator_arm64": [":grpc_core_ios_sim_arm64"], | ||
":ios_simulator_x64": [":grpc_core_ios_x64"], | ||
":macos_arm64": [":grpc_core_macos_arm64"], | ||
"//conditions:default": [], | ||
}), | ||
) | ||
|
||
config_setting( | ||
name = "ios_device_arm64", | ||
constraint_values = [ | ||
"@platforms//os:ios", | ||
"@platforms//cpu:arm64", | ||
"@build_bazel_apple_support//constraints:device", | ||
], | ||
) | ||
|
||
config_setting( | ||
name = "ios_simulator_arm64", | ||
constraint_values = [ | ||
"@platforms//os:ios", | ||
"@platforms//cpu:arm64", | ||
"@build_bazel_apple_support//constraints:simulator", | ||
], | ||
) | ||
|
||
config_setting( | ||
name = "ios_simulator_x64", | ||
constraint_values = [ | ||
"@platforms//os:ios", | ||
"@platforms//cpu:x86_64", | ||
"@build_bazel_apple_support//constraints:simulator", | ||
], | ||
) | ||
|
||
config_setting( | ||
name = "macos_arm64", | ||
constraint_values = [ | ||
"@platforms//os:macos", | ||
"@platforms//cpu:arm64", | ||
], | ||
) | ||
|
||
cc_import( | ||
name = "grpc_core_ios_arm64", | ||
static_library = ":libgrpc_fat.ios_arm64.a", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_import( | ||
name = "grpc_core_ios_sim_arm64", | ||
static_library = ":libgrpc_fat.ios_simulator_arm64.a", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_import( | ||
name = "grpc_core_ios_x64", | ||
static_library = ":libgrpc_fat.ios_x64.a", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_import( | ||
name = "grpc_core_macos_arm64", | ||
static_library = ":libgrpc_fat.macos_arm64.a", | ||
visibility = ["//visibility:public"], | ||
) |
Binary file not shown.
1 change: 1 addition & 0 deletions
1
cinterop-c/prebuilt-deps/grpc_fat/libgrpc_fat.ios_arm64.a.sha256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7bc13a15c59850048e77b5719ea262a6fcc22884ab42c9f14f38859200faac9b |
Binary file not shown.
1 change: 1 addition & 0 deletions
1
cinterop-c/prebuilt-deps/grpc_fat/libgrpc_fat.ios_simulator_arm64.a.sha256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
55fde875508372860125ee8505ef4e84b95c9620744378a99f8f4095292dcae5 |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7544113fe9b6c9d0a57c8b8864b5272e01e3a64ea36cd0802d4a76e98ad5e232 |
Binary file not shown.
1 change: 1 addition & 0 deletions
1
cinterop-c/prebuilt-deps/grpc_fat/libgrpc_fat.macos_arm64.a.sha256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6cf466a285832ecd0f41041164c412e9b541388210058310d7bb59f6a27d09de |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
HeaderInfo = provider(fields = ["headers_dir", "headers"]) | ||
|
||
# determines if the file is in the given repository | ||
def _same_repo(file, repo_name): | ||
p = file.path | ||
if repo_name: | ||
return ("external/%s/" % repo_name) in p | ||
|
||
return "external/" not in p | ||
|
||
def _include_dir_impl(ctx): | ||
cc = ctx.attr.target[CcInfo].compilation_context | ||
|
||
# transitive headers | ||
all_hdrs = cc.headers | ||
hdrs = all_hdrs.to_list() if type(all_hdrs) == "depset" else all_hdrs | ||
|
||
repo_name = ctx.attr.target.label.repo_name | ||
|
||
# filter: same repo + paths that contain /include/ | ||
hdrs = [ | ||
f | ||
for f in hdrs | ||
if "/include/" in f.path and _same_repo(f, repo_name) | ||
] | ||
|
||
manifest = ctx.actions.declare_file(ctx.label.name + ".headers.list") | ||
outdir = ctx.actions.declare_directory(ctx.label.name + "_includes") | ||
|
||
ctx.actions.write(manifest, "\n".join([f.path for f in hdrs])) | ||
|
||
# copy them to some output directory | ||
ctx.actions.run_shell( | ||
inputs = hdrs + [manifest], | ||
outputs = [outdir], | ||
command = r""" | ||
set -euo pipefail | ||
OUT="$1"; MAN="$2" | ||
python3 - <<'PY' "$OUT" "$MAN" | ||
import os, shutil, sys | ||
out, man = sys.argv[1], sys.argv[2] | ||
for p in open(man): | ||
p = p.strip() | ||
if not p: continue | ||
# keep include/ prefix once | ||
rel = p.split("/include/", 1)[1] | ||
dst = os.path.join(out, "include", rel) | ||
os.makedirs(os.path.dirname(dst), exist_ok=True) | ||
shutil.copy2(p, dst) | ||
PY | ||
""", | ||
arguments = [outdir.path, manifest.path], | ||
progress_message = "Collecting repo-scoped headers into %s" % outdir.path, | ||
mnemonic = "CollectIncludes", | ||
) | ||
|
||
return [ | ||
DefaultInfo(files = depset([outdir])), | ||
HeaderInfo(headers_dir = outdir, headers = depset(hdrs)), | ||
] | ||
|
||
# rule to copy the include directory of some target to some location | ||
include_dir = rule( | ||
implementation = _include_dir_impl, | ||
attrs = { | ||
"target": attr.label(mandatory = True), | ||
}, | ||
) | ||
|
||
def _cc_headers_only_impl(ctx): | ||
dep_cc = ctx.attr.dep[CcInfo].compilation_context | ||
|
||
# keep only source headers; this skips generated headers and their actions. | ||
all_hdrs = dep_cc.headers.to_list() | ||
src_hdrs = [f for f in all_hdrs if getattr(f, "is_source", False)] | ||
cc_ctx = cc_common.create_compilation_context( | ||
headers = depset(src_hdrs), | ||
includes = dep_cc.includes, | ||
quote_includes = dep_cc.quote_includes, | ||
system_includes = dep_cc.system_includes, | ||
framework_includes = dep_cc.framework_includes, | ||
defines = dep_cc.defines, | ||
) | ||
return [CcInfo(compilation_context = cc_ctx)] | ||
|
||
# rule to build a CcInfo that only contains the headers of a given CcInfo target. | ||
# this allows us to combine the headers of the dependency sources with our pre-compiled dependencies. | ||
cc_headers_only = rule( | ||
implementation = _cc_headers_only_impl, | ||
attrs = {"dep": attr.label(mandatory = True, providers = [CcInfo])}, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.