-
Notifications
You must be signed in to change notification settings - Fork 104
/
zproject_java.gsl
1549 lines (1300 loc) · 52.1 KB
/
zproject_java.gsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Generate minimal JNI language bindings.
#
# These are not meant to be idiomatic, but to provide a minimal platform
# of JNI function bindings on which to base idiomatic Java classes.
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/zeromq/gsl for details.
#
# Copyright (c) the Contributors as noted in the AUTHORS file.
# This file is part of zproject.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
register_target ("java", "Java JNI binding")
# Target provides name space isolation for its functions
function target_java
gsl from "zproject_java_lib.gsl"
.macro generate_wrapper
.output "$(topdir)/$(project.prefix:c)-jni/CMakeLists.txt"
$(project.GENERATED_WARNING_HEADER:)
cmake_minimum_required (VERSION 2.8)
project ($(project.linkname)jni CXX)
enable_language (C)
# Search for Find*.cmake files in the following locations
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../..")
########################################################################
# JNI dependency
########################################################################
find_package (JNI REQUIRED)
include_directories (${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} src/native/include)
.for use where use.optional = 0
########################################################################
# $(USE.PROJECT) dependency
########################################################################
find_package($(use.project) REQUIRED)
IF ($(USE.PROJECT)_FOUND)
include_directories(${$(use.project)_INCLUDE_DIRS})
list(APPEND MORE_LIBRARIES ${$(use.project)_LIBRARIES})
ELSE ($(USE.PROJECT)_FOUND)
message( FATAL_ERROR "$(use.project) not found." )
ENDIF ($(USE.PROJECT)_FOUND)
.endfor
########################################################################
# $(PROJECT.PREFIX) dependency
########################################################################
find_package($(project.prefix) REQUIRED)
IF ($(PROJECT.PREFIX)_FOUND)
include_directories(${$(project.prefix)_INCLUDE_DIRS})
list(APPEND MORE_LIBRARIES ${$(project.prefix)_LIBRARIES})
ELSE ($(PROJECT.PREFIX)_FOUND)
message( FATAL_ERROR "$(project.prefix) not found." )
ENDIF ($(PROJECT.PREFIX)_FOUND)
set ($(project.linkname)jni_sources
.for project.class where class.okay
src/main/c/$(namespace:c)_$(class.name:pascal).c
.endfor
)
add_library ($(project.linkname)jni SHARED ${$(project.linkname)jni_sources})
add_definitions (-D$(PROJECT.PREFIX)_BUILD_DRAFT_API)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -O2")
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
target_link_libraries ($(project.linkname)jni ${MORE_LIBRARIES})
.
.output "$(topdir)/$(project.prefix:c)-jni/Find$(project.prefix:c).cmake"
$(project.GENERATED_WARNING_HEADER:)
if (NOT MSVC)
include(FindPkgConfig)
pkg_check_modules(PC_$(PROJECT.NAME) "$(project.libname)")
if (NOT PC_$(PROJECT.NAME)_FOUND)
pkg_check_modules(PC_$(PROJECT.NAME) "$(project.libname)")
endif (NOT PC_$(PROJECT.NAME)_FOUND)
if (PC_$(PROJECT.NAME)_FOUND)
# some libraries install the headers is a subdirectory of the include dir
# returned by pkg-config, so use a wildcard match to improve chances of finding
# headers and SOs.
set(PC_$(PROJECT.NAME)_INCLUDE_HINTS ${PC_$(PROJECT.NAME)_INCLUDE_DIRS} ${PC_$(PROJECT.NAME)_INCLUDE_DIRS}/*)
set(PC_$(PROJECT.NAME)_LIBRARY_HINTS ${PC_$(PROJECT.NAME)_LIBRARY_DIRS} ${PC_$(PROJECT.NAME)_LIBRARY_DIRS}/*)
endif(PC_$(PROJECT.NAME)_FOUND)
endif (NOT MSVC)
find_path (
${CMAKE_FIND_PACKAGE_NAME}_INCLUDE_DIRS
NAMES $(project.header)
HINTS ${PC_$(PROJECT.NAME)_INCLUDE_HINTS}
)
find_library (
${CMAKE_FIND_PACKAGE_NAME}_LIBRARIES
NAMES $(project.linkname)
HINTS ${PC_$(PROJECT.NAME)_LIBRARY_HINTS}
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
${CMAKE_FIND_PACKAGE_NAME}
REQUIRED_VARS ${CMAKE_FIND_PACKAGE_NAME}_LIBRARIES ${CMAKE_FIND_PACKAGE_NAME}_INCLUDE_DIRS
)
mark_as_advanced(
${CMAKE_FIND_PACKAGE_NAME}_FOUND
${CMAKE_FIND_PACKAGE_NAME}_LIBRARIES ${CMAKE_FIND_PACKAGE_NAME}_INCLUDE_DIRS
)
$(project.GENERATED_WARNING_HEADER:)
.
.output "$(topdir)/build.gradle"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
buildscript {
configurations.configureEach {
resolutionStrategy {
force 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
}
exclude group: 'xerces', module: 'xercesImpl'
}
}
plugins {
id 'java'
id 'maven-publish'
id 'com.jfrog.artifactory' version '5.2.3'
id 'com.jfrog.bintray' version '1.8.5'
id 'com.google.osdetector' version '1.7.3'
}
wrapper.gradleVersion = '8.9'
subprojects {
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'com.google.osdetector'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
group = '$(project.namespace)'
if (project.hasProperty('isRelease')) {
version = '$(->version.major).$(->version.minor).$(->version.patch)'
} else {
version = '$(->version.major).$(->version.minor).$(->version.patch)-SNAPSHOT'
}
}
artifactory {
contextUrl = "https://oss.jfrog.org/artifactory" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = 'oss-snapshot-local'
username = System.getenv('ARTIFACTORY_USERNAME')
password = System.getenv('ARTIFACTORY_PASSWORD')
}
}
}
bintrayPublish.enabled = false
.
.output "$(topdir)/$(project.prefix:c)-jni/build.gradle"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
ext.jni_dependencies_version = project.hasProperty('isRelease') ? 'latest.release' : 'latest.integration'
ext.hasNotEmptyProperty = { propertyName ->
return project.hasProperty(propertyName) ? project[propertyName]?.trim() : false
}
dependencies {
. for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
implementation "org.zeromq.$(use.project):$(use.project)-jni:$jni_dependencies_version"
. endif
. endfor
implementation 'org.scijava:native-lib-loader:2.5.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
}
// ------------------------------------------------------------------
// Build section
tasks.register('generateJniHeaders', Exec) {
dependsOn 'classes'
def classpath = sourceSets.main.output.classesDirs
def appclasspath = configurations.runtimeClasspath.files*.getAbsolutePath().join(File.pathSeparator)
def nativeIncludes = 'src/native/include'
def jniClasses = [
. for project.class where class.okay
'src/main/java/$(name_path)/$(name:pascal).java'$(last ()?? ''? ',')
. endfor
]
def utilityClasses = [
'src/main/java/org/zeromq/tools/ZmqNativeLoader.java'
]
commandLine("javac", "-h", "$nativeIncludes", "-classpath", "$classpath${File.pathSeparator}$appclasspath", *jniClasses, *utilityClasses)
}
tasks.withType(Test).configureEach {
def defaultJavaLibraryPath = System.getProperty("java.library.path")
if (osdetector.os == 'windows') {
def extraJavaLibraryPath = hasNotEmptyProperty('buildPrefix') ? "$project.buildPrefix\\\\bin;$project.buildPrefix\\\\lib" : ''
extraJavaLibraryPath = extraJavaLibraryPath.replace("/", "\\\\")
systemProperty "java.library.path", "${projectDir}\\\\build\\\\Release${File.pathSeparator}" +
"${extraJavaLibraryPath}${File.pathSeparator}" +
"${defaultJavaLibraryPath}"
} else {
def extraJavaLibraryPath = hasNotEmptyProperty('buildPrefix') ? "$project.buildPrefix/lib" : ''
systemProperty "java.library.path", "${projectDir}/build${File.pathSeparator}" +
"/usr/local/lib${File.pathSeparator}" +
"/tmp/lib${File.pathSeparator}" +
"${extraJavaLibraryPath}${File.pathSeparator}" +
"${defaultJavaLibraryPath}"
}
}
tasks.register('initCMake', Exec) {
dependsOn 'generateJniHeaders'
workingDir 'build'
def prefixPath = hasNotEmptyProperty('buildPrefix') ? "-DCMAKE_PREFIX_PATH=$project.buildPrefix" : ''
commandLine 'cmake', "$prefixPath", '..'
}
tasks.register('buildNative', Exec) {
dependsOn 'initCMake'
if (osdetector.os == 'windows') {
commandLine 'cmake',
'--build', 'build',
'--config', 'Release',
'--target', '$(project.linkname)jni',
'--', '-verbosity:Minimal', '-maxcpucount'
} else {
commandLine 'cmake',
'--build', 'build'
}
}
jar.dependsOn buildNative
test.dependsOn buildNative
// ------------------------------------------------------------------
// Install and Publish section
tasks.register('sourcesJar', Jar) {
dependsOn 'classes'
archiveClassifier = 'sources'
from sourceSets.main.allSource
}
tasks.register('javadocJar', Jar) {
dependsOn 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
artifactId = '$(project.name:c)-jni'
pom {
name = '$(project.name:c)-jni'
description = '$(project.description:no)'
packaging = 'jar'
url = '$(project.url)'
licenses {
license {
name = 'Mozilla Public License Version 2.0'
url = 'https://www.mozilla.org/en-US/MPL/2.0/'
}
}
scm {
connection = '$(project.url).git'
developerConnection = '$(project.url).git'
url = '$(project.url)'
}
}
}
}
}
artifactoryPublish {
publications ('mavenJava')
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publications = ['mavenJava']
publish = true
override = true
pkg {
repo = 'maven'
name = '$(project.name:c)-jni'
desc = '$(project.description:no)'
userOrg = System.getenv('BINTRAY_USER_ORG')
licenses = ['MPL-2.0']
websiteUrl = '$(project.url)'
issueTrackerUrl = '$(project.url)/issues'
vcsUrl = '$(project.url).git'
githubRepo = System.getenv('BINTRAY_USER_ORG') + '/$(project.name:c)'
version {
name = project.version
vcsTag= project.version
}
}
}
// ------------------------------------------------------------------
// Cleanup section
clean.doFirst {
delete 'CMakeFiles', 'msvc'
delete fileTree(projectDir) {
include '*.so'
include '*.dylib'
include 'cmake_install.cmake'
include 'Makefile'
include 'CMakeCache.txt'
}
}
.
.directory.create ("$(topdir)/$(project.prefix:c)-jni-all")
.output "$(topdir)/$(project.prefix:c)-jni-all/build.gradle"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
dependencies {
implementation project(':$(project.prefix:c)-jni')
runtimeOnly "$(project.namespace):$(project.prefix:c)-jni-linux-x86_64:${project.version}"
runtimeOnly "$(project.namespace):$(project.prefix:c)-jni-osx-x86_64:${project.version}"
runtimeOnly "$(project.namespace):$(project.prefix:c)-jni-windows-x86_64:${project.version}"
. for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
implementation 'org.zeromq.$(use.project):$(use.project)-jni:latest.release'
runtimeOnly 'org.zeromq.$(use.project):$(use.project)-jni-all:latest.release'
. endif
. endfor
}
// ------------------------------------------------------------------
// Install and Publish section
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifactId = '$(project.name:c)-jni-all'
pom {
name = '$(project.name:c)-jni-all'
description = '$(project.description:no)'
packaging = 'jar'
url = '$(project.url)'
licenses {
license {
name = 'Mozilla Public License Version 2.0'
url = 'https://www.mozilla.org/en-US/MPL/2.0/'
}
}
scm {
connection = '$(project.url).git'
developerConnection = '$(project.url).git'
url = '$(project.url)'
}
}
}
}
}
artifactoryPublish {
publications ('mavenJava')
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publications = ['mavenJava']
publish = true
override = true
pkg {
repo = 'maven'
name = '$(project.name:c)-jni-all'
desc = '$(project.description:no)'
userOrg = System.getenv('BINTRAY_USER_ORG')
licenses = ['MPL-2.0']
websiteUrl = '$(project.url)'
issueTrackerUrl = '$(project.url)/issues'
vcsUrl = '$(project.url).git'
githubRepo = System.getenv('BINTRAY_USER_ORG') + '/$(project.name:c)'
version {
name = project.version
vcsTag= project.version
}
}
}
.
.directory.create ("$(topdir)/$(project.prefix:c)-jni-native")
.output "$(topdir)/$(project.prefix:c)-jni-native/build.gradle"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
dependencies {
implementation project(':$(project.prefix:c)-jni')
. for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
runtimeOnly "org.zeromq.$(use.project):$(use.project)-jni-${osdetector.classifier}:latest.release"
. endif
. endfor
}
// ------------------------------------------------------------------
// Build section
tasks.register('copyLibs', Copy) {
def libraryPaths = []
if (project.hasProperty('buildPrefix')) {
if (osdetector.os == 'windows') {
// DLLs are installed to the bin directory by cmake
libraryPaths.add("${project.buildPrefix}/bin")
}
libraryPaths.add("${project.buildPrefix}/lib")
}
def javaLibraryPaths = System.getProperty('java.library.path').split(File.pathSeparator).toList()
libraryPaths.addAll(javaLibraryPaths)
libraryPaths.add('/usr/local/lib')
if (osdetector.os == 'windows') {
libraryPaths.add("${rootDir}/$(project.prefix:c)-jni/build/Release")
} else {
libraryPaths.add("${rootDir}/$(project.prefix:c)-jni/build")
}
def oldStrategy = duplicatesStrategy
duplicatesStrategy = DuplicatesStrategy.WARN
libraryPaths.each { path ->
from path
include '$(project.libname)jni.so'
include '$(project.libname)jni.dylib'
include '*$(project.linkname)jni*.dll'
include '$(project.libname).so'
include '$(project.libname).dylib'
include '*$(project.linkname)*.dll'
. for project.use
include '$(use.libname).so'
include '$(use.libname).dylib'
include '*$(use.linkname)*.dll'
. endfor
into 'build/natives'
}
duplicatesStrategy = oldStrategy
}
jar.archiveBaseName = "$(project.prefix:c)-jni-${osdetector.classifier}"
jar.dependsOn copyLibs
jar {
def arch = osdetector.arch.contains('64') ? '64' : '32'
from 'build/natives'
include '*'
into "natives/${osdetector.os}_${arch}"
}
// ------------------------------------------------------------------
// Install and Publish section
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifactId = "$(project.name:c)-jni-${osdetector.classifier}"
pom {
name = "$(project.name:c)-jni-${osdetector.classifier}"
description = '$(project.description:no)'
packaging = 'jar'
url = '$(project.url)'
licenses {
license {
name = 'Mozilla Public License Version 2.0'
url = 'https://www.mozilla.org/en-US/MPL/2.0/'
}
}
scm {
connection = '$(project.url).git'
developerConnection = '$(project.url).git'
url = '$(project.url)'
}
}
}
}
}
artifactoryPublish {
publications ('mavenJava')
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publications = ['mavenJava']
publish = true
override = true
pkg {
repo = 'maven'
name = "$(project.name:c)-jni-${osdetector.classifier}"
desc = '$(project.description:no)'
userOrg = System.getenv('BINTRAY_USER_ORG')
licenses = ['MPL-2.0']
websiteUrl = '$(project.url)'
issueTrackerUrl = '$(project.url)/issues'
vcsUrl = '$(project.url).git'
githubRepo = System.getenv('BINTRAY_USER_ORG') + '/$(project.name:c)'
version {
name = project.version
vcsTag= project.version
}
}
}
// ------------------------------------------------------------------
// Cleanup section
clean.doFirst {
delete fileTree(projectDir) {
include '*.so'
include '*.dylib'
}
}
.
.output "$(topdir)/README.md"
# $(project.prefix)-jni
JNI Binding for $(project.name:)
## Preamble
As stated in LIBZMQ documentation, Android build systems are still DRAFT.
[ZActor & ZLoop](https://github.com/zeromq/czmq/issues/2214) are not (yet ?) supported.
It's also probably the case for a few other features.
This being said, CZMQ can already be used for Android.
## Prerequisites
GRADLE need to be installed on your system.
Note also that GRADLE requires CMake 3.6. For old distributions, this
may mean an upgrade of CMake. This can do done from sources and is rather
easy to rebuild/install though (tested on CentOS 7, Fedora 24, ...)
## Building the JNI Layer for Linux and OSX
Before you start make sure $(project.name:) is built and installed on your system.
Next, ensure you have gradle and cmake installed, then run:
gradle build jar
gradle test
If you don't like to install gradle beforehand simply use the gradle wrapper.
./gradlew build jar
./gradlew test
This does the following:
* It calls javah to build the headers in src/native/include
* It compiles the C and Java classes
* It creates a jar file and a shareable native library
If libraries of dependent projects are not installed in any of the default locations of your OS use parameter `buildPrefix` to point to their location, e.g.:
./gradlew build jar -PbuildPrefix=/tmp/jni_build
## Building the JNI Layer for Android
### Manual build
Before you start make sure that you've built the JNI Layer for your current OS.
Please read the preamble section of the [README](../../builds/android/README.md) in the android build directory.
You only need to set the environment variables.
Then in the jni's android directory ($(project.prefix)-jni/android), run:
export XXX=xxx
export YYY=yyy
cd <$(project.name:c)>/bindings/jni/$(project.prefix)-jni/android
./build.sh [ arm | arm64 | x86 | x86_64 ]
This does the following:
* It compiles the $(project.name:) C sources for Android, into a native library $(project.libname).so in /tmp/android_build/<architecture>/lib
* It compiles the JNI Java classes into a jar file $(project.prefix)-jni-$(->version.major).$(->version.minor).$(->version.patch).jar in bindings/jni/$(project.prefix)-jni/build/libs
* It compiles the JNI C sources for Android, into a native library $(project.libname)jni.so.
.for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
* It takes $(use.project)-jni-*.jar, which must already be built in ../$(use.project)/bindings/jni/$(use.prefix)-jni/build/libs/
. endif
.endfor
* It combines all these into jar file for the built architecture, which you can use in your Android projects.
* It merges the jar files built for the different architectures into one jar file.
### More automated build mecanism
You may also use `bindings/jni/ci_build.sh`:
export XXX=xxx
export YYY=yyy
./ci_build.sh
Basically, this script builds the whole for JAVA and but also for Android,
but generated libraries are available in a different place:
* bindings/jni/.deps # all required dependencies
* bindings/jni/.build/ # all generated native libraries
* bindings/jni/.build/prefix # all generated android libraries
If you have your own `prebuilt` Android libraries, place them under
* bindings/jni/.build/prefix/{arm,arm6,x86,x86_64}/lib/.
They will be automatically packed in generated JAR files.
### Compatibility
This build system is tested on a few recent distributions:
* CentOS 7 (see [PREREQUISITES](#prerequisites)
* Fedora (24 to 37 and see [PREREQUISITES](#prerequisites)
* Rocky Linux (8 & 9)
* Debian (9 to 11)
* Ubuntu (16, 18, 20 & 22.04)
Both build systems (`build.sh` and `ci_build.sh`) are tested with NDK 19 to 25,
with current default of `android-ndk-25`.
### Configuration
Both come with many different configuration possibilities.
Again, refer to [builds/android/README](../../builds/android/README.md) for details.
## Building the JNI Layer for Windows
Prerequisites:
* MS Visual Studio or MS Visual Studio Tools 2010 or later are installed
* Java JDK 8 or later is installed
Environment Variables:
* Add MSBuild.exe to the PATH (e.g. `C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\MSBuild\\Current\\Bin`)
* Set JAVA_HOME to the installation location (e.g. `C:\\Program Files\\Java\\jdk1.8.0_66`).
1. Check out all dependent projects from github, at the same level as this project (e.g. libzmq, czmq).
2. Follow the dependent projects instructions to build their `.dll` and `.lib` file.
If you used cmake to install the dependencies you can skip the following steps.
3. Create a folder where to place the dlls and libs (e.g. `C:\\tmp\\deps`).
4. Copy all dependent `.dll` files to the `bin` subfolder (e.g. `C:\\tmp\\deps\\bin`)
5. Copy all dependent `.lib` files to the `lib` subfolder (e.g. `C:\\tmp\\deps\\lib`)
6. Copy all dependent `.h` files to the `include` subfolder (e.g. `C:\\tmp\deps\\include`)
Now run:
gradlew build jar -PbuildPrefix=C:\\tmp\\deps
gradlew test -PbuildPrefix=C:\\tmp\\deps
## Installing the JNI Layer
If you like to use this JNI Layer in another project you'll need to distribute it
to a location where the other project can locate it. The easiest way to do this
is by leveraging maven and install to the local maven repository located at
$HOME/.m2. Therefore simply run:
./gradlew publishToMavenLocal
By default the JNI Layer builds SNAPSHOT versions (e.g. 1.1.3-SNAPSHOT). If you
like to build a release version you need the set the release switch:
./gradlew publishToMavenLocal -PisRelease
## Using the JNI API
- to be written.
## License
$(project->license.:)
## Information for maintainers
BINTRAY is no more accepting PUBLISH. Hence, this chapter has to be reviewed.
See [CZMQ issue #2249](https://github.com/zeromq/czmq/issues/2249) and probably a few others.
### Create or update the gradle wrapper
The gradle wrapper is a tool that allows to use gradle on multiple platforms
without installing it beforehand. Make sure you have installed a version of
gradle that is at least the version the wrapper should have (local version >= wrapper version).
Then just run
gradle wrapper
Now commit all generated files to the project. Yes the jar file as well! Users
will now be able to call the gradle wrapper (gradlew) which will install gradle
for them.
### Travis build
Travis can build and check this jni layer there add the following line to your
travis environment matrix
- BUILD_TYPE=bindings BINDING=jni
### Deploy to bintray with Travis CI
When tagging a release travis can automatically deploy this jni layer to bintray.
Therefore you'll need to supply travis with three environment variables:
* BINTRAY_USER - your personal user name
* BINTRAY_KEY - your personal api key
* BINTRAY_USER_ORG - the organisation you like to publish to
You may extent .travis.yml as follows
- BUILD_TYPE=bindings BINDING=jni BINTRAY_USER=<user> BINTRAY_KEY=<key> BINTRAY_USER_ORG=<org>
But I recommend to encrypt your bintray api key. This can be done with the
travis commandline client
travis encrypt BINTRAY_KEY=123...
Please be aware that secure environmental variables can only be added as global.
global:
- secure: "ZMvDhR..."
matrix:
- BUILD_TYPE=bindings BINDING=jni BINTRAY_USER=<user> BINTRAY_USER_ORG=<org>
.
.output "$(topdir)/settings.gradle"
rootProject.name = '$(project.prefix)-jni'
include '$(project.prefix)-jni'
include '$(project.prefix)-jni-native'
include '$(project.prefix)-jni-all'
.
.output "$(topdir)/.gitignore"
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
\.gradle
build/
src/native
gradle-app.setting
$(project.libname)jni.so
*.class
.
.directory.create ("$(topdir)/$(project.prefix:c)-jni/android")
.terminator="\n"
.output "$(topdir)/$(project.prefix:c)-jni/android/build.sh"
#!/bin/bash
$(project.GENERATED_WARNING_HEADER:)
# Build JNI interface for Android
#
# Requires these environment variables be set, e.g.:
#
# NDK_VERSION=$(project.android_ndk_version)
#
# Exit if any step fails
set -e
# Use directory of current script as the working directory
cd "\$( dirname "${BASH_SOURCE[0]}" )"
PROJECT_ROOT="\$(cd ../../../.. && pwd)"
# Configuration
export NDK_VERSION="${NDK_VERSION:-$(project.android_ndk_version)}"
export ANDROID_NDK_ROOT="${ANDROID_NDK_ROOT:-/tmp/${NDK_VERSION}}"
export MIN_SDK_VERSION=${MIN_SDK_VERSION:-$(project.android_min_sdk_version)}
export ANDROID_BUILD_DIR="${ANDROID_BUILD_DIR:-/tmp/android_build}"
export ANDROID_DEPENDENCIES_DIR="${ANDROID_DEPENDENCIES_DIR:-/tmp/tmp-deps}"
export CI_CONFIG_QUIET="${CI_CONFIG_QUIET:-yes}"
export CI_TIME="${CI_TIME:-}"
export CI_TRACE="${CI_TRACE:-no}"
########################################################################
# Utilities
########################################################################
# Get access to android_build functions and variables
# Perform some sanity checks and calculate some variables.
source "${PROJECT_ROOT}/builds/android/android_build_helper.sh"
function usage {
echo "$(PROJECT.NAME) - Usage:"
echo " export XXX=xxx"
echo " ./build.sh [ arm | arm64 | x86 | x86_64 ]"
echo ""
echo "See this file (configuration & tuning options) for details"
echo "on variables XXX and their values xxx"
exit 1
}
########################################################################
# Sanity checks
########################################################################
BUILD_ARCH="$1"
[ -z "${BUILD_ARCH}" ] && usage
# Export android build's environment variables for cmake
android_build_set_env "${BUILD_ARCH}"
android_download_ndk
case "$CI_TIME" in
[Yy][Ee][Ss]|[Oo][Nn]|[Tt][Rr][Uu][Ee])
CI_TIME="time -p " ;;
[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee])
CI_TIME="" ;;
esac
case "$CI_TRACE" in
[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee])
set +x ;;
[Yy][Ee][Ss]|[Oo][Nn]|[Tt][Rr][Uu][Ee])
set -x
MAKE_OPTIONS=VERBOSE=1
;;
esac
########################################################################
# Compilation
########################################################################
GRADLEW_OPTS=()
GRADLEW_OPTS+=("-PbuildPrefix=$BUILD_PREFIX")
GRADLEW_OPTS+=("--info")
# Build any dependent libraries
# Use a default value assuming that dependent libraries sit alongside this one
.for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
( cd ${$(USE.PROJECT)_ROOT}/bindings/jni/$(use.prefix)-jni/android; ./build.sh $BUILD_ARCH )
. endif
.endfor
# Ensure we've built dependencies for Android
android_build_trace "Building Android native libraries"
( cd ../../../../builds/android && ./build.sh $BUILD_ARCH )
# Ensure we've built JNI interface
android_build_trace "Building JNI interface & classes"
( cd ../.. && TERM=dumb ./gradlew build jar ${GRADLEW_OPTS[@]} ${$(PROJECT.PREFIX)_GRADLEW_OPTS} )
android_build_trace "Building JNI for Android"
rm -rf build && mkdir build && cd build
(
VERBOSE=1 \\
cmake \\
-DANDROID_ABI=$TOOLCHAIN_ABI \\
-DANDROID_PLATFORM=$MIN_SDK_VERSION \\
-DANDROID_STL=c++_shared \\
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake \\
-DCMAKE_FIND_ROOT_PATH=$ANDROID_BUILD_PREFIX \\
..
)
# CMake wrongly searches current directory and then toolchain path instead
# of lib path for these files, so make them available temporarily
ln -s $ANDROID_SYS_ROOT/usr/lib/crtend_so.o
ln -s $ANDROID_SYS_ROOT/usr/lib/crtbegin_so.o
make $MAKE_OPTIONS
android_build_trace "Building jar for $TOOLCHAIN_ABI"
# Copy class files into org/zeromq/etc.
find ../../build/libs/ -type f -name '$(project.prefix)-jni-*.jar' ! -name '*javadoc.jar' ! -name '*sources.jar' -exec unzip -q {} +
.for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
unzip -qo "${$(USE.PROJECT)_ROOT}/bindings/jni/$(use.project)-jni/android/$(use.project)-android*$TOOLCHAIN_ABI*.jar"
. endif
.endfor
# Copy native libraries into lib/$TOOLCHAIN_ABI
mkdir -p lib/$TOOLCHAIN_ABI
cp $(project.libname)jni.so lib/$TOOLCHAIN_ABI
cp $ANDROID_BUILD_PREFIX/lib/*.so lib/$TOOLCHAIN_ABI
cp ${ANDROID_STL_ROOT}/${ANDROID_STL} lib/$TOOLCHAIN_ABI
# Build android jar
zip -r -m ../$(project.prefix)-android-$TOOLCHAIN_ABI-$(->version.major).$(->version.minor).$(->version.patch).jar lib/ org/ META-INF/
cd ..
rm -rf build
android_build_trace "Merging ABI jars"
mkdir build && cd build
# Copy contents from all ABI jar - overwriting class files and manifest
unzip -qo '../$(project.prefix)-android-*$(->version.major).$(->version.minor).$(->version.patch).jar'
# Build merged jar
zip -r -m ../$(project.prefix)-android-$(->version.major).$(->version.minor).$(->version.patch).jar lib/ org/ META-INF/
cd ..
rm -rf build
android_build_trace "Android JNI build successful"
$(project.GENERATED_WARNING_HEADER:)
.chmod_x ("$(topdir)/$(project.prefix:c)-jni/android/build.sh")
.
.output "$(topdir)/$(project.prefix:c)-jni/android/CMakeLists.txt"
$(project.GENERATED_WARNING_HEADER:)
cmake_minimum_required (VERSION 3.6)
project ($(project.name:c)jni CXX)
enable_language (C)
# Search for Find*.cmake files in the following locations
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../..")
.for use where use.optional = 0
########################################################################
# $(USE.PROJECT) dependency
########################################################################
find_package($(use.project) REQUIRED)
IF ($(USE.PROJECT)_FOUND)
include_directories(${$(use.project)_INCLUDE_DIRS})
list(APPEND MORE_LIBRARIES ${$(use.project)_LIBRARIES})
ELSE ($(USE.PROJECT)_FOUND)
message( FATAL_ERROR "$(use.project) not found." )
ENDIF ($(USE.PROJECT)_FOUND)
.endfor
########################################################################
# $(PROJECT.PREFIX) dependency
########################################################################
find_package($(project.prefix) REQUIRED)
IF ($(PROJECT.PREFIX)_FOUND)
include_directories(${$(project.prefix)_INCLUDE_DIRS})
list(APPEND MORE_LIBRARIES ${$(project.prefix)_LIBRARIES})
ELSE ($(PROJECT.PREFIX)_FOUND)
message( FATAL_ERROR "$(project.prefix) not found." )
ENDIF ($(PROJECT.PREFIX)_FOUND)
include_directories(../src/native/include)
set ($(project.linkname)jni_sources
.for project.class where class.okay
../src/main/c/$(namespace:c)_$(class.name:pascal).c
.endfor