Skip to content

Commit 1e2d1e4

Browse files
committed
grpc-native: Use header sources from dependency instead of copy (bazel)
Signed-off-by: Johannes Zottele <[email protected]>
1 parent ec296fb commit 1e2d1e4

File tree

7 files changed

+66
-16
lines changed

7 files changed

+66
-16
lines changed

cinterop-c/include/kgrpc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ typedef struct {
2323
void *user_data;
2424
} kgrpc_cb_tag;
2525

26+
27+
/*
28+
* Call to grpc_iomgr_run_in_background(), which is not exposed as extern "C" and therefore must be wrapped.
29+
*/
30+
bool kgrpc_iomgr_run_in_background();
31+
2632
#ifdef __cplusplus
2733
}
2834
#endif

cinterop-c/prebuilt-deps/grpc_fat/BUILD.bazel

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
2-
load("//:tools/collect_headers.bzl", "include_dir")
2+
load("//:tools/collect_headers.bzl", "cc_headers_only", "include_dir")
33

44
package(default_visibility = ["//visibility:public"])
55

@@ -19,19 +19,24 @@ include_dir(
1919

2020
#### Targets for pre-built dependency ####
2121

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+
2228
cc_library(
2329
name = "grpc_core_prebuilt",
24-
hdrs = glob(["include/**"]),
25-
includes = ["include"],
2630
visibility = ["//visibility:public"],
27-
# pick the right prebuilt .a:
28-
deps = select({
29-
":ios_device_arm64": [":grpc_core_ios_arm64"],
30-
":ios_simulator_arm64": [":grpc_core_ios_sim_arm64"],
31-
":ios_simulator_x64": [":grpc_core_ios_x64"],
32-
":macos_arm64": [":grpc_core_macos_arm64"],
33-
"//conditions:default": [],
34-
}),
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+
}),
3540
)
3641

3742
config_setting(

cinterop-c/src/kgrpc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
22

33
#include <kgrpc.h>
4+
#include "src/core/lib/iomgr/iomgr.h"
45

56
extern "C" {
67

8+
bool kgrpc_iomgr_run_in_background() {
9+
return grpc_iomgr_run_in_background();
10+
}
11+
712
}
813

914

cinterop-c/tools/collect_headers.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def _include_dir_impl(ctx):
2828
outdir = ctx.actions.declare_directory(ctx.label.name + "_includes")
2929

3030
ctx.actions.write(manifest, "\n".join([f.path for f in hdrs]))
31+
32+
# copy them to some output directory
3133
ctx.actions.run_shell(
3234
inputs = hdrs + [manifest],
3335
outputs = [outdir],
@@ -57,9 +59,33 @@ PY
5759
HeaderInfo(headers_dir = outdir, headers = depset(hdrs)),
5860
]
5961

62+
# rule to copy the include directory of some target to some location
6063
include_dir = rule(
6164
implementation = _include_dir_impl,
6265
attrs = {
6366
"target": attr.label(mandatory = True),
6467
},
6568
)
69+
70+
def _cc_headers_only_impl(ctx):
71+
dep_cc = ctx.attr.dep[CcInfo].compilation_context
72+
73+
# keep only source headers; this skips generated headers and their actions.
74+
all_hdrs = dep_cc.headers.to_list()
75+
src_hdrs = [f for f in all_hdrs if getattr(f, "is_source", False)]
76+
cc_ctx = cc_common.create_compilation_context(
77+
headers = depset(src_hdrs),
78+
includes = dep_cc.includes,
79+
quote_includes = dep_cc.quote_includes,
80+
system_includes = dep_cc.system_includes,
81+
framework_includes = dep_cc.framework_includes,
82+
defines = dep_cc.defines,
83+
)
84+
return [CcInfo(compilation_context = cc_ctx)]
85+
86+
# rule to build a CcInfo that only contains the headers of a given CcInfo target.
87+
# this allows us to combine the headers of the dependency sources with our pre-compiled dependencies.
88+
cc_headers_only = rule(
89+
implementation = _cc_headers_only_impl,
90+
attrs = {"dep": attr.label(mandatory = True, providers = [CcInfo])},
91+
)

gradle-conventions/src/main/kotlin/conventions-root.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import util.other.isPublicModule
66
import util.other.libs
77
import util.setupPage
88
import util.tasks.*
9-
import java.io.OutputStream
109
import java.nio.file.Path
1110
import kotlin.io.path.*
1211

@@ -116,7 +115,7 @@ gradle.afterProject {
116115
tasks.register<Exec>("checkBazel") {
117116
commandLine("which", "bazel")
118117
isIgnoreExitValue = true
119-
standardOutput = OutputStream.nullOutputStream()
118+
// standardOutput = ByteArrayOutputStream()
120119
doLast {
121120
if (executionResult.get().exitValue != 0) {
122121
throw GradleException("'bazel' not found on PATH. Please install Bazel (https://bazel.build/).")

gradle-conventions/src/main/kotlin/util/cinterop.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package util
77
import org.gradle.api.NamedDomainObjectContainer
88
import org.gradle.api.Project
99
import org.gradle.api.file.Directory
10+
import org.gradle.api.provider.Provider
1011
import org.gradle.api.tasks.Exec
1112
import org.gradle.api.tasks.TaskContainer
1213
import org.gradle.api.tasks.TaskProvider
@@ -76,7 +77,11 @@ fun KotlinMultiplatformExtension.configureCLibDependency(
7677
project: Project,
7778
bazelTask: String,
7879
bazelExtractIncludeTask: String? = null,
80+
bazelExtractIncludeOutputDir: Provider<Directory>? = null,
7981
) {
82+
require((bazelExtractIncludeTask == null) == (bazelExtractIncludeOutputDir == null)) {
83+
"Either both bazelExtractIncludeTask and bazelExtractIncludeOutputDir must be specified or neither."
84+
}
8085
val buildTargetName = bazelTask.split(":").last()
8186
val prebuiltLibDir = project.cLibPrebuiltDepsDir.resolve(buildTargetName)
8287

@@ -102,16 +107,17 @@ fun KotlinMultiplatformExtension.configureCLibDependency(
102107
}
103108

104109
if (bazelExtractIncludeTask != null) {
110+
val includeDir = bazelExtractIncludeOutputDir!!.get().asFile
105111
project.tasks.register<Exec>("buildIncludeDirCLib${buildTargetName.capitalized()}") {
106112
dependsOn(":checkBazel")
107113
group = "build"
108114
workingDir = project.cinteropLibDir
109115
commandLine(
110116
"bash",
111117
"-c",
112-
"./extract_include_dir.sh //prebuilt-deps/grpc_fat:grpc_include_dir $prebuiltLibDir"
118+
"./extract_include_dir.sh //prebuilt-deps/grpc_fat:grpc_include_dir $includeDir"
113119
)
114-
outputs.dir(prebuiltLibDir.resolve("include"))
120+
outputs.dir(includeDir.resolve("include"))
115121
}
116122
}
117123

grpc/grpc-core/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ kotlin {
7171
}
7272
}
7373

74+
75+
val grpcIncludeDir = project.layout.buildDirectory.dir("bazel-out/grpc-include")
7476
configureCLibCInterop(project, ":kgrpc") { cLibSource, cLibOutDir ->
7577
val grpcPrebuiltDir = cLibSource.resolve("prebuilt-deps/grpc_fat")
7678

@@ -88,7 +90,8 @@ kotlin {
8890
configureCLibDependency(
8991
project,
9092
bazelTask = "//prebuilt-deps/grpc_fat:grpc_fat",
91-
bazelExtractIncludeTask = "//prebuilt-deps/grpc_fat:grpc_include_dir"
93+
bazelExtractIncludeTask = "//prebuilt-deps/grpc_fat:grpc_include_dir",
94+
bazelExtractIncludeOutputDir = grpcIncludeDir,
9295
)
9396

9497
}

0 commit comments

Comments
 (0)