1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 21:12:40 +08:00

Configure Rust interop

This commit is contained in:
Him188 2022-05-26 15:14:13 +01:00
parent ee71263f41
commit 70868b335a
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
2 changed files with 35 additions and 9 deletions
buildSrc/src/main/kotlin
gradle.properties

View File

@ -167,7 +167,11 @@ private fun Project.configureNativeInterop(
val cbindgen = tasks.register("cbindgen${compilationName.titlecase()}") {
group = "mirai"
inputs.files(nativeInteropDir.resolve("src"))
description = "Generate C Headers from Rust"
inputs.files(
project.objects.fileTree().from(nativeInteropDir.resolve("src"))
.filterNot { it.name == "bindings.rs" }
)
outputs.file(nativeInteropDir.resolve(headerName))
doLast {
exec {
@ -182,30 +186,49 @@ private fun Project.configureNativeInterop(
}
}
val cinteropTask = tasks.getByName("cinterop${compilationName.titlecase()}Native")
cinteropTask.mustRunAfter(cbindgen)
val generateRustBindings = tasks.register("generateRustBindings${compilationName.titlecase()}") {
group = "mirai"
description = "Generates Rust bindings for Kotlin"
dependsOn(cbindgen)
dependsOn(cinteropTask)
}
val bindgen = tasks.register("bindgen${compilationName.titlecase()}") {
group = "mirai"
val bindingsPath = nativeInteropDir.resolve("src/bindings.rs")
inputs.files(nativeInteropDir.resolve("src"))
val headerFile = buildDir.resolve("bin/native/debugShared/lib${crateName}_api.h")
inputs.files(headerFile)
outputs.file(bindingsPath)
mustRunAfter(tasks.findByName("linkDebugSharedNative"))
doLast {
exec {
workingDir(nativeInteropDir)
// bindgen input.h -o bindings.rs
commandLine(
"bindgen",
buildDir.resolve("bin/native/debugShared/lib${crateName}_api.h"),
headerFile,
"-o", bindingsPath,
)
}
}
}
val generateKotlinBindings = tasks.register("generateKotlinBindings${compilationName.titlecase()}") {
group = "mirai"
description = "Generates Kotlin bindings for Rust"
dependsOn(bindgen)
dependsOn(tasks.findByName("linkDebugSharedNative"))
}
var targetCompilation: KotlinNativeCompilation? = null
configure(nativeTargets) {
val compilations = compilations.filter { nativeInteropDir.name.contains(it.name, ignoreCase = true) }
check(compilations.isNotEmpty()) { "Should be at lease one corresponding native compilation, but found 0" }
targetCompilation = compilations.single()
targetCompilation!!.compileKotlinTask.dependsOn(cbindgen)
// targetCompilation!!.compileKotlinTask.dependsOn(cbindgen)
// tasks.getByName("cinteropNative$name").dependsOn(cbindgen)
}
targetCompilation!!
@ -213,17 +236,19 @@ private fun Project.configureNativeInterop(
val compileRust = tasks.register("compileRust${compilationName.titlecase()}") {
group = "mirai"
inputs.files(nativeInteropDir.resolve("src"))
outputs.file(rustLibDir.resolve("lib$crateName.dynlib"))
dependsOn(targetCompilation!!.compileKotlinTask)
outputs.file(rustLibDir.resolve("lib$crateName.dylib"))
// dependsOn(targetCompilation!!.compileKotlinTask)
dependsOn(bindgen)
dependsOn(tasks.findByName("linkDebugSharedNative"))
dependsOn(tasks.findByName("linkDebugSharedNative")) // dylib to link
doLast {
exec {
workingDir(nativeInteropDir)
commandLine(
"cargo",
"build",
"--all"
"--color", "always",
"--all",
"--", "--color", "always", "2>&1"
)
}
}

View File

@ -22,4 +22,5 @@ gnsp.disableApplyOnlyOnRootProjectEnforcement=true
# We may target 15 with Kotlin 1.5 IR
mirai.android.target.api.level=24
# Enable if you want to use mavenLocal for both Gradle plugin and project dependencies resolutions.
systemProp.use.maven.local=false
systemProp.use.maven.local=false
org.gradle.caching=true