Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / cmake / jni.cmake
bloba22fe8e0e35a52c30c09037638d6b5365952b871
1 # ---------------------------------------------------------------------------
2 # SquirrelJME
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
10 # Where are we?
11 if(NOT DEFINED SQUIRRELJME_JNI_CMAKE_WHERE)
12         set(SQUIRRELJME_JNI_CMAKE_WHERE "${CMAKE_CURRENT_LIST_DIR}")
13 endif()
15 if(NOT DEFINED SQUIRRELJME_JNI_CMAKE_WHERE)
16         set(SQUIRRELJME_JNI_CMAKE_WHERE "${CMAKE_SOURCE_DIR}")
17 endif()
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$")
28                 unset(${var})
29                 unset(${var} CACHE)
30         endif()
32         if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13")
33                 if("$CACHE{${var}}" MATCHES "-NOTFOUND$")
34                         unset(${var} CACHE)
35                 endif()
36         endif()
37 endmacro()
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
49 if(JNI_FOUND)
50         set(HOST_JNI_FOUND "${JNI_FOUND}")
51         unset(JNI_FOUND)
53         if(DEFINED JNI_INCLUDE_DIRS)
54                 set(HOST_JNI_INCLUDE_DIRS "${JNI_INCLUDE_DIRS}")
55                 unset(JNI_INCLUDE_DIRS)
56         endif()
58         if(DEFINED JAVA_INCLUDE_PATH)
59                 set(HOST_JAVA_INCLUDE_PATH "${JAVA_INCLUDE_PATH}")
60                 unset(JAVA_INCLUDE_PATH)
61         endif()
63         if(DEFINED JAVA_INCLUDE_PATH2)
64                 set(HOST_JAVA_INCLUDE_PATH2 "${JAVA_INCLUDE_PATH2}")
65                 unset(JAVA_INCLUDE_PATH2)
66         endif()
68         if(DEFINED JAVA_AWT_INCLUDE_PATH)
69                 set(HOST_JAVA_AWT_INCLUDE_PATH "${JAVA_AWT_INCLUDE_PATH}")
70                 unset(JAVA_AWT_INCLUDE_PATH)
71         endif()
73         if(DEFINED JAVA_JVM_LIBRARY)
74                 set(HOST_JAVA_JVM_LIBRARY "${JAVA_JVM_LIBRARY}")
75                 unset(JAVA_JVM_LIBRARY)
76         endif()
78         if(DEFINED JAVA_AWT_LIBRARY)
79                 set(HOST_JAVA_AWT_LIBRARY "${JAVA_AWT_LIBRARY}")
80                 unset(JAVA_AWT_LIBRARY)
81         endif()
82 endif()
84 # Check to see if the host can compile or not
85 if(HOST_JNI_FOUND)
86         # JVM?
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}")
96         # JAWT?
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)
114         else()
115                 set(SQUIRRELJME_HOST_JNI_MD_VALID NO)
116         endif()
117         message(STATUS "jni_md.h Valid?: ${SQUIRRELJME_HOST_JNI_MD_VALID}")
118         message(DEBUG "jni_md.h path: ${SQUIRRELJME_HOST_JNI_MD}")
119 else()
120         set(SQUIRRELJME_HOST_JVM_VALID NO)
121         set(SQUIRRELJME_HOST_JNI_MD_VALID NO)
122         set(SQUIRRELJME_HOST_AWT_VALID NO)
123 endif()
125 # JNI is always valid
126 set(JNI_FOUND YES)
128 # Use the host JNI or our own?
129 if(SQUIRRELJME_HOST_JVM_VALID AND SQUIRRELJME_HOST_JNI_MD_VALID)
130         # Includes
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}")
135         # Libraries
136         set(JAVA_JVM_LIBRARY "${HOST_JAVA_JVM_LIBRARY}")
138 # Use our own
139 else()
140         # Includes
141         set(JNI_INCLUDE_DIRS
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)
150 endif()
152 # Use host AWT?
153 if(SQUIRRELJME_HOST_AWT_VALID)
154         # Includes
155         set(JAVA_AWT_INCLUDE_PATH "${HOST_JAVA_AWT_INCLUDE_PATH}")
157         # Libraries
158         set(JAVA_AWT_LIBRARY "${HOST_JAVA_AWT_LIBRARY}")
160 # Use our own
161 else()
162         # Includes
163         set(JAVA_AWT_INCLUDE_PATH
164                 "${SQUIRRELJME_JNI_CMAKE_WHERE}/../include/3rdparty/jni")
166         # Stubbed library
167         squirreljme_library_set(JAVA_AWT_LIBRARY jawt)
168 endif()
170 # Debugging
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}")