Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix the build on appveyor since travis has no artifacts
[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 add_dependencies(tests simgrid-java)
36
37 get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
38 if (COMMON_INCLUDES)
39   set_target_properties(simgrid-java PROPERTIES  INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
40 else()
41   set_target_properties(simgrid-java PROPERTIES  INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
42 endif()
43
44 get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
45 message(STATUS "[Java] simgrid-java includes: ${CHECK_INCLUDES}")
46
47 # Rules to build simgrid.jar
48 ############################
49
50 ## Files to include in simgrid.jar
51 ##
52 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
53 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.in")
54 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
55
56 set(LIBSIMGRID_SO       libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
57 set(LIBSIMGRID_JAVA_SO  ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
58
59 ## Here is how to build simgrid.jar
60 ##
61 if(CMAKE_VERSION VERSION_LESS "2.8.12")
62   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
63   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC})
64 else()
65   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
66 endif()
67
68 if(enable_lib_in_jar)
69   add_dependencies(simgrid-java_jar simgrid-java)
70   add_dependencies(simgrid-java_jar simgrid)
71 endif()
72
73 if (enable_documentation)
74   add_custom_command(
75     TARGET simgrid-java_jar POST_BUILD
76     COMMENT "Add the documentation into simgrid.jar..."
77     DEPENDS ${MANIFEST_IN_FILE}
78             ${CMAKE_HOME_DIRECTORY}/COPYING
79             ${CMAKE_HOME_DIRECTORY}/ChangeLog
80             ${CMAKE_HOME_DIRECTORY}/NEWS
81             ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
82
83     COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
84     COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
85     COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
86
87     COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.trace
88
89     COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR} doc/javadoc
90     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
91   )
92 endif()
93
94 ###
95 ### Pack the java libraries into the jarfile if asked to do so
96 ###
97
98 if(enable_lib_in_jar)
99   set(SG_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
100
101   if(${SG_SYSTEM_NAME} MATCHES "kFreeBSD")
102     set(SG_SYSTEM_NAME GNU/kFreeBSD)
103   endif()
104
105   set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
106   if( (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$") OR
107       (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR
108       (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64") )
109     if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
110       set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/x86)
111     else()
112       set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/amd64)
113     endif()
114   endif()
115   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7l")
116     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/arm) # Default arm (soft-float ABI)
117   endif()
118
119   add_custom_command(
120     TARGET simgrid-java_jar POST_BUILD
121     COMMENT "Add the native libs into simgrid.jar..."
122     DEPENDS simgrid simgrid-java ${JAVALIBS}
123         
124     COMMAND ${CMAKE_COMMAND} -E make_directory   ${JAVA_NATIVE_PATH}
125
126     COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}
127     COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO}
128   )
129
130 if(WIN32)
131   add_custom_command(
132     TARGET simgrid-java_jar POST_BUILD
133     COMMENT "Add the windows-specific native libs into simgrid.jar..."
134     DEPENDS simgrid simgrid-java ${JAVALIBS}
135
136     # There is no way to disable the dependency of mingw-64 on that lib, unfortunately nor to script cmake -E properly
137     # So let's be brutal and copy it in any case (even on non-windows builds) from the location where appveyor provides it.
138     # The copy is only expected to work on the appveyor builder, but that's all we need right now
139     # since our users are directed to download that file as nightly build.
140     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
141     COMMAND ${CMAKE_COMMAND} -E copy_if_different C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/libwinpthread-1.dll  ${JAVA_NATIVE_PATH}/libwinpthread-1.dll || true
142   )
143 endif()
144
145 if(APPLE)
146   add_custom_command(
147     TARGET simgrid-java_jar POST_BUILD
148     COMMENT "Add the apple-specific native libs into simgrid.jar..."
149     DEPENDS simgrid simgrid-java ${JAVALIBS}
150
151     # We need to fix the rpath of the simgrid-java library so that it
152     #    searches the simgrid library in the right location
153     #
154     # Since we don't officially install the lib before copying it in
155     # the jarfile, the lib is searched for where it was built. Given
156     # how we unpack it, we need to instruct simgrid-java to forget
157     # about the build path, and search in its current directory
158     # instead.
159     #
160     # This has to be done with the classical Apple tools, as follows:
161
162     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}
163   )
164 endif(APPLE)
165
166   add_custom_command(
167     TARGET simgrid-java_jar POST_BUILD
168     COMMENT "Packing back the simgrid.jar with the native libs..."
169     DEPENDS simgrid simgrid-java ${JAVALIBS}
170
171     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  ${JAVA_NATIVE_PATH}
172
173     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
174     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
175   )
176
177 endif(enable_lib_in_jar)
178
179 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})