From 1dc2e449f88fe56dbb29187224efd1d4690a96fa Mon Sep 17 00:00:00 2001
From: Raffi Enficiaud <raffi.enficiaud@tuebingen.mpg.de>
Date: Thu, 19 Feb 2015 12:41:23 +0100
Subject: [PATCH] Fixing problems related to spaces in directory names

---
 Modules/FindMatlab.cmake          | 17 +++++++++--------
 Modules/MatlabTestsRedirect.cmake |  5 +++--
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/Modules/FindMatlab.cmake b/Modules/FindMatlab.cmake
index 6185935..05b5eda 100644
--- a/Modules/FindMatlab.cmake
+++ b/Modules/FindMatlab.cmake
@@ -235,8 +235,8 @@ set(MATLAB_VERSIONS_MAPPING
 # temporary folder for all Matlab runs
 set(_matlab_temporary_folder ${CMAKE_BINARY_DIR}/Matlab)
 
-if(NOT EXISTS ${_matlab_temporary_folder})
-  file(MAKE_DIRECTORY ${_matlab_temporary_folder})
+if(NOT EXISTS "${_matlab_temporary_folder}")
+  file(MAKE_DIRECTORY "${_matlab_temporary_folder}")
 endif()
 
 #.rst:
@@ -590,11 +590,11 @@ function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_ve
     message(STATUS "[MATLAB] Determining the version of Matlab from ${matlab_binary_program}")
   endif()
 
-  if(EXISTS ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp)
+  if(EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
     if(MATLAB_FIND_DEBUG)
       message(STATUS "[MATLAB] Removing previous ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp file")
     endif()
-    file(REMOVE ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp)
+    file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
   endif()
 
 
@@ -611,10 +611,11 @@ function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_ve
   # independent manner however, not setting it would flush the output of Matlab
   # in the current console (unix variant)
   execute_process(
-    COMMAND ${matlab_binary_program} -nosplash -nojvm ${_matlab_additional_commands} -logfile ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp -nodesktop -nodisplay -r "version, exit"
+    COMMAND "${matlab_binary_program}" -nosplash -nojvm ${_matlab_additional_commands} -logfile "matlabVersionLog.cmaketmp" -nodesktop -nodisplay -r "version, exit"
     OUTPUT_VARIABLE _matlab_version_from_cmd_dummy
     RESULT_VARIABLE _matlab_result_version_call
     TIMEOUT 30
+    WORKING_DIRECTORY "${_matlab_temporary_folder}"
     )
 
 
@@ -623,7 +624,7 @@ function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_ve
       message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call returned with error ${_matlab_result_version_call}.")
     endif()
     return()
-  elseif(NOT EXISTS ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp)
+  elseif(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()
@@ -631,8 +632,8 @@ function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_ve
   endif()
 
   # if successful, read back the log
-  file(READ ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp _matlab_version_from_cmd)
-  file(REMOVE ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp)
+  file(READ "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp" _matlab_version_from_cmd)
+  file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
 
   set(index -1)
   string(FIND ${_matlab_version_from_cmd} "ans" index)
diff --git a/Modules/MatlabTestsRedirect.cmake b/Modules/MatlabTestsRedirect.cmake
index 46d1ef8..804101c 100644
--- a/Modules/MatlabTestsRedirect.cmake
+++ b/Modules/MatlabTestsRedirect.cmake
@@ -60,13 +60,14 @@ set(Matlab_SCRIPT_TO_RUN
     "addpath(${concat_string}), path, ${cmd_to_run_before_test}, ${unittest_to_run}"
    )
 
-set(Matlab_LOG_FILE ${output_directory}/${test_name}.log)
+set(Matlab_LOG_FILE "${output_directory}/${test_name}.log")
 
 execute_process(
-  COMMAND ${Matlab_PROGRAM} ${Matlab_UNIT_TESTS_CMD} -logfile ${Matlab_LOG_FILE} -r ${Matlab_SCRIPT_TO_RUN}
+  COMMAND "${Matlab_PROGRAM}" ${Matlab_UNIT_TESTS_CMD} -logfile "${test_name}.log" -r "${Matlab_SCRIPT_TO_RUN}"
   RESULT_VARIABLE res
   TIMEOUT ${test_timeout}
   OUTPUT_QUIET # we do not want the output twice
+  WORKING_DIRECTORY "${output_directory}"
   )
 
 if(NOT EXISTS ${Matlab_LOG_FILE})
-- 
1.9.1

