Skip to content

Instantly share code, notes, and snippets.

@micolous
Created October 28, 2019 08:47
Show Gist options
  • Save micolous/c00b14b2dc321fdb0eab8ad796d71b80 to your computer and use it in GitHub Desktop.
Save micolous/c00b14b2dc321fdb0eab8ad796d71b80 to your computer and use it in GitHub Desktop.
run pkgConfig in gradle config for Kotlin/Native
/**
* Runs `pkg-config`:
* https://github.com/JetBrains/kotlin-native/issues/1534#issuecomment-384894431
*/
fun runPkgConfig(
vararg packageNames: String,
cflags: Boolean = false,
libs: Boolean = false): List<String> {
val p = ProcessBuilder(*(listOfNotNull(
"pkg-config",
if (cflags) "--cflags" else null,
if (libs) "--libs" else null
).toTypedArray() + packageNames)).run {
// https://github.com/JetBrains/kotlin-native/issues/3484#issuecomment-544926683
environment()["PKG_CONFIG_ALLOW_SYSTEM_LIBS"] = "1"
start()
}.also { it.waitFor(10, TimeUnit.SECONDS) }
if (p.exitValue() != 0) {
throw GradleException("Error executing pkg-config: ${p.errorStream.bufferedReader().readText()}")
}
return p.inputStream.bufferedReader().readText().split(" ").map{ it.trim() }
}
fun DefaultCInteropSettings.pkgConfig(
vararg packageNames: String,
cflags: Boolean = true,
libs: Boolean = true) {
if (cflags) {
compilerOpts.addAll(runPkgConfig(*packageNames, cflags=true))
}
if (libs) {
linkerOpts.addAll(runPkgConfig(*packageNames, libs=true))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment