The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
39282 urbaneks 1
if test -z "${R_HOME}"; then
61924 ripley 2
    echo "This command can be only executed via R CMD!"
39282 urbaneks 3
    exit 1
4
fi
5
 
61911 ripley 6
## pick up configure-time settings
61924 ripley 7
. "${R_HOME}/etc${R_ARCH}/javaconf"
61911 ripley 8
 
61888 ripley 9
DYLIB_EXT=`${R_HOME}/bin/R CMD config DYLIB_EXT`
39282 urbaneks 10
tools_classpath=${R_SHARE_DIR}/java
11
 
47443 hornik 12
revision='$Rev: 63241 $'
47442 hornik 13
version=`set - ${revision}; echo ${2}`
47522 ripley 14
version="R Java configurator: ${R_VERSION} (r${version})
47442 hornik 15
 
61888 ripley 16
Copyright (C) 2002-13 The R Core Team.
47442 hornik 17
This is free software; see the GNU General Public License version 2
18
or later for copying conditions.  There is NO warranty."
19
 
39282 urbaneks 20
usage="
21
Usage: R CMD javareconf [options]
22
 
23
Detect current Java setup and update the corresponding configuration in R.
24
 
25
Options:
41185 urbaneks 26
  -h, --help     print this help message and exit
47442 hornik 27
  -v, --version  print version info and exit
39282 urbaneks 28
  -n, --dry-run  perform Java detection, but don't touch any
61924 ripley 29
                 configuration files
41185 urbaneks 30
  -e <prog>      same as -n but exports all detected variables
31
                 and runs <prog>. If -e is the last argument
32
                 or <prog> is '' then a shell is used instead
39282 urbaneks 33
  xxx=yyy        evaluate the corresponding expression
34
                 (e.g. JAVA_HOME=/usr/lib/java)
35
 
36
Environment variables that can be used to influence the detection:
37
  JAVA           path to a Java interpreter executable
38
                 By default first 'java' command found on the PATH
39
                 is taken (unless JAVA_HOME is also specified).
40
  JAVA_HOME      home of the Java environment. If not specified,
41
                 it will be detected automatically from the Java
42
                 interpreter.
39782 urbaneks 43
  JAVAC          path to a Java compiler
44
  JAVAH          path to a Java header/stub generator
45
  JAR            path to a Java archive tool
39282 urbaneks 46
 
41169 urbaneks 47
The following variables should be used with extreme caution. They
48
must all match, so use only if you have a very special setup that
49
javareconf cannot detect automatically:
50
  JAVA_LD_LIBRARY_PATH library path necessary at run-time
41185 urbaneks 51
  JAVA_CPPFLAGS  C preprocessor flags necessary to compile JNI programs
41169 urbaneks 52
  JAVA_LIBS      libraries (as linker flags) necessary to compile
53
                 JNI programs
54
 
58676 ripley 55
Report bugs at bugs.r-project.org ."
39282 urbaneks 56
 
57
 
58
dry_run=no
41185 urbaneks 59
export_vars=no
39282 urbaneks 60
while test -n "${1}"; do
61
    case ${1} in
62
	-help | --help | -h) echo "${usage}"; exit 0 ;;
47442 hornik 63
	-v | --version) echo "${version}"; exit 0 ;;
39282 urbaneks 64
	-n | --dry-run) dry_run=yes ;;
57430 urbaneks 65
	-e) dry_run=yes; export_vars=yes; run_prog="${2}"; if test "$#" != 1; then shift; fi ;;
39282 urbaneks 66
	*=*) eval ${1} ;;
67
    esac
68
    shift
69
done
70
 
71
## find java compiler binaries
72
if test -z "${JAVA_HOME}" ; then
73
    JAVA_PATH=${PATH}
74
else
52710 ripley 75
    if test ! -d "${JAVA_HOME}"; then
39282 urbaneks 76
	echo "*** JAVA_HOME is not a valid path, ignoring"
77
	JAVA_HOME=
43163 urbaneks 78
	JAVA_PATH=${PATH}
79
    else
80
	## try jre/bin first just in case we don't have full JDK
81
	JAVA_PATH=${JAVA_HOME}:${JAVA_HOME}/jre/bin:${JAVA_HOME}/bin:${JAVA_HOME}/../bin:${PATH}
39282 urbaneks 82
    fi
83
fi
84
## if 'java' is not on the PATH or JAVA_HOME, add some guesses as of
85
## where java could live
43163 urbaneks 86
JAVA_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/bin
39282 urbaneks 87
 
88
if test -z "$JAVA"; then
89
    save_IFS=$IFS; IFS=:
90
    for dir in ${JAVA_PATH}; do
91
	if test -f "$dir/java"; then
92
	    JAVA="$dir/java"
39285 urbaneks 93
	    break
39282 urbaneks 94
	fi
95
    done
39285 urbaneks 96
    IFS=${save_IFS}
39282 urbaneks 97
fi
98
 
99
if test -z "$JAVA"; then
43235 ripley 100
    echo "~*** Cannot find any Java interpreter~*** Please make sure 'java' is on your PATH or set JAVA_HOME correspondingly"|${SED-sed} -e 'y/~/\n/' >&2
39282 urbaneks 101
    exit 1
102
fi
103
 
104
echo "Java interpreter : $JAVA"
105
jires=`$JAVA -classpath ${tools_classpath} getsp -test`
106
if test "$jires" != "Test1234OK"; then
107
    echo "$jires"
43235 ripley 108
    echo "~*** Java interpreter doesn't work properly.~"|${SED-sed} -e 'y/~/\n/' >&2
39282 urbaneks 109
    exit 1
110
fi
111
 
112
if test -z "${JAVA_HOME}"; then
113
    JAVA_HOME=`$JAVA -classpath ${tools_classpath} getsp java.home`
114
fi
115
 
116
if test -z "${JAVA_HOME}"; then
43235 ripley 117
    echo "~*** Cannot find Java environemnt.~*** Please set JAVA_HOME correspondingly.~"|${SED-sed} -e 'y/~/\n/' >&2
39282 urbaneks 118
    exit 1
119
fi
120
 
121
echo "Java version     : `$JAVA -classpath ${tools_classpath} getsp java.version 2>/dev/null`"
122
echo "Java home path   : ${JAVA_HOME}"
123
 
39782 urbaneks 124
# optional parts - compiler, javah and jar
125
if test -z "$JAVAC"; then
126
    save_IFS=$IFS; IFS=:
127
    for dir in ${JAVA_PATH}; do
128
	if test -f "$dir/javac"; then
129
	    JAVAC="$dir/javac"
130
	    break
131
	fi
132
    done
133
    IFS=${save_IFS}
134
fi
135
 
61937 ripley 136
: ${TMPDIR=/tmp}
62851 ripley 137
{ tempdir=`(mktemp -d -q "${TMPDIR}/Rjavareconf.XXXXXX") 2>/dev/null` \
46462 pd 138
	&& test -n "${tempdir}" && test -d "${tempdir}" ; } ||
62851 ripley 139
{ test -n "${RANDOM}" && tempdir=${TMPDIR}/Rjavareconf.$$-${RANDOM} \
46462 pd 140
	&& (mkdir "${tempdir}") ; } ||
62851 ripley 141
{ tempdir=${TMPDIR}/Rjavareconf.$$-`date +%m%d%H%M%S` \
46462 pd 142
	&& (mkdir "${tempdir}"); } ||
62851 ripley 143
{ tempdir=${TMPDIR}/Rjavareconf.$$ && (mkdir "${tempdir}") ; } ||
46462 pd 144
    (error "cannot create temporary directory" && exit 1)
145
 
61937 ripley 146
# test functionality of the compiler
147
javac_works='not present'
148
if test -n "$JAVAC"; then
149
    javac_works='not functional'
46459 pd 150
    echo "public class A { }" > ${tempdir}/A.java
51821 ripley 151
    if test -f ${tempdir}/A.java; then
46459 pd 152
	if "${JAVAC}" ${tempdir}/A.java >/dev/null; then
51821 ripley 153
           if test -f ${tempdir}/A.class; then
39782 urbaneks 154
		javac_works=yes
155
	    fi
156
	fi
157
    fi
158
fi
159
if test "${javac_works}" = yes; then
160
    echo "Java compiler    : ${JAVAC}"
161
else
162
    echo "Java compiler    : ${javac_works}"
163
fi
164
 
165
if test -z "$JAVAH"; then
166
    save_IFS=$IFS; IFS=:
167
    for dir in ${JAVA_PATH}; do
168
	if test -f "$dir/javah"; then
169
	    JAVAH="$dir/javah"
170
	    break
171
	fi
172
    done
173
    IFS=${save_IFS}
174
fi
175
echo "Java headers gen.: ${JAVAH}"
176
 
177
if test -z "$JAR"; then
178
    save_IFS=$IFS; IFS=:
179
    for dir in ${JAVA_PATH}; do
180
	if test -f "$dir/jar"; then
181
	    JAR="$dir/jar"
182
	    break
183
	fi
184
    done
185
    IFS=${save_IFS}
186
fi
187
echo "Java archive tool: ${JAR}"
188
 
41169 urbaneks 189
: ${JAVA_LIBS=~autodetect~}
190
: ${JAVA_CPPFLAGS=~autodetect~}
191
: ${JAVA_LD_LIBRARY_PATH=~autodetect~}
192
custom_JAVA_LIBS="${JAVA_LIBS}"
193
custom_JAVA_CPPFLAGS="${JAVA_CPPFLAGS}"
194
custom_JAVA_LD_LIBRARY_PATH="${JAVA_LD_LIBRARY_PATH}"
39782 urbaneks 195
 
41169 urbaneks 196
 
39782 urbaneks 197
# sys-dependent tweaks to JNI flags
39282 urbaneks 198
hostos=`uname 2>/dev/null`
62022 ripley 199
if test "${hostos}" = "Darwin"; then
61924 ripley 200
     pref=`echo "${JAVA_HOME}" | grep "/Home$"`
201
     if test "${pref}" = "${JAVA_HOME}"; then 
62022 ripley 202
       echo "System Java on OS X"
203
       JAVA_CPPFLAGS="-I/System/Library/Frameworks/JavaVM.framework/Headers"
61924 ripley 204
       JAVA_LIBS="-framework JavaVM"
205
       JAVA_LD_LIBRARY_PATH=
57316 urbaneks 206
	has_libjvm=unknown
61924 ripley 207
    else
208
       echo "Non-system Java on OS X"
209
    fi
62022 ripley 210
fi
61930 ripley 211
## we now look for a path to put in R_LD_LIBRARY_PATH which will
212
## enable libjvm to be found at run time.  This should not be needed,
213
## but it seems that libjvm is often not in the ld.so cache.
61924 ripley 214
if test "${JAVA_LIBS}" = "~autodetect~"; then
39282 urbaneks 215
	## we need to reset LD_LIBRARY_PATH, because Java automatically
216
	## adds all LD_LIBRARY_PATH to Java path
217
	save_LDLP=${LD_LIBRARY_PATH}
218
	LD_LIBRARY_PATH=
219
	export LD_LIBRARY_PATH
57316 urbaneks 220
	# remove trailing/leading : (if any)
221
	java_library_path=`"$JAVA" -classpath "${tools_classpath}" getsp java.library.path| ${SED-sed} -e 's/:$//' -e 's/^://'`
61930 ripley 222
	## see if libjvm is on this path: it is not for recent Oracle Java
57316 urbaneks 223
	has_libjvm=no
224
	save_IFS=$IFS; IFS=:
225
	for dir in ${java_library_path}; do
61888 ripley 226
	    if test -f "$dir/libjvm${DYLIB_EXT}"; then
57316 urbaneks 227
		has_libjvm=yes
61930 ripley 228
		java_library_path="${dir}"
57316 urbaneks 229
		break
230
	    fi
231
	done
61930 ripley 232
	# Then try some heuristics using sun.boot.library.path
57316 urbaneks 233
	if test ${has_libjvm} = no; then
234
	    boot_path=`"$JAVA" -classpath "${tools_classpath}" getsp sun.boot.library.path| ${SED-sed} -e 's/:$//' -e 's/^://'`
235
	    if test -n "${boot_path}"; then
236
		for dir in "${boot_path}" "${boot_path}/client" "${boot_path}/server"; do
61888 ripley 237
		    if test -f "$dir/libjvm${DYLIB_EXT}"; then
57316 urbaneks 238
			has_libjvm=yes
61930 ripley 239
			java_library_path="${dir}"
57316 urbaneks 240
			break
241
		    fi
242
		done
243
	    fi
244
	fi	
245
	IFS=${save_IFS}
246
	# replace occurrences of JAVA_HOME with $(JAVA_HOME)
247
	JAVA_LD_LIBRARY_PATH=`echo "${java_library_path}" | ${SED-sed} -e" s:${JAVA_HOME}:\$\(JAVA_HOME\):g"`
248
 
39282 urbaneks 249
	LD_LIBRARY_PATH=${save_LDLP}
250
	export LD_LIBRARY_PATH
251
	# build JAVA_LIBS from the detected path
43235 ripley 252
	JAVA_LIBS=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's|:| -L|g'`
39282 urbaneks 253
	if test -n "${JAVA_LIBS}"; then JAVA_LIBS="-L${JAVA_LIBS}"; fi
254
	JAVA_LIBS="${JAVA_LIBS} -ljvm"
255
	# create shell-version to be used in scripts
43235 ripley 256
	JAVA_LD_LIBRARY_PATH_SH=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's:$(JAVA_HOME):\$\{JAVA_HOME\}:g'`
41169 urbaneks 257
 
258
        ## includes consist of two parts - jni.h and machine-dependent jni_md.h
259
        jinc=''
43163 urbaneks 260
	JAVA_CPPFLAGS=''
41169 urbaneks 261
        for pinc in include ../include jre/include; do
51821 ripley 262
          if test -f "${JAVA_HOME}/${pinc}/jni.h"; then jinc="${JAVA_HOME}/${pinc}"; break; fi
41169 urbaneks 263
        done
264
        ## only if we get jni.h we can try to find jni_md.h
265
        if test -n "${jinc}"; then
266
           JAVA_CPPFLAGS="-I${jinc}"
43163 urbaneks 267
           jmdinc=''
41169 urbaneks 268
           jmdirs=''
269
	   ## we are not in configure, so we need to find the OS from R
270
	   host_os=`echo 'cat(R.version$os)'|${R_HOME}/bin/R --vanilla --slave 2>/dev/null`
271
           ## put the most probable locations for each system in the first place
272
           case "${host_os}" in
62022 ripley 273
             darwin*)  jmdirs=darwin;;
274
             linux*)   jmdirs=linux;;
41169 urbaneks 275
             bsdi*)    jmdirs=bsdos;;
276
             osf*)     jmdirs=alpha;;
62022 ripley 277
             solaris*) jmdirs=solaris;;
41169 urbaneks 278
             freebsd*) jmdirs=freebsd; add_java_libs='-lpthread';;
279
           esac
280
	   ## in case host_os detection failed, add all candidates
281
	   if test -z "${jmdirs}"; then jmdirs='linux solaris freebsd alpha bsdos'; fi
282
           ## prepend . and append less-likely ones
283
           jmdirs=". ${jmdirs} genunix ppc x86 iris hp-ux aix win32 cygwin openbsd"
284
           for pimd in ${jmdirs}; do
51821 ripley 285
             if test -f "${jinc}/${pimd}/jni_md.h"; then jmdinc="${jinc}/${pimd}"; break; fi
41169 urbaneks 286
           done
287
           if test -z "${jmdinc}"; then
288
             # ultima-ratio: use find and pray that it works
44692 ripley 289
             jmdinc=`find "${jinc}/" -name jni_md.h 2> /dev/null |head -n 1 2>/dev/null`
41169 urbaneks 290
             if test -n "${jmdinc}"; then jmdinc=`dirname "${jmdinc}"`; fi
291
           fi
292
           if test -n "${jmdinc}"; then
293
             if test "${jmdinc}" != "${jinc}/."; then
294
               JAVA_CPPFLAGS="${JAVA_CPPFLAGS} -I${jmdinc}"
295
             fi
296
           fi
43235 ripley 297
	   JAVA_CPPFLAGS=`echo ${JAVA_CPPFLAGS} | ${SED-sed} -e s:${JAVA_HOME}:\$\(JAVA_HOME\):g`
41169 urbaneks 298
        fi
61924 ripley 299
fi
39282 urbaneks 300
 
41169 urbaneks 301
if test -n "${add_java_libs}"; then JAVA_LIBS="${JAVA_LIBS} ${add_java_libs}"; fi
302
 
303
## honor user overrides
304
acx_custom_java_libs=no
305
if test "${custom_JAVA_LIBS}" != '~autodetect~'; then
306
  JAVA_LIBS="${custom_JAVA_LIBS}"
43235 ripley 307
  JAVA_LIBS0=`echo ${JAVA_LIBS} | ${SED-sed} -e s:${JAVA_HOME}:\$\(JAVA_HOME\):g`
41169 urbaneks 308
  acx_custom_java_libs=yes
309
fi
310
if test "${custom_JAVA_CPPFLAGS}" != '~autodetect~'; then
311
  JAVA_CPPFLAGS="${custom_JAVA_CPPFLAGS}"
312
fi
313
if test "${custom_JAVA_LD_LIBRARY_PATH}" != '~autodetect~'; then
314
  JAVA_LD_LIBRARY_PATH="${custom_JAVA_LD_LIBRARY_PATH}"
43235 ripley 315
  JAVA_LD_LIBRARY_PATH_SH=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's:$(JAVA_HOME):\$\{JAVA_HOME\}:g'`
41169 urbaneks 316
fi
317
 
61937 ripley 318
## Do cpmpile test in the temporary dir
319
wd=${PWD}
320
cd ${tempdir}
321
 
61911 ripley 322
echo
323
echo "trying to compile and link a JNI progam "
324
echo "detected JNI cpp flags    : ${JAVA_CPPFLAGS}"
325
echo "detected JNI linker flags : ${JAVA_LIBS}"
39282 urbaneks 326
 
61911 ripley 327
cat <<_ACEOF >conftest.c
328
#include <jni.h>
329
 
330
int main(void) {
331
    JNI_CreateJavaVM(0, 0, 0);
332
    return 0;
333
}
334
_ACEOF
335
 
336
JAVA_CPPFLAGS1=`echo ${JAVA_CPPFLAGS}|${SED-sed} -e 's:$(JAVA_HOME):'"${JAVA_HOME}:g"`
337
JAVA_LIBS1=`echo ${JAVA_LIBS}|${SED-sed} -e 's:$(JAVA_HOME):'"${JAVA_HOME}:g"`
338
cat <<_ACEOF >Makevars
339
PKG_CPPFLAGS = ${JAVA_CPPFLAGS1}
340
PKG_LIBS = ${JAVA_LIBS1}
341
_ACEOF
342
 
61970 ripley 343
## in case e.g. -k was passed in the environment
344
export MAKEFLAGS=
61911 ripley 345
${R_HOME}/bin/R CMD SHLIB conftest.c
346
ac_status=$?
347
 
348
if test $ac_status != 0; then
349
  echo "Unable to compile a JNI program"
350
  JAVA_LD_LIBRARY_PATH=
351
  JAVA_LIBS=
352
  JAVA_CPPFLAGS=
353
fi
354
 
61937 ripley 355
rm -f conftest.c conftest.o conftest.so Makevars
356
cd ${wd}
357
rm -rf ${tempdir}
61911 ripley 358
 
359
 
39282 urbaneks 360
echo ""
361
 
41185 urbaneks 362
if test "${export_vars}" = yes; then
57430 urbaneks 363
    ## we have to re-substitute $(JAVA_HOME), otherwise it will be replaced by R's
364
    ## Makeconf setting which is highly likely different and thus not intended
365
    JAVA_LD_LIBRARY_PATH=`echo ${JAVA_LD_LIBRARY_PATH}|${SED-sed} -e 's:$(JAVA_HOME):'"${JAVA_HOME}:g"`
366
    JAVA_CPPFLAGS=`echo ${JAVA_CPPFLAGS}|${SED-sed} -e 's:$(JAVA_HOME):'"${JAVA_HOME}:g"`
367
    JAVA_LIBS=`echo ${JAVA_LIBS}|${SED-sed} -e 's:$(JAVA_HOME):'"${JAVA_HOME}:g"`
368
 
41185 urbaneks 369
    export JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATH
370
    echo "The following Java variables have been exported:"
371
    echo "JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATH"
372
    test -z "${run_prog}" && test -n "${SHELL}" && run_prog="${SHELL}"
373
    test -z "${run_prog}" && run_prog=/bin/sh
57430 urbaneks 374
    echo "Running: ${run_prog}"
41185 urbaneks 375
    ${run_prog}
376
    exit 0
377
fi
378
 
61911 ripley 379
echo ""
380
echo "Java library path: ${JAVA_LD_LIBRARY_PATH}"
381
echo "JNI cpp flags    : ${JAVA_CPPFLAGS}"
382
echo "JNI linker flags : ${JAVA_LIBS}"
383
 
41169 urbaneks 384
test "${dry_run}" = yes && exit 0
39282 urbaneks 385
 
386
echo "Updating Java configuration in ${R_HOME}"
387
 
61945 ripley 388
## We update both the base and arch-specific versions as etc/Makefile
61947 ripley 389
## copies the first to the second (only the second is used).
61945 ripley 390
if test -n "${R_ARCH}"; then
391
  files="${R_HOME}/etc/Makeconf ${R_HOME}/etc/ldpaths ${R_HOME}/etc${R_ARCH}/Makeconf ${R_HOME}/etc${R_ARCH}/ldpaths"
392
else
393
  files="${R_HOME}/etc/Makeconf ${R_HOME}/etc/ldpaths"
394
fi
39282 urbaneks 395
for file in $files; do
51642 ripley 396
    ${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|" -e "s|JAVA_CPPFLAGS =.\{0,\}|JAVA_CPPFLAGS = ${JAVA_CPPFLAGS}|g"  "${file}" > "${file}.new"
39282 urbaneks 397
    if test -f "${file}.new"; then
398
	mv "${file}" "${file}.old"
399
	mv "${file}.new" "${file}"
39284 urbaneks 400
    else
43235 ripley 401
	echo "*** cannot create ${file}.new~*** Please run as root if required.~" | ${SED-sed} -e 'y/~/\n/' >&2
39284 urbaneks 402
	exit 1
39282 urbaneks 403
    fi
404
done
405
 
406
echo "Done."
41169 urbaneks 407
echo ''
63241 ripley 408
 
409
### Local Variables: ***
410
### mode: sh ***
411
### sh-indentation: 2 ***
412
### End: ***