From e6699b9b59a12f6fda8f535d370453bb41f3acce Mon Sep 17 00:00:00 2001
From: Julien Marrec <julien.marrec@gmail.com>
Date: Wed, 11 Mar 2020 18:37:19 +0100
Subject: FindRuby: Validate Ruby_EXECUTABLE before accepting it

---
 Modules/FindRuby.cmake        | 40 ++++++++++++++++++++++++++++++++++++++++
 Tests/FindRuby/CMakeLists.txt |  2 +-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/Modules/FindRuby.cmake b/Modules/FindRuby.cmake
index 1e010bf..699d2b6 100644
--- a/Modules/FindRuby.cmake
+++ b/Modules/FindRuby.cmake
@@ -125,10 +125,50 @@ if(_Ruby_DEBUG_OUTPUT)
   message("_Ruby_POSSIBLE_EXECUTABLE_NAMES=${_Ruby_POSSIBLE_EXECUTABLE_NAMES}")
 endif()
 
+function (_RUBY_VALIDATE_INTERPRETER)
+  if (NOT Ruby_EXECUTABLE)
+    return()
+  endif()
+
+  cmake_parse_arguments (PARSE_ARGV 0 _RVI "EXACT;CHECK_EXISTS" "" "")
+  if (_RVI_UNPARSED_ARGUMENTS)
+    set (expected_version ${_RVI_UNPARSED_ARGUMENTS})
+  else()
+    unset (expected_version)
+  endif()
+
+  if (_RVI_CHECK_EXISTS AND NOT EXISTS "${Ruby_EXECUTABLE}")
+    # interpreter does not exist anymore
+    set (_Ruby_Interpreter_REASON_FAILURE "Cannot find the interpreter \"${Ruby_EXECUTABLE}\"")
+    set_property (CACHE Ruby_EXECUTABLE PROPERTY VALUE "Ruby_EXECUTABLE-NOTFOUND")
+    return()
+  endif()
+
+  # Check the version it returns
+  # executable found must have a specific version
+  execute_process (COMMAND "${Ruby_EXECUTABLE}" -e "puts RUBY_VERSION"
+                   RESULT_VARIABLE result
+                   OUTPUT_VARIABLE version
+                   ERROR_QUIET
+                   OUTPUT_STRIP_TRAILING_WHITESPACE)
+  if (result OR (_RVI_EXACT AND NOT version VERSION_EQUAL expected_version) OR (version VERSION_LESS expected_version))
+    # interpreter not usable or has wrong major version
+    if (result)
+      set (_Ruby_Interpreter_REASON_FAILURE "Cannot use the interpreter \"${Ruby_EXECUTABLE}\"")
+    else()
+      set (_Ruby_Interpreter_REASON_FAILURE "Wrong major version for the interpreter \"${Ruby_EXECUTABLE}\"")
+    endif()
+    set_property (CACHE Ruby_EXECUTABLE PROPERTY VALUE "Ruby_EXECUTABLE-NOTFOUND")
+    return()
+  endif()
+
+endfunction()
+
 find_program (Ruby_EXECUTABLE
   NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}
   NAMES_PER_DIR
   )
+_RUBY_VALIDATE_INTERPRETER (${Ruby_FIND_VERSION})
 
 if(Ruby_EXECUTABLE AND NOT Ruby_VERSION_MAJOR)
   function(_RUBY_CONFIG_VAR RBVAR OUTVAR)
diff --git a/Tests/FindRuby/CMakeLists.txt b/Tests/FindRuby/CMakeLists.txt
index 193cb4f..aaab839 100644
--- a/Tests/FindRuby/CMakeLists.txt
+++ b/Tests/FindRuby/CMakeLists.txt
@@ -24,7 +24,7 @@ if(CMake_TEST_FindRuby)
     --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
     )
   set_tests_properties(FindRuby.Fail PROPERTIES
-    PASS_REGULAR_EXPRESSION "Could NOT find Ruby: Found unsuitable version \".*\", but required is.*least \"[0-9]+\\.[0-9]+\\.[0-9]+\" \\(found .*\\)")
+    PASS_REGULAR_EXPRESSION "Could NOT find Ruby.*(Required is at least version \"[0-9]+\\.[0-9]+\\.[0-9]+\")")
 
   # Looks for 1.9.9 EXACTLY, which unlike the "FindRuby" test above will fail on every machine
   # since this version doesn't exist (ruby goes from 1.9.3 to 2.0.0)
-- 
cgit v0.12