Skip to content

Commit

Permalink
Package all .cmake, .la and .pc files from lib dir
Browse files Browse the repository at this point in the history
Previously was only picking up from specific subdirs, but libopenjpeg puts cmake files in non-standard lib/openjpeg-2.5 instead of lib/cmake/openjpeg-2.5
  • Loading branch information
ViliusSutkus89 committed Nov 12, 2023
1 parent 6e683d8 commit 95afc83
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,31 @@ class PrefabPackageBuilder(
val srcDir = installDirectory.resolve("$abi/lib")
val dstDir = libsDir.resolve("android.${abi.abiName}")

val abiInstallDir = installDirectory.resolve("$abi")
val generatedDir = installDirectory.resolve("../dependencies/generated/${abi.triple}").absoluteFile
val abiInstallDir = installDirectory.resolve("$abi").absolutePath
val generatedDir = installDirectory.resolve("../dependencies/generated/${abi.triple}").absolutePath
val ndkPath = ndk.path.absolutePath

listOf(
srcDir.resolve("cmake"),
srcDir.resolve("pkgconfig")
).forEach { dir ->
dir.walkTopDown().forEach {
val dst = dstDir.resolve(it.relativeTo(srcDir))
if (it.isDirectory) {
dst.mkdir()
} else if (it.isFile && it.extension in listOf("cmake", "pc")) {
dst.writeText(
it.readText()
.replace(abiInstallDir.absolutePath, "/__PREFAB__PACKAGE__PATH__")
.replace(generatedDir.absolutePath, "/__PREFAB__PACKAGE__PATH__")
.replace(ndk.path.absolutePath, "/__NDK__PATH__")
)
}
srcDir.walkTopDown().forEach { srcFile ->
if (srcFile.isFile && listOf("cmake", "la", "pc").contains(srcFile.extension)) {
val dstFile = dstDir.resolve(srcFile.relativeTo(srcDir))
dstFile.parentFile.mkdirs()
var configFileContent = srcFile.readText()
.replace(abiInstallDir, "/__PREFAB__PACKAGE__PATH__")
.replace(generatedDir, "/__PREFAB__PACKAGE__PATH__")
.replace(ndkPath, "/__NDK__PATH__")

// Some dependencies link against static libraries,
// but don't pick up private dependencies
if (module.static && it.isFile && it.extension == "pc") {
// but don't pick up private dependencies.
// Workaround this issue by marking all private
// dependencies as public dependencies in pkg-config.
if (module.static && dstFile.extension == "pc") {
val sb = StringBuilder()
val libs = mutableListOf<String>()
val libsPrivate = mutableListOf<String>()
val requires = mutableListOf<String>()
val requiresPrivate = mutableListOf<String>()
dst.readLines().forEach { line ->

configFileContent.split("\r\n", "\r", "\n").forEach { line ->
if (line.startsWith(prefix = "Libs:", ignoreCase = true)) {
libs.add(line.substring("Libs:".length))
}
Expand All @@ -262,18 +258,11 @@ class PrefabPackageBuilder(
sb.appendLine("Requires: $requiresWithPrivates")
}
}
dst.writeText(sb.toString())
configFileContent = sb.toString()
}
}
}

(srcDir.listFiles { file -> file.extension == "la" } ?: arrayOf<File>()).forEach {
dstDir.resolve(it.relativeTo(srcDir)).writeText(
it.readText()
.replace(abiInstallDir.absolutePath, "/__PREFAB__PACKAGE__PATH__")
.replace(generatedDir.absolutePath, "/__PREFAB__PACKAGE__PATH__")
.replace(ndk.path.absolutePath, "/__NDK__PATH__")
)
dstFile.writeText(configFileContent)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,20 @@ class PrefabSysrootPlugin(
}

private fun installConfigFiles(module: Module, libDir: File, abiTriple: String) {
val src = module.path.toFile().resolve("libs/android.${targetTripleToAbiName(abiTriple)}")

src.listFiles { file, filename ->
listOf("cmake", "pkgconfig").contains(filename) || file.extension == "la"
}?.forEach {
it.copyRecursively(libDir.resolve(it.name)) { file, exception ->
if (exception !is FileAlreadyExistsException) {
throw exception
val srcDir = module.path.toFile().resolve("libs/android.${targetTripleToAbiName(abiTriple)}")
srcDir.walkTopDown().forEach { srcFile ->
if (srcFile.isFile && listOf("cmake", "la", "pc").contains(srcFile.extension)) {
val dstFile = libDir.resolve(srcFile.relativeTo(srcDir))
dstFile.parentFile.mkdirs()
try {
srcFile.copyTo(dstFile)
} catch(e: FileAlreadyExistsException) {
if (!srcFile.readBytes().contentEquals(dstFile.readBytes())) {
throw RuntimeException(
"Found duplicate config files with non-equal contents: ${srcFile.relativeTo(srcDir)}"
)
}
}

if (!file.readBytes().contentEquals(exception.file.readBytes())) {
val path = file.relativeTo(src)
throw RuntimeException(
"Found duplicate headers with non-equal contents: $path"
)
}

OnErrorAction.SKIP
}
}
}
Expand Down
16 changes: 1 addition & 15 deletions openjpeg/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import com.android.ndkports.CMakePortTask
import com.android.ndkports.CMakeCompatibleVersion
import com.android.ndkports.CMakePortTask
import com.android.ndkports.PrefabSysrootPlugin
import org.gradle.jvm.tasks.Jar

Expand Down Expand Up @@ -28,22 +28,8 @@ val buildTask = tasks.register<CMakePortTask>("buildPort") {
cmake {
arg("-DBUILD_CODEC=OFF")
}

doLast {
com.android.ndkports.Abi.values().forEach { abi ->
installDirectoryFor(abi).let { installDirectory ->
val dstDir = installDirectory.resolve("include/android.${abi.abiName}/lib/openjpeg-2.5").apply { mkdirs() }
installDirectory.resolve("lib/openjpeg-2.5").listFiles()?.forEach {
dstDir.resolve(it.name).writeText(
it.readText().replace(installDirectory.absolutePath, "/__PREFAB__PACKAGE__PATH__")
)
}
}
}
}
}


tasks.prefabPackage {
version.set(CMakeCompatibleVersion.parse(portVersion))

Expand Down

0 comments on commit 95afc83

Please sign in to comment.