瀏覽代碼

CMake: Remove target_use_packages

Instead, let's use a PKG::PKGNAME interface library, which simplifies
the linking and also allows us to use imported libraries from
find_package in the future.
Sam Edwards 7 年之前
父節點
當前提交
1520d712d4

+ 1 - 1
CMakeLists.txt

@@ -73,7 +73,7 @@ if(WIN32)
 endif()
 
 # Include global modules needed for configure scripts
-include(PackageConfig)      # Defines package_option AND target_use_packages
+include(PackageConfig)      # Defines package_option
 
 # Configure Panda3D
 include(dtool/PandaVersion.cmake)

+ 1 - 1
cmake/macros/BuildMetalib.cmake

@@ -22,7 +22,7 @@ function(target_link_libraries target)
   foreach(library ${ARGN})
     # This is a quick and dirty regex to tell targets apart from other stuff.
     # It just checks if it's alphanumeric and starts with p3/panda.
-    if(library MATCHES "^(p3|panda)[A-Za-z0-9]*$")
+    if(library MATCHES "^(PKG::|p3|panda)[A-Za-z0-9]*$")
       # We need to add "library"'s include directories to "target"
       # (and transitively to INTERFACE_INCLUDE_DIRECTORIES so further
       # dependencies will work)

+ 1 - 1
cmake/macros/Interrogate.cmake

@@ -317,7 +317,7 @@ function(add_python_target target)
   set(sources ${ARGN})
 
   add_library(${target} ${MODULE_TYPE} ${sources})
-  target_use_packages(${target} PYTHON)
+  target_link_libraries(${target} PKG::PYTHON)
 
   if(BUILD_SHARED_LIBS)
     set_target_properties(${target} PROPERTIES

+ 25 - 39
cmake/macros/PackageConfig.cmake

@@ -6,6 +6,11 @@
 # Assumes an attempt to find the package has already been made with
 # find_package(). (i.e. relies on packagename_FOUND variable)
 #
+# The packages are added as imported/interface libraries in the PKG::
+# namespace.  If the package is not found (or disabled by the user),
+# a dummy package will be created instead.  Therefore, it is safe
+# to link against the PKG::PACKAGENAME target unconditionally.
+#
 # Function: package_option
 # Usage:
 #   package_option(package_name package_doc_string
@@ -39,13 +44,6 @@
 #   This prints the package usage report using the information provided in
 #   calls to config_package above.
 #
-#
-# Function: target_use_packages
-# Usage:
-#   target_use_packages(target [PACKAGES ...])
-# Examples:
-#   target_use_packages(mylib PYTHON PNG)
-#
 
 #
 # package_option
@@ -112,7 +110,6 @@ function(package_option name)
       else()
         list(FIND PANDA_DIST_USE_LICENSES ${license} license_index)
         # If the license isn't in the accept listed, don't use the package
-        message("INDEX for ${name}: ${license_index}")
         if(${license_index} EQUAL "-1")
           set(default OFF)
         else()
@@ -147,19 +144,31 @@ function(package_option name)
 
   # Create the option.
   option("HAVE_${name}" "${cache_string}" "${default}")
+
+  # Create the library if the package is available.
+  add_library(PKG::${name} INTERFACE IMPORTED GLOBAL)
+
   if(HAVE_${name})
-    set(_PKG_${name}_INCLUDES ${${found_as}_INCLUDE_DIRS} ${${found_as}_INCLUDE_DIR}
-      CACHE INTERNAL "<Internal>")
+    if(${found_as}_INCLUDE_DIRS)
+      set(includes ${${found_as}_INCLUDE_DIRS})
+    else()
+      set(includes "${${found_as}_INCLUDE_DIR}")
+    endif()
     if(${found_as}_LIBRARIES)
-      set(_PKG_${name}_LIBRARIES ${${found_as}_LIBRARIES} CACHE INTERNAL "<Internal>")
+      set(libs ${${found_as}_LIBRARIES})
     else()
-      set(_PKG_${name}_LIBRARIES "${${found_as}_LIBRARY}" CACHE INTERNAL "<Internal>")
+      set(libs "${${found_as}_LIBRARY}")
     endif()
-  else()
-    unset(_PKG_${name}_INCLUDES CACHE)
-    unset(_PKG_${name}_LIBRARIES CACHE)
+
+    target_link_libraries(PKG::${name} INTERFACE ${libs})
+
+    # This is gross, but we actually want to hide package include directories
+    # from Interrogate to make sure it relies on parser-inc instead, so we'll
+    # use some generator expressions to do that.
+    set_target_properties(PKG::${name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
+      "$<$<NOT:$<BOOL:$<TARGET_PROPERTY:IS_INTERROGATE>>>:${includes}>")
   endif()
-endfunction()
+endfunction(package_option)
 
 set(_ALL_CONFIG_PACKAGES CACHE INTERNAL "Internal variable")
 
@@ -218,26 +227,3 @@ function(show_packages)
     endif()
   endforeach()
 endfunction()
-
-#
-# target_use_packages
-#
-# Useful macro that picks up a package located using find_package
-# as dependencies of a target that is going to be built.
-#
-macro(target_use_packages target)
-  set(libs ${ARGV})
-  list(REMOVE_AT libs 0)
-
-  foreach(lib ${libs})
-    if(HAVE_${lib})
-      target_link_libraries("${target}" ${_PKG_${lib}_LIBRARIES})
-
-      # This is gross, but we actually want to hide package include directories
-      # from Interrogate to make sure it relies on parser-inc instead, so we'll
-      # use some generator expressions to do that.
-      target_include_directories("${target}" PUBLIC
-        $<$<NOT:$<BOOL:$<TARGET_PROPERTY:IS_INTERROGATE>>>:${_PKG_${lib}_INCLUDES}>)
-    endif()
-  endforeach(lib)
-endmacro(target_use_packages)

+ 1 - 2
direct/src/dcparser/CMakeLists.txt

@@ -49,8 +49,7 @@ composite_sources(p3dcparser P3DCPARSER_SOURCES)
 add_component_library(p3dcparser NOINIT SYMBOL BUILDING_DIRECT_DCPARSER
   ${P3DCPARSER_HEADERS} ${P3DCPARSER_SOURCES} ${P3DCPARSER_PARSER_SOURCES})
 target_compile_definitions(p3dcparser PUBLIC WITHIN_PANDA)
-target_link_libraries(p3dcparser p3directbase panda)
-target_use_packages(p3dcparser PYTHON)
+target_link_libraries(p3dcparser p3directbase panda PKG::PYTHON)
 target_interrogate(p3dcparser ${P3DCPARSER_HEADERS} ${P3DCPARSER_SOURCES})
 
 if(NOT BUILD_METALIBS)

+ 1 - 2
direct/src/distributed/CMakeLists.txt

@@ -17,8 +17,7 @@ set(P3DISTRIBUTED_SOURCES
 add_component_library(p3distributed SYMBOL BUILDING_DIRECT_DISTRIBUTED
   ${P3DISTRIBUTED_HEADERS} ${P3DISTRIBUTED_SOURCES})
 target_compile_definitions(p3distributed PUBLIC WITHIN_PANDA)
-target_link_libraries(p3distributed p3directbase p3dcparser panda)
-target_use_packages(p3distributed PYTHON)
+target_link_libraries(p3distributed p3directbase p3dcparser panda PKG::PYTHON)
 target_interrogate(p3distributed ALL)
 
 if(NOT BUILD_METALIBS)

+ 1 - 1
dtool/src/dtoolbase/CMakeLists.txt

@@ -89,7 +89,7 @@ add_component_library(p3dtoolbase NOINIT SYMBOL BUILDING_DTOOL_DTOOLBASE
 target_include_directories(p3dtoolbase PUBLIC
   ${CMAKE_CURRENT_BINARY_DIR}
   ${PROJECT_BINARY_DIR}/include/${CMAKE_CFG_INTDIR})
-target_use_packages(p3dtoolbase THREADS EIGEN)
+target_link_libraries(p3dtoolbase PKG::EIGEN)
 target_interrogate(p3dtoolbase ${P3DTOOLBASE_SOURCES} EXTENSIONS ${P3DTOOLBASE_IGATEEXT})
 
 if(NOT BUILD_METALIBS)

+ 4 - 6
dtool/src/interrogate/CMakeLists.txt

@@ -52,14 +52,12 @@ set(INTERROGATE_SOURCES
 composite_sources(interrogate INTERROGATE_SOURCES)
 
 add_executable(interrogate ${INTERROGATE_HEADERS} ${INTERROGATE_SOURCES})
-target_link_libraries(interrogate
-  p3cppParser p3dtoolconfig p3pystub)
-target_use_packages(interrogate OPENSSL)
+target_link_libraries(interrogate p3cppParser p3dtoolconfig p3pystub
+  PKG::OPENSSL)
 
 add_executable(interrogate_module interrogate_module.cxx)
-target_link_libraries(interrogate_module
-  p3cppParser p3dtoolconfig p3pystub)
-target_use_packages(interrogate_module OPENSSL)
+target_link_libraries(interrogate_module p3cppParser p3dtoolconfig p3pystub
+  PKG::OPENSSL)
 
 if(NOT CMAKE_CROSSCOMPILING)
   add_executable(host_interrogate ALIAS interrogate)

+ 1 - 2
dtool/src/interrogatedb/CMakeLists.txt

@@ -61,8 +61,7 @@ add_library(p3igateruntime
   ${P3IGATERUNTIME_HEADERS} ${P3IGATERUNTIME_SOURCES})
 set_target_properties(p3igateruntime PROPERTIES
   DEFINE_SYMBOL BUILDING_INTERROGATEDB) # HACK
-target_link_libraries(p3igateruntime p3dtoolconfig)
-target_use_packages(p3igateruntime PYTHON)
+target_link_libraries(p3igateruntime p3dtoolconfig PKG::PYTHON)
 
 install(TARGETS p3igateruntime DESTINATION lib RUNTIME DESTINATION bin)
 install(FILES ${P3IGATERUNTIME_HEADERS} DESTINATION include/panda3d)

+ 1 - 2
dtool/src/prc/CMakeLists.txt

@@ -73,8 +73,7 @@ composite_sources(p3prc P3PRC_SOURCES)
 add_component_library(p3prc NOINIT SYMBOL BUILDING_DTOOL_PRC
   ${P3PRC_HEADERS} ${P3PRC_SOURCES})
 target_include_directories(p3prc PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
-target_link_libraries(p3prc p3dtool)
-target_use_packages(p3prc OPENSSL)
+target_link_libraries(p3prc p3dtool PKG::OPENSSL)
 target_interrogate(p3prc ALL EXTENSIONS ${P3PRC_IGATEEXT})
 
 if(NOT BUILD_METALIBS)

+ 3 - 6
panda/src/audiotraits/CMakeLists.txt

@@ -23,8 +23,7 @@ if(HAVE_RAD_MSS)
   composite_sources(p3miles_audio P3MILES_SOURCES)
   add_library(p3miles_audio ${P3MILES_HEADERS} ${P3MILES_SOURCES})
   set_target_properties(p3miles_audio PROPERTIES DEFINE_SYMBOL BUILDING_MILES_AUDIO)
-  target_link_libraries(p3miles_audio panda)
-  target_use_packages(p3miles_audio MILES)
+  target_link_libraries(p3miles_audio panda PKG::MILES)
 
   install(TARGETS p3miles_audio DESTINATION lib RUNTIME DESTINATION bin)
 endif()
@@ -41,8 +40,7 @@ if(HAVE_FMODEX)
   composite_sources(p3fmod_audio P3FMOD_SOURCES)
   add_library(p3fmod_audio ${P3FMOD_HEADERS} ${P3FMOD_SOURCES})
   set_target_properties(p3fmod_audio PROPERTIES DEFINE_SYMBOL BUILDING_FMOD_AUDIO)
-  target_link_libraries(p3fmod_audio panda)
-  target_use_packages(p3fmod_audio FMODEX)
+  target_link_libraries(p3fmod_audio panda PKG::FMODEX)
 
   install(TARGETS p3fmod_audio DESTINATION lib RUNTIME DESTINATION bin)
 endif()
@@ -60,8 +58,7 @@ if(HAVE_OPENAL)
   composite_sources(p3openal_audio P3OPENAL_SOURCES)
   add_library(p3openal_audio ${P3OPENAL_HEADERS} ${P3OPENAL_SOURCES})
   set_target_properties(p3openal_audio PROPERTIES DEFINE_SYMBOL BUILDING_OPENAL_AUDIO)
-  target_link_libraries(p3openal_audio panda)
-  target_use_packages(p3openal_audio OPENAL)
+  target_link_libraries(p3openal_audio panda PKG::OPENAL)
 
   install(TARGETS p3openal_audio DESTINATION lib RUNTIME DESTINATION bin)
 endif()

+ 1 - 2
panda/src/bullet/CMakeLists.txt

@@ -109,8 +109,7 @@ set(P3BULLET_SOURCES
 composite_sources(p3bullet P3BULLET_SOURCES)
 add_library(p3bullet ${P3BULLET_SOURCES} ${P3BULLET_HEADERS})
 set_target_properties(p3bullet PROPERTIES DEFINE_SYMBOL BUILDING_PANDABULLET)
-target_link_libraries(p3bullet panda)
-target_use_packages(p3bullet BULLET)
+target_link_libraries(p3bullet panda PKG::BULLET)
 target_interrogate(p3bullet ALL)
 
 install(TARGETS p3bullet DESTINATION lib RUNTIME DESTINATION bin)

+ 2 - 2
panda/src/express/CMakeLists.txt

@@ -135,8 +135,8 @@ composite_sources(p3express P3EXPRESS_SOURCES)
 add_component_library(p3express SYMBOL BUILDING_PANDA_EXPRESS
   ${P3EXPRESS_SOURCES} ${P3EXPRESS_HEADERS})
 
-target_link_libraries(p3express p3pandabase p3dtoolconfig p3dtool)
-target_use_packages(p3express TAR ZLIB)
+target_link_libraries(p3express p3pandabase p3dtoolconfig p3dtool
+  PKG::TAR PKG::ZLIB)
 target_interrogate(p3express ALL EXTENSIONS ${P3EXPRESS_IGATEEXT})
 
 if(WIN32)

+ 2 - 2
panda/src/ffmpeg/CMakeLists.txt

@@ -22,8 +22,8 @@ set(P3FFMPEG_SOURCES
 composite_sources(p3ffmpeg P3FFMPEG_SOURCES)
 add_library(p3ffmpeg ${P3FFMPEG_HEADERS} ${P3FFMPEG_SOURCES})
 set_target_properties(p3ffmpeg PROPERTIES DEFINE_SYMBOL BUILDING_FFMPEG)
-target_link_libraries(p3ffmpeg panda)
-target_use_packages(p3ffmpeg FFMPEG SWSCALE SWRESAMPLE)
+target_link_libraries(p3ffmpeg panda
+  PKG::FFMPEG PKG::SWSCALE PKG::SWRESAMPLE)
 
 install(TARGETS p3ffmpeg DESTINATION lib RUNTIME DESTINATION bin)
 install(FILES ${P3FFMPEG_HEADERS} DESTINATION include/panda3d)

+ 1 - 2
panda/src/glgsg/CMakeLists.txt

@@ -15,8 +15,7 @@ composite_sources(p3glgsg P3GLGSG_SOURCES)
 add_component_library(p3glgsg SYMBOL BUILDING_PANDA_GLGSG
   ${P3GLGSG_HEADERS} ${P3GLGSG_SOURCES})
 target_link_libraries(p3glgsg p3glstuff panda
-  ${OPENGL_LIBRARIES})
-target_use_packages(p3glgsg CG)
+  PKG::CG PKG::GL)
 
 if(NOT BUILD_METALIBS)
   install(TARGETS p3glgsg DESTINATION lib RUNTIME DESTINATION bin)

+ 2 - 2
panda/src/gobj/CMakeLists.txt

@@ -170,8 +170,8 @@ set(P3GOBJ_IGATEEXT
 composite_sources(p3gobj P3GOBJ_SOURCES)
 add_component_library(p3gobj NOINIT SYMBOL BUILDING_PANDA_GOBJ
   ${P3GOBJ_HEADERS} ${P3GOBJ_SOURCES})
-target_link_libraries(p3gobj p3gsgbase p3pnmimage)
-target_use_packages(p3gobj ZLIB SQUISH CG)
+target_link_libraries(p3gobj p3gsgbase p3pnmimage
+  PKG::ZLIB PKG::SQUISH PKG::CG)
 target_interrogate(p3gobj ALL EXTENSIONS ${P3GOBJ_IGATEEXT})
 
 if(NOT BUILD_METALIBS)

+ 1 - 2
panda/src/linmath/CMakeLists.txt

@@ -46,8 +46,7 @@ set(P3LINMATH_SOURCES
 composite_sources(p3linmath P3LINMATH_SOURCES)
 add_component_library(p3linmath SYMBOL BUILDING_PANDA_LINMATH
   ${P3LINMATH_HEADERS} ${P3LINMATH_SOURCES})
-target_link_libraries(p3linmath p3pandabase pandaexpress)
-target_use_packages(p3linmath EIGEN)
+target_link_libraries(p3linmath p3pandabase pandaexpress PKG::EIGEN)
 target_interrogate(p3linmath ALL)
 
 if(NOT BUILD_METALIBS)

+ 1 - 2
panda/src/mathutil/CMakeLists.txt

@@ -63,8 +63,7 @@ set(P3MATHUTIL_SOURCES
 composite_sources(p3mathutil P3MATHUTIL_SOURCES)
 add_component_library(p3mathutil SYMBOL BUILDING_PANDA_MATHUTIL
   ${P3MATHUTIL_HEADERS} ${P3MATHUTIL_SOURCES})
-target_link_libraries(p3mathutil p3event)
-target_use_packages(p3mathutil FFTW)
+target_link_libraries(p3mathutil p3event PKG::FFTW)
 target_interrogate(p3mathutil ALL)
 
 if(NOT BUILD_METALIBS)

+ 1 - 2
panda/src/ode/CMakeLists.txt

@@ -87,8 +87,7 @@ composite_sources(p3ode P3ODE_SOURCES)
 add_library(p3ode ${P3ODE_SOURCES} ${P3ODE_HEADERS})
 set_target_properties(p3ode PROPERTIES DEFINE_SYMBOL BUILDING_PANDAODE)
 target_compile_definitions(p3ode PUBLIC dSINGLE)
-target_link_libraries(p3ode p3igateruntime panda)
-target_use_packages(p3ode ODE)
+target_link_libraries(p3ode p3igateruntime panda PKG::ODE)
 target_interrogate(p3ode ${P3ODE_IGATE_SOURCES} EXTENSIONS ${P3ODE_IGATEEXT})
 
 install(TARGETS p3ode DESTINATION lib RUNTIME DESTINATION bin)

+ 1 - 2
panda/src/pnmimagetypes/CMakeLists.txt

@@ -33,8 +33,7 @@ set(P3PNMIMAGETYPES_SOURCES
 composite_sources(p3pnmimagetypes P3PNMIMAGETYPES_SOURCES)
 add_component_library(p3pnmimagetypes SYMBOL BUILDING_PANDA_PNMIMAGETYPES
   ${P3PNMIMAGETYPES_HEADERS} ${P3PNMIMAGETYPES_SOURCES})
-target_link_libraries(p3pnmimagetypes p3pnmimage)
-target_use_packages(p3pnmimagetypes JPEG TIFF PNG)
+target_link_libraries(p3pnmimagetypes p3pnmimage PKG::JPEG PKG::TIFF PKG::PNG)
 
 if(NOT BUILD_METALIBS)
   install(TARGETS p3pnmimagetypes DESTINATION lib RUNTIME DESTINATION bin)

+ 1 - 2
panda/src/pnmtext/CMakeLists.txt

@@ -20,8 +20,7 @@ set(P3PNMTEXT_SOURCES
 composite_sources(p3pnmtext P3PNMTEXT_SOURCES)
 add_component_library(p3pnmtext SYMBOL BUILDING_PANDA_PNMTEXT
   ${P3PNMTEXT_HEADERS} ${P3PNMTEXT_SOURCES})
-target_link_libraries(p3pnmtext p3parametrics p3pnmimage)
-target_use_packages(p3pnmtext FREETYPE)
+target_link_libraries(p3pnmtext p3parametrics p3pnmimage PKG::FREETYPE)
 target_interrogate(p3pnmtext ALL)
 
 if(NOT BUILD_METALIBS)

+ 1 - 2
pandatool/src/gtk-stats/CMakeLists.txt

@@ -26,8 +26,7 @@ set(GTKSTATS_SOURCES
 
 composite_sources(gtkstats GTKSTATS_SOURCES)
 add_executable(gtkstats ${GTKSTATS_HEADERS} ${GTKSTATS_SOURCES})
-target_link_libraries(gtkstats p3progbase p3pstatserver p3pystub)
-target_use_packages(gtkstats GTK2)
+target_link_libraries(gtkstats p3progbase p3pstatserver p3pystub PKG::GTK2)
 
 # This program is NOT actually called gtkstats. It's pstats-gtk on Win32 and
 # pstats everywhere else (as the Win32 GUI is not built).

+ 1 - 2
pandatool/src/progbase/CMakeLists.txt

@@ -10,8 +10,7 @@ set(P3PROGBASE_SOURCES
 
 composite_sources(p3progbase P3PROGBASE_SOURCES)
 add_library(p3progbase STATIC ${P3PROGBASE_HEADERS} ${P3PROGBASE_SOURCES})
-target_link_libraries(p3progbase p3pandatoolbase panda)
-target_use_packages(p3progbase ZLIB)
+target_link_libraries(p3progbase p3pandatoolbase panda PKG::ZLIB)
 
 # This is only needed for binaries in the pandatool package. It is not useful
 # for user applications, so it is not installed.