Skip to content

Commit d82acd3

Browse files
committed
Merge pull request realm#1704 from realm/mc/debug-build-for-jni
More options for jni build.gradle
2 parents 993a9cd + e925ca0 commit d82acd3

2 files changed

Lines changed: 70 additions & 21 deletions

File tree

realm/realm-jni/build.gradle

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ import java.security.MessageDigest
33
ext.coreVersion = '0.94.4'
44
// empty or comment out this to disable hash checking
55
ext.coreSha256Hash = 'feeae46e5ebdb7ce912b3f111b6a044c4dd0ef7b7eac9b313f8c7a40664d9fae'
6-
ext.forceDownloadCore = false
7-
ext.clang = false // gcc is default for the NDK. It also produces smaller binaries
6+
ext.forceDownloadCore =
7+
project.hasProperty('forceDownloadCore') ? project.getProperty('forceDownloadCore').toBoolean() : false
8+
// gcc is default for the NDK. It also produces smaller binaries
9+
ext.clang = project.hasProperty('clang') ? project.getProperty('clang').toBoolean() : false
10+
// Build with debug symbols
11+
ext.debugBuild = project.hasProperty('debugBuild') ? project.getProperty('debugBuild').toBoolean() : false
12+
// Strip the symbols from the so file or not. If debugBuild is true, this one will be always false.
13+
ext.stripSymbols = project.hasProperty('stripSymbols') ? project.getProperty('stripSymbols').toBoolean() : true
14+
// Set the core source code path. By setting this, the core will be built from source. And coreVersion will be read from
15+
// core source code.
16+
ext.coreSourcePath = project.hasProperty('coreSourcePath') ? project.getProperty('coreSourcePath') : null
817

918
def commonCflags = [ '-std=c++11', '-ffunction-sections', '-fdata-sections', '-flto' ]
1019

@@ -75,11 +84,13 @@ buildscript {
7584

7685
apply plugin: 'de.undercouch.download'
7786

78-
if (project.clang instanceof String) {
79-
ext.clang = Boolean.parseBoolean(project.clang)
87+
if (ext.debugBuild) {
88+
// Debug build should never strip symbols
89+
ext.stripSymbols = false
8090
}
81-
if (project.forceDownloadCore instanceof String) {
82-
ext.forceDownloadCore = Boolean.parseBoolean(project.forceDownloadCore)
91+
if (ext.coreSourcePath) {
92+
// Run the "sh build.sh get-version" to get the core version.
93+
ext.coreVersion = "sh build.sh get-version".execute([], file(coreSourcePath)).text.trim()
8394
}
8495

8596
def getNdk() {
@@ -100,6 +111,14 @@ def getNdk() {
100111
return ndkDir
101112
}
102113

114+
def getStrippedExt() {
115+
return stripSymbols ? "-stripped" : ""
116+
}
117+
118+
def getDebugExt() {
119+
return debugBuild ? "-dbg" : ""
120+
}
121+
103122
ext.coreArchiveFile = rootProject.file("../core-android-${project.coreVersion}.tar.gz")
104123
ext.coreDir = file("${buildDir}/core-${project.coreVersion}")
105124

@@ -163,11 +182,40 @@ task downloadCore(group: 'build setup', description: 'Download the latest versio
163182
}
164183
}
165184

185+
task compileCore(group: 'build setup', description: 'Compile the core library from source code') {
186+
// Build the library from core source code
187+
doFirst {
188+
if (!coreSourcePath) {
189+
throw new GradleException('The coreSourcePath is not set.')
190+
}
191+
exec {
192+
workingDir = coreSourcePath
193+
commandLine = [
194+
"bash",
195+
"build.sh",
196+
"build-android"
197+
]
198+
}
199+
}
200+
201+
// Copy the core tar ball
202+
doLast {
203+
copy {
204+
from "${coreSourcePath}/realm-core-android-${coreVersion}.tar.gz"
205+
into project.coreArchiveFile.parent
206+
rename "realm-core-android-${coreVersion}.tar.gz", "core-android-${coreVersion}.tar.gz"
207+
}
208+
}
209+
}
210+
166211
task deployCore(group: 'build setup', description: 'Deploy the latest version of realm core') {
167-
dependsOn { downloadCore }
212+
dependsOn {
213+
coreSourcePath ? compileCore : downloadCore
214+
}
168215

169216
outputs.upToDateWhen {
170-
if (coreDownloaded) {
217+
// Clean up the coreDir if it is newly downloaded or compiled from source
218+
if (coreDownloaded || coreSourcePath) {
171219
return false
172220
}
173221

@@ -232,27 +280,27 @@ targets.each { target ->
232280
"-l${Runtime.getRuntime().availableProcessors()}",
233281
'-C', "${projectDir}/src",
234282
"CC_IS=${clang?'clang':'gcc'}",
235-
"REALM_CFLAGS=-Wno-variadic-macros -DREALM_HAVE_CONFIG -DPIC -I${project.coreDir}/include",
283+
"REALM_CFLAGS_COMMON=-Wno-variadic-macros -DREALM_HAVE_CONFIG -DPIC -I${project.coreDir}/include",
236284
"CFLAGS_ARCH=${(commonCflags + target.cflags).join(' ')}",
237285
"BASE_DENOM=${target.name}",
238-
"REALM_LDFLAGS=-lrealm-android-${target.name} -lstdc++ -lsupc++ -llog -L${project.coreDir} -Wl,--gc-sections -flto",
286+
"REALM_LDFLAGS_COMMON=-lrealm-android-${target.name} -lstdc++ -lsupc++ -llog -L${project.coreDir} -Wl,--gc-sections -flto",
239287
'LIB_SUFFIX_SHARED=.so',
240-
"librealm-jni-${target.name}.so"
288+
"librealm-jni-${target.name}${getDebugExt()}.so"
241289
]
242290
}
243291

244292
task "copyAndroidJni${target.name.capitalize()}"(dependsOn: "buildAndroidJni${target.name.capitalize()}") << {
245293
copy {
246-
from "${projectDir}/src/librealm-jni-${target.name}-stripped.so"
294+
from "${projectDir}/src/librealm-jni-${target.name}${getDebugExt()}${getStrippedExt()}.so"
247295
into "${projectDir}/../realm-library/src/main/jniLibs/${target.jniFolder}"
248-
rename "librealm-jni-${target.name}-stripped.so", 'librealm-jni.so'
296+
rename "librealm-jni-${target.name}${getDebugExt()}${getStrippedExt()}.so", 'librealm-jni.so'
249297
}
250298

251299
// Store the unstripped version
252300
copy {
253-
from "${projectDir}/src/librealm-jni-${target.name}.so"
301+
from "${projectDir}/src/librealm-jni-${target.name}${getDebugExt()}.so"
254302
into "${projectDir}/../build/output/jniLibs-unstripped/${target.jniFolder}"
255-
rename "librealm-jni-${target.name}.so", 'librealm-jni.so'
303+
rename "librealm-jni-${target.name}${getDebugExt()}.so", 'librealm-jni.so'
256304
}
257305
}
258306
}
@@ -268,6 +316,7 @@ task clean(type: Delete) {
268316

269317
delete fileTree(dir: "${projectDir}/../realm-library/src/main/jniLibs/", include: '**/librealm-jni*.so')
270318
delete fileTree(dir: "${projectDir}/../build/output/jniLibs-unstripped/", include: '**/librealm-jni*.so')
319+
delete fileTree(dir: "${projectDir}/src/", include: '**/librealm-jni*-stripped.so')
271320

272321
doLast {
273322
targets.each { target ->

realm/realm-jni/project.mk

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ else
5656
CFLAGS_OPTIM = -Os -DNDEBUG
5757
endif
5858

59-
PROJECT_CFLAGS_OPTIM += $(REALM_CFLAGS)
60-
PROJECT_CFLAGS_DEBUG += $(REALM_CFLAGS_DBG)
61-
PROJECT_CFLAGS_COVER += $(REALM_CFLAGS_DBG)
62-
PROJECT_LDFLAGS_OPTIM += $(REALM_LDFLAGS)
63-
PROJECT_LDFLAGS_DEBUG += $(REALM_LDFLAGS_DBG)
64-
PROJECT_LDFLAGS_COVER += $(REALM_LDFLAGS_DBG)
59+
PROJECT_CFLAGS_OPTIM += ${REALM_CFLAGS_COMMON} $(REALM_CFLAGS)
60+
PROJECT_CFLAGS_DEBUG += ${REALM_CFLAGS_COMMON} $(REALM_CFLAGS_DBG)
61+
PROJECT_CFLAGS_COVER += ${REALM_CFLAGS_COMMON} $(REALM_CFLAGS_DBG)
62+
PROJECT_LDFLAGS_OPTIM += $(REALM_LDFLAGS_COMMON) $(REALM_LDFLAGS)
63+
PROJECT_LDFLAGS_DEBUG += $(REALM_LDFLAGS_COMMON) $(REALM_LDFLAGS_DBG)
64+
PROJECT_LDFLAGS_COVER += $(REALM_LDFLAGS_COMMON) $(REALM_LDFLAGS_DBG)

0 commit comments

Comments
 (0)