PackageConfig.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. # Filename: PackageConfig.cmake
  2. #
  3. # This module defines functions which find and configure libraries
  4. # and packages for Panda3D.
  5. #
  6. # Assumes an attempt to find the package has already been made with
  7. # find_package(). (i.e. relies on packagename_FOUND variable)
  8. #
  9. # The packages are added as imported/interface libraries in the PKG::
  10. # namespace. If the package is not found (or disabled by the user),
  11. # a dummy package will be created instead. Therefore, it is safe
  12. # to link against the PKG::PACKAGENAME target unconditionally.
  13. #
  14. # Function: package_option
  15. # Usage:
  16. # package_option(package_name package_doc_string
  17. # [DEFAULT ON | OFF]
  18. # [IMPORTED_AS CMake::Imported::Target [...]]
  19. # [FOUND_AS find_name]
  20. # [LICENSE license])
  21. #
  22. # Examples:
  23. # package_option(LIBNAME "Enables LIBNAME support." DEFAULT OFF)
  24. #
  25. # If no default is given, the default in normal
  26. # builds is to enable all found third-party packages.
  27. # In builds for redistribution, there is the additional requirement that
  28. # the package be suitably-licensed.
  29. #
  30. # FOUND_AS indicates the name of the CMake find_package() module, which
  31. # may differ from Panda3D's internal name for that package.
  32. #
  33. # IMPORTED_AS is used to indicate that the find_package() may have
  34. # provided one or more IMPORTED targets, and that if at least one is
  35. # found, the IMPORTED target(s) should be used instead of the
  36. # variables provided by find_package()
  37. #
  38. #
  39. # Function: package_status
  40. # Usage:
  41. # package_status(package_name "Package description" ["Config summary"])
  42. #
  43. # Examples:
  44. # package_status(OpenAL "OpenAL Audio Output")
  45. # package_status(ROCKET "Rocket" "without Python bindings")
  46. #
  47. #
  48. # Function: show_packages
  49. # Usage:
  50. # show_packages()
  51. #
  52. # This prints the package usage report using the information provided in
  53. # calls to package_status above.
  54. #
  55. set(_ALL_PACKAGE_OPTIONS CACHE INTERNAL "Internal variable")
  56. #
  57. # package_option
  58. #
  59. # In order to make sure no third-party licenses are inadvertently violated,
  60. # this imposes a few rules regarding license:
  61. # 1) If there is no license, no special restrictions.
  62. # 2) If there is a license, but the build is not flagged for redistribution,
  63. # no special restrictions.
  64. # 3) If there is a license, and this is for redistribution, the package is
  65. # forcibly defaulted off and must be explicitly enabled, unless the license
  66. # matches a list of licenses suitable for redistribution.
  67. #
  68. function(package_option name)
  69. # Parse the arguments.
  70. set(command)
  71. set(default)
  72. set(found_as "${name}")
  73. set(imported_as)
  74. set(license "")
  75. set(cache_string)
  76. string(TOUPPER "${name}" name)
  77. foreach(arg ${ARGN})
  78. if(command STREQUAL "DEFAULT")
  79. set(default "${arg}")
  80. set(command)
  81. elseif(command STREQUAL "FOUND_AS")
  82. set(found_as "${arg}")
  83. set(command)
  84. elseif(command STREQUAL "LICENSE")
  85. set(license "${arg}")
  86. set(command)
  87. elseif(arg STREQUAL "DEFAULT")
  88. set(command "DEFAULT")
  89. elseif(arg STREQUAL "FOUND_AS")
  90. set(command "FOUND_AS")
  91. elseif(arg STREQUAL "LICENSE")
  92. set(command "LICENSE")
  93. elseif(arg STREQUAL "IMPORTED_AS")
  94. set(command "IMPORTED_AS")
  95. elseif(command STREQUAL "IMPORTED_AS")
  96. list(APPEND imported_as "${arg}")
  97. else()
  98. # Yes, a list, because semicolons can be in there, and
  99. # that gets split up into multiple args, so we have to
  100. # join it back together here.
  101. list(APPEND cache_string "${arg}")
  102. endif()
  103. endforeach()
  104. if(command AND NOT command STREQUAL "IMPORTED_AS")
  105. message(SEND_ERROR "${command} in package_option takes an argument")
  106. endif()
  107. # If the default is not set, we set it.
  108. if(NOT DEFINED default)
  109. if(IS_DIST_BUILD)
  110. # Accept things that don't have a configured license
  111. if(license STREQUAL "")
  112. set(default "${${found_as}_FOUND}")
  113. else()
  114. list(FIND PANDA_DIST_USE_LICENSES ${license} license_index)
  115. # If the license isn't in the accept listed, don't use the package
  116. if(${license_index} EQUAL "-1")
  117. set(default OFF)
  118. else()
  119. set(default "${${found_as}_FOUND}")
  120. endif()
  121. endif()
  122. else()
  123. set(default "${${found_as}_FOUND}")
  124. endif()
  125. endif()
  126. option("HAVE_${name}" "${cache_string}" "${default}")
  127. # If it was set by the user but not found, display an error.
  128. string(TOUPPER "${found_as}" FOUND_AS)
  129. if(HAVE_${name} AND NOT ${found_as}_FOUND AND NOT ${FOUND_AS}_FOUND)
  130. message(SEND_ERROR "NOT FOUND: ${name}. Disable HAVE_${name} to continue.")
  131. endif()
  132. # Prevent the function from being called twice.
  133. # This would indicate a cmake error.
  134. if(";${_ALL_PACKAGE_OPTIONS};" MATCHES ";${name};")
  135. message(SEND_ERROR "package_option(${name}) was called twice.
  136. This is a bug in the cmake build scripts.")
  137. else()
  138. list(APPEND _ALL_PACKAGE_OPTIONS "${name}")
  139. set(_ALL_PACKAGE_OPTIONS "${_ALL_PACKAGE_OPTIONS}" CACHE INTERNAL "Internal variable")
  140. endif()
  141. set(PANDA_PACKAGE_DEFAULT_${name} "${default}" PARENT_SCOPE)
  142. if(${found_as}_FOUND OR ${FOUND_AS}_FOUND)
  143. set(PANDA_PACKAGE_FOUND_${name} ON PARENT_SCOPE)
  144. else()
  145. set(PANDA_PACKAGE_FOUND_${name} OFF PARENT_SCOPE)
  146. endif()
  147. # Create the INTERFACE library used to depend on this package.
  148. add_library(PKG::${name} INTERFACE IMPORTED GLOBAL)
  149. # Explicitly record the package's include directories as system include
  150. # directories. CMake does do this automatically for INTERFACE libraries, but
  151. # it does it by discovering all transitive links first, then reading
  152. # INTERFACE_INCLUDE_DIRECTORIES for those which are INTERFACE libraries. So,
  153. # this would be broken for the metalib system (pre CMake 3.12) which doesn't
  154. # "link" the object libraries.
  155. if(CMAKE_VERSION VERSION_LESS "3.12")
  156. set_target_properties(PKG::${name} PROPERTIES
  157. INTERFACE_SYSTEM_INCLUDE_DIRECTORIES
  158. "$<TARGET_PROPERTY:PKG::${name},INTERFACE_INCLUDE_DIRECTORIES>")
  159. endif()
  160. # If the option actually is enabled, populate the INTERFACE library created above
  161. if(HAVE_${name})
  162. set(use_variables ON)
  163. # This is gross, but we actually want to hide package include directories
  164. # from Interrogate to make sure it relies on parser-inc instead, so we'll
  165. # use some generator expressions to do that.
  166. set(_is_not_interface_lib
  167. "$<NOT:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,INTERFACE_LIBRARY>>")
  168. set(_is_not_interrogate
  169. "$<NOT:$<BOOL:$<${_is_not_interface_lib}:$<TARGET_PROPERTY:IS_INTERROGATE>>>>")
  170. foreach(implib ${imported_as})
  171. if(TARGET ${implib})
  172. # We found one of the implibs, so we don't need to use variables
  173. # (below) anymore
  174. set(use_variables OFF)
  175. # Hide it from Interrogate
  176. target_link_libraries(PKG::${name} INTERFACE
  177. "$<${_is_not_interrogate}:$<TARGET_NAME:${implib}>>")
  178. endif()
  179. endforeach(implib)
  180. if(use_variables)
  181. if(DEFINED ${found_as}_INCLUDE_DIRS)
  182. set(includes ${${found_as}_INCLUDE_DIRS})
  183. elseif(DEFINED ${found_as}_INCLUDE_DIR)
  184. set(includes "${${found_as}_INCLUDE_DIR}")
  185. elseif(DEFINED ${FOUND_AS}_INCLUDE_DIRS)
  186. set(includes ${${FOUND_AS}_INCLUDE_DIRS})
  187. else()
  188. set(includes "${${FOUND_AS}_INCLUDE_DIR}")
  189. endif()
  190. if(DEFINED ${found_as}_LIBRARIES)
  191. set(libs ${${found_as}_LIBRARIES})
  192. elseif(DEFINED ${found_as}_LIBRARY)
  193. set(libs "${${found_as}_LIBRARY}")
  194. elseif(DEFINED ${FOUND_AS}_LIBRARIES)
  195. set(libs ${${FOUND_AS}_LIBRARIES})
  196. else()
  197. set(libs "${${FOUND_AS}_LIBRARY}")
  198. endif()
  199. target_link_libraries(PKG::${name} INTERFACE ${libs})
  200. # Hide it from Interrogate
  201. set_target_properties(PKG::${name} PROPERTIES
  202. INTERFACE_INCLUDE_DIRECTORIES "$<${_is_not_interrogate}:${includes}>")
  203. endif()
  204. endif()
  205. endfunction(package_option)
  206. set(_ALL_CONFIG_PACKAGES CACHE INTERNAL "Internal variable")
  207. #
  208. # package_status
  209. #
  210. function(package_status name desc)
  211. set(note "")
  212. foreach(arg ${ARGN})
  213. set(note "${arg}")
  214. endforeach()
  215. string(TOUPPER "${name}" name)
  216. if(NOT ";${_ALL_PACKAGE_OPTIONS};" MATCHES ";${name};")
  217. message(SEND_ERROR "package_status(${name}) was called before package_option(${name}).
  218. This is a bug in the cmake build scripts.")
  219. return()
  220. endif()
  221. if(";${_ALL_CONFIG_PACKAGES};" MATCHES ";${name};")
  222. message(SEND_ERROR "package_status(${name}) was called twice.
  223. This is a bug in the cmake build scripts.")
  224. else()
  225. list(APPEND _ALL_CONFIG_PACKAGES "${name}")
  226. set(_ALL_CONFIG_PACKAGES "${_ALL_CONFIG_PACKAGES}" CACHE INTERNAL "Internal variable")
  227. endif()
  228. set(PANDA_PACKAGE_DESC_${name} "${desc}" PARENT_SCOPE)
  229. set(PANDA_PACKAGE_NOTE_${name} "${note}" PARENT_SCOPE)
  230. endfunction()
  231. #
  232. # show_packages
  233. #
  234. function(show_packages)
  235. message("")
  236. message("Configuring support for the following optional third-party packages:")
  237. foreach(package ${_ALL_CONFIG_PACKAGES})
  238. set(desc "${PANDA_PACKAGE_DESC_${package}}")
  239. set(note "${PANDA_PACKAGE_NOTE_${package}}")
  240. if(HAVE_${package} AND PANDA_PACKAGE_FOUND_${package})
  241. if(NOT note STREQUAL "")
  242. message("+ ${desc} (${note})")
  243. else()
  244. message("+ ${desc}")
  245. endif()
  246. elseif(HAVE_${package})
  247. message("! ${desc} (enabled but not found)")
  248. else()
  249. if(NOT PANDA_PACKAGE_FOUND_${package})
  250. set(reason "not found")
  251. elseif(NOT PANDA_PACKAGE_DEFAULT_${package})
  252. set(reason "not requested")
  253. else()
  254. set(reason "disabled")
  255. endif()
  256. message("- ${desc} (${reason})")
  257. endif()
  258. endforeach()
  259. endfunction()
  260. #
  261. # export_packages(filename)
  262. #
  263. # Generates an includable CMake file that contains definitions for every PKG::
  264. # package defined.
  265. #
  266. function(export_packages filename)
  267. set(exports "# Exports for Panda3D PKG:: packages\n")
  268. foreach(pkg ${_ALL_PACKAGE_OPTIONS})
  269. set(exports "${exports}\n# Create imported target PKG::${pkg}\n")
  270. set(exports "${exports}add_library(PKG::${pkg} INTERFACE IMPORTED)\n\n")
  271. set(exports "${exports}set_target_properties(PKG::${pkg} PROPERTIES\n")
  272. foreach(prop
  273. INTERFACE_COMPILE_DEFINITIONS
  274. INTERFACE_COMPILE_FEATURES
  275. INTERFACE_COMPILE_OPTIONS
  276. INTERFACE_INCLUDE_DIRECTORIES
  277. INTERFACE_LINK_DEPENDS
  278. INTERFACE_LINK_DIRECTORIES
  279. INTERFACE_LINK_OPTIONS
  280. INTERFACE_POSITION_INDEPENDENT_CODE
  281. #INTERFACE_SYSTEM_INCLUDE_DIRECTORIES # Let the consumer dictate this
  282. INTERFACE_SOURCES)
  283. set(prop_ex "$<TARGET_PROPERTY:PKG::${pkg},${prop}>")
  284. set(exports "${exports}$<$<BOOL:${prop_ex}>: ${prop} \"${prop_ex}\"\n>")
  285. endforeach(prop)
  286. # Ugh, INTERFACE_LINK_LIBRARIES isn't transitive. Fine. Take care of it
  287. # by hand:
  288. set(libraries)
  289. set(stack "PKG::${pkg}")
  290. set(history)
  291. while(stack)
  292. # Remove head item from stack
  293. unset(head)
  294. while(NOT DEFINED head)
  295. if(NOT stack)
  296. break()
  297. endif()
  298. list(GET stack 0 head)
  299. list(REMOVE_AT stack 0)
  300. # Don't visit anything twice
  301. list(FIND history "${head}" _index)
  302. if(_index GREATER -1)
  303. unset(head)
  304. endif()
  305. endwhile()
  306. if(head)
  307. list(APPEND history "${head}")
  308. else()
  309. break()
  310. endif()
  311. # If head isn't a target, add it to `libraries`, else recurse
  312. if(TARGET "${head}")
  313. get_target_property(link_libs "${head}" INTERFACE_LINK_LIBRARIES)
  314. if(link_libs)
  315. list(APPEND stack ${link_libs})
  316. endif()
  317. get_target_property(type "${head}" TYPE)
  318. if(NOT type STREQUAL "INTERFACE_LIBRARY")
  319. get_target_property(imported_location "${head}" IMPORTED_LOCATION)
  320. get_target_property(imported_implib "${head}" IMPORTED_IMPLIB)
  321. if(imported_implib)
  322. list(APPEND libraries ${imported_implib})
  323. elseif(imported_location)
  324. list(APPEND libraries ${imported_location})
  325. endif()
  326. get_target_property(configs "${head}" IMPORTED_CONFIGURATIONS)
  327. if(configs AND NOT imported_location)
  328. foreach(config ${configs})
  329. get_target_property(imported_location "${head}" IMPORTED_LOCATION_${config})
  330. # Prefer IMPORTED_IMPLIB where present
  331. get_target_property(imported_implib "${head}" IMPORTED_IMPLIB_${config})
  332. if(imported_implib)
  333. set(imported_location "${imported_implib}")
  334. endif()
  335. if(imported_location)
  336. if(configs MATCHES ".*;.*")
  337. set(_bling "$<1:$>") # genex-escaped $
  338. list(APPEND libraries "${_bling}<${_bling}<CONFIG:${config}>:${imported_location}>")
  339. else()
  340. list(APPEND libraries ${imported_location})
  341. endif()
  342. endif()
  343. endforeach(config)
  344. endif()
  345. elseif(CMAKE_VERSION VERSION_GREATER "3.8")
  346. # This is an INTERFACE_LIBRARY, and CMake is new enough to support
  347. # IMPORTED_IMPLIB
  348. get_target_property(imported_libname "${head}" IMPORTED_LIBNAME)
  349. if(imported_libname)
  350. list(APPEND libraries ${imported_libname})
  351. endif()
  352. endif()
  353. elseif("${head}" MATCHES "\\$<TARGET_NAME:\([^>]+\)>")
  354. string(REGEX REPLACE ".*\\$<TARGET_NAME:\([^>]+\)>.*" "\\1" match "${head}")
  355. list(APPEND stack "${match}")
  356. else()
  357. list(APPEND libraries "${head}")
  358. endif()
  359. endwhile(stack)
  360. set(exports "${exports} INTERFACE_LINK_LIBRARIES \"${libraries}\"\n")
  361. set(exports "${exports})\n")
  362. endforeach(pkg)
  363. # file(GENERATE) does not like $<LINK_ONLY:...> (and it's meant to be
  364. # consumed by our importer) so we escape it
  365. set(_bling "$<1:$>") # genex-escaped $
  366. string(REPLACE "$<LINK_ONLY:" "${_bling}<LINK_ONLY:" exports "${exports}")
  367. file(GENERATE OUTPUT "${filename}" CONTENT "${exports}")
  368. endfunction(export_packages)
  369. #
  370. # export_targets(set [NAMESPACE namespace] [COMPONENT component])
  371. #
  372. # Export targets in the export set named by "set"
  373. #
  374. # NAMESPACE overrides the namespace prefixed to the exported targets; it
  375. # defaults to "Panda3D::[set]::" if no explicit override is given.
  376. #
  377. # COMPONENT overrides the install component for the generated .cmake file; it
  378. # defaults to "[set]" if no explicit override is given.
  379. #
  380. function(export_targets set)
  381. set(namespace "Panda3D::${set}::")
  382. set(component "${set}")
  383. set(keyword)
  384. foreach(arg ${ARGN})
  385. if(arg STREQUAL "NAMESPACE" OR
  386. arg STREQUAL "COMPONENT")
  387. set(keyword "${arg}")
  388. elseif(keyword STREQUAL "NAMESPACE")
  389. set(namespace "${arg}")
  390. elseif(keyword STREQUAL "COMPONENT")
  391. set(component "${arg}")
  392. else()
  393. message(FATAL_ERROR "export_targets() given unexpected arg: ${arg}")
  394. endif()
  395. endforeach(arg)
  396. export(EXPORT "${set}" NAMESPACE "${namespace}"
  397. FILE "${PROJECT_BINARY_DIR}/Panda3D${set}Targets.cmake")
  398. install(EXPORT "${set}" NAMESPACE "${namespace}"
  399. FILE "Panda3D${set}Targets.cmake"
  400. COMPONENT "${component}" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Panda3D)
  401. endfunction(export_targets)
  402. #
  403. # find_package
  404. #
  405. # This override implements CMAKE_FIND_PACKAGE_PREFER_CONFIG on versions of
  406. # CMake too old to include it.
  407. #
  408. if(CMAKE_VERSION VERSION_LESS "3.15")
  409. macro(find_package name)
  410. if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);")
  411. # Caller explicitly asking for a certain mode; so be it.
  412. _find_package(${ARGV})
  413. elseif(CMAKE_FIND_PACKAGE_PREFER_CONFIG)
  414. # Try CONFIG
  415. _find_package("${name}" CONFIG ${ARGN})
  416. if(NOT ${name}_FOUND)
  417. # CONFIG didn't work, fall back to MODULE
  418. _find_package("${name}" MODULE ${ARGN})
  419. endif()
  420. else()
  421. # Default behavior
  422. _find_package(${ARGV})
  423. endif()
  424. endmacro(find_package)
  425. endif()