Skip to content

Commit 003f74d

Browse files
eagleoflqjwengxt
andauthored
make boost::filesystem optional (#81)
__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]>
1 parent 2f5fb82 commit 003f74d

6 files changed

+77
-32
lines changed

Diff for: CMakeLists.txt

+18-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ option(ENABLE_COVERAGE "Build the project with gcov support (Need ENABLE_TEST=On
1313
set(GCOV_TOOL "gcov" CACHE STRING "Path to gcov tool used by coverage.")
1414
option(ENABLE_DOC "Build doxygen" Off)
1515
option(ENABLE_DATA "Build data" On)
16+
option(ENABLE_TOOLS "Build tools" On)
1617

1718
#########################################
1819
# Dependency
@@ -34,7 +35,19 @@ pkg_check_modules(ZSTD REQUIRED IMPORTED_TARGET "libzstd")
3435
find_package(Fcitx5Utils REQUIRED)
3536
include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake")
3637

37-
find_package(Boost 1.61 REQUIRED COMPONENTS iostreams filesystem)
38+
include(CheckCXXSourceCompiles)
39+
check_cxx_source_compiles("
40+
#include <filesystem>
41+
int main() {
42+
return std::filesystem::absolute(\".\").string()[0] == '/';
43+
}
44+
" HAS_STD_FILESYSTEM)
45+
46+
find_package(Boost 1.61 REQUIRED COMPONENTS iostreams)
47+
if (NOT HAS_STD_FILESYSTEM)
48+
find_package(Boost REQUIRED COMPONENTS filesystem)
49+
endif()
50+
3851
set(LIBIME_INSTALL_PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/libime")
3952
set(LIBIME_INSTALL_LIBDATADIR "${CMAKE_INSTALL_FULL_LIBDIR}/libime")
4053

@@ -56,7 +69,10 @@ if(ENABLE_TEST)
5669
endif()
5770

5871
add_subdirectory(src)
59-
add_subdirectory(tools)
72+
73+
if (ENABLE_TOOLS)
74+
add_subdirectory(tools)
75+
endif()
6076

6177
if (ENABLE_DATA)
6278
add_subdirectory(data)

Diff for: tools/CMakeLists.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ target_link_libraries(libime_tabledict LibIME::Table)
2424
install(TARGETS libime_tabledict DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools)
2525
add_executable(LibIME::tabledict ALIAS libime_tabledict)
2626

27+
add_library(filesystem_helper filesystem_helper.cpp)
28+
if (HAS_STD_FILESYSTEM)
29+
target_compile_definitions(filesystem_helper PRIVATE -DHAS_STD_FILESYSTEM)
30+
else()
31+
target_link_libraries(filesystem_helper PUBLIC Boost::filesystem)
32+
endif()
33+
2734
add_executable(libime_migrate_fcitx4_table libime_migrate_fcitx4_table.cpp)
28-
target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams Boost::filesystem)
35+
target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams filesystem_helper)
2936
install(TARGETS libime_migrate_fcitx4_table DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools)
3037
add_executable(LibIME::migrate_fcitx4_table ALIAS libime_migrate_fcitx4_table)
3138

3239
add_executable(libime_migrate_fcitx4_pinyin libime_migrate_fcitx4_pinyin.cpp)
33-
target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams Boost::filesystem)
40+
target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams filesystem_helper)
3441
install(TARGETS libime_migrate_fcitx4_pinyin DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools)
3542
add_executable(LibIME::migrate_fcitx4_pinyin ALIAS libime_migrate_fcitx4_pinyin)

Diff for: tools/filesystem_helper.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024~2024 CSSlayer <[email protected]>
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
#include <string>
8+
#include <string_view>
9+
10+
#ifdef HAS_STD_FILESYSTEM
11+
#include <filesystem>
12+
#else
13+
#include <boost/filesystem.hpp>
14+
#endif
15+
16+
namespace libime {
17+
18+
std::string absolutePath(const std::string &path) {
19+
#ifdef HAS_STD_FILESYSTEM
20+
return std::filesystem::absolute(path);
21+
#else
22+
return boost::filesystem::absolute(path).string();
23+
#endif
24+
}
25+
26+
} // namespace libime

Diff for: tools/filesystem_helper.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024~2024 CSSlayer <[email protected]>
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
#ifndef _TOOLS_FILESYSTEM_HELPER_H_
8+
#define _TOOLS_FILESYSTEM_HELPER_H_
9+
10+
#include <string>
11+
#include <string_view>
12+
13+
namespace libime {
14+
15+
std::string absolutePath(const std::string &path);
16+
17+
}
18+
19+
#endif

Diff for: tools/libime_migrate_fcitx4_pinyin.cpp

+3-17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
*/
77

8+
#include "filesystem_helper.h"
89
#include "libime/core/historybigram.h"
910
#include "libime/core/utils.h"
1011
#include "libime/core/utils_p.h"
@@ -18,12 +19,6 @@
1819
#include <string_view>
1920
#include <unordered_map>
2021

21-
#if __GNUC__ <= 8
22-
#include <boost/filesystem.hpp>
23-
#else
24-
#include <filesystem>
25-
#endif
26-
2722
static const std::array<std::string, 412> PYFA = {
2823
"AA", "AB", "AC", "AD", "AE", "AF", "AH", "AI", "AJ", "AU", "AV", "AW",
2924
"AX", "AY", "AZ", "Aa", "Ac", "Ad", "Ae", "BA", "BB", "BC", "BD", "BE",
@@ -347,11 +342,7 @@ int main(int argc, char *argv[]) {
347342
if (dictFile[0] == '/') {
348343
outputDictFile = dictFile;
349344
} else {
350-
#if __GNUC__ <= 8
351-
outputDictFile = boost::filesystem::absolute(dictFile).string();
352-
#else
353-
outputDictFile = std::filesystem::absolute(dictFile);
354-
#endif
345+
outputDictFile = absolutePath(dictFile);
355346
}
356347
}
357348
StandardPath::global().safeSave(
@@ -373,12 +364,7 @@ int main(int argc, char *argv[]) {
373364
if (historyFile[0] == '/') {
374365
outputHistoryFile = historyFile;
375366
} else {
376-
#if __GNUC__ <= 8
377-
outputHistoryFile =
378-
boost::filesystem::absolute(historyFile).string();
379-
#else
380-
outputHistoryFile = std::filesystem::absolute(historyFile);
381-
#endif
367+
outputHistoryFile = absolutePath(historyFile);
382368
}
383369
}
384370
StandardPath::global().safeSave(

Diff for: tools/libime_migrate_fcitx4_table.cpp

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88
#include "config.h"
9+
#include "filesystem_helper.h"
910
#include "libime/core/historybigram.h"
1011
#include "libime/core/utils.h"
1112
#include "libime/core/utils_p.h"
@@ -17,12 +18,6 @@
1718
#include <fcntl.h>
1819
#include <sstream>
1920

20-
#if __GNUC__ <= 8
21-
#include <boost/filesystem.hpp>
22-
#else
23-
#include <filesystem>
24-
#endif
25-
2621
using namespace libime;
2722
using namespace fcitx;
2823

@@ -82,11 +77,7 @@ struct MigrationCommonOption {
8277
return stringutils::joinPath("table", path);
8378
}
8479

85-
#if __GNUC__ <= 8
86-
return boost::filesystem::absolute(path).string();
87-
#else
88-
return std::filesystem::absolute(path);
89-
#endif
80+
return absolutePath(path);
9081
}
9182
return path;
9283
}

0 commit comments

Comments
 (0)