Skip to content

Commit d577667

Browse files
committed
fix(compat): Add drop-in std::filesystem header for older systems
Signed-off-by: Steffen Vogel <steffen.vogel@opal-rt.com>
1 parent 864f8b5 commit d577667

File tree

26 files changed

+6219
-84
lines changed

26 files changed

+6219
-84
lines changed

CMakeLists.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,16 @@ find_package(Lua)
7676
find_package(LibDataChannel)
7777
find_package(re)
7878
find_package(OpenDSSC)
79+
find_package(FileSystem)
7980

8081
# Check for tools
82+
find_program(PROTOBUFC_COMPILER NAMES protoc-c)
83+
find_program(PROTOBUF_COMPILER NAMES protoc)
8184
find_program(PASTE NAMES paste)
8285
if(NOT PASTE)
8386
message(SEND_ERROR "GNU paste is missing. Please install coreutils")
8487
endif()
8588

86-
# Check programs
87-
find_program(PROTOBUFC_COMPILER NAMES protoc-c)
88-
find_program(PROTOBUF_COMPILER NAMES protoc)
89-
90-
# Build without any GPL-code
91-
option(WITHOUT_GPL "Build VILLASnode without any GPL code" OFF)
9289

9390
set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/usr/local/lib/pkgconfig:/usr/local/lib64/pkgconfig:/usr/local/share/pkgconfig:/usr/lib64/pkgconfig")
9491

@@ -157,7 +154,15 @@ else()
157154
set(FOUND_FPGA_SUBMODULES OFF)
158155
endif()
159156

157+
if(CXX17_FILESYSTEM)
158+
set(STDCXX_FS_NOT_FOUND OFF)
159+
else()
160+
set(STDCXX_FS_NOT_FOUND ON)
161+
endif()
162+
160163
# Build options
164+
cmake_dependent_option(WITHOUT_GPL "Build VILLASnode without any GPL code" OFF "" ON)
165+
cmake_dependent_option(WITH_GHC_FS "Build using ghc::filesystem, a drop in replacement for std::filesystem" ON "STDCXX_FS_NOT_FOUND" OFF)
161166
cmake_dependent_option(WITH_DEFAULTS "Defaults for non required build options" ON "" OFF)
162167

163168
cmake_dependent_option(WITH_API "Build with remote control API" "${WITH_DEFAULTS}" "" OFF)
@@ -206,7 +211,7 @@ cmake_dependent_option(WITH_NODE_WEBSOCKET "Build with websocket node-type"
206211
cmake_dependent_option(WITH_NODE_ZEROMQ "Build with zeromq node-type" "${WITH_DEFAULTS}" "LIBZMQ_FOUND; NOT WITHOUT_GPL" OFF)
207212
cmake_dependent_option(WITH_NODE_OPENDSS "Build with opendss node-type" "${WITH_DEFAULTS}" "OpenDSSC_FOUND" OFF)
208213

209-
# set a default for the build type
214+
# Set a default for the build type
210215
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
211216
set(CMAKE_BUILD_TYPE "Debug")
212217
endif()
@@ -227,6 +232,11 @@ else()
227232
add_compile_options(-Wno-error)
228233
endif()
229234

235+
if(WITH_GHC_FS)
236+
find_package(ghc_filesystem 1.5.14 REQUIRED)
237+
include_directories($<TARGET_PROPERTY:ghcFilesystem::ghc_filesystem,INTERFACE_INCLUDE_DIRECTORIES>)
238+
endif()
239+
230240
# Get version info and buildid from Git
231241
GetVersion(${PROJECT_SOURCE_DIR} "CMAKE_PROJECT")
232242

cmake/FindFileSystem.cmake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# CMakeLists.txt.
2+
#
3+
# Author: Steffen Vogel <post@steffenvogel.de>
4+
# SPDX-FileCopyrightText: 2025 Steffen Vogel <steffen.vogel@opal-rt.com>
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
set(FILESYSTEM_TEST_CODE "
8+
#include <filesystem>
9+
10+
int main(void) {
11+
return std::filesystem::is_regular_file(\"/\") ? 0 : 1;
12+
}
13+
")
14+
15+
include(CMakePushCheckState)
16+
17+
cmake_push_check_state(RESET)
18+
check_cxx_source_compiles("${FILESYSTEM_TEST_CODE}" CXX17_FILESYSTEM)
19+
cmake_pop_check_state()
20+
21+
unset(FILESYSTEM_TEST_CODE)

common/include/villas/config.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353

5454
// Library features
5555
#cmakedefine FMT_LEGACY_OSTREAM_FORMATTER
56+
#cmakedefine WITH_GHC_FS
5657

5758
// clang-format on

common/include/villas/fs.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* std::filesystem compatability
2+
*
3+
* SPDX-FileCopyrightText: 2025, ghc::filesystem Authors
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <villas/config.hpp>
8+
9+
#ifdef WITH_GHC_FS
10+
#include <ghc/filesystem.hpp>
11+
namespace fs = ghc::filesystem;
12+
#else
13+
#include <filesystem>
14+
namespace fs = std::filesystem;
15+
#endif

common/include/villas/kernel/devices/device.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#pragma once
1010

11-
#include <filesystem>
1211
#include <optional>
1312

13+
#include <villas/fs.hpp>
1414
#include <villas/kernel/devices/driver.hpp>
1515

1616
namespace villas {
@@ -24,8 +24,8 @@ class Device {
2424
virtual std::optional<std::unique_ptr<Driver>> driver() const = 0;
2525
virtual std::optional<int> iommu_group() const = 0;
2626
virtual std::string name() const = 0;
27-
virtual std::filesystem::path override_path() const = 0;
28-
virtual std::filesystem::path path() const = 0;
27+
virtual fs::path override_path() const = 0;
28+
virtual fs::path path() const = 0;
2929
virtual void probe() const = 0;
3030
};
3131

common/include/villas/kernel/devices/ip_device.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#pragma once
1010

11-
#include <filesystem>
1211
#include <vector>
1312

13+
#include <villas/fs.hpp>
1414
#include <villas/kernel/devices/platform_device.hpp>
1515

1616
namespace villas {
@@ -19,21 +19,21 @@ namespace devices {
1919

2020
class IpDevice : public PlatformDevice {
2121
public:
22-
static IpDevice from(const std::filesystem::path unsafe_path);
23-
static bool is_path_valid(const std::filesystem::path unsafe_path);
22+
static IpDevice from(const fs::path unsafe_path);
23+
static bool is_path_valid(const fs::path unsafe_path);
2424

2525
private:
2626
IpDevice() = delete;
2727
IpDevice(
28-
const std::filesystem::path valid_path) //! Dont allow unvalidated paths
28+
const fs::path valid_path) //! Dont allow unvalidated paths
2929
: PlatformDevice(valid_path){};
3030

3131
public:
3232
size_t addr() const;
3333
std::string ip_name() const;
3434

3535
static std::vector<villas::kernel::devices::IpDevice>
36-
from_directory(std::filesystem::path devices_directory);
36+
from_directory(fs::path devices_directory);
3737
};
3838

3939
} // namespace devices

common/include/villas/kernel/devices/linux_driver.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#pragma once
1010

11-
#include <filesystem>
1211
#include <fstream>
1312
#include <iostream>
1413

14+
#include <villas/fs.hpp>
1515
#include <villas/kernel/devices/driver.hpp>
1616

1717
namespace villas {
@@ -24,20 +24,20 @@ class LinuxDriver : public Driver {
2424
static constexpr char UNBIND_DEFAULT[] = "unbind";
2525

2626
public:
27-
const std::filesystem::path path;
27+
const fs::path path;
2828

2929
private:
30-
const std::filesystem::path bind_path;
31-
const std::filesystem::path unbind_path;
30+
const fs::path bind_path;
31+
const fs::path unbind_path;
3232

3333
public:
34-
LinuxDriver(const std::filesystem::path path)
35-
: LinuxDriver(path, path / std::filesystem::path(BIND_DEFAULT),
36-
path / std::filesystem::path(UNBIND_DEFAULT)){};
34+
LinuxDriver(const fs::path path)
35+
: LinuxDriver(path, path / fs::path(BIND_DEFAULT),
36+
path / fs::path(UNBIND_DEFAULT)){};
3737

38-
LinuxDriver(const std::filesystem::path path,
39-
const std::filesystem::path bind_path,
40-
const std::filesystem::path unbind_path)
38+
LinuxDriver(const fs::path path,
39+
const fs::path bind_path,
40+
const fs::path unbind_path)
4141
: path(path), bind_path(bind_path), unbind_path(unbind_path){};
4242

4343
public:

common/include/villas/kernel/devices/platform_device.hpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
#pragma once
1010

11-
#include <filesystem>
12-
11+
#include <villas/fs.hpp>
1312
#include <villas/kernel/devices/device.hpp>
1413
#include <villas/kernel/devices/driver.hpp>
1514

@@ -23,27 +22,27 @@ class PlatformDevice : public Device {
2322
static constexpr char OVERRIDE_DEFAULT[] = "driver_override";
2423

2524
private:
26-
const std::filesystem::path m_path;
27-
const std::filesystem::path m_probe_path;
28-
const std::filesystem::path m_override_path;
25+
const fs::path m_path;
26+
const fs::path m_probe_path;
27+
const fs::path m_override_path;
2928

3029
public:
31-
PlatformDevice(const std::filesystem::path path)
32-
: PlatformDevice(path, std::filesystem::path(PROBE_DEFAULT),
33-
path / std::filesystem::path(OVERRIDE_DEFAULT)){};
30+
PlatformDevice(const fs::path path)
31+
: PlatformDevice(path, fs::path(PROBE_DEFAULT),
32+
path / fs::path(OVERRIDE_DEFAULT)){};
3433

35-
PlatformDevice(const std::filesystem::path path,
36-
const std::filesystem::path probe_path,
37-
const std::filesystem::path override_path)
34+
PlatformDevice(const fs::path path,
35+
const fs::path probe_path,
36+
const fs::path override_path)
3837
: m_path(path), m_probe_path(probe_path),
3938
m_override_path(override_path){};
4039

4140
// Implement device interface
4241
std::optional<std::unique_ptr<Driver>> driver() const override;
4342
std::optional<int> iommu_group() const override;
4443
std::string name() const override;
45-
std::filesystem::path override_path() const override;
46-
std::filesystem::path path() const override;
44+
fs::path override_path() const override;
45+
fs::path path() const override;
4746
void probe() const override;
4847
};
4948

common/include/villas/utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <cassert>
1212
#include <cstdint>
1313
#include <cstdlib>
14-
#include <filesystem>
1514
#include <list>
1615
#include <string>
1716
#include <vector>
@@ -21,6 +20,7 @@
2120
#include <signal.h>
2221
#include <sys/types.h>
2322

23+
#include <villas/fs.hpp>
2424
#include <villas/config.hpp>
2525

2626
#ifdef __GNUC__
@@ -212,9 +212,9 @@ template <class... Ts> struct overloaded : Ts... {
212212
// Explicit deduction guide (not needed as of C++20)
213213
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
214214

215-
void write_to_file(std::string data, const std::filesystem::path file);
215+
void write_to_file(std::string data, const fs::path file);
216216
std::vector<std::string>
217-
read_names_in_directory(const std::filesystem::path &directory);
217+
read_names_in_directory(const fs::path &directory);
218218

219219
namespace base64 {
220220

common/lib/kernel/devices/device_connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ DeviceConnection DeviceConnection::from(
2727

2828
// Bind the devicetree device to vfio driver
2929
LinuxDriver driver(
30-
std::filesystem::path("/sys/bus/platform/drivers/vfio-platform"));
30+
fs::path("/sys/bus/platform/drivers/vfio-platform"));
3131
driver.attach(device);
3232

3333
// Attach vfio container to the iommu group

0 commit comments

Comments
 (0)