From edb16bd41834ca03f2b7328101fe65bdeb2edb5a Mon Sep 17 00:00:00 2001 From: Caleb Schilly Date: Wed, 7 Aug 2024 17:03:05 -0400 Subject: [PATCH] #102: add support for external fmt --- CMakeLists.txt | 11 ++++++ apps/vt-tv_standalone.cc | 2 +- bindings/python/tv.h | 2 +- cmake/load_packages.cmake | 19 ++++++++-- cmake/vtTVConfig.cmake.in | 5 +++ examples/example2.cc | 2 +- examples/example3.cc | 2 +- lib/CLI/CMakeLists.txt | 2 ++ lib/brotli/CMakeLists.txt | 1 + lib/fmt/CMakeLists.txt | 6 ++-- lib/fmt/include/{fmt-vt => fmt-vt-tv}/core.h | 0 .../{fmt-vt => fmt-vt-tv}/format-inl.h | 0 .../include/{fmt-vt => fmt-vt-tv}/format.h | 0 .../include/{fmt-vt => fmt-vt-tv}/ostream.h | 0 lib/fmt/src/format.cc | 2 +- lib/json/CMakeLists.txt | 2 ++ lib/yaml-cpp/CMakeLists.txt | 1 + src/CMakeLists.txt | 35 +++++++++++-------- src/vt-tv/api/info.h | 2 +- src/vt-tv/api/object_communicator.h | 2 +- src/vt-tv/render/render.h | 2 +- src/vt-tv/utility/decompressor.impl.h | 2 +- src/vt-tv/utility/json_reader.cc | 4 +-- tests/unit/test_json_reader.cc | 2 +- 24 files changed, 74 insertions(+), 32 deletions(-) rename lib/fmt/include/{fmt-vt => fmt-vt-tv}/core.h (100%) rename lib/fmt/include/{fmt-vt => fmt-vt-tv}/format-inl.h (100%) rename lib/fmt/include/{fmt-vt => fmt-vt-tv}/format.h (100%) rename lib/fmt/include/{fmt-vt => fmt-vt-tv}/ostream.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6d58c6af5..44f9bd79fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,17 @@ message(STATUS "CMAKE_CXX_STANDARD: ${CMAKE_CXX_STANDARD}") option(VT_TV_PYTHON_BINDINGS_ENABLED "Build vt-tv with Python bindings" OFF) option(VT_TV_OPENMP_ENABLED "Build vt-tv with openMP support" ON) +option(VT_TV_EXTERNAL_FMT "Build vt-tv with an external fmt library" ON) + +if(VT_TV_EXTERNAL_FMT) + set(FMT_INCLUDE_NAME fmt) +else() + set(FMT_INCLUDE_NAME fmt-vt-tv) +endif() + +add_definitions(-DINCLUDE_FMT_CORE=<${FMT_INCLUDE_NAME}/core.h>) +add_definitions(-DINCLUDE_FMT_FORMAT=<${FMT_INCLUDE_NAME}/format.h>) +add_definitions(-DINCLUDE_FMT_OSTREAM=<${FMT_INCLUDE_NAME}/ostream.h>) # add -fPIC to all targets (if building with nanobind) if(VT_TV_PYTHON_BINDINGS_ENABLED) diff --git a/apps/vt-tv_standalone.cc b/apps/vt-tv_standalone.cc index 9edb7e55de..7beba0fb1c 100644 --- a/apps/vt-tv_standalone.cc +++ b/apps/vt-tv_standalone.cc @@ -47,7 +47,7 @@ #include #include "../tests/unit/cmake_config.h" -#include +#include INCLUDE_FMT_FORMAT #include int main(int argc, char** argv) { diff --git a/bindings/python/tv.h b/bindings/python/tv.h index 65c3a31312..7d2f742aec 100644 --- a/bindings/python/tv.h +++ b/bindings/python/tv.h @@ -46,7 +46,7 @@ #include -#include +#include INCLUDE_FMT_FORMAT #include "vt-tv/render/render.h" #include "vt-tv/api/types.h" #include "vt-tv/api/info.h" diff --git a/cmake/load_packages.cmake b/cmake/load_packages.cmake index 503c4084af..372c0c331a 100644 --- a/cmake/load_packages.cmake +++ b/cmake/load_packages.cmake @@ -7,10 +7,23 @@ if (NOT TARGET nlohmann_json) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib/json) endif() -# fmt always included in the build -if (NOT TARGET fmt) +# use included fmt or external one +if(VT_TV_EXTERNAL_FMT) + # user should provide 'fmt_DIR' or 'fmt_ROOT' to CMake (unless fmt is installed in system libs) + if(fmt_ROOT) + message(STATUS "VT_TV_EXTERNAL_FMT=ON. Using fmt located at ${fmt_ROOT}") + elseif(fmt_DIR) + message(STATUS "VT_TV_EXTERNAL_FMT=ON. Using fmt located at ${fmt_DIR}") + else() + message(STATUS "VT_TV_EXTERNAL_FMT=ON but neither fmt_DIR nor fmt_ROOT is provided!") + endif() + find_package(fmt 7.1.0 REQUIRED) set(FMT_LIBRARY fmt) - add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib/fmt) +else() + if (NOT TARGET fmt) + set(FMT_LIBRARY fmt) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib/fmt) + endif() endif() if (NOT TARGET brotli) diff --git a/cmake/vtTVConfig.cmake.in b/cmake/vtTVConfig.cmake.in index c848824dcb..6fa6e12bfb 100644 --- a/cmake/vtTVConfig.cmake.in +++ b/cmake/vtTVConfig.cmake.in @@ -3,3 +3,8 @@ get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) include(${SELF_DIR}/vtTVTargets.cmake) include(CMakeFindDependencyMacro) + +if (@VT_TV_EXTERNAL_FMT@) + set (fmt_DIR @fmt_DIR@) + find_dependency(fmt REQUIRED HINTS @fmt_DIR@) +endif() diff --git a/examples/example2.cc b/examples/example2.cc index 82d7e2f69a..fbe32eefa4 100644 --- a/examples/example2.cc +++ b/examples/example2.cc @@ -44,7 +44,7 @@ #include #include -#include +#include INCLUDE_FMT_FORMAT #include "../tests/unit/cmake_config.h" diff --git a/examples/example3.cc b/examples/example3.cc index bea800b91f..278ddeefc1 100644 --- a/examples/example3.cc +++ b/examples/example3.cc @@ -41,7 +41,7 @@ //@HEADER */ -#include +#include INCLUDE_FMT_FORMAT #include void printRecur(typename YAML::Node node, int tabs) { diff --git a/lib/CLI/CMakeLists.txt b/lib/CLI/CMakeLists.txt index f4679579e2..808ec2fc69 100644 --- a/lib/CLI/CMakeLists.txt +++ b/lib/CLI/CMakeLists.txt @@ -1,6 +1,8 @@ add_library(CLI INTERFACE) target_include_directories(CLI INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/CLI) +set_target_properties(CLI PROPERTIES OUTPUT_NAME CLI-vt-tv) + install( DIRECTORY CLI DESTINATION include diff --git a/lib/brotli/CMakeLists.txt b/lib/brotli/CMakeLists.txt index 4f120cb243..dfdadf4151 100644 --- a/lib/brotli/CMakeLists.txt +++ b/lib/brotli/CMakeLists.txt @@ -186,6 +186,7 @@ foreach(lib IN LISTS BROTLI_SHARED_LIBS) endforeach() foreach(lib IN LISTS BROTLI_SHARED_LIBS BROTLI_STATIC_LIBS) + set_target_properties (${lib} PROPERTIES OUTPUT_NAME ${lib}-vt-tv) target_include_directories( ${lib} INTERFACE diff --git a/lib/fmt/CMakeLists.txt b/lib/fmt/CMakeLists.txt index 0357be8374..c3842c7162 100644 --- a/lib/fmt/CMakeLists.txt +++ b/lib/fmt/CMakeLists.txt @@ -3,7 +3,7 @@ project(FMT CXX) function(add_headers VAR) set(headers ${${VAR}}) foreach (header ${ARGN}) - set(headers ${headers} include/fmt-vt/${header}) + set(headers ${headers} include/fmt-vt-tv/${header}) endforeach() set(${VAR} ${headers} PARENT_SCOPE) endfunction() @@ -14,7 +14,7 @@ set(FMT_SOURCES src/format.cc) add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS}) add_library(fmt::fmt ALIAS fmt) -set_target_properties(fmt PROPERTIES OUTPUT_NAME fmt-vt) +set_target_properties(fmt PROPERTIES OUTPUT_NAME fmt-vt-tv) target_include_directories(fmt SYSTEM PUBLIC $ @@ -26,7 +26,7 @@ set_target_properties(fmt PROPERTIES DEBUG_POSTFIX "${FMT_DEBUG_POSTFIX}") install( - DIRECTORY include/fmt-vt + DIRECTORY include/fmt-vt-tv DESTINATION include CONFIGURATIONS ${build_type_list} FILES_MATCHING PATTERN "*" diff --git a/lib/fmt/include/fmt-vt/core.h b/lib/fmt/include/fmt-vt-tv/core.h similarity index 100% rename from lib/fmt/include/fmt-vt/core.h rename to lib/fmt/include/fmt-vt-tv/core.h diff --git a/lib/fmt/include/fmt-vt/format-inl.h b/lib/fmt/include/fmt-vt-tv/format-inl.h similarity index 100% rename from lib/fmt/include/fmt-vt/format-inl.h rename to lib/fmt/include/fmt-vt-tv/format-inl.h diff --git a/lib/fmt/include/fmt-vt/format.h b/lib/fmt/include/fmt-vt-tv/format.h similarity index 100% rename from lib/fmt/include/fmt-vt/format.h rename to lib/fmt/include/fmt-vt-tv/format.h diff --git a/lib/fmt/include/fmt-vt/ostream.h b/lib/fmt/include/fmt-vt-tv/ostream.h similarity index 100% rename from lib/fmt/include/fmt-vt/ostream.h rename to lib/fmt/include/fmt-vt-tv/ostream.h diff --git a/lib/fmt/src/format.cc b/lib/fmt/src/format.cc index c9e00acdcd..a349545095 100644 --- a/lib/fmt/src/format.cc +++ b/lib/fmt/src/format.cc @@ -5,7 +5,7 @@ // // For the license information refer to format.h. -#include "fmt-vt/format-inl.h" +#include "fmt-vt-tv/format-inl.h" FMT_BEGIN_NAMESPACE namespace detail { diff --git a/lib/json/CMakeLists.txt b/lib/json/CMakeLists.txt index fa77a5aed2..ff1a23dd81 100644 --- a/lib/json/CMakeLists.txt +++ b/lib/json/CMakeLists.txt @@ -79,6 +79,8 @@ target_include_directories( $ ) +set_target_properties(${NLOHMANN_JSON_TARGET_NAME} PROPERTIES OUTPUT_NAME ${NLOHMANN_JSON_TARGET_NAME}-vt-tv) + ## add debug view definition file for msvc (natvis) if (MSVC) set(NLOHMANN_ADD_NATVIS TRUE) diff --git a/lib/yaml-cpp/CMakeLists.txt b/lib/yaml-cpp/CMakeLists.txt index b230b9e6de..f94381db95 100644 --- a/lib/yaml-cpp/CMakeLists.txt +++ b/lib/yaml-cpp/CMakeLists.txt @@ -122,6 +122,7 @@ if (NOT DEFINED CMAKE_DEBUG_POSTFIX) endif() set_target_properties(yaml-cpp PROPERTIES + OUTPUT_NAME yaml-cpp-vt-tv VERSION "${PROJECT_VERSION}" SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}" PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8493e34d20..95e3b861ef 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,9 +84,15 @@ target_link_libraries( ${VT_TV_LIBRARY} PUBLIC ${JSON_LIBRARY} ) -target_link_libraries( - ${VT_TV_LIBRARY} PUBLIC ${FMT_LIBRARY} -) +if(${VT_TV_EXTERNAL_FMT}) + target_link_libraries( + ${VT_TV_LIBRARY} PUBLIC fmt::fmt + ) +else() + target_link_libraries( + ${VT_TV_LIBRARY} PUBLIC ${FMT_LIBRARY} + ) +endif() target_link_libraries( ${VT_TV_LIBRARY} PUBLIC ${BROTLI_LIBRARY} @@ -125,17 +131,18 @@ install( COMPONENT runtime ) -install(TARGETS ${FMT_LIBRARY} EXPORT ${VT_TV_LIBRARY}) -install(TARGETS ${JSON_LIBRARY} EXPORT ${VT_TV_LIBRARY}) -install(TARGETS ${BROTLI_LIBRARY} EXPORT ${VT_TV_LIBRARY}) -install(TARGETS ${YAML_LIBRARY} EXPORT ${VT_TV_LIBRARY}) +set(LIBRARIES_TO_INSTALL ${JSON_LIBRARY} ${BROTLI_LIBRARY} ${YAML_LIBRARY}) + +if(NOT VT_TV_EXTERNAL_FMT) + list(APPEND LIBRARIES_TO_INSTALL ${FMT_LIBRARY}) +endif() + +foreach(LIBRARY ${LIBRARIES_TO_INSTALL}) + install(TARGETS ${LIBRARY} EXPORT ${VT_TV_LIBRARY}) +endforeach() export( - TARGETS ${VT_TV_LIBRARY} - ${FMT_LIBRARY} - ${JSON_LIBRARY} - ${BROTLI_LIBRARY} - ${YAML_LIBRARY} - FILE "vtTVTargets.cmake" - NAMESPACE vt::lib:: + TARGETS ${VT_TV_LIBRARY} ${LIBRARIES_TO_INSTALL} + FILE "vtTVTargets.cmake" + NAMESPACE vt::lib:: ) diff --git a/src/vt-tv/api/info.h b/src/vt-tv/api/info.h index f3f536623e..93d88a37a6 100644 --- a/src/vt-tv/api/info.h +++ b/src/vt-tv/api/info.h @@ -48,7 +48,7 @@ #include "vt-tv/api/rank.h" #include "vt-tv/api/object_info.h" -#include +#include INCLUDE_FMT_FORMAT #include #include diff --git a/src/vt-tv/api/object_communicator.h b/src/vt-tv/api/object_communicator.h index c93801f489..97f3e2cfff 100644 --- a/src/vt-tv/api/object_communicator.h +++ b/src/vt-tv/api/object_communicator.h @@ -10,7 +10,7 @@ #include #include "vt-tv/api/types.h" -#include +#include INCLUDE_FMT_FORMAT namespace vt::tv { diff --git a/src/vt-tv/render/render.h b/src/vt-tv/render/render.h index eb4ae257c6..cf8516f76a 100644 --- a/src/vt-tv/render/render.h +++ b/src/vt-tv/render/render.h @@ -83,7 +83,7 @@ #include "vt-tv/api/rank.h" #include "vt-tv/api/info.h" -#include +#include INCLUDE_FMT_FORMAT #include #include #include diff --git a/src/vt-tv/utility/decompressor.impl.h b/src/vt-tv/utility/decompressor.impl.h index 49f3acd158..22f9fa4973 100644 --- a/src/vt-tv/utility/decompressor.impl.h +++ b/src/vt-tv/utility/decompressor.impl.h @@ -48,7 +48,7 @@ #include -#include +#include INCLUDE_FMT_FORMAT namespace vt::tv::utility { diff --git a/src/vt-tv/utility/json_reader.cc b/src/vt-tv/utility/json_reader.cc index c3a3da306c..51576ee1d0 100644 --- a/src/vt-tv/utility/json_reader.cc +++ b/src/vt-tv/utility/json_reader.cc @@ -48,8 +48,8 @@ #include "vt-tv/utility/qoi_serializer.h" #include -#include -#include +#include INCLUDE_FMT_CORE +#include INCLUDE_FMT_FORMAT #include diff --git a/tests/unit/test_json_reader.cc b/tests/unit/test_json_reader.cc index 78c3d6b39e..f3bce04551 100644 --- a/tests/unit/test_json_reader.cc +++ b/tests/unit/test_json_reader.cc @@ -48,7 +48,7 @@ #include #include "vt-tv/utility/qoi_serializer.h" -#include +#include INCLUDE_FMT_FORMAT #include "cmake_config.h"