Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[win] I fail to disable winpthread + I think it's harmless, so re-enable it
[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 cmake_minimum_required(VERSION 2.8.6)
7
8 find_package(Java 1.7 REQUIRED)
9 include(UseJava)
10
11 # Rules to build libsimgrid-java
12 #
13 add_library(simgrid-java SHARED ${JMSG_C_SRC})
14 set_target_properties(simgrid-java PROPERTIES VERSION ${libsimgrid-java_version})
15 if (CMAKE_VERSION VERSION_LESS "2.8.8")
16   include_directories(${JNI_INCLUDE_DIRS})
17
18   message("[Java] Try to workaround missing feature in older CMake. You should better update CMake to version 2.8.8 or above.")
19   get_directory_property(CHECK_INCLUDES INCLUDE_DIRECTORIES)
20 else()
21   get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
22   if (COMMON_INCLUDES)
23     set_target_properties(simgrid-java PROPERTIES
24       INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
25   else()
26     set_target_properties(simgrid-java PROPERTIES
27       INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
28   endif()
29
30   get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
31 endif()
32 message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}")
33
34 target_link_libraries(simgrid-java simgrid)
35
36
37 if(WIN32)
38   exec_program("java -d32 -version" OUTPUT_VARIABLE IS_32_BITS_JVM)
39   STRING( FIND ${IS_32_BITS_JVM} "Error" POSITION )
40   if(NOT ${POSITION} GREATER -1)
41     message(fatal_error "SimGrid can only use Java 64 bits")
42   endif()
43 endif()
44
45 # Rules to build simgrid.jar
46 #
47
48 ## Files to include in simgrid.jar
49 ##
50 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
51 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.MF.in")
52 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
53
54 set(LIBSIMGRID_SO
55   libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
56 set(LIBSIMGRID_JAVA_SO
57   ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
58 set(LIBSURF_JAVA_SO
59   ${CMAKE_SHARED_LIBRARY_PREFIX}surf-java${CMAKE_SHARED_LIBRARY_SUFFIX})
60
61 ## Here is how to build simgrid.jar
62 ##
63 if(CMAKE_VERSION VERSION_LESS "2.8.12")
64   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
65   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC})
66 else()
67   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
68 endif()
69
70 add_custom_command(
71   TARGET simgrid-java_jar POST_BUILD
72   COMMENT "Add the documentation into simgrid.jar..."
73   DEPENDS ${MANIFEST_IN_FILE}
74           ${CMAKE_HOME_DIRECTORY}/COPYING
75           ${CMAKE_HOME_DIRECTORY}/ChangeLog
76           ${CMAKE_HOME_DIRECTORY}/NEWS
77           ${CMAKE_HOME_DIRECTORY}/ChangeLog.SimGrid-java
78           ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
79             
80   COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
81   COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
82   COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
83
84   COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.surf org.simgrid.trace
85   
86   COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR} doc/javadoc -C ${CMAKE_HOME_DIRECTORY} COPYING ChangeLog ChangeLog.SimGrid-java LICENSE-LGPL-2.1 NEWS
87   )
88
89 ###
90 ### Pack the java libraries into the jarfile if asked to do so
91 ###
92
93 if(enable_lib_in_jar)
94   find_program(STRIP_COMMAND strip)
95   if(NOT STRIP_COMMAND)
96     set(STRIP_COMMAND "cmake -E echo (strip not found)")
97   endif()
98   if(WIN32)
99     set(STRIP_COMMAND "cmake -E echo (dont strip on windows)")
100   endif()
101
102   set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
103   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$")
104     set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/x86)
105   endif()
106   if(  (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR
107        (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")     )
108     set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/amd64)
109   endif()
110
111   add_custom_command(
112     TARGET simgrid-java_jar POST_BUILD
113     COMMENT "Add the native libs into simgrid.jar..."
114     DEPENDS simgrid-java 
115             ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
116             ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO}
117             ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}
118           
119     COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
120     COMMAND ${CMAKE_COMMAND} -E make_directory                                     ${JAVA_NATIVE_PATH}
121     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}
122     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}
123     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}    ${JAVA_NATIVE_PATH}
124     
125     # strip seems to fail on Mac on binaries that are already stripped.
126     # It then spits: "symbols referenced by indirect symbol table entries that can't be stripped"
127     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}      || true
128     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO} || true
129     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSURF_JAVA_SO}    || true
130
131     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  NATIVE
132     COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
133     
134     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
135     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
136     )
137   if(MINGW)
138     find_library(WINPTHREAD_DLL
139       NAME winpthread winpthread-1
140       PATHS C:\\MinGW C:\\MinGW64 C:\\MinGW\\bin C:\\MinGW64\\bin
141     )
142     add_custom_command(
143       TARGET simgrid-java_jar POST_BUILD
144       COMMENT "Add the MinGW libs into simgrid.jar..."
145       DEPENDS ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
146
147       COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
148       COMMAND ${CMAKE_COMMAND} -E make_directory          ${JAVA_NATIVE_PATH}
149       COMMAND ${CMAKE_COMMAND} -E copy ${WINPTHREAD_DLL}  ${JAVA_NATIVE_PATH}
150
151       COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  NATIVE
152       COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
153     )
154   endif(MINGW)
155 endif(enable_lib_in_jar)
156
157 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
158
159 if(enable_maintainer_mode)
160   set(CMAKE_SWIG_FLAGS "-package" "org.simgrid.surf")
161   set(CMAKE_SWIG_OUTDIR "${CMAKE_HOME_DIRECTORY}/src/bindings/java/org/simgrid/surf")
162
163   set_source_files_properties(${JSURF_SWIG_SRC} PROPERTIES CPLUSPLUS 1)
164
165   swig_add_module(surf-java java ${JSURF_SWIG_SRC} ${JSURF_JAVA_C_SRC})
166   swig_link_libraries(surf-java simgrid)
167 else()
168   add_library(surf-java SHARED ${JSURF_C_SRC})
169   target_link_libraries(surf-java simgrid)
170 endif()
171
172 set_target_properties(surf-java PROPERTIES SKIP_BUILD_RPATH ON)
173 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
174
175 add_dependencies(simgrid-java surf-java)
176 add_dependencies(simgrid-java_jar surf-java)
177