diff options
19 files changed, 1502 insertions, 0 deletions
diff --git a/make/build.xml b/make/build.xml index ab538be..884c499 100755 --- a/make/build.xml +++ b/make/build.xml @@ -96,6 +96,425 @@ </antlr> </target> + <!-- Use GlueGen to generate the source code for the NativeLibrary + implementation. Note that to make the build process simpler (in + particular, the nightly builds) we do not do this every time we + run the build, but instead check in the generated sources to + the source tree. --> + + <property name="gluegen.root" value="../" /> + <import file="gluegen-cpptasks.xml" /> + <target name="generate.nativelibrary.sources" depends="init" > + <!-- Define the appropriate include paths --> + <dirset id="stub.includes.fileset.windows" dir="."> + <include name="stub_includes/windows/**" /> + </dirset> + <dirset id="stub.includes.fileset.unix" dir="."> + <include name="stub_includes/unix/**" /> + </dirset> + <dirset id="stub.includes.fileset.macosx" dir="."> + <include name="stub_includes/macosx/**" /> + </dirset> + <!-- Define the classpath we should use --> + <path id="gluegen.classpath" > + <pathelement location="${classes}" /> + <pathelement location="${antlr.jar}" /> + </path> + <!-- Define the GlueGen task we just built --> + <taskdef name="gluegen" classname="com.sun.gluegen.ant.GlueGenTask" + classpathref="gluegen.classpath" /> + + <!-- Execute it against all of the OSs' header files --> + <gluegen src="stub_includes/windows/dynamic-linker.h" + config="dynlink-windows.cfg" + includeRefId="stub.includes.fileset.windows" + emitter="com.sun.gluegen.JavaEmitter"> + <classpath refid="gluegen.classpath" /> + </gluegen> + <gluegen src="stub_includes/unix/dynamic-linker.h" + config="dynlink-unix.cfg" + includeRefId="stub.includes.fileset.unix" + emitter="com.sun.gluegen.JavaEmitter"> + <classpath refid="gluegen.classpath" /> + </gluegen> + <gluegen src="stub_includes/macosx/dynamic-linker.h" + config="dynlink-macosx.cfg" + includeRefId="stub.includes.fileset.macosx" + emitter="com.sun.gluegen.JavaEmitter"> + <classpath refid="gluegen.classpath" /> + </gluegen> + </target> + + <!-- ================================================================== --> + <!-- + - Set up java.home.dir appropriately on all platforms. + --> + <target name="setup.java.home.dir.nonmacosx" depends="load.user.properties" unless="isOSX"> + <!-- java home dir is up one directory as java.home points to '<java-install-dir>/jre' --> + <property name="java.home.dir" value="${java.home}/.." /> + </target> + <target name="setup.java.home.dir.macosx" depends="load.user.properties" if="isOSX"> + <property name="java.home.dir" value="/System/Library/Frameworks/JavaVM.framework/Home" /> + </target> + <target name="setup.java.home.dir" depends="setup.java.home.dir.nonmacosx,setup.java.home.dir.macosx" > + <property name="java.includes.dir" value="${java.home.dir}/include" /> + </target> + + <!-- ================================================================== --> + <!-- + - Compile the native C code for GlueGen's dynamic linker interface. + --> + + <target name="declare.win32.vc6" if="isVC6"> + <echo message="Win32.VC6" /> + <property name="compiler.cfg.id" value="compiler.cfg.win32.msvc" /> + <property name="linker.cfg.id" value="linker.cfg.win32.msvc" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-windows-i586.jar" /> + </target> + + <target name="declare.win32.vc7" if="isVC7"> + <echo message="Win32.VC7" /> + <property name="compiler.cfg.id" value="compiler.cfg.win32.msvc" /> + <property name="linker.cfg.id" value="linker.cfg.win32.msvc" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-windows-i586.jar" /> + </target> + + <target name="declare.win32.vc8" if="isVC8"> + <echo message="Win32.VC8" /> + <property name="compiler.cfg.id" value="compiler.cfg.win32.msvc" /> + <property name="linker.cfg.id" value="linker.cfg.win32.msvc" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-windows-i586.jar" /> + </target> + + <target name="declare.win32.vc8_x64" if="isVC8_X64"> + <echo message="Win32.VC8_X64" /> + <property name="compiler.cfg.id" value="compiler.cfg.win32.msvc" /> + <property name="linker.cfg.id" value="linker.cfg.win32.msvc" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-windows-amd64.jar" /> + </target> + + <target name="declare.win32.mingw" if="isMingW"> + <echo message="Win32.MingW" /> + <property name="compiler.cfg.id" value="compiler.cfg.win32.mingw" /> + <property name="linker.cfg.id" value="linker.cfg.win32.mingw" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-windows-i586.jar" /> + </target> + + <target name="declare.win32" depends="declare.win32.vc6,declare.win32.vc7,declare.win32.vc8,declare.win32.vc8_x64,declare.win32.mingw" if="isWindows" > + <property name="c.src.dir" value="windows" /> + <property name="java.includes.dir.platform" value="${java.includes.dir}/win32" /> + <property name="gluegen.rt.natives.dso" value="gluegen-rt.dll" /> + </target> + + <target name="declare.linux.x86" if="isLinuxX86"> + <echo message="Linux.x86" /> + <property name="compiler.cfg.id" value="compiler.cfg.linux" /> + <property name="linker.cfg.id" value="linker.cfg.linux" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-linux-i586.jar" /> + </target> + + <target name="declare.linux.amd64" if="isLinuxAMD64"> + <echo message="Linux.AMD64" /> + <property name="compiler.cfg.id" value="compiler.cfg.linux.amd64" /> + <property name="linker.cfg.id" value="linker.cfg.linux.amd64" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-linux-amd64.jar" /> + </target> + + <target name="declare.linux.ia64" if="isLinuxIA64"> + <echo message="Linux.IA64" /> + <property name="compiler.cfg.id" value="compiler.cfg.linux" /> + <property name="linker.cfg.id" value="linker.cfg.linux" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-linux-ia64.jar" /> + </target> + + <target name="declare.linux" depends="declare.linux.x86,declare.linux.amd64,declare.linux.ia64" if="isLinux" > + <property name="c.src.dir" value="unix" /> + <property name="java.includes.dir.platform" value="${java.includes.dir}/linux" /> + <property name="gluegen.rt.natives.dso" value="libgluegen-rt.so" /> + </target> + + <target name="declare.solaris32" if="isSolaris32Bit"> + <echo message="Solaris" /> + <property name="compiler.cfg.id" value="compiler.cfg.solaris" /> + <property name="linker.cfg.id" value="linker.cfg.solaris" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-solaris-${os.arch}.jar" /> + </target> + + <target name="declare.solarisSparcv9" if="isSolarisSparcv9"> + <echo message="SolarisSparcv9" /> + <property name="compiler.cfg.id" value="compiler.cfg.solarisSparcv9" /> + <property name="linker.cfg.id" value="linker.cfg.solarisSparcv9" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-solaris-sparcv9.jar" /> + </target> + + <target name="declare.solarisAMD64" if="isSolarisAMD64"> + <echo message="SolarisAMD64" /> + <property name="compiler.cfg.id" value="compiler.cfg.solarisAMD64" /> + <property name="linker.cfg.id" value="linker.cfg.solarisAMD64" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-solaris-amd64.jar" /> + </target> + + <target name="declare.solaris" depends="declare.solaris32,declare.solarisSparcv9,declare.solarisAMD64" if="isSolaris" > + <property name="c.src.dir" value="unix" /> + <property name="java.includes.dir.platform" value="${java.includes.dir}/solaris" /> + <property name="gluegen.rt.natives.dso" value="libgluegen-rt.so" /> + </target> + + <target name="declare.macosx.ppc" unless="macosxfat"> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-macosx-ppc.jar" /> + </target> + + <target name="declare.macosx.universal" if="macosxfat"> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-macosx-universal.jar" /> + </target> + + <target name="declare.macosx" depends="declare.macosx.ppc,declare.macosx.universal" if="isOSX"> + <echo message="MacOSX" /> + <property name="compiler.cfg.id" value="compiler.cfg.macosx" /> + <property name="linker.cfg.id" value="linker.cfg.macosx" /> + <property name="c.src.dir" value="macosx" /> + <property name="java.includes.dir.platform" value="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Headers" /> + <property name="gluegen.rt.natives.dso" value="libgluegen-rt.jnilib" /> + </target> + + <target name="declare.freebsd" if="isFreeBSD"> + <echo message="FreeBSD" /> + <property name="compiler.cfg.id" value="compiler.cfg.freebsd" /> + <property name="linker.cfg.id" value="linker.cfg.linux" /> + <property name="c.src.dir" value="unix" /> + <property name="java.includes.dir.freebsd" value="${java.includes.dir}/freebsd" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-freebsd-i586.jar" /> + <property name="gluegen.rt.natives.dso" value="libgluegen-rt.so" /> + </target> + + <target name="declare.hpux" if="isHPUX"> + <echo message="HP-UX" /> + <property name="compiler.cfg.id" value="compiler.cfg.hpux" /> + <property name="linker.cfg.id" value="linker.cfg.hpux" /> + <property name="c.src.dir" value="unix" /> + <property name="java.includes.dir.hpux" value="${java.includes.dir}/hp-ux" /> + <property name="gluegen.rt.natives.jar" value="gluegen-rt-natives-hpux-hppa.jar" /> + <property name="gluegen.rt.natives.dso" value="libgluegen-rt.so" /> + </target> + + <target name="c.configure" depends="gluegen.cpptasks.detect.os,gluegen.cpptasks.detect.compiler,setup.java.home.dir,declare.win32,declare.linux,declare.solaris,declare.macosx,declare.freebsd,declare.hpux" unless="build.javaonly" > + <!-- compiler configuration --> + <compiler id="compiler.cfg.linux" name="gcc" /> + + <compiler id="compiler.cfg.linux.amd64" name="gcc"> + <compilerarg value="-fPIC"/> + </compiler> + + <compiler id="compiler.cfg.solaris" name="suncc"> + </compiler> + + <compiler id="compiler.cfg.solarisSparcv9" name="suncc"> + <compilerarg value="-fast" /> + <compilerarg value="-xchip=ultra" /> + <compilerarg value="-xarch=v9a" /> + <compilerarg value="-xcode=32" /> + </compiler> + + <compiler id="compiler.cfg.solarisAMD64" name="suncc"> + <compilerarg value="-fast" /> + <compilerarg value="-xchip=opteron" /> + <compilerarg value="-xarch=amd64" /> + <!-- <compilerarg value="-xcache=64/64/2:1024/64/16" /> --> + </compiler> + + <compiler id="compiler.cfg.win32.mingw" name="gcc"> + <defineset> + <define name="_DEBUG" if="c.compiler.use-debug"/> + <define name="DEBUG" if="c.compiler.use-debug"/> + <define name="NDEBUG" unless="c.compiler.use-debug"/> + + <define name="DBUILD_DLL" /> + <define name="_WINGDI_" /> + <define name="_STRICT_ANSI" /> + <define name="_JNI_IMPLEMENTATION_" /> + </defineset> + </compiler> + + <compiler id="compiler.cfg.win32.msvc" name="msvc"> + <compilerarg value="/W3"/> <!-- set warning level --> + <compilerarg value="/Ob1"/> <!-- inline only functions marked inline --> + <compilerarg value="/GF"/> <!-- enable string pooling --> + <compilerarg value="/Gy"/> <!-- enable function level linking --> + <compilerarg value="/GS" if="isVC7"/> <!-- buffer security checks --> + <compilerarg value="/Wp64" if="isVC7"/> <!-- detect 64-bit port problems --> + <compilerarg value="/RTCcsu" if="isVC7"/> <!-- various runtime checks --> + <!-- Note: previous compiler options for VC7 were: + Debug: /MDd /Yd /GS /RTCs /RTCu /RTCc /W3 /Od /GF /EHsc /Zi /GS /Gy /Wp64 /Zi /D "_DEBUG" + Optimized: /MD /W3 /O2 /Ob1 /GF /EHsc /GS /Gy /Wp64 /D "NDEBUG" --> + <compilerarg value="/GS" if="isVC8"/> <!-- buffer security checks --> + <compilerarg value="/Wp64" if="isVC8"/> <!-- detect 64-bit port problems --> + <compilerarg value="/RTCcsu" if="isVC8"/> <!-- various runtime checks --> + + <defineset> + <define name="_DEBUG" if="c.compiler.use-debug"/> + <define name="DEBUG" if="c.compiler.use-debug"/> + <define name="NDEBUG" unless="c.compiler.use-debug"/> + </defineset> + </compiler> + + <compiler id="compiler.cfg.macosx" name="gcc"> + <compilerarg value="-arch" if="macosxfat"/> + <compilerarg value="ppc" if="macosxfat"/> + <compilerarg value="-arch" if="macosxfat"/> + <compilerarg value="i386" if="macosxfat"/> + <compilerarg value="-Wmost" /> + <compilerarg value="-ObjC" /> + <defineset> + <define name="macosx" /> + </defineset> + </compiler> + + <compiler id="compiler.cfg.freebsd" name="gcc" /> + + <compiler id="compiler.cfg.hpux" name="aCC"> + <!-- Interpret source as ANSI C89 (not C++) --> + <compilerarg value="-Ae"/> + <!-- Dereferences are performed with four-byte loads and stores. --> + <compilerarg value="+u4"/> + <!-- Suppress warnings: + 942: signed/unsigned assignment compatibility + 129: redeclarations in stub_includes --> + <compilerarg value="+W942,129"/> + <defineset> + <!-- aCC doesn't seem to define a generic system macro, a la + "__sun". The documentation claims it defines the following, + but don't seem to work: __HP_aCC, __LP64__, __RISC_0__. + HP-UX docs on JNI say to use the defs below + (http://www.hp.com/products1/unix/java/infolibrary/prog_guide/JNI_java2.html) --> + <!-- Single underscore prefix, as recommended by aCC docs... --> + <define name="_HPUX"/> + <define name="_POSIX_C_SOURCE" value="199506L"/> + </defineset> + </compiler> + + <!-- linker configuration --> + + <linker id="linker.cfg.linux" name="gcc" /> + + <linker id="linker.cfg.linux.amd64" name="gcc" /> + + <linker id="linker.cfg.solaris" name="suncc" /> + + <linker id="linker.cfg.solarisSparcv9" name="suncc"> + <linkerarg value="-xarch=v9a" /> + </linker> + + <linker id="linker.cfg.solarisAMD64" name="suncc"> + <linkerarg value="-xarch=amd64" /> + </linker> + + <linker id="linker.cfg.win32.mingw" name="gcc" incremental="false"> + <linkerarg value="-Wl,--kill-at" /> <!-- remove @ from function names --> + </linker> + + <linker id="linker.cfg.win32.msvc" name="msvc" incremental="false"> + <linkerarg value="/OPT:REF,ICF" /> <!-- enable link-time optimisations --> + <linkerarg value="/SUBSYSTEM:WINDOWS" /> <!-- output is not a console app as uses WinMain entry point --> + <linkerarg value="/MACHINE:X86" if="isVC6" /> <!-- explicity set target platform --> + <linkerarg value="/MACHINE:X86" if="isVC7" /> <!-- explicity set target platform --> + <linkerarg value="/MACHINE:X86" if="isVC8" /> <!-- explicity set target platform --> + <linkerarg value="/MACHINE:X64" if="isVC8_X64"/> <!-- explicity set target platform (IX64) --> + </linker> + + <linker id="linker.cfg.macosx" name="gcc"> + <linkerarg value="-arch" if="macosxfat"/> + <linkerarg value="ppc" if="macosxfat"/> + <linkerarg value="-arch" if="macosxfat"/> + <linkerarg value="i386" if="macosxfat"/> + </linker> + + <linker id="linker.cfg.hpux" name="aCC" /> + + <!-- Common properties --> + <property name="java.includes.dir" value="${java.home.dir}/include" /> <!-- NOTE: this MUST be relative for FileSet --> + + <property name="c.compiler.src.files" value="src/native/${c.src.dir}/*.c" /> + + <property name="output.lib.name" value="gluegen-rt" /> + </target> + + <target name="c.rename.lib.mingw" if="isMingW"> + <!-- FIXME: this is a hack; the cpptask should have an option to change the + suffix or at least understand the override from .so to .dll --> + <move file="../build/obj/libgluegen-rt.so" tofile="../build/obj/gluegen-rt.dll" /> + </target> + + <target name="c.rename.lib.macosx" if="isOSX"> + <!-- FIXME: this is a hack; the cpptask should have an option to change the + suffix or at least understand the override from dylib to jnilib --> + <move file="../build/obj/libgluegen-rt.dylib" tofile="../build/obj/libgluegen-rt.jnilib" /> + </target> + + <target name="c.build" depends="c.configure" unless="build.javaonly" > + <fail message="Requires '${c.compiler.src.files}'" unless="c.compiler.src.files"/> + <fail message="Requires '${compiler.cfg.id}'" unless="compiler.cfg.id"/> + <fail message="Requires '${linker.cfg.id}'" unless="linker.cfg.id"/> + <fail message="Requires '${output.lib.name}'" unless="output.lib.name"/> + + <echo message="Output lib name = ${output.lib.name}" /> + + <!-- NOTE: the value of the debug and optimise attributes will not be overridden if already set externally --> + <property name="c.compiler.debug" value="false" /> + <!-- Optimise flags one of { none, size, speed, minimal, full, aggressive, extreme, unsafe } --> + <property name="c.compiler.optimise" value="none" /> + + <condition property="c.compiler.use-debug"><istrue value="${c.compiler.debug}"/></condition> + + <mkdir dir="../build/obj" /> + + <echo message="Compiling ${c.compiler.src.files}" /> + <echo message="user.dir=${user.dir}" /> + + <cc outtype="shared" + objdir="../build/obj" + outfile="../build/obj/${output.lib.name}" + optimize="${c.compiler.optimise}" + debug="${c.compiler.debug}" + multithreaded="true" + exceptions="false" + rtti="false"> + + <!-- TODO: versioninfo companyname="java.net" + legalcopyright="Copyright" + productname="GlueGen" + productversion="x.y.z" + description="Description" + fileversion="x.y.z" + filecomments="File Comment" /--> + + <fileset dir=".."> + <patternset> + <include name="${c.compiler.src.files}" /> + </patternset> + </fileset> + + <compiler extends="${compiler.cfg.id}" > + <sysincludepath path="${java.includes.dir}"/> + <sysincludepath path="${java.includes.dir.platform}"/> + </compiler> + + <linker extends="${linker.cfg.id}" /> + </cc> + + <antcall target="c.rename.lib.mingw" inheritRefs="true" /> + <antcall target="c.rename.lib.macosx" inheritRefs="true" /> + <!-- Create Java Web Start jar file from built file --> + <jar destfile="../build/${gluegen.rt.natives.jar}"> + <fileset dir="../build/obj"> + <include name="${gluegen.rt.natives.dso}" /> + </fileset> + </jar> + </target> + + <target name="build.nativelibrary" if="build.native" > + <antcall target="c.build" inheritRefs="true" /> + </target> + <!-- ================================================================== --> <!-- - Build GlueGen. @@ -146,6 +565,10 @@ <classpath refid="antlr.classpath" /> </javac> + <!-- Build the (native code) NativeLibrary support for + gluegen-rt.jar using the just-built GlueGen. --> + <antcall target="build.nativelibrary" inheritRefs="true" /> + <!-- Build gluegen.jar. --> <jar destfile="${build}/gluegen.jar" manifest="Manifest"> <fileset dir="${classes}"> diff --git a/make/dynlink-macosx.cfg b/make/dynlink-macosx.cfg new file mode 100755 index 0000000..8ba4c51 --- /dev/null +++ b/make/dynlink-macosx.cfg @@ -0,0 +1,25 @@ +Style AllStatic +JavaClass MacOSXDynamicLinkerImpl +Package com.sun.gluegen.runtime +Implements MacOSXDynamicLinkerImpl DynamicLinker +JavaOutputDir ../src/java +NativeOutputDir ../src/native/macosx +HierarchicalNativeOutput false + +# Note that we can use the same CustomJavaCode as the unix +# implementation, but some of the constants (e.g. RTLD_GLOBAL) resolve +# to different values +IncludeAs CustomJavaCode MacOSXDynamicLinkerImpl dynlink-unix-CustomJavaCode.java +CustomCCode #include <dlfcn.h> +CustomCCode #include </usr/include/machine/types.h> + +ArgumentIsString dlopen 0 +ArgumentIsString dlsym 1 +ReturnsString dlerror +Opaque long void* + +# Hide the platform-specific functions +AccessControl dlopen PRIVATE +AccessControl dlsym PRIVATE +AccessControl dlclose PRIVATE +AccessControl dlerror PRIVATE diff --git a/make/dynlink-unix-CustomJavaCode.java b/make/dynlink-unix-CustomJavaCode.java new file mode 100755 index 0000000..36a1603 --- /dev/null +++ b/make/dynlink-unix-CustomJavaCode.java @@ -0,0 +1,18 @@ +public long openLibrary(String pathname) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_GLOBAL); +} + +public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); +} + +public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); +} diff --git a/make/dynlink-unix.cfg b/make/dynlink-unix.cfg new file mode 100755 index 0000000..d762c8f --- /dev/null +++ b/make/dynlink-unix.cfg @@ -0,0 +1,22 @@ +Style AllStatic +JavaClass UnixDynamicLinkerImpl +Package com.sun.gluegen.runtime +Implements UnixDynamicLinkerImpl DynamicLinker +JavaOutputDir ../src/java +NativeOutputDir ../src/native/unix +HierarchicalNativeOutput false + +IncludeAs CustomJavaCode UnixDynamicLinkerImpl dynlink-unix-CustomJavaCode.java +CustomCCode #include <dlfcn.h> +CustomCCode #include <inttypes.h> + +ArgumentIsString dlopen 0 +ArgumentIsString dlsym 1 +ReturnsString dlerror +Opaque long void* + +# Hide the platform-specific functions +AccessControl dlopen PRIVATE +AccessControl dlsym PRIVATE +AccessControl dlclose PRIVATE +AccessControl dlerror PRIVATE diff --git a/make/dynlink-windows-CustomJavaCode.java b/make/dynlink-windows-CustomJavaCode.java new file mode 100755 index 0000000..986ec81 --- /dev/null +++ b/make/dynlink-windows-CustomJavaCode.java @@ -0,0 +1,11 @@ +public long openLibrary(String libraryName) { + return LoadLibraryA(libraryName); +} + +public long lookupSymbol(long libraryHandle, String symbolName) { + return GetProcAddress(libraryHandle, symbolName); +} + +public void closeLibrary(long libraryHandle) { + FreeLibrary(libraryHandle); +} diff --git a/make/dynlink-windows.cfg b/make/dynlink-windows.cfg new file mode 100755 index 0000000..871326e --- /dev/null +++ b/make/dynlink-windows.cfg @@ -0,0 +1,25 @@ +Style AllStatic +JavaClass WindowsDynamicLinkerImpl +Package com.sun.gluegen.runtime +Implements WindowsDynamicLinkerImpl DynamicLinker +JavaOutputDir ../src/java +NativeOutputDir ../src/native/windows +HierarchicalNativeOutput false + +IncludeAs CustomJavaCode WindowsDynamicLinkerImpl dynlink-windows-CustomJavaCode.java +CustomCCode #include <windows.h> +CustomCCode /* This typedef is only needed for VC6 */ +CustomCCode #if _MSC_VER <= 1200 +CustomCCode typedef int intptr_t; +CustomCCode #endif + +ArgumentIsString LoadLibraryA 0 +ArgumentIsString GetProcAddress 1 +Opaque long HANDLE +Opaque long PROC + +# Hide the platform-specific functions +AccessControl GetLastError PRIVATE +AccessControl LoadLibraryA PRIVATE +AccessControl GetProcAddress PRIVATE +AccessControl FreeLibrary PRIVATE diff --git a/make/gluegen.properties b/make/gluegen.properties index 9ca1f06..81aba60 100755 --- a/make/gluegen.properties +++ b/make/gluegen.properties @@ -15,3 +15,15 @@ antlr.jar=C:/Users/kbr/ANTLR/antlr-2.7.2/antlr.jar # antlr.jar=/Users/kbr/antlr-2.7.2/antlr.jar # Solaris # antlr.jar=/export/kbr/ANTLR/antlr-2.7.2/antlr.jar + +# If you are building the native code for the GlueGen run-time library +# on Windows and have the Microsoft Visual C++ compilers installed, +# you can choose an alternate compiler with which to build the native +# code. Valid strings here are "vc6", "vc7", "vc8", "vc8_x64", and +# "mingw". +win32.c.compiler=vc6 + +# If you are building the native code for the GlueGen run-time library +# on a Mac OS X system supporting cross-compilation and want to +# generate fat (PPC and x86) binaries, uncomment the property below +# macosxfat=true diff --git a/make/stub_includes/macosx/dynamic-linker.h b/make/stub_includes/macosx/dynamic-linker.h new file mode 100755 index 0000000..11afcdc --- /dev/null +++ b/make/stub_includes/macosx/dynamic-linker.h @@ -0,0 +1,46 @@ +/* Portions extracted from Mac OS X dlfcn.h */ + +/* + * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +/* + Based on the dlcompat work done by: + Jorge Acereda <[email protected]> & + Peter O'Gorman <[email protected]> +*/ + +extern int dlclose(void * __handle); +extern char * dlerror(void); +extern void * dlopen(const char * __path, int __mode); +extern void * dlsym(void * __handle, const char * __symbol); + +#define RTLD_LAZY 0x1 +#define RTLD_NOW 0x2 +#define RTLD_LOCAL 0x4 +#define RTLD_GLOBAL 0x8 + +/* + * Special handle arguments for dlsym(). + */ +#define RTLD_NEXT -1LL /* Search subsequent objects. */ +#define RTLD_DEFAULT -2LL /* Use default search algorithm. */ diff --git a/make/stub_includes/unix/dynamic-linker.h b/make/stub_includes/unix/dynamic-linker.h new file mode 100755 index 0000000..7071438 --- /dev/null +++ b/make/stub_includes/unix/dynamic-linker.h @@ -0,0 +1,47 @@ +/* Portions extracted from Solaris dlfcn.h */ + +/* + * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * Copyright (c) 1989 AT&T + * All Rights Reserved + * + */ + +/* + * Valid values for handle argument to dlsym(3x). + */ +#define RTLD_NEXT -1LL /* look in `next' dependency */ +#define RTLD_DEFAULT -2LL /* look up symbol from scope */ + /* of current object */ +#define RTLD_SELF -3LL /* look in `ourself' */ +#define RTLD_PROBE -4LL /* look up symbol from scope */ + /* of current object, */ + /* using currently */ + /* loaded objects only. */ + +/* + * Valid values for mode argument to dlopen. + */ +#define RTLD_LAZY 0x00001 /* deferred function binding */ +#define RTLD_NOW 0x00002 /* immediate function binding */ +#define RTLD_NOLOAD 0x00004 /* don't load object */ + +#define RTLD_GLOBAL 0x00100 /* export symbols to others */ +#define RTLD_LOCAL 0x00000 /* symbols are only available */ + /* to group members */ +#define RTLD_PARENT 0x00200 /* add parent (caller) to */ + /* a group dependencies */ +#define RTLD_GROUP 0x00400 /* resolve symbols within */ + /* members of the group */ +#define RTLD_WORLD 0x00800 /* resolve symbols within */ + /* global objects */ +#define RTLD_NODELETE 0x01000 /* do not remove members */ +#define RTLD_FIRST 0x02000 /* only first object is */ + /* available for dlsym */ + +extern void *dlopen(const char *, int); +extern void *dlsym(void *, const char *); +extern int dlclose(void *); +extern char *dlerror(void); diff --git a/make/stub_includes/windows/dynamic-linker.h b/make/stub_includes/windows/dynamic-linker.h new file mode 100755 index 0000000..e8a8bd5 --- /dev/null +++ b/make/stub_includes/windows/dynamic-linker.h @@ -0,0 +1,31 @@ +/* Windows #defines and typedefs required for processing of extracts + from WINDOWS.H */ + +#define FAR +#define WINBASEAPI +#define WINAPI +#define CONST const +#define VOID void +typedef int BOOL; +typedef unsigned char BYTE; +typedef unsigned int DWORD; +typedef int INT; +typedef int INT32; +typedef __int64 INT64; +typedef float FLOAT; +typedef struct _handle* HANDLE; +typedef HANDLE HMODULE; +typedef long LONG; +typedef const char* LPCSTR; +typedef void* LPVOID; +typedef struct _proc* PROC; +typedef unsigned int* PUINT; +typedef unsigned int UINT; +typedef unsigned short USHORT; +typedef unsigned short WORD; + +// Dynamic loading routines +WINBASEAPI DWORD WINAPI GetLastError(VOID); +WINBASEAPI HMODULE WINAPI LoadLibraryA(LPCSTR lpLibFileName); +WINBASEAPI PROC WINAPI GetProcAddress(HMODULE hModule, LPCSTR lpProcName); +WINBASEAPI BOOL WINAPI FreeLibrary(HMODULE hLibModule); diff --git a/src/java/com/sun/gluegen/runtime/DynamicLinker.java b/src/java/com/sun/gluegen/runtime/DynamicLinker.java new file mode 100755 index 0000000..d0ee39b --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/DynamicLinker.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.gluegen.runtime; + +/** Provides an abstract interface to the OS's low-level dynamic + linking functionality. */ + +interface DynamicLinker { + public long openLibrary(String pathname); + public long lookupSymbol(long libraryHandle, String symbolName); + public void closeLibrary(long libraryHandle); +} diff --git a/src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java b/src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java new file mode 100755 index 0000000..37e3cd0 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java @@ -0,0 +1,49 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:27:00 PDT 2006 ----! */ + +package com.sun.gluegen.runtime; + +import com.sun.gluegen.runtime.*; + +public class MacOSXDynamicLinkerImpl implements DynamicLinker +{ + + public static final int RTLD_LAZY = 0x1; + public static final int RTLD_NOW = 0x2; + public static final int RTLD_LOCAL = 0x4; + public static final int RTLD_GLOBAL = 0x8; + + /** Interface to C language function: <br> <code> int dlclose(void * __handle); </code> */ + private static native int dlclose(long __handle); + + /** Interface to C language function: <br> <code> char * dlerror(void); </code> */ + private static native java.lang.String dlerror(); + + /** Interface to C language function: <br> <code> void * dlopen(const char * __path, int __mode); </code> */ + private static native long dlopen(java.lang.String __path, int __mode); + + /** Interface to C language function: <br> <code> void * dlsym(void * __handle, const char * __symbol); </code> */ + private static native long dlsym(long __handle, java.lang.String __symbol); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibrary(String pathname) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_GLOBAL); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class MacOSXDynamicLinkerImpl diff --git a/src/java/com/sun/gluegen/runtime/NativeLibLoader.java b/src/java/com/sun/gluegen/runtime/NativeLibLoader.java new file mode 100755 index 0000000..4d57867 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/NativeLibLoader.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.gluegen.runtime; + +import java.security.*; + +/** Class providing control over whether GlueGen loads the native code + associated with the NativeLibrary implementation. Alternative app + launchers such as those running within applets may want to disable + this default loading behavior and load the native code via another + (manual) mechanism. */ +public class NativeLibLoader { + private static volatile boolean loadingEnabled = true; + private static volatile boolean didLoading; + + public static void disableLoading() { + loadingEnabled = false; + } + + public static void enableLoading() { + loadingEnabled = true; + } + + public static void loadGlueGenRT() { + if (!didLoading && loadingEnabled) { + synchronized (NativeLibLoader.class) { + if (!didLoading && loadingEnabled) { + didLoading = true; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + System.loadLibrary("gluegen-rt"); + return null; + } + }); + } + } + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/NativeLibrary.java b/src/java/com/sun/gluegen/runtime/NativeLibrary.java new file mode 100755 index 0000000..f61afb6 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/NativeLibrary.java @@ -0,0 +1,305 @@ +/* + * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.gluegen.runtime; + +import java.io.*; +import java.lang.reflect.*; +import java.security.*; +import java.util.*; + +/** Provides low-level, relatively platform-independent access to + shared ("native") libraries. The core library routines + <code>System.load()</code> and <code>System.loadLibrary()</code> + in general provide suitable functionality for applications using + native code, but are not flexible enough to support certain kinds + of glue code generation and deployment strategies. This class + supports direct linking of native libraries to other shared + objects not necessarily installed on the system (in particular, + via the use of dlopen(RTLD_GLOBAL) on Unix platforms) as well as + manual lookup of function names to support e.g. GlueGen's + ProcAddressTable glue code generation style without additional + supporting code needed in the generated library. */ + +public class NativeLibrary { + private static final int WINDOWS = 1; + private static final int UNIX = 2; + private static final int MACOSX = 3; + private static int platform; + private static DynamicLinker dynLink; + private static String prefix; + private static String[] suffixes; + + static { + // Determine platform we're running on + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.startsWith("wind")) { + platform = WINDOWS; + } else if (osName.startsWith("mac os x")) { + platform = MACOSX; + } else { + platform = UNIX; + } + return null; + } + }); + // Instantiate dynamic linker implementation + switch (platform) { + case WINDOWS: + dynLink = new WindowsDynamicLinkerImpl(); + prefix = ""; + suffixes = new String[] { ".dll" }; + break; + case UNIX: + dynLink = new UnixDynamicLinkerImpl(); + prefix = "lib"; + suffixes = new String[] { ".so" }; + break; + case MACOSX: + dynLink = new MacOSXDynamicLinkerImpl(); + prefix = "lib"; + suffixes = new String[] { ".dylib", ".jnilib" }; + break; + default: + throw new InternalError("Platform not initialized properly"); + } + } + + // Platform-specific representation for the handle to the open + // library. This is an HMODULE on Windows and a void* (the result of + // a dlopen() call) on Unix and Mac OS X platforms. + private long libraryHandle; + + // May as well keep around the path to the library we opened + private String libraryPath; + + // Private constructor to prevent arbitrary instances from floating around + private NativeLibrary(long libraryHandle, String libraryPath) { + this.libraryHandle = libraryHandle; + this.libraryPath = libraryPath; + } + + /** Opens the given native library, assuming it has the same base + name on all platforms, in the context of the specified + ClassLoader, which is used to help find the library in the case + of e.g. Java Web Start. */ + public static NativeLibrary open(String libName, ClassLoader loader) { + return open(libName, libName, libName, loader); + } + + /** Opens the given native library, assuming it has the given base + names (no "lib" prefix or ".dll/.so/.dylib" suffix) on the + Windows, Unix and Mac OS X platforms, respectively, and in the + context of the specified ClassLoader, which is used to help find + the library in the case of e.g. Java Web Start. */ + public static NativeLibrary open(String windowsLibName, + String unixLibName, + String macOSXLibName, + ClassLoader loader) { + List possiblePaths = enumerateLibraryPaths(windowsLibName, + unixLibName, + macOSXLibName, + loader); + // Iterate down these and see which one if any we can actually find. + for (Iterator iter = possiblePaths.iterator(); iter.hasNext(); ) { + String path = (String) iter.next(); + ensureNativeLibLoaded(); + long res = dynLink.openLibrary(path); + if (res != 0) { + return new NativeLibrary(res, path); + } + } + // For now, just return null to indicate the open operation didn't + // succeed (could also throw an exception if we could tell which + // of the openLibrary operations actually failed) + return null; + } + + /** Looks up the given function name in this native library. */ + public long lookupFunction(String functionName) { + if (libraryHandle == 0) + throw new RuntimeException("Library is not open"); + return dynLink.lookupSymbol(libraryHandle, functionName); + } + + /** Retrieves the low-level library handle from this NativeLibrary + object. On the Windows platform this is an HMODULE, and on Unix + and Mac OS X platforms the void* result of calling dlopen(). */ + public long getLibraryHandle() { + return libraryHandle; + } + + /** Retrieves the path under which this library was opened. */ + public String getLibraryPath() { + return libraryPath; + } + + /** Closes this native library. Further lookup operations are not + allowed after calling this method. */ + public void close() { + if (libraryHandle == 0) + throw new RuntimeException("Library already closed"); + long handle = libraryHandle; + libraryHandle = 0; + dynLink.closeLibrary(handle); + } + + /** Given the base library names (no prefixes/suffixes) for the + various platforms, enumerate the possible locations and names of + the indicated native library on the system. */ + private static List enumerateLibraryPaths(String windowsLibName, + String unixLibName, + String macOSXLibName, + ClassLoader loader) { + String libName = selectName(windowsLibName, unixLibName, macOSXLibName); + String[] baseNames = buildNames(libName); + + List paths = new ArrayList(); + + // The idea to ask the ClassLoader to find the library is borrowed + // from the LWJGL library + String clPath = getPathFromClassLoader(libName, loader); + if (clPath != null) + paths.add(clPath); + + // Add entries from java.library.path + String javaLibraryPath = + (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("java.library.path"); + } + }); + if (javaLibraryPath != null) { + StringTokenizer tokenizer = new StringTokenizer(javaLibraryPath, File.pathSeparator); + while (tokenizer.hasMoreTokens()) { + addPaths(tokenizer.nextToken(), baseNames, paths); + } + } + + // Add current working directory + String userDir = + (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("user.dir"); + } + }); + addPaths(userDir, baseNames, paths); + + // Add just the library names to use the OS's search algorithm + for (int i = 0; i < baseNames.length; i++) { + paths.add(baseNames[i]); + } + + return paths; + } + + + private static String selectName(String windowsLibName, + String unixLibName, + String macOSXLibName) { + switch (platform) { + case WINDOWS: + return windowsLibName; + case UNIX: + return unixLibName; + case MACOSX: + return macOSXLibName; + default: + throw new InternalError(); + } + } + + private static String[] buildNames(String libName) { + String[] res = new String[suffixes.length]; + for (int i = 0; i < suffixes.length; i++) { + res[i] = prefix + libName + suffixes[i]; + } + return res; + } + + private static void addPaths(String path, String[] baseNames, List paths) { + for (int j = 0; j < baseNames.length; j++) { + paths.add(path + File.separator + baseNames[j]); + } + } + + private static boolean initializedFindLibraryMethod = false; + private static Method findLibraryMethod = null; + private static String getPathFromClassLoader(final String libName, ClassLoader loader) { + if (loader == null) + return null; + if (!initializedFindLibraryMethod) { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + findLibraryMethod = ClassLoader.class.getDeclaredMethod("findLibrary", + new Class[] { String.class }); + findLibraryMethod.setAccessible(true); + } catch (Exception e) { + // Fail silently disabling this functionality + } + initializedFindLibraryMethod = true; + return null; + } + }); + } + if (findLibraryMethod != null) { + try { + return (String) findLibraryMethod.invoke(loader, new Object[] { libName }); + } catch (Exception e) { + // Fail silently and continue with other search algorithms + } + } + return null; + } + + private static volatile boolean loadedDynLinkNativeLib; + private static void ensureNativeLibLoaded() { + if (!loadedDynLinkNativeLib) { + synchronized (NativeLibrary.class) { + if (!loadedDynLinkNativeLib) { + loadedDynLinkNativeLib = true; + NativeLibLoader.loadGlueGenRT(); + } + } + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java b/src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java new file mode 100755 index 0000000..006f90d --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java @@ -0,0 +1,55 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +package com.sun.gluegen.runtime; + +import com.sun.gluegen.runtime.*; + +public class UnixDynamicLinkerImpl implements DynamicLinker +{ + + public static final int RTLD_LAZY = 0x00001; + public static final int RTLD_NOW = 0x00002; + public static final int RTLD_NOLOAD = 0x00004; + public static final int RTLD_GLOBAL = 0x00100; + public static final int RTLD_LOCAL = 0x00000; + public static final int RTLD_PARENT = 0x00200; + public static final int RTLD_GROUP = 0x00400; + public static final int RTLD_WORLD = 0x00800; + public static final int RTLD_NODELETE = 0x01000; + public static final int RTLD_FIRST = 0x02000; + + /** Interface to C language function: <br> <code> int dlclose(void * ); </code> */ + private static native int dlclose(long arg0); + + /** Interface to C language function: <br> <code> char * dlerror(void); </code> */ + private static native java.lang.String dlerror(); + + /** Interface to C language function: <br> <code> void * dlopen(const char * , int); </code> */ + private static native long dlopen(java.lang.String arg0, int arg1); + + /** Interface to C language function: <br> <code> void * dlsym(void * , const char * ); </code> */ + private static native long dlsym(long arg0, java.lang.String arg1); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibrary(String pathname) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_GLOBAL); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class UnixDynamicLinkerImpl diff --git a/src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java b/src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java new file mode 100755 index 0000000..c1d2fa0 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java @@ -0,0 +1,38 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +package com.sun.gluegen.runtime; + +import com.sun.gluegen.runtime.*; + +public class WindowsDynamicLinkerImpl implements DynamicLinker +{ + + + /** Interface to C language function: <br> <code> BOOL FreeLibrary(HANDLE hLibModule); </code> */ + private static native int FreeLibrary(long hLibModule); + + /** Interface to C language function: <br> <code> DWORD GetLastError(void); </code> */ + private static native int GetLastError(); + + /** Interface to C language function: <br> <code> PROC GetProcAddress(HANDLE hModule, LPCSTR lpProcName); </code> */ + private static native long GetProcAddress(long hModule, java.lang.String lpProcName); + + /** Interface to C language function: <br> <code> HANDLE LoadLibraryA(LPCSTR lpLibFileName); </code> */ + private static native long LoadLibraryA(java.lang.String lpLibFileName); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibrary(String libraryName) { + return LoadLibraryA(libraryName); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return GetProcAddress(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + FreeLibrary(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class WindowsDynamicLinkerImpl diff --git a/src/native/macosx/MacOSXDynamicLinkerImpl_JNI.c b/src/native/macosx/MacOSXDynamicLinkerImpl_JNI.c new file mode 100755 index 0000000..2b0a2a7 --- /dev/null +++ b/src/native/macosx/MacOSXDynamicLinkerImpl_JNI.c @@ -0,0 +1,89 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:27:00 PDT 2006 ----! */ + +#include <jni.h> + +#include <assert.h> + + #include <dlfcn.h> + #include </usr/include/machine/types.h> + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.MacOSXDynamicLinkerImpl + * Java method: int dlclose(long __handle) + * C function: int dlclose(void * __handle); + */ +JNIEXPORT jint JNICALL +Java_com_sun_gluegen_runtime_MacOSXDynamicLinkerImpl_dlclose__J(JNIEnv *env, jclass _unused, jlong __handle) { + int _res; + _res = dlclose((void *) (intptr_t) __handle); + return _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.MacOSXDynamicLinkerImpl + * Java method: java.lang.String dlerror() + * C function: char * dlerror(void); + */ +JNIEXPORT jstring JNICALL +Java_com_sun_gluegen_runtime_MacOSXDynamicLinkerImpl_dlerror__(JNIEnv *env, jclass _unused) { + char * _res; + _res = dlerror(); + if (_res == NULL) return NULL; return (*env)->NewStringUTF(env, _res); +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.MacOSXDynamicLinkerImpl + * Java method: long dlopen(java.lang.String __path, int __mode) + * C function: void * dlopen(const char * __path, int __mode); + */ +JNIEXPORT jlong JNICALL +Java_com_sun_gluegen_runtime_MacOSXDynamicLinkerImpl_dlopen__Ljava_lang_String_2I(JNIEnv *env, jclass _unused, jstring __path, jint __mode) { + const char* _UTF8__path = NULL; + void * _res; + if (__path != NULL) { + if (__path != NULL) { + _UTF8__path = (*env)->GetStringUTFChars(env, __path, (jboolean*)NULL); + if (_UTF8__path == NULL) { + (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), + "Failed to get UTF-8 chars for argument \"__path\" in native dispatcher for \"dlopen\""); + return 0; + } + } + } + _res = dlopen((char *) _UTF8__path, (int) __mode); + if (__path != NULL) { + (*env)->ReleaseStringUTFChars(env, __path, _UTF8__path); + } + return (jlong) (intptr_t) _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.MacOSXDynamicLinkerImpl + * Java method: long dlsym(long __handle, java.lang.String __symbol) + * C function: void * dlsym(void * __handle, const char * __symbol); + */ +JNIEXPORT jlong JNICALL +Java_com_sun_gluegen_runtime_MacOSXDynamicLinkerImpl_dlsym__JLjava_lang_String_2(JNIEnv *env, jclass _unused, jlong __handle, jstring __symbol) { + const char* _UTF8__symbol = NULL; + void * _res; + if (__symbol != NULL) { + if (__symbol != NULL) { + _UTF8__symbol = (*env)->GetStringUTFChars(env, __symbol, (jboolean*)NULL); + if (_UTF8__symbol == NULL) { + (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), + "Failed to get UTF-8 chars for argument \"__symbol\" in native dispatcher for \"dlsym\""); + return 0; + } + } + } + _res = dlsym((void *) (intptr_t) __handle, (char *) _UTF8__symbol); + if (__symbol != NULL) { + (*env)->ReleaseStringUTFChars(env, __symbol, _UTF8__symbol); + } + return (jlong) (intptr_t) _res; +} + + diff --git a/src/native/unix/UnixDynamicLinkerImpl_JNI.c b/src/native/unix/UnixDynamicLinkerImpl_JNI.c new file mode 100755 index 0000000..6d72646 --- /dev/null +++ b/src/native/unix/UnixDynamicLinkerImpl_JNI.c @@ -0,0 +1,89 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +#include <jni.h> + +#include <assert.h> + + #include <dlfcn.h> + #include <inttypes.h> + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.UnixDynamicLinkerImpl + * Java method: int dlclose(long arg0) + * C function: int dlclose(void * ); + */ +JNIEXPORT jint JNICALL +Java_com_sun_gluegen_runtime_UnixDynamicLinkerImpl_dlclose__J(JNIEnv *env, jclass _unused, jlong arg0) { + int _res; + _res = dlclose((void *) (intptr_t) arg0); + return _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.UnixDynamicLinkerImpl + * Java method: java.lang.String dlerror() + * C function: char * dlerror(void); + */ +JNIEXPORT jstring JNICALL +Java_com_sun_gluegen_runtime_UnixDynamicLinkerImpl_dlerror__(JNIEnv *env, jclass _unused) { + char * _res; + _res = dlerror(); + if (_res == NULL) return NULL; return (*env)->NewStringUTF(env, _res); +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.UnixDynamicLinkerImpl + * Java method: long dlopen(java.lang.String arg0, int arg1) + * C function: void * dlopen(const char * , int); + */ +JNIEXPORT jlong JNICALL +Java_com_sun_gluegen_runtime_UnixDynamicLinkerImpl_dlopen__Ljava_lang_String_2I(JNIEnv *env, jclass _unused, jstring arg0, jint arg1) { + const char* _UTF8arg0 = NULL; + void * _res; + if (arg0 != NULL) { + if (arg0 != NULL) { + _UTF8arg0 = (*env)->GetStringUTFChars(env, arg0, (jboolean*)NULL); + if (_UTF8arg0 == NULL) { + (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), + "Failed to get UTF-8 chars for argument \"arg0\" in native dispatcher for \"dlopen\""); + return 0; + } + } + } + _res = dlopen((char *) _UTF8arg0, (int) arg1); + if (arg0 != NULL) { + (*env)->ReleaseStringUTFChars(env, arg0, _UTF8arg0); + } + return (jlong) (intptr_t) _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.UnixDynamicLinkerImpl + * Java method: long dlsym(long arg0, java.lang.String arg1) + * C function: void * dlsym(void * , const char * ); + */ +JNIEXPORT jlong JNICALL +Java_com_sun_gluegen_runtime_UnixDynamicLinkerImpl_dlsym__JLjava_lang_String_2(JNIEnv *env, jclass _unused, jlong arg0, jstring arg1) { + const char* _UTF8arg1 = NULL; + void * _res; + if (arg1 != NULL) { + if (arg1 != NULL) { + _UTF8arg1 = (*env)->GetStringUTFChars(env, arg1, (jboolean*)NULL); + if (_UTF8arg1 == NULL) { + (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), + "Failed to get UTF-8 chars for argument \"arg1\" in native dispatcher for \"dlsym\""); + return 0; + } + } + } + _res = dlsym((void *) (intptr_t) arg0, (char *) _UTF8arg1); + if (arg1 != NULL) { + (*env)->ReleaseStringUTFChars(env, arg1, _UTF8arg1); + } + return (jlong) (intptr_t) _res; +} + + diff --git a/src/native/windows/WindowsDynamicLinkerImpl_JNI.c b/src/native/windows/WindowsDynamicLinkerImpl_JNI.c new file mode 100755 index 0000000..2b99098 --- /dev/null +++ b/src/native/windows/WindowsDynamicLinkerImpl_JNI.c @@ -0,0 +1,92 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +#include <jni.h> + +#include <assert.h> + + #include <windows.h> + /* This typedef is only needed for VC6 */ + #if _MSC_VER <= 1200 + typedef int intptr_t; + #endif + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.WindowsDynamicLinkerImpl + * Java method: int FreeLibrary(long hLibModule) + * C function: BOOL FreeLibrary(HANDLE hLibModule); + */ +JNIEXPORT jint JNICALL +Java_com_sun_gluegen_runtime_WindowsDynamicLinkerImpl_FreeLibrary__J(JNIEnv *env, jclass _unused, jlong hLibModule) { + BOOL _res; + _res = FreeLibrary((HANDLE) (intptr_t) hLibModule); + return _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.WindowsDynamicLinkerImpl + * Java method: int GetLastError() + * C function: DWORD GetLastError(void); + */ +JNIEXPORT jint JNICALL +Java_com_sun_gluegen_runtime_WindowsDynamicLinkerImpl_GetLastError__(JNIEnv *env, jclass _unused) { + DWORD _res; + _res = GetLastError(); + return _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.WindowsDynamicLinkerImpl + * Java method: long GetProcAddress(long hModule, java.lang.String lpProcName) + * C function: PROC GetProcAddress(HANDLE hModule, LPCSTR lpProcName); + */ +JNIEXPORT jlong JNICALL +Java_com_sun_gluegen_runtime_WindowsDynamicLinkerImpl_GetProcAddress__JLjava_lang_String_2(JNIEnv *env, jclass _unused, jlong hModule, jstring lpProcName) { + const char* _UTF8lpProcName = NULL; + PROC _res; + if (lpProcName != NULL) { + if (lpProcName != NULL) { + _UTF8lpProcName = (*env)->GetStringUTFChars(env, lpProcName, (jboolean*)NULL); + if (_UTF8lpProcName == NULL) { + (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), + "Failed to get UTF-8 chars for argument \"lpProcName\" in native dispatcher for \"GetProcAddress\""); + return 0; + } + } + } + _res = GetProcAddress((HANDLE) (intptr_t) hModule, (LPCSTR) _UTF8lpProcName); + if (lpProcName != NULL) { + (*env)->ReleaseStringUTFChars(env, lpProcName, _UTF8lpProcName); + } + return (jlong) (intptr_t) _res; +} + + +/* Java->C glue code: + * Java package: com.sun.gluegen.runtime.WindowsDynamicLinkerImpl + * Java method: long LoadLibraryA(java.lang.String lpLibFileName) + * C function: HANDLE LoadLibraryA(LPCSTR lpLibFileName); + */ +JNIEXPORT jlong JNICALL +Java_com_sun_gluegen_runtime_WindowsDynamicLinkerImpl_LoadLibraryA__Ljava_lang_String_2(JNIEnv *env, jclass _unused, jstring lpLibFileName) { + const char* _UTF8lpLibFileName = NULL; + HANDLE _res; + if (lpLibFileName != NULL) { + if (lpLibFileName != NULL) { + _UTF8lpLibFileName = (*env)->GetStringUTFChars(env, lpLibFileName, (jboolean*)NULL); + if (_UTF8lpLibFileName == NULL) { + (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), + "Failed to get UTF-8 chars for argument \"lpLibFileName\" in native dispatcher for \"LoadLibraryA\""); + return 0; + } + } + } + _res = LoadLibraryA((LPCSTR) _UTF8lpLibFileName); + if (lpLibFileName != NULL) { + (*env)->ReleaseStringUTFChars(env, lpLibFileName, _UTF8lpLibFileName); + } + return (jlong) (intptr_t) _res; +} + + |