Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix my borken english
[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 # find_package(SWIG)
18 # if(${SWIG_FOUND})
19 #   include(UseSWIG)
20 #   message("-- [Java] Swig found: version ${SWIG_VERSION}")
21 # else()
22 #   message("-- [Java] Swig NOT FOUND. That's fine unless you work on this part yourself.")
23 # endif()
24 #mark_as_advanced(SWIG_EXECUTABLE)
25
26 # Rules to build libsimgrid-java
27 #
28 add_library(simgrid-java SHARED ${JMSG_C_SRC})
29 set_target_properties(simgrid-java PROPERTIES VERSION ${libsimgrid-java_version})
30
31 get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
32 if (COMMON_INCLUDES)
33   set_target_properties(simgrid-java PROPERTIES
34     INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
35 else()
36   set_target_properties(simgrid-java PROPERTIES
37     INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
38 endif()
39
40 get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
41 message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}")
42
43 target_link_libraries(simgrid-java simgrid)
44
45
46 if(WIN32)
47   exec_program("java -d32 -version" OUTPUT_VARIABLE IS_32_BITS_JVM)
48   STRING( FIND ${IS_32_BITS_JVM} "Error" POSITION )
49   if(NOT ${POSITION} GREATER -1)
50     message(fatal_error "SimGrid can only use Java 64 bits")
51   endif()
52 endif()
53
54 # Rules to build simgrid.jar
55 #
56
57 ## Files to include in simgrid.jar
58 ##
59 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
60 set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.MF.in")
61 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
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
68 ## Here is how to build simgrid.jar
69 ##
70 if(CMAKE_VERSION VERSION_LESS "2.8.12")
71   set(CMAKE_JAVA_TARGET_OUTPUT_NAME simgrid)
72   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC})
73 else()
74   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
75 endif()
76
77 if (enable_documentation)
78   add_custom_command(
79     TARGET simgrid-java_jar POST_BUILD
80     COMMENT "Add the documentation into simgrid.jar..."
81     DEPENDS ${MANIFEST_IN_FILE}
82             ${CMAKE_HOME_DIRECTORY}/COPYING
83             ${CMAKE_HOME_DIRECTORY}/ChangeLog
84             ${CMAKE_HOME_DIRECTORY}/NEWS
85             ${CMAKE_HOME_DIRECTORY}/ChangeLog.SimGrid-java
86             ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
87
88     COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
89     COMMAND ${CMAKE_COMMAND} -E echo "Specification-Version: \\\"${SIMGRID_VERSION_MAJOR}.${SIMGRID_VERSION_MINOR}.${SIMGRID_VERSION_PATCH}\\\"" >> ${MANIFEST_FILE}
90     COMMAND ${CMAKE_COMMAND} -E echo "Implementation-Version: \\\"${GIT_VERSION}\\\"" >> ${MANIFEST_FILE}
91
92     COMMAND ${Java_JAVADOC_EXECUTABLE} -quiet -d doc/javadoc -sourcepath ${CMAKE_HOME_DIRECTORY}/src/bindings/java/ org.simgrid.msg org.simgrid.trace
93
94     COMMAND ${JAVA_ARCHIVE} -uvmf ${MANIFEST_FILE} ${SIMGRID_JAR} doc/javadoc
95     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
96   )
97 endif()
98
99 ###
100 ### Pack the java libraries into the jarfile if asked to do so
101 ###
102
103 if(enable_lib_in_jar)
104
105   # Stripping disabled for the time being to make Java debuggable. We should introduce a proper option for that.
106   #find_program(STRIP_COMMAND strip)
107   #mark_as_advanced(STRIP_COMMAND)
108   #if(NOT STRIP_COMMAND)
109   #  set(STRIP_COMMAND "cmake -E echo (strip not found)")
110   #endif()
111   
112   set(SG_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
113   
114   if(${SG_SYSTEM_NAME} MATCHES "kFreeBSD")
115     set(SG_SYSTEM_NAME GNU/kFreeBSD)
116   endif()
117
118   set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/${CMAKE_SYSTEM_PROCESSOR})
119   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$")
120     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/x86)
121   endif()
122   if(  (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR
123        (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")     )
124     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/amd64)
125   endif()
126   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7l")
127     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/arm) # Default arm (soft-float ABI)
128   endif()
129
130   # Find what to copy
131   set(JAVALIBS ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO} ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO})
132   if (HAVE_BOOST_CONTEXT)
133     set(JAVALIBS ${JAVALIBS} ${Boost_CONTEXT_LIBRARY_RELEASE})
134   endif()
135   if(MINGW)
136     find_library(WINPTHREAD_DLL
137       NAME winpthread winpthread-1
138       PATHS C:\\MinGW C:\\MinGW64 C:\\MinGW\\bin C:\\MinGW64\\bin C:\\msys64\\mingw32\\bin C:\\msys64\\mingw32 C:\\msys\\mingw32\\bin C:\\msys\\mingw32 C:\\msys64\\mingw64\\bin C:\\msys64\\mingw64 C:\\msys\\mingw64\\bin C:\\msys\\mingw64
139     )
140     if(${WINPTHREAD_DLL})
141       set(JAVALIBS ${JAVALIBS} ${WINPTHREAD_DLL})
142     endif()
143   endif(MINGW)
144
145   add_custom_command(
146     TARGET simgrid-java_jar POST_BUILD
147     COMMENT "Add the native libs into simgrid.jar..."
148     DEPENDS simgrid simgrid-java ${JAVALIBS}
149           
150     COMMAND ${CMAKE_COMMAND} -E remove_directory ${JAVA_NATIVE_PATH}
151     COMMAND ${CMAKE_COMMAND} -E make_directory   ${JAVA_NATIVE_PATH}
152     COMMAND cp ${JAVALIBS} ${JAVA_NATIVE_PATH} # cp is less portable, but cmake cannot copy several files at once
153     
154     ## strip seems to fail on Mac on binaries that are already stripped.
155     ## It then spits: "symbols referenced by indirect symbol table entries that can't be stripped"
156     #COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}      || true
157     #COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO} || true
158
159     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  ${JAVA_NATIVE_PATH}
160     COMMAND ${CMAKE_COMMAND} -E remove_directory ${JAVA_NATIVE_PATH}
161     
162     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
163     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
164     )
165     
166 endif(enable_lib_in_jar)
167
168 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
169
170 set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
171
172 add_dependencies(simgrid-java_jar simgrid-java)
173 add_dependencies(simgrid-java_jar simgrid)
174
175