Skip to content

Commit

Permalink
Remove hardcoded ccache Android build config
Browse files Browse the repository at this point in the history
  • Loading branch information
louwers committed Nov 25, 2024
1 parent d282b3b commit c0812e2
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions platform/android/buildSrc/src/main/kotlin/NativeBuildPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.android.build.gradle.BaseExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType
import java.io.File

open class NativeBuildPlugin : Plugin<Project> {
override fun apply(project: Project) {
Expand All @@ -17,6 +18,27 @@ open class NativeBuildExtension {
var nativeTargets: List<String> = listOf()
}

fun findCcachePath(): String? {
val command = if (System.getProperty("os.name").startsWith("Windows")) {
"where ccache"
} else {
"which ccache"
}

return try {
val process = Runtime.getRuntime().exec(command)
val result = process.inputStream.bufferedReader().readText().trim()
if (process.waitFor() == 0 && result.isNotEmpty()) {
File(result).absolutePath
} else {
null
}
} catch (e: Exception) {
null
}
}


fun Project.nativeBuild(nativeTargets: List<String>) =
this.extensions.getByType<BaseExtension>().run {

Expand Down Expand Up @@ -66,11 +88,12 @@ fun Project.nativeBuild(nativeTargets: List<String>) =
)

// Enable ccache if available.
val ccachePaths = listOf("/usr/bin/ccache", "/usr/local/bin/ccache")
for (path in ccachePaths) {
if (file(path).exists()) {
arguments.add("-DANDROID_CCACHE=$path")
}
val ccachePath = findCcachePath()
if (ccachePath != null) {
arguments.add("-D=$ccachePath")
println("ccache enabled at: $ccachePath")
} else {
println("ccache not found on the system, continuing without it.")
}

cFlags.add("-Qunused-arguments")
Expand Down

0 comments on commit c0812e2

Please sign in to comment.