Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip find_package for already present targets #236

Merged
merged 10 commits into from
Dec 6, 2024
113 changes: 72 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ option(REFLECTCPP_BUILD_TESTS "Build tests" OFF)
option(REFLECTCPP_USE_BUNDLED_DEPENDENCIES "Use the bundled dependencies" ON)

set(REFLECTCPP_USE_VCPKG_DEFAULT OFF)

if(REFLECTCPP_BUILD_BENCHMARKS)
set(REFLECTCPP_JSON ON CACHE BOOL "" FORCE)
set(REFLECTCPP_BSON ON CACHE BOOL "" FORCE)
Expand All @@ -29,6 +30,7 @@ if(REFLECTCPP_BUILD_BENCHMARKS)
set(REFLECTCPP_UBJSON ON CACHE BOOL "" FORCE)
set(REFLECTCPP_YAML ON CACHE BOOL "" FORCE)
endif()

if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR
REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_YAML)
Expand Down Expand Up @@ -72,7 +74,10 @@ if (REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
reflectcpp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/rfl/thirdparty>)
else ()
find_package(ctre CONFIG REQUIRED)
if (NOT TARGET ctre::ctre)
find_package(ctre CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC ctre::ctre)
endif ()

if (REFLECTCPP_JSON)
Expand All @@ -89,7 +94,9 @@ if (REFLECTCPP_JSON)
reflectcpp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/rfl/thirdparty>)
else ()
find_package(yyjson CONFIG REQUIRED)
if (NOT TARGET yyjson::yyjson)
find_package(yyjson CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC yyjson::yyjson)
endif ()
endif ()
Expand All @@ -98,8 +105,10 @@ if (REFLECTCPP_BSON)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_bson.cpp
)
find_package(bson-1.0 CONFIG REQUIRED)
target_link_libraries(reflectcpp PRIVATE $<IF:$<TARGET_EXISTS:mongo::bson_static>,mongo::bson_static,mongo::bson_shared>)
if (NOT TARGET mongo::bson_static AND NOT TARGET mongo::bson_shared)
find_package(bson-1.0 CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC $<IF:$<TARGET_EXISTS:mongo::bson_static>,mongo::bson_static,mongo::bson_shared>)
endif ()

if (REFLECTCPP_CBOR)
Expand All @@ -115,7 +124,9 @@ if (REFLECTCPP_CBOR)
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libtinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif ()
else()
find_package(tinycbor)
if (NOT TARGET tinycbor)
find_package(tinycbor)
endif()
target_link_libraries(reflectcpp PUBLIC tinycbor::tinycbor)
endif()
endif ()
Expand All @@ -124,7 +135,9 @@ if (REFLECTCPP_FLEXBUFFERS)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_flexbuf.cpp
)
find_package(flatbuffers CONFIG REQUIRED)
if (NOT TARGET flatbuffers::flatbuffers)
find_package(flatbuffers CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC flatbuffers::flatbuffers)
endif ()

Expand All @@ -133,9 +146,13 @@ if (REFLECTCPP_MSGPACK)
src/reflectcpp_msgpack.cpp
)
if(REFLECTCPP_USE_VCPKG)
find_package(msgpack-c CONFIG REQUIRED)
if (NOT TARGET msgpack-c)
find_package(msgpack-c CONFIG REQUIRED)
endif()
else()
find_package(msgpack CONFIG REQUIRED)
if (NOT TARGET msgpack)
find_package(msgpack CONFIG REQUIRED)
endif()
endif()
target_link_libraries(reflectcpp PUBLIC msgpack-c)
endif ()
Expand All @@ -149,7 +166,9 @@ if (REFLECTCPP_TOML)
pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus)
target_link_libraries(reflectcpp PUBLIC PkgConfig::tomlplusplus)
else()
find_package(tomlplusplus)
if (NOT TARGET tomlplusplus)
find_package(tomlplusplus)
endif()
target_link_libraries(reflectcpp PUBLIC tomlplusplus::tomlplusplus)
endif()
endif()
Expand All @@ -158,22 +177,28 @@ if (REFLECTCPP_UBJSON)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_ubjson.cpp
)
find_package(jsoncons CONFIG REQUIRED)
if (NOT TARGET jsoncons)
find_package(jsoncons CONFIG REQUIRED)
endif()
endif ()

if (REFLECTCPP_XML)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_xml.cpp
)
find_package(pugixml CONFIG REQUIRED)
if (NOT TARGET pugixml::pugixml)
find_package(pugixml CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC pugixml::pugixml)
endif ()

if (REFLECTCPP_YAML)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_yaml.cpp
)
find_package(yaml-cpp CONFIG REQUIRED)
if (NOT TARGET yaml-cpp::yaml-cpp)
find_package(yaml-cpp CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC yaml-cpp::yaml-cpp)
endif ()

Expand All @@ -189,42 +214,48 @@ if (REFLECTCPP_BUILD_TESTS)
endif ()

if (REFLECTCPP_BUILD_BENCHMARKS)
find_package(benchmark CONFIG REQUIRED)
find_package(simdjson CONFIG REQUIRED)
if (NOT TARGET benchmark::benchmark)
find_package(benchmark CONFIG REQUIRED)
endif ()
if (NOT TARGET simdjson::simdjson)
find_package(simdjson CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC benchmark::benchmark simdjson::simdjson)
add_subdirectory(benchmarks)
endif ()

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
if (PROJECT_IS_TOP_LEVEL)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

configure_package_config_file(reflectcpp-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp
)
configure_package_config_file(reflectcpp-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp
)

install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp"
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp"
)

file(GLOB_RECURSE RFL_HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${CMAKE_CURRENT_LIST_DIR}/include/*" )
file(GLOB_RECURSE RFL_HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${CMAKE_CURRENT_LIST_DIR}/include/*" )

target_sources(reflectcpp
PUBLIC
FILE_SET reflectcpp_headers
TYPE HEADERS
BASE_DIRS $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
FILES ${RFL_HEADERS})
target_sources(reflectcpp
PUBLIC
FILE_SET reflectcpp_headers
TYPE HEADERS
BASE_DIRS $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
FILES ${RFL_HEADERS})

install(
TARGETS reflectcpp
EXPORT reflectcpp-exports
FILE_SET reflectcpp_headers DESTINATION ${INCLUDE_INSTALL_DIR}
)
install(
TARGETS reflectcpp
EXPORT reflectcpp-exports
FILE_SET reflectcpp_headers DESTINATION ${INCLUDE_INSTALL_DIR}
)

install(
EXPORT reflectcpp-exports
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp
NAMESPACE reflectcpp::
)
install(
EXPORT reflectcpp-exports
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp
NAMESPACE reflectcpp::
)
endif ()
Loading