Logo AND Algorithmique Numérique Distribuée

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