Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
provide a backtrace implementation that uses boost.stacktrace
[simgrid.git] / tools / cmake / Java.cmake
index 613272c..4aaf5a3 100644 (file)
@@ -11,59 +11,49 @@ set(Java_FOUND 1)
 include(UseJava)
 
 find_package(JNI REQUIRED)
-message("-- [Java] JNI found: ${JNI_FOUND}")
-message("-- [Java] JNI include dirs: ${JNI_INCLUDE_DIRS}")
-
-# find_package(SWIG)
-# if(${SWIG_FOUND})
-#   include(UseSWIG)
-#   message("-- [Java] Swig found: version ${SWIG_VERSION}")
-# else()
-#   message("-- [Java] Swig NOT FOUND. That's fine unless you work on this part yourself.")
-# endif()
-#mark_as_advanced(SWIG_EXECUTABLE)
+message(STATUS "[Java] JNI found: ${JNI_FOUND}")
+message(STATUS "[Java] JNI include dirs: ${JNI_INCLUDE_DIRS}")
+
+if(WIN32)
+  execute_process(COMMAND         java -d64 -version
+                  OUTPUT_VARIABLE JVM_IS_64_BITS)
+  if("${JVM_IS_64_BITS}" MATCHES "Error")
+    message(fatal_error "SimGrid can only use Java 64 bits")
+  endif()
+endif()
 
 # Rules to build libsimgrid-java
-#
+################################
+
 add_library(simgrid-java SHARED ${JMSG_C_SRC})
-set_target_properties(simgrid-java PROPERTIES VERSION ${libsimgrid-java_version})
+set_target_properties(simgrid-java PROPERTIES VERSION          ${libsimgrid-java_version})
+set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
+set_property(TARGET simgrid-java
+             APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
+
+target_link_libraries(simgrid-java simgrid)
 
 get_target_property(COMMON_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
 if (COMMON_INCLUDES)
-  set_target_properties(simgrid-java PROPERTIES
-    INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
+  set_target_properties(simgrid-java PROPERTIES  INCLUDE_DIRECTORIES "${COMMON_INCLUDES};${JNI_INCLUDE_DIRS}")
 else()
-  set_target_properties(simgrid-java PROPERTIES
-    INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
+  set_target_properties(simgrid-java PROPERTIES  INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}")
 endif()
 
 get_target_property(CHECK_INCLUDES simgrid-java INCLUDE_DIRECTORIES)
-message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}")
-
-target_link_libraries(simgrid-java simgrid)
-
-
-if(WIN32)
-  exec_program("java -d32 -version" OUTPUT_VARIABLE IS_32_BITS_JVM)
-  STRING( FIND ${IS_32_BITS_JVM} "Error" POSITION )
-  if(NOT ${POSITION} GREATER -1)
-    message(fatal_error "SimGrid can only use Java 64 bits")
-  endif()
-endif()
+message(STATUS "[Java] simgrid-java includes: ${CHECK_INCLUDES}")
 
 # Rules to build simgrid.jar
-#
+############################
 
 ## Files to include in simgrid.jar
 ##
 set(SIMGRID_JAR "${CMAKE_BINARY_DIR}/simgrid.jar")
-set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.MF.in")
+set(MANIFEST_IN_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/MANIFEST.in")
 set(MANIFEST_FILE "${CMAKE_BINARY_DIR}/src/bindings/java/MANIFEST.MF")
 
-set(LIBSIMGRID_SO
-  libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
-set(LIBSIMGRID_JAVA_SO
-  ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
+set(LIBSIMGRID_SO       libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX})
+set(LIBSIMGRID_JAVA_SO  ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX})
 
 ## Here is how to build simgrid.jar
 ##
@@ -74,6 +64,11 @@ else()
   add_jar(simgrid-java_jar ${JMSG_JAVA_SRC} OUTPUT_NAME simgrid)
 endif()
 
+if(enable_lib_in_jar)
+  add_dependencies(simgrid-java_jar simgrid-java)
+  add_dependencies(simgrid-java_jar simgrid)
+endif()
+
 if (enable_documentation)
   add_custom_command(
     TARGET simgrid-java_jar POST_BUILD
@@ -82,7 +77,6 @@ if (enable_documentation)
             ${CMAKE_HOME_DIRECTORY}/COPYING
             ${CMAKE_HOME_DIRECTORY}/ChangeLog
             ${CMAKE_HOME_DIRECTORY}/NEWS
-            ${CMAKE_HOME_DIRECTORY}/ChangeLog.SimGrid-java
             ${CMAKE_HOME_DIRECTORY}/LICENSE-LGPL-2.1
 
     COMMAND ${CMAKE_COMMAND} -E copy ${MANIFEST_IN_FILE} ${MANIFEST_FILE}
@@ -101,16 +95,8 @@ endif()
 ###
 
 if(enable_lib_in_jar)
-
-  # Stripping disabled for the time being to make Java debuggable. We should introduce a proper option for that.
-  #find_program(STRIP_COMMAND strip)
-  #mark_as_advanced(STRIP_COMMAND)
-  #if(NOT STRIP_COMMAND)
-  #  set(STRIP_COMMAND "cmake -E echo (strip not found)")
-  #endif()
-  
   set(SG_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
-  
+
   if(${SG_SYSTEM_NAME} MATCHES "kFreeBSD")
     set(SG_SYSTEM_NAME GNU/kFreeBSD)
   endif()
@@ -127,66 +113,63 @@ if(enable_lib_in_jar)
     set(JAVA_NATIVE_PATH NATIVE/${SG_SYSTEM_NAME}/arm) # Default arm (soft-float ABI)
   endif()
 
-  # Find how to copy
-  set(SHEXE sh)
-  if(WIN32)
-    set(SHEXE sh.exe)
-  endif()
-
-  # Find what to copy
-  set(JAVALIBS ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO} ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO})
-  if(MINGW)
-    find_library(WINPTHREAD_DLL
-      NAME winpthread winpthread-1
-      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
-    )
-    if(${WINPTHREAD_DLL})
-      set(JAVALIBS ${JAVALIBS} ${WINPTHREAD_DLL})
-    endif()
-  endif(MINGW)
-  string(REPLACE ";" " " JAVALIBS "${JAVALIBS}")
-  message("-- [Java] Native Libs: ${JAVALIBS}")
-
   add_custom_command(
     TARGET simgrid-java_jar POST_BUILD
     COMMENT "Add the native libs into simgrid.jar..."
     DEPENDS simgrid simgrid-java ${JAVALIBS}
-         
-    COMMAND ${CMAKE_COMMAND} -E remove_directory ${JAVA_NATIVE_PATH}
+       
     COMMAND ${CMAKE_COMMAND} -E make_directory   ${JAVA_NATIVE_PATH}
-    
-    COMMAND echo cp ${JAVALIBS} ${JAVA_NATIVE_PATH} # Just display what's going on
-    # So, first of all, I'm sorry for the next few lines. Here is what's going on.
-    # I need to copy some files, depending on the environment. 
-    # I cannot use several POST_BUILD commands because cmake does
-    #     force them to run sequentially, so it fails if we build with -j
-    # So I add some content to the JAVALIBS cmake variable, and copy them in one shoot.
-    # But cmake list variables are ; separated, not space separated.
-    # So I string(REPLACE a bit above to change ; into spaces.
-    # But if I do so, cmake still passes the space-separated list as a single argument to cp.
-    # So I have to fire a sh -c, just to correctly parse the cp parameters.
-    # Yup. That's the ways it goes. cmake is so lovely, that's wonderful.
-    COMMAND ${SHEXE} -c "cp ${JAVALIBS} ${JAVA_NATIVE_PATH}" # cp is less portable, but cmake cannot copy several files at once
-    
-    ## strip seems to fail on Mac on binaries that are already stripped.
-    ## It then spits: "symbols referenced by indirect symbol table entries that can't be stripped"
-    #COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}      || true
-    #COMMAND ${STRIP_COMMAND} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO} || true
+
+    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO}      ${JAVA_NATIVE_PATH}/${LIBSIMGRID_SO}
+    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} ${JAVA_NATIVE_PATH}/${LIBSIMGRID_JAVA_SO}
+  )
+
+if(WIN32)
+  add_custom_command(
+    TARGET simgrid-java_jar POST_BUILD
+    COMMENT "Add the windows-specific native libs into simgrid.jar..."
+    DEPENDS simgrid simgrid-java ${JAVALIBS}
+
+    # There is no way to disable the dependency of mingw-64 on that lib, unfortunately nor to script cmake -E properly
+    # So let's be brutal and copy it in any case (even on non-windows builds) from the location where appveyor provides it.
+    # The copy is only expected to work on the appveyor builder, but that's all we need right now
+    # since our users are directed to download that file as nightly build.
+    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
+  )
+endif()
+
+if(APPLE)
+  add_custom_command(
+    TARGET simgrid-java_jar POST_BUILD
+    COMMENT "Add the apple-specific native libs into simgrid.jar..."
+    DEPENDS simgrid simgrid-java ${JAVALIBS}
+
+    # We need to fix the rpath of the simgrid-java library so that it
+    #    searches the simgrid library in the right location
+    #
+    # Since we don't officially install the lib before copying it in
+    # the jarfile, the lib is searched for where it was built. Given
+    # how we unpack it, we need to instruct simgrid-java to forget
+    # about the build path, and search in its current directory
+    # instead.
+    #
+    # This has to be done with the classical Apple tools, as follows:
+
+    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}
+  )
+endif(APPLE)
+
+  add_custom_command(
+    TARGET simgrid-java_jar POST_BUILD
+    COMMENT "Packing back the simgrid.jar with the native libs..."
+    DEPENDS simgrid simgrid-java ${JAVALIBS}
 
     COMMAND ${JAVA_ARCHIVE} -uvf ${SIMGRID_JAR}  ${JAVA_NATIVE_PATH}
-    COMMAND ${CMAKE_COMMAND} -E remove_directory ${JAVA_NATIVE_PATH}
-    
+
     COMMAND ${CMAKE_COMMAND} -E echo "-- Cmake put the native code in ${JAVA_NATIVE_PATH}"
     COMMAND "${Java_JAVA_EXECUTABLE}" -classpath "${SIMGRID_JAR}" org.simgrid.NativeLib
-    )
-    
+  )
+
 endif(enable_lib_in_jar)
 
 include_directories(${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
-
-set_target_properties(simgrid-java PROPERTIES SKIP_BUILD_RPATH ON)
-
-add_dependencies(simgrid-java_jar simgrid-java)
-add_dependencies(simgrid-java_jar simgrid)
-
-