FindRuby.cmake 11 KB

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