Skip to content

Commit

Permalink
Temp work on handling JAVA_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
nberth committed Aug 20, 2024
1 parent eb021a0 commit 78c2fe5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 43 deletions.
4 changes: 0 additions & 4 deletions build_aux/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

2024-08-14 Nicolas Berthier <nicolas.berthier@ocamlpro.com>

* pre-inst-env.in: append JNI_LIBPATH to LIBRARY variables

2023-01-21 Simon Sobisch <simonsobisch@gnu.org>

* pre-inst-env.in: prefer config.status replacement over environment var
Expand Down
8 changes: 4 additions & 4 deletions build_aux/pre-inst-env.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ exec_prefix="@exec_prefix@"
COB_LIBS="@COB_LIBS@ ${COB_LIBS}"
COB_CONFIG_DIR="@abs_top_srcdir@/config"
COB_COPY_DIR="@abs_top_srcdir@/copy"
LD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${LD_LIBRARY_PATH}" # GNU/Linux, Solaris, ...
DYLD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${DYLD_LIBRARY_PATH}" # Mac OS
SHLIB_PATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${SHLIB_PATH}" # HP-UX
LIBPATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${LIBPATH}" # AIX
LD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:${LD_LIBRARY_PATH}" # GNU/Linux, Solaris, ...
DYLD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:${DYLD_LIBRARY_PATH}" # Mac OS
SHLIB_PATH="@abs_top_builddir@/libcob/.libs:${SHLIB_PATH}" # HP-UX
LIBPATH="@abs_top_builddir@/libcob/.libs:${LIBPATH}" # AIX

COB_LIBRARY_PATH="@abs_top_builddir@/extras:${COB_LIBRARY_PATH}" # only in pre-inst case: prepend

Expand Down
8 changes: 1 addition & 7 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -965,24 +965,19 @@ AS_IF([test "x$with_java" != "xno"], [
for _dir in "${JAVA_HOME}/jre/lib" "${JAVA_HOME}/lib"; do
if test -d "$_dir"; then
JNI_LIBS="$JNI_LIBS -L$_dir"
JNI_LIBPATH="$JNI_LIBPATH:$_dir"
fi
if test -d "$_dir/server"; then
JNI_LIBS="$JNI_LIBS -L$_dir/server"
JNI_LIBPATH="$JNI_LIBPATH:$_dir/server"
fi
done
curr_LIBS="$LIBS"
LIBS="$LIBS $JNI_LIBS"
AC_CHECK_LIB([jvm], [JNI_CreateJavaVM], [
AC_DEFINE([WITH_JNI], [1])
JNI_LDFLAGS="$JNI_LIBS"
JNI_LIBPATH="$JNI_LIBPATH:" dnl <- append a colon
JNI_LIBS="-ljvm"
cob_has_jni=yes
], [
JNI_LIBPATH=""])
])
LIBS="$curr_LIBS"
])
])
Expand Down Expand Up @@ -2719,7 +2714,6 @@ AC_SUBST([LMDB_CFLAGS])
AC_SUBST([JAVAC])
AC_SUBST([JNI_LIBS])
AC_SUBST([JNI_LIBPATH])
AC_SUBST([JNI_LDFLAGS])
AC_SUBST([JNI_CPPFLAGS])
Expand Down
87 changes: 76 additions & 11 deletions libcob/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ lt_dlsym (HMODULE hmod, const char *p)
return modun.voidptr;
}

#define lt_dlopenlcl(x) lt_dlopen(x)
#define lt_dlclose(x) FreeLibrary(x)
#define lt_dlopenlcl(x) lt_dlopen(x)
#define lt_dlclose(x) FreeLibrary(x)
#define lt_dlinit()
#define lt_dlexit()
#define lt_dlhandle HMODULE
#define lt_dlhandle HMODULE
#define lt_dladdsearchdir(x) AddDllDirectory(x)

#if 0 /* RXWRXW - dlerror */
static char errbuf[64];
Expand Down Expand Up @@ -123,6 +124,15 @@ lt_dlerror (void)

#endif

#if defined (_WIN32) || defined (USE_LIBDL)
/* Try pre-loading libjvm/jvm.dll if JAVA_HOME is set. */
# define JVM_PRELOAD 1
static lt_dlhandle jvm_handle = NULL;
#else
/* Using libltdl, no need to preload. */
# define JVM_PRELOAD 0
#endif

#include "sysdefines.h"

/* Force symbol exports */
Expand Down Expand Up @@ -1160,6 +1170,7 @@ cob_load_lib (const char *library, const char *entry, char *reason)
}
#endif

DEBUG_LOG ("call", ("lt_dlopenlcl '%s'\n", library));
p = lt_dlopenlcl (library);
if (p) {
p = lt_dlsym (p, entry);
Expand Down Expand Up @@ -1845,6 +1856,13 @@ cob_exit_call (void)
}
base_dynload_ptr = NULL;

#if JVM_PRELOAD
if (jvm_handle) {
lt_dlclose (jvm_handle);
jvm_handle = NULL;
}
#endif

#if !defined(_WIN32) && !defined(USE_LIBDL)
lt_dlexit ();
#if 0 /* RXWRXW - ltdl leak */
Expand Down Expand Up @@ -1976,19 +1994,64 @@ cob_init_call (cob_global *lptr, cob_settings* sptr, const int check_mainhandle)

/* Java API handling */

/* Note: also in fileio.c */

#if defined (__CYGWIN__)
#define LIB_PRF "cyg"
/* "Standard" path suffixes to the dynamically loadable JVM library, from
"typical" JAVA_HOME. */
const char* const path_to_jvm[] = {
#if defined(_WIN32) || defined(__CYGWIN__)
# define JVM_FILE "jvm.dll"
"\\jre\\bin\\server",
"\\jre\\bin\\client",
#else
#define LIB_PRF "lib"
# define JVM_FILE "libjvm.so"
"/lib/server",
"/lib/client",
#endif
NULL,
};

#if defined(_WIN32) || defined(__CYGWIN__)
#define LIB_SUF "-1." COB_MODULE_EXT
static void
init_jvm_search_dirs (void) {
const char *java_home;
const char *path_suffix = NULL;
char jvm_path[COB_FILE_MAX];
unsigned int i = 0;

if ((java_home = getenv ("JAVA_HOME")) == NULL) {
DEBUG_LOG ("call", ("JAVA_HOME is not defined\n"));
return;
}

DEBUG_LOG ("call", ("JAVA_HOME='%s'\n", java_home));

while ((path_suffix = path_to_jvm[i++]) != NULL) {
#if JVM_PRELOAD
/* Lookup libjvm.so/jvm.dll */
if (snprintf (jvm_path, (size_t)COB_FILE_MAX, "%s%s%c%s",
java_home, path_suffix,
SLASH_CHAR, JVM_FILE) == 0) {
continue;
}
if (access (jvm_path, F_OK) != 0) {
DEBUG_LOG ("call", ("'%s': not found\n", jvm_path));
continue;
}
DEBUG_LOG ("call", ("preloading '%s': ", jvm_path));
jvm_handle = lt_dlopen (jvm_path);
DEBUG_LOG ("call", ("%s\n", jvm_handle != NULL ? "success" : "failed"));
break;
#else
#define LIB_SUF "." COB_MODULE_EXT
/* Append to search path. */
# warning On some systems, JAVA_HOME-based lookup via `libltdl` does not work
if (snprintf (jvm_path, (size_t)COB_FILE_MAX, "%s%s",
java_home, path_suffix) == 0) {
continue;
}
DEBUG_LOG ("call", ("appending '%s' to load path: ", jvm_path));
int success = lt_dladdsearchdir (jvm_path);
DEBUG_LOG ("call", ("%s\n", success == 0 ? "success" : "failed"));
#endif
}
}

#define LIBCOBJNI_MODULE_NAME (LIB_PRF "cobjni" LIB_SUF)
#define LIBCOBJNI_ENTRY_NAME "cob_jni_init"
Expand All @@ -2002,6 +2065,8 @@ static int
cob_init_java (void) {
java_init_func jinit;

init_jvm_search_dirs ();

java_api = cob_malloc (sizeof (cob_java_api));
if (java_api == NULL) {
return 1;
Expand Down
13 changes: 0 additions & 13 deletions libcob/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,6 @@ static struct cob_fileio_funcs *fileio_funcs[COB_IO_MAX] = {
&not_available_funcs
};

#if defined (__CYGWIN__)
#define LIB_PRF "cyg"
#else
#define LIB_PRF "lib"
#endif

#if defined(_WIN32) || defined(__CYGWIN__)
#define LIB_SUF "-1." COB_MODULE_EXT
#else
#define LIB_SUF "." COB_MODULE_EXT
#endif

static struct {
int loaded; /* Module is loaded and ready */
int config; /* Module was configured into compiler */
Expand Down Expand Up @@ -9824,4 +9812,3 @@ cob_fork_fileio (cob_global *lptr, cob_settings *sptr)
}
}
}

12 changes: 12 additions & 0 deletions libcob/sysdefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@
#define SLASH_STR "\\"
#endif

#if defined (__CYGWIN__)
#define LIB_PRF "cyg"
#else
#define LIB_PRF "lib"
#endif

#if defined(_WIN32) || defined(__CYGWIN__)
#define LIB_SUF "-1." COB_MODULE_EXT
#else
#define LIB_SUF "." COB_MODULE_EXT
#endif

#ifdef __DJGPP__
#define HAVE_8DOT3_FILENAMES
#endif
Expand Down
8 changes: 4 additions & 4 deletions tests/atlocal.in
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ if test "$GNUCOBOL_ENV_SETUP" != "1" -a "$GNUCOBOL_TEST_LOCAL" != "1"; then
COB_LIBS="@COB_LIBS@"
COB_CONFIG_DIR="@abs_top_srcdir@/config"
COB_COPY_DIR="@abs_top_srcdir@/copy"
LD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${LD_LIBRARY_PATH}"
LD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:${LD_LIBRARY_PATH}"
PATH="@abs_top_builddir@/libcob/.libs:${PATH}"
DYLD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${DYLD_LIBRARY_PATH}"
SHLIB_PATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${SHLIB_PATH}"
LIBPATH="@abs_top_builddir@/libcob/.libs:@JNI_LIBPATH@${LIBPATH}"
DYLD_LIBRARY_PATH="@abs_top_builddir@/libcob/.libs:${DYLD_LIBRARY_PATH}"
SHLIB_PATH="@abs_top_builddir@/libcob/.libs:${SHLIB_PATH}"
LIBPATH="@abs_top_builddir@/libcob/.libs:${LIBPATH}"
COB_LIBRARY_PATH="@abs_top_builddir@/extras"

export COB_CFLAGS COB_LDFLAGS COB_LIBS
Expand Down

0 comments on commit 78c2fe5

Please sign in to comment.