From b09071f20f1d28bda9edf4314dc6f53227288ac0 Mon Sep 17 00:00:00 2001
From: kytv <kytv@mail.i2p>
Date: Sun, 8 Jan 2012 23:23:00 +0000
Subject: [PATCH] update of jbigi scripts

- merge mbuild_jbigi.sh into build_jbigi.sh and drop mbuild_jbigi.sh
- make build.sh's tests optional
- try to determine the locations of JAVA_HOME and I2P
---
 core/c/jbigi/README                    |   5 +-
 core/c/jbigi/build.sh                  | 125 +++++++++++++++++--------
 core/c/jbigi/build_jbigi.sh            |  83 ++++++++--------
 core/c/jbigi/find-java-home            |  24 +++++
 core/c/jbigi/mbuild-all.sh             |   2 +-
 core/c/jbigi/mbuild_jbigi.sh           |  72 --------------
 debian/patches/0002-jbigi-soname.patch |  16 ++--
 7 files changed, 162 insertions(+), 165 deletions(-)
 create mode 100644 core/c/jbigi/find-java-home
 delete mode 100755 core/c/jbigi/mbuild_jbigi.sh

diff --git a/core/c/jbigi/README b/core/c/jbigi/README
index 98cba74c02..9e06381d6d 100644
--- a/core/c/jbigi/README
+++ b/core/c/jbigi/README
@@ -10,9 +10,10 @@ TODO: Document generated folder structure
 
 Linux-specific information:
 ===========================
-Some linux distributions comes bundled with GMP.
+Some linux distributions come bundled with GMP.
 Try 'locate lib/libgmp.so' to see.
-If so, install the the libgmp3-dev debian package to get the libgmp headers.
+
+In Debian/Ubuntu, install the the libgmp3-dev package to get the libgmp headers.
 Then export I2P=/path/to/your/i2p/install.
 Then do 'build.sh dynamic'. This will do a quick build using your installed libgmp library
 and then test it and the jbigi in your I2P installation to see which is faster.
diff --git a/core/c/jbigi/build.sh b/core/c/jbigi/build.sh
index e511a0fd89..f87932e485 100755
--- a/core/c/jbigi/build.sh
+++ b/core/c/jbigi/build.sh
@@ -17,63 +17,106 @@
 #  The resulting library is lib/libjbigi.so
 #
 
-mkdir -p lib/
-mkdir -p bin/local
+rm -rf bin/local
+mkdir -p lib bin/local
+
+# Use 4.3.2 32bit CPUs.
+# Use 5.0.2 64bit CPUs.
 VER=4.3.2
 
+# If JAVA_HOME isn't set, try to figure it out on our own
+[ -z $JAVA_HOME ] && . ./find-java-home
+if [ ! -f "$JAVA_HOME/include/jni.h" ]; then
+    echo "ERROR: Cannot find jni.h! Looked in \"$JAVA_HOME/include/jni.h\"" >&2
+    echo "Please set JAVA_HOME to a java home that has the JNI" >&2
+    exit 1
+fi
+
+# Abort script on uncaught errors
 set -e
 
-if [ "$1" != "dynamic" -a ! -d gmp-$VER ]
-then
-	TAR=gmp-$VER.tar.bz2
-        if [ ! -f $TAR ]
-        then
-	    echo "Downloading ftp://ftp.gmplib.org/pub/gmp-${VER}/gmp-${VER}.tar.bz2"
-	    wget ftp://ftp.gmplib.org/pub/gmp-${VER}/gmp-${VER}.tar.bz2
-        fi
+download_gmp ()
+{
+if [ $(which wget) ]; then
+    echo "Downloading ftp://ftp.gmplib.org/pub/gmp-${VER}/${TAR}"
+    wget -N --progress=dot ftp://ftp.gmplib.org/pub/gmp-${VER}/${TAR}
+else
+    echo "ERROR: Cannot find wget." >&2
+    echo >&2
+    echo "Please download ftp://ftp.gmplib.org/pub/gmp-${VER}/${TAR}" >&2
+    echo "manually and rerun this script." >&2
+    exit 1
+fi
+}
+
+extract_gmp ()
+{
+tar -xjf ${TAR} > /dev/null 2>&1|| (rm -f ${TAR} && download_gmp && extract_gmp || exit 1)
+}
+
+TAR=gmp-${VER}.tar.bz2
 
-	echo "Building the jbigi library with GMP Version $VER"
+if [ "$1" != "dynamic" -a ! -d gmp-${VER} ]; then
+    if [ ! -f $TAR ]; then
+        download_gmp
+    fi
 
-	echo "Extracting GMP..."
-	tar -xjf gmp-$VER.tar.bz2
+    echo "Building the jbigi library with GMP Version ${VER}"
+    echo "Extracting GMP..."
+    extract_gmp
 fi
 
 cd bin/local
 
 echo "Building..."
-if [ "$1" != "dynamic" ]
-then
-	case `uname -sr` in
-		Darwin*)
-			# --with-pic is required for static linking
-			../../gmp-$VER/configure --with-pic;;
-		*)
-			# and it's required for ASLR
-			../../gmp-$VER/configure --with-pic;;
-	esac
-	make
-	sh ../../build_jbigi.sh static
+if [ "$1" != "dynamic" ]; then
+    case `uname -sr` in
+        Darwin*)
+            # --with-pic is required for static linking
+            ../../gmp-${VER}/configure --with-pic;;
+        *)
+            # and it's required for ASLR
+            ../../gmp-${VER}/configure --with-pic;;
+    esac
+    make
+    sh ../../build_jbigi.sh static
 else
-	sh ../../build_jbigi.sh dynamic
+    shift
+    sh ../../build_jbigi.sh dynamic
 fi
 
 cp *jbigi???* ../../lib/
 echo 'Library copied to lib/'
 cd ../..
 
-if [ ! -f $I2P/lib/i2p.jar ]
-then
-	echo "I2P installation not found"
-    echo "We looked in '$I2P'"
-    echo "Not running tests against I2P installation without knowing where it is"
-    echo "Please set the environment variable I2P to the location of your I2P installation (so that \$I2P/lib/i2p.jar works)"
-    echo "If you do so, this script will run two tests to compare your installed jbigi with the one here you just compiled (to see if there is a marked improvement)"
-	exit 1
+if [ "$1" != "notest" ]; then
+    if [ -z "$I2P" ]; then
+        if [ -r $HOME/i2p/lib/i2p.jar ]; then
+            I2P="$HOME/i2p"
+        elif [ -r /usr/share/i2p/lib/i2p.jar ]; then
+            I2P="/usr/share/i2p"
+        else
+            echo "Please set the environment variable \$I2P to run tests." >&2
+        fi
+    fi
+
+    if [ ! -f $I2P/lib/i2p.jar ]; then
+        echo "I2P installation not found" >&2
+        echo "We looked in $I2P" >&2
+        echo "Not running tests against I2P installation without knowing where it is." >&2
+        echo >&2
+        echo "Please set the environment variable I2P to the location of your"
+        echo "I2P installation (so that \$I2P/lib/i2p.jar works)." >&2
+        echo "If you do so, this script will run two tests to compare your" >&2
+        echo "installed jbigi with the one here you just compiled to see if" >&2
+        echo "there is a marked improvement." >&2
+        exit 1
+    fi
+    echo 'Running test with standard I2P installation...'
+    java -cp $I2P/lib/i2p.jar:$I2P/lib/jbigi.jar net.i2p.util.NativeBigInteger
+    echo
+    echo 'Running test with new libjbigi...'
+    java -Djava.library.path=lib/ -cp $I2P/lib/i2p.jar:$I2P/lib/jbigi.jar net.i2p.util.NativeBigInteger
+    echo 'If the second run shows better performance, please use the jbigi that you have compiled so that I2P will work better!'
+    echo "(You can do that just by copying lib/libjbigi.so over the existing libjbigi.so file in \$I2P)"
 fi
-echo 'Running test with standard I2P installation...'
-java -cp $I2P/lib/i2p.jar:$I2P/lib/jbigi.jar net.i2p.util.NativeBigInteger
-echo
-echo 'Running test with new libjbigi...'
-java -Djava.library.path=lib/ -cp $I2P/lib/i2p.jar:$I2P/lib/jbigi.jar net.i2p.util.NativeBigInteger
-echo 'If the second is better performance, please use the jbigi you have compiled i2p will work better!'
-echo '(You can do that just by copying lib/libjbigi.so over the existing libjbigi.so file in $I2P)'
diff --git a/core/c/jbigi/build_jbigi.sh b/core/c/jbigi/build_jbigi.sh
index b5f45b7dbc..91135d211d 100755
--- a/core/c/jbigi/build_jbigi.sh
+++ b/core/c/jbigi/build_jbigi.sh
@@ -1,61 +1,62 @@
 #!/bin/sh
-# When executed in Mingw: Produces an jbigi.dll
-# When executed in Linux/FreeBSD: Produces an libjbigi.so
-# Darwin produces libjbigi.jnilib, right?
-
+# When executed in Mingw: Produces a jbigi.dll
+# When executed in Linux/FreeBSD: Produces a libjbigi.so
+# When executed in OSX: Produces a libjbigi.jnilib
 CC="gcc"
 
-case `uname -sr` in
+# If JAVA_HOME isn't set we'll try to figure it out
+[ -z $JAVA_HOME ] && . ./find-java-home
+if [ ! -f "$JAVA_HOME/include/jni.h" ]; then
+    echo "Cannot find jni.h! Looked in '$JAVA_HOME/include/jni.h'"
+    echo "Please set JAVA_HOME to a java home that has the JNI"
+    exit 1
+fi
+
+case `uname -s` in
 MINGW*)
-	JAVA_HOME="c:/software/j2sdk1.4.2_05"
-	COMPILEFLAGS="-Wall"
-	INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include/win32/ -I$JAVA_HOME/include/"
-	LINKFLAGS="-shared -Wl,--kill-at"
-	LIBFILE="jbigi.dll";;
+        JAVA_HOME="c:/software/j2sdk1.4.2_05"
+        COMPILEFLAGS="-Wall"
+        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include/win32/ -I$JAVA_HOME/include/"
+        LINKFLAGS="-shared -Wl,--kill-at"
+        LIBFILE="jbigi.dll";;
 CYGWIN*)
-	JAVA_HOME="c:/software/j2sdk1.4.2_05"
-	COMPILEFLAGS="-Wall -mno-cygwin"
-	INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include/win32/ -I$JAVA_HOME/include/"
-	LINKFLAGS="-shared -Wl,--kill-at"
-	LIBFILE="jbigi.dll";;
+        JAVA_HOME="c:/software/j2sdk1.4.2_05"
+        COMPILEFLAGS="-Wall -mno-cygwin"
+        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include/win32/ -I$JAVA_HOME/include/"
+        LINKFLAGS="-shared -Wl,--kill-at"
+        LIBFILE="jbigi.dll";;
 Darwin*)
-        JAVA_HOME="/Library/Java/Home"
-        COMPILEFLAGS="-Wall"
+        JAVA_HOME=$(/usr/libexec/java_home)
+        COMPILEFLAGS="-fPIC -Wall"
         INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include"
         LINKFLAGS="-dynamiclib -framework JavaVM"
         LIBFILE="libjbigi.jnilib";;
-SunOS*)
+SunOS*|OpenBSD*|NetBSD*|FreeBSD*|Linux*)
+        UNIXTYPE=$(uname -s | tr "[A-Z]" "[a-z]")
+        if [ $UNIXTYPE = "sunos" ]; then
+            UNIXTYPE="solaris"
+        fi
         COMPILEFLAGS="-fPIC -Wall"
-        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include -I$JAVA_HOME/include/solaris"
+        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include -I$JAVA_HOME/include/${UNIXTYPE}"
         LINKFLAGS="-shared -Wl,-soname,libjbigi.so"
         LIBFILE="libjbigi.so";;
 *)
-	COMPILEFLAGS="-fPIC -Wall"
-	INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include -I$JAVA_HOME/include/linux"
-	LINKFLAGS="-shared -Wl,-soname,libjbigi.so"
-	LIBFILE="libjbigi.so";;
+        echo "Unsupported system type."
+        exit 1;;
 esac
 
-if [ ! -f "$JAVA_HOME/include/jni.h" ]; then
-    echo "Cannot find jni.h! Looked in '$JAVA_HOME/include/jni.h'"
-    echo "Please set JAVA_HOME to a java home that has the JNI"
-    exit 1
-fi
-
-#To link dynamically to GMP (use libgmp.so or gmp.lib), uncomment the first line below
-#To link statically to GMP, uncomment the second line below
-# Bug!!! Quote *BOTH* or neither! --Sponge
-if test "$1" = "dynamic"
-then
-	echo "Building jbigi lib that is dynamically linked to GMP" 
-	LIBPATH="-L.libs"
-	INCLUDELIBS="-lgmp"
+if [ "$1" = "dynamic" ] ; then
+        echo "Building a jbigi lib that is dynamically linked to GMP"
+        LIBPATH="-L.libs"
+        INCLUDELIBS="-lgmp"
 else
-	echo "Building jbigi lib that is statically linked to GMP"
-	STATICLIBS=".libs/libgmp.a"
+        echo "Building a jbigi lib that is statically linked to GMP"
+        STATICLIBS=".libs/libgmp.a"
 fi
 
 echo "Compiling C code..."
 rm -f jbigi.o $LIBFILE
-$CC -c $COMPILEFLAGS $INCLUDES ../../jbigi/src/jbigi.c
-$CC $LINKFLAGS $INCLUDES -o $LIBFILE jbigi.o $INCLUDELIBS $STATICLIBS
+$CC -c $COMPILEFLAGS $INCLUDES ../../jbigi/src/jbigi.c || exit 1
+$CC $LINKFLAGS $INCLUDES -o $LIBFILE jbigi.o $INCLUDELIBS $STATICLIBS || exit 1
+
+exit 0
diff --git a/core/c/jbigi/find-java-home b/core/c/jbigi/find-java-home
new file mode 100644
index 0000000000..fd069f4a64
--- /dev/null
+++ b/core/c/jbigi/find-java-home
@@ -0,0 +1,24 @@
+UNIXTYPE=$(uname -s | tr "[A-Z]" "[a-z]")
+if [ $UNIXTYPE = "freebsd" ]; then
+    if [ -d /usr/local/openjdk6 ]; then
+        JAVA_HOME="/usr/local/openjdk6"
+    elif [ -d /usr/local/openjdk7 ]; then
+        JAVA_HOME="/usr/local/openjdk7"
+    fi
+elif [ $UNIXTYPE = "openbsd" ]; then
+    if [ -d /usr/local/jdk-1.7.0 ]; then
+        JAVA_HOME="/usr/local/jdk-1.7.0"
+    fi
+elif [ $UNIXTYPE = "netbsd" ]; then
+    if [ -d /usr/pkg/java/openjdk7 ]; then
+        JAVA_HOME="/usr/pkg/java/openjdk7"
+    fi
+elif [ $UNIXTYPE = "linux" ] && [ -e /etc/debian_version ]; then
+    if [ -d /usr/lib/jvm/default-java ]; then
+       JAVA_HOME="/usr/lib/jvm/default-java"
+    fi
+elif [ $UNIXTYPE = "darwin" ]; then
+    JAVA_HOME=$(/usr/libexec/java_home)
+fi
+
+export JAVA_HOME
diff --git a/core/c/jbigi/mbuild-all.sh b/core/c/jbigi/mbuild-all.sh
index c257278caf..711b960df0 100755
--- a/core/c/jbigi/mbuild-all.sh
+++ b/core/c/jbigi/mbuild-all.sh
@@ -154,7 +154,7 @@ esac
 
 make_static () {
         $ECHO "Attempting .${4} creation for ${3}${5}${2}"
-        ../../mbuild_jbigi.sh static || return 1
+        ../../build_jbigi.sh static || return 1
         cp ${3}.${4} ../../lib/net/i2p/util/${3}${5}${2}.${4}
         return 0
 }
diff --git a/core/c/jbigi/mbuild_jbigi.sh b/core/c/jbigi/mbuild_jbigi.sh
deleted file mode 100755
index 4b05dc4f3c..0000000000
--- a/core/c/jbigi/mbuild_jbigi.sh
+++ /dev/null
@@ -1,72 +0,0 @@
-#!/bin/sh
-# When executed in Mingw: Produces a jbigi.dll
-# When executed in Linux/FreeBSD: Produces a libjbigi.so
-# When executed in OSX: Produces a libjbigi.jnilib
-CC="gcc"
-
-case `uname -s` in
-MINGW*)
-        JAVA_HOME="c:/software/j2sdk1.4.2_05"
-        COMPILEFLAGS="-Wall"
-        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include/win32/ -I$JAVA_HOME/include/"
-        LINKFLAGS="-shared -Wl,--kill-at"
-        LIBFILE="jbigi.dll";;
-CYGWIN*)
-        JAVA_HOME="c:/software/j2sdk1.4.2_05"
-        COMPILEFLAGS="-Wall -mno-cygwin"
-        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include/win32/ -I$JAVA_HOME/include/"
-        LINKFLAGS="-shared -Wl,--kill-at"
-        LIBFILE="jbigi.dll";;
-Darwin*)
-        JAVA_HOME=$(/usr/libexec/java_home)
-        COMPILEFLAGS="-fPIC -Wall"
-        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include"
-        LINKFLAGS="-dynamiclib -framework JavaVM"
-        LIBFILE="libjbigi.jnilib";;
-SunOS*|OpenBSD*|NetBSD*|FreeBSD*|Linux*)
-        UNIXTYPE=$(uname -s | tr "[A-Z]" "[a-z]")
-        if [ $UNIXTYPE = "sunos" ]; then
-                UNIXTYPE="solaris"
-        elif [ $UNIXTYPE = "freebsd" ]; then
-                if [ -d /usr/local/openjdk6 ]; then
-                        JAVA_HOME="/usr/local/openjdk6"
-                elif [ -d /usr/local/openjdk7 ]; then
-                        JAVA_HOME="/usr/local/openjdk7"
-                fi
-        elif [ $UNIXTYPE = "openbsd" ]; then
-                if [ -d /usr/local/jdk-1.7.0 ]; then
-                        JAVA_HOME="/usr/local/jdk-1.7.0"
-                fi
-        elif [ $UNIXTYPE = "netbsd" ]; then
-                if [ -d /usr/pkg/java/openjdk7 ]; then
-                        JAVA_HOME="/usr/pkg/java/openjdk7"
-                fi
-        elif [ $UNIXTYPE = "linux" ] && [ -e /etc/debian_version ]; then
-                if [ -d /usr/lib/jvm/default-java ]; then
-                        JAVA_HOME="/usr/lib/jvm/default-java"
-                fi
-        fi
-        COMPILEFLAGS="-fPIC -Wall"
-        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include -I$JAVA_HOME/include/${UNIXTYPE}"
-        LINKFLAGS="-shared -Wl,-soname,libjbigi.so"
-        LIBFILE="libjbigi.so";;
-*)
-        echo "Unsupported system type."
-        exit 1;;
-esac
-
-if [ "$1" = "dynamic" ] ; then
-        echo "Building a jbigi lib that is dynamically linked to GMP"
-        LIBPATH="-L.libs"
-        INCLUDELIBS="-lgmp"
-else
-        echo "Building a jbigi lib that is statically linked to GMP"
-        STATICLIBS=".libs/libgmp.a"
-fi
-
-echo "Compiling C code..."
-rm -f jbigi.o $LIBFILE
-$CC -c $COMPILEFLAGS $INCLUDES ../../jbigi/src/jbigi.c || exit 1
-$CC $LINKFLAGS $INCLUDES -o $LIBFILE jbigi.o $INCLUDELIBS $STATICLIBS || exit 1
-
-exit 0
diff --git a/debian/patches/0002-jbigi-soname.patch b/debian/patches/0002-jbigi-soname.patch
index f38d5fb89d..09085deb6e 100644
--- a/debian/patches/0002-jbigi-soname.patch
+++ b/debian/patches/0002-jbigi-soname.patch
@@ -2,18 +2,18 @@ From: Kill Your TV <killyourtv@i2pmail.org>
 Date: Wed, 11 May 2011 00:12:04 +0000
 Subject: jbigi soname
 
-The purpose of this patch is to change the path that mbuild_jbigi.sh
+The purpose of this patch is to change the path that build_jbigi.sh
 expects to find the source files at. At the same time I'm specifying
 a soname to shut lintian up.
 
 ---
- core/c/jbigi/mbuild_jbigi.sh |    6 +++---
- 1 files changed, 3 insertions(+), 3 deletions(-)
+ core/c/jbigi/build_jbigi.sh |    4 +++---
+ 1 files changed, 2 insertions(+), 2 deletions(-)
 
---- a/core/c/jbigi/mbuild_jbigi.sh
-+++ b/core/c/jbigi/mbuild_jbigi.sh
-@@ -47,7 +47,7 @@
-                 fi
+--- a/core/c/jbigi/build_jbigi.sh
++++ b/core/c/jbigi/build_jbigi.sh
+@@ -37,7 +37,7 @@
+             UNIXTYPE="solaris"
          fi
          COMPILEFLAGS="-fPIC -Wall"
 -        INCLUDES="-I. -I../../jbigi/include -I$JAVA_HOME/include -I$JAVA_HOME/include/${UNIXTYPE}"
@@ -21,7 +21,7 @@ a soname to shut lintian up.
          LINKFLAGS="-shared -Wl,-soname,libjbigi.so"
          LIBFILE="libjbigi.so";;
  *)
-@@ -66,7 +66,7 @@
+@@ -56,7 +56,7 @@
  
  echo "Compiling C code..."
  rm -f jbigi.o $LIBFILE
-- 
GitLab