Browse Source

FindRuby: Add support for versions up to 2.7

Fixes: #20370
Julien Marrec 5 years ago
parent
commit
46064c8193
1 changed files with 82 additions and 30 deletions
  1. 82 30
      Modules/FindRuby.cmake

+ 82 - 30
Modules/FindRuby.cmake

@@ -8,15 +8,27 @@ FindRuby
 Find Ruby
 
 This module finds if Ruby is installed and determines where the
-include files and libraries are.  Ruby 1.8, 1.9, 2.0 and 2.1 are
+include files and libraries are.  Ruby 1.8 through 2.7 are
 supported.
 
 The minimum required version of Ruby can be specified using the
-standard syntax, e.g.  find_package(Ruby 1.8)
+standard syntax, e.g.
 
-It also determines what the name of the library is.  This code sets
-the following variables:
+.. code-block:: cmake
 
+  find_package(Ruby 2.5.1 EXACT REQUIRED)
+  # OR
+  find_package(Ruby 2.4)
+
+It also determines what the name of the library is.
+
+Result Variables
+^^^^^^^^^^^^^^^^
+
+This module will set the following variables in your project:
+
+``Ruby_FOUND``
+  set to true if ruby was found successfully
 ``Ruby_EXECUTABLE``
   full path to the ruby binary
 ``Ruby_INCLUDE_DIRS``
@@ -25,8 +37,13 @@ the following variables:
   libraries needed to use ruby from C.
 ``Ruby_VERSION``
   the version of ruby which was found, e.g. "1.8.7"
-``Ruby_FOUND``
-  set to true if ruby ws found successfully
+``Ruby_VERSION_MAJOR``
+  Ruby major version.
+``Ruby_VERSION_MINOR``
+  Ruby minor version.
+``Ruby_VERSION_PATCH``
+  Ruby patch version.
+
 
 Also:
 
@@ -63,37 +80,57 @@ endforeach()
 # on which version of ruby is required
 set(_Ruby_POSSIBLE_EXECUTABLE_NAMES ruby)
 
-# if 1.9 is required, don't look for ruby18 and ruby1.8, default to version 1.8
-if(DEFINED Ruby_FIND_VERSION_MAJOR AND DEFINED Ruby_FIND_VERSION_MINOR)
-  set(Ruby_FIND_VERSION_SHORT_NODOT "${Ruby_FIND_VERSION_MAJOR}${Ruby_FIND_VERSION_MINOR}")
-  # we can't construct that if only major version is given
-  set(_Ruby_POSSIBLE_EXECUTABLE_NAMES
-    ruby${Ruby_FIND_VERSION_MAJOR}.${Ruby_FIND_VERSION_MINOR}
-    ruby${Ruby_FIND_VERSION_MAJOR}${Ruby_FIND_VERSION_MINOR}
-    ${_Ruby_POSSIBLE_EXECUTABLE_NAMES})
-else()
-  set(Ruby_FIND_VERSION_SHORT_NODOT "18")
+# 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()
 
+if(_Ruby_DEBUG_OUTPUT)
+  message("Ruby_FIND_VERSION=${Ruby_FIND_VERSION}")
+  message("Ruby_FIND_VERSION_MAJOR=${Ruby_FIND_VERSION_MAJOR}")
+  message("Ruby_FIND_VERSION_MINOR=${Ruby_FIND_VERSION_MINOR}")
+  message("Ruby_FIND_VERSION_PATCH=${Ruby_FIND_VERSION_PATCH}")
+endif()
+
+set(Ruby_FIND_VERSION_SHORT_NODOT "${Ruby_FIND_VERSION_MAJOR}${Ruby_FIND_VERSION_MINOR}")
+
+# Set name of possible executables, ignoring the minor
+# Eg:
+# 2.1.1 => from ruby27 to ruby21 included
+# 2.1   => from ruby27 to ruby21 included
+# 2     => from ruby26 to ruby20 included
+# empty => from ruby27 to ruby18 included
 if(NOT Ruby_FIND_VERSION_EXACT)
-  list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.4 ruby24)
-  list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.3 ruby23)
-  list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.2 ruby22)
-  list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.1 ruby21)
-  list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.0 ruby20)
-  list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby1.9 ruby19)
-
-  # if we want a version below 1.9, also look for ruby 1.8
-  if("${Ruby_FIND_VERSION_SHORT_NODOT}" VERSION_LESS "19")
-    list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby1.8 ruby18)
-  endif()
+
+  foreach(_ruby_version RANGE 27 18 -1)
+    string(SUBSTRING "${_ruby_version}" 0 1 _ruby_major_version)
+    string(SUBSTRING "${_ruby_version}" 1 1 _ruby_minor_version)
+
+    if(NOT "${_ruby_major_version}${_ruby_minor_version}" VERSION_LESS ${Ruby_FIND_VERSION_SHORT_NODOT})
+      # Append both rubyX.Y and rubyXY (eg: ruby2.7 ruby27)
+      list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby${_ruby_major_version}.${_ruby_minor_version} ruby${_ruby_major_version}${_ruby_minor_version})
+    else()
+      break()
+    endif()
+
+  endforeach()
 
   list(REMOVE_DUPLICATES _Ruby_POSSIBLE_EXECUTABLE_NAMES)
 endif()
 
-find_program(Ruby_EXECUTABLE NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES})
+if(_Ruby_DEBUG_OUTPUT)
+  message("_Ruby_POSSIBLE_EXECUTABLE_NAMES=${_Ruby_POSSIBLE_EXECUTABLE_NAMES}")
+endif()
 
-if(Ruby_EXECUTABLE  AND NOT  Ruby_VERSION_MAJOR)
+find_program (Ruby_EXECUTABLE
+  NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}
+  NAMES_PER_DIR
+  )
+
+if(Ruby_EXECUTABLE AND NOT Ruby_VERSION_MAJOR)
   function(_RUBY_CONFIG_VAR RBVAR OUTVAR)
     execute_process(COMMAND ${Ruby_EXECUTABLE} -r rbconfig -e "print RbConfig::CONFIG['${RBVAR}']"
       RESULT_VARIABLE _Ruby_SUCCESS
@@ -205,6 +242,21 @@ if(Ruby_EXECUTABLE AND NOT Ruby_VERSION_MAJOR)
     set(Ruby_VERSION_MAJOR 2)
     set(Ruby_VERSION_MINOR 4)
   endif()
+  # check whether we found 2.5.x
+  if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?5")
+    set(Ruby_VERSION_MAJOR 2)
+    set(Ruby_VERSION_MINOR 5)
+  endif()
+  # check whether we found 2.6.x
+  if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?6")
+    set(Ruby_VERSION_MAJOR 2)
+    set(Ruby_VERSION_MINOR 6)
+  endif()
+  # check whether we found 2.7.x
+  if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?7")
+    set(Ruby_VERSION_MAJOR 2)
+    set(Ruby_VERSION_MINOR 7)
+  endif()
 endif()
 
 if(Ruby_VERSION_MAJOR)
@@ -225,7 +277,7 @@ find_path(Ruby_INCLUDE_DIR
 set(Ruby_INCLUDE_DIRS ${Ruby_INCLUDE_DIR})
 
 # if ruby > 1.8 is required or if ruby > 1.8 was found, search for the config.h dir
-if( "${Ruby_FIND_VERSION_SHORT_NODOT}" GREATER 18  OR  "${_Ruby_VERSION_SHORT_NODOT}" GREATER 18  OR  Ruby_HDR_DIR)
+if( Ruby_FIND_VERSION VERSION_GREATER_EQUAL "1.9"  OR  Ruby_VERSION VERSION_GREATER_EQUAL "1.9"  OR  Ruby_HDR_DIR)
   find_path(Ruby_CONFIG_INCLUDE_DIR
     NAMES ruby/config.h  config.h
     HINTS