forked from zeromq/zproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zproject_java.gsl
957 lines (813 loc) · 30.4 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
# 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/imatix/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)/CMakeLists.txt"
$(project.GENERATED_WARNING_HEADER:)
cmake_minimum_required (VERSION 2.8)
project ($(project.prefix)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.prefix)jni_sources
.for project.class where class.okay
src/main/c/$(namespace:c)_$(class.name:pascal).c
.endfor
)
add_library ($(project.prefix)jni SHARED ${$(project.prefix)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.prefix)jni ${MORE_LIBRARIES})
.
.output "$(topdir)/Find$(project.name: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 (
$(PROJECT.NAME)_INCLUDE_DIRS
NAMES $(project.header)
HINTS ${PC_$(PROJECT.NAME)_INCLUDE_HINTS}
)
find_library (
$(PROJECT.NAME)_LIBRARIES
NAMES $(project.prefix)
HINTS ${PC_$(PROJECT.NAME)_LIBRARY_HINTS}
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
$(PROJECT.NAME)
REQUIRED_VARS $(PROJECT.NAME)_LIBRARIES $(PROJECT.NAME)_INCLUDE_DIRS
)
mark_as_advanced(
$(PROJECT.NAME)_FOUND
$(PROJECT.NAME)_LIBRARIES $(PROJECT.NAME)_INCLUDE_DIRS
)
$(project.GENERATED_WARNING_HEADER:)
.
.output "$(topdir)/build.gradle"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
plugins {
id 'java'
id 'maven-publish'
id "com.jfrog.bintray" version "1.7.3"
}
group = "org.zeromq"
version = "$(->version.major).$(->version.minor).$(->version.patch)"
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
. for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
compile 'org.zeromq:$(use.project)-jni:+'
. endif
. endfor
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-all:1.3'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.13'
}
// ------------------------------------------------------------------
// Build section
task generateJniHeaders(type: Exec, dependsOn: 'classes') {
def classpath = sourceSets.main.output.classesDir
def appclasspath = configurations.runtime.files*.getAbsolutePath().join(":")
def nativeIncludes = "src/native/include"
def jniClasses = [
. for project.class where class.okay
'$(namespace).$(name:pascal)'$(last ()?? ""? ",")
. endfor
]
commandLine("javah", "-d", "$nativeIncludes", "-classpath", "$classpath:$appclasspath", *jniClasses)
}
tasks.withType(Test) {
systemProperty "java.library.path", "/usr/lib:/usr/local/lib:$projectDir"
}
task initCMake(type: Exec, dependsOn: 'generateJniHeaders') {
commandLine "cmake", "."
}
task buildNative(type: Exec, dependsOn: 'initCMake') {
commandLine "make"
}
jar.dependsOn buildNative
test.dependsOn buildNative
// ------------------------------------------------------------------
// Install and Publish section
task sourcesJar(type: Jar, dependsOn: 'classes') {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: 'javadoc') {
classifier = 'javadoc'
from javadoc.destinationDir
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom.withXml {
asNode().appendNode('packaging', 'jar')
asNode().appendNode('name', '$(project.name:c)-jni')
asNode().appendNode('description', '$(project.description:no)')
asNode().appendNode('url', '$(project.url)')
def license = asNode().appendNode('licenses').appendNode('license')
license.appendNode('name', 'Mozilla Public License Version 2.0')
license.appendNode('url', 'https://www.mozilla.org/en-US/MPL/2.0/')
def scm = asNode().appendNode('scm')
scm.appendNode('connection', '$(project.url).git')
scm.appendNode('developerConnection', '$(project.url).git')
scm.appendNode('url', '$(project.url)')
}
}
}
}
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 = '$(->version.major).$(->version.minor).$(->version.patch)'
vcsTag= '$(->version.major).$(->version.minor).$(->version.patch)'
}
}
}
// ------------------------------------------------------------------
// Cleanup section
clean.doFirst {
delete "${rootDir}/CMakeCache.txt"
delete "${rootDir}/libczmqjni.so"
}
.
.output "$(topdir)/README.md"
# $(project.prefix)-jni
[ ![Download](https://api.bintray.com/packages/zeromq/maven/$(project.prefix)-jni/images/download.svg) ](https://bintray.com/zeromq/maven/$(project.prefix)-jni/_latestVersion)
JNI Binding for $(project.name:)
## Building the JNI Layer for Linux
Ensure you have gradle and cmake installed, then run:
gradle build jar
gradle test
If you don't like to install gradle beforehand just use the gradle wrapper.
./gradlew build jar
./gradlew test
This calls javah to build the headers in src/native/include, and then compiles the C and Java pieces to create a jar file a sharable library (.so).
## Installing the JNI Layer for Linux
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
## Building the JNI Layer for Android
See bindings/jni/android/build.sh.
You need the Android Native Development Kit (NDK) installed.
Set these environment variables, e.g:
ANDROID_NDK_ROOT=$HOME/android-ndk-r11c
TOOLCHAIN_VERSION=4.9
TOOLCHAIN_HOST=arm-linux-androideabi
TOOLCHAIN_NAME=$TOOLCHAIN_HOST-$TOOLCHAIN_VERSION
TOOLCHAIN_ARCH=arm
TOOLCHAIN_PATH=$ANDROID_NDK_ROOT/toolchains/$TOOLCHAIN_NAME/prebuilt/linux-x86_64/bin
Then in the android directory, run:
./build.sh
This does the following:
* It compiles the $(project.name:) C sources for Android, into a native library $(project.libname).so in builds/android/
* It compiles the JNI Java classes into a jar file $(project.prefix)-jni-$(->version.major).$(->version.minor).$(->version.patch).jar in bindings/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/build/libs/
. endif
.endfor
* It combines all these into $(project.prefix)-android.jar, which you can use in your Android projects.
## Building the JNI Layer for Windows
You need MS Visual Studio 2010 or later.
You need the Java SDK. Set the JAVA_HOME environment 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. In each project, open a console in builds/msvc/vs2010 and run the build.bat batch file.
3. In this project, open a console in bindings/jni/msvc/vs2010 and run the build.bat batch file.
The resulting libraries ($(project.prefix)jni.dll, $(project.prefix)jni.lib) are created in bindings/jni/msvc/bin.
## Using the JNI API
- to be written.
## License
$(project->license.:)
## Information for maintainers
### Building 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
### Travis deploy to bintray
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'
.
.output "$(topdir)/.gitignore"
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
\.gradle
build/
src/native
gradle-app.setting
$(project.libname)jni.so
.
.directory.create ("$(topdir)/android")
.terminator="\n"
.output "$(topdir)/android/build.sh"
#!/bin/bash
$(project.GENERATED_WARNING_HEADER:)
# Build JNI interface for Android
# Targets Android API level 8, ARM architecture (see below)
#
# Requires these environment variables be set, e.g.:
#
# ANDROID_NDK_ROOT=$HOME/android-ndk-r11c
# TOOLCHAIN_NAME=arm-linux-androideabi-4.9
# TOOLCHAIN_VERSION=4.9
# TOOLCHAIN_HOST=arm-linux-androideabi
# TOOLCHAIN_ARCH=arm
# TOOLCHAIN_PATH=$ANDROID_NDK_ROOT/toolchains/$TOOLCHAIN_NAME/prebuilt/linux-x86_64/bin
#
# Exit if any step fails
set -e
set -x
export ANDROID_API_LEVEL=android-8
export ANDROID_SYS_ROOT=$ANDROID_NDK_ROOT/platforms/$ANDROID_API_LEVEL/arch-$TOOLCHAIN_ARCH
if [ "$1" = "-d" ]; then
MAKE_OPTIONS=VERBOSE=1
fi
source ../../../builds/android/android_build_helper.sh
android_build_env
# Build any dependent libraries
.for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
( cd ../../../../$(use.project)/bindings/jni/android; ./build.sh )
. endif
.endfor
# Ensure we've built dependencies for Android
echo "******** Building $(project.name:) Android native libraries"
( cd ../../../builds/android && ./build.sh )
# Ensure we've built JNI interface
echo "******** Building $(project.name:) JNI interface & classes"
( cd .. && ./gradlew build jar )
echo "******** Building $(project.name:) JNI for Android"
rm -rf build && mkdir build && cd build
export ANDROID_BUILD_PREFIX=$ANDROID_BUILD_PREFIX
cmake -v -DCMAKE_TOOLCHAIN_FILE=../android_toolchain.cmake ..
# 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
echo "******** Building $(project.name:c).jar for Android"
# Copy class files into org/zeromq/etc.
unzip -q ../../build/libs/$(project.prefix)-jni-$(->version.major).$(->version.minor).$(->version.patch).jar
.for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
unzip -q -o ../../../../../$(use.project)/bindings/jni/android/$(use.project)-android.jar
. endif
.endfor
# Copy native libraries into lib/armeabi
mkdir -p lib/armeabi
mv $(project.libname)jni.so lib/armeabi
cp ../../../../builds/android/prefix/*/lib/*.so lib/armeabi
cp $ANDROID_NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$TOOLCHAIN_VERSION/libs/armeabi/libgnustl_shared.so lib/armeabi
zip -r -m ../$(project.prefix)-android.jar lib/ org/ META-INF/
cd ..
rm -rf build
echo "******** Complete"
.chmod_x ("$(topdir)/android/build.sh")
.
.output "$(topdir)/android/CMakeLists.txt"
$(project.GENERATED_WARNING_HEADER:)
cmake_minimum_required (VERSION 2.8)
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}/../../..")
########################################################################
# 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.prefix)jni_sources
.for project.class where class.okay
../src/main/c/$(namespace:c)_$(class.name:pascal).c
.endfor
)
add_library ($(project.prefix)jni SHARED ${$(project.prefix)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.prefix)jni ${MORE_LIBRARIES})
.
.output "$(topdir)/android/android_toolchain.cmake"
$(project.GENERATED_WARNING_HEADER:)
# CMake toolchain script
#
# Targets Android 8, ARM
# Called from build.sh via cmake
set (CMAKE_SYSTEM_NAME Linux) # Tell CMake we're cross-compiling
set (ANDROID True)
set (BUILD_ANDROID True)
include (CMakeForceCompiler)
include (FindPkgConfig)
# Where is the target environment
set (ANDROID_NDK_ROOT $ENV{ANDROID_NDK_ROOT})
set (ANDROID_SYS_ROOT $ENV{ANDROID_SYS_ROOT})
set (ANDROID_API_LEVEL $ENV{ANDROID_API_LEVEL})
set (TOOLCHAIN_PATH $ENV{TOOLCHAIN_PATH})
set (TOOLCHAIN_HOST $ENV{TOOLCHAIN_HOST})
set (TOOLCHAIN_ARCH $ENV{TOOLCHAIN_ARCH})
set (LIBRARIES_BUILD_PREFIX $ENV{ANDROID_BUILD_PREFIX})
# Here is the target environment located
set (CMAKE_INSTALL_PREFIX "${LIBRARIES_BUILD_PREFIX}")
set (CMAKE_FIND_ROOT_PATH
"${TOOLCHAIN_PATH}"
"${LIBRARIES_BUILD_PREFIX}"
"${LIBRARIES_BUILD_PREFIX}/share")
# pkg_config should not search for packages of the host system,
# but for the target system. To do this, there is the environment variable
# PKG_CONFIG_LIBDIR, which was added to pkg_config explicitely for crosscompiling.
# So set this here to point inside the CMAKE_FIND_ROOT_PATH so it can find
# only packages for the target system when crosscompiling.
set(pkgconfigLibDir)
foreach(currentFindRoot ${CMAKE_FIND_ROOT_PATH})
set(pkgconfigLibDir "${pkgconfigLibDir}:${currentFindRoot}/lib/pkgconfig")
endforeach(currentFindRoot)
set(ENV{PKG_CONFIG_LIBDIR} "${pkgconfigLibDir}")
set(ENV{PKG_CONFIG_PATH} "")
# Prefix detection only works with compiler id "GNU"
# CMake will look for prefixed g++, cpp, ld, etc. automatically
CMAKE_FORCE_C_COMPILER (${TOOLCHAIN_PATH}/arm-linux-androideabi-gcc GNU)
cmake_policy (SET CMP0015 NEW) # Use relative paths in link_directories
include_directories (${ANDROID_SYS_ROOT}/usr/include)
link_directories (${ANDROID_SYS_ROOT}/usr/lib)
.
.output "$(topdir)/ci_build.sh"
#!/usr/bin/env bash
set -e
# Set this to enable verbose profiling
[ -n "${CI_TIME-}" ] || CI_TIME=""
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
# Set this to enable verbose tracing
[ -n "${CI_TRACE-}" ] || CI_TRACE="no"
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 ;;
esac
########################################################################
# Build and check the jni binding
########################################################################
BUILD_PREFIX=/tmp
CONFIG_OPTS=()
CONFIG_OPTS+=("CFLAGS=-I${BUILD_PREFIX}/include")
CONFIG_OPTS+=("CPPFLAGS=-I${BUILD_PREFIX}/include")
CONFIG_OPTS+=("CXXFLAGS=-I${BUILD_PREFIX}/include")
CONFIG_OPTS+=("LDFLAGS=-L${BUILD_PREFIX}/lib")
CONFIG_OPTS+=("PKG_CONFIG_PATH=${BUILD_PREFIX}/lib/pkgconfig")
CONFIG_OPTS+=("--prefix=${BUILD_PREFIX}")
CONFIG_OPTS+=("--with-docs=no")
if [ -z "${CI_CONFIG_QUIET-}" ] || [ "${CI_CONFIG_QUIET-}" = yes ] || [ "${CI_CONFIG_QUIET-}" = true ]; then
CONFIG_OPTS+=("--quiet")
fi
pushd ../../..
# Clone and build dependencies
[ -z "$CI_TIME" ] || echo "`date`: Starting build of dependencies (if any)..."
. for use where defined (use.tarball)
wget $(use.tarball)
tar -xzf \$(basename "$(use.tarball)")
cd \$(basename "$(use.tarball)" .tar.gz)
. if count(use.add_config_opts) > 0
( # Custom additional options for $(use.project)
. for use.add_config_opts as add_cfgopt
CONFIG_OPTS+=("$(add_cfgopt)")
. endfor
$CI_TIME ./configure "${CONFIG_OPTS[@]}"
)
. else
$CI_TIME ./configure "${CONFIG_OPTS[@]}"
. endif
$CI_TIME make -j4
$CI_TIME make install
cd ..
. endfor
. for use where defined (use.repository)
. if defined (use.release)
$CI_TIME git clone --quiet --depth 1 -b $(use.release:) $(use.repository) $(use.project)
. else
$CI_TIME git clone --quiet --depth 1 $(use.repository) $(use.project)
. endif
cd $(use.project)
git --no-pager log --oneline -n1
if [ -e autogen.sh ]; then
$CI_TIME ./autogen.sh 2> /dev/null
fi
if [ -e buildconf ]; then
$CI_TIME ./buildconf 2> /dev/null
fi
if [ ! -e autogen.sh ] && [ ! -e buildconf ] && [ ! -e ./configure ] && [ -s ./configure.ac ]; then
$CI_TIME libtoolize --copy --force && \\
$CI_TIME aclocal -I . && \\
$CI_TIME autoheader && \\
$CI_TIME automake --add-missing --copy && \\
$CI_TIME autoconf || \\
$CI_TIME autoreconf -fiv
fi
. if count(use.add_config_opts) > 0
( # Custom additional options for $(use.project)
. for use.add_config_opts as add_cfgopt
CONFIG_OPTS+=("$(add_cfgopt)")
. endfor
$CI_TIME ./configure "${CONFIG_OPTS[@]}"
)
. else
$CI_TIME ./configure "${CONFIG_OPTS[@]}"
. endif
$CI_TIME make -j4
$CI_TIME make install
. if count (project->dependencies.class, class.project = use.project) > 0
# Build jni dependency
( cd bindings/jni && TERM=dumb PKG_CONFIG_PATH=$BUILD_PREFIX/lib/pkgconfig $CI_TIME ./gradlew publishToMavenLocal )
. endif
cd ..
. endfor
popd
pushd ../..
[ -z "$CI_TIME" ] || echo "`date`: Starting build of currently tested project..."
. if count(add_config_opts) > 0
# Custom additional options for this project
. for add_config_opts as add_cfgopt
CONFIG_OPTS+=("$(add_cfgopt)")
. endfor
. endif
$CI_TIME ./autogen.sh 2> /dev/null
$CI_TIME ./configure "${CONFIG_OPTS[@]}"
$CI_TIME make -j4
$CI_TIME make install
[ -z "$CI_TIME" ] || echo "`date`: Build completed without fatal errors!"
popd
TERM=dumb PKG_CONFIG_PATH=$BUILD_PREFIX/lib/pkgconfig $CI_TIME ./gradlew build jar
TERM=dumb $CI_TIME ./gradlew clean
########################################################################
# Build and check the jni android binding
########################################################################
pushd ../../builds/android
\. ./ci_build.sh
popd
pushd android
TERM=dumb PKG_CONFIG_PATH=$BUILD_PREFIX/lib/pkgconfig $CI_TIME ./build.sh
popd
.close
.chmod_x ("$(topdir)/ci_build.sh")
.if ! file.exists ("$(topdir)/gradle/wrapper/gradle-wrapper.jar")
. echo "Note: Could not locate gradle wrapper for JNI Binding! See bindings/jni/README.md."
.endif
.endmacro
.macro generate_class_in_java (class)
. directory.create ("$(topdir)/src/main/java/$(name_path)")
. output "$(topdir)/src/main/java/$(name_path)/$(my.class.name:pascal).java"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
package org.zeromq.$(project.prefix:c);
. for project.use
. if count (project->dependencies.class, class.project = use.project) > 0
import org.zeromq.$(use.project).*;
. endif
. endfor
public class $(my.class.name:pascal) \
. if count (my.class.constructor)
implements AutoCloseable\
. endif
{
static {
try {
System.loadLibrary ("$(project.prefix:c)jni");
}
catch (Exception e) {
System.exit (-1);
}
}
public long self;
. my.class.jni_void_new = 0
. for constructor where okay
/*
$(description:no,block)
*/
native static $(->return.jni_shim_type:) __$(name:camel) ($(jni_shim_signature_java:));
. if index () = 1
public $(my.class.name:pascal) ($(jni_method_signature:)) {
/* TODO: if __$(name:camel) fails, self is null... */
self = __$(name:camel) ($(jni_shim_invocation_java:));
}
public $(my.class.name:pascal) (long pointer) {
self = pointer;
}
. else
public $(static) $(->return.jni_java_type:) $(jni_name:) ($(jni_method_signature:)) {
return new $(->return.type:pascal) (__$(name:camel) ($(jni_shim_invocation_java:)));
}
. endif
. endfor
. for destructor
/*
$(description:no,block)
*/
native static void __$(name:camel) (long self);
@Override
public void close () {
__$(name:camel) (self);
self = 0;
}
. endfor
. for method where okay
. if name = "test"
. my.prefix = "public static"
. else
. my.prefix = "public"
. endif
/*
$(description:no,block)
*/
native static $(->return.jni_shim_type:) __$(name:camel) ($(jni_shim_signature_java:));
$(my.prefix) $(->return.jni_java_type:) $(jni_name:) ($(jni_method_signature:)) {
. if defined (method.return_self_p)
self = __$(name:camel) ($(jni_shim_invocation_java:));
return 0;
. elsif ->return.type = "nothing"
__$(name:camel) ($(jni_shim_invocation_java:));
. elsif ->return.jni_is_class = 1
return new $(->return.type:pascal) (__$(name:camel) ($(jni_shim_invocation_java:)));
. else
return __$(name:camel) ($(jni_shim_invocation_java:));
. endif
}
. endfor
}
.endmacro
.macro generate_jni_method_c (method)
. if defined (my.method.return_self_p) & !my.method.is_destructor
JNIEXPORT jlong JNICALL
. else
JNIEXPORT $(->return.jni_jni_type:) JNICALL
. endif
Java_$(namespace:c)_$(class.name:pascal)__1_1$(my.method.name:camel) (JNIEnv *env, jclass c\
. if jni_shim_signature_c <> ""
, \
. endif
$(jni_shim_signature_c:))
{
. for argument
. if type = "string" | type = "format"
char *$(c_name)_ = (char *) (*env)->GetStringUTFChars (env, $(c_name), NULL);
. elsif type = "buffer"
jbyte *$(c_name)_ = (byte *) (*env)->GetByteArrayElements (env, $(c_name), 0);
. endif
. endfor
.#
. if name = "new"
// Disable CZMQ signal handling; allow Java to deal with it
zsys_handler_set (NULL);
. endif
. if ->return.type = "nothing"
$(class.c_name)_$(c_name) ($(jni_native_invocation_c));
. my.return = ""
.#
. elsif ->return.type = "buffer"
. if regexp.match ("^\\.(.*)", ->return.size, my.size)
. my.size = "$(class.c_name)_$(my.size) (($(class.c_name)_t *) (intptr_t) self)"
. else
. my.size = ->return.size
. endif
jbyte *$(c_name)_ = (jbyte *) $(class.c_name)_$(c_name) ($(jni_native_invocation_c));
jint return_size_ = (jint) $(my.size);
jbyteArray return_data_ = (*env)->NewByteArray (env, return_size_);
(*env)->SetByteArrayRegion (env, return_data_, 0, return_size_, (jbyte *) $(c_name)_);
. my.return = " return return_data_;\n"
.#
. elsif ->return.type = "string"
char *$(c_name)_ = (char *) $(class.c_name)_$(c_name) ($(jni_native_invocation_c));
jstring return_string_ = (*env)->NewStringUTF (env, $(c_name)_);
. if ->return.fresh
zstr_free (&$(c_name)_);
. endif
. my.return = " return return_string_;\n"
.#
. elsif ->return.jni_is_class = 1 | ->return.type = "anything"
jlong $(c_name)_ = (jlong) (intptr_t) $(class.c_name)_$(c_name) ($(jni_native_invocation_c));
. my.return = " return $(c_name)_;\n"
. else
$(->return.jni_jni_type:) $(c_name)_ = ($(->return.jni_jni_type:)) $(class.c_name)_$(c_name) ($(jni_native_invocation_c));
. if defined (my.method.return_self_p)
. my.return = " return self;\n"
. else
. my.return = " return $(c_name)_;\n"
. endif
. endif
.#
. for argument
. if type = "string" | type = "format"
(*env)->ReleaseStringUTFChars (env, $(c_name), $(c_name)_);
. elsif type = "buffer"
(*env)->ReleaseByteArrayElements (env, $(c_name), (jbyte *) $(c_name)_, 0);
. endif
. endfor
$(my.return)\
}
.endmacro
.macro generate_class_in_c (class)
. directory.create ("$(topdir)/src/main/c/")
. my.cname = "$(namespace:c)_$(my.class.name:pascal)"
. output "$(topdir)/src/main/c/$(my.cname:).c"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "$(project.header:)"
#include "$(my.cname:).h"
. for constructor where okay
. generate_jni_method_c (constructor)
. endfor
. for destructor where okay
. generate_jni_method_c (destructor)
. endfor
. for method where okay
. generate_jni_method_c (method)
. endfor
.endmacro
.macro generate_test_wrapper (class)
. directory.create ("$(topdir)/src/test/java/$(name_path)")
. output "$(topdir)/src/test/java/$(name_path)/$(my.class.name:pascal)Test.java"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
package org.zeromq.$(project.prefix:c);
import org.junit.Assert;
import org.junit.Test;
public class $(my.class.name:pascal)Test {
static {
try {
System.loadLibrary ("$(project.prefix:c)jni");
}
catch (Exception e) {
System.exit (-1);
}
}
@Test
public void test () {
$(my.class.name:pascal).test (false);
}
}
.endmacro
project.namespace ?= switches.namespace? "org.zeromq.$(project.prefix)"
project.name_path ?= switches.name_path? "org/zeromq/$(project.prefix)"
project.topdir = "bindings/jni"
directory.create (topdir)
for project.class
resolve_class (class)
if class.okay
generate_class_in_java (class)
generate_class_in_c (class)
generate_test_wrapper (class)
endif
endfor
if count (project.class, okay = 1)
generate_wrapper ()
endif
endfunction