PackageConfig.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Filename: PackageConfig.cmake
  2. #
  3. # This modules defines functions which find and configures libraries
  4. # and packages for Panda3Ds configuration file headers (.h files).
  5. #
  6. # Assumes the file has already been found with find_package().
  7. #
  8. # Function: package_option
  9. # Usage:
  10. # package_option(package_name DOCSTRING
  11. # [DEFAULT ON | OFF]
  12. # [LICENSE license])
  13. # Examples:
  14. # package_option(LIBNAME "Enables LIBNAME support." DEFAULT OFF)
  15. #
  16. # If no default is given, the default in normal
  17. # builds is to enable all found third-party packages.
  18. #
  19. #
  20. # Function: target_use_packages
  21. # Usage:
  22. # target_use_packages(target [PACKAGES ...])
  23. # Examples:
  24. # target_use_packages(mylib PYTHON PNG)
  25. #
  26. #
  27. # package_option
  28. #
  29. function(package_option name)
  30. # Parse the arguments.
  31. set(command)
  32. set(default)
  33. set(license "")
  34. set(cache_string)
  35. foreach(arg ${ARGN})
  36. if(command STREQUAL "DEFAULT")
  37. set(default "${arg}")
  38. set(command)
  39. elseif(command STREQUAL "LICENSE")
  40. set(license "${arg}")
  41. set(command)
  42. elseif(arg STREQUAL "DEFAULT")
  43. set(command "DEFAULT")
  44. elseif(arg STREQUAL "LICENSE")
  45. set(command "LICENSE")
  46. else()
  47. # Yes, a list, because semicolons can be in there, and
  48. # that gets split up into multiple args, so we have to
  49. # join it back together here.
  50. list(APPEND cache_string "${arg}")
  51. endif()
  52. endforeach()
  53. if(command STREQUAL "DEFAULT")
  54. message(SEND_ERROR "DEFAULT in package_option takes an argument")
  55. endif()
  56. # If the default is not set, we set it.
  57. if(NOT DEFINED default)
  58. if(IS_DIST_BUILD)
  59. # Accept things that don't have a configured license
  60. if(license STREQUAL "")
  61. set(default "${${name}_FOUND}")
  62. else()
  63. list(FIND PANDA_DIST_USE_LICENSES ${license} license_index)
  64. # If the license isn't in the accept listed, don't use the package
  65. message("INDEX for ${name}: ${license_index}")
  66. if(${license_index} EQUAL "-1")
  67. set(default OFF)
  68. else()
  69. set(default "${${name}_FOUND}")
  70. endif()
  71. endif()
  72. elseif(IS_MINSIZE_BUILD)
  73. set(default OFF)
  74. else()
  75. set(default "${${name}_FOUND}")
  76. endif()
  77. endif()
  78. # If it was set by the user but not found, display an error.
  79. if(HAVE_${name} AND NOT ${name}_FOUND)
  80. message(SEND_ERROR "NOT FOUND: ${name}. Disable HAVE_${name} to continue.")
  81. endif()
  82. # Prevent the function from being called twice.
  83. # This would indicate a cmake error.
  84. if(PANDA_DID_SET_OPTION_${name})
  85. message(SEND_ERROR "package_option(${name}) was called twice.
  86. This is a bug in the cmake build scripts.")
  87. else()
  88. set(PANDA_DID_SET_OPTION_${name} TRUE PARENT_SCOPE)
  89. endif()
  90. # Create the option.
  91. option("HAVE_${name}" "${cache_string}" "${default}")
  92. if(HAVE_${name})
  93. set(_${name}_LIBRARY ${${name}_LIBRARY} CACHE INTERNAL "<Internal>")
  94. set(_${name}_LIBRARIES ${${name}_LIBRARIES} CACHE INTERNAL "<Internal>")
  95. else()
  96. unset(_${name}_LIBRARY CACHE)
  97. unset(_${name}_LIBRARIES CACHE)
  98. endif()
  99. endfunction()
  100. #
  101. # target_use_packages
  102. #
  103. # Useful macro that picks up a package located using find_package
  104. # as dependencies of a target that is going to be built.
  105. #
  106. macro(target_use_packages target)
  107. set(libs ${ARGV})
  108. list(REMOVE_AT libs 0)
  109. foreach(lib ${libs})
  110. if(HAVE_${lib})
  111. target_include_directories("${target}" PUBLIC "${${lib}_INCLUDE_DIRS};${${lib}_INCLUDE_DIR}")
  112. if(${lib}_LIBRARIES)
  113. target_link_libraries("${target}" ${_${lib}_LIBRARIES})
  114. else()
  115. target_link_libraries("${target}" ${_${lib}_LIBRARY})
  116. endif()
  117. endif()
  118. endforeach(lib)
  119. endmacro(target_use_packages)