Skip to content

Commit

Permalink
make boost::filesystem optional (#81)
Browse files Browse the repository at this point in the history
__GNUC__ doesn't work as intended on clang, change to another
way to detect the presence of std::filesystem. Also make boost::filesystem
fully optional.

Co-authored-by: Weng Xuetian <[email protected]>
  • Loading branch information
eagleoflqj and wengxt authored Nov 18, 2024
1 parent 2f5fb82 commit 003f74d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +35,19 @@ 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)
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <filesystem>
int main() {
return std::filesystem::absolute(\".\").string()[0] == '/';
}
" HAS_STD_FILESYSTEM)

find_package(Boost 1.61 REQUIRED COMPONENTS iostreams)
if (NOT HAS_STD_FILESYSTEM)
find_package(Boost REQUIRED COMPONENTS filesystem)
endif()

set(LIBIME_INSTALL_PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/libime")
set(LIBIME_INSTALL_LIBDATADIR "${CMAKE_INSTALL_FULL_LIBDIR}/libime")

Expand All @@ -56,7 +69,10 @@ if(ENABLE_TEST)
endif()

add_subdirectory(src)
add_subdirectory(tools)

if (ENABLE_TOOLS)
add_subdirectory(tools)
endif()

if (ENABLE_DATA)
add_subdirectory(data)
Expand Down
11 changes: 9 additions & 2 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
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)
26 changes: 26 additions & 0 deletions tools/filesystem_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2024~2024 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include <string>
#include <string_view>

#ifdef HAS_STD_FILESYSTEM
#include <filesystem>
#else
#include <boost/filesystem.hpp>
#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
19 changes: 19 additions & 0 deletions tools/filesystem_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-FileCopyrightText: 2024~2024 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/

#ifndef _TOOLS_FILESYSTEM_HELPER_H_
#define _TOOLS_FILESYSTEM_HELPER_H_

#include <string>
#include <string_view>

namespace libime {

std::string absolutePath(const std::string &path);

}

#endif
20 changes: 3 additions & 17 deletions tools/libime_migrate_fcitx4_pinyin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/

#include "filesystem_helper.h"
#include "libime/core/historybigram.h"
#include "libime/core/utils.h"
#include "libime/core/utils_p.h"
Expand All @@ -18,12 +19,6 @@
#include <string_view>
#include <unordered_map>

#if __GNUC__ <= 8
#include <boost/filesystem.hpp>
#else
#include <filesystem>
#endif

static const std::array<std::string, 412> 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",
Expand Down Expand Up @@ -347,11 +342,7 @@ int main(int argc, char *argv[]) {
if (dictFile[0] == '/') {
outputDictFile = dictFile;
} else {
#if __GNUC__ <= 8
outputDictFile = boost::filesystem::absolute(dictFile).string();
#else
outputDictFile = std::filesystem::absolute(dictFile);
#endif
outputDictFile = absolutePath(dictFile);
}
}
StandardPath::global().safeSave(
Expand All @@ -373,12 +364,7 @@ int main(int argc, char *argv[]) {
if (historyFile[0] == '/') {
outputHistoryFile = historyFile;
} else {
#if __GNUC__ <= 8
outputHistoryFile =
boost::filesystem::absolute(historyFile).string();
#else
outputHistoryFile = std::filesystem::absolute(historyFile);
#endif
outputHistoryFile = absolutePath(historyFile);
}
}
StandardPath::global().safeSave(
Expand Down
13 changes: 2 additions & 11 deletions tools/libime_migrate_fcitx4_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,12 +18,6 @@
#include <fcntl.h>
#include <sstream>

#if __GNUC__ <= 8
#include <boost/filesystem.hpp>
#else
#include <filesystem>
#endif

using namespace libime;
using namespace fcitx;

Expand Down Expand Up @@ -82,11 +77,7 @@ struct MigrationCommonOption {
return stringutils::joinPath("table", path);
}

#if __GNUC__ <= 8
return boost::filesystem::absolute(path).string();
#else
return std::filesystem::absolute(path);
#endif
return absolutePath(path);
}
return path;
}
Expand Down

0 comments on commit 003f74d

Please sign in to comment.