From 0495699cb85889ceda413e16808344532e8b1305 Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Mon, 13 Feb 2023 14:12:38 +0100 Subject: [PATCH] feat!: Migrate to `std::filesystem` consistently --- cmake/FairMQProjectSettings.cmake | 16 +++++---- cmake/FairMQSummary.cmake | 2 +- fairmq/CMakeLists.txt | 2 +- fairmq/PluginManager.cxx | 5 ++- fairmq/PluginManager.h | 44 ++++++++++++++++++------- fairmq/Properties.cxx | 28 +++++++++++++--- fairmq/shmem/Manager.h | 3 +- fairmq/shmem/UnmanagedRegion.h | 3 +- test/plugins/_plugin_manager.cxx | 5 +-- test/properties/_properties.cxx | 55 +++++++++++++++++++++++++++---- 10 files changed, 123 insertions(+), 40 deletions(-) diff --git a/cmake/FairMQProjectSettings.cmake b/cmake/FairMQProjectSettings.cmake index c860c6da..62147e11 100644 --- a/cmake/FairMQProjectSettings.cmake +++ b/cmake/FairMQProjectSettings.cmake @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (C) 2018-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # +# Copyright (C) 2018-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # # # # This software is distributed under the terms of the # # GNU Lesser General Public Licence (LGPL) version 3, # @@ -168,11 +168,15 @@ if(CMAKE_GENERATOR STREQUAL Ninja AND ENABLE_CCACHE) endif() endif() -if( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" - AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9) - set(FAIRMQ_HAS_STD_FILESYSTEM 0) -else() - set(FAIRMQ_HAS_STD_FILESYSTEM 1) +if(NOT DEFINED FAIRMQ_HAS_STD_FILESYSTEM) + if( ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" + AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9) + OR ( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" + AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)) + set(FAIRMQ_HAS_STD_FILESYSTEM 0) + else() + set(FAIRMQ_HAS_STD_FILESYSTEM 1) + endif() endif() if(NOT DEFINED FAIRMQ_HAS_STD_PMR) diff --git a/cmake/FairMQSummary.cmake b/cmake/FairMQSummary.cmake index 50966110..af55ac75 100644 --- a/cmake/FairMQSummary.cmake +++ b/cmake/FairMQSummary.cmake @@ -97,6 +97,6 @@ endmacro() macro(fairmq_summary_compile_definitions) message(STATUS " ") message(STATUS " ${Cyan}COMPILE DEFINITION VALUE${CR}") - message(STATUS " ${BWhite}FAIRMQ_HAS_STD_FILESYSTEM${CR} ${FAIRMQ_HAS_STD_FILESYSTEM}") + message(STATUS " ${BWhite}FAIRMQ_HAS_STD_FILESYSTEM${CR} ${FAIRMQ_HAS_STD_FILESYSTEM} (overridable with ${BMagenta}-DFAIRMQ_HAS_STD_FILESYSTEM=0|1${CR})") message(STATUS " ${BWhite}FAIRMQ_HAS_STD_PMR${CR} ${FAIRMQ_HAS_STD_PMR} (overridable with ${BMagenta}-DFAIRMQ_HAS_STD_PMR=0|1${CR})") endmacro() diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index 1b49e599..39244af3 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -197,7 +197,7 @@ if(BUILD_FAIRMQ) $<$:rt> Boost::boost Boost::program_options - Boost::filesystem + Boost::filesystem # still needed for Boost.DLL Boost::regex FairLogger::FairLogger diff --git a/fairmq/PluginManager.cxx b/fairmq/PluginManager.cxx index 9bf1cc34..8a528f09 100644 --- a/fairmq/PluginManager.cxx +++ b/fairmq/PluginManager.cxx @@ -22,7 +22,6 @@ using namespace std; using fair::mq::Plugin; using fair::mq::tools::ToString; using fair::mq::tools::ToStrVector; -namespace fs = boost::filesystem; namespace po = boost::program_options; namespace dll = boost::dll; using boost::optional; @@ -171,7 +170,7 @@ auto fair::mq::PluginManager::LoadPluginPrelinkedDynamic(const string& pluginNam } auto fair::mq::PluginManager::SearchPluginFile(const string& pluginName) const - -> boost::filesystem::path + -> fs::path { for (const auto& searchPath : SearchPaths()) { for (const auto& libPrefix : {fgkLibPrefix, fgkLibPrefixAlt}) { @@ -181,7 +180,7 @@ auto fair::mq::PluginManager::SearchPluginFile(const string& pluginName) const libPrefix, pluginName, boost::dll::detail::shared_library_impl::suffix().native()); - auto const found = boost::filesystem::exists(file); + auto const found = fs::exists(file); if (found) { return file; } diff --git a/fairmq/PluginManager.h b/fairmq/PluginManager.h index 34710e5a..6ce4882b 100644 --- a/fairmq/PluginManager.h +++ b/fairmq/PluginManager.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2017-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -13,9 +13,16 @@ #include #include +#if FAIRMQ_HAS_STD_FILESYSTEM +#include +namespace fs = std::filesystem; +#else #define BOOST_FILESYSTEM_VERSION 3 #define BOOST_FILESYSTEM_NO_DEPRECATED #include +namespace fs = ::boost::filesystem; +#endif + #include #include #include @@ -60,13 +67,13 @@ class PluginManager LOG(debug) << "Shutting down Plugin Manager"; } - auto SetSearchPaths(const std::vector&) -> void; - auto AppendSearchPath(const boost::filesystem::path&) -> void; - auto PrependSearchPath(const boost::filesystem::path&) -> void; - auto SearchPaths() const -> const std::vector& { return fSearchPaths; } + auto SetSearchPaths(const std::vector&) -> void; + auto AppendSearchPath(const fs::path&) -> void; + auto PrependSearchPath(const fs::path&) -> void; + auto SearchPaths() const -> const std::vector& { return fSearchPaths; } struct BadSearchPath : std::invalid_argument { using std::invalid_argument::invalid_argument; }; - auto SearchPluginFile(const std::string&) const -> boost::filesystem::path; + auto SearchPluginFile(const std::string&) const -> fs::path; struct PluginNotFound : std::runtime_error { using std::runtime_error::runtime_error; }; auto LoadPlugin(const std::string& pluginName) -> void; auto LoadPlugins(const std::vector& pluginNames) -> void { for(const auto& pluginName : pluginNames) { LoadPlugin(pluginName); } } @@ -88,18 +95,33 @@ class PluginManager auto WaitForPluginsToReleaseDeviceControl() -> void { fPluginServices->WaitForReleaseDeviceControl(); } private: - static auto ValidateSearchPath(const boost::filesystem::path&) -> void; + static auto ValidateSearchPath(const fs::path&) -> void; auto LoadPluginPrelinkedDynamic(const std::string& pluginName) -> void; auto LoadPluginDynamic(const std::string& pluginName) -> void; auto LoadPluginStatic(const std::string& pluginName) -> void; - template - auto LoadSymbols(const std::string& pluginName, Args&&... args) -> void +#if FAIRMQ_HAS_STD_FILESYSTEM + template + static auto AdaptPathType(T&& path) + { + if constexpr(std::is_same_v) { + return boost::filesystem::path(std::forward(path)); + } else { + return std::forward(path); + } + } +#endif + template + auto LoadSymbols(const std::string& pluginName, FirstArg&& farg, Args&&... args) -> void { using namespace boost::dll; using fair::mq::tools::ToString; - auto lib = shared_library{std::forward(args)...}; +#if FAIRMQ_HAS_STD_FILESYSTEM + auto lib = shared_library{AdaptPathType(std::forward(farg)), std::forward(args)...}; +#else + auto lib = shared_library{std::forward(farg), std::forward(args)...}; +#endif fgDLLKeepAlive.push_back(lib); fPluginFactories[pluginName] = import_alias( @@ -121,7 +143,7 @@ class PluginManager static const std::string fgkLibPrefix; static const std::string fgkLibPrefixAlt; - std::vector fSearchPaths; + std::vector fSearchPaths; static std::vector fgDLLKeepAlive; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) std::map> fPluginFactories; std::unique_ptr fPluginServices; diff --git a/fairmq/Properties.cxx b/fairmq/Properties.cxx index fd710fd4..4c076b72 100644 --- a/fairmq/Properties.cxx +++ b/fairmq/Properties.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -8,7 +8,15 @@ #include +#if FAIRMQ_HAS_STD_FILESYSTEM +#include +namespace fs = std::filesystem; +#else +#define BOOST_FILESYSTEM_VERSION 3 +#define BOOST_FILESYSTEM_NO_DEPRECATED #include +namespace fs = ::boost::filesystem; +#endif using namespace std; using boost::any_cast; @@ -84,7 +92,12 @@ unordered_map(const Property&)>> Prope { type_index(typeid(long double)), [](const Property& p) { return getStringPair(p, "long double"); } }, { type_index(typeid(bool)), [](const Property& p) { stringstream ss; ss << boolalpha << any_cast(p); return pair{ ss.str(), "bool" }; } }, { type_index(typeid(vector)), [](const Property& p) { stringstream ss; ss << boolalpha << any_cast>(p); return pair{ ss.str(), "vector>" }; } }, - { type_index(typeid(boost::filesystem::path)), [](const Property& p) { return getStringPair(p, "boost::filesystem::path"); } }, + { type_index(typeid(fs::path)), [](const Property& p) { return getStringPair(p, +#if FAIRMQ_HAS_STD_FILESYSTEM + "std::filesystem::path"); } }, +#else + "boost::filesystem::path"); } }, +#endif { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, @@ -100,7 +113,12 @@ unordered_map(const Property&)>> Prope { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, - { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, + { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, +#if FAIRMQ_HAS_STD_FILESYSTEM + "vector"); } }, +#else + "vector"); } }, +#endif }; unordered_map PropertyHelper::fEventEmitters = { @@ -122,7 +140,7 @@ unordered_map(k, any_cast(p)); } }, { type_index(typeid(bool)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, - { type_index(typeid(boost::filesystem::path)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, + { type_index(typeid(fs::path)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, @@ -138,7 +156,7 @@ unordered_map)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, - { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, + { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, }; } // namespace fair::mq diff --git a/fairmq/shmem/Manager.h b/fairmq/shmem/Manager.h index 04eb4f69..b6bec69e 100644 --- a/fairmq/shmem/Manager.h +++ b/fairmq/shmem/Manager.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -20,7 +20,6 @@ #include #include -#include #include #include #include diff --git a/fairmq/shmem/UnmanagedRegion.h b/fairmq/shmem/UnmanagedRegion.h index 6ef408cd..cb9464ea 100644 --- a/fairmq/shmem/UnmanagedRegion.h +++ b/fairmq/shmem/UnmanagedRegion.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -16,7 +16,6 @@ #include -#include #include #include #include diff --git a/test/plugins/_plugin_manager.cxx b/test/plugins/_plugin_manager.cxx index b371bc41..506d5274 100644 --- a/test/plugins/_plugin_manager.cxx +++ b/test/plugins/_plugin_manager.cxx @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -26,7 +27,7 @@ namespace _plugin_manager { using namespace fair::mq; -using namespace boost::filesystem; +using namespace fs; using namespace boost::program_options; using namespace std; @@ -134,7 +135,7 @@ TEST(PluginManager, SearchPathValidation) TEST(PluginManager, SearchPaths) { - const auto temp = temp_directory_path() / unique_path(); + const auto temp = temp_directory_path() / fair::mq::tools::Uuid(); create_directories(temp); const auto non_existing_dir = temp / "non-existing-dir"; const auto existing_dir = temp / "existing-dir"; diff --git a/test/properties/_properties.cxx b/test/properties/_properties.cxx index b8aedc4e..51d30098 100644 --- a/test/properties/_properties.cxx +++ b/test/properties/_properties.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2019-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -8,7 +8,16 @@ #include +#if FAIRMQ_HAS_STD_FILESYSTEM +#include +namespace fs = std::filesystem; +#else +#define BOOST_FILESYSTEM_VERSION 3 +#define BOOST_FILESYSTEM_NO_DEPRECATED #include +namespace fs = ::boost::filesystem; +#endif + #include #include #include @@ -52,7 +61,13 @@ TEST(ProgOptions, SetAndGet) set_and_get(o, "_double", 33.22); set_and_get(o, "_long double", 333.222); set_and_get(o, "_bool", true); - set_and_get(o, "_boost::filesystem::path", boost::filesystem::path("C:\\Windows")); + set_and_get(o, +#if FAIRMQ_HAS_STD_FILESYSTEM + "_std::filesystem::path", +#else + "_boost::filesystem::path", +#endif + fs::path("C:\\Windows")); set_and_get>(o, "_vector", { true, true }); set_and_get>(o, "_vector", { 'a', 'b', 'c' }); set_and_get>(o, "_vector", { -1, -2, -3 }); @@ -71,7 +86,13 @@ TEST(ProgOptions, SetAndGet) set_and_get>(o, "_vector", { 3.2, 3.3, 3.4 }); set_and_get>(o, "_vector", { 33.22, 33.23, 33.24 }); set_and_get>(o, "_vector", { 333.222, 333.223, 333.224 }); - set_and_get>(o, "_vector", { boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") }); + set_and_get>(o, +#if FAIRMQ_HAS_STD_FILESYSTEM + "_vector", +#else + "_vector", +#endif + { fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") }); ASSERT_THROW(o.ParseAll({"cmd", "--unregistered", "option"}, false), boost::program_options::unknown_option); ASSERT_NO_THROW(o.ParseAll({"cmd", "--unregistered", "option"}, true)); @@ -136,7 +157,13 @@ TEST(ProgOptions, SubscribeAndSet) subscribe_and_set(o, "_double", 33.22); subscribe_and_set(o, "_long double", 333.222); subscribe_and_set(o, "_bool", true); - subscribe_and_set(o, "_boost::filesystem::path", boost::filesystem::path("C:\\Windows")); + subscribe_and_set(o, +#if FAIRMQ_HAS_STD_FILESYSTEM + "_std::filesystem::path", +#else + "_boost::filesystem::path", +#endif + fs::path("C:\\Windows")); subscribe_and_set>(o, "_vector", { true, true }); subscribe_and_set>(o, "_vector", { 'a', 'b', 'c' }); subscribe_and_set>(o, "_vector", { -1, -2, -3 }); @@ -155,7 +182,13 @@ TEST(ProgOptions, SubscribeAndSet) subscribe_and_set>(o, "_vector", { 3.2, 3.3, 3.4 }); subscribe_and_set>(o, "_vector", { 33.22, 33.23, 33.24 }); subscribe_and_set>(o, "_vector", { 333.222, 333.223, 333.224 }); - subscribe_and_set>(o, "_vector", { boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") }); + subscribe_and_set>(o, +#if FAIRMQ_HAS_STD_FILESYSTEM + "_vector", +#else + "_vector", +#endif + { fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") }); } TEST(PropertyHelper, ConvertPropertyToString) @@ -179,7 +212,11 @@ TEST(PropertyHelper, ConvertPropertyToString) EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(static_cast(33.22))), "33.22"); EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(static_cast(333.222))), "333.222"); EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(static_cast(true))), "true"); - EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(boost::filesystem::path("C:\\Windows"))), "\"C:\\Windows\""); +#if FAIRMQ_HAS_STD_FILESYSTEM + EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(fs::path("C:\\Windows"))), "\"C:\\\\Windows\""); +#else + EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(fs::path("C:\\Windows"))), "\"C:\\Windows\""); +#endif EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ 'a', 'b', 'c' }))), "a, b, c"); EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ -1, -2, -3 }))), "-1, -2, -3"); EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ 1, 2, 3 }))), "1, 2, 3"); @@ -198,7 +235,11 @@ TEST(PropertyHelper, ConvertPropertyToString) EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ 33.22, 33.23, 33.24 }))), "33.22, 33.23, 33.24"); EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ 333.222, 333.223, 333.224 }))), "333.222, 333.223, 333.224"); EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ true, true }))), "true, true"); - EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") }))), "\"C:\\Windows\", \"C:\\Windows\\System32\""); +#if FAIRMQ_HAS_STD_FILESYSTEM + EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") }))), "\"C:\\\\Windows\", \"C:\\\\Windows\\\\System32\""); +#else + EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector({ fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") }))), "\"C:\\Windows\", \"C:\\Windows\\System32\""); +#endif } } // namespace