Skip to content

Commit

Permalink
filesystem: Manage filesystem selection
Browse files Browse the repository at this point in the history
Moves the selection of the filesystem implementation into the filesystem
library so it is visible to other libraries.
  • Loading branch information
mvf committed Oct 18, 2020
1 parent 4237734 commit c19351d
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 101 deletions.
62 changes: 5 additions & 57 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ endif()
set(SURGE_PRODUCT_DIR ${CMAKE_BINARY_DIR}/surge_products)
file(MAKE_DIRECTORY ${SURGE_PRODUCT_DIR})

add_subdirectory(libs/catch2)

add_library(surge-shared INTERFACE)
add_library(surge-tests INTERFACE)

add_subdirectory(libs/catch2)
add_subdirectory(libs/filesystem)

target_link_libraries(surge-shared INTERFACE surge::filesystem)

# We want to run this once alas, since JUCE needs it even though it is a byproduct of a phase to build the
# JUCE Cmake file list.
execute_process(
Expand Down Expand Up @@ -156,57 +159,6 @@ else()
set( BUILD_LV2 false )
endif()

function(use_fallback_fs)
add_subdirectory(libs/filesystem)
target_link_libraries(surge-shared INTERFACE surge::filesystem)
target_compile_definitions(surge-shared INTERFACE USE_FALLBACK_FILESYSTEM=1)
message(STATUS "Using fallback filesystem")
endfunction()

set(SURGE_DEVEL_FORCE_FALLBACK_FS OFF CACHE BOOL "Force fallback std::filesystem impl (development only)")
mark_as_advanced(FORCE SURGE_DEVEL_FORCE_FALLBACK_FS)
if (${SURGE_DEVEL_FORCE_FALLBACK_FS})
use_fallback_fs()
else()
include(CheckCXXSymbolExists)
CHECK_CXX_SYMBOL_EXISTS(std::filesystem::path::preferred_separator "filesystem" CXX_STD_FS)
if( CXX_STD_FS )
# This is not strictly necessary but since we build with 17 on unix lets check with 17 here
if( UNIX AND NOT APPLE )
set (CMAKE_REQUIRED_FLAGS "-std=c++17")
endif()
target_compile_definitions(surge-shared INTERFACE USE_STD_FILESYSTEM=1)
if( UNIX AND NOT APPLE )
set (CMAKE_REQUIRED_FLAGS "")
endif()
message( STATUS "Using std::filesystem" )
else()
# if that check failed we are on one of the wonky gccs in te 7 and 5 family which need to check this with 11 and link stdc++fs
if( UNIX AND NOT APPLE )
set (CMAKE_REQUIRED_FLAGS "-std=c++11")
set (CMAKE_REQUIRED_LIBRARIES "stdc++fs")
endif()
CHECK_CXX_SYMBOL_EXISTS(std::experimental::filesystem::path::preferred_separator "experimental/filesystem" CXX_EXP_STD_FS)
if( UNIX AND NOT APPLE )
set (CMAKE_REQUIRED_FLAGS "")
set (CMAKE_REQUIRED_LIBRARIES "")
endif()

if( CXX_EXP_STD_FS )
target_compile_definitions(surge-shared INTERFACE USE_STD_EXPERIMENTAL_FILESYSTEM=1)
message( STATUS "Using std::experimental::filesystem" )
else()
CHECK_CXX_SYMBOL_EXISTS(std::experimental::filesystem::path::preferred_separator "filesystem" CXX_EXP_STD_FS_FS_HDR)
if( CXX_EXP_STD_FS_FS_HDR )
target_compile_definitions(surge-shared INTERFACE USE_STD_EXPERIMENTAL_FILESYSTEM_FROM_FILESYSTEM=1)
message( STATUS "Using std::experimental::filesystem but from <filesystem> header" )
else()
use_fallback_fs()
endif()
endif()
endif()
endif()

# Surge features at compile time
if( NOT DEFINED SURGE_EXTRA_FILTERS )
set( SURGE_EXTRA_FILTERS False )
Expand Down Expand Up @@ -565,9 +517,6 @@ elseif( UNIX AND NOT APPLE )

set(OS_LINK_LIBRARIES_NOGUI
pthread
stdc++fs
gcc_s
gcc
dl
-Wl,--no-undefined
)
Expand Down Expand Up @@ -1030,7 +979,6 @@ if( BUILD_HEADLESS )
endif()
target_link_libraries(surge-headless
PRIVATE
stdc++fs
Threads::Threads
)

Expand Down
81 changes: 65 additions & 16 deletions libs/filesystem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
project(filesystem VERSION 0.0.0 LANGUAGES CXX)

add_library(${PROJECT_NAME}
src/directory_iterator.cpp
src/filesystem.cpp
src/path.cpp
src/recursive_directory_iterator.cpp
src/util.h
include/${PROJECT_NAME}/directory_entry.h
include/${PROJECT_NAME}/directory_iterator.h
include/${PROJECT_NAME}/filesystem.h
include/${PROJECT_NAME}/filesystem_error.h
include/${PROJECT_NAME}/path.h
include/${PROJECT_NAME}/recursive_directory_iterator.h
)
add_library(surge::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PUBLIC include)
function(generate_header FS_HEADER FS_NAMESPACE)
message(STATUS "${PROJECT_NAME}: Using ${FS_NAMESPACE} from ${FS_HEADER}")
set(INC_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
set(HEADER_DIR "${INC_DIR}/${PROJECT_NAME}")
set(HEADER_FILE "${HEADER_DIR}/import.h")
configure_file(src/import.h.in "${HEADER_FILE}" @ONLY)
target_sources(${PROJECT_NAME} INTERFACE "${HEADER_FILE}")
target_include_directories(${PROJECT_NAME} INTERFACE "${INC_DIR}")
endfunction()

function(use_fallback_fs)
add_library(${PROJECT_NAME}
src/directory_iterator.cpp
src/filesystem.cpp
src/path.cpp
src/recursive_directory_iterator.cpp
src/util.h
include/${PROJECT_NAME}/directory_entry.h
include/${PROJECT_NAME}/directory_iterator.h
include/${PROJECT_NAME}/filesystem.h
include/${PROJECT_NAME}/filesystem_error.h
include/${PROJECT_NAME}/path.h
include/${PROJECT_NAME}/recursive_directory_iterator.h
)
target_include_directories(${PROJECT_NAME} PUBLIC include)

target_sources(surge-tests INTERFACE src/tests.cpp)
generate_header("\"filesystem/filesystem.h\"" "Surge::filesystem")
endfunction()

target_sources(surge-tests INTERFACE src/tests.cpp)
function(use_platform_fs FS_HEADER FS_NAMESPACE)
add_library(${PROJECT_NAME} INTERFACE)
generate_header("${FS_HEADER}" "${FS_NAMESPACE}")
endfunction()

set(SURGE_DEVEL_FORCE_FALLBACK_FS OFF CACHE BOOL "Force fallback std::filesystem impl (development only)")
mark_as_advanced(FORCE SURGE_DEVEL_FORCE_FALLBACK_FS)
if (${SURGE_DEVEL_FORCE_FALLBACK_FS})
use_fallback_fs()
else()
include(CheckCXXSymbolExists)
CHECK_CXX_SYMBOL_EXISTS(std::filesystem::path::preferred_separator "filesystem" FS_FOUND)
if (FS_FOUND)
use_platform_fs("<filesystem>" "std::filesystem")
else()
# if that check failed we are on one of the wonky gccs in the 7 and 5 family which need to check this with 11 and link stdc++fs
if (UNIX AND NOT APPLE)
set (CMAKE_REQUIRED_LIBRARIES "stdc++fs")
endif()
CHECK_CXX_SYMBOL_EXISTS(std::experimental::filesystem::path::preferred_separator "experimental/filesystem" FS_FOUND)
if (FS_FOUND)
use_platform_fs("<experimental/filesystem>" "std::experimental::filesystem")
target_link_libraries(${PROJECT_NAME} INTERFACE stdc++fs)
else()
set (CMAKE_REQUIRED_LIBRARIES "")
CHECK_CXX_SYMBOL_EXISTS(std::experimental::filesystem::path::preferred_separator "filesystem" FS_FOUND)
if (FS_FOUND)
use_platform_fs("<filesystem>" "std::experimental::filesystem")
else()
use_fallback_fs()
endif()
endif()
endif()
endif()

add_library(surge::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
23 changes: 5 additions & 18 deletions src/common/ImportFilesystem.h → libs/filesystem/src/import.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,12 @@
** supporting VS2017 and macos 10.12 and stuff we need it.
*/


#if USE_STD_FILESYSTEM
#include <filesystem>
namespace fs = std::filesystem;
#elif USE_STD_EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#elif USE_STD_EXPERIMENTAL_FILESYSTEM_FROM_FILESYSTEM
#include <filesystem>
namespace fs = std::experimental::filesystem;
#elif USE_FALLBACK_FILESYSTEM || TARGET_RACK
#include "filesystem/filesystem.h"
namespace fs = Surge::filesystem;
#else
#error FILESYSTEM is not configured by build system
#endif
#include @FS_HEADER@
namespace fs = @FS_NAMESPACE@;

inline std::string path_to_string(const fs::path& path)
{
#if WINDOWS && !TARGET_RACK
#ifdef _WIN32
return path.u8string();
#else
return path.generic_string();
Expand All @@ -35,9 +21,10 @@ inline std::string path_to_string(const fs::path& path)

inline fs::path string_to_path(const std::string& path)
{
#if WINDOWS && !TARGET_RACK
#ifdef _WIN32
return fs::u8path(path);
#else
return fs::path(path);
#endif
}

2 changes: 1 addition & 1 deletion src/common/SurgeStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#endif
#include <tinyxml.h>

#include "ImportFilesystem.h"
#include "filesystem/import.h"

#include <fstream>
#include <iterator>
Expand Down
2 changes: 1 addition & 1 deletion src/common/SurgeSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#include "SurgeParamConfig.h"

#include "UserDefaults.h"
#include "ImportFilesystem.h"
#include "filesystem/import.h"

using namespace std;

Expand Down
2 changes: 1 addition & 1 deletion src/common/SurgeSynthesizerIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <time.h>
#include <vt_dsp/vt_dsp_endian.h>

#include "ImportFilesystem.h"
#include "filesystem/import.h"

#include <fstream>
#include <iterator>
Expand Down
2 changes: 1 addition & 1 deletion src/common/UserDefaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <sstream>
#include <fstream>

#include "ImportFilesystem.h"
#include "filesystem/import.h"

namespace Surge
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/WavSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <cerrno>
#include <cstring>

#include "ImportFilesystem.h"
#include "filesystem/import.h"


// Sigh - lets write a portable ntol by hand
Expand Down
2 changes: 1 addition & 1 deletion src/common/gui/COscillatorDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "guihelpers.h"
#include "SkinColors.h"

#include "ImportFilesystem.h"
#include "filesystem/import.h"

using namespace VSTGUI;

Expand Down
2 changes: 1 addition & 1 deletion src/common/gui/CSnapshotMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "CScalableBitmap.h"
#include "SurgeBitmaps.h"
#include "SurgeStorage.h" // for TINYXML macro
#include "ImportFilesystem.h"
#include "filesystem/import.h"
#include "SkinColors.h"
#include "guihelpers.h"

Expand Down
2 changes: 1 addition & 1 deletion src/common/gui/SkinSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "UIInstrumentation.h"
#include "CScalableBitmap.h"

#include "ImportFilesystem.h"
#include "filesystem/import.h"

#include <array>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion src/common/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct RememberForgetGuard {
#include "aulayer.h"
#endif

#include "ImportFilesystem.h"
#include "filesystem/import.h"

#if LINUX
#include "vstgui/lib/platform/platform_x11.h"
Expand Down
2 changes: 1 addition & 1 deletion utils/svgshow/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
#include <iomanip>

#include "ImportFilesystem.h"
#include "filesystem/import.h"

#if MAC
#include "cocoa_minimal_main.h"
Expand Down

0 comments on commit c19351d

Please sign in to comment.