Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[java] strip the windows version now that it works
[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
99   set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
100   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$")
101     set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/x86)
102   endif()
103   if(  (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR
104        (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")     )
105     set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/amd64)
106   endif()
107
108   add_custom_command(
109     TARGET simgrid-java_jar POST_BUILD
110     COMMENT "Add the native libs into simgrid.jar..."
111     DEPENDS simgrid-java 
112             ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
113             ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO}
114             ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}
115           
116     COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
117     COMMAND ${CMAKE_COMMAND} -E make_directory                                     ${JAVA_NATIVE_PATH}
118     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}
119     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}
120     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}    ${JAVA_NATIVE_PATH}
121     
122     # strip seems to fail on Mac on binaries that are already stripped.
123     # It then spits: "symbols referenced by indirect symbol table entries that can't be stripped"
124     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}      || true
125     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO} || true
126     COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSURF_JAVA_SO}    || true
127
128     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  NATIVE
129     COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
130     
131     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
132     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
133     )
134   if(MINGW)
135     find_library(WINPTHREAD_DLL
136       NAME winpthread winpthread-1
137       PATHS C:\\MinGW C:\\MinGW64 C:\\MinGW\\bin C:\\MinGW64\\bin
138     )
139     add_custom_command(
140       TARGET simgrid-java_jar POST_BUILD
141       COMMENT "Add the MinGW libs into simgrid.jar..."
142       DEPENDS ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
143
144       COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
145       COMMAND ${CMAKE_COMMAND} -E make_directory          ${JAVA_NATIVE_PATH}
146       COMMAND ${CMAKE_COMMAND} -E copy ${WINPTHREAD_DLL}  ${JAVA_NATIVE_PATH}
147
148       COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  NATIVE
149       COMMAND ${CMAKE_COMMAND} -E remove_directory NATIVE
150     )
151   endif(MINGW)
152 endif(enable_lib_in_jar)
153
154 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
155
156 if(enable_maintainer_mode)
157   set(CMAKE_SWIG_FLAGS "-package" "org.simgrid.surf")
158   set(CMAKE_SWIG_OUTDIR "${CMAKE_HOME_DIRECTORY}/src/bindings/java/org/simgrid/surf")
159
160   set_source_files_properties(${JSURF_SWIG_SRC} PROPERTIES CPLUSPLUS 1)
161
162   swig_add_module(surf-java java ${JSURF_SWIG_SRC} ${JSURF_JAVA_C_SRC})
163   swig_link_libraries(surf-java simgrid)
164 else()
165   add_library(surf-java SHARED ${JSURF_C_SRC})
166   target_link_libraries(surf-java simgrid)
167 endif()
168
169 set_target_properties(surf-java PROPERTIES SKIP_BUILD_RPATH ON)
170 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
171
172 add_dependencies(simgrid-java surf-java)
173 add_dependencies(simgrid-java_jar surf-java)
174