1 # ---------------------------------------------------------------------------
3 # Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
4 # ---------------------------------------------------------------------------
5 # SquirrelJME is under the Mozilla Public License Version 2.0.
6 # See license.mkd for licensing and copyright information.
7 # ---------------------------------------------------------------------------
8 # DESCRIPTION: Attempts to find JNI and related headers
11 if(NOT DEFINED SQUIRRELJME_JNI_CMAKE_WHERE)
12 set(SQUIRRELJME_JNI_CMAKE_WHERE "${CMAKE_CURRENT_LIST_DIR}")
15 if(NOT DEFINED SQUIRRELJME_JNI_CMAKE_WHERE)
16 set(SQUIRRELJME_JNI_CMAKE_WHERE "${CMAKE_SOURCE_DIR}")
19 # Use standard JNI search
20 find_package(JNI QUIET)
22 # Used to remove any NOTFOUNDs from variables
23 macro(squirreljme_notfound_strip var)
24 unset(${var}-NOTFOUND)
25 unset(${var}-NOTFOUND CACHE)
27 if (${var} MATCHES "-NOTFOUND$")
32 if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13")
33 if("$CACHE{${var}}" MATCHES "-NOTFOUND$")
39 # Clear always the NOTFOUND variables, since these will cause CMake to
40 # just fail if it is missing
41 squirreljme_notfound_strip(JNI_INCLUDE_DIRS)
42 squirreljme_notfound_strip(JAVA_JVM_LIBRARY)
43 squirreljme_notfound_strip(JAVA_INCLUDE_PATH)
44 squirreljme_notfound_strip(JAVA_INCLUDE_PATH2)
45 squirreljme_notfound_strip(JAVA_AWT_LIBRARY)
46 squirreljme_notfound_strip(JAVA_AWT_INCLUDE_PATH)
48 # If found, use specific host variables then remove them all
50 set(HOST_JNI_FOUND "${JNI_FOUND}")
53 if(DEFINED JNI_INCLUDE_DIRS)
54 set(HOST_JNI_INCLUDE_DIRS "${JNI_INCLUDE_DIRS}")
55 unset(JNI_INCLUDE_DIRS)
58 if(DEFINED JAVA_INCLUDE_PATH)
59 set(HOST_JAVA_INCLUDE_PATH "${JAVA_INCLUDE_PATH}")
60 unset(JAVA_INCLUDE_PATH)
63 if(DEFINED JAVA_INCLUDE_PATH2)
64 set(HOST_JAVA_INCLUDE_PATH2 "${JAVA_INCLUDE_PATH2}")
65 unset(JAVA_INCLUDE_PATH2)
68 if(DEFINED JAVA_AWT_INCLUDE_PATH)
69 set(HOST_JAVA_AWT_INCLUDE_PATH "${JAVA_AWT_INCLUDE_PATH}")
70 unset(JAVA_AWT_INCLUDE_PATH)
73 if(DEFINED JAVA_JVM_LIBRARY)
74 set(HOST_JAVA_JVM_LIBRARY "${JAVA_JVM_LIBRARY}")
75 unset(JAVA_JVM_LIBRARY)
78 if(DEFINED JAVA_AWT_LIBRARY)
79 set(HOST_JAVA_AWT_LIBRARY "${JAVA_AWT_LIBRARY}")
80 unset(JAVA_AWT_LIBRARY)
84 # Check to see if the host can compile or not
87 try_compile(SQUIRRELJME_HOST_JVM_VALID
88 "${CMAKE_CURRENT_BINARY_DIR}"
89 SOURCES "${CMAKE_CURRENT_LIST_DIR}/tryJni.c"
90 CMAKE_FLAGS "-DCMAKE_TRY_COMPILE_TARGET_TYPE=EXECUTABLE"
91 LINK_LIBRARIES "${JAVA_JVM_LIBRARY}"
92 OUTPUT_VARIABLE SQUIRRELJME_HOST_JVM_VALID_DEBUG)
93 message(STATUS "JNI Valid?: ${SQUIRRELJME_HOST_JVM_VALID}")
94 message(DEBUG "${SQUIRRELJME_HOST_JVM_VALID_DEBUG}")
97 try_compile(SQUIRRELJME_HOST_AWT_VALID
98 "${CMAKE_CURRENT_BINARY_DIR}"
99 SOURCES "${CMAKE_CURRENT_LIST_DIR}/tryJawt.c"
100 CMAKE_FLAGS "-DCMAKE_TRY_COMPILE_TARGET_TYPE=EXECUTABLE"
101 LINK_LIBRARIES "${JAVA_JVM_LIBRARY}" "${JAVA_AWT_LIBRARY}"
102 OUTPUT_VARIABLE SQUIRRELJME_HOST_AWT_VALID_DEBUG)
103 message(STATUS "JAWT Valid?: ${SQUIRRELJME_HOST_AWT_VALID}")
104 message(DEBUG "${SQUIRRELJME_HOST_AWT_VALID_DEBUG}")
106 # It is possible that there are other libraries but there
107 # is no jni_md.h, which is a broken installation of JNI
108 find_file(SQUIRRELJME_HOST_JNI_MD "jni_md.h"
109 "${HOST_JNI_INCLUDE_DIRS}"
110 "${HOST_JAVA_INCLUDE_PATH}"
111 "${HOST_JAVA_INCLUDE_PATH2}")
112 if(SQUIRRELJME_HOST_JNI_MD)
113 set(SQUIRRELJME_HOST_JNI_MD_VALID YES)
115 set(SQUIRRELJME_HOST_JNI_MD_VALID NO)
117 message(STATUS "jni_md.h Valid?: ${SQUIRRELJME_HOST_JNI_MD_VALID}")
118 message(DEBUG "jni_md.h path: ${SQUIRRELJME_HOST_JNI_MD}")
120 set(SQUIRRELJME_HOST_JVM_VALID NO)
121 set(SQUIRRELJME_HOST_JNI_MD_VALID NO)
122 set(SQUIRRELJME_HOST_AWT_VALID NO)
125 # JNI is always valid
128 # Use the host JNI or our own?
129 if(SQUIRRELJME_HOST_JVM_VALID AND SQUIRRELJME_HOST_JNI_MD_VALID)
131 set(JNI_INCLUDE_DIRS "${HOST_JNI_INCLUDE_DIRS}")
132 set(JAVA_INCLUDE_PATH "${HOST_JAVA_INCLUDE_PATH}")
133 set(JAVA_INCLUDE_PATH2 "${HOST_JAVA_INCLUDE_PATH2}")
136 set(JAVA_JVM_LIBRARY "${HOST_JAVA_JVM_LIBRARY}")
142 "${SQUIRRELJME_JNI_CMAKE_WHERE}/../include/3rdparty/jni")
143 set(JAVA_INCLUDE_PATH
144 "${SQUIRRELJME_JNI_CMAKE_WHERE}/../include/3rdparty/jni")
145 set(JAVA_INCLUDE_PATH2
146 "${SQUIRRELJME_JNI_CMAKE_WHERE}/../include/3rdparty/jni")
148 # Use stubbed libraries
149 squirreljme_library_set(JAVA_JVM_LIBRARY jvm)
153 if(SQUIRRELJME_HOST_AWT_VALID)
155 set(JAVA_AWT_INCLUDE_PATH "${HOST_JAVA_AWT_INCLUDE_PATH}")
158 set(JAVA_AWT_LIBRARY "${HOST_JAVA_AWT_LIBRARY}")
163 set(JAVA_AWT_INCLUDE_PATH
164 "${SQUIRRELJME_JNI_CMAKE_WHERE}/../include/3rdparty/jni")
167 squirreljme_library_set(JAVA_AWT_LIBRARY jawt)
171 message(STATUS "JNI Include: ${JNI_INCLUDE_DIRS}")
172 message(STATUS "JVM Library: ${JAVA_JVM_LIBRARY}")
173 message(STATUS "JVM Include 1: ${JAVA_INCLUDE_PATH}")
174 message(STATUS "JVM Include 2: ${JAVA_INCLUDE_PATH2}")
175 message(STATUS "JAWT Library: ${JAVA_AWT_LIBRARY}")
176 message(STATUS "JAWT Include: ${JAVA_AWT_INCLUDE_PATH}")