diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e23de112f..785e5a5ed3 100644 --- a/CMakeLists.txt +++ b/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) diff --git a/cmake/macros/BuildMetalib.cmake b/cmake/macros/BuildMetalib.cmake index 716f8c09d8..5c14ee3c09 100644 --- a/cmake/macros/BuildMetalib.cmake +++ b/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) diff --git a/cmake/macros/Interrogate.cmake b/cmake/macros/Interrogate.cmake index df0c173090..7b4d201936 100644 --- a/cmake/macros/Interrogate.cmake +++ b/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 diff --git a/cmake/macros/PackageConfig.cmake b/cmake/macros/PackageConfig.cmake index b259a465e0..9be8004ac7 100644 --- a/cmake/macros/PackageConfig.cmake +++ b/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 "") - if(${found_as}_LIBRARIES) - set(_PKG_${name}_LIBRARIES ${${found_as}_LIBRARIES} CACHE INTERNAL "") + if(${found_as}_INCLUDE_DIRS) + set(includes ${${found_as}_INCLUDE_DIRS}) else() - set(_PKG_${name}_LIBRARIES "${${found_as}_LIBRARY}" CACHE INTERNAL "") + set(includes "${${found_as}_INCLUDE_DIR}") endif() - else() - unset(_PKG_${name}_INCLUDES CACHE) - unset(_PKG_${name}_LIBRARIES CACHE) + if(${found_as}_LIBRARIES) + set(libs ${${found_as}_LIBRARIES}) + else() + set(libs "${${found_as}_LIBRARY}") + endif() + + 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 + "$<$>>:${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 - $<$>>:${_PKG_${lib}_INCLUDES}>) - endif() - endforeach(lib) -endmacro(target_use_packages) diff --git a/direct/src/dcparser/CMakeLists.txt b/direct/src/dcparser/CMakeLists.txt index 9a4faff8e9..3780cf5293 100644 --- a/direct/src/dcparser/CMakeLists.txt +++ b/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) diff --git a/direct/src/distributed/CMakeLists.txt b/direct/src/distributed/CMakeLists.txt index 4ae6e406bd..09662ac5eb 100644 --- a/direct/src/distributed/CMakeLists.txt +++ b/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) diff --git a/dtool/src/dtoolbase/CMakeLists.txt b/dtool/src/dtoolbase/CMakeLists.txt index cff3e9b17c..ebcc827fb5 100644 --- a/dtool/src/dtoolbase/CMakeLists.txt +++ b/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) diff --git a/dtool/src/interrogate/CMakeLists.txt b/dtool/src/interrogate/CMakeLists.txt index 544934e7c8..0367567f66 100644 --- a/dtool/src/interrogate/CMakeLists.txt +++ b/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) diff --git a/dtool/src/interrogatedb/CMakeLists.txt b/dtool/src/interrogatedb/CMakeLists.txt index ace718342c..3088a31e78 100644 --- a/dtool/src/interrogatedb/CMakeLists.txt +++ b/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) diff --git a/dtool/src/prc/CMakeLists.txt b/dtool/src/prc/CMakeLists.txt index dbeceacb55..9c74352017 100644 --- a/dtool/src/prc/CMakeLists.txt +++ b/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) diff --git a/panda/src/audiotraits/CMakeLists.txt b/panda/src/audiotraits/CMakeLists.txt index 4521ee6e5c..a40f4dc301 100644 --- a/panda/src/audiotraits/CMakeLists.txt +++ b/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() diff --git a/panda/src/bullet/CMakeLists.txt b/panda/src/bullet/CMakeLists.txt index c2c4cd6b6d..781e61f397 100644 --- a/panda/src/bullet/CMakeLists.txt +++ b/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) diff --git a/panda/src/express/CMakeLists.txt b/panda/src/express/CMakeLists.txt index 259bac1e57..2d2929fed3 100644 --- a/panda/src/express/CMakeLists.txt +++ b/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) diff --git a/panda/src/ffmpeg/CMakeLists.txt b/panda/src/ffmpeg/CMakeLists.txt index 09d7e38c3d..84a498a54e 100644 --- a/panda/src/ffmpeg/CMakeLists.txt +++ b/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) diff --git a/panda/src/glgsg/CMakeLists.txt b/panda/src/glgsg/CMakeLists.txt index 0d030a7f0a..9421def529 100644 --- a/panda/src/glgsg/CMakeLists.txt +++ b/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) diff --git a/panda/src/gobj/CMakeLists.txt b/panda/src/gobj/CMakeLists.txt index d3301f64de..a4df5f7f32 100644 --- a/panda/src/gobj/CMakeLists.txt +++ b/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) diff --git a/panda/src/linmath/CMakeLists.txt b/panda/src/linmath/CMakeLists.txt index 6fd6b342b9..c3fa7332bd 100644 --- a/panda/src/linmath/CMakeLists.txt +++ b/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) diff --git a/panda/src/mathutil/CMakeLists.txt b/panda/src/mathutil/CMakeLists.txt index 0febd3958d..6018510008 100644 --- a/panda/src/mathutil/CMakeLists.txt +++ b/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) diff --git a/panda/src/ode/CMakeLists.txt b/panda/src/ode/CMakeLists.txt index dab81d57cb..934fdbc04f 100644 --- a/panda/src/ode/CMakeLists.txt +++ b/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) diff --git a/panda/src/pnmimagetypes/CMakeLists.txt b/panda/src/pnmimagetypes/CMakeLists.txt index fc2e20efed..688caba7c5 100644 --- a/panda/src/pnmimagetypes/CMakeLists.txt +++ b/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) diff --git a/panda/src/pnmtext/CMakeLists.txt b/panda/src/pnmtext/CMakeLists.txt index 6b42b9feef..1e1c2c9e14 100644 --- a/panda/src/pnmtext/CMakeLists.txt +++ b/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) diff --git a/pandatool/src/gtk-stats/CMakeLists.txt b/pandatool/src/gtk-stats/CMakeLists.txt index b976f805a4..130ed34b4a 100644 --- a/pandatool/src/gtk-stats/CMakeLists.txt +++ b/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). diff --git a/pandatool/src/progbase/CMakeLists.txt b/pandatool/src/progbase/CMakeLists.txt index 44bb8b80b2..6d037b460d 100644 --- a/pandatool/src/progbase/CMakeLists.txt +++ b/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.