FindOpenSceneGraph.cmake 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. FindOpenSceneGraph
  5. ------------------
  6. Find OpenSceneGraph (3D graphics application programming interface)
  7. This module searches for the OpenSceneGraph core "osg" library as well
  8. as :module:`FindOpenThreads`, and whatever additional ``COMPONENTS``
  9. (nodekits) that you specify.
  10. ::
  11. See http://www.openscenegraph.org
  12. NOTE: To use this module effectively you must either require ``CMake >=
  13. 2.6.3`` with :command:`cmake_minimum_required(VERSION 2.6.3)` or download
  14. and place :module:`FindOpenThreads`, :module:`Findosg` functions,
  15. :module:`Findosg` and ``Find<etc>.cmake`` files into your
  16. :variable:`CMAKE_MODULE_PATH`.
  17. ==================================
  18. This module accepts the following variables (note mixed case)
  19. ::
  20. OpenSceneGraph_DEBUG - Enable debugging output
  21. ::
  22. OpenSceneGraph_MARK_AS_ADVANCED - Mark cache variables as advanced
  23. automatically
  24. The following environment variables are also respected for finding the
  25. OSG and it's various components. :variable:`CMAKE_PREFIX_PATH` can also be
  26. used for this (see :command:`find_library` CMake documentation).
  27. ``<MODULE>_DIR``
  28. (where ``MODULE`` is of the form "OSGVOLUME" and there is
  29. a :module:`FindosgVolume`.cmake` file)
  30. ``OSG_DIR``
  31. ..
  32. ``OSGDIR``
  33. ..
  34. ``OSG_ROOT``
  35. ..
  36. [CMake 2.8.10]: The CMake variable ``OSG_DIR`` can now be used as well to
  37. influence detection, instead of needing to specify an environment
  38. variable.
  39. This module defines the following output variables:
  40. ::
  41. OPENSCENEGRAPH_FOUND - Was the OSG and all of the specified components found?
  42. ::
  43. OPENSCENEGRAPH_VERSION - The version of the OSG which was found
  44. ::
  45. OPENSCENEGRAPH_INCLUDE_DIRS - Where to find the headers
  46. ::
  47. OPENSCENEGRAPH_LIBRARIES - The OSG libraries
  48. ================================== Example Usage:
  49. .. code-block:: cmake
  50. find_package(OpenSceneGraph 2.0.0 REQUIRED osgDB osgUtil)
  51. # libOpenThreads & libosg automatically searched
  52. include_directories(${OPENSCENEGRAPH_INCLUDE_DIRS})
  53. .. code-block:: cmake
  54. add_executable(foo foo.cc)
  55. target_link_libraries(foo ${OPENSCENEGRAPH_LIBRARIES})
  56. #]=======================================================================]
  57. cmake_policy(PUSH)
  58. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  59. #
  60. # Naming convention:
  61. # Local variables of the form _osg_foo
  62. # Input variables of the form OpenSceneGraph_FOO
  63. # Output variables of the form OPENSCENEGRAPH_FOO
  64. #
  65. include(${CMAKE_CURRENT_LIST_DIR}/Findosg_functions.cmake)
  66. set(_osg_modules_to_process)
  67. foreach(_osg_component ${OpenSceneGraph_FIND_COMPONENTS})
  68. list(APPEND _osg_modules_to_process ${_osg_component})
  69. endforeach()
  70. list(APPEND _osg_modules_to_process "osg" "OpenThreads")
  71. list(REMOVE_DUPLICATES _osg_modules_to_process)
  72. if(OpenSceneGraph_DEBUG)
  73. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  74. "Components = ${_osg_modules_to_process}")
  75. endif()
  76. #
  77. # First we need to find and parse osg/Version
  78. #
  79. OSG_FIND_PATH(OSG osg/Version)
  80. if(OpenSceneGraph_MARK_AS_ADVANCED)
  81. OSG_MARK_AS_ADVANCED(OSG)
  82. endif()
  83. # Try to ascertain the version...
  84. if(OSG_INCLUDE_DIR)
  85. if(OpenSceneGraph_DEBUG)
  86. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  87. "Detected OSG_INCLUDE_DIR = ${OSG_INCLUDE_DIR}")
  88. endif()
  89. set(_osg_Version_file "${OSG_INCLUDE_DIR}/osg/Version")
  90. if("${OSG_INCLUDE_DIR}" MATCHES "\\.framework$" AND NOT EXISTS "${_osg_Version_file}")
  91. set(_osg_Version_file "${OSG_INCLUDE_DIR}/Headers/Version")
  92. endif()
  93. if(EXISTS "${_osg_Version_file}")
  94. file(STRINGS "${_osg_Version_file}" _osg_Version_contents
  95. REGEX "#define (OSG_VERSION_[A-Z]+|OPENSCENEGRAPH_[A-Z]+_VERSION)[ \t]+[0-9]+")
  96. else()
  97. set(_osg_Version_contents "unknown")
  98. endif()
  99. string(REGEX MATCH ".*#define OSG_VERSION_MAJOR[ \t]+[0-9]+.*"
  100. _osg_old_defines "${_osg_Version_contents}")
  101. string(REGEX MATCH ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+[0-9]+.*"
  102. _osg_new_defines "${_osg_Version_contents}")
  103. if(_osg_old_defines)
  104. string(REGEX REPLACE ".*#define OSG_VERSION_MAJOR[ \t]+([0-9]+).*"
  105. "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
  106. string(REGEX REPLACE ".*#define OSG_VERSION_MINOR[ \t]+([0-9]+).*"
  107. "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
  108. string(REGEX REPLACE ".*#define OSG_VERSION_PATCH[ \t]+([0-9]+).*"
  109. "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
  110. elseif(_osg_new_defines)
  111. string(REGEX REPLACE ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+([0-9]+).*"
  112. "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
  113. string(REGEX REPLACE ".*#define OPENSCENEGRAPH_MINOR_VERSION[ \t]+([0-9]+).*"
  114. "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
  115. string(REGEX REPLACE ".*#define OPENSCENEGRAPH_PATCH_VERSION[ \t]+([0-9]+).*"
  116. "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
  117. else()
  118. message(WARNING "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  119. "Failed to parse version number, please report this as a bug")
  120. endif()
  121. unset(_osg_Version_contents)
  122. set(OPENSCENEGRAPH_VERSION "${_osg_VERSION_MAJOR}.${_osg_VERSION_MINOR}.${_osg_VERSION_PATCH}"
  123. CACHE INTERNAL "The version of OSG which was detected")
  124. if(OpenSceneGraph_DEBUG)
  125. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  126. "Detected version ${OPENSCENEGRAPH_VERSION}")
  127. endif()
  128. endif()
  129. set(_osg_quiet)
  130. if(OpenSceneGraph_FIND_QUIETLY)
  131. set(_osg_quiet "QUIET")
  132. endif()
  133. #
  134. # Here we call find_package() on all of the components
  135. #
  136. foreach(_osg_module ${_osg_modules_to_process})
  137. if(OpenSceneGraph_DEBUG)
  138. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  139. "Calling find_package(${_osg_module} ${_osg_required} ${_osg_quiet})")
  140. endif()
  141. find_package(${_osg_module} ${_osg_quiet})
  142. string(TOUPPER ${_osg_module} _osg_module_UC)
  143. # append to list if module was found OR is required
  144. if( ${_osg_module_UC}_FOUND OR OpenSceneGraph_FIND_REQUIRED )
  145. list(APPEND OPENSCENEGRAPH_INCLUDE_DIR ${${_osg_module_UC}_INCLUDE_DIR})
  146. list(APPEND OPENSCENEGRAPH_LIBRARIES ${${_osg_module_UC}_LIBRARIES})
  147. endif()
  148. if(OpenSceneGraph_MARK_AS_ADVANCED)
  149. OSG_MARK_AS_ADVANCED(${_osg_module})
  150. endif()
  151. endforeach()
  152. if(OPENSCENEGRAPH_INCLUDE_DIR)
  153. list(REMOVE_DUPLICATES OPENSCENEGRAPH_INCLUDE_DIR)
  154. endif()
  155. #
  156. # Check each module to see if it's found
  157. #
  158. set(_osg_component_founds)
  159. if(OpenSceneGraph_FIND_REQUIRED)
  160. foreach(_osg_module ${_osg_modules_to_process})
  161. string(TOUPPER ${_osg_module} _osg_module_UC)
  162. list(APPEND _osg_component_founds ${_osg_module_UC}_FOUND)
  163. endforeach()
  164. endif()
  165. include(FindPackageHandleStandardArgs)
  166. find_package_handle_standard_args(OpenSceneGraph
  167. REQUIRED_VARS OPENSCENEGRAPH_LIBRARIES OPENSCENEGRAPH_INCLUDE_DIR ${_osg_component_founds}
  168. VERSION_VAR OPENSCENEGRAPH_VERSION)
  169. unset(_osg_component_founds)
  170. set(OPENSCENEGRAPH_INCLUDE_DIRS ${OPENSCENEGRAPH_INCLUDE_DIR})
  171. cmake_policy(POP)