FindRuby.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindRuby
  5. --------
  6. Find Ruby
  7. This module finds if Ruby is installed and determines where the
  8. include files and libraries are. Ruby 1.8, 1.9, 2.0 and 2.1 are
  9. supported.
  10. The minimum required version of Ruby can be specified using the
  11. standard syntax, e.g. find_package(Ruby 1.8)
  12. It also determines what the name of the library is. This code sets
  13. the following variables:
  14. ``RUBY_EXECUTABLE``
  15. full path to the ruby binary
  16. ``RUBY_INCLUDE_DIRS``
  17. include dirs to be used when using the ruby library
  18. ``RUBY_LIBRARY``
  19. full path to the ruby library
  20. ``RUBY_VERSION``
  21. the version of ruby which was found, e.g. "1.8.7"
  22. ``RUBY_FOUND``
  23. set to true if ruby ws found successfully
  24. Also:
  25. ``RUBY_INCLUDE_PATH``
  26. same as RUBY_INCLUDE_DIRS, only provided for compatibility reasons, don't use it
  27. #]=======================================================================]
  28. # RUBY_ARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"archdir"@:>@)'`
  29. # RUBY_SITEARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitearchdir"@:>@)'`
  30. # RUBY_SITEDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitelibdir"@:>@)'`
  31. # RUBY_LIBDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"libdir"@:>@)'`
  32. # RUBY_LIBRUBYARG=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"LIBRUBYARG_SHARED"@:>@)'`
  33. # uncomment the following line to get debug output for this file
  34. # set(_RUBY_DEBUG_OUTPUT TRUE)
  35. # Determine the list of possible names of the ruby executable depending
  36. # on which version of ruby is required
  37. set(_RUBY_POSSIBLE_EXECUTABLE_NAMES ruby)
  38. # if 1.9 is required, don't look for ruby18 and ruby1.8, default to version 1.8
  39. if(DEFINED Ruby_FIND_VERSION_MAJOR AND DEFINED Ruby_FIND_VERSION_MINOR)
  40. set(Ruby_FIND_VERSION_SHORT_NODOT "${Ruby_FIND_VERSION_MAJOR}${RUBY_FIND_VERSION_MINOR}")
  41. # we can't construct that if only major version is given
  42. set(_RUBY_POSSIBLE_EXECUTABLE_NAMES
  43. ruby${Ruby_FIND_VERSION_MAJOR}.${Ruby_FIND_VERSION_MINOR}
  44. ruby${Ruby_FIND_VERSION_MAJOR}${Ruby_FIND_VERSION_MINOR}
  45. ${_RUBY_POSSIBLE_EXECUTABLE_NAMES})
  46. else()
  47. set(Ruby_FIND_VERSION_SHORT_NODOT "18")
  48. endif()
  49. if(NOT Ruby_FIND_VERSION_EXACT)
  50. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby2.4 ruby24)
  51. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby2.3 ruby23)
  52. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby2.2 ruby22)
  53. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby2.1 ruby21)
  54. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby2.0 ruby20)
  55. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby1.9 ruby19)
  56. # if we want a version below 1.9, also look for ruby 1.8
  57. if("${Ruby_FIND_VERSION_SHORT_NODOT}" VERSION_LESS "19")
  58. list(APPEND _RUBY_POSSIBLE_EXECUTABLE_NAMES ruby1.8 ruby18)
  59. endif()
  60. list(REMOVE_DUPLICATES _RUBY_POSSIBLE_EXECUTABLE_NAMES)
  61. endif()
  62. find_program(RUBY_EXECUTABLE NAMES ${_RUBY_POSSIBLE_EXECUTABLE_NAMES})
  63. if(RUBY_EXECUTABLE AND NOT RUBY_VERSION_MAJOR)
  64. function(_RUBY_CONFIG_VAR RBVAR OUTVAR)
  65. execute_process(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print RbConfig::CONFIG['${RBVAR}']"
  66. RESULT_VARIABLE _RUBY_SUCCESS
  67. OUTPUT_VARIABLE _RUBY_OUTPUT
  68. ERROR_QUIET)
  69. if(_RUBY_SUCCESS OR _RUBY_OUTPUT STREQUAL "")
  70. execute_process(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['${RBVAR}']"
  71. RESULT_VARIABLE _RUBY_SUCCESS
  72. OUTPUT_VARIABLE _RUBY_OUTPUT
  73. ERROR_QUIET)
  74. endif()
  75. set(${OUTVAR} "${_RUBY_OUTPUT}" PARENT_SCOPE)
  76. endfunction()
  77. # query the ruby version
  78. _RUBY_CONFIG_VAR("MAJOR" RUBY_VERSION_MAJOR)
  79. _RUBY_CONFIG_VAR("MINOR" RUBY_VERSION_MINOR)
  80. _RUBY_CONFIG_VAR("TEENY" RUBY_VERSION_PATCH)
  81. # query the different directories
  82. _RUBY_CONFIG_VAR("archdir" RUBY_ARCH_DIR)
  83. _RUBY_CONFIG_VAR("arch" RUBY_ARCH)
  84. _RUBY_CONFIG_VAR("rubyhdrdir" RUBY_HDR_DIR)
  85. _RUBY_CONFIG_VAR("rubyarchhdrdir" RUBY_ARCHHDR_DIR)
  86. _RUBY_CONFIG_VAR("libdir" RUBY_POSSIBLE_LIB_DIR)
  87. _RUBY_CONFIG_VAR("rubylibdir" RUBY_RUBY_LIB_DIR)
  88. # site_ruby
  89. _RUBY_CONFIG_VAR("sitearchdir" RUBY_SITEARCH_DIR)
  90. _RUBY_CONFIG_VAR("sitelibdir" RUBY_SITELIB_DIR)
  91. # vendor_ruby available ?
  92. execute_process(COMMAND ${RUBY_EXECUTABLE} -r vendor-specific -e "print 'true'"
  93. OUTPUT_VARIABLE RUBY_HAS_VENDOR_RUBY ERROR_QUIET)
  94. if(RUBY_HAS_VENDOR_RUBY)
  95. _RUBY_CONFIG_VAR("vendorlibdir" RUBY_VENDORLIB_DIR)
  96. _RUBY_CONFIG_VAR("vendorarchdir" RUBY_VENDORARCH_DIR)
  97. endif()
  98. # save the results in the cache so we don't have to run ruby the next time again
  99. set(RUBY_VERSION_MAJOR ${RUBY_VERSION_MAJOR} CACHE PATH "The Ruby major version" FORCE)
  100. set(RUBY_VERSION_MINOR ${RUBY_VERSION_MINOR} CACHE PATH "The Ruby minor version" FORCE)
  101. set(RUBY_VERSION_PATCH ${RUBY_VERSION_PATCH} CACHE PATH "The Ruby patch version" FORCE)
  102. set(RUBY_ARCH_DIR ${RUBY_ARCH_DIR} CACHE PATH "The Ruby arch dir" FORCE)
  103. set(RUBY_HDR_DIR ${RUBY_HDR_DIR} CACHE PATH "The Ruby header dir (1.9+)" FORCE)
  104. set(RUBY_ARCHHDR_DIR ${RUBY_ARCHHDR_DIR} CACHE PATH "The Ruby arch header dir (2.0+)" FORCE)
  105. set(RUBY_POSSIBLE_LIB_DIR ${RUBY_POSSIBLE_LIB_DIR} CACHE PATH "The Ruby lib dir" FORCE)
  106. set(RUBY_RUBY_LIB_DIR ${RUBY_RUBY_LIB_DIR} CACHE PATH "The Ruby ruby-lib dir" FORCE)
  107. set(RUBY_SITEARCH_DIR ${RUBY_SITEARCH_DIR} CACHE PATH "The Ruby site arch dir" FORCE)
  108. set(RUBY_SITELIB_DIR ${RUBY_SITELIB_DIR} CACHE PATH "The Ruby site lib dir" FORCE)
  109. set(RUBY_HAS_VENDOR_RUBY ${RUBY_HAS_VENDOR_RUBY} CACHE BOOL "Vendor Ruby is available" FORCE)
  110. set(RUBY_VENDORARCH_DIR ${RUBY_VENDORARCH_DIR} CACHE PATH "The Ruby vendor arch dir" FORCE)
  111. set(RUBY_VENDORLIB_DIR ${RUBY_VENDORLIB_DIR} CACHE PATH "The Ruby vendor lib dir" FORCE)
  112. mark_as_advanced(
  113. RUBY_ARCH_DIR
  114. RUBY_ARCH
  115. RUBY_HDR_DIR
  116. RUBY_ARCHHDR_DIR
  117. RUBY_POSSIBLE_LIB_DIR
  118. RUBY_RUBY_LIB_DIR
  119. RUBY_SITEARCH_DIR
  120. RUBY_SITELIB_DIR
  121. RUBY_HAS_VENDOR_RUBY
  122. RUBY_VENDORARCH_DIR
  123. RUBY_VENDORLIB_DIR
  124. RUBY_VERSION_MAJOR
  125. RUBY_VERSION_MINOR
  126. RUBY_VERSION_PATCH
  127. )
  128. endif()
  129. # In case RUBY_EXECUTABLE could not be executed (e.g. cross compiling)
  130. # try to detect which version we found. This is not too good.
  131. if(RUBY_EXECUTABLE AND NOT RUBY_VERSION_MAJOR)
  132. # by default assume 1.8.0
  133. set(RUBY_VERSION_MAJOR 1)
  134. set(RUBY_VERSION_MINOR 8)
  135. set(RUBY_VERSION_PATCH 0)
  136. # check whether we found 1.9.x
  137. if(${RUBY_EXECUTABLE} MATCHES "ruby1\\.?9")
  138. set(RUBY_VERSION_MAJOR 1)
  139. set(RUBY_VERSION_MINOR 9)
  140. endif()
  141. # check whether we found 2.0.x
  142. if(${RUBY_EXECUTABLE} MATCHES "ruby2\\.?0")
  143. set(RUBY_VERSION_MAJOR 2)
  144. set(RUBY_VERSION_MINOR 0)
  145. endif()
  146. # check whether we found 2.1.x
  147. if(${RUBY_EXECUTABLE} MATCHES "ruby2\\.?1")
  148. set(RUBY_VERSION_MAJOR 2)
  149. set(RUBY_VERSION_MINOR 1)
  150. endif()
  151. # check whether we found 2.2.x
  152. if(${RUBY_EXECUTABLE} MATCHES "ruby2\\.?2")
  153. set(RUBY_VERSION_MAJOR 2)
  154. set(RUBY_VERSION_MINOR 2)
  155. endif()
  156. # check whether we found 2.3.x
  157. if(${RUBY_EXECUTABLE} MATCHES "ruby2\\.?3")
  158. set(RUBY_VERSION_MAJOR 2)
  159. set(RUBY_VERSION_MINOR 3)
  160. endif()
  161. # check whether we found 2.4.x
  162. if(${RUBY_EXECUTABLE} MATCHES "ruby2\\.?4")
  163. set(RUBY_VERSION_MAJOR 2)
  164. set(RUBY_VERSION_MINOR 4)
  165. endif()
  166. endif()
  167. if(RUBY_VERSION_MAJOR)
  168. set(RUBY_VERSION "${RUBY_VERSION_MAJOR}.${RUBY_VERSION_MINOR}.${RUBY_VERSION_PATCH}")
  169. set(_RUBY_VERSION_SHORT "${RUBY_VERSION_MAJOR}.${RUBY_VERSION_MINOR}")
  170. set(_RUBY_VERSION_SHORT_NODOT "${RUBY_VERSION_MAJOR}${RUBY_VERSION_MINOR}")
  171. set(_RUBY_NODOT_VERSION "${RUBY_VERSION_MAJOR}${RUBY_VERSION_MINOR}${RUBY_VERSION_PATCH}")
  172. endif()
  173. find_path(RUBY_INCLUDE_DIR
  174. NAMES ruby.h
  175. HINTS
  176. ${RUBY_HDR_DIR}
  177. ${RUBY_ARCH_DIR}
  178. /usr/lib/ruby/${_RUBY_VERSION_SHORT}/i586-linux-gnu/
  179. )
  180. set(RUBY_INCLUDE_DIRS ${RUBY_INCLUDE_DIR} )
  181. # if ruby > 1.8 is required or if ruby > 1.8 was found, search for the config.h dir
  182. if( "${Ruby_FIND_VERSION_SHORT_NODOT}" GREATER 18 OR "${_RUBY_VERSION_SHORT_NODOT}" GREATER 18 OR RUBY_HDR_DIR)
  183. find_path(RUBY_CONFIG_INCLUDE_DIR
  184. NAMES ruby/config.h config.h
  185. HINTS
  186. ${RUBY_HDR_DIR}/${RUBY_ARCH}
  187. ${RUBY_ARCH_DIR}
  188. ${RUBY_ARCHHDR_DIR}
  189. )
  190. set(RUBY_INCLUDE_DIRS ${RUBY_INCLUDE_DIRS} ${RUBY_CONFIG_INCLUDE_DIR} )
  191. endif()
  192. # Determine the list of possible names for the ruby library
  193. set(_RUBY_POSSIBLE_LIB_NAMES ruby ruby-static ruby${_RUBY_VERSION_SHORT} ruby${_RUBY_VERSION_SHORT_NODOT} ruby-${_RUBY_VERSION_SHORT} ruby-${RUBY_VERSION})
  194. if(WIN32)
  195. set( _RUBY_MSVC_RUNTIME "" )
  196. if( MSVC_VERSION EQUAL 1200 )
  197. set( _RUBY_MSVC_RUNTIME "60" )
  198. endif()
  199. if( MSVC_VERSION EQUAL 1300 )
  200. set( _RUBY_MSVC_RUNTIME "70" )
  201. endif()
  202. if( MSVC_VERSION EQUAL 1310 )
  203. set( _RUBY_MSVC_RUNTIME "71" )
  204. endif()
  205. if( MSVC_VERSION EQUAL 1400 )
  206. set( _RUBY_MSVC_RUNTIME "80" )
  207. endif()
  208. if( MSVC_VERSION EQUAL 1500 )
  209. set( _RUBY_MSVC_RUNTIME "90" )
  210. endif()
  211. set(_RUBY_ARCH_PREFIX "")
  212. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  213. set(_RUBY_ARCH_PREFIX "x64-")
  214. endif()
  215. list(APPEND _RUBY_POSSIBLE_LIB_NAMES
  216. "${_RUBY_ARCH_PREFIX}msvcr${_RUBY_MSVC_RUNTIME}-ruby${_RUBY_NODOT_VERSION}"
  217. "${_RUBY_ARCH_PREFIX}msvcr${_RUBY_MSVC_RUNTIME}-ruby${_RUBY_NODOT_VERSION}-static"
  218. "${_RUBY_ARCH_PREFIX}msvcrt-ruby${_RUBY_NODOT_VERSION}"
  219. "${_RUBY_ARCH_PREFIX}msvcrt-ruby${_RUBY_NODOT_VERSION}-static" )
  220. endif()
  221. find_library(RUBY_LIBRARY NAMES ${_RUBY_POSSIBLE_LIB_NAMES} HINTS ${RUBY_POSSIBLE_LIB_DIR} )
  222. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  223. set(_RUBY_REQUIRED_VARS RUBY_EXECUTABLE RUBY_INCLUDE_DIR RUBY_LIBRARY)
  224. if(_RUBY_VERSION_SHORT_NODOT GREATER 18)
  225. list(APPEND _RUBY_REQUIRED_VARS RUBY_CONFIG_INCLUDE_DIR)
  226. endif()
  227. if(_RUBY_DEBUG_OUTPUT)
  228. message(STATUS "--------FindRuby.cmake debug------------")
  229. message(STATUS "_RUBY_POSSIBLE_EXECUTABLE_NAMES: ${_RUBY_POSSIBLE_EXECUTABLE_NAMES}")
  230. message(STATUS "_RUBY_POSSIBLE_LIB_NAMES: ${_RUBY_POSSIBLE_LIB_NAMES}")
  231. message(STATUS "RUBY_ARCH_DIR: ${RUBY_ARCH_DIR}")
  232. message(STATUS "RUBY_HDR_DIR: ${RUBY_HDR_DIR}")
  233. message(STATUS "RUBY_POSSIBLE_LIB_DIR: ${RUBY_POSSIBLE_LIB_DIR}")
  234. message(STATUS "Found RUBY_VERSION: \"${RUBY_VERSION}\" , short: \"${_RUBY_VERSION_SHORT}\", nodot: \"${_RUBY_VERSION_SHORT_NODOT}\"")
  235. message(STATUS "_RUBY_REQUIRED_VARS: ${_RUBY_REQUIRED_VARS}")
  236. message(STATUS "RUBY_EXECUTABLE: ${RUBY_EXECUTABLE}")
  237. message(STATUS "RUBY_LIBRARY: ${RUBY_LIBRARY}")
  238. message(STATUS "RUBY_INCLUDE_DIR: ${RUBY_INCLUDE_DIR}")
  239. message(STATUS "RUBY_CONFIG_INCLUDE_DIR: ${RUBY_CONFIG_INCLUDE_DIR}")
  240. message(STATUS "--------------------")
  241. endif()
  242. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Ruby REQUIRED_VARS ${_RUBY_REQUIRED_VARS}
  243. VERSION_VAR RUBY_VERSION )
  244. mark_as_advanced(
  245. RUBY_EXECUTABLE
  246. RUBY_LIBRARY
  247. RUBY_INCLUDE_DIR
  248. RUBY_CONFIG_INCLUDE_DIR
  249. )
  250. # Set some variables for compatibility with previous version of this file
  251. set(RUBY_POSSIBLE_LIB_PATH ${RUBY_POSSIBLE_LIB_DIR})
  252. set(RUBY_RUBY_LIB_PATH ${RUBY_RUBY_LIB_DIR})
  253. set(RUBY_INCLUDE_PATH ${RUBY_INCLUDE_DIRS})