From df0dfa3b5c47f6a4ad13f34422fcfdaddad22e64 Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Mon, 11 Nov 2024 19:04:26 -0500 Subject: [PATCH 1/5] make boost::filesystem optional --- CMakeLists.txt | 8 ++++++-- tools/CMakeLists.txt | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21102aa..496988f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ option(ENABLE_COVERAGE "Build the project with gcov support (Need ENABLE_TEST=On set(GCOV_TOOL "gcov" CACHE STRING "Path to gcov tool used by coverage.") option(ENABLE_DOC "Build doxygen" Off) option(ENABLE_DATA "Build data" On) +option(ENABLE_TOOLS "Build tools" On) ######################################### # Dependency @@ -34,7 +35,7 @@ pkg_check_modules(ZSTD REQUIRED IMPORTED_TARGET "libzstd") find_package(Fcitx5Utils REQUIRED) include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake") -find_package(Boost 1.61 REQUIRED COMPONENTS iostreams filesystem) +find_package(Boost 1.61 REQUIRED COMPONENTS iostreams OPTIONAL_COMPONENTS filesystem) set(LIBIME_INSTALL_PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/libime") set(LIBIME_INSTALL_LIBDATADIR "${CMAKE_INSTALL_FULL_LIBDIR}/libime") @@ -56,7 +57,10 @@ if(ENABLE_TEST) endif() add_subdirectory(src) -add_subdirectory(tools) + +if (ENABLE_TOOLS) + add_subdirectory(tools) +endif() if (ENABLE_DATA) add_subdirectory(data) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index ca1e6ac..3a21af6 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -24,6 +24,7 @@ target_link_libraries(libime_tabledict LibIME::Table) install(TARGETS libime_tabledict DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::tabledict ALIAS libime_tabledict) +if (NOT APPLE) add_executable(libime_migrate_fcitx4_table libime_migrate_fcitx4_table.cpp) target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams Boost::filesystem) install(TARGETS libime_migrate_fcitx4_table DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) @@ -33,3 +34,4 @@ add_executable(libime_migrate_fcitx4_pinyin libime_migrate_fcitx4_pinyin.cpp) target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams Boost::filesystem) install(TARGETS libime_migrate_fcitx4_pinyin DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_pinyin ALIAS libime_migrate_fcitx4_pinyin) +endif() From 562c02a0e3998ff4b7c23298a31006d0ffb4eea0 Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Sun, 17 Nov 2024 20:26:30 -0500 Subject: [PATCH 2/5] check std::filesystem availability --- CMakeLists.txt | 18 +++++++++++++++++- tools/CMakeLists.txt | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 496988f..5cddbb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,23 @@ pkg_check_modules(ZSTD REQUIRED IMPORTED_TARGET "libzstd") find_package(Fcitx5Utils REQUIRED) include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake") -find_package(Boost 1.61 REQUIRED COMPONENTS iostreams OPTIONAL_COMPONENTS filesystem) +include(CheckCXXSourceCompiles) +check_cxx_source_compiles(" +#include +int main() { + return std::filesystem::is_regular_file(\"/\") ? 0 : 1; +} +" HAS_STD_FILESYSTEM) + +if (HAS_STD_FILESYSTEM) + set(BOOST_FILESYSTEM) + set(BOOST_FILESYSTEM_LIB) +else() + set(BOOST_FILESYSTEM filesystem) + set(BOOST_FILESYSTEM_LIB Boost::filesystem) +endif() + +find_package(Boost 1.61 REQUIRED COMPONENTS iostreams ${BOOST_FILESYSTEM}) set(LIBIME_INSTALL_PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/libime") set(LIBIME_INSTALL_LIBDATADIR "${CMAKE_INSTALL_FULL_LIBDIR}/libime") diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 3a21af6..2c2e411 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -26,12 +26,12 @@ add_executable(LibIME::tabledict ALIAS libime_tabledict) if (NOT APPLE) add_executable(libime_migrate_fcitx4_table libime_migrate_fcitx4_table.cpp) -target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams Boost::filesystem) +target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams ${BOOST_FILESYSTEM_LIB}) install(TARGETS libime_migrate_fcitx4_table DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_table ALIAS libime_migrate_fcitx4_table) add_executable(libime_migrate_fcitx4_pinyin libime_migrate_fcitx4_pinyin.cpp) -target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams Boost::filesystem) +target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams ${BOOST_FILESYSTEM_LIB}) install(TARGETS libime_migrate_fcitx4_pinyin DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_pinyin ALIAS libime_migrate_fcitx4_pinyin) endif() From 111bf5e35f0b55a9c317aae469486d8622e8e375 Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Sun, 17 Nov 2024 23:43:32 -0500 Subject: [PATCH 3/5] clang has a low __GNUC__ --- CMakeLists.txt | 1 + tools/libime_migrate_fcitx4_pinyin.cpp | 6 +++--- tools/libime_migrate_fcitx4_table.cpp | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cddbb7..ae6cbd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ if (HAS_STD_FILESYSTEM) set(BOOST_FILESYSTEM) set(BOOST_FILESYSTEM_LIB) else() + add_definitions(-DUSE_BOOST_FILESYSTEM) set(BOOST_FILESYSTEM filesystem) set(BOOST_FILESYSTEM_LIB Boost::filesystem) endif() diff --git a/tools/libime_migrate_fcitx4_pinyin.cpp b/tools/libime_migrate_fcitx4_pinyin.cpp index 54ee35c..35f9a2c 100644 --- a/tools/libime_migrate_fcitx4_pinyin.cpp +++ b/tools/libime_migrate_fcitx4_pinyin.cpp @@ -18,7 +18,7 @@ #include #include -#if __GNUC__ <= 8 +#ifdef USE_BOOST_FILESYSTEM #include #else #include @@ -347,7 +347,7 @@ int main(int argc, char *argv[]) { if (dictFile[0] == '/') { outputDictFile = dictFile; } else { -#if __GNUC__ <= 8 +#ifdef USE_BOOST_FILESYSTEM outputDictFile = boost::filesystem::absolute(dictFile).string(); #else outputDictFile = std::filesystem::absolute(dictFile); @@ -373,7 +373,7 @@ int main(int argc, char *argv[]) { if (historyFile[0] == '/') { outputHistoryFile = historyFile; } else { -#if __GNUC__ <= 8 +#ifdef USE_BOOST_FILESYSTEM outputHistoryFile = boost::filesystem::absolute(historyFile).string(); #else diff --git a/tools/libime_migrate_fcitx4_table.cpp b/tools/libime_migrate_fcitx4_table.cpp index ea78869..ff5ae21 100644 --- a/tools/libime_migrate_fcitx4_table.cpp +++ b/tools/libime_migrate_fcitx4_table.cpp @@ -17,7 +17,7 @@ #include #include -#if __GNUC__ <= 8 +#ifdef USE_BOOST_FILESYSTEM #include #else #include @@ -82,7 +82,7 @@ struct MigrationCommonOption { return stringutils::joinPath("table", path); } -#if __GNUC__ <= 8 +#ifdef USE_BOOST_FILESYSTEM return boost::filesystem::absolute(path).string(); #else return std::filesystem::absolute(path); From 4cebf580d7414b5ee794608de3e09f38b0c870df Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Mon, 18 Nov 2024 00:24:18 -0500 Subject: [PATCH 4/5] chore --- CMakeLists.txt | 2 +- tools/CMakeLists.txt | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae6cbd3..cec340d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ include(CheckCXXSourceCompiles) check_cxx_source_compiles(" #include int main() { - return std::filesystem::is_regular_file(\"/\") ? 0 : 1; + return std::filesystem::absolute(\".\").string()[0] == '/'; } " HAS_STD_FILESYSTEM) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 2c2e411..08a0189 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -24,7 +24,6 @@ target_link_libraries(libime_tabledict LibIME::Table) install(TARGETS libime_tabledict DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::tabledict ALIAS libime_tabledict) -if (NOT APPLE) add_executable(libime_migrate_fcitx4_table libime_migrate_fcitx4_table.cpp) target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams ${BOOST_FILESYSTEM_LIB}) install(TARGETS libime_migrate_fcitx4_table DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) @@ -34,4 +33,3 @@ add_executable(libime_migrate_fcitx4_pinyin libime_migrate_fcitx4_pinyin.cpp) target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams ${BOOST_FILESYSTEM_LIB}) install(TARGETS libime_migrate_fcitx4_pinyin DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_pinyin ALIAS libime_migrate_fcitx4_pinyin) -endif() From 236a140b445e1f9cc1b1d2df27bf03abd0aa425b Mon Sep 17 00:00:00 2001 From: Weng Xuetian Date: Mon, 18 Nov 2024 10:08:50 -0800 Subject: [PATCH 5/5] Clean up --- CMakeLists.txt | 11 +++-------- tools/CMakeLists.txt | 11 +++++++++-- tools/filesystem_helper.cpp | 26 ++++++++++++++++++++++++++ tools/filesystem_helper.h | 19 +++++++++++++++++++ tools/libime_migrate_fcitx4_pinyin.cpp | 20 +++----------------- tools/libime_migrate_fcitx4_table.cpp | 13 ++----------- 6 files changed, 62 insertions(+), 38 deletions(-) create mode 100644 tools/filesystem_helper.cpp create mode 100644 tools/filesystem_helper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cec340d..79877f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,16 +43,11 @@ int main() { } " HAS_STD_FILESYSTEM) -if (HAS_STD_FILESYSTEM) - set(BOOST_FILESYSTEM) - set(BOOST_FILESYSTEM_LIB) -else() - add_definitions(-DUSE_BOOST_FILESYSTEM) - set(BOOST_FILESYSTEM filesystem) - set(BOOST_FILESYSTEM_LIB Boost::filesystem) +find_package(Boost 1.61 REQUIRED COMPONENTS iostreams) +if (NOT HAS_STD_FILESYSTEM) + find_package(Boost REQUIRED COMPONENTS filesystem) endif() -find_package(Boost 1.61 REQUIRED COMPONENTS iostreams ${BOOST_FILESYSTEM}) set(LIBIME_INSTALL_PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/libime") set(LIBIME_INSTALL_LIBDATADIR "${CMAKE_INSTALL_FULL_LIBDIR}/libime") diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 08a0189..e5ecf4b 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -24,12 +24,19 @@ target_link_libraries(libime_tabledict LibIME::Table) install(TARGETS libime_tabledict DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::tabledict ALIAS libime_tabledict) +add_library(filesystem_helper filesystem_helper.cpp) +if (HAS_STD_FILESYSTEM) + target_compile_definitions(filesystem_helper PRIVATE -DHAS_STD_FILESYSTEM) +else() + target_link_libraries(filesystem_helper PUBLIC Boost::filesystem) +endif() + add_executable(libime_migrate_fcitx4_table libime_migrate_fcitx4_table.cpp) -target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams ${BOOST_FILESYSTEM_LIB}) +target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams filesystem_helper) install(TARGETS libime_migrate_fcitx4_table DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_table ALIAS libime_migrate_fcitx4_table) add_executable(libime_migrate_fcitx4_pinyin libime_migrate_fcitx4_pinyin.cpp) -target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams ${BOOST_FILESYSTEM_LIB}) +target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams filesystem_helper) install(TARGETS libime_migrate_fcitx4_pinyin DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_pinyin ALIAS libime_migrate_fcitx4_pinyin) diff --git a/tools/filesystem_helper.cpp b/tools/filesystem_helper.cpp new file mode 100644 index 0000000..4a3d9b9 --- /dev/null +++ b/tools/filesystem_helper.cpp @@ -0,0 +1,26 @@ +/* + * SPDX-FileCopyrightText: 2024~2024 CSSlayer + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include +#include + +#ifdef HAS_STD_FILESYSTEM +#include +#else +#include +#endif + +namespace libime { + +std::string absolutePath(const std::string &path) { +#ifdef HAS_STD_FILESYSTEM + return std::filesystem::absolute(path); +#else + return boost::filesystem::absolute(path).string(); +#endif +} + +} // namespace libime diff --git a/tools/filesystem_helper.h b/tools/filesystem_helper.h new file mode 100644 index 0000000..fa02206 --- /dev/null +++ b/tools/filesystem_helper.h @@ -0,0 +1,19 @@ +/* + * SPDX-FileCopyrightText: 2024~2024 CSSlayer + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#ifndef _TOOLS_FILESYSTEM_HELPER_H_ +#define _TOOLS_FILESYSTEM_HELPER_H_ + +#include +#include + +namespace libime { + +std::string absolutePath(const std::string &path); + +} + +#endif diff --git a/tools/libime_migrate_fcitx4_pinyin.cpp b/tools/libime_migrate_fcitx4_pinyin.cpp index 35f9a2c..ebc3197 100644 --- a/tools/libime_migrate_fcitx4_pinyin.cpp +++ b/tools/libime_migrate_fcitx4_pinyin.cpp @@ -5,6 +5,7 @@ * */ +#include "filesystem_helper.h" #include "libime/core/historybigram.h" #include "libime/core/utils.h" #include "libime/core/utils_p.h" @@ -18,12 +19,6 @@ #include #include -#ifdef USE_BOOST_FILESYSTEM -#include -#else -#include -#endif - static const std::array PYFA = { "AA", "AB", "AC", "AD", "AE", "AF", "AH", "AI", "AJ", "AU", "AV", "AW", "AX", "AY", "AZ", "Aa", "Ac", "Ad", "Ae", "BA", "BB", "BC", "BD", "BE", @@ -347,11 +342,7 @@ int main(int argc, char *argv[]) { if (dictFile[0] == '/') { outputDictFile = dictFile; } else { -#ifdef USE_BOOST_FILESYSTEM - outputDictFile = boost::filesystem::absolute(dictFile).string(); -#else - outputDictFile = std::filesystem::absolute(dictFile); -#endif + outputDictFile = absolutePath(dictFile); } } StandardPath::global().safeSave( @@ -373,12 +364,7 @@ int main(int argc, char *argv[]) { if (historyFile[0] == '/') { outputHistoryFile = historyFile; } else { -#ifdef USE_BOOST_FILESYSTEM - outputHistoryFile = - boost::filesystem::absolute(historyFile).string(); -#else - outputHistoryFile = std::filesystem::absolute(historyFile); -#endif + outputHistoryFile = absolutePath(historyFile); } } StandardPath::global().safeSave( diff --git a/tools/libime_migrate_fcitx4_table.cpp b/tools/libime_migrate_fcitx4_table.cpp index ff5ae21..11c6286 100644 --- a/tools/libime_migrate_fcitx4_table.cpp +++ b/tools/libime_migrate_fcitx4_table.cpp @@ -6,6 +6,7 @@ * */ #include "config.h" +#include "filesystem_helper.h" #include "libime/core/historybigram.h" #include "libime/core/utils.h" #include "libime/core/utils_p.h" @@ -17,12 +18,6 @@ #include #include -#ifdef USE_BOOST_FILESYSTEM -#include -#else -#include -#endif - using namespace libime; using namespace fcitx; @@ -82,11 +77,7 @@ struct MigrationCommonOption { return stringutils::joinPath("table", path); } -#ifdef USE_BOOST_FILESYSTEM - return boost::filesystem::absolute(path).string(); -#else - return std::filesystem::absolute(path); -#endif + return absolutePath(path); } return path; }