Skip to content

Commit 9211fca

Browse files
committed
Make the JNI build system work everywhere
1 parent bed08e5 commit 9211fca

7 files changed

Lines changed: 2175 additions & 173 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ core
2020
# JNI libs
2121
/realm/src/main/jniLibs
2222

23-
# Dynamic Libraries
23+
# Build artifacts
2424
*.so
25+
*.d
26+
*.o
2527

2628
# Backup files
2729
*.bak

local.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
# For customization when using a Version Control System, please read the
99
# header note.
1010
#Fri Aug 29 09:25:34 CEST 2014
11-
ndk.dir=/opt/android-ndk-r10
12-
sdk.dir=/opt/android-sdk-linux
13-
#sdk.dir=/usr/local/Cellar/android-sdk/22.6.2
14-
#ndk.dir=/usr/local/Cellar/android-ndk/r9c
11+
#ndk.dir=/opt/android-ndk-r10
12+
#sdk.dir=/opt/android-sdk-linux
13+
sdk.dir=/usr/local/Cellar/android-sdk/22.6.2
14+
ndk.dir=/usr/local/Cellar/android-ndk/r9c
1515

realm-jni/build.gradle

Lines changed: 115 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
apply plugin: 'cpp'
21
apply plugin: 'download-task'
32

43
Properties localProperties = new Properties()
@@ -7,111 +6,6 @@ localProperties.entrySet().each() { entry ->
76
project.ext[entry.getKey()] = localProperties.setProperty(entry.getKey(), entry.getValue())
87
}
98

10-
model {
11-
platforms {
12-
androidArm {
13-
operatingSystem "linux"
14-
architecture "arm"
15-
}
16-
androidArmv7a {
17-
operatingSystem "linux"
18-
architecture "arm"
19-
}
20-
/*
21-
androidMips {
22-
operatingSystem "linux"
23-
}
24-
androidIntel {
25-
operatingSystem "linux"
26-
architecture "x86"
27-
}
28-
*/
29-
}
30-
toolChains {
31-
androidArm(Gcc) {
32-
def prefix = "arm-linux-androideabi-"
33-
cCompiler.executable = prefix + cCompiler.executable
34-
cppCompiler.executable = prefix + cppCompiler.executable
35-
assembler.executable = prefix + assembler.executable
36-
linker.executable = prefix + linker.executable
37-
staticLibArchiver.executable = prefix + staticLibArchiver.executable
38-
path "${buildDir}/standalone-toolchains/arm/bin"
39-
addPlatformConfiguration(new AndroidArmPlatformConfiguration())
40-
}
41-
/*
42-
androidMips(Gcc) {
43-
def prefix = "mipsel-linux-android-"
44-
cCompiler.executable = prefix + cCompiler.executable
45-
cppCompiler.executable = prefix + cppCompiler.executable
46-
assembler.executable = prefix + assembler.executable
47-
linker.executable = prefix + linker.executable
48-
staticLibArchiver.executable = prefix + staticLibArchiver.executable
49-
path "${rootDir}/standalone-toolchains/mips/bin"
50-
addPlatformConfiguration(new AndroidMipsPlatformConfiguration())
51-
}
52-
androidIntel(Gcc) {
53-
def prefix = "i686-linux-android-"
54-
cCompiler.executable = prefix + cCompiler.executable
55-
cppCompiler.executable = prefix + cppCompiler.executable
56-
assembler.executable = prefix + assembler.executable
57-
linker.executable = prefix + linker.executable
58-
staticLibArchiver.executable = prefix + staticLibArchiver.executable
59-
path "${rootDir}/standalone-toolchains/x86/bin"
60-
addPlatformConfiguration(new AndroidIntelPlatformConfiguration())
61-
}
62-
*/
63-
}
64-
repositories {
65-
libs(PrebuiltLibraries) {
66-
tightdb {
67-
headers.srcDirs "${rootDir}/core/include", "${rootDir}/core/include/tightdb"
68-
binaries.withType(StaticLibraryBinary) {
69-
if (targetPlatform == platforms.androidArm) {
70-
staticLibraryFile = file("${rootDir}/core/libtightdb-android-arm.a")
71-
} else if (targetPlatform == platforms.androidArmv7a) {
72-
staticLibraryFile = file("${rootDir}/core/libtightdb-android-arm-v7a.a")
73-
} else if (targetPlatform == platforms.androidMips) {
74-
staticLibraryFile = file("${rootDir}/core/libtightdb-android-mips.a")
75-
} else if (targetPlatform == platforms.androidIntel) {
76-
staticLibraryFile = file("${rootDir}/core/libtightdb-android-x86.a")
77-
}
78-
}
79-
}
80-
}
81-
}
82-
}
83-
84-
libraries {
85-
jni {
86-
binaries.withType(SharedLibraryBinary) {
87-
cppCompiler.args '-Os'
88-
cppCompiler.args "-I${rootDir}/core/include"
89-
if (targetPlatform == platforms.androidArm) {
90-
cppCompiler.args '-mthumb'
91-
} else if (targetPlatform == platforms.androidArmv7a) {
92-
cppCompiler.args '-mthumb'
93-
cppCompiler.args '-march=armv7-a'
94-
cppCompiler.args '-mfloat-abi=softfp'
95-
cppCompiler.args '-mfpu=vfpv3-d16'
96-
}
97-
cppCompiler.define 'TIGHTDB_HAVE_CONFIG'
98-
cppCompiler.define 'PIC'
99-
lib library: 'tightdb', linkage: 'static'
100-
}
101-
}
102-
}
103-
104-
sources {
105-
jni {
106-
cpp {
107-
source {
108-
srcDir 'src'
109-
include '*.cpp'
110-
}
111-
}
112-
}
113-
}
114-
1159
task checkProperties(group: 'check', description: 'Check the user provided gradle.properties') << {
11610
if (!project.ext['ndk.dir']) {
11711
throw new GradleException('The ndkDir property in the gradle.properties file is not set.')
@@ -136,92 +30,145 @@ task downloadCore(group: 'build setup', description: 'Download the latest versio
13630
}
13731
}
13832

139-
for (platform in ['arm'/*, 'mips', 'x86'*/]) {
33+
for (platform in ['arm', 'mips', 'x86']) {
14034
task "generateNdkToolchain${platform.capitalize()}"(type: Exec) {
14135
group 'build setup'
14236
description "Generate the NDK standalone toolchain for the ${platform.capitalize()} platform"
14337
dependsOn { checkProperties }
14438
outputs.dir new File("${buildDir}/standalone-toolchains/${platform}")
14539
commandLine = [
146-
"bash",
147-
"${project.ext['ndk.dir']}/build/tools/make-standalone-toolchain.sh",
148-
"--platform=android-${platform.contains('arm')?8:9}",
149-
"--install-dir=${buildDir}/standalone-toolchains/${platform}",
150-
"--arch=${platform}"
40+
"bash",
41+
"${project.ext['ndk.dir']}/build/tools/make-standalone-toolchain.sh",
42+
"--platform=android-${platform.contains('arm')?8:9}",
43+
"--install-dir=${buildDir}/standalone-toolchains/${platform}",
44+
"--arch=${platform}"
15145
]
15246
}
15347
}
15448

155-
task generateAllNdkToolchains {
156-
group 'build setup'
157-
description 'Generate the NDK standalone toolchains for all the supported frameworks'
158-
dependsOn tasks.findAll { task -> task.name.contains('generateNdkToolchain') }
159-
}
160-
161-
class AndroidArmPlatformConfiguration implements TargetPlatformConfiguration {
162-
boolean supportsPlatform(Platform targetPlatform) {
163-
return targetPlatform.name.contains('androidArm') &&
164-
targetPlatform.operatingSystem.name.equals('linux') &&
165-
targetPlatform.architecture.name.equals('arm')
49+
task buildAndroidJniArm {
50+
group 'build'
51+
description 'Build the Android JNI shared library for the Arm platform'
52+
dependsOn generateNdkToolchainArm
53+
dependsOn downloadCore
54+
doLast {
55+
exec {
56+
environment PATH: "${buildDir}/standalone-toolchains/arm/arm-linux-androideabi/bin:${System.env.PATH}"
57+
environment CC: 'gcc'
58+
environment TIGHTDB_ANDROID: '1'
59+
commandLine = [
60+
'make',
61+
'-C', "${projectDir}/src",
62+
'CC_IS=gcc',
63+
"TIGHTDB_CFLAGS=-DTIGHTDB_HAVE_CONFIG -DPIC -I${rootDir}/core/include",
64+
'CFLAGS_ARCH=-mthumb',
65+
'BASE_DENOM=arm',
66+
"TIGHTDB_LDFLAGS=-ltightdb-android-arm -L${rootDir}/core",
67+
'LIB_SUFFIX_SHARED=.so',
68+
'libtightdb-jni-arm.so'
69+
]
70+
}
71+
copy {
72+
from "${projectDir}/src/libtightdb-jni-arm.so"
73+
into "${projectDir}/../realm/src/main/jniLibs/armeabi"
74+
rename 'libtightdb-jni-arm.so', 'libtightdb-jni.so'
75+
}
16676
}
167-
List<String> getCppCompilerArgs() { [] }
168-
List<String> getCCompilerArgs() { [] }
169-
List<String> getObjectiveCCompilerArgs() { [] }
170-
List<String> getObjectiveCppCompilerArgs() { [] }
171-
List<String> getAssemblerArgs() { [] }
172-
List<String> getLinkerArgs() { [] }
173-
List<String> getStaticLibraryArchiverArgs() { [] }
17477
}
17578

176-
class AndroidIntelPlatformConfiguration implements TargetPlatformConfiguration {
177-
boolean supportsPlatform(Platform targetPlatform) {
178-
return targetPlatform.name.contains('androidIntel') &&
179-
targetPlatform.operatingSystem.name.equals('linux') &&
180-
targetPlatform.architecture.name.equals('x86')
79+
task buildAndroidJniArmv7a {
80+
group 'build'
81+
description 'Build the Android JNI shared library for the Arm-v7a platform'
82+
dependsOn generateNdkToolchainArm
83+
dependsOn downloadCore
84+
doLast {
85+
exec {
86+
environment PATH: "${buildDir}/standalone-toolchains/arm/arm-linux-androideabi/bin:${System.env.PATH}"
87+
environment CC: 'gcc'
88+
environment TIGHTDB_ANDROID: '1'
89+
commandLine = [
90+
'make',
91+
'-C', "${projectDir}/src",
92+
'CC_IS=gcc',
93+
"TIGHTDB_CFLAGS=-DTIGHTDB_HAVE_CONFIG -DPIC -I${rootDir}/core/include",
94+
'CFLAGS_ARCH=-mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16',
95+
'BASE_DENOM=arm-v7a',
96+
"TIGHTDB_LDFLAGS=-ltightdb-android-arm-v7a -L${rootDir}/core",
97+
'LIB_SUFFIX_SHARED=.so',
98+
'libtightdb-jni-arm-v7a.so'
99+
]
100+
}
101+
copy {
102+
from "${projectDir}/src/libtightdb-jni-arm-v7a.so"
103+
into "${projectDir}/../realm/src/main/jniLibs/armeabi-v7a"
104+
rename 'libtightdb-jni-arm-v7a.so', 'libtightdb-jni.so'
105+
}
181106
}
182-
List<String> getCppCompilerArgs() { [] }
183-
List<String> getCCompilerArgs() { [] }
184-
List<String> getObjectiveCCompilerArgs() { [] }
185-
List<String> getObjectiveCppCompilerArgs() { [] }
186-
List<String> getAssemblerArgs() { [] }
187-
List<String> getLinkerArgs() { [] }
188-
List<String> getStaticLibraryArchiverArgs() { [] }
189107
}
190108

191-
class AndroidMipsPlatformConfiguration implements TargetPlatformConfiguration {
192-
boolean supportsPlatform(Platform targetPlatform) {
193-
return targetPlatform.name.contains('androidMips') &&
194-
targetPlatform.operatingSystem.name.equals('linux') &&
195-
!targetPlatform.architecture.name.equals('arm')
196-
!targetPlatform.architecture.name.equals('x86')
109+
task buildAndroidJniMips {
110+
group 'build'
111+
description 'Build the Android JNI shared library for the Mips platform'
112+
dependsOn generateNdkToolchainMips
113+
dependsOn downloadCore
114+
doLast {
115+
exec {
116+
environment PATH: "${buildDir}/standalone-toolchains/mips/mipsel-linux-android/bin:${System.env.PATH}"
117+
environment CC: 'gcc'
118+
environment TIGHTDB_ANDROID: '1'
119+
commandLine = [
120+
'make',
121+
'-C', "${projectDir}/src",
122+
'CC_IS=gcc',
123+
"TIGHTDB_CFLAGS=-DTIGHTDB_HAVE_CONFIG -DPIC -I${rootDir}/core/include",
124+
'CFLAGS_ARCH=',
125+
'BASE_DENOM=mips',
126+
"TIGHTDB_LDFLAGS=-ltightdb-android-mips -L${rootDir}/core",
127+
'LIB_SUFFIX_SHARED=.so',
128+
'libtightdb-jni-mips.so'
129+
]
130+
}
131+
copy {
132+
from "${projectDir}/src/libtightdb-jni-mips.so"
133+
into "${projectDir}/../realm/src/main/jniLibs/mips"
134+
rename 'libtightdb-jni-mips.so', 'libtightdb-jni.so'
135+
}
197136
}
198-
List<String> getCppCompilerArgs() { [] }
199-
List<String> getCCompilerArgs() { [] }
200-
List<String> getObjectiveCCompilerArgs() { [] }
201-
List<String> getObjectiveCppCompilerArgs() { [] }
202-
List<String> getAssemblerArgs() { [] }
203-
List<String> getLinkerArgs() { [] }
204-
List<String> getStaticLibraryArchiverArgs() { [] }
205137
}
206138

207-
task buildAndroidJni(group: 'build', description: 'Build the Android JNI shared library for all the supported platforms') {
208-
dependsOn binaries.withType(SharedLibraryBinary)
139+
task buildAndroidJniIntel {
140+
group 'build'
141+
description 'Build the Android JNI shared library for the Intel platform'
142+
dependsOn generateNdkToolchainX86
143+
dependsOn downloadCore
209144
doLast {
210-
copy {
211-
from "${projectDir}/build/binaries/jniSharedLibrary/androidArm/libjni.so"
212-
into "${projectDir}/../realm/src/main/jniLibs/armeabi/"
213-
rename 'libjni.so', 'libtightdb-jni.so'
145+
exec {
146+
environment PATH: "${buildDir}/standalone-toolchains/x86/i686-linux-android/bin:${System.env.PATH}"
147+
environment CC: 'gcc'
148+
environment TIGHTDB_ANDROID: '1'
149+
commandLine = [
150+
'make',
151+
'-C', "${projectDir}/src",
152+
'CC_IS=gcc',
153+
"TIGHTDB_CFLAGS=-DTIGHTDB_HAVE_CONFIG -DPIC -I${rootDir}/core/include",
154+
'CFLAGS_ARCH=',
155+
'BASE_DENOM=x86',
156+
"TIGHTDB_LDFLAGS=-ltightdb-android-x86 -L${rootDir}/core",
157+
'LIB_SUFFIX_SHARED=.so',
158+
'libtightdb-jni-x86.so'
159+
]
214160
}
215161
copy {
216-
from "${projectDir}/build/binaries/jniSharedLibrary/androidArmv7a/libjni.so"
217-
into "${projectDir}/../realm/src/main/jniLibs/armeabi-v7a/"
218-
rename 'libjni.so', 'libtightdb-jni.so'
162+
from "${projectDir}/src/libtightdb-jni-x86.so"
163+
into "${projectDir}/../realm/src/main/jniLibs/x86"
164+
rename 'libtightdb-jni-x86.so', 'libtightdb-jni.so'
219165
}
220166
}
221167
}
222168

223-
def jniTasks = tasks.findAll { task -> task.name.contains('jni') }
224-
jniTasks.each() { task ->
225-
task.dependsOn downloadCore
226-
task.dependsOn generateAllNdkToolchains
169+
task buildAndroidJni(group: 'build', description: 'Build the Android JNI shared library for all the supported platforms') {
170+
dependsOn buildAndroidJniArm
171+
dependsOn buildAndroidJniArmv7a
172+
dependsOn buildAndroidJniMips
173+
dependsOn buildAndroidJniIntel
227174
}

0 commit comments

Comments
 (0)