FindRuby: Use find_program VALIDATOR

Avoid caching an unsuitable version of `Ruby_EXECUTABLE`.
This commit is contained in:
Charlie Savage
2025-01-12 16:53:43 -08:00
committed by Brad King
parent e891159657
commit 61c3718a00
2 changed files with 27 additions and 45 deletions

View File

@@ -112,14 +112,6 @@ endforeach()
# on which version of ruby is required
set(_Ruby_POSSIBLE_EXECUTABLE_NAMES ruby)
# If not specified, allow everything as far back as 1.8.0
if(NOT DEFINED Ruby_FIND_VERSION_MAJOR)
set(Ruby_FIND_VERSION "1.8.0")
set(Ruby_FIND_VERSION_MAJOR 1)
set(Ruby_FIND_VERSION_MINOR 8)
set(Ruby_FIND_VERSION_PATCH 0)
endif()
# Set name of possible executables, ignoring the minor
# Eg:
# 3.2.6 => from ruby34 to ruby32 included
@@ -144,42 +136,34 @@ elseif (NOT DEFINED Ruby_FIND_VIRTUALENV)
set (Ruby_FIND_VIRTUALENV "FIRST")
endif()
function (_RUBY_VALIDATE_INTERPRETER)
if (NOT Ruby_EXECUTABLE)
function (_RUBY_VALIDATE_INTERPRETER result_var path)
# Get the interpreter version
execute_process (COMMAND "${path}" -e "puts RUBY_VERSION"
RESULT_VARIABLE result
OUTPUT_VARIABLE version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (NOT result EQUAL 0)
set (_Ruby_Interpreter_REASON_FAILURE "Cannot use the interpreter \"${path}\"")
set(${result_var} FALSE PARENT_SCOPE)
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}\"")
if (Ruby_FIND_VERSION)
if (Ruby_FIND_VERSION_EXACT AND NOT version VERSION_EQUAL Ruby_FIND_VERSION)
message(DEBUG "Incorrect Ruby found. Requested: ${Ruby_FIND_VERSION}. Found: ${version}. Path: \"${path}\"")
set(${result_var} FALSE PARENT_SCOPE)
return()
elseif (version VERSION_LESS Ruby_FIND_VERSION)
message(DEBUG "Ruby version is too old. Minimum: ${Ruby_FIND_VERSION}. Found: ${version}. Path: \"${path}\"")
set(${result_var} FALSE PARENT_SCOPE)
return()
endif()
set_property (CACHE Ruby_EXECUTABLE PROPERTY VALUE "Ruby_EXECUTABLE-NOTFOUND")
return()
endif()
# Found valid Ruby interpreter!
set(${result_var} TRUE PARENT_SCOPE)
endfunction()
function(_RUBY_CONFIG_VAR RBVAR OUTVAR)
@@ -207,13 +191,12 @@ function (_RUBY_CHECK_RVM)
NAMES_PER_DIR
PATHS ENV MY_RUBY_HOME
PATH_SUFFIXES bin Scripts
VALIDATOR _RUBY_VALIDATE_INTERPRETER
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH)
_RUBY_VALIDATE_INTERPRETER (${Ruby_FIND_VERSION}})
if(Ruby_EXECUTABLE)
set(Ruby_ENV "RVM" CACHE INTERNAL "Ruby environment")
endif()
@@ -223,9 +206,8 @@ endfunction()
function (_RUBY_CHECK_SYSTEM)
find_program (Ruby_EXECUTABLE
NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}
NAMES_PER_DIR)
_RUBY_VALIDATE_INTERPRETER (${Ruby_FIND_VERSION})
NAMES_PER_DIR
VALIDATOR _RUBY_VALIDATE_INTERPRETER)
if(Ruby_EXECUTABLE)
set(Ruby_ENV "Standard" CACHE INTERNAL "Ruby environment")

View File

@@ -39,7 +39,7 @@ if(CMake_TEST_FindRuby)
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
set_tests_properties(FindRuby.FailExact PROPERTIES
PASS_REGULAR_EXPRESSION "Could NOT find Ruby: Found unsuitable version \".*\", but required is.*exact version \"[0-9]+\\.[0-9]+\\.[0-9]+\" \\(found .*\\)")
PASS_REGULAR_EXPRESSION "Could NOT find Ruby.*Required[ \n]+is[ \n]+exact[ \n]+version[ \n]+\"1\\.9\\.9\"")
# RVM specific test
if(CMake_TEST_FindRuby_RVM)