Skip to content

Commit 107eba0

Browse files
committed
grpc-native: Extract headers on demand
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 1e2d1e4 commit 107eba0

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import java.io.File
2525
fun KotlinMultiplatformExtension.configureCLibCInterop(
2626
project: Project,
2727
bazelTask: String,
28+
cinteropTaskDependsOn: List<Any> = emptyList(),
2829
configureCinterop: NamedDomainObjectContainer<DefaultCInteropSettings>.(cLibSource: File, cLibOutDir: File) -> Unit,
2930
) {
3031
val buildTargetName = bazelTask.split(":").last()
@@ -56,6 +57,7 @@ fun KotlinMultiplatformExtension.configureCLibCInterop(
5657
val interopTask = "cinterop${interop.name.capitalized()}${this@configureEach.targetName.capitalized()}"
5758
project.tasks.named(interopTask, CInteropProcess::class) {
5859
dependsOn(buildCinteropCLib)
60+
cinteropTaskDependsOn.forEach { dependsOn(it) }
5961
}
6062
}
6163
}
@@ -76,12 +78,7 @@ fun KotlinMultiplatformExtension.configureCLibCInterop(
7678
fun KotlinMultiplatformExtension.configureCLibDependency(
7779
project: Project,
7880
bazelTask: String,
79-
bazelExtractIncludeTask: String? = null,
80-
bazelExtractIncludeOutputDir: Provider<Directory>? = null,
8181
) {
82-
require((bazelExtractIncludeTask == null) == (bazelExtractIncludeOutputDir == null)) {
83-
"Either both bazelExtractIncludeTask and bazelExtractIncludeOutputDir must be specified or neither."
84-
}
8582
val buildTargetName = bazelTask.split(":").last()
8683
val prebuiltLibDir = project.cLibPrebuiltDepsDir.resolve(buildTargetName)
8784

@@ -106,21 +103,6 @@ fun KotlinMultiplatformExtension.configureCLibDependency(
106103
}
107104
}
108105

109-
if (bazelExtractIncludeTask != null) {
110-
val includeDir = bazelExtractIncludeOutputDir!!.get().asFile
111-
project.tasks.register<Exec>("buildIncludeDirCLib${buildTargetName.capitalized()}") {
112-
dependsOn(":checkBazel")
113-
group = "build"
114-
workingDir = project.cinteropLibDir
115-
commandLine(
116-
"bash",
117-
"-c",
118-
"./extract_include_dir.sh //prebuilt-deps/grpc_fat:grpc_include_dir $includeDir"
119-
)
120-
outputs.dir(includeDir.resolve("include"))
121-
}
122-
}
123-
124106
project.tasks.register("buildDependencyAllTargetsForCLib${buildTargetName.capitalized()}") {
125107
group = "build"
126108
targets.withType<KotlinNativeTarget>().forEach { target ->
@@ -135,6 +117,26 @@ fun KotlinMultiplatformExtension.configureCLibDependency(
135117
}
136118
}
137119

120+
fun Project.registerBuildCLibIncludeDirTask(
121+
bazelTask: String,
122+
bazelExtractIncludeOutputDir: Provider<Directory>,
123+
): TaskProvider<Exec> {
124+
val buildTargetName = bazelTask.split(":").last()
125+
val includeDir = bazelExtractIncludeOutputDir.get().asFile
126+
return project.tasks.register<Exec>("buildIncludeDirCLib${buildTargetName.capitalized()}") {
127+
dependsOn(":checkBazel")
128+
group = "build"
129+
workingDir = project.cinteropLibDir
130+
commandLine(
131+
"bash",
132+
"-c",
133+
"./extract_include_dir.sh //prebuilt-deps/grpc_fat:grpc_include_dir $includeDir"
134+
)
135+
outputs.dir(includeDir.resolve("include"))
136+
}
137+
}
138+
139+
138140
private val Project.cLibPrebuiltDepsDir: File get() = cinteropLibDir.resolve("prebuilt-deps")
139141

140142
private val Project.bazelBuildDir: Directory

grpc/grpc-core/build.gradle.kts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.rpc.internal.InternalRpcApi
88
import kotlinx.rpc.internal.configureLocalProtocGenDevelopmentDependency
99
import util.configureCLibCInterop
1010
import util.configureCLibDependency
11+
import util.registerBuildCLibIncludeDirTask
1112

1213
plugins {
1314
alias(libs.plugins.conventions.kmp)
@@ -72,28 +73,33 @@ kotlin {
7273
}
7374

7475

76+
// configure task to extract the include dir from the gRPC core library
7577
val grpcIncludeDir = project.layout.buildDirectory.dir("bazel-out/grpc-include")
76-
configureCLibCInterop(project, ":kgrpc") { cLibSource, cLibOutDir ->
78+
val grpcIncludeDirTask = project.registerBuildCLibIncludeDirTask(
79+
"//prebuilt-deps/grpc_fat:grpc_include_dir",
80+
grpcIncludeDir
81+
)
82+
83+
// configure pre-built gRPC core library
84+
configureCLibDependency(project, "//prebuilt-deps/grpc_fat:grpc_fat")
85+
86+
configureCLibCInterop(
87+
project, ":kgrpc",
88+
// depends on the grpc include dir
89+
cinteropTaskDependsOn = listOf(grpcIncludeDirTask)
90+
) { cLibSource, cLibOutDir ->
7791
val grpcPrebuiltDir = cLibSource.resolve("prebuilt-deps/grpc_fat")
7892

7993
@Suppress("unused")
8094
val libkgrpc by creating {
8195
includeDirs(
8296
cLibSource.resolve("include"),
83-
cLibSource.resolve("$grpcPrebuiltDir/include"),
97+
cLibSource.resolve("${grpcIncludeDir.get()}/include"),
8498
)
8599
extraOpts("-libraryPath", "$grpcPrebuiltDir")
86100
extraOpts("-libraryPath", "$cLibOutDir")
87101
}
88102
}
89-
90-
configureCLibDependency(
91-
project,
92-
bazelTask = "//prebuilt-deps/grpc_fat:grpc_fat",
93-
bazelExtractIncludeTask = "//prebuilt-deps/grpc_fat:grpc_include_dir",
94-
bazelExtractIncludeOutputDir = grpcIncludeDir,
95-
)
96-
97103
}
98104

99105
configureLocalProtocGenDevelopmentDependency()

0 commit comments

Comments
 (0)