Move globals into Utils

This commit is contained in:
Attila Uygun 2023-09-05 17:50:32 +02:00
parent 48fb1589c6
commit ea93f80aa1
1 changed files with 57 additions and 54 deletions

View File

@ -52,6 +52,7 @@ class Utils implements Plugin<Project> {
project.android.productFlavors.each { game -> project.android.productFlavors.each { game ->
if (game.dimension == 'game') { if (game.dimension == 'game') {
project.android.productFlavors.each { arch -> project.android.productFlavors.each { arch ->
// Only need to add tasks for arch types which maps to a single ABI
if (arch.dimension == 'arch' && arch.ndk.abiFilters.size() == 1) { if (arch.dimension == 'arch' && arch.ndk.abiFilters.size() == 1) {
project.android.buildTypes.each { buildType -> project.android.buildTypes.each { buildType ->
callback(game.name.capitalize(), arch.name.capitalize(), buildType.name.capitalize()) callback(game.name.capitalize(), arch.name.capitalize(), buildType.name.capitalize())
@ -64,7 +65,7 @@ class Utils implements Plugin<Project> {
void forEachBuildType(Closure callback) { void forEachBuildType(Closure callback) {
project.android.buildTypes.each { buildType -> project.android.buildTypes.each { buildType ->
callback(buildType.name.capitalize()) callback(buildType.name)
} }
} }
@ -75,48 +76,48 @@ class Utils implements Plugin<Project> {
} }
return outList return outList
} }
}
def generateGnArgsContent(String buildType, String arch) { def getArchTypesRegExp() {
def content = 'target_os="android"\n' def outList = []
content += 'target_cpu="' + utils.CPU_CODES[arch] + '"\n' project.android.productFlavors.each { flavor ->
content += "is_debug=${buildType != 'Release'}\n" if (flavor.dimension == 'arch') {
content += 'ndk="' + android.ndkDirectory + '"\n' outList += flavor.name.capitalize()
content += "ndk_api=${rootProject.ext.minSdk}\n" }
return content
}
def getOutDir(String buildType) {
return "${project.buildDir}/gn_out/${buildType.toLowerCase()}"
}
def getAssetsDir(String buildType) {
return "${project.buildDir}/gn_out/${buildType.toLowerCase()}/assets"
}
def getJniLibsDir(String buildType) {
return "${project.buildDir}/gn_out/jniLibs/${buildType.toLowerCase()}"
}
def getGnTargetFor(String game) {
def outStr = ''
android.productFlavors.find { flavor ->
if (flavor.dimension == 'game' && flavor.name.capitalize() == game) {
outStr = flavor.ext.gnTarget
return true
} }
return outList
} }
return outStr
}
def getArchTypesRegExp() { def getGnTargetFor(String game) {
def outList = [] def outStr = ''
android.productFlavors.each { flavor -> project.android.productFlavors.find { flavor ->
if (flavor.dimension == 'arch') { if (flavor.dimension == 'game' && flavor.name.capitalize() == game) {
outList += flavor.name.capitalize() outStr = flavor.ext.gnTarget
return true
}
} }
return outStr
}
def generateGnArgsContent(String buildType, String arch) {
def content = 'target_os="android"\n'
content += 'target_cpu="' + CPU_CODES[arch] + '"\n'
content += "is_debug=${buildType != 'Release'}\n"
content += 'ndk="' + project.android.ndkDirectory + '"\n'
content += "ndk_api=${project.rootProject.ext.minSdk}\n"
return content
}
def getOutDir(String buildType) {
return "${project.buildDir}/gn_out/${buildType.toLowerCase()}"
}
def getAssetsDir(String buildType) {
return "${project.buildDir}/gn_out/${buildType.toLowerCase()}/assets"
}
def getJniLibsDir(String buildType) {
return "${project.buildDir}/gn_out/jniLibs/${buildType.toLowerCase()}"
} }
return outList
} }
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
@ -190,6 +191,7 @@ android {
abiFilters = ["x86_64"] abiFilters = ["x86_64"]
} }
} }
allArchs { allArchs {
dimension 'arch' dimension 'arch'
ndk { ndk {
@ -214,14 +216,14 @@ android {
main { main {
java.srcDirs += ['../../../src/engine/platform/java/com/kaliber/base'] java.srcDirs += ['../../../src/engine/platform/java/com/kaliber/base']
utils.forEachBuildType { buildType -> utils.forEachBuildType { buildType ->
"${buildType.toLowerCase()}" { "${buildType}" {
assets.srcDirs = [getAssetsDir(buildType)] assets.srcDirs = [utils.getAssetsDir(buildType)]
} }
} }
} }
utils.forEachBuildType { buildType -> utils.forEachBuildType { buildType ->
"${buildType.toLowerCase()}" { "${buildType}" {
jniLibs.srcDirs = [getJniLibsDir(buildType)] jniLibs.srcDirs = [utils.getJniLibsDir(buildType)]
} }
} }
} }
@ -238,8 +240,8 @@ dependencies {
utils.addTask('generateGnArgsFor') { String taskName, String buildType, String arch, String game -> utils.addTask('generateGnArgsFor') { String taskName, String buildType, String arch, String game ->
task(taskName, type: WriteFileTask) { task(taskName, type: WriteFileTask) {
content = generateGnArgsContent(buildType, arch) content = utils.generateGnArgsContent(buildType, arch)
target = project.layout.file(provider { new File("${getOutDir(buildType)}/${arch}", 'args.gn') }) target = project.layout.file(provider { new File("${utils.getOutDir(buildType)}/${arch}", 'args.gn') })
} }
} }
@ -248,10 +250,10 @@ utils.addTask('runGnFor') { String taskName, String buildType, String arch, Stri
dependsOn "generateGnArgsFor${game}${arch}${buildType}" dependsOn "generateGnArgsFor${game}${arch}${buildType}"
executable rootProject.ext.gn executable rootProject.ext.gn
args '--fail-on-unused-args', 'gen', "${getOutDir(buildType)}/${arch}" args '--fail-on-unused-args', 'gen', "${utils.getOutDir(buildType)}/${arch}"
inputs.file(new File("${getOutDir(buildType)}/${arch}", 'args.gn')) inputs.file(new File("${utils.getOutDir(buildType)}/${arch}", 'args.gn'))
outputs.file(new File("${getOutDir(buildType)}/${arch}", 'build.ninja')) outputs.file(new File("${utils.getOutDir(buildType)}/${arch}", 'build.ninja'))
} }
} }
@ -260,8 +262,9 @@ utils.addTask('runNinjaFor') { String taskName, String buildType, String arch, S
dependsOn "runGnFor${game}${arch}${buildType}" dependsOn "runGnFor${game}${arch}${buildType}"
executable rootProject.ext.ninja executable rootProject.ext.ninja
args '-C', "${getOutDir(buildType)}/${arch}", getGnTargetFor(game) args '-C', "${utils.getOutDir(buildType)}/${arch}", utils.getGnTargetFor(game)
// Always run ninja and let it figure out what needs to be compiled.
outputs.upToDateWhen { false } outputs.upToDateWhen { false }
} }
} }
@ -270,8 +273,8 @@ utils.addTask('copyAssetsFor') { String taskName, String buildType, String arch,
task(taskName, type: Copy) { task(taskName, type: Copy) {
dependsOn "runNinjaFor${game}${arch}${buildType}" dependsOn "runNinjaFor${game}${arch}${buildType}"
from "${getOutDir(buildType)}/${arch}/assets" from "${utils.getOutDir(buildType)}/${arch}/assets"
into getAssetsDir(buildType) into utils.getAssetsDir(buildType)
} }
} }
@ -279,16 +282,16 @@ utils.addTask('copyJniLibsFor') { String taskName, String buildType, String arch
task(taskName, type: Copy) { task(taskName, type: Copy) {
dependsOn "runNinjaFor${game}${arch}${buildType}" dependsOn "runNinjaFor${game}${arch}${buildType}"
from("${getOutDir(buildType)}/${arch}") { from("${utils.getOutDir(buildType)}/${arch}") {
include "lib${getGnTargetFor(game)}.so" include "lib${utils.getGnTargetFor(game)}.so"
rename "lib${getGnTargetFor(game)}.so", "libkaliber.so" rename "lib${utils.getGnTargetFor(game)}.so", "libkaliber.so"
} }
into "${getJniLibsDir(buildType)}/${utils.ABI_CODES[arch]}" into "${utils.getJniLibsDir(buildType)}/${utils.ABI_CODES[arch]}"
} }
} }
tasks.configureEach { task -> tasks.configureEach { task ->
def variantPattern = /(\w+)(${getArchTypesRegExp().join('|')})(${utils.getBuildTypesRegExp().join('|')})/ def variantPattern = /(\w+)(${utils.getArchTypesRegExp().join('|')})(${utils.getBuildTypesRegExp().join('|')})/
def match = task.name =~ /^merge/ + variantPattern + /JniLibFolders$/ def match = task.name =~ /^merge/ + variantPattern + /JniLibFolders$/
if (match) { if (match) {