diff options
author | Brad King <brad.king@kitware.com> | 2022-03-22 13:00:10 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-03-22 13:00:16 (GMT) |
commit | 621120e87c0b4d48bbf0e6af1a1b26e8bb410830 (patch) | |
tree | 195ac1a80f7132916b67bd4cda45f55599a54d20 | |
parent | c49480377817184b23c36d8c09d2181c0ecad654 (diff) | |
parent | 8468dfb35f427853f8bd3006160c0e66960f6f39 (diff) | |
download | CMake-621120e87c0b4d48bbf0e6af1a1b26e8bb410830.zip CMake-621120e87c0b4d48bbf0e6af1a1b26e8bb410830.tar.gz CMake-621120e87c0b4d48bbf0e6af1a1b26e8bb410830.tar.bz2 |
Merge topic 'FindMatlab-batch'
8468dfb35f FindMatlab: Use -batch option in matlab_add_unit_test if possible
ebb0685824 FindMatlab: Add fallback to use -batch option in version extraction
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !7088
-rw-r--r-- | Modules/FindMatlab.cmake | 65 | ||||
-rw-r--r-- | Modules/MatlabTestsRedirect.cmake | 3 |
2 files changed, 60 insertions, 8 deletions
diff --git a/Modules/FindMatlab.cmake b/Modules/FindMatlab.cmake index 17c1fa1..81bc473 100644 --- a/Modules/FindMatlab.cmake +++ b/Modules/FindMatlab.cmake @@ -769,6 +769,10 @@ function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_ve set(devnull INPUT_FILE NUL) endif() + # we first try to run a simple program using the -r option, and then we use the + # -batch option that is supported and recommended since R2019a + set(_matlab_get_version_failed_with_r_option FALSE) + # timeout set to 120 seconds, in case it does not start # note as said before OUTPUT_VARIABLE cannot be used in a platform # independent manner however, not setting it would flush the output of Matlab @@ -786,21 +790,57 @@ function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_ve if(_matlab_result_version_call MATCHES "timeout") if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Unable to determine the version of Matlab." - " Matlab call timed out after 120 seconds.") + " Matlab call with -r option timed out after 120 seconds.") endif() - return() + set(_matlab_get_version_failed_with_r_option TRUE) endif() - if(${_matlab_result_version_call}) + if(NOT ${_matlab_get_version_failed_with_r_option} AND ${_matlab_result_version_call}) if(MATLAB_FIND_DEBUG) - message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call returned with error ${_matlab_result_version_call}.") + message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call with -r option returned with error ${_matlab_result_version_call}.") endif() - return() - elseif(NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") + set(_matlab_get_version_failed_with_r_option TRUE) + elseif(NOT ${_matlab_get_version_failed_with_r_option} AND NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Unable to determine the version of Matlab. The log file does not exist.") endif() - return() + set(_matlab_get_version_failed_with_r_option TRUE) + endif() + + if(_matlab_get_version_failed_with_r_option) + execute_process( + COMMAND "${matlab_binary_program}" -nosplash -nojvm ${_matlab_additional_commands} -logfile "matlabVersionLog.cmaketmp" -nodesktop -nodisplay -batch "version, exit" + OUTPUT_VARIABLE _matlab_version_from_cmd_dummy_batch + RESULT_VARIABLE _matlab_result_version_call_batch + ERROR_VARIABLE _matlab_result_version_call_error_batch + TIMEOUT 120 + WORKING_DIRECTORY "${_matlab_temporary_folder}" + ${devnull} + ) + + if(_matlab_result_version_call_batch MATCHES "timeout") + if(MATLAB_FIND_DEBUG) + message(WARNING "[MATLAB] Unable to determine the version of Matlab." + " Matlab call with -batch option timed out after 120 seconds.") + endif() + return() + endif() + + if(${_matlab_result_version_call_batch}) + if(MATLAB_FIND_DEBUG) + message(WARNING "[MATLAB] Command executed \"${matlab_binary_program}\" -nosplash -nojvm ${_matlab_additional_commands} -logfile \"matlabVersionLog.cmaketmp\" -nodesktop -nodisplay -batch \"version, exit\"") + message(WARNING "_matlab_version_from_cmd_dummy_batch (OUTPUT_VARIABLE): ${_matlab_version_from_cmd_dummy_batch}") + message(WARNING "_matlab_result_version_call_batch (RESULT_VARIABLE): ${_matlab_result_version_call_batch}") + message(WARNING "_matlab_result_version_call_error_batch (ERROR_VARIABLE): ${_matlab_result_version_call_error_batch}") + message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call with -batch option returned with error ${_matlab_result_version_call_batch}.") + endif() + return() + elseif(NOT ${_matlab_get_version_failed_with_r_option} AND NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") + if(MATLAB_FIND_DEBUG) + message(WARNING "[MATLAB] Unable to determine the version of Matlab. The log file does not exist.") + endif() + return() + endif() endif() # if successful, read back the log @@ -928,6 +968,16 @@ function(matlab_add_unit_test) message(FATAL_ERROR "[MATLAB] The Matlab test name cannot be empty") endif() + # The option to run a batch program with MATLAB changes depending on the MATLAB version + # For MATLAB before R2019a (9.6), the only supported option is -r, afterwords the suggested option + # is -batch as -r is deprecated + set(maut_BATCH_OPTION "-r") + if(NOT (Matlab_VERSION_STRING STREQUAL "")) + if(Matlab_VERSION_STRING VERSION_GREATER_EQUAL "9.6") + set(maut_BATCH_OPTION "-batch") + endif() + endif() + add_test(NAME ${${prefix}_NAME} COMMAND ${CMAKE_COMMAND} "-Dtest_name=${${prefix}_NAME}" @@ -941,6 +991,7 @@ function(matlab_add_unit_test) "-Dunittest_file_to_run=${${prefix}_UNITTEST_FILE}" "-Dcustom_Matlab_test_command=${${prefix}_CUSTOM_TEST_COMMAND}" "-Dcmd_to_run_before_test=${${prefix}_UNITTEST_PRECOMMAND}" + "-Dmaut_BATCH_OPTION=${maut_BATCH_OPTION}" -P ${_FindMatlab_SELF_DIR}/MatlabTestsRedirect.cmake ${${prefix}_TEST_ARGS} ${${prefix}_UNPARSED_ARGUMENTS} diff --git a/Modules/MatlabTestsRedirect.cmake b/Modules/MatlabTestsRedirect.cmake index fc36fc3..d651cdd 100644 --- a/Modules/MatlabTestsRedirect.cmake +++ b/Modules/MatlabTestsRedirect.cmake @@ -15,6 +15,7 @@ # -Dcustom_Matlab_test_command="" # -Dcmd_to_run_before_test="" # -Dunittest_file_to_run +# -Dmaut_BATCH_OPTION="-batch" # -P FindMatlab_TestsRedirect.cmake set(Matlab_UNIT_TESTS_CMD -nosplash -nodesktop -nodisplay ${Matlab_ADDITIONAL_STARTUP_OPTIONS}) @@ -84,7 +85,7 @@ execute_process( # Do not use a full path to log file. Depend on the fact that the log file # is always going to go in the working_directory. This is because matlab # on unix is a shell script that does not handle spaces in the logfile path. - COMMAND "${Matlab_PROGRAM}" ${Matlab_UNIT_TESTS_CMD} -logfile "${log_file_name}" -r "${Matlab_SCRIPT_TO_RUN}" + COMMAND "${Matlab_PROGRAM}" ${Matlab_UNIT_TESTS_CMD} -logfile "${log_file_name}" "${maut_BATCH_OPTION}" "${Matlab_SCRIPT_TO_RUN}" RESULT_VARIABLE res ${test_timeout} OUTPUT_QUIET # we do not want the output twice |