Logo AND Algorithmique Numérique Distribuée

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