forked from zeromq/zproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zproject_java.gsl
849 lines (729 loc) · 25.6 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
# 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"
cmake_minimum_required (VERSION 2.8)
project ($(project.prefix)jni CXX)
enable_language (C)
find_package (JNI REQUIRED)
find_library (ZMQ_LIBRARY NAMES zmq libzmq REQUIRED)
find_library ($(PROJECT.PREFIX)_LIBRARY NAMES $(project.prefix) REQUIRED)
include_directories (${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} ../../include src/native/include)
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 ${ZMQ_LIBRARY} ${$(PROJECT.PREFIX)_LIBRARY})
.
.directory.create ("$(topdir)/gradle/wrapper")
.output "$(topdir)/gradle/wrapper/gradle-wrapper.properties"
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-2.8-bin.zip
.
.output "$(topdir)/build.gradle"
/*
$(project.GENERATED_WARNING_HEADER:)
*/
apply plugin: 'java'
apply plugin: 'maven'
group = "org.zeromq"
version = "$(->version.major).$(->version.minor).$(->version.patch)"
repositories {
mavenLocal()
mavenCentral()
}
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 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
.
.output "$(topdir)/gradlew"
#!/usr/bin/env bash
# Gradle start up script for Linux/UN*X
#
$(project.GENERATED_WARNING_HEADER:)
#
# Add default JVM options here.
# You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \\(.*\\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \\"$PRG\\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \\"-Xdock:name=$APP_NAME\\" \\"-Xdock:icon=$APP_HOME/media/gradle.icns\\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\\"$arg\\""
fi
i=$\((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
.
.terminator="\r\n"
.output "$(topdir)/gradlew.bat"
@if "%DEBUG%" == "" @echo off
$(project.generated_warning_header_for_dot_bat:)
@rem Gradle startup script for Windows
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here.
@rem You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\\gradle\\wrapper\\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
.
.terminator="\n"
.output "$(topdir)/README.md"
# $(project.prefix)-jni
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
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).
## 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-r10e
TOOLCHAIN_NAME=arm-linux-androideabi-4.9
TOOLCHAIN_HOST=arm-linux-androideabi
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.:)
.
.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-r10e
# 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
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 .. && gradle build jar )
rm -rf build && mkdir build && cd build
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
echo "******** Building $(project.name:) JNI for Android"
make $MAKE_OPTIONS
echo "******** Building $(project.name:c).jar for Android"
# Copy class files into org/zeromq/etc.
unzip -q ../../build/libs/$(project.prefix)*.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)
find_package (JNI REQUIRED)
include_directories (${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} ../src/native/include)
# message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
# message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
# message (STATUS "JNI_FOUND=${JNI_FOUND}")
# message (STATUS "JAVA_AWT_LIBRARY=${JAVA_AWT_LIBRARY}")
# message (STATUS "JAVA_JVM_LIBRARY=${JAVA_JVM_LIBRARY}")
# message (STATUS "JAVA_INCLUDE_PATH=${JAVA_INCLUDE_PATH}")
# message (STATUS "JAVA_INCLUDE_PATH2=${JAVA_INCLUDE_PATH2}")
# message (STATUS "JAVA_AWT_INCLUDE_PATH=${JAVA_AWT_INCLUDE_PATH}")
# message (STATUS "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
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)
target_link_libraries ($(project.prefix)jni
$(project.linkname)
.for project.use where optional = 0
$(use.linkname)
.endfor
)
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)
.
.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})
# api level see doc https://github.com/taka-no-me/android-cmake
set (CMAKE_INSTALL_PREFIX /tmp)
set (CMAKE_FIND_ROOT_PATH
"${TOOLCHAIN_PATH}"
"${CMAKE_INSTALL_PREFIX}"
"${CMAKE_INSTALL_PREFIX}/share")
# 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)
.for use where optional = 0
# Find location of $(use.header) file
pkg_check_modules (PC_$(USE.PROJECT) "$(use.libname)")
if (NOT PC_$(USE.PROJECT)_FOUND)
pkg_check_modules(PC_$(USE.PROJECT) "$(use.prefix)")
endif (NOT PC_$(USE.PROJECT)_FOUND)
if (PC_$(USE.PROJECT)_FOUND)
set (PC_$(USE.PROJECT)_INCLUDE_HINTS ${PC_$(USE.PROJECT)_INCLUDE_DIRS} ${PC_$(USE.PROJECT)_INCLUDE_DIRS}/*)
endif (PC_$(USE.PROJECT)_FOUND)
find_path (
$(USE.PROJECT)_INCLUDE_DIR
NAMES $(use.header)
HINTS ${PC_$(USE.PROJECT)_INCLUDE_HINTS}
)
.endfor
cmake_policy (SET CMP0015 NEW) # Use relative paths in link_directories
include_directories (
.for use where optional = 0
${$(USE.PROJECT)_INCLUDE_DIR}
.endfor
../../../include
../../../bindings/jni/src/native/include
../../../builds/android/prefix/arm-linux-androideabi-4.9/include
${ANDROID_SYS_ROOT}/usr/include
)
link_directories (
../../../builds/android/prefix/arm-linux-androideabi-4.9/lib
${ANDROID_SYS_ROOT}/usr/lib
)
.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 $(->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