FindRuby.cmake 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindRuby
  5. --------
  6. Finds Ruby installation and the locations of its include files and libraries:
  7. .. code-block:: cmake
  8. find_package(Ruby [<version>] [COMPONENTS <components>...] [...])
  9. Ruby is a general-purpose programming language. This module supports Ruby
  10. 2.0 through 4.0. Virtual environments, such as RVM or RBENV, are also
  11. supported.
  12. Components
  13. ^^^^^^^^^^
  14. .. versionadded:: 4.3
  15. This module supports the following components:
  16. ``Interpreter``
  17. The Ruby interpreter executable.
  18. ``Development``
  19. Headers and libraries needed to build Ruby extensions or embed Ruby.
  20. If no components are specified, both ``Interpreter`` and ``Development``
  21. are searched for.
  22. Imported Targets
  23. ^^^^^^^^^^^^^^^^
  24. .. versionadded:: 4.3
  25. This module defines the following :prop_tgt:`IMPORTED` targets:
  26. ``Ruby::Interpreter``
  27. Ruby interpreter. Target defined if component ``Interpreter`` is found.
  28. ``Ruby::Ruby``
  29. Ruby library for embedding Ruby in C/C++ applications.
  30. Target defined if component ``Development`` is found.
  31. ``Ruby::Module``
  32. Ruby library for building Ruby extension modules.
  33. Target defined if component ``Development`` is found.
  34. Use this target when creating native extensions that will be
  35. loaded into Ruby via ``require``. On most platforms, extension
  36. modules do not link directly to libruby. Includes appropriate
  37. symbol visibility settings.
  38. Result Variables
  39. ^^^^^^^^^^^^^^^^
  40. This module defines the following variables:
  41. ``Ruby_FOUND``
  42. .. versionadded:: 3.3
  43. Boolean indicating whether (the requested version of) ruby was found.
  44. ``Ruby_VERSION``
  45. The version of ruby which was found, e.g. ``3.2.6``.
  46. ``Ruby_VERSION_MAJOR``
  47. Ruby major version.
  48. ``Ruby_VERSION_MINOR``
  49. Ruby minor version.
  50. ``Ruby_VERSION_PATCH``
  51. Ruby patch version.
  52. ``Ruby_EXECUTABLE``
  53. The full path to the ruby binary.
  54. ``Ruby_INCLUDE_DIRS``
  55. Include dirs to be used when using the ruby library.
  56. ``Ruby_LIBRARIES``
  57. .. versionadded:: 3.18
  58. Libraries needed to use ruby from C.
  59. .. versionchanged:: 3.18
  60. Previous versions of CMake used the ``RUBY_`` prefix for all variables.
  61. Hints
  62. ^^^^^
  63. This module accepts the following variables:
  64. ``Ruby_FIND_VIRTUALENV``
  65. .. versionadded:: 3.18
  66. This variable defines the handling of virtual environments.
  67. It can be left empty or be set to one of the following values:
  68. * ``FIRST``: Virtual Ruby environments are searched for first,
  69. then the system Ruby installation.
  70. This is the default.
  71. * ``ONLY``: Only virtual environments are searched
  72. * ``STANDARD``: Only the system Ruby installation is searched.
  73. Virtual environments may be provided by:
  74. ``rvm``
  75. Requires that the ``MY_RUBY_HOME`` environment is defined.
  76. ``rbenv``
  77. Requires that ``rbenv`` is installed in ``~/.rbenv/bin``
  78. or that the ``RBENV_ROOT`` environment variable is defined.
  79. Deprecated Variables
  80. ^^^^^^^^^^^^^^^^^^^^
  81. The following variables are provided for backward compatibility:
  82. .. deprecated:: 4.0
  83. The following variables are deprecated. See policy :policy:`CMP0185`.
  84. ``RUBY_FOUND``
  85. Same as ``Ruby_FOUND``.
  86. ``RUBY_VERSION``
  87. Same as ``Ruby_VERSION``.
  88. ``RUBY_EXECUTABLE``
  89. Same as ``Ruby_EXECUTABLE``.
  90. ``RUBY_INCLUDE_DIRS``
  91. Same as ``Ruby_INCLUDE_DIRS``.
  92. ``RUBY_INCLUDE_PATH``
  93. Same as ``Ruby_INCLUDE_DIRS``.
  94. ``RUBY_LIBRARY``
  95. Same as ``Ruby_LIBRARY``.
  96. Examples
  97. ^^^^^^^^
  98. Finding Ruby and specifying the minimum required version:
  99. .. code-block:: cmake
  100. find_package(Ruby 3.2.6 EXACT REQUIRED)
  101. # or
  102. find_package(Ruby 3.2)
  103. #]=======================================================================]
  104. cmake_policy(GET CMP0185 _Ruby_CMP0185)
  105. if(NOT _Ruby_CMP0185 STREQUAL "NEW")
  106. # Backwards compatibility
  107. # Define camel case versions of input variables
  108. foreach (UPPER
  109. RUBY_EXECUTABLE
  110. RUBY_LIBRARY
  111. RUBY_INCLUDE_DIR
  112. RUBY_CONFIG_INCLUDE_DIR)
  113. if (DEFINED ${UPPER})
  114. string(REPLACE "RUBY_" "Ruby_" Camel ${UPPER})
  115. if (NOT DEFINED ${Camel})
  116. set(${Camel} ${${UPPER}})
  117. endif ()
  118. endif ()
  119. endforeach ()
  120. endif()
  121. # Uncomment the following line to get debug output for this file
  122. # set(CMAKE_MESSAGE_LOG_LEVEL DEBUG)
  123. # Determine the list of possible names of the ruby executable depending
  124. # on which version of ruby is required
  125. set(_Ruby_POSSIBLE_EXECUTABLE_NAMES ruby)
  126. # If the user has not specified a Ruby version, create a list of Ruby versions
  127. # to search (newest to oldest). Based on https://www.ruby-lang.org/en/downloads/releases/
  128. if (NOT Ruby_FIND_VERSION_EXACT)
  129. set(_Ruby_SUPPORTED_VERSIONS 40 34 33 32)
  130. set(_Ruby_UNSUPPORTED_VERSIONS 31 30 27 26 25 24 23 22 21 20)
  131. foreach (_ruby_version IN LISTS _Ruby_SUPPORTED_VERSIONS _Ruby_UNSUPPORTED_VERSIONS)
  132. string(SUBSTRING "${_ruby_version}" 0 1 _ruby_major_version)
  133. string(SUBSTRING "${_ruby_version}" 1 1 _ruby_minor_version)
  134. # Append both rubyX.Y and rubyXY (eg: ruby3.4 ruby34)
  135. list(APPEND _Ruby_POSSIBLE_EXECUTABLE_NAMES
  136. ruby${_ruby_major_version}.${_ruby_minor_version}
  137. ruby${_ruby_major_version}${_ruby_minor_version})
  138. endforeach ()
  139. endif ()
  140. # Virtual environment handling
  141. if (DEFINED Ruby_FIND_VIRTUALENV AND NOT Ruby_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY|STANDARD)$")
  142. message(AUTHOR_WARNING "FindRuby: ${Ruby_FIND_VIRTUALENV}: invalid value for 'Ruby_FIND_VIRTUALENV'. 'FIRST', 'ONLY' or 'STANDARD' expected. 'FIRST' will be used instead.")
  143. set(Ruby_FIND_VIRTUALENV "FIRST")
  144. elseif (NOT DEFINED Ruby_FIND_VIRTUALENV)
  145. # Default is to search for virtual environments first
  146. set(Ruby_FIND_VIRTUALENV "FIRST")
  147. endif ()
  148. # Validate the found Ruby interpreter to make sure that it is
  149. # callable and that its version matches the requested version
  150. function(_RUBY_VALIDATE_INTERPRETER result_var path)
  151. # Get the interpreter version
  152. execute_process(COMMAND "${path}" -e "puts RUBY_VERSION"
  153. RESULT_VARIABLE result
  154. OUTPUT_VARIABLE version
  155. ERROR_QUIET
  156. OUTPUT_STRIP_TRAILING_WHITESPACE)
  157. if (NOT result EQUAL 0)
  158. set(_Ruby_Interpreter_REASON_FAILURE "Cannot use the interpreter \"${path}\"")
  159. set(${result_var} FALSE PARENT_SCOPE)
  160. return()
  161. endif ()
  162. if (Ruby_FIND_VERSION)
  163. if (Ruby_FIND_VERSION_EXACT AND NOT version VERSION_EQUAL Ruby_FIND_VERSION)
  164. message(DEBUG "Incorrect Ruby found. Requested: ${Ruby_FIND_VERSION}. Found: ${version}. Path: \"${path}\"")
  165. set(${result_var} FALSE PARENT_SCOPE)
  166. return()
  167. elseif (version VERSION_LESS Ruby_FIND_VERSION)
  168. message(DEBUG "Ruby version is too old. Minimum: ${Ruby_FIND_VERSION}. Found: ${version}. Path: \"${path}\"")
  169. set(${result_var} FALSE PARENT_SCOPE)
  170. return()
  171. endif ()
  172. endif ()
  173. # Found valid Ruby interpreter!
  174. set(${result_var} TRUE PARENT_SCOPE)
  175. endfunction()
  176. # Query Ruby RBConfig module for the specified variable (_RUBY_CONFIG_VAR)
  177. function(_RUBY_CONFIG_VAR RBVAR OUTVAR)
  178. execute_process(COMMAND "${Ruby_EXECUTABLE}" -r rbconfig -e "print RbConfig::CONFIG['${RBVAR}']"
  179. RESULT_VARIABLE _Ruby_SUCCESS
  180. OUTPUT_VARIABLE _Ruby_OUTPUT
  181. ERROR_QUIET)
  182. set(${OUTVAR} "${_Ruby_OUTPUT}" PARENT_SCOPE)
  183. endfunction()
  184. # Check for RVM virtual environments
  185. function(_RUBY_CHECK_RVM)
  186. if (NOT DEFINED ENV{MY_RUBY_HOME})
  187. return()
  188. endif ()
  189. find_program(Ruby_EXECUTABLE
  190. NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}
  191. NAMES_PER_DIR
  192. PATHS ENV MY_RUBY_HOME
  193. PATH_SUFFIXES bin Scripts
  194. VALIDATOR _RUBY_VALIDATE_INTERPRETER
  195. NO_CMAKE_PATH
  196. NO_CMAKE_ENVIRONMENT_PATH
  197. NO_SYSTEM_ENVIRONMENT_PATH
  198. NO_CMAKE_SYSTEM_PATH)
  199. if (Ruby_EXECUTABLE)
  200. set(Ruby_ENV "RVM" CACHE INTERNAL "Ruby environment")
  201. endif ()
  202. endfunction()
  203. # Check for RBENV virtual environments
  204. function(_RUBY_CHECK_RBENV)
  205. find_program(Ruby_RBENV_EXECUTABLE
  206. NAMES rbenv
  207. NAMES_PER_DIR
  208. PATHS "$ENV{HOME}/.rbenv/bin/rbenv" ENV RBENV_ROOT
  209. PATH_SUFFIXES bin Scripts
  210. NO_CACHE
  211. NO_CMAKE_PATH
  212. NO_CMAKE_ENVIRONMENT_PATH
  213. NO_CMAKE_SYSTEM_PATH)
  214. execute_process(COMMAND "${Ruby_RBENV_EXECUTABLE}" "which" "ruby"
  215. RESULT_VARIABLE result
  216. OUTPUT_VARIABLE ruby_exe
  217. ERROR_QUIET
  218. OUTPUT_STRIP_TRAILING_WHITESPACE)
  219. if (NOT result EQUAL 0)
  220. return()
  221. endif ()
  222. cmake_path(GET ruby_exe PARENT_PATH ruby_dir)
  223. find_program(Ruby_EXECUTABLE
  224. NAMES ruby
  225. NAMES_PER_DIR
  226. PATHS ${ruby_dir}
  227. VALIDATOR _RUBY_VALIDATE_INTERPRETER
  228. NO_DEFAULT_PATH)
  229. if (Ruby_EXECUTABLE)
  230. set(Ruby_ENV "RBENV" CACHE INTERNAL "Ruby environment")
  231. endif ()
  232. endfunction()
  233. # Check Ruby installed via Homebrew on macOS
  234. function(_RUBY_CHECK_BREW)
  235. # Try to locate brew in common locations and in PATH
  236. find_program(_BREW_EXECUTABLE
  237. NAMES brew
  238. NAMES_PER_DIR
  239. PATHS
  240. /opt/homebrew/bin # Apple Silicon default
  241. /usr/local/bin # Intel default
  242. ENV PATH
  243. NO_CACHE
  244. )
  245. if (NOT _BREW_EXECUTABLE)
  246. return()
  247. endif ()
  248. message(DEBUG "Found brew at: ${_BREW_EXECUTABLE}")
  249. # Query Homebrew for the prefix of the 'ruby' formula.
  250. # If Ruby is not installed via Homebrew, this will fail.
  251. execute_process(
  252. COMMAND "${_BREW_EXECUTABLE}" --prefix ruby
  253. RESULT_VARIABLE _Ruby_BREW_RESULT
  254. OUTPUT_VARIABLE _Ruby_BREW_DIR
  255. ERROR_QUIET
  256. OUTPUT_STRIP_TRAILING_WHITESPACE
  257. )
  258. message(DEBUG "Ruby BREW is: ${_Ruby_BREW_DIR}")
  259. if (NOT _Ruby_BREW_RESULT EQUAL 0 OR _Ruby_BREW_DIR STREQUAL "")
  260. # No 'ruby' formula installed in Homebrew
  261. return()
  262. endif ()
  263. find_program(Ruby_EXECUTABLE
  264. NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}
  265. NAMES_PER_DIR
  266. PATHS "${_Ruby_BREW_DIR}/bin"
  267. VALIDATOR _RUBY_VALIDATE_INTERPRETER
  268. NO_DEFAULT_PATH
  269. )
  270. if (Ruby_EXECUTABLE)
  271. set(Ruby_ENV "BREW" CACHE INTERNAL "Ruby environment")
  272. endif ()
  273. endfunction()
  274. # Check system installed Ruby
  275. function(_RUBY_CHECK_SYSTEM)
  276. find_program(Ruby_EXECUTABLE
  277. NAMES ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}
  278. NAMES_PER_DIR
  279. VALIDATOR _RUBY_VALIDATE_INTERPRETER)
  280. if (Ruby_EXECUTABLE)
  281. set(Ruby_ENV "Standard" CACHE INTERNAL "Ruby environment")
  282. endif ()
  283. endfunction()
  284. # Find Ruby
  285. if (NOT Ruby_EXECUTABLE AND Ruby_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY)$")
  286. # First check for RVM virtual environments
  287. _RUBY_CHECK_RVM()
  288. # Second check for RBENV virtual environments
  289. if (NOT Ruby_EXECUTABLE)
  290. _RUBY_CHECK_RBENV()
  291. endif ()
  292. endif ()
  293. # Check for Homebrew Ruby (non-virtualenv, common on MacOS)
  294. if (NOT Ruby_EXECUTABLE AND NOT Ruby_FIND_VIRTUALENV STREQUAL "ONLY")
  295. _RUBY_CHECK_BREW()
  296. endif ()
  297. # Fallback to system installed Ruby
  298. if (NOT Ruby_EXECUTABLE AND NOT Ruby_FIND_VIRTUALENV STREQUAL "ONLY")
  299. _RUBY_CHECK_SYSTEM()
  300. endif ()
  301. # We found a new Ruby or a Ruby that is different than the last one we found.
  302. # So reload a number of variables by querying the Ruby interpreter.
  303. if (Ruby_EXECUTABLE AND NOT Ruby_EXECUTABLE STREQUAL "${_Ruby_EXECUTABLE_LAST_QUERIED}")
  304. # query the ruby version
  305. _RUBY_CONFIG_VAR("MAJOR" Ruby_VERSION_MAJOR)
  306. _RUBY_CONFIG_VAR("MINOR" Ruby_VERSION_MINOR)
  307. _RUBY_CONFIG_VAR("TEENY" Ruby_VERSION_PATCH)
  308. # Ruby extensions information
  309. _RUBY_CONFIG_VAR("arch" Ruby_ARCH) # x86_64-linux, arm64-darwin, x64-mswin64_140, etc
  310. # Extension directory where so/bundle files are stored
  311. _RUBY_CONFIG_VAR("archdir" Ruby_ARCH_DIR)
  312. # Extension suffix
  313. _RUBY_CONFIG_VAR("DLEXT" _Ruby_DLEXT) # so, bundle, *not* dll
  314. # Headers
  315. _RUBY_CONFIG_VAR("rubyhdrdir" Ruby_HDR_DIR)
  316. _RUBY_CONFIG_VAR("rubyarchhdrdir" Ruby_ARCHHDR_DIR)
  317. # Ruby library information
  318. _RUBY_CONFIG_VAR("libdir" _Ruby_POSSIBLE_LIB_DIR) # /usr/lib64
  319. _RUBY_CONFIG_VAR("RUBY_SO_NAME" _Ruby_SO_NAME) # ruby, x64-vcruntime140-ruby340, etc.
  320. # Ruby directory for ruby files (*.rb). TODO - not relevant should be removed
  321. _RUBY_CONFIG_VAR("rubylibdir" Ruby_RUBY_LIB_DIR)
  322. # site_ruby - TODO - not relevant and should be removed
  323. _RUBY_CONFIG_VAR("sitearchdir" Ruby_SITEARCH_DIR)
  324. _RUBY_CONFIG_VAR("sitelibdir" Ruby_SITELIB_DIR)
  325. # vendor_ruby - TODO - Not relevant and should be removed.
  326. execute_process(COMMAND "${Ruby_EXECUTABLE}" -r vendor-specific -e "print 'true'"
  327. OUTPUT_VARIABLE Ruby_HAS_VENDOR_RUBY ERROR_QUIET)
  328. if (Ruby_HAS_VENDOR_RUBY)
  329. _RUBY_CONFIG_VAR("vendorlibdir" Ruby_VENDORLIB_DIR)
  330. _RUBY_CONFIG_VAR("vendorarchdir" Ruby_VENDORARCH_DIR)
  331. endif ()
  332. # save the results in the cache so we don't have to run ruby the next time again
  333. set(_Ruby_EXECUTABLE_LAST_QUERIED "${Ruby_EXECUTABLE}" CACHE INTERNAL "The ruby executable last queried for version and path info")
  334. set(Ruby_VERSION_MAJOR ${Ruby_VERSION_MAJOR} CACHE STRING "The Ruby major version" FORCE)
  335. set(Ruby_VERSION_MINOR ${Ruby_VERSION_MINOR} CACHE STRING "The Ruby minor version" FORCE)
  336. set(Ruby_VERSION_PATCH ${Ruby_VERSION_PATCH} CACHE STRING "The Ruby patch version" FORCE)
  337. set(Ruby_ARCH_DIR ${Ruby_ARCH_DIR} CACHE INTERNAL "The Ruby arch dir" FORCE)
  338. set(Ruby_HDR_DIR ${Ruby_HDR_DIR} CACHE INTERNAL "The Ruby header dir (1.9+)" FORCE)
  339. set(Ruby_ARCHHDR_DIR ${Ruby_ARCHHDR_DIR} CACHE INTERNAL "The Ruby arch header dir (2.0+)" FORCE)
  340. set(_Ruby_POSSIBLE_LIB_DIR ${_Ruby_POSSIBLE_LIB_DIR} CACHE INTERNAL "The Ruby lib dir" FORCE)
  341. set(Ruby_RUBY_LIB_DIR ${Ruby_RUBY_LIB_DIR} CACHE INTERNAL "The Ruby ruby-lib dir" FORCE)
  342. set(_Ruby_SO_NAME ${_Ruby_SO_NAME} CACHE STRING "The Ruby shared library name" FORCE)
  343. set(_Ruby_DLEXT ${_Ruby_DLEXT} CACHE STRING "Ruby extensions extension" FORCE)
  344. set(Ruby_SITEARCH_DIR ${Ruby_SITEARCH_DIR} CACHE INTERNAL "The Ruby site arch dir" FORCE)
  345. set(Ruby_SITELIB_DIR ${Ruby_SITELIB_DIR} CACHE INTERNAL "The Ruby site lib dir" FORCE)
  346. set(Ruby_HAS_VENDOR_RUBY ${Ruby_HAS_VENDOR_RUBY} CACHE BOOL "Vendor Ruby is available" FORCE)
  347. set(Ruby_VENDORARCH_DIR ${Ruby_VENDORARCH_DIR} CACHE INTERNAL "The Ruby vendor arch dir" FORCE)
  348. set(Ruby_VENDORLIB_DIR ${Ruby_VENDORLIB_DIR} CACHE INTERNAL "The Ruby vendor lib dir" FORCE)
  349. mark_as_advanced(
  350. Ruby_ARCH_DIR
  351. Ruby_ARCH
  352. Ruby_HDR_DIR
  353. Ruby_ARCHHDR_DIR
  354. _Ruby_POSSIBLE_LIB_DIR
  355. Ruby_RUBY_LIB_DIR
  356. _Ruby_SO_NAME
  357. _Ruby_DLEXT
  358. Ruby_SITEARCH_DIR
  359. Ruby_SITELIB_DIR
  360. Ruby_HAS_VENDOR_RUBY
  361. Ruby_VENDORARCH_DIR
  362. Ruby_VENDORLIB_DIR
  363. Ruby_VERSION_MAJOR
  364. Ruby_VERSION_MINOR
  365. Ruby_VERSION_PATCH
  366. )
  367. endif ()
  368. if (Ruby_VERSION_MAJOR)
  369. set(Ruby_VERSION "${Ruby_VERSION_MAJOR}.${Ruby_VERSION_MINOR}.${Ruby_VERSION_PATCH}")
  370. set(_Ruby_VERSION_NODOT "${Ruby_VERSION_MAJOR}${Ruby_VERSION_MINOR}${Ruby_VERSION_PATCH}")
  371. set(_Ruby_VERSION_NODOT_ZERO_PATCH "${Ruby_VERSION_MAJOR}${Ruby_VERSION_MINOR}0")
  372. set(_Ruby_VERSION_SHORT "${Ruby_VERSION_MAJOR}.${Ruby_VERSION_MINOR}")
  373. set(_Ruby_VERSION_SHORT_NODOT "${Ruby_VERSION_MAJOR}${Ruby_VERSION_MINOR}")
  374. endif ()
  375. # Save CMAKE_FIND_FRAMEWORK
  376. set(_Ruby_CMAKE_FIND_FRAMEWORK_ORIGINAL ${CMAKE_FIND_FRAMEWORK})
  377. # Avoid finding the ancient Ruby framework included in macOS.
  378. set(CMAKE_FIND_FRAMEWORK LAST)
  379. find_path(Ruby_INCLUDE_DIR
  380. NAMES ruby.h
  381. HINTS ${Ruby_HDR_DIR})
  382. find_path(Ruby_CONFIG_INCLUDE_DIR
  383. NAMES ruby/config.h config.h
  384. HINTS ${Ruby_ARCHHDR_DIR})
  385. set(Ruby_INCLUDE_DIRS ${Ruby_INCLUDE_DIR} ${Ruby_CONFIG_INCLUDE_DIR})
  386. find_library(Ruby_LIBRARY
  387. NAMES "${_Ruby_SO_NAME}"
  388. HINTS ${_Ruby_POSSIBLE_LIB_DIR})
  389. # Restore CMAKE_FIND_FRAMEWORK
  390. set(CMAKE_FIND_FRAMEWORK ${_Ruby_CMAKE_FIND_FRAMEWORK_ORIGINAL})
  391. message(DEBUG "--------FindRuby.cmake debug------------")
  392. message(DEBUG "_Ruby_POSSIBLE_EXECUTABLE_NAMES: ${_Ruby_POSSIBLE_EXECUTABLE_NAMES}")
  393. message(DEBUG "_Ruby_POSSIBLE_LIB_DIR: ${_Ruby_POSSIBLE_LIB_DIR}")
  394. message(DEBUG "_Ruby_SO_NAME: ${_Ruby_SO_NAME}")
  395. message(DEBUG "_Ruby_DLEXT: ${_Ruby_DLEXT}")
  396. message(DEBUG "Ruby_FIND_VIRTUALENV=${Ruby_FIND_VIRTUALENV}")
  397. message(DEBUG "Ruby_ENV: ${Ruby_ENV}")
  398. message(DEBUG "Found Ruby_VERSION: \"${Ruby_VERSION}\"")
  399. message(DEBUG "Ruby_EXECUTABLE: ${Ruby_EXECUTABLE}")
  400. message(DEBUG "Ruby_LIBRARY: ${Ruby_LIBRARY}")
  401. message(DEBUG "Ruby_INCLUDE_DIR: ${Ruby_INCLUDE_DIR}")
  402. message(DEBUG "Ruby_CONFIG_INCLUDE_DIR: ${Ruby_CONFIG_INCLUDE_DIR}")
  403. message(DEBUG "Ruby_HDR_DIR: ${Ruby_HDR_DIR}")
  404. message(DEBUG "Ruby_ARCH_DIR: ${Ruby_ARCH_DIR}")
  405. message(DEBUG "Ruby_ARCHHDR_DIR: ${Ruby_ARCHHDR_DIR}")
  406. message(DEBUG "--------------------")
  407. # Components
  408. #
  409. # If the caller does not request components, preserve legacy behavior
  410. if (NOT Ruby_FIND_COMPONENTS)
  411. set(Ruby_FIND_COMPONENTS Interpreter Development)
  412. endif ()
  413. set(_Ruby_WANT_INTERPRETER FALSE)
  414. set(_Ruby_WANT_DEVELOPMENT FALSE)
  415. set(_Ruby_REQUIRED_VARS "")
  416. foreach (component IN LISTS Ruby_FIND_COMPONENTS)
  417. if (component STREQUAL "Interpreter")
  418. set(_Ruby_WANT_INTERPRETER TRUE)
  419. list(APPEND _Ruby_REQUIRED_VARS Ruby_EXECUTABLE)
  420. elseif (component STREQUAL "Development")
  421. set(_Ruby_WANT_DEVELOPMENT TRUE)
  422. list(APPEND _Ruby_REQUIRED_VARS Ruby_INCLUDE_DIR Ruby_CONFIG_INCLUDE_DIR)
  423. if (WIN32)
  424. list(APPEND _Ruby_REQUIRED_VARS Ruby_LIBRARY)
  425. endif ()
  426. else ()
  427. message(FATAL_ERROR
  428. "FindRuby: Unsupported component '${component}'. Supported components are: Interpreter, Development")
  429. endif ()
  430. endforeach ()
  431. # Set component found flags
  432. if (Ruby_EXECUTABLE)
  433. set(Ruby_Interpreter_FOUND TRUE)
  434. else ()
  435. set(Ruby_Interpreter_FOUND FALSE)
  436. endif ()
  437. if (Ruby_INCLUDE_DIR AND Ruby_CONFIG_INCLUDE_DIR AND (Ruby_LIBRARY OR NOT WIN32))
  438. set(Ruby_Development_FOUND TRUE)
  439. else ()
  440. set(Ruby_Development_FOUND FALSE)
  441. endif ()
  442. include(FindPackageHandleStandardArgs)
  443. find_package_handle_standard_args(Ruby
  444. REQUIRED_VARS ${_Ruby_REQUIRED_VARS}
  445. VERSION_VAR Ruby_VERSION
  446. HANDLE_COMPONENTS)
  447. if (Ruby_FOUND)
  448. if (NOT TARGET Ruby::Interpreter)
  449. add_executable(Ruby::Interpreter IMPORTED GLOBAL)
  450. set_target_properties(Ruby::Interpreter PROPERTIES
  451. IMPORTED_LOCATION "${Ruby_EXECUTABLE}"
  452. )
  453. endif ()
  454. if (Ruby_Development_FOUND)
  455. set(Ruby_LIBRARIES ${Ruby_LIBRARY})
  456. if (Ruby_LIBRARY AND NOT TARGET Ruby::Ruby)
  457. add_library(Ruby::Ruby UNKNOWN IMPORTED)
  458. set_target_properties(Ruby::Ruby PROPERTIES
  459. IMPORTED_LOCATION "${Ruby_LIBRARY}"
  460. INTERFACE_INCLUDE_DIRECTORIES "${Ruby_INCLUDE_DIRS}"
  461. # Custom property for extension suffix (with dot), e.g. ".so", ".bundle"
  462. INTERFACE_RUBY_EXTENSION_SUFFIX ".${_Ruby_DLEXT}"
  463. )
  464. endif ()
  465. # Ruby::Module - For building Ruby extension modules
  466. if (NOT TARGET Ruby::Module)
  467. if (WIN32)
  468. add_library(Ruby::Module UNKNOWN IMPORTED)
  469. set_target_properties(Ruby::Module PROPERTIES
  470. IMPORTED_LOCATION "${Ruby_LIBRARY}"
  471. )
  472. else ()
  473. add_library(Ruby::Module INTERFACE IMPORTED)
  474. endif ()
  475. set_target_properties(Ruby::Module PROPERTIES
  476. INTERFACE_INCLUDE_DIRECTORIES "${Ruby_INCLUDE_DIRS}"
  477. # Custom property for extension suffix (with dot), e.g. ".so", ".bundle"
  478. INTERFACE_RUBY_EXTENSION_SUFFIX ".${_Ruby_DLEXT}"
  479. INTERFACE_C_VISIBILITY_PRESET hidden
  480. INTERFACE_CXX_VISIBILITY_PRESET hidden
  481. INTERFACE_VISIBILITY_INLINES_HIDDEN ON
  482. )
  483. # macOS: allow unresolved Ruby API symbols; resolved when Ruby loads the bundle.
  484. if (APPLE)
  485. target_link_options(Ruby::Module INTERFACE
  486. "LINKER:-undefined,dynamic_lookup"
  487. )
  488. endif ()
  489. # Linux (and other ELF platforms):
  490. # Normally undefined Ruby API symbols are allowed in shared objects and resolved at dlopen().
  491. # But if the toolchain/preset adds -Wl,--no-undefined, linking will fail.
  492. # This counteracts that.
  493. if (UNIX AND NOT APPLE)
  494. target_link_options(Ruby::Module INTERFACE
  495. "LINKER:--unresolved-symbols=ignore-all"
  496. )
  497. endif ()
  498. endif ()
  499. endif ()
  500. endif ()
  501. mark_as_advanced(
  502. Ruby_EXECUTABLE
  503. Ruby_LIBRARY
  504. Ruby_INCLUDE_DIR
  505. Ruby_CONFIG_INCLUDE_DIR
  506. )
  507. if(NOT _Ruby_CMP0185 STREQUAL "NEW")
  508. # Set some variables for compatibility with previous version of this file (no need to provide a CamelCase version of that...)
  509. set(RUBY_POSSIBLE_LIB_PATH ${_Ruby_POSSIBLE_LIB_DIR})
  510. set(RUBY_RUBY_LIB_PATH ${Ruby_RUBY_LIB_DIR})
  511. set(RUBY_INCLUDE_PATH ${Ruby_INCLUDE_DIRS})
  512. # Backwards compatibility
  513. # Define upper case versions of output variables
  514. foreach (Camel
  515. Ruby_EXECUTABLE
  516. Ruby_INCLUDE_DIRS
  517. Ruby_LIBRARY
  518. Ruby_VERSION
  519. Ruby_VERSION_MAJOR
  520. Ruby_VERSION_MINOR
  521. Ruby_VERSION_PATCH
  522. Ruby_ARCH_DIR
  523. Ruby_ARCH
  524. Ruby_HDR_DIR
  525. Ruby_ARCHHDR_DIR
  526. Ruby_RUBY_LIB_DIR
  527. Ruby_SITEARCH_DIR
  528. Ruby_SITELIB_DIR
  529. Ruby_HAS_VENDOR_RUBY
  530. Ruby_VENDORARCH_DIR
  531. Ruby_VENDORLIB_DIR)
  532. string(TOUPPER ${Camel} UPPER)
  533. set(${UPPER} ${${Camel}})
  534. endforeach ()
  535. endif()