FindRuby.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # - Find Ruby
  2. # This module finds if Ruby is installed and determines where the include files
  3. # and libraries are. Ruby 1.8 and 1.9 are supported. The minimum required version
  4. # specified in the find_package() command is honored.
  5. # It also determines what the name of the library is. This
  6. # code sets the following variables:
  7. #
  8. # RUBY_EXECUTABLE = full path to the ruby binary
  9. # RUBY_INCLUDE_DIRS = include dirs to be used when using the ruby library
  10. # RUBY_LIBRARY = full path to the ruby library
  11. # RUBY_VERSION = the version of ruby which was found, e.g. "1.8.7"
  12. # RUBY_FOUND = set to true if ruby ws found successfully
  13. #
  14. # RUBY_INCLUDE_PATH = same as RUBY_INCLUDE_DIRS, only provided for compatibility reasons, don't use it
  15. # Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  16. # See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  17. # RUBY_ARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"archdir"@:>@)'`
  18. # RUBY_SITEARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitearchdir"@:>@)'`
  19. # RUBY_SITEDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitelibdir"@:>@)'`
  20. # RUBY_LIBDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"libdir"@:>@)'`
  21. # RUBY_LIBRUBYARG=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"LIBRUBYARG_SHARED"@:>@)'`
  22. # uncomment the following line to get debug output for this file
  23. # SET(_RUBY_DEBUG_OUTPUT TRUE)
  24. # Determine the list of possible names of the ruby executable depending
  25. # on which version of ruby is required
  26. SET(_RUBY_POSSIBLE_EXECUTABLE_NAMES ruby)
  27. # if 1.9 is required, don't look for ruby18 and ruby1.8, default to version 1.8
  28. IF(Ruby_FIND_VERSION_MAJOR AND Ruby_FIND_VERSION_MINOR)
  29. SET(Ruby_FIND_VERSION_SHORT_NODOT "${Ruby_FIND_VERSION_MAJOR}${RUBY_FIND_VERSION_MINOR}")
  30. ELSE(Ruby_FIND_VERSION_MAJOR AND Ruby_FIND_VERSION_MINOR)
  31. SET(Ruby_FIND_VERSION_SHORT_NODOT "18")
  32. ENDIF(Ruby_FIND_VERSION_MAJOR AND Ruby_FIND_VERSION_MINOR)
  33. SET(_RUBY_POSSIBLE_EXECUTABLE_NAMES ${_RUBY_POSSIBLE_EXECUTABLE_NAMES} ruby1.9 ruby19)
  34. # if we want a version below 1.9, also look for ruby 1.8
  35. IF("${Ruby_FIND_VERSION_SHORT_NODOT}" VERSION_LESS "19")
  36. SET(_RUBY_POSSIBLE_EXECUTABLE_NAMES ${_RUBY_POSSIBLE_EXECUTABLE_NAMES} ruby1.8 ruby18)
  37. ENDIF("${Ruby_FIND_VERSION_SHORT_NODOT}" VERSION_LESS "19")
  38. FIND_PROGRAM(RUBY_EXECUTABLE NAMES ${_RUBY_POSSIBLE_EXECUTABLE_NAMES})
  39. IF(RUBY_EXECUTABLE AND NOT RUBY_MAJOR_VERSION)
  40. # query the ruby version
  41. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['MAJOR']"
  42. OUTPUT_VARIABLE RUBY_VERSION_MAJOR)
  43. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['MINOR']"
  44. OUTPUT_VARIABLE RUBY_VERSION_MINOR)
  45. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['TEENY']"
  46. OUTPUT_VARIABLE RUBY_VERSION_PATCH)
  47. # query the different directories
  48. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['archdir']"
  49. OUTPUT_VARIABLE RUBY_ARCH_DIR)
  50. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['arch']"
  51. OUTPUT_VARIABLE RUBY_ARCH)
  52. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['rubyhdrdir']"
  53. OUTPUT_VARIABLE RUBY_HDR_DIR)
  54. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['libdir']"
  55. OUTPUT_VARIABLE RUBY_POSSIBLE_LIB_DIR)
  56. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['rubylibdir']"
  57. OUTPUT_VARIABLE RUBY_RUBY_LIB_DIR)
  58. # site_ruby
  59. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['sitearchdir']"
  60. OUTPUT_VARIABLE RUBY_SITEARCH_DIR)
  61. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['sitelibdir']"
  62. OUTPUT_VARIABLE RUBY_SITELIB_DIR)
  63. # vendor_ruby available ?
  64. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r vendor-specific -e "print 'true'"
  65. OUTPUT_VARIABLE RUBY_HAS_VENDOR_RUBY ERROR_QUIET)
  66. IF(RUBY_HAS_VENDOR_RUBY)
  67. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['vendorlibdir']"
  68. OUTPUT_VARIABLE RUBY_VENDORLIB_DIR)
  69. EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['vendorarchdir']"
  70. OUTPUT_VARIABLE RUBY_VENDORARCH_DIR)
  71. ENDIF(RUBY_HAS_VENDOR_RUBY)
  72. # save the results in the cache so we don't have to run ruby the next time again
  73. SET(RUBY_VERSION_MAJOR ${RUBY_VERSION_MAJOR} CACHE PATH "The Ruby major version" FORCE)
  74. SET(RUBY_VERSION_MINOR ${RUBY_VERSION_MINOR} CACHE PATH "The Ruby minor version" FORCE)
  75. SET(RUBY_VERSION_PATCH ${RUBY_VERSION_PATCH} CACHE PATH "The Ruby patch version" FORCE)
  76. SET(RUBY_ARCH_DIR ${RUBY_ARCH_DIR} CACHE PATH "The Ruby arch dir" FORCE)
  77. SET(RUBY_HDR_DIR ${RUBY_HDR_DIR} CACHE PATH "The Ruby header dir (1.9)" FORCE)
  78. SET(RUBY_POSSIBLE_LIB_DIR ${RUBY_POSSIBLE_LIB_DIR} CACHE PATH "The Ruby lib dir" FORCE)
  79. SET(RUBY_RUBY_LIB_DIR ${RUBY_RUBY_LIB_DIR} CACHE PATH "The Ruby ruby-lib dir" FORCE)
  80. SET(RUBY_SITEARCH_DIR ${RUBY_SITEARCH_DIR} CACHE PATH "The Ruby site arch dir" FORCE)
  81. SET(RUBY_SITELIB_DIR ${RUBY_SITELIB_DIR} CACHE PATH "The Ruby site lib dir" FORCE)
  82. SET(RUBY_HAS_VENDOR_RUBY ${RUBY_HAS_VENDOR_RUBY} CACHE BOOL "Vendor Ruby is available" FORCE)
  83. SET(RUBY_VENDORARCH_DIR ${RUBY_VENDORARCH_DIR} CACHE PATH "The Ruby vendor arch dir" FORCE)
  84. SET(RUBY_VENDORLIB_DIR ${RUBY_VENDORLIB_DIR} CACHE PATH "The Ruby vendor lib dir" FORCE)
  85. MARK_AS_ADVANCED(
  86. RUBY_ARCH_DIR
  87. RUBY_ARCH
  88. RUBY_HDR_DIR
  89. RUBY_POSSIBLE_LIB_DIR
  90. RUBY_RUBY_LIB_DIR
  91. RUBY_SITEARCH_DIR
  92. RUBY_SITELIB_DIR
  93. RUBY_HAS_VENDOR_RUBY
  94. RUBY_VENDORARCH_DIR
  95. RUBY_VENDORLIB_DIR
  96. RUBY_VERSION_MAJOR
  97. RUBY_VERSION_MINOR
  98. RUBY_VERSION_PATCH
  99. )
  100. ENDIF(RUBY_EXECUTABLE AND NOT RUBY_MAJOR_VERSION)
  101. # In case RUBY_EXECUTABLE could not be executed (e.g. cross compiling)
  102. # try to detect which version we found. This is not too good.
  103. IF(NOT RUBY_VERSION_MAJOR)
  104. # by default assume 1.8.0
  105. SET(RUBY_VERSION_MAJOR 1)
  106. SET(RUBY_VERSION_MINOR 8)
  107. SET(RUBY_VERSION_PATCH 0)
  108. # check whether we found 1.9.x
  109. IF(${RUBY_EXECUTABLE} MATCHES "ruby1.?9" OR RUBY_HDR_DIR)
  110. SET(RUBY_VERSION_MAJOR 1)
  111. SET(RUBY_VERSION_MINOR 9)
  112. ENDIF(${RUBY_EXECUTABLE} MATCHES "ruby1.?9" OR RUBY_HDR_DIR)
  113. ENDIF(NOT RUBY_VERSION_MAJOR)
  114. SET(RUBY_VERSION "${RUBY_VERSION_MAJOR}.${RUBY_VERSION_MINOR}.${RUBY_VERSION_PATCH}")
  115. SET(_RUBY_VERSION_SHORT "${RUBY_VERSION_MAJOR}.${RUBY_VERSION_MINOR}")
  116. SET(_RUBY_VERSION_SHORT_NODOT "${RUBY_VERSION_MAJOR}${RUBY_VERSION_MINOR}")
  117. # Now we know which version we found
  118. IF(Ruby_FIND_VERSION)
  119. IF(${RUBY_VERSION} VERSION_LESS ${Ruby_FIND_VERSION})
  120. # force running ruby the next time again
  121. SET(RUBY_VERSION_MAJOR "" CACHE PATH "The Ruby major version" FORCE)
  122. IF(Ruby_FIND_REQUIRED)
  123. MESSAGE(FATAL_ERROR "Ruby version ${Ruby_FIND_VERSION} required, but only version ${RUBY_VERSION} found.")
  124. ELSE(Ruby_FIND_REQUIRED)
  125. IF(NOT Ruby_FIND_QUIETLY)
  126. MESSAGE(STATUS "Ruby version ${Ruby_FIND_VERSION} required, but only version ${RUBY_VERSION} found.")
  127. ENDIF(NOT Ruby_FIND_QUIETLY)
  128. RETURN()
  129. ENDIF(Ruby_FIND_REQUIRED)
  130. ENDIF(${RUBY_VERSION} VERSION_LESS ${Ruby_FIND_VERSION})
  131. ENDIF(Ruby_FIND_VERSION)
  132. FIND_PATH(RUBY_INCLUDE_DIR
  133. NAMES ruby.h
  134. HINTS
  135. ${RUBY_HDR_DIR}
  136. ${RUBY_ARCH_DIR}
  137. /usr/lib/ruby/${_RUBY_VERSION_SHORT}/i586-linux-gnu/ )
  138. SET(RUBY_INCLUDE_DIRS ${RUBY_INCLUDE_DIR} )
  139. # if ruby > 1.8 is required or if ruby > 1.8 was found, search for the config.h dir
  140. IF( ${Ruby_FIND_VERSION_SHORT_NODOT} GREATER 18 OR ${_RUBY_VERSION_SHORT_NODOT} GREATER 18 OR RUBY_HDR_DIR)
  141. message(STATUS "lookign for config.h")
  142. FIND_PATH(RUBY_CONFIG_INCLUDE_DIR
  143. NAMES ruby/config.h config.h
  144. HINTS
  145. ${RUBY_HDR_DIR}/${RUBY_ARCH}
  146. ${RUBY_ARCH_DIR}
  147. )
  148. SET(RUBY_INCLUDE_DIRS ${RUBY_INCLUDE_DIRS} ${RUBY_CONFIG_INCLUDE_DIR} )
  149. ENDIF( ${Ruby_FIND_VERSION_SHORT_NODOT} GREATER 18 OR ${_RUBY_VERSION_SHORT_NODOT} GREATER 18 OR RUBY_HDR_DIR)
  150. # Determine the list of possible names for the ruby library
  151. SET(_RUBY_POSSIBLE_LIB_NAMES ruby ruby-static ruby${_RUBY_VERSION_SHORT})
  152. IF(WIN32)
  153. SET( _RUBY_MSVC_RUNTIME "" )
  154. IF( MSVC60 )
  155. SET( _RUBY_MSVC_RUNTIME "60" )
  156. ENDIF( MSVC60 )
  157. IF( MSVC70 )
  158. SET( _RUBY_MSVC_RUNTIME "70" )
  159. ENDIF( MSVC70 )
  160. IF( MSVC71 )
  161. SET( _RUBY_MSVC_RUNTIME "71" )
  162. ENDIF( MSVC71 )
  163. IF( MSVC80 )
  164. SET( _RUBY_MSVC_RUNTIME "80" )
  165. ENDIF( MSVC80 )
  166. IF( MSVC90 )
  167. SET( _RUBY_MSVC_RUNTIME "90" )
  168. ENDIF( MSVC90 )
  169. LIST(APPEND _RUBY_POSSIBLE_LIB_NAMES
  170. "msvcr${_RUBY_MSVC_RUNTIME}-ruby${RUBY_NODOT_VERSION}"
  171. "msvcr${_RUBY_MSVC_RUNTIME}-ruby${RUBY_NODOT_VERSION}-static"
  172. "msvcrt-ruby${RUBY_NODOT_VERSION}"
  173. "msvcrt-ruby${RUBY_NODOT_VERSION}-static" )
  174. ENDIF(WIN32)
  175. FIND_LIBRARY(RUBY_LIBRARY NAMES ${_RUBY_POSSIBLE_LIB_NAMES} HINTS ${RUBY_POSSIBLE_LIB_DIR} )
  176. INCLUDE(FindPackageHandleStandardArgs)
  177. SET(_RUBY_REQUIRED_VARS RUBY_EXECUTABLE RUBY_INCLUDE_DIR RUBY_LIBRARY)
  178. IF(_RUBY_VERSION_SHORT_NODOT GREATER 18)
  179. LIST(APPEND _RUBY_REQUIRED_VARS RUBY_CONFIG_INCLUDE_DIR)
  180. ENDIF(_RUBY_VERSION_SHORT_NODOT GREATER 18)
  181. IF(_RUBY_DEBUG_OUTPUT)
  182. MESSAGE(STATUS "--------FindRuby.cmake debug------------")
  183. MESSAGE(STATUS "_RUBY_POSSIBLE_EXECUTABLE_NAMES: ${_RUBY_POSSIBLE_EXECUTABLE_NAMES}")
  184. MESSAGE(STATUS "_RUBY_POSSIBLE_LIB_NAMES: ${_RUBY_POSSIBLE_LIB_NAMES}")
  185. MESSAGE(STATUS "RUBY_ARCH_DIR: ${RUBY_ARCH_DIR}")
  186. MESSAGE(STATUS "RUBY_HDR_DIR: ${RUBY_HDR_DIR}")
  187. MESSAGE(STATUS "RUBY_POSSIBLE_LIB_DIR: ${RUBY_POSSIBLE_LIB_DIR}")
  188. MESSAGE(STATUS "Found RUBY_VERSION: \"${RUBY_VERSION}\" , short: \"${_RUBY_VERSION_SHORT}\", nodot: \"${_RUBY_VERSION_SHORT_NODOT}\"")
  189. MESSAGE(STATUS "_RUBY_REQUIRED_VARS: ${_RUBY_REQUIRED_VARS}")
  190. MESSAGE(STATUS "--------------------")
  191. ENDIF(_RUBY_DEBUG_OUTPUT)
  192. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Ruby DEFAULT_MSG ${_RUBY_REQUIRED_VARS})
  193. MARK_AS_ADVANCED(
  194. RUBY_EXECUTABLE
  195. RUBY_LIBRARY
  196. RUBY_INCLUDE_DIR
  197. RUBY_CONFIG_INCLUDE_DIR
  198. )
  199. # Set some variables for compatibility with previous version of this file
  200. SET(RUBY_POSSIBLE_LIB_PATH ${RUBY_POSSIBLE_LIB_DIR})
  201. SET(RUBY_RUBY_LIB_PATH ${RUBY_RUBY_LIB_DIR})
  202. SET(RUBY_INCLUDE_PATH ${RUBY_INCLUDE_DIRS})