compile SDL once & cross-platformize gradle tasks

This commit is contained in:
Kylie C 2025-08-13 21:58:05 -04:00
parent b8fbd39213
commit b81d67cf7f
2 changed files with 56 additions and 32 deletions

View File

@ -73,22 +73,35 @@ if (DOWNLOAD_DEPENDENCIES)
# FetchContent downloads and configures dependencies
message(STATUS "Fetching SDL3 and iniparser. This might take a while...")
include(FetchContent)
if (WINDOWS_STORE)
FetchContent_Declare(
SDL3
GIT_REPOSITORY "https://github.com/Helloyunho/SDL3-uwp.git"
GIT_TAG "main"
EXCLUDE_FROM_ALL
if(ANDROID)
set(SDL3_SOURCE_DIR "${CMAKE_SOURCE_DIR}/android-project/build/_deps/sdl3-src")
add_library(SDL3 SHARED IMPORTED)
set_target_properties(SDL3 PROPERTIES
IMPORTED_LOCATION "${SDL3_SOURCE_DIR}/build-android/${CMAKE_ANDROID_ARCH_ABI}-build/libSDL3.so"
INTERFACE_INCLUDE_DIRECTORIES "${SDL3_SOURCE_DIR}/include"
)
add_library(SDL3::SDL3 ALIAS SDL3)
add_library(SDL3::Headers ALIAS SDL3)
else()
FetchContent_Declare(
SDL3
GIT_REPOSITORY "https://github.com/libsdl-org/SDL.git"
GIT_TAG "main"
EXCLUDE_FROM_ALL
)
if (WINDOWS_STORE)
FetchContent_Declare(
SDL3
GIT_REPOSITORY "https://github.com/Helloyunho/SDL3-uwp.git"
GIT_TAG "main"
EXCLUDE_FROM_ALL
)
else()
FetchContent_Declare(
SDL3
GIT_REPOSITORY "https://github.com/libsdl-org/SDL.git"
GIT_TAG "main"
EXCLUDE_FROM_ALL
)
endif()
FetchContent_MakeAvailable(SDL3)
endif()
FetchContent_MakeAvailable(SDL3)
FetchContent_Declare(
iniparser
@ -106,9 +119,6 @@ else()
# find_package looks for already-installed system packages.
# Configure with `-DCMAKE_PREFIX_PATH="/path/to/package1;/path/to/package2"`
# to add search paths.
if (ANDROID)
message(STATUS "Please place the version-appropriate SDL3.aar in android-project/app/libs")
endif()
find_package(SDL3 CONFIG REQUIRED)
find_package(iniparser REQUIRED CONFIG COMPONENTS shared)
@ -761,7 +771,7 @@ endif()
set(install_extra_targets)
if(DOWNLOAD_DEPENDENCIES)
get_property(sdl3_type TARGET SDL3::SDL3 PROPERTY TYPE)
if(sdl3_type STREQUAL "SHARED_LIBRARY")
if(sdl3_type STREQUAL "SHARED_LIBRARY" AND NOT ANDROID)
list(APPEND install_extra_targets "SDL3-shared")
endif()
endif()

View File

@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
}
def androidProject = projectDir.parentFile
android {
namespace "org.legoisland.isle"
@ -15,16 +16,14 @@ android {
externalNativeBuild {
cmake {
arguments '-DANDROID_STL=c++_shared', "-DFETCHCONTENT_BASE_DIR=${projectDir.parentFile}/build/_deps"
arguments '-DANDROID_STL=c++_shared', "-DFETCHCONTENT_BASE_DIR=${androidProject}/build/_deps"
if (project.hasProperty('cmakeArgs')) {
project.cmakeArgs.split(" ").each {arg -> arguments arg}
}
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
// abiFilters 'x86_64', 'arm64-v8a'
}
}
}
externalNativeBuild {
cmake {
version "3.30.5"
@ -41,7 +40,6 @@ android {
storePassword = System.getenv("SIGNING_STORE_PASSWORD")
}
}
buildTypes {
release {
minifyEnabled true
@ -68,20 +66,36 @@ afterEvaluate {
}
}
def aarDest = file("${projectDir}/libs/SDL3.aar")
tasks.register('downloadSDL3', Exec) {
workingDir = androidProject
commandLine 'cmake', '-P', 'downloadSDL3.cmake'
}
tasks.register('compileSDL3AndroidArchive', Exec) {
def rootDir = projectDir.parentFile
def sdl3Dir = "build/_deps/sdl3-src"
def cmakeCommand = "cmake -P downloadSDL3.cmake"
def pythonCommand = "python3 ${sdl3Dir}/build-scripts/build-release.py " +
"--actions android " +
"--fast " +
"--force " +
"--root='${sdl3Dir}'"
def mvCommand = "mv ${sdl3Dir}/build-android/SDL3-*.aar ${projectDir}/libs/SDL3.aar"
workingDir = androidProject
dependsOn(downloadSDL3)
commandLine 'sh', '-c', "cd ${rootDir} && ${cmakeCommand} && ${pythonCommand} && mkdir -p ${projectDir}/libs/ && ${mvCommand}"
def sdl3Dir = "build/_deps/sdl3-src"
commandLine 'python', "${sdl3Dir}/build-scripts/build-release.py",
'--actions', 'android',
'--fast', '--force',
"--root=${sdl3Dir}"
doLast {
def aarFile = file("${androidProject}/${sdl3Dir}/build-android").listFiles().find {
it.name.endsWith(".aar")
}
aarDest.parentFile.mkdirs()
aarFile.renameTo(aarDest)
}
}
tasks.register('ensureSDL3') {
// if DOWNLOAD_DEPENDENCIES=OFF download the version appropriate
// 'devel android zip' and place the .aar at `aarDest`
if (aarDest.exists()) { return false }
dependsOn(compileSDL3AndroidArchive)
}
dependencies {
implementation files('libs/SDL3.aar').builtBy(compileSDL3AndroidArchive)
implementation files('libs/SDL3.aar').builtBy(ensureSDL3)
}