FindRuby.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_LIBRARIES``
  19. libraries needed to use ruby from C.
  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. # Backwards compatibility
  29. # Define camel case versions of input variables
  30. foreach(UPPER
  31. RUBY_EXECUTABLE
  32. RUBY_LIBRARY
  33. RUBY_INCLUDE_DIR
  34. RUBY_CONFIG_INCLUDE_DIR
  35. )
  36. if (DEFINED ${UPPER})
  37. string(REPLACE "RUBY_" "Ruby_" Camel ${UPPER})
  38. if (NOT DEFINED ${Camel})
  39. set(${Camel} ${${UPPER}})
  40. endif()
  41. endif()
  42. endforeach()
  43. # Ruby_ARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"archdir"@:>@)'`
  44. # Ruby_SITEARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitearchdir"@:>@)'`
  45. # Ruby_SITEDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitelibdir"@:>@)'`
  46. # Ruby_LIBDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"libdir"@:>@)'`
  47. # Ruby_LIBRUBYARG=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"LIBRUBYARG_SHARED"@:>@)'`
  48. # uncomment the following line to get debug output for this file
  49. # set(_Ruby_DEBUG_OUTPUT TRUE)
  50. # Determine the list of possible names of the ruby executable depending
  51. # on which version of ruby is required
  52. set(_Ruby_POSSIBLE_EXECUTABLE_NAMES ruby)
  53. # if 1.9 is required, don't look for ruby18 and ruby1.8, default to version 1.8
  54. if(DEFINED Ruby_FIND_VERSION_MAJOR AND DEFINED Ruby_FIND_VERSION_MINOR)
  55. set(Ruby_FIND_VERSION_SHORT_NODOT "${Ruby_FIND_VERSION_MAJOR}${Ruby_FIND_VERSION_MINOR}")
  56. # we can't construct that if only major version is given
  57. set(_Ruby_POSSIBLE_EXECUTABLE_NAMES
  58. ruby${Ruby_FIND_VERSION_MAJOR}.${Ruby_FIND_VERSION_MINOR}
  59. ruby${Ruby_FIND_VERSION_MAJOR}${Ruby_FIND_VERSION_MINOR}
  60. ${_Ruby_POSSIBLE_EXECUTABLE_NAMES})
  61. else()
  62. set(Ruby_FIND_VERSION_SHORT_NODOT "18")
  63. endif()
  64. if(NOT Ruby_FIND_VERSION_EXACT)
  65. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.4 ruby24)
  66. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.3 ruby23)
  67. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.2 ruby22)
  68. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.1 ruby21)
  69. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby2.0 ruby20)
  70. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby1.9 ruby19)
  71. # if we want a version below 1.9, also look for ruby 1.8
  72. if("${Ruby_FIND_VERSION_SHORT_NODOT}" VERSION_LESS "19")
  73. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES ruby1.8 ruby18)
  74. endif()
  75. list(REMOVE_DUPLICATES _Ruby_POSSIBLE_EXECUTABLE_NAMES)
  76. endif()
  77. find_program(Ruby_EXECUTABLE NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES})
  78. if(Ruby_EXECUTABLE AND NOT Ruby_VERSION_MAJOR)
  79. function(_RUBY_CONFIG_VAR RBVAR OUTVAR)
  80. execute_process(COMMAND ${Ruby_EXECUTABLE} -r rbconfig -e "print RbConfig::CONFIG['${RBVAR}']"
  81. RESULT_VARIABLE _Ruby_SUCCESS
  82. OUTPUT_VARIABLE _Ruby_OUTPUT
  83. ERROR_QUIET)
  84. if(_Ruby_SUCCESS OR _Ruby_OUTPUT STREQUAL "")
  85. execute_process(COMMAND ${Ruby_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['${RBVAR}']"
  86. RESULT_VARIABLE _Ruby_SUCCESS
  87. OUTPUT_VARIABLE _Ruby_OUTPUT
  88. ERROR_QUIET)
  89. endif()
  90. set(${OUTVAR} "${_Ruby_OUTPUT}" PARENT_SCOPE)
  91. endfunction()
  92. # query the ruby version
  93. _RUBY_CONFIG_VAR("MAJOR" Ruby_VERSION_MAJOR)
  94. _RUBY_CONFIG_VAR("MINOR" Ruby_VERSION_MINOR)
  95. _RUBY_CONFIG_VAR("TEENY" Ruby_VERSION_PATCH)
  96. # query the different directories
  97. _RUBY_CONFIG_VAR("archdir" Ruby_ARCH_DIR)
  98. _RUBY_CONFIG_VAR("arch" Ruby_ARCH)
  99. _RUBY_CONFIG_VAR("rubyhdrdir" Ruby_HDR_DIR)
  100. _RUBY_CONFIG_VAR("rubyarchhdrdir" Ruby_ARCHHDR_DIR)
  101. _RUBY_CONFIG_VAR("libdir" Ruby_POSSIBLE_LIB_DIR)
  102. _RUBY_CONFIG_VAR("rubylibdir" Ruby_RUBY_LIB_DIR)
  103. # site_ruby
  104. _RUBY_CONFIG_VAR("sitearchdir" Ruby_SITEARCH_DIR)
  105. _RUBY_CONFIG_VAR("sitelibdir" Ruby_SITELIB_DIR)
  106. # vendor_ruby available ?
  107. execute_process(COMMAND ${Ruby_EXECUTABLE} -r vendor-specific -e "print 'true'"
  108. OUTPUT_VARIABLE Ruby_HAS_VENDOR_RUBY ERROR_QUIET)
  109. if(Ruby_HAS_VENDOR_RUBY)
  110. _RUBY_CONFIG_VAR("vendorlibdir" Ruby_VENDORLIB_DIR)
  111. _RUBY_CONFIG_VAR("vendorarchdir" Ruby_VENDORARCH_DIR)
  112. endif()
  113. # save the results in the cache so we don't have to run ruby the next time again
  114. set(Ruby_VERSION_MAJOR ${Ruby_VERSION_MAJOR} CACHE PATH "The Ruby major version" FORCE)
  115. set(Ruby_VERSION_MINOR ${Ruby_VERSION_MINOR} CACHE PATH "The Ruby minor version" FORCE)
  116. set(Ruby_VERSION_PATCH ${Ruby_VERSION_PATCH} CACHE PATH "The Ruby patch version" FORCE)
  117. set(Ruby_ARCH_DIR ${Ruby_ARCH_DIR} CACHE PATH "The Ruby arch dir" FORCE)
  118. set(Ruby_HDR_DIR ${Ruby_HDR_DIR} CACHE PATH "The Ruby header dir (1.9+)" FORCE)
  119. set(Ruby_ARCHHDR_DIR ${Ruby_ARCHHDR_DIR} CACHE PATH "The Ruby arch header dir (2.0+)" FORCE)
  120. set(Ruby_POSSIBLE_LIB_DIR ${Ruby_POSSIBLE_LIB_DIR} CACHE PATH "The Ruby lib dir" FORCE)
  121. set(Ruby_RUBY_LIB_DIR ${Ruby_RUBY_LIB_DIR} CACHE PATH "The Ruby ruby-lib dir" FORCE)
  122. set(Ruby_SITEARCH_DIR ${Ruby_SITEARCH_DIR} CACHE PATH "The Ruby site arch dir" FORCE)
  123. set(Ruby_SITELIB_DIR ${Ruby_SITELIB_DIR} CACHE PATH "The Ruby site lib dir" FORCE)
  124. set(Ruby_HAS_VENDOR_RUBY ${Ruby_HAS_VENDOR_RUBY} CACHE BOOL "Vendor Ruby is available" FORCE)
  125. set(Ruby_VENDORARCH_DIR ${Ruby_VENDORARCH_DIR} CACHE PATH "The Ruby vendor arch dir" FORCE)
  126. set(Ruby_VENDORLIB_DIR ${Ruby_VENDORLIB_DIR} CACHE PATH "The Ruby vendor lib dir" FORCE)
  127. mark_as_advanced(
  128. Ruby_ARCH_DIR
  129. Ruby_ARCH
  130. Ruby_HDR_DIR
  131. Ruby_ARCHHDR_DIR
  132. Ruby_POSSIBLE_LIB_DIR
  133. Ruby_RUBY_LIB_DIR
  134. Ruby_SITEARCH_DIR
  135. Ruby_SITELIB_DIR
  136. Ruby_HAS_VENDOR_RUBY
  137. Ruby_VENDORARCH_DIR
  138. Ruby_VENDORLIB_DIR
  139. Ruby_VERSION_MAJOR
  140. Ruby_VERSION_MINOR
  141. Ruby_VERSION_PATCH
  142. )
  143. endif()
  144. # In case Ruby_EXECUTABLE could not be executed (e.g. cross compiling)
  145. # try to detect which version we found. This is not too good.
  146. if(Ruby_EXECUTABLE AND NOT Ruby_VERSION_MAJOR)
  147. # by default assume 1.8.0
  148. set(Ruby_VERSION_MAJOR 1)
  149. set(Ruby_VERSION_MINOR 8)
  150. set(Ruby_VERSION_PATCH 0)
  151. # check whether we found 1.9.x
  152. if(${Ruby_EXECUTABLE} MATCHES "ruby1\\.?9")
  153. set(Ruby_VERSION_MAJOR 1)
  154. set(Ruby_VERSION_MINOR 9)
  155. endif()
  156. # check whether we found 2.0.x
  157. if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?0")
  158. set(Ruby_VERSION_MAJOR 2)
  159. set(Ruby_VERSION_MINOR 0)
  160. endif()
  161. # check whether we found 2.1.x
  162. if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?1")
  163. set(Ruby_VERSION_MAJOR 2)
  164. set(Ruby_VERSION_MINOR 1)
  165. endif()
  166. # check whether we found 2.2.x
  167. if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?2")
  168. set(Ruby_VERSION_MAJOR 2)
  169. set(Ruby_VERSION_MINOR 2)
  170. endif()
  171. # check whether we found 2.3.x
  172. if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?3")
  173. set(Ruby_VERSION_MAJOR 2)
  174. set(Ruby_VERSION_MINOR 3)
  175. endif()
  176. # check whether we found 2.4.x
  177. if(${Ruby_EXECUTABLE} MATCHES "ruby2\\.?4")
  178. set(Ruby_VERSION_MAJOR 2)
  179. set(Ruby_VERSION_MINOR 4)
  180. endif()
  181. endif()
  182. if(Ruby_VERSION_MAJOR)
  183. set(Ruby_VERSION "${Ruby_VERSION_MAJOR}.${Ruby_VERSION_MINOR}.${Ruby_VERSION_PATCH}")
  184. set(_Ruby_VERSION_SHORT "${Ruby_VERSION_MAJOR}.${Ruby_VERSION_MINOR}")
  185. set(_Ruby_VERSION_SHORT_NODOT "${Ruby_VERSION_MAJOR}${Ruby_VERSION_MINOR}")
  186. set(_Ruby_NODOT_VERSION "${Ruby_VERSION_MAJOR}${Ruby_VERSION_MINOR}${Ruby_VERSION_PATCH}")
  187. endif()
  188. find_path(Ruby_INCLUDE_DIR
  189. NAMES ruby.h
  190. HINTS
  191. ${Ruby_HDR_DIR}
  192. ${Ruby_ARCH_DIR}
  193. /usr/lib/ruby/${_Ruby_VERSION_SHORT}/i586-linux-gnu/
  194. )
  195. set(Ruby_INCLUDE_DIRS ${Ruby_INCLUDE_DIR})
  196. # if ruby > 1.8 is required or if ruby > 1.8 was found, search for the config.h dir
  197. if( "${Ruby_FIND_VERSION_SHORT_NODOT}" GREATER 18 OR "${_Ruby_VERSION_SHORT_NODOT}" GREATER 18 OR Ruby_HDR_DIR)
  198. find_path(Ruby_CONFIG_INCLUDE_DIR
  199. NAMES ruby/config.h config.h
  200. HINTS
  201. ${Ruby_HDR_DIR}/${Ruby_ARCH}
  202. ${Ruby_ARCH_DIR}
  203. ${Ruby_ARCHHDR_DIR}
  204. )
  205. set(Ruby_INCLUDE_DIRS ${Ruby_INCLUDE_DIRS} ${Ruby_CONFIG_INCLUDE_DIR} )
  206. endif()
  207. # Determine the list of possible names for the ruby library
  208. set(_Ruby_POSSIBLE_LIB_NAMES ruby ruby-static ruby${_Ruby_VERSION_SHORT} ruby${_Ruby_VERSION_SHORT_NODOT} ruby-${_Ruby_VERSION_SHORT} ruby-${Ruby_VERSION})
  209. if(WIN32)
  210. if(MSVC_TOOLSET_VERSION)
  211. set(_Ruby_MSVC_RUNTIME "${MSVC_TOOLSET_VERSION}")
  212. else()
  213. set(_Ruby_MSVC_RUNTIME "")
  214. endif()
  215. set(_Ruby_ARCH_PREFIX "")
  216. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  217. set(_Ruby_ARCH_PREFIX "x64-")
  218. endif()
  219. list(APPEND _Ruby_POSSIBLE_LIB_NAMES
  220. "${_Ruby_ARCH_PREFIX}msvcr${_Ruby_MSVC_RUNTIME}-ruby${_Ruby_NODOT_VERSION}"
  221. "${_Ruby_ARCH_PREFIX}msvcr${_Ruby_MSVC_RUNTIME}-ruby${_Ruby_NODOT_VERSION}-static"
  222. "${_Ruby_ARCH_PREFIX}msvcrt-ruby${_Ruby_NODOT_VERSION}"
  223. "${_Ruby_ARCH_PREFIX}msvcrt-ruby${_Ruby_NODOT_VERSION}-static" )
  224. endif()
  225. find_library(Ruby_LIBRARY NAMES ${_Ruby_POSSIBLE_LIB_NAMES} HINTS ${Ruby_POSSIBLE_LIB_DIR} )
  226. set(_Ruby_REQUIRED_VARS Ruby_EXECUTABLE Ruby_INCLUDE_DIR Ruby_LIBRARY)
  227. if(_Ruby_VERSION_SHORT_NODOT GREATER 18)
  228. list(APPEND _Ruby_REQUIRED_VARS Ruby_CONFIG_INCLUDE_DIR)
  229. endif()
  230. if(_Ruby_DEBUG_OUTPUT)
  231. message(STATUS "--------FindRuby.cmake debug------------")
  232. message(STATUS "_Ruby_POSSIBLE_EXECUTABLE_NAMES: ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}")
  233. message(STATUS "_Ruby_POSSIBLE_LIB_NAMES: ${_Ruby_POSSIBLE_LIB_NAMES}")
  234. message(STATUS "Ruby_ARCH_DIR: ${Ruby_ARCH_DIR}")
  235. message(STATUS "Ruby_HDR_DIR: ${Ruby_HDR_DIR}")
  236. message(STATUS "Ruby_POSSIBLE_LIB_DIR: ${Ruby_POSSIBLE_LIB_DIR}")
  237. message(STATUS "Found Ruby_VERSION: \"${Ruby_VERSION}\" , short: \"${_Ruby_VERSION_SHORT}\", nodot: \"${_Ruby_VERSION_SHORT_NODOT}\"")
  238. message(STATUS "_Ruby_REQUIRED_VARS: ${_Ruby_REQUIRED_VARS}")
  239. message(STATUS "Ruby_EXECUTABLE: ${Ruby_EXECUTABLE}")
  240. message(STATUS "Ruby_LIBRARY: ${Ruby_LIBRARY}")
  241. message(STATUS "Ruby_INCLUDE_DIR: ${Ruby_INCLUDE_DIR}")
  242. message(STATUS "Ruby_CONFIG_INCLUDE_DIR: ${Ruby_CONFIG_INCLUDE_DIR}")
  243. message(STATUS "--------------------")
  244. endif()
  245. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  246. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Ruby REQUIRED_VARS ${_Ruby_REQUIRED_VARS}
  247. VERSION_VAR Ruby_VERSION )
  248. if(Ruby_FOUND)
  249. set(Ruby_LIBRARIES ${Ruby_LIBRARY})
  250. endif()
  251. mark_as_advanced(
  252. Ruby_EXECUTABLE
  253. Ruby_LIBRARY
  254. Ruby_INCLUDE_DIR
  255. Ruby_CONFIG_INCLUDE_DIR
  256. )
  257. # Set some variables for compatibility with previous version of this file (no need to provide a CamelCase version of that...)
  258. set(RUBY_POSSIBLE_LIB_PATH ${Ruby_POSSIBLE_LIB_DIR})
  259. set(RUBY_RUBY_LIB_PATH ${Ruby_RUBY_LIB_DIR})
  260. set(RUBY_INCLUDE_PATH ${Ruby_INCLUDE_DIRS})
  261. # Backwards compatibility
  262. # Define upper case versions of output variables
  263. foreach(Camel
  264. Ruby_EXECUTABLE
  265. Ruby_INCLUDE_DIRS
  266. Ruby_LIBRARY
  267. Ruby_VERSION
  268. Ruby_VERSION_MAJOR
  269. Ruby_VERSION_MINOR
  270. Ruby_VERSION_PATCH
  271. Ruby_INCLUDE_PATH
  272. Ruby_ARCH_DIR
  273. Ruby_ARCH
  274. Ruby_HDR_DIR
  275. Ruby_ARCHHDR_DIR
  276. Ruby_POSSIBLE_LIB_DIR
  277. Ruby_RUBY_LIB_DIR
  278. Ruby_SITEARCH_DIR
  279. Ruby_SITELIB_DIR
  280. Ruby_HAS_VENDOR_RUBY
  281. Ruby_VENDORARCH_DIR
  282. )
  283. string(TOUPPER ${Camel} UPPER)
  284. set(${UPPER} ${${Camel}})
  285. endforeach()