Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
we won't strip java: we are not robust enough, unfortunately
[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("-- [Java] JNI found: ${JNI_FOUND}")
15 message("-- [Java] JNI include dirs: ${JNI_INCLUDE_DIRS}")
16
17 if(WIN32)
18   exec_program("java -d32 -version" OUTPUT_VARIABLE IS_32_BITS_JVM)
19   STRING( FIND ${IS_32_BITS_JVM} "Error" POSITION )
20   if(NOT ${POSITION} GREATER -1)
21     message(fatal_error "SimGrid can only use Java 64 bits")
22   endif()
23 endif()
24
25 # find_package(SWIG)
26 # if(${SWIG_FOUND})
27 #   include(UseSWIG)
28 #   message("-- [Java] Swig found: version ${SWIG_VERSION}")
29 # else()
30 #   message("-- [Java] Swig NOT FOUND. That's fine unless you work on this part yourself.")
31 # endif()
32 #mark_as_advanced(SWIG_EXECUTABLE)
33
34 # Rules to build libsimgrid-java
35 ################################
36
37 add_library(simgrid-java SHARED ${JMSG_C_SRC})
38 set_target_properties(simgrid-java PROPERTIES VERSION          ${libsimgrid-java_version})
39 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
40
41 target_link_libraries(simgrid-java simgrid)
42
43 get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
44 if (COMMON_INCLUDES)
45   set_target_properties(simgrid-java PROPERTIES
46     INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
47 else()
48   set_target_properties(simgrid-java PROPERTIES
49     INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
50 endif()
51
52 get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
53 message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}")
54
55 # Rules to build simgrid.jar
56 ############################
57
58 ## Files to include in simgrid.jar
59 ##
60 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
61 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.MF.in")
62 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
63
64 set(LIBSIMGRID_SO
65   libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
66 set(LIBSIMGRID_JAVA_SO
67   ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
68
69 ## Here is how to build simgrid.jar
70 ##
71 if(CMAKE_VERSION VERSION_LESS "2.8.12")
72   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
73   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC})
74 else()
75   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
76 endif()
77
78 add_dependencies(simgrid-java_jar simgrid-java)
79 add_dependencies(simgrid-java_jar simgrid)
80
81 if (enable_documentation)
82   add_custom_command(
83     TARGET simgrid-java_jar POST_BUILD
84     COMMENT "Add the documentation into simgrid.jar..."
85     DEPENDS ${MANIFEST_IN_FILE}
86             ${CMAKE_HOME_DIRECTORY}/COPYING
87             ${CMAKE_HOME_DIRECTORY}/ChangeLog
88             ${CMAKE_HOME_DIRECTORY}/NEWS
89             ${CMAKE_HOME_DIRECTORY}/ChangeLog.SimGrid-java
90             ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
91
92     COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
93     COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
94     COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
95
96     COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.trace
97
98     COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR} doc/javadoc
99     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
100   )
101 endif()
102
103 ###
104 ### Pack the java libraries into the jarfile if asked to do so
105 ###
106
107 if(enable_lib_in_jar)
108
109   
110   set(SG_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
111   
112   if(${SG_SYSTEM_NAME} MATCHES "kFreeBSD")
113     set(SG_SYSTEM_NAME GNU/kFreeBSD)
114   endif()
115
116   set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
117   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$")
118     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/x86)
119   endif()
120   if(  (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR
121        (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")     )
122     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/amd64)
123   endif()
124   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7l")
125     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/arm) # Default arm (soft-float ABI)
126   endif()
127
128   add_custom_command(
129     TARGET simgrid-java_jar POST_BUILD
130     COMMENT "Add the native libs into simgrid.jar..."
131     DEPENDS simgrid simgrid-java ${JAVALIBS}
132           
133     COMMAND ${CMAKE_COMMAND} -E remove_directory ${JAVA_NATIVE_PATH}
134     COMMAND ${CMAKE_COMMAND} -E make_directory   ${JAVA_NATIVE_PATH}
135     
136     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}
137     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}
138     # There is no way to disable the dependency of mingw-64 on that lib, unfortunately
139     # nor to script cmake -E properly, so let's be brutal
140     COMMAND ${CMAKE_COMMAND} -E copy C:/mingw64/bin/libwinpthread-1.dll  ${JAVA_NATIVE_PATH} || true
141     
142     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  ${JAVA_NATIVE_PATH}
143     COMMAND ${CMAKE_COMMAND} -E remove_directory ${JAVA_NATIVE_PATH}
144     
145     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
146     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
147   )
148     
149 endif(enable_lib_in_jar)
150
151 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})