Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[cmake] 'not' and 'NOT' are different, but only on windows
[simgrid.git] / tools / cmake / Java.cmake
1 ##
2 ## The Cmake definitions for the use of Java (and Scala)
3 ##   This file is loaded only if the Java option is activated
4 ##
5
6 find_package(Java 1.7 COMPONENTS Runtime Development)
7 if (NOT ${Java_FOUND})
8   message(FATAL_ERROR "Java not found (need at least Java7). Please install the JDK or disable that option")
9 endif()
10 set(Java_FOUND 1)
11 include(UseJava)
12
13 find_package(JNI REQUIRED)
14 message("-- [Java] JNI found: ${JNI_FOUND}")
15 message("-- [Java] JNI include dirs: ${JNI_INCLUDE_DIRS}")
16
17 find_package(SWIG)
18 if(${SWIG_FOUND})
19   include(UseSWIG)
20   message("-- [Java] Swig found: version ${SWIG_VERSION}")
21 else()
22   message("-- [Java] Swig NOT FOUND. Surf java bindings won't get refreshed. That's fine unless you work on this part yourself.")
23 endif()
24 mark_as_advanced(SWIG_EXECUTABLE)
25
26
27 # Rules to build libsimgrid-java
28 #
29 add_library(simgrid-java SHARED ${JMSG_C_SRC})
30 set_target_properties(simgrid-java PROPERTIES VERSION ${libsimgrid-java_version})
31
32 get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
33 if (COMMON_INCLUDES)
34   set_target_properties(simgrid-java PROPERTIES
35     INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
36 else()
37   set_target_properties(simgrid-java PROPERTIES
38     INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
39 endif()
40
41 get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
42 message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}")
43
44 target_link_libraries(simgrid-java simgrid)
45
46
47 if(WIN32)
48   exec_program("java -d32 -version" OUTPUT_VARIABLE IS_32_BITS_JVM)
49   STRING( FIND ${IS_32_BITS_JVM} "Error" POSITION )
50   if(NOT ${POSITION} GREATER -1)
51     message(fatal_error "SimGrid can only use Java 64 bits")
52   endif()
53 endif()
54
55 # Rules to build simgrid.jar
56 #
57
58 ## Files to include in simgrid.jar
59 ##
60 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
61 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.MF.in")
62 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
63
64 set(LIBSIMGRID_SO
65   libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
66 set(LIBSIMGRID_JAVA_SO
67   ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
68 set(LIBSURF_JAVA_SO
69   ${CMAKE_SHARED_LIBRARY_PREFIX}surf-java${CMAKE_SHARED_LIBRARY_SUFFIX})
70
71 ## Here is how to build simgrid.jar
72 ##
73 if(CMAKE_VERSION VERSION_LESS "2.8.12")
74   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
75   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC})
76 else()
77   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
78 endif()
79
80 add_custom_command(
81   TARGET simgrid-java_jar POST_BUILD
82   COMMENT "Add the documentation into simgrid.jar..."
83   DEPENDS ${MANIFEST_IN_FILE}
84           ${CMAKE_HOME_DIRECTORY}/COPYING
85           ${CMAKE_HOME_DIRECTORY}/ChangeLog
86           ${CMAKE_HOME_DIRECTORY}/NEWS
87           ${CMAKE_HOME_DIRECTORY}/ChangeLog.SimGrid-java
88           ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
89             
90   COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
91   COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
92   COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
93
94   COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.surf org.simgrid.trace
95   
96   COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR} doc/javadoc -C ${CMAKE_HOME_DIRECTORY} COPYING ChangeLog ChangeLog.SimGrid-java LICENSE-LGPL-2.1 NEWS
97   )
98
99 ###
100 ### Pack the java libraries into the jarfile if asked to do so
101 ###
102
103 if(enable_lib_in_jar)
104   find_program(STRIP_COMMAND strip)
105   mark_as_advanced(STRIP_COMMAND)
106   if(NOT STRIP_COMMAND)
107     set(STRIP_COMMAND "cmake -E echo (strip not found)")
108   endif()
109   set(SG_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
110   
111   if(${SG_SYSTEM_NAME} MATCHES "kFreeBSD")
112     set(SG_SYSTEM_NAME GNU/kFreeBSD)
113   endif()
114
115   set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
116   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$")
117     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/x86)
118   endif()
119   if(  (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR
120        (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")     )
121     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/amd64)
122   endif()
123   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7l")
124     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/arm) # Default arm (soft-float ABI)
125   endif()
126
127   add_custom_command(
128     TARGET simgrid-java_jar POST_BUILD
129     COMMENT "Add the native libs into simgrid.jar..."
130     DEPENDS simgrid-java 
131             ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
132             ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO}
133             ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}
134           
135     COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
136     COMMAND ${CMAKE_COMMAND} -E make_directory                                     ${JAVA_NATIVE_PATH}
137     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}
138     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}
139     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}    ${JAVA_NATIVE_PATH}
140     
141     # strip seems to fail on Mac on binaries that are already stripped.
142     # It then spits: "symbols referenced by indirect symbol table entries that can't be stripped"
143     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}      || true
144     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO} || true
145     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSURF_JAVA_SO}    || true
146
147     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  NATIVE
148     COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
149     
150     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
151     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
152     )
153   if(MINGW)
154     find_library(WINPTHREAD_DLL
155       NAME winpthread winpthread-1
156       PATHS C:\\MinGW C:\\MinGW64 C:\\MinGW\\bin C:\\MinGW64\\bin
157     )
158     add_custom_command(
159       TARGET simgrid-java_jar POST_BUILD
160       COMMENT "Add the MinGW libs into simgrid.jar..."
161       DEPENDS ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
162
163       COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
164       COMMAND ${CMAKE_COMMAND} -E make_directory          ${JAVA_NATIVE_PATH}
165       COMMAND ${CMAKE_COMMAND} -E copy ${WINPTHREAD_DLL}  ${JAVA_NATIVE_PATH}
166
167       COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  NATIVE
168       COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
169     )
170   endif(MINGW)
171 endif(enable_lib_in_jar)
172
173 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
174
175 if(${SWIG_FOUND})
176   set(CMAKE_SWIG_FLAGS "-package" "org.simgrid.surf")
177   set(CMAKE_SWIG_OUTDIR "${CMAKE_HOME_DIRECTORY}/src/bindings/java/org/simgrid/surf")
178
179   set_source_files_properties(${JSURF_SWIG_SRC} PROPERTIES CPLUSPLUS 1)
180
181   swig_add_module(surf-java java ${JSURF_SWIG_SRC} ${JSURF_JAVA_C_SRC})
182   swig_link_libraries(surf-java simgrid)
183 else()
184   add_library(surf-java SHARED ${JSURF_C_SRC})
185   target_link_libraries(surf-java simgrid)
186 endif()
187
188 set_target_properties(surf-java PROPERTIES SKIP_BUILD_RPATH ON)
189 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
190
191 add_dependencies(simgrid-java surf-java)
192 add_dependencies(simgrid-java_jar surf-java)
193