From fc9ca4c774829b90f6b5fea5f3a274ffcde87df7 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Mon, 12 Oct 2026 16:36:04 +0100 Subject: [PATCH 1/2] Hide std::filesystem functions if not supported by compiler Guard every library use of std::filesystem with __cpp_lib_filesystem, including first so the feature macro is defined on libstdc++ and libc++. Covers get_dsp_legacy, the path overloads of get_dsp, validate_nam_file, detail::load_wav_ir, and the unused includes in dsp.cpp and convnet.h/.cpp. Co-Authored-By: Claude Opus 5.5 (1M context) --- NAM/convnet.cpp | 6 +++++- NAM/convnet.h | 6 +++++- NAM/dsp.cpp | 6 +++++- NAM/dsp.h | 8 +++++++- NAM/get_dsp.cpp | 4 ++++ NAM/get_dsp.h | 5 +++++ NAM/nam_file.cpp | 2 ++ NAM/nam_file.h | 8 +++++++- NAM/wav.cpp | 2 ++ NAM/wav.h | 8 +++++++- 10 files changed, 49 insertions(+), 6 deletions(-) diff --git a/NAM/convnet.cpp b/NAM/convnet.cpp index 329caf61..5cbadcfb 100644 --- a/NAM/convnet.cpp +++ b/NAM/convnet.cpp @@ -1,11 +1,15 @@ #include // std::max_element #include #include // pow, tanh, expf -#include #include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "dsp.h" #include "registry.h" diff --git a/NAM/convnet.h b/NAM/convnet.h index 394389e7..b7627968 100644 --- a/NAM/convnet.h +++ b/NAM/convnet.h @@ -1,11 +1,15 @@ #pragma once -#include #include #include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include diff --git a/NAM/dsp.cpp b/NAM/dsp.cpp index dc6449e9..be841806 100644 --- a/NAM/dsp.cpp +++ b/NAM/dsp.cpp @@ -1,12 +1,16 @@ #include // std::max_element #include // pow, tanh, expf #include -#include #include #include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "dsp.h" #define tanh_impl_ std::tanh diff --git a/NAM/dsp.h b/NAM/dsp.h index d632852e..a887f682 100644 --- a/NAM/dsp.h +++ b/NAM/dsp.h @@ -1,12 +1,16 @@ #pragma once #include -#include #include #include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include @@ -371,12 +375,14 @@ struct dspData /// \param version Config version string to verify void verify_config_version(const std::string version); +#if defined(__cpp_lib_filesystem) /// \brief Legacy loader for directory-style DSPs /// /// Loads models from a directory structure (older format). /// \param dirname Path to the directory containing the model /// \return Unique pointer to a DSP object std::unique_ptr get_dsp_legacy(const std::filesystem::path dirname); +#endif }; // namespace nam #include "linear.h" diff --git a/NAM/get_dsp.cpp b/NAM/get_dsp.cpp index 4e205e36..fbd3777c 100644 --- a/NAM/get_dsp.cpp +++ b/NAM/get_dsp.cpp @@ -156,11 +156,13 @@ void populate_dsp_data(const nlohmann::json& config, dspData& returnedConfig) returnedConfig.expected_sample_rate = nam::get_sample_rate_from_nam_file(config); } +#if defined(__cpp_lib_filesystem) std::unique_ptr get_dsp(const std::filesystem::path config_filename, DspLoadOptions options) { dspData temp; return get_dsp(config_filename, temp, options); } +#endif std::unique_ptr get_dsp(const nlohmann::json& config, DspLoadOptions options) { @@ -168,6 +170,7 @@ std::unique_ptr get_dsp(const nlohmann::json& config, DspLoadOptions option return get_dsp(config, temp, options); } +#if defined(__cpp_lib_filesystem) std::unique_ptr get_dsp(const std::filesystem::path config_filename, dspData& returnedConfig, DspLoadOptions options) { @@ -202,6 +205,7 @@ std::unique_ptr get_dsp(const std::filesystem::path config_filename, dspDat return get_dsp(conf, options); } +#endif std::unique_ptr get_dsp(const nlohmann::json& config, dspData& returnedConfig, DspLoadOptions options) { diff --git a/NAM/get_dsp.h b/NAM/get_dsp.h index 4783d71e..1f80a73b 100644 --- a/NAM/get_dsp.h +++ b/NAM/get_dsp.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "dsp.h" #include "nam_file.h" @@ -77,6 +78,7 @@ struct DspLoadOptions std::optional prewarm = std::nullopt; }; +#if defined(__cpp_lib_filesystem) /// \brief Get NAM from a .nam model or mono/stereo .wav impulse response /// \param config_filename Path to the .nam model or .wav impulse response file /// \param options Loading options @@ -88,6 +90,7 @@ struct DspLoadOptions /// \throws std::runtime_error If a WAV file is invalid, empty, has more than two channels, or uses an unsupported format /// \throws NamFileValidationError If a .nam file cannot be read or does not contain a minimally valid configuration std::unique_ptr get_dsp(const std::filesystem::path config_filename, DspLoadOptions options = DspLoadOptions()); +#endif /// \brief Get NAM from a provided configuration struct /// \param conf DSP data structure containing model configuration and weights @@ -95,6 +98,7 @@ std::unique_ptr get_dsp(const std::filesystem::path config_filename, DspLoa /// \return Unique pointer to a DSP object std::unique_ptr get_dsp(dspData& conf, DspLoadOptions options = DspLoadOptions()); +#if defined(__cpp_lib_filesystem) /// \brief Get NAM from a .nam model or mono/stereo .wav impulse response and store its configuration /// /// Creates an instance of DSP and also returns a dspData struct that holds the data of the model. @@ -110,6 +114,7 @@ std::unique_ptr get_dsp(dspData& conf, DspLoadOptions options = DspLoadOpti /// \throws NamFileValidationError If a .nam file cannot be read or does not contain a minimally valid configuration std::unique_ptr get_dsp(const std::filesystem::path config_filename, dspData& returnedConfig, DspLoadOptions options = DspLoadOptions()); +#endif /// \brief Get NAM from a provided configuration JSON object /// \param config JSON configuration object diff --git a/NAM/nam_file.cpp b/NAM/nam_file.cpp index 069f8ead..d84273b8 100644 --- a/NAM/nam_file.cpp +++ b/NAM/nam_file.cpp @@ -4,6 +4,7 @@ #include #include +#if defined(__cpp_lib_filesystem) namespace nam { nlohmann::json validate_nam_file(const std::filesystem::path& filename) @@ -39,3 +40,4 @@ nlohmann::json validate_nam_file(const std::filesystem::path& filename) return config; } } // namespace nam +#endif diff --git a/NAM/nam_file.h b/NAM/nam_file.h index a4cda049..6370b83e 100644 --- a/NAM/nam_file.h +++ b/NAM/nam_file.h @@ -1,7 +1,11 @@ #pragma once -#include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" @@ -14,9 +18,11 @@ class NamFileValidationError : public std::runtime_error using std::runtime_error::runtime_error; }; +#if defined(__cpp_lib_filesystem) /// \brief Parse and validate a .nam file /// \param filename Path to the .nam file /// \return The parsed model configuration /// \throws NamFileValidationError If the file cannot be read, parsed, or does not contain the minimum required fields nlohmann::json validate_nam_file(const std::filesystem::path& filename); +#endif } // namespace nam diff --git a/NAM/wav.cpp b/NAM/wav.cpp index 66e69afd..32cd23e5 100644 --- a/NAM/wav.cpp +++ b/NAM/wav.cpp @@ -10,6 +10,7 @@ #include #include +#if defined(__cpp_lib_filesystem) namespace nam { namespace detail @@ -142,3 +143,4 @@ std::vector load_wav_ir(const std::filesystem::path& filename, double& sa } } // namespace detail } // namespace nam +#endif diff --git a/NAM/wav.h b/NAM/wav.h index d63f8d43..a12b4ddc 100644 --- a/NAM/wav.h +++ b/NAM/wav.h @@ -1,8 +1,13 @@ #pragma once -#include #include +#include +#if defined(__cpp_lib_filesystem) + #include +#endif + +#if defined(__cpp_lib_filesystem) namespace nam { namespace detail @@ -12,3 +17,4 @@ namespace detail std::vector load_wav_ir(const std::filesystem::path& filename, double& sample_rate, int& out_channels); } // namespace detail } // namespace nam +#endif From 0c36a34bcc50c167b3621b79f4ebdddd5a9d8560 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Tue, 3 Nov 2026 18:37:45 +0000 Subject: [PATCH 2/2] Guard std::filesystem tests with __cpp_lib_filesystem Skip tests that load models from file paths, write temporary .nam files, or use the path overloads of get_dsp and validate_nam_file when the standard library does not provide std::filesystem. Matching calls in run_tests.cpp are guarded the same way. Co-Authored-By: Claude Opus 5.5 --- tools/run_tests.cpp | 16 ++++++++++++++++ tools/test/test_container.cpp | 7 +++++++ tools/test/test_extensible.cpp | 10 +++++++++- tools/test/test_get_dsp.cpp | 12 +++++++++++- tools/test/test_get_dsp_wav.cpp | 8 +++++++- tools/test/test_nam_file.cpp | 8 +++++++- tools/test/test_render_slim.cpp | 8 +++++++- tools/test/test_sequential.cpp | 10 +++++++++- tools/test/test_slimmable_wavenet.cpp | 8 +++++++- 9 files changed, 80 insertions(+), 7 deletions(-) diff --git a/tools/run_tests.cpp b/tools/run_tests.cpp index ee827818..0bab1ed2 100644 --- a/tools/run_tests.cpp +++ b/tools/run_tests.cpp @@ -44,8 +44,10 @@ int main() { std::cout << "Running tests..." << std::endl; +#if defined(__cpp_lib_filesystem) test_get_dsp_wav::test_formats_and_configuration(); test_get_dsp_wav::test_invalid_files(); +#endif // TODO Automatically loop, catch exceptions, log results test_activations::TestFastTanh::test_core_function(); @@ -317,8 +319,10 @@ int main() test_get_dsp::test_gets_input_level(); test_get_dsp::test_gets_output_level(); +#if defined(__cpp_lib_filesystem) test_get_dsp::test_empty_nam_file_throws_validation_error(); test_get_dsp::test_malformed_nam_file_throws_validation_error(); +#endif test_get_dsp::test_null_input_level(); test_get_dsp::test_null_output_level(); test_get_dsp::test_version_patch_one_beyond_supported(); @@ -332,12 +336,16 @@ int main() test_get_dsp::test_get_dsp_prewarm_option_forces_constructor_reset_prewarm(); test_get_dsp::test_get_dsp_with_returned_config_constructs_once(); +#if defined(__cpp_lib_filesystem) test_nam_file::test_accepts_minimum_valid_file(); test_nam_file::test_rejects_non_object_json(); test_nam_file::test_rejects_missing_required_keys(); +#endif // Finally, some end-to-end tests. +#if defined(__cpp_lib_filesystem) test_get_dsp::test_load_and_process_nam_files(); +#endif // Extensibility: external architecture registration and get_dsp (issue #230) test_extensible::run_extensibility_tests(); @@ -352,14 +360,18 @@ int main() test_container::test_container_last_max_value_must_cover_one(); test_container::test_container_unsorted_submodels_throws(); test_container::test_container_sample_rate_mismatch_throws(); +#if defined(__cpp_lib_filesystem) test_container::test_container_load_from_file(); +#endif test_container::test_container_default_is_max_size(); test_container::test_container_reset_only_resets_active_submodel(); test_container::test_container_switch_resets_before_activation(); // Sequential tests test_sequential::test_sequential_loads_canonical_container_envelope(); +#if defined(__cpp_lib_filesystem) test_sequential::test_sequential_loads_from_file_path(); +#endif test_sequential::test_sequential_process_matches_manual_series(); test_sequential::test_sequential_process_is_realtime_safe_after_warmup(); test_sequential::test_sequential_rejects_blocks_larger_than_reset_maximum(); @@ -373,12 +385,15 @@ int main() test_sequential::test_sequential_rejects_channel_mismatch(); // Render --slim tests +#if defined(__cpp_lib_filesystem) test_render_slim::test_slim_changes_output(); test_render_slim::test_slim_rejects_non_slimmable(); test_render_slim::test_slim_boundary_values(); test_render_slim::test_slim_applied_before_processing(); +#endif // SlimmableWavenet tests +#if defined(__cpp_lib_filesystem) test_slimmable_wavenet::test_loads_from_file(); test_slimmable_wavenet::test_implements_slimmable(); test_slimmable_wavenet::test_processes_audio(); @@ -387,6 +402,7 @@ int main() test_slimmable_wavenet::test_default_is_max_size(); test_slimmable_wavenet::test_ratio_mapping(); test_slimmable_wavenet::test_slimmable_breakpoints(); +#endif test_slimmable_wavenet::test_from_json(); test_slimmable_wavenet::test_wavenet_without_slimmable_loads_as_regular(); test_slimmable_wavenet::test_unsupported_method_throws(); diff --git a/tools/test/test_container.cpp b/tools/test/test_container.cpp index c12cdb96..f72e9f9d 100644 --- a/tools/test/test_container.cpp +++ b/tools/test/test_container.cpp @@ -7,6 +7,11 @@ #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" @@ -370,6 +375,7 @@ void test_container_sample_rate_mismatch_throws() std::cerr.rdbuf(originalCerr); } +#if defined(__cpp_lib_filesystem) void test_container_load_from_file() { std::filesystem::path path("example_models/slimmable_container.nam"); @@ -377,6 +383,7 @@ void test_container_load_from_file() assert(dsp != nullptr); process_and_verify(dsp.get(), 3, 64); } +#endif void test_container_default_is_max_size() { diff --git a/tools/test/test_extensible.cpp b/tools/test/test_extensible.cpp index a0e15d9f..1b0a3c46 100644 --- a/tools/test/test_extensible.cpp +++ b/tools/test/test_extensible.cpp @@ -5,10 +5,14 @@ #include #include #include -#include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" @@ -89,6 +93,7 @@ static void test_get_dsp_detects_dummy_architecture_from_json() } } +#if defined(__cpp_lib_filesystem) static void test_get_dsp_detects_dummy_architecture_from_nam_file() { const std::string namContent = R"({ @@ -132,11 +137,14 @@ static void test_get_dsp_detects_dummy_architecture_from_nam_file() std::filesystem::remove(path); } +#endif void run_extensibility_tests() { test_get_dsp_detects_dummy_architecture_from_json(); +#if defined(__cpp_lib_filesystem) test_get_dsp_detects_dummy_architecture_from_nam_file(); +#endif } } // namespace test_extensible diff --git a/tools/test/test_get_dsp.cpp b/tools/test/test_get_dsp.cpp index 538f70c0..ef85ab11 100644 --- a/tools/test/test_get_dsp.cpp +++ b/tools/test/test_get_dsp.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -9,6 +8,11 @@ #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" @@ -55,6 +59,7 @@ nam::dspData _GetConfig(const std::string& configStr = basicConfigStr) namespace { +#if defined(__cpp_lib_filesystem) class TemporaryNamFile { public: @@ -69,6 +74,7 @@ class TemporaryNamFile const std::filesystem::path path; }; +#endif constexpr const char* kConstructorResetArchitecture = "ConstructorResetPrewarmPolicyTest"; int gConstructorResetConstructCount = 0; @@ -111,6 +117,7 @@ nlohmann::json build_constructor_reset_config() } // namespace +#if defined(__cpp_lib_filesystem) void test_empty_nam_file_throws_validation_error() { const TemporaryNamFile file("nam_core_empty_file_test.nam", ""); @@ -144,6 +151,7 @@ void test_malformed_nam_file_throws_validation_error() assert(message.find("parse") != std::string::npos); } } +#endif void test_gets_input_level() { @@ -183,6 +191,7 @@ void test_null_output_level() assert(!dsp->HasOutputLevel()); } +#if defined(__cpp_lib_filesystem) // Helper function to process buffers through a DSP model void process_buffers(nam::DSP* dsp, int num_buffers, int buffer_size) { @@ -259,6 +268,7 @@ void test_load_and_process_nam_files() process_buffers(dsp.get(), num_buffers, buffer_size); } } +#endif // Helper function to create a config string with a specific version std::string createConfigWithVersion(const std::string& version) diff --git a/tools/test/test_get_dsp_wav.cpp b/tools/test/test_get_dsp_wav.cpp index d7f38647..3c9817a7 100644 --- a/tools/test/test_get_dsp_wav.cpp +++ b/tools/test/test_get_dsp_wav.cpp @@ -2,13 +2,18 @@ #include #include #include -#include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "NAM/get_dsp.h" #include "NAM/linear.h" +#if defined(__cpp_lib_filesystem) namespace test_get_dsp_wav { using Bytes = std::vector; @@ -205,3 +210,4 @@ void test_invalid_files() assert(threw); } } // namespace test_get_dsp_wav +#endif diff --git a/tools/test/test_nam_file.cpp b/tools/test/test_nam_file.cpp index 9162e892..4d0c5d60 100644 --- a/tools/test/test_nam_file.cpp +++ b/tools/test/test_nam_file.cpp @@ -1,13 +1,18 @@ #include -#include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" #include "NAM/nam_file.h" +#if defined(__cpp_lib_filesystem) namespace test_nam_file { namespace @@ -80,3 +85,4 @@ void test_rejects_missing_required_keys() } } // namespace test_nam_file +#endif diff --git a/tools/test/test_render_slim.cpp b/tools/test/test_render_slim.cpp index 3f9513a7..a655cd8b 100644 --- a/tools/test/test_render_slim.cpp +++ b/tools/test/test_render_slim.cpp @@ -1,13 +1,18 @@ #include #include -#include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "NAM/dsp.h" #include "NAM/get_dsp.h" #include "NAM/slimmable.h" +#if defined(__cpp_lib_filesystem) namespace test_render_slim { @@ -127,3 +132,4 @@ void test_slim_applied_before_processing() } } // namespace test_render_slim +#endif diff --git a/tools/test/test_sequential.cpp b/tools/test/test_sequential.cpp index 4783a2e6..6388a6c4 100644 --- a/tools/test/test_sequential.cpp +++ b/tools/test/test_sequential.cpp @@ -1,13 +1,17 @@ #include #include #include -#include #include #include #include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" @@ -20,6 +24,7 @@ namespace test_sequential namespace { +#if defined(__cpp_lib_filesystem) class TemporaryNamFile { public: @@ -34,6 +39,7 @@ class TemporaryNamFile const std::filesystem::path path; }; +#endif nlohmann::json make_linear_model(const std::vector& weights, const int receptive_field, const double sample_rate = 48000.0, const int in_channels = 1, @@ -158,6 +164,7 @@ void test_sequential_loads_canonical_container_envelope() assert(dsp->GetExpectedSampleRate() == 48000.0); } +#if defined(__cpp_lib_filesystem) void test_sequential_loads_from_file_path() { const auto model = make_sequential_model({make_linear_model({0.5f}, 1), make_linear_model({-2.0f}, 1)}); @@ -168,6 +175,7 @@ void test_sequential_loads_from_file_path() assert(dsp != nullptr); assert(dsp->GetExpectedSampleRate() == 48000.0); } +#endif void test_sequential_process_matches_manual_series() { diff --git a/tools/test/test_slimmable_wavenet.cpp b/tools/test/test_slimmable_wavenet.cpp index 131ead9f..bc5b8628 100644 --- a/tools/test/test_slimmable_wavenet.cpp +++ b/tools/test/test_slimmable_wavenet.cpp @@ -1,9 +1,13 @@ #include #include -#include #include #include #include +#include + +#if defined(__cpp_lib_filesystem) + #include +#endif #include "json.hpp" @@ -52,6 +56,7 @@ void process_and_verify(nam::DSP* dsp, int num_buffers, int buffer_size) // Tests // ===================================================================== +#if defined(__cpp_lib_filesystem) void test_loads_from_file() { @@ -257,6 +262,7 @@ void test_slimmable_breakpoints() assert(std::abs(breakpoints[0] - (1.0 / 3.0)) < 1e-12); assert(std::abs(breakpoints[1] - (2.0 / 3.0)) < 1e-12); } +#endif void test_from_json() {