Remove stdc++fs requirement by using an external library

This commit is contained in:
Jordon Brooks 2024-06-06 19:20:29 +01:00
parent 7f0fa0b90d
commit a7a80571ea
Signed by: jordon
GPG key ID: DBD9758CD53E786A
3 changed files with 55 additions and 34 deletions

3
.gitattributes vendored
View file

@ -37,3 +37,6 @@
*.exe binary *.exe binary
*.out binary *.out binary
*.app binary *.app binary
# Linux shell files must always be LF
*.sh text eol=lf

View file

@ -28,6 +28,18 @@ FetchContent_MakeAvailable(fmt)
set_target_properties(fmt PROPERTIES FOLDER External) set_target_properties(fmt PROPERTIES FOLDER External)
set_target_properties(fmt PROPERTIES POSITION_INDEPENDENT_CODE TRUE) set_target_properties(fmt PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
# Fetch ghc library
FetchContent_Declare(
ghc_filesystem
GIT_REPOSITORY https://github.com/gulrak/filesystem.git
GIT_TAG v1.5.14 # Specify the desired version of ghc
)
FetchContent_MakeAvailable(ghc_filesystem)
set_target_properties(ghc_filesystem PROPERTIES FOLDER External)
set_target_properties(ghc_filesystem PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
# Find the current Git branch and the last commit timestamp # Find the current Git branch and the last commit timestamp
find_package(Git QUIET) find_package(Git QUIET)
if(GIT_FOUND) if(GIT_FOUND)
@ -199,35 +211,39 @@ set_target_properties(HarmonyLinkLibStatic PROPERTIES OUTPUT_NAME "HarmonyLinkLi
target_link_libraries(HarmonyLinkLibStatic PRIVATE fmt::fmt-header-only) target_link_libraries(HarmonyLinkLibStatic PRIVATE fmt::fmt-header-only)
target_link_libraries(HarmonyLinkLibShared PRIVATE fmt::fmt-header-only) target_link_libraries(HarmonyLinkLibShared PRIVATE fmt::fmt-header-only)
# Link ghc to HarmonyLinkLib
target_link_libraries(HarmonyLinkLibStatic PRIVATE ghc_filesystem)
target_link_libraries(HarmonyLinkLibShared PRIVATE ghc_filesystem)
# Determine the compiler and set appropriate flags for libc++ # Determine the compiler and set appropriate flags for libc++
if (UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") #if (UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Use libc++ instead of libstdc++ with Clang # # Use libc++ instead of libstdc++ with Clang
target_compile_options(HarmonyLinkLibStatic PRIVATE -stdlib=libc++) # target_compile_options(HarmonyLinkLibStatic PRIVATE -stdlib=libc++)
target_compile_options(HarmonyLinkLibShared PRIVATE -stdlib=libc++) # target_compile_options(HarmonyLinkLibShared PRIVATE -stdlib=libc++)
target_link_options(HarmonyLinkLibStatic PRIVATE -stdlib=libc++) # target_link_options(HarmonyLinkLibStatic PRIVATE -stdlib=libc++)
target_link_options(HarmonyLinkLibShared PRIVATE -stdlib=libc++) # target_link_options(HarmonyLinkLibShared PRIVATE -stdlib=libc++)
#
# Ensure the proper include paths for libc++ # # Ensure the proper include paths for libc++
target_include_directories(HarmonyLinkLibStatic SYSTEM PRIVATE # target_include_directories(HarmonyLinkLibStatic SYSTEM PRIVATE
/usr/include/c++/v1 # /usr/include/c++/v1
/usr/local/include/c++/v1 # /usr/local/include/c++/v1
/usr/include # /usr/include
) # )
target_include_directories(HarmonyLinkLibShared SYSTEM PRIVATE # target_include_directories(HarmonyLinkLibShared SYSTEM PRIVATE
/usr/include/c++/v1 # /usr/include/c++/v1
/usr/local/include/c++/v1 # /usr/local/include/c++/v1
/usr/include # /usr/include
) # )
#
# Link against the libc++ library and the filesystem library # # Link against the libc++ library and the filesystem library
target_link_libraries(HarmonyLinkLibStatic PRIVATE c++ c++abi c++experimental) # target_link_libraries(HarmonyLinkLibStatic PRIVATE c++ c++abi c++experimental)
target_link_libraries(HarmonyLinkLibShared PRIVATE c++ c++abi c++experimental) # target_link_libraries(HarmonyLinkLibShared PRIVATE c++ c++abi c++experimental)
elseif (UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "GNU") #elseif (UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
# Use libstdc++ with GCC # # Use libstdc++ with GCC
target_link_options(HarmonyLinkLibStatic PRIVATE -static-libgcc -static-libstdc++) # target_link_options(HarmonyLinkLibStatic PRIVATE -static-libgcc -static-libstdc++)
target_link_options(HarmonyLinkLibShared PRIVATE -static-libgcc -static-libstdc++) # target_link_options(HarmonyLinkLibShared PRIVATE -static-libgcc -static-libstdc++)
#
# Link against the libstdc++ filesystem library if necessary # # Link against the libstdc++ filesystem library if necessary
target_link_libraries(HarmonyLinkLibStatic PRIVATE stdc++fs) # target_link_libraries(HarmonyLinkLibStatic PRIVATE stdc++fs)
target_link_libraries(HarmonyLinkLibShared PRIVATE stdc++fs) # target_link_libraries(HarmonyLinkLibShared PRIVATE stdc++fs)
endif() #endif()

View file

@ -18,12 +18,14 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
#include <filesystem> #include <ghc/filesystem.hpp>
#ifdef BUILD_WINDOWS #ifdef BUILD_WINDOWS
#include <windows.h> #include <windows.h>
#endif #endif
namespace fs = ghc::filesystem;
namespace HarmonyLinkLib namespace HarmonyLinkLib
{ {
bool force_detect_wine = false; bool force_detect_wine = false;
@ -38,7 +40,7 @@ namespace HarmonyLinkLib
FBattery result = {}; FBattery result = {};
for (int i = 0; i <= 9; ++i) { for (int i = 0; i <= 9; ++i) {
if (std::string bat_path = append + "/sys/class/power_supply/BAT" + std::to_string(i); std::filesystem::exists(bat_path)) { if (std::string bat_path = append + "/sys/class/power_supply/BAT" + std::to_string(i); fs::exists(bat_path)) {
result.has_battery = true; result.has_battery = true;
std::ifstream status_file(bat_path + "/status"); std::ifstream status_file(bat_path + "/status");