Skip to content

Commit 92a6f5f

Browse files
committed
Addons CMake for includes/libs and src
1 parent b5a4d97 commit 92a6f5f

File tree

2 files changed

+156
-33
lines changed

2 files changed

+156
-33
lines changed

scripts/templates/android/ofApp/build.gradle

Lines changed: 122 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,148 @@ tasks.register("prepareKotlinBuildScriptModel"){
2525
def projectRoot = rootProject.projectDir.absolutePath
2626
def addonFile = new File(projectRoot, "addons.make")
2727

28+
println "projectRoot = ${projectRoot}"
29+
println "addonFile = ${addonFile.absolutePath}"
30+
31+
2832
// Get all addon paths from addons.make
33+
34+
def ofRoot = "/../../../"
35+
2936
def addonPaths = []
3037
if (addonFile.exists()) {
3138
addonFile.eachLine { line ->
32-
def addonPath = ofRoot + "addons/" + line
33-
if (new File(addonPath).exists()) {
39+
def addonRelPath = ofRoot + "/addons/" + line.trim()
40+
def addonPath = new File(projectRoot, addonRelPath).canonicalFile
41+
println "Resolved addon path: ${addonPath}"
42+
if (addonPath.exists()) {
3443
addonPaths.add(addonPath)
44+
} else {
45+
println "Missing addon path: ${addonPath}"
3546
}
3647
}
3748
}
3849

50+
println "Checking path: ${addonPaths}"
51+
52+
3953
def addonIncludeDirs = []
54+
def addonSourceFiles = []
4055
def addonLibs = []
4156

4257
// Collect include directories and libraries for each addon
4358
addonPaths.each { addon ->
59+
// Add addon/src
4460
def includePath = new File(addon, "src")
4561
if (includePath.exists()) {
4662
addonIncludeDirs.add(includePath.absolutePath)
63+
includePath.eachFileRecurse { file ->
64+
if (file.name.endsWith(".cpp") || file.name.endsWith(".c")) {
65+
println "Found C/CPP source: ${file}"
66+
addonSourceFiles.add(file.absolutePath)
67+
}
68+
}
69+
def javaDirs = [
70+
new File(addon, "java"),
71+
new File(addon, "kotlin"),
72+
new File(addon, "srcJava"),
73+
new File(addon, "src")
74+
]
75+
javaDirs.each { dir ->
76+
if (dir.exists()) {
77+
dir.eachFileRecurse { file ->
78+
if (file.name.endsWith(".java") || file.name.endsWith(".kt")) {
79+
println "Found Java/Kotlin source: ${file}"
80+
addonSourceFiles.add(file.absolutePath)
81+
}
82+
}
83+
}
84+
}
85+
}
86+
// Add libs/*/include/
87+
def libsDir = new File(addon, "libs")
88+
if (libsDir.exists()) {
89+
libsDir.eachDir { subLib ->
90+
def includeDir = new File(subLib, "include").canonicalFile
91+
if (includeDir.exists()) {
92+
println "Found sublib include: ${includeDir}"
93+
addonIncludeDirs.add(includeDir.absolutePath)
94+
}
95+
}
4796
}
4897
}
49-
5098
// Define supported Android ABIs
5199
def supportedAbis = ["arm64-v8a", "armeabi-v7a", "x86_64"]
100+
def currentAbis = []
101+
if (android.defaultConfig.ndk.abiFilters != null && !android.defaultConfig.ndk.abiFilters.isEmpty()) {
102+
currentAbis = android.defaultConfig.ndk.abiFilters.toList()
103+
} else {
104+
currentAbis = supportedAbis
105+
}
106+
println "abiFilters type: ${android.defaultConfig.ndk.abiFilters?.getClass()}"
107+
println "abiFilters value: ${android.defaultConfig.ndk.abiFilters}"
108+
println "currentAbis value: ${currentAbis}"
52109

110+
def addonLibMap = [:].withDefault { [] }
111+
112+
supportedAbis.each { abi -> addonLibMap[abi] = [] }
53113
addonPaths.each { addon ->
54-
supportedAbis.each { abi ->
55-
def libPath = new File(addon, "libs/android/${abi}")
56-
if (libPath.exists()) {
57-
def libFiles = libPath.listFiles()?.findAll { it.name.endsWith(".so") || it.name.endsWith(".a") }
58-
if (libFiles) {
59-
addonLibs.addAll(libFiles*.absolutePath)
114+
def libsDir = new File(addon, "libs")
115+
if (libsDir.exists() && libsDir.isDirectory()) {
116+
libsDir.eachDir { subLib ->
117+
def libRoot = new File(subLib, "lib/android")
118+
supportedAbis.each { abi ->
119+
def archDir = new File(libRoot, abi).canonicalFile
120+
if (archDir.exists() && archDir.isDirectory()) {
121+
def libFiles = archDir.listFiles()?.findAll {
122+
it.name.endsWith(".so") || it.name.endsWith(".a")
123+
}
124+
if (libFiles) {
125+
addonLibMap[abi].addAll(libFiles*.absolutePath)
126+
}
127+
}
60128
}
61129
}
62130
}
63131
}
64-
println "Addon Libraries found: ${addonLibs}"
132+
133+
addonPaths.each { addon ->
134+
def libsDir = new File(addon, "libs")
135+
if (libsDir.exists() && libsDir.isDirectory()) {
136+
libsDir.eachDir { subLib ->
137+
def libRoot = new File(subLib, "lib/android")
138+
supportedAbis.each { abi ->
139+
def archDir = new File(libRoot, abi).canonicalFile
140+
println "Checking: ${archDir}"
141+
if (archDir.exists() && archDir.isDirectory()) {
142+
def libFiles = archDir.listFiles()?.findAll {
143+
it.name.endsWith(".so") || it.name.endsWith(".a")
144+
}
145+
if (libFiles && !libFiles.isEmpty()) {
146+
println "Found ${libFiles.size()} libraries in ${archDir}:"
147+
libFiles.each { lib -> println " - ${lib.absolutePath}" }
148+
addonLibs.addAll(libFiles*.absolutePath)
149+
} else {
150+
println "No libs found in ${archDir}"
151+
}
152+
}
153+
}
154+
}
155+
} else {
156+
println "No 'libs/' folder in ${addon.name}"
157+
}
158+
}
159+
println "Addon Libraries found: [${addonLibs}]"
65160

66161
// Convert lists to CMake arguments
67-
def cmakeIncludeArgs = "-DADDON_INCLUDE_DIRS=" + addonIncludeDirs.join(";")
68-
def cmakeLibArgs = "-DADDON_LIBS=" + addonLibs.join(";")
162+
def cmakeIncludeArgs = "-DADDON_INCLUDE_DIRS=\"" + addonIncludeDirs.join(";") + "\""
163+
def cmakeLibArgs = "-DADDON_LIBS=\"" + addonLibs.join(";") + "\""
164+
def cmakeSrcArgs = "-DADDON_SOURCES=" + addonSourceFiles.join(";")
69165

70166
println "Found addons: " + addonPaths
71167
println "Include directories: " + addonIncludeDirs
72-
println "Libraries: " + addonLibs
168+
169+
73170

74171
android {
75172
compileSdkVersion 34
@@ -91,30 +188,27 @@ android {
91188
}
92189
}
93190
defaultConfig {
94-
applicationId "cc.openframeworks.emptyExample" // IMPORTANT : THIS DEFINES THE ID OF THE APK
191+
applicationId "cc.openframeworks.androidAssimpExample" // IMPORTANT : THIS DEFINES THE ID OF THE APK
95192
minSdkVersion 24
96193
targetSdkVersion 34
97194
versionCode 12
98195
versionName '12.0'
99196
ndk.abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
100197

198+
def androidSTL = "-DANDROID_STL=c++_shared"
199+
if (project.hasProperty("OF_STATIC") && project.OF_STATIC.toBoolean()) {
200+
androidSTL "-DANDROID_STL=c++_static"
201+
}
202+
101203
externalNativeBuild {
102204
cmake {
103205
arguments "-DANDROID_TOOLCHAIN=clang",
104-
"-DTARGET_OPENGLES=TRUE"
105-
106-
// Use c++_static if building OpenFrameworks as a STATIC library
107-
if (project.hasProperty("OF_STATIC") && project.OF_STATIC.toBoolean()) {
108-
arguments "-DANDROID_STL=c++_static"
109-
} else {
110-
arguments "-DANDROID_STL=c++_shared"
111-
}
112-
113-
// Enable NEON only for ARM architectures
114-
if (android.defaultConfig.ndk.abiFilters.contains("armeabi-v7a") || android.defaultConfig.ndk.abiFilters.contains("arm64-v8a")) {
115-
arguments "-DANDROID_ARM_NEON=TRUE"
116-
}
117-
206+
"-DTARGET_OPENGLES=TRUE",
207+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
208+
cmakeIncludeArgs,
209+
cmakeLibArgs,
210+
cmakeSrcArgs,
211+
androidSTL
118212
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
119213
version '3.22.1'
120214
}
@@ -173,7 +267,6 @@ android {
173267
cmake {
174268
path "${CMAKELIST_PATH}/CMakeLists.txt"
175269
}
176-
177270
}
178271
compileOptions {
179272
sourceCompatibility JavaVersion.VERSION_1_8

scripts/templates/android/ofApp/src/CMakeLists.txt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ set(CMAKE_CXX_STANDARD 23)
5050
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5151
set(CMAKE_CXX_EXTENSIONS OFF)
5252
set(TARGET_ANDROID ON)
53-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ferror-limit=0 -std=c17 -Oz -Wall -fno-short-enums -fPIE -fPIC -fexceptions -ffunction-sections -fdata-sections")
54-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ferror-limit=0 -std=c++23 -Oz -stdlib=libc++ -Wall -fno-short-enums -fPIE -fPIC -fexceptions -ffunction-sections -fdata-sections")
53+
add_definitions(-DANDROID)
54+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ferror-limit=0 -std=c17 -O3 -Wall -fno-short-enums -fPIE -fPIC -ffunction-sections -fdata-sections")
55+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ferror-limit=0 -std=c++23 -O3 -stdlib=libc++ -Wall -fno-short-enums -fPIE -fPIC -fexceptions -ffunction-sections -fdata-sections")
5556
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-export-dynamic")
5657
print_all_variables()
5758
set(OF_LIBRARY_TYPE "SHARED") # or "STATIC"
@@ -110,16 +111,45 @@ include_directories(
110111
)
111112

112113
# Load add-on include directories from Gradle
114+
115+
if(DEFINED ADDON_SOURCES)
116+
message(STATUS "Adding addon sources: ${ADDON_SOURCES}")
117+
foreach(SRC ${ADDON_SOURCES})
118+
message(STATUS " - ${SRC}")
119+
list(APPEND ADDON_SRC_LIST ${SRC})
120+
endforeach()
121+
target_sources(${PROJECT_NAME} PRIVATE ${ADDON_SRC_LIST})
122+
endif()
123+
124+
125+
if(NOT DEFINED ADDON_INCLUDE_DIRS AND DEFINED ENV{ADDON_INCLUDE_DIRS})
126+
set(ADDON_INCLUDE_DIRS $ENV{ADDON_INCLUDE_DIRS})
127+
message(STATUS "ADDON_INCLUDE_DIRS not defined, falling back to ENV: ${ADDON_INCLUDE_DIRS}")
128+
endif()
129+
113130
if(DEFINED ADDON_INCLUDE_DIRS)
131+
message(STATUS "Adding addon include dirs: ${ADDON_INCLUDE_DIRS}")
114132
foreach(DIR ${ADDON_INCLUDE_DIRS})
133+
message(STATUS "Adding addon include DIR: ${DIR}")
115134
include_directories(${DIR})
116135
endforeach()
117136
endif()
137+
138+
if(NOT DEFINED ADDON_LIBS AND DEFINED ENV{ADDON_LIBS})
139+
set(ADDON_LIBS $ENV{ADDON_LIBS})
140+
message(STATUS "ADDON_LIBS not defined, falling back to ENV: ${ADDON_LIBS}")
141+
endif()
142+
118143
if(DEFINED ADDON_LIBS)
119-
message(STATUS "Adding addon libraries: ${ADDON_LIBS}")
144+
message(STATUS "Original ADDON_LIBS: ${ADDON_LIBS}")
145+
set(FILTERED_ADDON_LIBS "")
120146
foreach(LIB ${ADDON_LIBS})
121-
target_link_libraries(${PROJECT_NAME} ${LIB})
147+
if("${LIB}" MATCHES "${ANDROID_ABI}")
148+
list(APPEND FILTERED_ADDON_LIBS ${LIB})
149+
endif()
122150
endforeach()
151+
message(STATUS "Filtered ADDON_LIBS for ABI '${ANDROID_ABI}': ${FILTERED_ADDON_LIBS}")
152+
target_link_libraries(${PROJECT_NAME} ${FILTERED_ADDON_LIBS})
123153
endif()
124154

125155
find_library(ANDROID_LIB NAMES android)

0 commit comments

Comments
 (0)