FindOpenVDB.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. # Copyright (c) DreamWorks Animation LLC
  2. #
  3. # All rights reserved. This software is distributed under the
  4. # Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
  5. #
  6. # Redistributions of source code must retain the above copyright
  7. # and license notice and the following restrictions and disclaimer.
  8. #
  9. # * Neither the name of DreamWorks Animation nor the names of
  10. # its contributors may be used to endorse or promote products derived
  11. # from this software without specific prior written permission.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
  18. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. # IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
  25. # LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
  26. #
  27. #[=======================================================================[.rst:
  28. FindOpenVDB
  29. -----------
  30. Find OpenVDB include dirs, libraries and settings
  31. Use this module by invoking find_package with the form::
  32. find_package(OpenVDB
  33. [version] [EXACT] # Minimum or EXACT version
  34. [REQUIRED] # Fail with error if OpenVDB is not found
  35. [COMPONENTS <libs>...] # OpenVDB libraries by their canonical name
  36. # e.g. "openvdb" for "libopenvdb"
  37. )
  38. IMPORTED Targets
  39. ^^^^^^^^^^^^^^^^
  40. ``OpenVDB::openvdb``
  41. The core openvdb library target.
  42. Result Variables
  43. ^^^^^^^^^^^^^^^^
  44. This will define the following variables:
  45. ``OpenVDB_FOUND``
  46. True if the system has the OpenVDB library.
  47. ``OpenVDB_VERSION``
  48. The version of the OpenVDB library which was found.
  49. ``OpenVDB_INCLUDE_DIRS``
  50. Include directories needed to use OpenVDB.
  51. ``OpenVDB_LIBRARIES``
  52. Libraries needed to link to OpenVDB.
  53. ``OpenVDB_LIBRARY_DIRS``
  54. OpenVDB library directories.
  55. ``OpenVDB_DEFINITIONS``
  56. Definitions to use when compiling code that uses OpenVDB.
  57. ``OpenVDB_{COMPONENT}_FOUND``
  58. True if the system has the named OpenVDB component.
  59. ``OpenVDB_USES_BLOSC``
  60. True if the OpenVDB Library has been built with blosc support
  61. ``OpenVDB_USES_LOG4CPLUS``
  62. True if the OpenVDB Library has been built with log4cplus support
  63. ``OpenVDB_USES_EXR``
  64. True if the OpenVDB Library has been built with openexr support
  65. ``OpenVDB_ABI``
  66. Set if this module was able to determine the ABI number the located
  67. OpenVDB Library was built against. Unset otherwise.
  68. Cache Variables
  69. ^^^^^^^^^^^^^^^
  70. The following cache variables may also be set:
  71. ``OpenVDB_INCLUDE_DIR``
  72. The directory containing ``openvdb/version.h``.
  73. ``OpenVDB_{COMPONENT}_LIBRARY``
  74. Individual component libraries for OpenVDB
  75. Hints
  76. ^^^^^
  77. Instead of explicitly setting the cache variables, the following variables
  78. may be provided to tell this module where to look.
  79. ``OPENVDB_ROOT``
  80. Preferred installation prefix.
  81. ``OPENVDB_INCLUDEDIR``
  82. Preferred include directory e.g. <prefix>/include
  83. ``OPENVDB_LIBRARYDIR``
  84. Preferred library directory e.g. <prefix>/lib
  85. ``SYSTEM_LIBRARY_PATHS``
  86. Paths appended to all include and lib searches.
  87. #]=======================================================================]
  88. cmake_minimum_required(VERSION 3.3)
  89. # Monitoring <PackageName>_ROOT variables
  90. if(POLICY CMP0074)
  91. cmake_policy(SET CMP0074 NEW)
  92. endif()
  93. # Include utility functions for version information
  94. include(${CMAKE_CURRENT_LIST_DIR}/OpenVDBUtils.cmake)
  95. mark_as_advanced(
  96. OpenVDB_INCLUDE_DIR
  97. OpenVDB_LIBRARY
  98. )
  99. set(_OPENVDB_COMPONENT_LIST
  100. openvdb
  101. )
  102. if(OpenVDB_FIND_COMPONENTS)
  103. set(OPENVDB_COMPONENTS_PROVIDED TRUE)
  104. set(_IGNORED_COMPONENTS "")
  105. foreach(COMPONENT ${OpenVDB_FIND_COMPONENTS})
  106. if(NOT ${COMPONENT} IN_LIST _OPENVDB_COMPONENT_LIST)
  107. list(APPEND _IGNORED_COMPONENTS ${COMPONENT})
  108. endif()
  109. endforeach()
  110. if(_IGNORED_COMPONENTS)
  111. message(STATUS "Ignoring unknown components of OpenVDB:")
  112. foreach(COMPONENT ${_IGNORED_COMPONENTS})
  113. message(STATUS " ${COMPONENT}")
  114. endforeach()
  115. list(REMOVE_ITEM OpenVDB_FIND_COMPONENTS ${_IGNORED_COMPONENTS})
  116. endif()
  117. else()
  118. set(OPENVDB_COMPONENTS_PROVIDED FALSE)
  119. set(OpenVDB_FIND_COMPONENTS ${_OPENVDB_COMPONENT_LIST})
  120. endif()
  121. # Append OPENVDB_ROOT or $ENV{OPENVDB_ROOT} if set (prioritize the direct cmake var)
  122. set(_OPENVDB_ROOT_SEARCH_DIR "")
  123. # Additionally try and use pkconfig to find OpenVDB
  124. find_package(PkgConfig)
  125. pkg_check_modules(PC_OpenVDB QUIET OpenVDB)
  126. # ------------------------------------------------------------------------
  127. # Search for OpenVDB include DIR
  128. # ------------------------------------------------------------------------
  129. set(_OPENVDB_INCLUDE_SEARCH_DIRS "")
  130. list(APPEND _OPENVDB_INCLUDE_SEARCH_DIRS
  131. ${OPENVDB_INCLUDEDIR}
  132. ${_OPENVDB_ROOT_SEARCH_DIR}
  133. ${PC_OpenVDB_INCLUDE_DIRS}
  134. ${SYSTEM_LIBRARY_PATHS}
  135. )
  136. # Look for a standard OpenVDB header file.
  137. find_path(OpenVDB_INCLUDE_DIR openvdb/version.h
  138. PATHS ${_OPENVDB_INCLUDE_SEARCH_DIRS}
  139. PATH_SUFFIXES include
  140. )
  141. OPENVDB_VERSION_FROM_HEADER("${OpenVDB_INCLUDE_DIR}/openvdb/version.h"
  142. VERSION OpenVDB_VERSION
  143. MAJOR OpenVDB_MAJOR_VERSION
  144. MINOR OpenVDB_MINOR_VERSION
  145. PATCH OpenVDB_PATCH_VERSION
  146. )
  147. # ------------------------------------------------------------------------
  148. # Search for OPENVDB lib DIR
  149. # ------------------------------------------------------------------------
  150. set(_OPENVDB_LIBRARYDIR_SEARCH_DIRS "")
  151. # Append to _OPENVDB_LIBRARYDIR_SEARCH_DIRS in priority order
  152. list(APPEND _OPENVDB_LIBRARYDIR_SEARCH_DIRS
  153. ${OPENVDB_LIBRARYDIR}
  154. ${_OPENVDB_ROOT_SEARCH_DIR}
  155. ${PC_OpenVDB_LIBRARY_DIRS}
  156. ${SYSTEM_LIBRARY_PATHS}
  157. )
  158. # Build suffix directories
  159. set(OPENVDB_PATH_SUFFIXES
  160. lib64
  161. lib
  162. )
  163. # Static library setup
  164. if(UNIX AND OPENVDB_USE_STATIC_LIBS)
  165. set(_OPENVDB_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  166. set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
  167. endif()
  168. set(OpenVDB_LIB_COMPONENTS "")
  169. foreach(COMPONENT ${OpenVDB_FIND_COMPONENTS})
  170. set(LIB_NAME ${COMPONENT})
  171. find_library(OpenVDB_${COMPONENT}_LIBRARY ${LIB_NAME} lib${LIB_NAME}
  172. PATHS ${_OPENVDB_LIBRARYDIR_SEARCH_DIRS}
  173. PATH_SUFFIXES ${OPENVDB_PATH_SUFFIXES}
  174. )
  175. list(APPEND OpenVDB_LIB_COMPONENTS ${OpenVDB_${COMPONENT}_LIBRARY})
  176. if(OpenVDB_${COMPONENT}_LIBRARY)
  177. set(OpenVDB_${COMPONENT}_FOUND TRUE)
  178. else()
  179. set(OpenVDB_${COMPONENT}_FOUND FALSE)
  180. endif()
  181. endforeach()
  182. if(UNIX AND OPENVDB_USE_STATIC_LIBS)
  183. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_OPENVDB_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  184. unset(_OPENVDB_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
  185. endif()
  186. # ------------------------------------------------------------------------
  187. # Cache and set OPENVDB_FOUND
  188. # ------------------------------------------------------------------------
  189. include(FindPackageHandleStandardArgs)
  190. find_package_handle_standard_args(OpenVDB
  191. FOUND_VAR OpenVDB_FOUND
  192. REQUIRED_VARS
  193. OpenVDB_INCLUDE_DIR
  194. OpenVDB_LIB_COMPONENTS
  195. VERSION_VAR OpenVDB_VERSION
  196. HANDLE_COMPONENTS
  197. )
  198. # ------------------------------------------------------------------------
  199. # Determine ABI number
  200. # ------------------------------------------------------------------------
  201. # Set the ABI number the library was built against. Uses vdb_print
  202. find_program(OPENVDB_PRINT vdb_print PATHS ${OpenVDB_INCLUDE_DIR} )
  203. OPENVDB_ABI_VERSION_FROM_PRINT(
  204. "${OPENVDB_PRINT}"
  205. ABI OpenVDB_ABI
  206. )
  207. if(NOT OpenVDB_FIND_QUIET)
  208. if(NOT OpenVDB_ABI)
  209. message(WARNING "Unable to determine OpenVDB ABI version from OpenVDB "
  210. "installation. The library major version \"${OpenVDB_MAJOR_VERSION}\" "
  211. "will be inferred. If this is not correct, use "
  212. "add_definitions(-DOPENVDB_ABI_VERSION_NUMBER=N)"
  213. )
  214. else()
  215. message(STATUS "OpenVDB ABI Version: ${OpenVDB_ABI}")
  216. endif()
  217. endif()
  218. # ------------------------------------------------------------------------
  219. # Handle OpenVDB dependencies
  220. # ------------------------------------------------------------------------
  221. # Add standard dependencies
  222. find_package(IlmBase COMPONENTS Half)
  223. if(NOT IlmBase_FOUND)
  224. pkg_check_modules(IlmBase QUIET IlmBase)
  225. endif()
  226. if (IlmBase_FOUND AND NOT TARGET IlmBase::Half)
  227. message(STATUS "Falling back to IlmBase found by pkg-config...")
  228. find_library(IlmHalf_LIBRARY NAMES Half)
  229. if(IlmHalf_LIBRARY-NOTFOUND)
  230. message(FATAL_ERROR "IlmBase::Half can not be found!")
  231. endif()
  232. add_library(IlmBase::Half UNKNOWN IMPORTED)
  233. set_target_properties(IlmBase::Half PROPERTIES
  234. IMPORTED_LOCATION "${IlmHalf_LIBRARY}"
  235. INTERFACE_INCLUDE_DIRECTORIES ${IlmBase_INCLUDE_DIRS})
  236. elseif(NOT IlmBase_FOUND)
  237. message(FATAL_ERROR "IlmBase::Half can not be found!")
  238. endif()
  239. find_package(TBB REQUIRED COMPONENTS tbb)
  240. find_package(ZLIB REQUIRED)
  241. find_package(Boost REQUIRED COMPONENTS iostreams system)
  242. # Use GetPrerequisites to see which libraries this OpenVDB lib has linked to
  243. # which we can query for optional deps. This basically runs ldd/otoll/objdump
  244. # etc to track deps. We could use a vdb_config binary tools here to improve
  245. # this process
  246. include(GetPrerequisites)
  247. set(_EXCLUDE_SYSTEM_PREREQUISITES 1)
  248. set(_RECURSE_PREREQUISITES 0)
  249. set(_OPENVDB_PREREQUISITE_LIST)
  250. if(NOT OPENVDB_USE_STATIC_LIBS)
  251. get_prerequisites(${OpenVDB_openvdb_LIBRARY}
  252. _OPENVDB_PREREQUISITE_LIST
  253. ${_EXCLUDE_SYSTEM_PREREQUISITES}
  254. ${_RECURSE_PREREQUISITES}
  255. ""
  256. "${SYSTEM_LIBRARY_PATHS}"
  257. )
  258. endif()
  259. unset(_EXCLUDE_SYSTEM_PREREQUISITES)
  260. unset(_RECURSE_PREREQUISITES)
  261. # As the way we resolve optional libraries relies on library file names, use
  262. # the configuration options from the main CMakeLists.txt to allow users
  263. # to manually identify the requirements of OpenVDB builds if they know them.
  264. set(OpenVDB_USES_BLOSC ${USE_BLOSC})
  265. set(OpenVDB_USES_LOG4CPLUS ${USE_LOG4CPLUS})
  266. set(OpenVDB_USES_ILM ${USE_EXR})
  267. set(OpenVDB_USES_EXR ${USE_EXR})
  268. # Search for optional dependencies
  269. foreach(PREREQUISITE ${_OPENVDB_PREREQUISITE_LIST})
  270. set(_HAS_DEP)
  271. get_filename_component(PREREQUISITE ${PREREQUISITE} NAME)
  272. string(FIND ${PREREQUISITE} "blosc" _HAS_DEP)
  273. if(NOT ${_HAS_DEP} EQUAL -1)
  274. set(OpenVDB_USES_BLOSC ON)
  275. endif()
  276. string(FIND ${PREREQUISITE} "log4cplus" _HAS_DEP)
  277. if(NOT ${_HAS_DEP} EQUAL -1)
  278. set(OpenVDB_USES_LOG4CPLUS ON)
  279. endif()
  280. string(FIND ${PREREQUISITE} "IlmImf" _HAS_DEP)
  281. if(NOT ${_HAS_DEP} EQUAL -1)
  282. set(OpenVDB_USES_ILM ON)
  283. endif()
  284. endforeach()
  285. unset(_OPENVDB_PREREQUISITE_LIST)
  286. unset(_HAS_DEP)
  287. if(OpenVDB_USES_BLOSC)
  288. find_package(Blosc )
  289. if(NOT Blosc_FOUND OR NOT TARGET Blosc::blosc)
  290. message(STATUS "find_package could not find Blosc. Using fallback blosc search...")
  291. find_path(Blosc_INCLUDE_DIR blosc.h)
  292. find_library(Blosc_LIBRARY NAMES blosc)
  293. if (Blosc_INCLUDE_DIR AND Blosc_LIBRARY)
  294. set(Blosc_FOUND TRUE)
  295. add_library(Blosc::blosc UNKNOWN IMPORTED)
  296. set_target_properties(Blosc::blosc PROPERTIES
  297. IMPORTED_LOCATION "${Blosc_LIBRARY}"
  298. INTERFACE_INCLUDE_DIRECTORIES ${Blosc_INCLUDE_DIR})
  299. elseif()
  300. message(FATAL_ERROR "Blosc library can not be found!")
  301. endif()
  302. endif()
  303. endif()
  304. if(OpenVDB_USES_LOG4CPLUS)
  305. find_package(Log4cplus REQUIRED)
  306. endif()
  307. if(OpenVDB_USES_ILM)
  308. find_package(IlmBase REQUIRED)
  309. endif()
  310. if(OpenVDB_USES_EXR)
  311. find_package(OpenEXR REQUIRED)
  312. endif()
  313. if(UNIX)
  314. find_package(Threads REQUIRED)
  315. endif()
  316. # Set deps. Note that the order here is important. If we're building against
  317. # Houdini 17.5 we must include OpenEXR and IlmBase deps first to ensure the
  318. # users chosen namespaced headers are correctly prioritized. Otherwise other
  319. # include paths from shared installs (including houdini) may pull in the wrong
  320. # headers
  321. set(_OPENVDB_VISIBLE_DEPENDENCIES
  322. Boost::iostreams
  323. Boost::system
  324. IlmBase::Half
  325. )
  326. set(_OPENVDB_DEFINITIONS)
  327. if(OpenVDB_ABI)
  328. list(APPEND _OPENVDB_DEFINITIONS "-DOPENVDB_ABI_VERSION_NUMBER=${OpenVDB_ABI}")
  329. endif()
  330. if(OpenVDB_USES_EXR)
  331. list(APPEND _OPENVDB_VISIBLE_DEPENDENCIES
  332. IlmBase::IlmThread
  333. IlmBase::Iex
  334. IlmBase::Imath
  335. OpenEXR::IlmImf
  336. )
  337. list(APPEND _OPENVDB_DEFINITIONS "-DOPENVDB_TOOLS_RAYTRACER_USE_EXR")
  338. endif()
  339. if(OpenVDB_USES_LOG4CPLUS)
  340. list(APPEND _OPENVDB_VISIBLE_DEPENDENCIES Log4cplus::log4cplus)
  341. list(APPEND _OPENVDB_DEFINITIONS "-DOPENVDB_USE_LOG4CPLUS")
  342. endif()
  343. list(APPEND _OPENVDB_VISIBLE_DEPENDENCIES
  344. TBB::tbb
  345. )
  346. if(UNIX)
  347. list(APPEND _OPENVDB_VISIBLE_DEPENDENCIES
  348. Threads::Threads
  349. )
  350. endif()
  351. set(_OPENVDB_HIDDEN_DEPENDENCIES)
  352. if(OpenVDB_USES_BLOSC)
  353. if(OPENVDB_USE_STATIC_LIBS)
  354. list(APPEND _OPENVDB_VISIBLE_DEPENDENCIES $<LINK_ONLY:Blosc::blosc>)
  355. else()
  356. list(APPEND _OPENVDB_HIDDEN_DEPENDENCIES Blosc::blosc)
  357. endif()
  358. endif()
  359. if(OPENVDB_USE_STATIC_LIBS)
  360. list(APPEND _OPENVDB_VISIBLE_DEPENDENCIES $<LINK_ONLY:ZLIB::ZLIB>)
  361. else()
  362. list(APPEND _OPENVDB_HIDDEN_DEPENDENCIES ZLIB::ZLIB)
  363. endif()
  364. # ------------------------------------------------------------------------
  365. # Configure imported target
  366. # ------------------------------------------------------------------------
  367. set(OpenVDB_LIBRARIES
  368. ${OpenVDB_LIB_COMPONENTS}
  369. )
  370. set(OpenVDB_INCLUDE_DIRS ${OpenVDB_INCLUDE_DIR})
  371. set(OpenVDB_DEFINITIONS)
  372. list(APPEND OpenVDB_DEFINITIONS "${PC_OpenVDB_CFLAGS_OTHER}")
  373. list(APPEND OpenVDB_DEFINITIONS "${_OPENVDB_DEFINITIONS}")
  374. list(REMOVE_DUPLICATES OpenVDB_DEFINITIONS)
  375. set(OpenVDB_LIBRARY_DIRS "")
  376. foreach(LIB ${OpenVDB_LIB_COMPONENTS})
  377. get_filename_component(_OPENVDB_LIBDIR ${LIB} DIRECTORY)
  378. list(APPEND OpenVDB_LIBRARY_DIRS ${_OPENVDB_LIBDIR})
  379. endforeach()
  380. list(REMOVE_DUPLICATES OpenVDB_LIBRARY_DIRS)
  381. foreach(COMPONENT ${OpenVDB_FIND_COMPONENTS})
  382. if(NOT TARGET OpenVDB::${COMPONENT})
  383. add_library(OpenVDB::${COMPONENT} UNKNOWN IMPORTED)
  384. set_target_properties(OpenVDB::${COMPONENT} PROPERTIES
  385. IMPORTED_LOCATION "${OpenVDB_${COMPONENT}_LIBRARY}"
  386. INTERFACE_COMPILE_OPTIONS "${OpenVDB_DEFINITIONS}"
  387. INTERFACE_INCLUDE_DIRECTORIES "${OpenVDB_INCLUDE_DIR}"
  388. IMPORTED_LINK_DEPENDENT_LIBRARIES "${_OPENVDB_HIDDEN_DEPENDENCIES}" # non visible deps
  389. INTERFACE_LINK_LIBRARIES "${_OPENVDB_VISIBLE_DEPENDENCIES}" # visible deps (headers)
  390. INTERFACE_COMPILE_FEATURES cxx_std_11
  391. )
  392. if (OPENVDB_USE_STATIC_LIBS)
  393. set_target_properties(OpenVDB::${COMPONENT} PROPERTIES
  394. INTERFACE_COMPILE_DEFINITIONS "OPENVDB_STATICLIB;OPENVDB_OPENEXR_STATICLIB"
  395. )
  396. endif()
  397. endif()
  398. endforeach()
  399. if(OpenVDB_FOUND AND NOT ${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  400. message(STATUS "OpenVDB libraries: ${OpenVDB_LIBRARIES}")
  401. endif()
  402. unset(_OPENVDB_DEFINITIONS)
  403. unset(_OPENVDB_VISIBLE_DEPENDENCIES)
  404. unset(_OPENVDB_HIDDEN_DEPENDENCIES)