Rev 46460 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/bin/shif test -z "${R_HOME}"; thenecho "This command can be executed via R CMD only!"exit 1fitools_classpath=${R_SHARE_DIR}/javausage="Usage: R CMD javareconf [options]Detect current Java setup and update the corresponding configuration in R.Options:-h, --help print this help message and exit-n, --dry-run perform Java detection, but don't touch anyconfigurations files-e <prog> same as -n but exports all detected variablesand runs <prog>. If -e is the last argumentor <prog> is '' then a shell is used insteadxxx=yyy evaluate the corresponding expression(e.g. JAVA_HOME=/usr/lib/java)Environment variables that can be used to influence the detection:JAVA path to a Java interpreter executableBy default first 'java' command found on the PATHis taken (unless JAVA_HOME is also specified).JAVA_HOME home of the Java environment. If not specified,it will be detected automatically from the Javainterpreter.JAVAC path to a Java compilerJAVAH path to a Java header/stub generatorJAR path to a Java archive toolThe following variables should be used with extreme caution. Theymust all match, so use only if you have a very special setup thatjavareconf cannot detect automatically:JAVA_LD_LIBRARY_PATH library path necessary at run-timeJAVA_CPPFLAGS C preprocessor flags necessary to compile JNI programsJAVA_LIBS libraries (as linker flags) necessary to compileJNI programsReport bugs to <r-bugs@r-project.org>."dry_run=noexport_vars=nowhile test -n "${1}"; docase ${1} in-help | --help | -h) echo "${usage}"; exit 0 ;;-n | --dry-run) dry_run=yes ;;-e) dry_run=yes; export_vars=yes; run_prog=${2}; shift ;;*=*) eval ${1} ;;esacshiftdone## find java compiler binariesif test -z "${JAVA_HOME}" ; thenJAVA_PATH=${PATH}elseif test ! -e "${JAVA_HOME}"; thenecho "*** JAVA_HOME is not a valid path, ignoring"JAVA_HOME=JAVA_PATH=${PATH}else## try jre/bin first just in case we don't have full JDKJAVA_PATH=${JAVA_HOME}:${JAVA_HOME}/jre/bin:${JAVA_HOME}/bin:${JAVA_HOME}/../bin:${PATH}fifi## if 'java' is not on the PATH or JAVA_HOME, add some guesses as of## where java could liveJAVA_PATH=${JAVA_PATH}:/usr/java/bin:/usr/jdk/bin:/usr/lib/java/bin:/usr/lib/jdk/bin:/usr/local/java/bin:/usr/local/jdk/bin:/usr/local/lib/java/bin:/usr/local/lib/jdk/binif test -z "$JAVA"; thensave_IFS=$IFS; IFS=:for dir in ${JAVA_PATH}; doif test -f "$dir/java"; thenJAVA="$dir/java"breakfidoneIFS=${save_IFS}fiif test -z "$JAVA"; thenecho "~*** Cannot find any Java interpreter~*** Please make sure 'java' is on your PATH or set JAVA_HOME correspondingly"|${SED-sed} -e 'y/~/\n/' >&2exit 1fiecho "Java interpreter : $JAVA"jires=`$JAVA -classpath ${tools_classpath} getsp -test`if test "$jires" != "Test1234OK"; thenecho "$jires"echo "~*** Java interpreter doesn't work properly.~"|${SED-sed} -e 'y/~/\n/' >&2exit 1fiif test -z "${JAVA_HOME}"; thenJAVA_HOME=`$JAVA -classpath ${tools_classpath} getsp java.home`fiif test -z "${JAVA_HOME}"; thenecho "~*** Cannot find Java environemnt.~*** Please set JAVA_HOME correspondingly.~"|${SED-sed} -e 'y/~/\n/' >&2exit 1fiecho "Java version : `$JAVA -classpath ${tools_classpath} getsp java.version 2>/dev/null`"echo "Java home path : ${JAVA_HOME}"# optional parts - compiler, javah and jarif test -z "$JAVAC"; thensave_IFS=$IFS; IFS=:for dir in ${JAVA_PATH}; doif test -f "$dir/javac"; thenJAVAC="$dir/javac"breakfidoneIFS=${save_IFS}fi# test functionality of the compilerjavac_works='not present'if test -n "$JAVAC"; thenjavac_works='not functional'# tempdir=`mktemp -d`: ${TMPDIR=/tmp}{ tempdir=`(mktemp -d -q "${TMPDIR}/RJRECONF.XXXXXX") 2>/dev/null` \&& test -n "${tempdir}" && test -d "${tempdir}" ; } ||{ test -n "${RANDOM}" && tempdir=${TMPDIR}/RJRECONF$$-${RANDOM} \&& (mkdir "${tempdir}") ; } ||{ tempdir=${TMPDIR}/RJRECONF.$$-`date +%m%d%H%M%S` \&& (mkdir "${tempdir}"); } ||{ tempdir=${TMPDIR}/RJRECONF.$$ && (mkdir "${tempdir}") ; } ||(error "cannot create temporary directory" && exit 1)echo "public class A { }" > ${tempdir}/A.javaif test -e ${tempdir}/A.java; thenif "${JAVAC}" ${tempdir}/A.java >/dev/null; thenif test -e ${tempdir}/A.class; thenjavac_works=yesfififirm -rf ${tempdir}fiif test "${javac_works}" = yes; thenecho "Java compiler : ${JAVAC}"elseecho "Java compiler : ${javac_works}"fiif test -z "$JAVAH"; thensave_IFS=$IFS; IFS=:for dir in ${JAVA_PATH}; doif test -f "$dir/javah"; thenJAVAH="$dir/javah"breakfidoneIFS=${save_IFS}fiecho "Java headers gen.: ${JAVAH}"if test -z "$JAR"; thensave_IFS=$IFS; IFS=:for dir in ${JAVA_PATH}; doif test -f "$dir/jar"; thenJAR="$dir/jar"breakfidoneIFS=${save_IFS}fiecho "Java archive tool: ${JAR}": ${JAVA_LIBS=~autodetect~}: ${JAVA_CPPFLAGS=~autodetect~}: ${JAVA_LD_LIBRARY_PATH=~autodetect~}custom_JAVA_LIBS="${JAVA_LIBS}"custom_JAVA_CPPFLAGS="${JAVA_CPPFLAGS}"custom_JAVA_LD_LIBRARY_PATH="${JAVA_LD_LIBRARY_PATH}"# sys-dependent tweaks to JNI flagshostos=`uname 2>/dev/null`case "$hostos" inDarwin*)JAVA_LD_LIBRARY_PATH=JAVA_LIBS="-framework JavaVM"JAVA_CPPFLAGS='-I$(JAVA_HOME)/include';;*)## we need to reset LD_LIBRARY_PATH, because Java automatically## adds all LD_LIBRARY_PATH to Java pathsave_LDLP=${LD_LIBRARY_PATH}LD_LIBRARY_PATH=export LD_LIBRARY_PATH# remove trailing : (if any) and replace occurrences of JAVA_HOME# with $(JAVA_HOME)JAVA_LD_LIBRARY_PATH=`$JAVA -classpath ${tools_classpath} getsp java.library.path| ${SED-sed} -e s:${JAVA_HOME}:\$\(JAVA_HOME\):g -e 's/:$//'`LD_LIBRARY_PATH=${save_LDLP}export LD_LIBRARY_PATH# build JAVA_LIBS from the detected pathJAVA_LIBS=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's|:| -L|g'`if test -n "${JAVA_LIBS}"; then JAVA_LIBS="-L${JAVA_LIBS}"; fiJAVA_LIBS="${JAVA_LIBS} -ljvm"# create shell-version to be used in scriptsJAVA_LD_LIBRARY_PATH_SH=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's:$(JAVA_HOME):\$\{JAVA_HOME\}:g'`## includes consist of two parts - jni.h and machine-dependent jni_md.hjinc=''JAVA_CPPFLAGS=''for pinc in include ../include jre/include; doif test -e "${JAVA_HOME}/${pinc}/jni.h"; then jinc="${JAVA_HOME}/${pinc}"; break; fidone## only if we get jni.h we can try to find jni_md.hif test -n "${jinc}"; thenJAVA_CPPFLAGS="-I${jinc}"jmdinc=''jmdirs=''## we are not in configure, so we need to find the OS from Rhost_os=`echo 'cat(R.version$os)'|${R_HOME}/bin/R --vanilla --slave 2>/dev/null`## put the most probable locations for each system in the first placecase "${host_os}" inlinux*) jmdirs=linux;;bsdi*) jmdirs=bsdos;;osf*) jmdirs=alpha;;solaris*) jmdirs=solaris;;freebsd*) jmdirs=freebsd; add_java_libs='-lpthread';;esac## in case host_os detection failed, add all candidatesif test -z "${jmdirs}"; then jmdirs='linux solaris freebsd alpha bsdos'; fi## prepend . and append less-likely onesjmdirs=". ${jmdirs} genunix ppc x86 iris hp-ux aix win32 cygwin openbsd"for pimd in ${jmdirs}; doif test -e "${jinc}/${pimd}/jni_md.h"; then jmdinc="${jinc}/${pimd}"; break; fidoneif test -z "${jmdinc}"; then# ultima-ratio: use find and pray that it worksjmdinc=`find "${jinc}/" -name jni_md.h 2> /dev/null |head -n 1 2>/dev/null`if test -n "${jmdinc}"; then jmdinc=`dirname "${jmdinc}"`; fifiif test -n "${jmdinc}"; thenif test "${jmdinc}" != "${jinc}/."; thenJAVA_CPPFLAGS="${JAVA_CPPFLAGS} -I${jmdinc}"fifiJAVA_CPPFLAGS=`echo ${JAVA_CPPFLAGS} | ${SED-sed} -e s:${JAVA_HOME}:\$\(JAVA_HOME\):g`fi;;esacif test -n "${add_java_libs}"; then JAVA_LIBS="${JAVA_LIBS} ${add_java_libs}"; fi## honor user overridesacx_custom_java_libs=noif test "${custom_JAVA_LIBS}" != '~autodetect~'; thenJAVA_LIBS="${custom_JAVA_LIBS}"JAVA_LIBS0=`echo ${JAVA_LIBS} | ${SED-sed} -e s:${JAVA_HOME}:\$\(JAVA_HOME\):g`acx_custom_java_libs=yesfiif test "${custom_JAVA_CPPFLAGS}" != '~autodetect~'; thenJAVA_CPPFLAGS="${custom_JAVA_CPPFLAGS}"fiif test "${custom_JAVA_LD_LIBRARY_PATH}" != '~autodetect~'; thenJAVA_LD_LIBRARY_PATH="${custom_JAVA_LD_LIBRARY_PATH}"JAVA_LD_LIBRARY_PATH_SH=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's:$(JAVA_HOME):\$\{JAVA_HOME\}:g'`if test "${acx_custom_java_libs}" != yes; thenecho ''echo ' ** Warning: you are using custom JAVA_LD_LIBRARY_PATH without' >&2echo ' custom JAVA_LIBS. We cannot guarantee that this will work,' >&2echo ' both settings must match for a proper Java operation!' >&2fielseif test "${acx_custom_java_libs}" = yes; thenecho ''echo ' ** Warning: you are using custom JAVA_LIBS setting without' >&2echo ' specifying JAVA_LD_LIBRARY_PATH. We cannot gearantee that' >&2echo ' this will work, both must match for a proper Java operation!' >&2fifiecho "Java library path: ${JAVA_LD_LIBRARY_PATH}"echo "JNI linker flags : ${JAVA_LIBS}"echo "JNI cpp flags : ${JAVA_CPPFLAGS}"echo ""if test "${export_vars}" = yes; thenexport JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATHecho "The following Java variables have been exported:"echo "JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATH"test -z "${run_prog}" && test -n "${SHELL}" && run_prog="${SHELL}"test -z "${run_prog}" && run_prog=/bin/shecho "Runnig: ${run_prog}"${run_prog}exit 0fitest "${dry_run}" = yes && exit 0echo "Updating Java configuration in ${R_HOME}"files="${R_HOME}/etc${R_ARCH}/Makeconf ${R_HOME}/etc${R_ARCH}/ldpaths"for file in $files; do${SED-sed} -e "s|JAVA =.\{0,\}|JAVA = $JAVA|" -e "s|JAVA_HOME =.\{0,\}|JAVA_HOME = ${JAVA_HOME}|" -e "s|: \${JAVA_HOME=.\{1,\}|: \${JAVA_HOME=${JAVA_HOME}}|" -e "s|: \${R_JAVA_LD_LIBRARY_PATH=.\{1,\}|: \${R_JAVA_LD_LIBRARY_PATH=${JAVA_LD_LIBRARY_PATH_SH}}|" -e "s|JAVA_LIBS =.\{0,\}|JAVA_LIBS = ${JAVA_LIBS}|g" -e "s|JAVA_LD_LIBRARY_PATH =.\{0,\}|JAVA_LD_LIBRARY_PATH = ${JAVA_LD_LIBRARY_PATH}|" -e "s|JAVAC =.\{0,\}|JAVAC = $JAVAC|" -e "s|JAVAH =.\{0,\}|JAVAH = $JAVAH|" -e "s|JAR =.\{0,\}|JAR = $JAR|" "${file}" -e "s|JAVA_CPPFLAGS =.\{0,\}|JAVA_CPPFLAGS = ${JAVA_CPPFLAGS}|g"> "${file}.new"if test -f "${file}.new"; thenmv "${file}" "${file}.old"mv "${file}.new" "${file}"elseecho "*** cannot create ${file}.new~*** Please run as root if required.~" | ${SED-sed} -e 'y/~/\n/' >&2exit 1fidoneecho "Done."echo ''