Skip to content

Commit 79ff193

Browse files
committed
build: add --enable-jni configure option
jni headers will be used if possible, otherwise jni support is disabled
1 parent f79d80a commit 79ff193

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

Makefile.am

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ COMMON_LIB = libsecp256k1_common.la
66
else
77
COMMON_LIB =
88
endif
9-
noinst_LTLIBRARIES = $(COMMON_LIB)
9+
if USE_JNI
10+
JNI_LIB = libsecp256k1_jni.la
11+
else
12+
JNI_LIB =
13+
endif
14+
noinst_LTLIBRARIES = $(COMMON_LIB) $(JNI_LIB)
1015
include_HEADERS = include/secp256k1.h
1116
noinst_HEADERS =
1217
noinst_HEADERS += src/scalar.h
@@ -52,8 +57,11 @@ endif
5257

5358
libsecp256k1_la_SOURCES = src/secp256k1.c
5459
libsecp256k1_la_CPPFLAGS = -I$(top_srcdir)/include $(SECP_INCLUDES)
55-
libsecp256k1_la_LIBADD = $(COMMON_LIB) $(SECP_LIBS)
60+
libsecp256k1_la_LIBADD = $(COMMON_LIB) $(JNI_LIB) $(SECP_LIBS)
61+
5662

63+
libsecp256k1_jni_la_SOURCES = src/java/org_bitcoin_NativeSecp256k1.c
64+
libsecp256k1_jni_la_CPPFLAGS = $(JNI_INCLUDES)
5765

5866
noinst_PROGRAMS =
5967
if USE_BENCHMARK

build-aux/m4/ax_jni_include_dir.m4

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# ===========================================================================
2+
# http://www.gnu.org/software/autoconf-archive/ax_jni_include_dir.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_JNI_INCLUDE_DIR
8+
#
9+
# DESCRIPTION
10+
#
11+
# AX_JNI_INCLUDE_DIR finds include directories needed for compiling
12+
# programs using the JNI interface.
13+
#
14+
# JNI include directories are usually in the Java distribution. This is
15+
# deduced from the value of $JAVA_HOME, $JAVAC, or the path to "javac", in
16+
# that order. When this macro completes, a list of directories is left in
17+
# the variable JNI_INCLUDE_DIRS.
18+
#
19+
# Example usage follows:
20+
#
21+
# AX_JNI_INCLUDE_DIR
22+
#
23+
# for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS
24+
# do
25+
# CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR"
26+
# done
27+
#
28+
# If you want to force a specific compiler:
29+
#
30+
# - at the configure.in level, set JAVAC=yourcompiler before calling
31+
# AX_JNI_INCLUDE_DIR
32+
#
33+
# - at the configure level, setenv JAVAC
34+
#
35+
# Note: This macro can work with the autoconf M4 macros for Java programs.
36+
# This particular macro is not part of the original set of macros.
37+
#
38+
# LICENSE
39+
#
40+
# Copyright (c) 2008 Don Anderson <[email protected]>
41+
#
42+
# Copying and distribution of this file, with or without modification, are
43+
# permitted in any medium without royalty provided the copyright notice
44+
# and this notice are preserved. This file is offered as-is, without any
45+
# warranty.
46+
47+
#serial 10
48+
49+
AU_ALIAS([AC_JNI_INCLUDE_DIR], [AX_JNI_INCLUDE_DIR])
50+
AC_DEFUN([AX_JNI_INCLUDE_DIR],[
51+
52+
JNI_INCLUDE_DIRS=""
53+
54+
if test "x$JAVA_HOME" != x; then
55+
_JTOPDIR="$JAVA_HOME"
56+
else
57+
if test "x$JAVAC" = x; then
58+
JAVAC=javac
59+
fi
60+
AC_PATH_PROG([_ACJNI_JAVAC], [$JAVAC], [no])
61+
if test "x$_ACJNI_JAVAC" = xno; then
62+
AC_MSG_ERROR([cannot find JDK; try setting \$JAVAC or \$JAVA_HOME])
63+
fi
64+
_ACJNI_FOLLOW_SYMLINKS("$_ACJNI_JAVAC")
65+
_JTOPDIR=`echo "$_ACJNI_FOLLOWED" | sed -e 's://*:/:g' -e 's:/[[^/]]*$::'`
66+
fi
67+
68+
case "$host_os" in
69+
darwin*) _JTOPDIR=`echo "$_JTOPDIR" | sed -e 's:/[[^/]]*$::'`
70+
_JINC="$_JTOPDIR/Headers";;
71+
*) _JINC="$_JTOPDIR/include";;
72+
esac
73+
_AS_ECHO_LOG([_JTOPDIR=$_JTOPDIR])
74+
_AS_ECHO_LOG([_JINC=$_JINC])
75+
76+
# On Mac OS X 10.6.4, jni.h is a symlink:
77+
# /System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/jni.h
78+
# -> ../../CurrentJDK/Headers/jni.h.
79+
AC_CHECK_FILE([$_JINC/jni.h],
80+
[JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $_JINC"],
81+
[_JTOPDIR=`echo "$_JTOPDIR" | sed -e 's:/[[^/]]*$::'`
82+
AC_CHECK_FILE([$_JTOPDIR/include/jni.h],
83+
[JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $_JTOPDIR/include"],
84+
AC_MSG_ERROR([cannot find JDK header files]))
85+
])
86+
87+
# get the likely subdirectories for system specific java includes
88+
case "$host_os" in
89+
bsdi*) _JNI_INC_SUBDIRS="bsdos";;
90+
freebsd*) _JNI_INC_SUBDIRS="freebsd";;
91+
linux*) _JNI_INC_SUBDIRS="linux genunix";;
92+
osf*) _JNI_INC_SUBDIRS="alpha";;
93+
solaris*) _JNI_INC_SUBDIRS="solaris";;
94+
mingw*) _JNI_INC_SUBDIRS="win32";;
95+
cygwin*) _JNI_INC_SUBDIRS="win32";;
96+
*) _JNI_INC_SUBDIRS="genunix";;
97+
esac
98+
99+
# add any subdirectories that are present
100+
for JINCSUBDIR in $_JNI_INC_SUBDIRS
101+
do
102+
if test -d "$_JTOPDIR/include/$JINCSUBDIR"; then
103+
JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $_JTOPDIR/include/$JINCSUBDIR"
104+
fi
105+
done
106+
])
107+
108+
# _ACJNI_FOLLOW_SYMLINKS <path>
109+
# Follows symbolic links on <path>,
110+
# finally setting variable _ACJNI_FOLLOWED
111+
# ----------------------------------------
112+
AC_DEFUN([_ACJNI_FOLLOW_SYMLINKS],[
113+
# find the include directory relative to the javac executable
114+
_cur="$1"
115+
while ls -ld "$_cur" 2>/dev/null | grep " -> " >/dev/null; do
116+
AC_MSG_CHECKING([symlink for $_cur])
117+
_slink=`ls -ld "$_cur" | sed 's/.* -> //'`
118+
case "$_slink" in
119+
/*) _cur="$_slink";;
120+
# 'X' avoids triggering unwanted echo options.
121+
*) _cur=`echo "X$_cur" | sed -e 's/^X//' -e 's:[[^/]]*$::'`"$_slink";;
122+
esac
123+
AC_MSG_RESULT([$_cur])
124+
done
125+
_ACJNI_FOLLOWED="$_cur"
126+
])# _ACJNI

configure.ac

+22
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ AC_ARG_ENABLE(endomorphism,
5454
[use_endomorphism=$enableval],
5555
[use_endomorphism=no])
5656

57+
AC_ARG_ENABLE(jni,
58+
AS_HELP_STRING([--enable-jni],[enable libsecp256k1_jni (default is auto)]),
59+
[use_jni=$enableval],
60+
[use_jni=auto])
61+
5762
AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=gmp|64bit|64bit_asm|32bit|auto],
5863
[Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
5964

@@ -221,6 +226,21 @@ if test x"$use_tests" = x"yes"; then
221226
fi
222227
fi
223228

229+
if test x"$use_jni" != x"no"; then
230+
AX_JNI_INCLUDE_DIR
231+
if test "x$JNI_INCLUDE_DIRS" = "x"; then
232+
if test x"$use_jni" = x"yes"; then
233+
AC_MSG_ERROR([jni support explicitly requested but headers were not found])
234+
fi
235+
AC_MSG_WARN([jni headers not found. jni support disabled])
236+
else
237+
use_jni=yes
238+
for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS; do
239+
JNI_INCLUDES="$JNI_INCLUDES -I$JNI_INCLUDE_DIR"
240+
done
241+
fi
242+
fi
243+
224244
if test x"$set_field" = x"gmp" || test x"$set_bignum" = x"gmp"; then
225245
SECP_LIBS="$SECP_LIBS $GMP_LIBS"
226246
fi
@@ -235,6 +255,7 @@ AC_MSG_NOTICE([Using scalar implementation: $set_scalar])
235255

236256
AC_CONFIG_HEADERS([src/libsecp256k1-config.h])
237257
AC_CONFIG_FILES([Makefile libsecp256k1.pc])
258+
AC_SUBST(JNI_INCLUDES)
238259
AC_SUBST(SECP_INCLUDES)
239260
AC_SUBST(SECP_LIBS)
240261
AC_SUBST(SECP_TEST_LIBS)
@@ -243,4 +264,5 @@ AC_SUBST(YASM_BINFMT)
243264
AM_CONDITIONAL([USE_ASM], [test x"$set_field" == x"64bit_asm"])
244265
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
245266
AM_CONDITIONAL([USE_BENCHMARK], [test x"$use_benchmark" != x"no"])
267+
AM_CONDITIONAL([USE_JNI], [test x"$use_jni" == x"yes"])
246268
AC_OUTPUT

src/java/org/bitcoin/NativeSecp256k1.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class NativeSecp256k1 {
1616
static {
1717
boolean isEnabled = true;
1818
try {
19-
System.loadLibrary("javasecp256k1");
19+
System.loadLibrary("secp256k1_jni");
2020
} catch (UnsatisfiedLinkError e) {
2121
isEnabled = false;
2222
}

0 commit comments

Comments
 (0)