Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the Storage::read_async and Storage::write_async methods
[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 find_package(Java 1.7 COMPONENTS Runtime Development)
7 if (NOT ${Java_FOUND})
8   message(FATAL_ERROR "Java not found (need at least Java7). Please install the JDK or disable that option")
9 endif()
10 set(Java_FOUND 1)
11 include(UseJava)
12
13 find_package(JNI REQUIRED)
14 message(STATUS "[Java] JNI found: ${JNI_FOUND}")
15 message(STATUS "[Java] JNI include dirs: ${JNI_INCLUDE_DIRS}")
16
17 if(WIN32)
18   execute_process(COMMAND         java -d64 -version
19                   OUTPUT_VARIABLE JVM_IS_64_BITS)
20   if("${JVM_IS_64_BITS}" MATCHES "Error")
21     message(fatal_error "SimGrid can only use Java 64 bits")
22   endif()
23 endif()
24
25 # Rules to build libsimgrid-java
26 ################################
27
28 add_library(simgrid-java SHARED ${JMSG_C_SRC})
29 set_target_properties(simgrid-java PROPERTIES VERSION          ${libsimgrid-java_version})
30 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
31 set_property(TARGET simgrid-java
32              APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
33
34 target_link_libraries(simgrid-java simgrid)
35
36 get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
37 if (COMMON_INCLUDES)
38   set_target_properties(simgrid-java PROPERTIES  INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
39 else()
40   set_target_properties(simgrid-java PROPERTIES  INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
41 endif()
42
43 get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
44 message(STATUS "[Java] simgrid-java includes: ${CHECK_INCLUDES}")
45
46 # Rules to build simgrid.jar
47 ############################
48
49 ## Files to include in simgrid.jar
50 ##
51 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
52 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.in")
53 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
54
55 set(LIBSIMGRID_SO       libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
56 set(LIBSIMGRID_JAVA_SO  ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
57
58 ## Here is how to build simgrid.jar
59 ##
60 if(CMAKE_VERSION VERSION_LESS "2.8.12")
61   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
62   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC})
63 else()
64   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
65 endif()
66
67 if(enable_lib_in_jar)
68   add_dependencies(simgrid-java_jar simgrid-java)
69   add_dependencies(simgrid-java_jar simgrid)
70 endif()
71
72 if (enable_documentation)
73   add_custom_command(
74     TARGET simgrid-java_jar POST_BUILD
75     COMMENT "Add the documentation into simgrid.jar..."
76     DEPENDS ${MANIFEST_IN_FILE}
77             ${CMAKE_HOME_DIRECTORY}/COPYING
78             ${CMAKE_HOME_DIRECTORY}/ChangeLog
79             ${CMAKE_HOME_DIRECTORY}/NEWS
80             ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
81
82     COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
83     COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
84     COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
85
86     COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.trace
87
88     COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR} doc/javadoc
89     COMMAND ${JAVA_ARCHIVE} -uvf  ${SIMGRID_JAR} -C ${CMAKE_HOME_DIRECTORY} COPYING -C ${CMAKE_HOME_DIRECTORY} ChangeLog -C ${CMAKE_HOME_DIRECTORY} LICENSE-LGPL-2.1 -C ${CMAKE_HOME_DIRECTORY} NEWS
90   )
91 endif()
92
93 ###
94 ### Pack the java libraries into the jarfile if asked to do so
95 ###
96
97 if(enable_lib_in_jar)
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   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7l")
113     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/arm) # Default arm (soft-float ABI)
114   endif()
115
116   add_custom_command(
117     TARGET simgrid-java_jar POST_BUILD
118     COMMENT "Add the native libs into simgrid.jar..."
119     DEPENDS simgrid simgrid-java ${JAVALIBS}
120         
121     COMMAND ${CMAKE_COMMAND} -E make_directory   ${JAVA_NATIVE_PATH}
122
123     COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}
124     COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO}
125   )
126
127 if(WIN32)
128   add_custom_command(
129     TARGET simgrid-java_jar POST_BUILD
130     COMMENT "Add the windows-specific native libs into simgrid.jar..."
131     DEPENDS simgrid simgrid-java ${JAVALIBS}
132
133     # There is no way to disable the dependency of mingw-64 on that lib, unfortunately nor to script cmake -E properly
134     # So let's be brutal and copy it in any case (even on non-windows builds) from the location where appveyor provides it.
135     # The copy is only expected to work on the appveyor builder, but that's all we need right now
136     # since our users are directed to download that file as nightly build.
137     COMMAND ${CMAKE_COMMAND} -E copy_if_different C:/mingw-w64/x86_64-7.2.0-posix-seh-rt_v5-rev1/mingw64/bin/libwinpthread-1.dll  ${JAVA_NATIVE_PATH}/libwinpthread-1.dll || true
138   )
139 endif()
140
141 if(APPLE)
142   add_custom_command(
143     TARGET simgrid-java_jar POST_BUILD
144     COMMENT "Add the apple-specific native libs into simgrid.jar..."
145     DEPENDS simgrid simgrid-java ${JAVALIBS}
146
147     # We need to fix the rpath of the simgrid-java library so that it
148     #    searches the simgrid library in the right location
149     #
150     # Since we don't officially install the lib before copying it in
151     # the jarfile, the lib is searched for where it was built. Given
152     # how we unpack it, we need to instruct simgrid-java to forget
153     # about the build path, and search in its current directory
154     # instead.
155     #
156     # This has to be done with the classical Apple tools, as follows:
157
158     COMMAND install_name_tool -change ${CMAKE_BINARY_DIR}/lib/libsimgrid.${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}${CMAKE_SHARED_LIBRARY_SUFFIX} @loader_path/libsimgrid.dylib ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO}
159   )
160 endif(APPLE)
161
162   add_custom_command(
163     TARGET simgrid-java_jar POST_BUILD
164     COMMENT "Packing back the simgrid.jar with the native libs..."
165     DEPENDS simgrid simgrid-java ${JAVALIBS}
166
167     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  ${JAVA_NATIVE_PATH}
168
169     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
170     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
171   )
172
173 endif(enable_lib_in_jar)
174
175 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})