From 787840c385098714d035f0b352cdfab6972dace9 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Mon, 11 Jul 2016 16:49:40 -0400 Subject: [PATCH] Set Java compile flags globally Move the logic to set the Java compile flags so that Java will target the matlab JVM version from drake itself to the superbuild, and propagate Java options to all externals. This should address mismatches in the targeted Java version between drake itself, and externals such as LCM. --- CMakeLists.txt | 4 --- cmake/DrakeExternals.cmake | 5 ++++ cmake/DrakePlatform.cmake | 57 +++++++++++++++++++++++++++++++++++++- drake/CMakeLists.txt | 23 --------------- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20e8cebffb23..8dd660d9fd7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,10 +214,6 @@ drake_add_external(drake LOCAL PUBLIC CMAKE ALWAYS BINARY_DIR ${PROJECT_BINARY_DIR}/drake ${DRAKE_BUILD_COMMANDS} CMAKE_ARGS - -DJava_JAVA_EXECUTABLE=${Java_JAVA_EXECUTABLE} - -DJava_JAVAC_EXECUTABLE=${Java_JAVAC_EXECUTABLE} - -DJava_JAVAH_EXECUTABLE=${Java_JAVAH_EXECUTABLE} - -DJava_VERSION_STRING=${Java_VERSION_STRING} -DWITH_PYTHON_3=${WITH_PYTHON_3} -DDISABLE_MATLAB=${DISABLE_MATLAB} DEPENDS diff --git a/cmake/DrakeExternals.cmake b/cmake/DrakeExternals.cmake index 9427493442e2..370f115dd21e 100644 --- a/cmake/DrakeExternals.cmake +++ b/cmake/DrakeExternals.cmake @@ -119,6 +119,11 @@ macro(drake_add_cmake_external PROJECT) -DCMAKE_MODULE_LINKER_FLAGS:STRING=${CMAKE_MODULE_LINKER_FLAGS} -DCMAKE_SHARED_LINKER_FLAGS:STRING=${CMAKE_SHARED_LINKER_FLAGS} -DCMAKE_STATIC_LINKER_FLAGS:STRING=${CMAKE_STATIC_LINKER_FLAGS} + -DJava_JAVA_EXECUTABLE:FILEPATH=${Java_JAVA_EXECUTABLE} + -DJava_JAVAC_EXECUTABLE:FILEPATH=${Java_JAVAC_EXECUTABLE} + -DJava_JAVAH_EXECUTABLE:FILEPATH=${Java_JAVAH_EXECUTABLE} + -DJava_VERSION_STRING:STRING=${Java_VERSION_STRING} + -DCMAKE_JAVA_COMPILE_FLAGS:STRING=${CMAKE_JAVA_COMPILE_FLAGS} ${_ext_VERBOSE} ${_ext_CMAKE_ARGS}) endmacro() diff --git a/cmake/DrakePlatform.cmake b/cmake/DrakePlatform.cmake index c8855e287957..5b6a2a3e5609 100644 --- a/cmake/DrakePlatform.cmake +++ b/cmake/DrakePlatform.cmake @@ -1,3 +1,51 @@ +#------------------------------------------------------------------------------ +function(drake_get_matlab_jvm_version) + # Find matlab + find_program(MATLAB_EXECUTABLE matlab) + + if(MATLAB_EXECUTABLE AND NOT MATLAB_JVM_VERSION) + # Set arguments for running matlab + set(_args -nodesktop -nodisplay -nosplash) + if(WIN32) + set(_args ${_args} -wait) + endif() + + # Ask matlab for its JVM version + execute_process( + COMMAND ${MATLAB_EXECUTABLE} ${_args} -r "version -java,quit" + RESULT_VARIABLE _result + ERROR_VARIABLE _output + OUTPUT_VARIABLE _output + TIMEOUT 180) + + # Test for a valid result + if(NOT _result AND _output MATCHES "Java ([0-9]+\\.[0-9]+)\\.[0-9_.]+") + set(MATLAB_JVM_VERSION ${CMAKE_MATCH_1} CACHE INTERNAL "") + set(_jsdk_version "${Java_VERSION_MAJOR}.${Java_VERSION_MINOR}") + + # Compare against JSDK version + if(NOT _matlab_jvm_version VERSION_EQUAL _jsdk_version) + message(WARNING + "MATLAB JVM version (${MATLAB_JVM_VERSION}) does not match " + "installed Java SDK version (${_jsdk_version})") + + # If JSDK is newer, set build options to build Java code compatible + # with the matlab JVM + if(MATLAB_JVM_VERSION VERSION_LESS _jsdk_version) + set(CMAKE_JAVA_COMPILE_FLAGS + -source ${MATLAB_JVM_VERSION} + -target ${MATLAB_JVM_VERSION} + CACHE INTERNAL "") + endif() + endif() + else() + message(WARNING "Could not determine MATLAB JVM version") + endif() + endif() +endfunction() + +############################################################################### + # Check minimum compiler requirements if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) message(FATAL_ERROR "GCC version must be at least 4.9") @@ -16,9 +64,16 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) endif() message(STATUS CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}) -# Require Java at the super-build level since libbot cannot configure without finding Java +# Require Java at the super-build level since libbot cannot configure without +# finding Java find_package(Java 1.6 REQUIRED) include(UseJava) +# If matlab is in use, try to determine its JVM version, as we need to build +# all externals to the same version +if(NOT DISABLE_MATLAB) + drake_get_matlab_jvm_version() +endif() + # Choose your python (major) version option(WITH_PYTHON_3 "Force Drake to use python 3 instead of python 2" OFF) diff --git a/drake/CMakeLists.txt b/drake/CMakeLists.txt index e2c2a9c2f9c3..c7fd6f4627fe 100644 --- a/drake/CMakeLists.txt +++ b/drake/CMakeLists.txt @@ -139,29 +139,6 @@ else() # along with what is needed to use matlab_add_unit_test if(MATLAB_FOUND) find_package(Matlab COMPONENTS MAIN_PROGRAM REQUIRED) - - set(Matlab_Java_MAIN_PROGRAM_ARGS -nodesktop -nodisplay -nosplash) - if(WIN32) - set(Matlab_Java_MAIN_PROGRAM_ARGS ${Matlab_Java_MAIN_PROGRAM_ARGS} -wait) - endif() - execute_process( - COMMAND ${Matlab_MAIN_PROGRAM} ${Matlab_Java_MAIN_PROGRAM_ARGS} -r "version -java,quit" - RESULT_VARIABLE Matlab_Java_RESULT_VARIABLE - ERROR_VARIABLE Matlab_Java_OUTPUT_VARIABLE - OUTPUT_VARIABLE Matlab_Java_OUTPUT_VARIABLE - TIMEOUT 180) - if(NOT Matlab_Java_RESULT_VARIABLE AND Matlab_Java_OUTPUT_VARIABLE MATCHES "Java ([0-9]+\\.[0-9]+)\\.[0-9_.]+") - set(Matlab_Java_VERSION_MAJOR_MINOR ${CMAKE_MATCH_1}) - set(Java_VERSION_MAJOR_MINOR "${Java_VERSION_MAJOR}.${Java_VERSION_MINOR}") - if(NOT Matlab_Java_VERSION_MAJOR_MINOR VERSION_EQUAL Java_VERSION_MAJOR_MINOR) - message(WARNING "MATLAB JVM version (${Matlab_Java_VERSION_MAJOR_MINOR}) does not match installed Java SDK version (${Java_VERSION_MAJOR_MINOR})") - if(Matlab_Java_VERSION_MAJOR_MINOR VERSION_LESS Java_VERSION_MAJOR_MINOR) - set(CMAKE_JAVA_COMPILE_FLAGS -source ${Matlab_Java_VERSION_MAJOR_MINOR} -target ${Matlab_Java_VERSION_MAJOR_MINOR}) - endif() - endif() - else() - message(WARNING "Could not determine MATLAB JVM version") - endif() endif() endif()