Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4d93dbcdfa7914e3feb1609fc9b503c53e715c87
[simgrid.git] / tools / cmake / MakeJava.cmake
1 cmake_minimum_required(VERSION 2.8.6)
2
3 include(UseJava)
4
5 # Rules to build libsimgrid-java
6 #
7 add_library(simgrid-java SHARED ${JMSG_C_SRC})
8 set_target_properties(simgrid-java PROPERTIES VERSION ${libsimgrid-java_version})
9 if (CMAKE_VERSION VERSION_LESS "2.8.8")
10   include_directories(${JNI_INCLUDE_DIRS})
11
12   message("[Java] Try to workaround missing feature in older CMake. You should better update CMake to version 2.8.8 or above.")
13   get_directory_property(CHECK_INCLUDES INCLUDE_DIRECTORIES)
14 else()
15   get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
16   if (COMMON_INCLUDES)
17     set_target_properties(simgrid-java PROPERTIES
18       INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
19   else()
20     set_target_properties(simgrid-java PROPERTIES
21       INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
22   endif()
23
24   get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
25 endif()
26 message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}")
27
28 target_link_libraries(simgrid-java simgrid)
29
30
31 if(WIN32)
32   exec_program("java -d32 -version"
33                 OUTPUT_VARIABLE IS_32_BITS_JVM)
34   STRING( FIND ${IS_32_BITS_JVM} "Error" POSITION )
35   if(${POSITION} GREATER -1)
36     message("POTENTIAL ERROR: Java JVM needs to be 32 bits to be able to run with Simgrid on Windows for now")
37   endif()
38
39   set_target_properties(simgrid-java PROPERTIES
40     LINK_FLAGS "-Wl,--subsystem,windows,--kill-at")
41   find_path(PEXPORTS_PATH NAMES pexports.exe PATHS NO_DEFAULT_PATHS)
42   message(STATUS "pexports: ${PEXPORTS_PATH}")
43   if(PEXPORTS_PATH)
44     add_custom_command(TARGET simgrid-java POST_BUILD
45       COMMAND ${PEXPORTS_PATH}/pexports.exe ${CMAKE_BINARY_DIR}/lib/simgrid-java.dll > ${CMAKE_BINARY_DIR}/lib/simgrid-java.def)
46   endif(PEXPORTS_PATH)
47 endif()
48
49 # Rules to build simgrid.jar
50 #
51
52 ## Files to include in simgrid.jar
53 ##
54 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
55 set(SIMGRID_FULL_JAR "${CMAKE_BINARY_DIR}/simgrid_full.jar")
56 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.MF.in")
57 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
58
59 set(LIBSIMGRID_SO
60   libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
61 set(LIBSIMGRID_JAVA_SO
62   ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
63 set(LIBSURF_JAVA_SO
64   ${CMAKE_SHARED_LIBRARY_PREFIX}surf-java${CMAKE_SHARED_LIBRARY_SUFFIX})
65
66 ## Here is how to build simgrid.jar
67 ##
68 if(CMAKE_VERSION VERSION_LESS "2.8.12")
69   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
70   add_jar(simgrid-java_pre_jar ${JMSG_JAVA_SRC})
71 else()
72   add_jar(simgrid-java_pre_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
73 endif()
74
75 set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
76 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$")
77   set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/x86)
78 endif()
79 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
80   set(JAVA_NATIVE_PATH NATIVE/${CMAKE_SYSTEM_NAME}/amd64)
81 endif()
82
83 add_custom_command(
84   COMMENT "Finalize simgrid.jar..."
85   OUTPUT ${SIMGRID_JAR}_finalized
86   DEPENDS simgrid simgrid-java simgrid-java_pre_jar
87           ${SIMGRID_JAR} 
88           ${MANIFEST_IN_FILE}
89           ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}
90           ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO}
91           ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}
92           ${CMAKE_HOME_DIRECTORY}/COPYING
93           ${CMAKE_HOME_DIRECTORY}/ChangeLog
94           ${CMAKE_HOME_DIRECTORY}/ChangeLog.SimGrid-java
95           ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
96           
97   COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR} -C ${CMAKE_HOME_DIRECTORY} COPYING ChangeLog ChangeLog.SimGrid-java LICENSE-LGPL-2.1
98   
99   COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
100   COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
101   COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
102   COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR}
103
104   COMMAND ${CMAKE_COMMAND} -E copy ${SIMGRID_JAR} ${SIMGRID_FULL_JAR}
105   COMMAND ${CMAKE_COMMAND} -E make_directory                                     ${JAVA_NATIVE_PATH}
106   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}
107   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}
108   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO}    ${JAVA_NATIVE_PATH}
109
110   COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_FULL_JAR}  NATIVE
111   COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.surf org.simgrid.trace
112   COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_FULL_JAR} doc/javadoc
113   
114   COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
115   COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
116   
117   COMMAND ${CMAKE_COMMAND} -E remove ${SIMGRID_JAR}_finalized
118   COMMAND ${CMAKE_COMMAND} -E touch ${SIMGRID_JAR}_finalized
119   )
120 add_custom_target(simgrid-java_jar ALL DEPENDS ${SIMGRID_JAR}_finalized)
121
122 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
123
124 if(enable_maintainer_mode)
125   set(CMAKE_SWIG_FLAGS "-package" "org.simgrid.surf")
126   set(CMAKE_SWIG_OUTDIR "${CMAKE_HOME_DIRECTORY}/src/bindings/java/org/simgrid/surf")
127
128   set_source_files_properties(${JSURF_SWIG_SRC} PROPERTIES CPLUSPLUS 1)
129
130   swig_add_module(surf-java java ${JSURF_SWIG_SRC} ${JSURF_JAVA_C_SRC})
131   swig_link_libraries(surf-java simgrid)
132 else()
133   add_library(surf-java SHARED ${JSURF_C_SRC})
134   target_link_libraries(surf-java simgrid)
135 endif()
136
137 set_target_properties(surf-java PROPERTIES SKIP_BUILD_RPATH ON)
138 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
139
140 add_dependencies(simgrid-java surf-java)
141 add_dependencies(simgrid-java_pre_jar surf-java)
142
143 if(WIN32)
144   set_target_properties(surf-java PROPERTIES
145     LINK_FLAGS "-Wl,--subsystem,windows,--kill-at")
146   if(PEXPORTS_PATH)
147     add_custom_command(TARGET surf-java POST_BUILD
148       COMMAND ${PEXPORTS_PATH}/pexports.exe ${CMAKE_BINARY_DIR}/lib/surf-java.dll > ${CMAKE_BINARY_DIR}/lib/surf-java.def)
149   endif(PEXPORTS_PATH)
150 endif()