FindOpenSceneGraph.cmake 14 KB

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