FindOpenSceneGraph.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. FindOpenSceneGraph
  5. ------------------
  6. Finds `OpenSceneGraph`_ (OSG), a 3D graphics application programming interface.
  7. .. note::
  8. OpenSceneGraph development has largely transitioned to its successor project,
  9. VulkanSceneGraph, which should be preferred for new code. Refer to the
  10. upstream documentation for guidance on using VulkanSceneGraph with CMake.
  11. This module searches for the OpenSceneGraph core osg library, its dependency
  12. OpenThreads, and additional OpenSceneGraph libraries, some of which are also
  13. known as *NodeKits*, if specified.
  14. When working with OpenSceneGraph, its core library headers are intended to be
  15. included in C++ project source code as:
  16. .. code-block:: c++
  17. #include <osg/PositionAttitudeTransform>
  18. Headers for the OpenSceneGraph libraries and NodeKits follow a similar inclusion
  19. structure, for example:
  20. .. code-block:: c++
  21. #include <osgAnimation/Animation>
  22. #include <osgDB/DatabasePager>
  23. #include <osgFX/BumpMapping>
  24. // ...
  25. .. _`OpenSceneGraph`: https://openscenegraph.github.io/openscenegraph.io/
  26. Components
  27. ^^^^^^^^^^
  28. OpenSceneGraph toolkit consists of the core library osg, and additional
  29. libraries, which can be optionally specified as components with the
  30. :command:`find_package` command:
  31. .. code-block:: cmake
  32. find_package(OpenSceneGraph [COMPONENTS <components>...])
  33. Supported components include:
  34. ``osg``
  35. Finds the core osg library (``libosg``), required to use OpenSceneGraph.
  36. This component is always automatically implied.
  37. ``OpenThreads``
  38. Finds the dependent OpenThreads library (``libOpenThreads``) via the
  39. :module:`FindOpenThreads` module. This component is always automatically
  40. implied as it is required to use OpenSceneGraph.
  41. ``osgAnimation``
  42. Finds the osgAnimation library, which provides general purpose utility classes
  43. for animation.
  44. ``osgDB``
  45. Finds the osgDB library for reading and writing scene graphs support.
  46. ``osgFX``
  47. Finds the osgFX NodeKit, which provides a framework for implementing special
  48. effects.
  49. ``osgGA``
  50. Finds the osgGA (GUI Abstraction) library, which provides facilities to work
  51. with varying window systems.
  52. ``osgIntrospection``
  53. Finds the osgIntrospection library, which provides a reflection framework for
  54. accessing and invoking class properties and methods at runtime without
  55. modifying the classes.
  56. .. note::
  57. The osgIntrospection library has been removed from the OpenSceneGraph
  58. toolkit as of OpenSceneGraph version 3.0.
  59. ``osgManipulator``
  60. Finds the osgManipulator NodeKit, which provides support for 3D interactive
  61. manipulators.
  62. ``osgParticle``
  63. Finds the osgParticle NodeKit, which provides support for particle effects.
  64. ``osgPresentation``
  65. Finds the osgPresentation NodeKit, which provides support for 3D scene graph
  66. based presentations.
  67. .. note::
  68. This NodeKit has been added in OpenSceneGraph 3.0.0.
  69. ``osgProducer``
  70. Finds the osgProducer utility library, which provides functionality for window
  71. management and event handling.
  72. .. note::
  73. The osgProducer has been removed from early versions of OpenSceneGraph
  74. toolkit 1.x, and has been superseded by the osgViewer library.
  75. ``osgQt``
  76. Finds the osgQt NodeKit, which provides various classes to aid the integration
  77. of Qt.
  78. .. note::
  79. As of OpenSceneGraph version 3.6, this NodeKit has been moved to its own
  80. repository.
  81. ``osgShadow``
  82. Finds the osgShadow NodeKit, which provides support for a range of shadow
  83. techniques.
  84. ``osgSim``
  85. Finds the osgSim NodeKit, which adds support for simulation features like
  86. navigation lights and OpenFlight-style movement controls.
  87. ``osgTerrain``
  88. Finds the osgTerrain NodeKit, which provides geospecifc terrain rendering
  89. support.
  90. ``osgText``
  91. Finds the osgText NodeKit, which provides high quality text support.
  92. ``osgUtil``
  93. Finds the osgUtil library, which provides general-purpose utilities like
  94. update, cull, and draw traversals, as well as scene graph tools such as
  95. optimization, triangle stripping, and tessellation.
  96. ``osgViewer``
  97. Finds the osgViewer library, which provides high level viewer functionality.
  98. ``osgVolume``
  99. Finds the osgVolume NodeKit, which provides volume rendering support.
  100. ``osgWidget``
  101. Finds the osgWidget NodeKit, which provides support for 2D and 3D GUI widget
  102. sets.
  103. If no components are specified, this module searches for the ``osg`` and
  104. ``OpenThreads`` components by default.
  105. Result Variables
  106. ^^^^^^^^^^^^^^^^
  107. This module defines the following variables:
  108. ``OpenSceneGraph_FOUND``
  109. Boolean indicating whether the (requested version of) OpenSceneGraph with all
  110. specified components is found. For backward compatibility, the
  111. ``OPENSCENEGRAPH_FOUND`` variable is also set to the same value.
  112. ``OPENSCENEGRAPH_VERSION``
  113. The version of the OSG which was found.
  114. ``OPENSCENEGRAPH_INCLUDE_DIRS``
  115. Include directories containing headers needed to use OpenSceneGraph.
  116. ``OPENSCENEGRAPH_LIBRARIES``
  117. Libraries needed to link against to use OpenSceneGraph.
  118. Hints
  119. ^^^^^
  120. This module accepts the following variables:
  121. ``OpenSceneGraph_DEBUG``
  122. Set this variable to boolean true to enable debugging output by this module.
  123. ``OpenSceneGraph_MARK_AS_ADVANCED``
  124. Set this variable to boolean true to mark cache variables of this module as
  125. advanced automatically.
  126. To help this module find OpenSceneGraph and its various components installed in
  127. custom location, :variable:`CMAKE_PREFIX_PATH` variable can be used.
  128. Additionally, the following variables are also respected:
  129. ``<COMPONENT>_DIR``
  130. Environment or CMake variable that can be set to the root of the OSG common
  131. installation, where ``<COMPONENT>`` is the uppercase form of component listed
  132. above. For example, ``OSGVOLUME_DIR`` to find the ``osgVolume`` component.
  133. ``OSG_DIR``
  134. Environment or CMake variable that can be set to influence detection of
  135. OpenSceneGraph installation root location as a whole.
  136. ``OSGDIR``
  137. Environment variable treated the same as ``OSG_DIR``.
  138. ``OSG_ROOT``
  139. Environment variable treated the same as ``OSG_DIR``.
  140. Examples
  141. ^^^^^^^^
  142. Finding the OpenSceneGraph with ``osgDB`` and ``osgUtil`` libraries specified as
  143. components and creating an interface :ref:`imported target <Imported Targets>`
  144. that encapsulates its usage requirements for linking to a project target:
  145. .. code-block:: cmake
  146. find_package(OpenSceneGraph 2.0.0 REQUIRED COMPONENTS osgDB osgUtil)
  147. if(OpenSceneGraph_FOUND AND NOT TARGET OpenSceneGraph::OpenSceneGraph)
  148. add_library(OpenSceneGraph::OpenSceneGraph INTERFACE IMPORTED)
  149. set_target_properties(
  150. OpenSceneGraph::OpenSceneGraph
  151. PROPERTIES
  152. INTERFACE_INCLUDE_DIRECTORIES "${OPENSCENEGRAPH_INCLUDE_DIRS}"
  153. INTERFACE_LINK_LIBRARIES "${OPENSCENEGRAPH_LIBRARIES}"
  154. )
  155. endif()
  156. add_executable(foo foo.cc)
  157. target_link_libraries(foo PRIVATE OpenSceneGraph::OpenSceneGraph)
  158. See Also
  159. ^^^^^^^^
  160. The following OpenSceneGraph-related helper find modules are used internally by
  161. this module when finding specific OpenSceneGraph components. These modules are
  162. not intended to be included or invoked directly by project code during typical
  163. use of ``find_package(OpenSceneGraph)``. However, they can be useful for
  164. advanced scenarios where finer control over component detection is needed. For
  165. example, to find them explicitly and override or bypass detection of specific
  166. OpenSceneGraph components:
  167. * The :module:`Findosg` module to find the core osg library.
  168. * The :module:`FindosgAnimation` module to find osgAnimation.
  169. * The :module:`FindosgDB` module to find osgDB.
  170. * The :module:`FindosgFX` module to find osgDB.
  171. * The :module:`FindosgGA` module to find osgGA.
  172. * The :module:`FindosgIntrospection` module to find osgIntrospection.
  173. * The :module:`FindosgManipulator` module to find osgManipulator.
  174. * The :module:`FindosgParticle` module to find osgParticle.
  175. * The :module:`FindosgPresentation` module to find osgPresentation.
  176. * The :module:`FindosgProducer` module to find osgProducer.
  177. * The :module:`FindosgQt` module to find osgQt.
  178. * The :module:`FindosgShadow` module to find osgShadow.
  179. * The :module:`FindosgSim` module to find osgSim.
  180. * The :module:`FindosgTerrain` module to find osgTerrain.
  181. * The :module:`FindosgText` module to find osgText.
  182. * The :module:`FindosgUtil` module to find osgUtil.
  183. * The :module:`FindosgViewer` module to find osgViewer.
  184. * The :module:`FindosgVolume` module to find osgVolume.
  185. * The :module:`FindosgWidget` module to find osgWidget.
  186. #]=======================================================================]
  187. cmake_policy(PUSH)
  188. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  189. include(${CMAKE_CURRENT_LIST_DIR}/Findosg_functions.cmake)
  190. set(_osg_modules_to_process)
  191. foreach(_osg_component ${OpenSceneGraph_FIND_COMPONENTS})
  192. list(APPEND _osg_modules_to_process ${_osg_component})
  193. endforeach()
  194. list(APPEND _osg_modules_to_process "osg" "OpenThreads")
  195. list(REMOVE_DUPLICATES _osg_modules_to_process)
  196. if(OpenSceneGraph_DEBUG)
  197. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  198. "Components = ${_osg_modules_to_process}")
  199. endif()
  200. #
  201. # First we need to find and parse osg/Version
  202. #
  203. OSG_FIND_PATH(OSG osg/Version)
  204. if(OpenSceneGraph_MARK_AS_ADVANCED)
  205. OSG_MARK_AS_ADVANCED(OSG)
  206. endif()
  207. # Try to ascertain the version...
  208. if(OSG_INCLUDE_DIR)
  209. if(OpenSceneGraph_DEBUG)
  210. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  211. "Detected OSG_INCLUDE_DIR = ${OSG_INCLUDE_DIR}")
  212. endif()
  213. set(_osg_Version_file "${OSG_INCLUDE_DIR}/osg/Version")
  214. if("${OSG_INCLUDE_DIR}" MATCHES "\\.framework$" AND NOT EXISTS "${_osg_Version_file}")
  215. set(_osg_Version_file "${OSG_INCLUDE_DIR}/Headers/Version")
  216. endif()
  217. if(EXISTS "${_osg_Version_file}")
  218. file(STRINGS "${_osg_Version_file}" _osg_Version_contents
  219. REGEX "#define (OSG_VERSION_[A-Z]+|OPENSCENEGRAPH_[A-Z]+_VERSION)[ \t]+[0-9]+")
  220. else()
  221. set(_osg_Version_contents "unknown")
  222. endif()
  223. string(REGEX MATCH ".*#define OSG_VERSION_MAJOR[ \t]+[0-9]+.*"
  224. _osg_old_defines "${_osg_Version_contents}")
  225. string(REGEX MATCH ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+[0-9]+.*"
  226. _osg_new_defines "${_osg_Version_contents}")
  227. if(_osg_old_defines)
  228. string(REGEX REPLACE ".*#define OSG_VERSION_MAJOR[ \t]+([0-9]+).*"
  229. "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
  230. string(REGEX REPLACE ".*#define OSG_VERSION_MINOR[ \t]+([0-9]+).*"
  231. "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
  232. string(REGEX REPLACE ".*#define OSG_VERSION_PATCH[ \t]+([0-9]+).*"
  233. "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
  234. elseif(_osg_new_defines)
  235. string(REGEX REPLACE ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+([0-9]+).*"
  236. "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
  237. string(REGEX REPLACE ".*#define OPENSCENEGRAPH_MINOR_VERSION[ \t]+([0-9]+).*"
  238. "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
  239. string(REGEX REPLACE ".*#define OPENSCENEGRAPH_PATCH_VERSION[ \t]+([0-9]+).*"
  240. "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
  241. else()
  242. message(WARNING "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  243. "Failed to parse version number, please report this as a bug")
  244. endif()
  245. unset(_osg_Version_contents)
  246. set(OPENSCENEGRAPH_VERSION "${_osg_VERSION_MAJOR}.${_osg_VERSION_MINOR}.${_osg_VERSION_PATCH}"
  247. CACHE INTERNAL "The version of OSG which was detected")
  248. if(OpenSceneGraph_DEBUG)
  249. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  250. "Detected version ${OPENSCENEGRAPH_VERSION}")
  251. endif()
  252. endif()
  253. set(_osg_quiet)
  254. if(OpenSceneGraph_FIND_QUIETLY)
  255. set(_osg_quiet "QUIET")
  256. endif()
  257. #
  258. # Here we call find_package() on all of the components
  259. #
  260. foreach(_osg_module ${_osg_modules_to_process})
  261. if(OpenSceneGraph_DEBUG)
  262. message(STATUS "[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
  263. "Calling find_package(${_osg_module} ${_osg_required} ${_osg_quiet})")
  264. endif()
  265. find_package(${_osg_module} ${_osg_quiet})
  266. string(TOUPPER ${_osg_module} _osg_module_UC)
  267. # append to list if module was found OR is required
  268. if( ${_osg_module_UC}_FOUND OR OpenSceneGraph_FIND_REQUIRED )
  269. list(APPEND OPENSCENEGRAPH_INCLUDE_DIR ${${_osg_module_UC}_INCLUDE_DIR})
  270. list(APPEND OPENSCENEGRAPH_LIBRARIES ${${_osg_module_UC}_LIBRARIES})
  271. endif()
  272. if(OpenSceneGraph_MARK_AS_ADVANCED)
  273. OSG_MARK_AS_ADVANCED(${_osg_module})
  274. endif()
  275. endforeach()
  276. if(OPENSCENEGRAPH_INCLUDE_DIR)
  277. list(REMOVE_DUPLICATES OPENSCENEGRAPH_INCLUDE_DIR)
  278. endif()
  279. #
  280. # Check each module to see if it's found
  281. #
  282. set(_osg_component_founds)
  283. if(OpenSceneGraph_FIND_REQUIRED)
  284. foreach(_osg_module ${_osg_modules_to_process})
  285. string(TOUPPER ${_osg_module} _osg_module_UC)
  286. list(APPEND _osg_component_founds ${_osg_module_UC}_FOUND)
  287. endforeach()
  288. endif()
  289. include(FindPackageHandleStandardArgs)
  290. find_package_handle_standard_args(OpenSceneGraph
  291. REQUIRED_VARS OPENSCENEGRAPH_LIBRARIES OPENSCENEGRAPH_INCLUDE_DIR ${_osg_component_founds}
  292. VERSION_VAR OPENSCENEGRAPH_VERSION)
  293. unset(_osg_component_founds)
  294. set(OPENSCENEGRAPH_INCLUDE_DIRS ${OPENSCENEGRAPH_INCLUDE_DIR})
  295. cmake_policy(POP)