From 72e8d2e950cb77c521d98a75f5e1dc9d6230a7c8 Mon Sep 17 00:00:00 2001 From: gulrak Date: Sun, 24 Mar 2019 01:32:13 -0700 Subject: [PATCH] Restructured include directories and enhanced CMake support. --- CMakeLists.txt | 25 ++++++++++++----- README.md | 29 +++++++++++++++++--- examples/CMakeLists.txt | 3 +- examples/dir.cpp | 2 +- filesystem.hpp => include/ghc/filesystem.hpp | 0 test/CMakeLists.txt | 9 ++++-- test/filesystem_test.cpp | 16 +++++++---- test/multi1.cpp | 2 +- test/multi2.cpp | 2 +- 9 files changed, 64 insertions(+), 24 deletions(-) rename filesystem.hpp => include/ghc/filesystem.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cf5cf7..b04dd5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,23 @@ cmake_minimum_required(VERSION 3.10) project(ghcfilesystem) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) +if(NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(CMAKE_CXX_EXTENSIONS OFF) +endif() +if(CMAKE_CXX_STANDARD LESS 11) + message(FATAL_ERROR "CMAKE_CXX_STANDARD is less than 11, ghc::filesystem only works with C++11 and above.") +endif() -add_compile_options("$<$:/utf-8>") -add_compile_options("$<$:/utf-8>") +add_library(ghc_filesystem INTERFACE) +target_sources(ghc_filesystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/ghc/filesystem.hpp) +target_include_directories(ghc_filesystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_compile_options(ghc_filesystem INTERFACE "$<$:/utf-8>") +target_compile_options(ghc_filesystem INTERFACE "$<$:/utf-8>") -add_subdirectory(test) -add_subdirectory(examples) +get_directory_property(hasParent PARENT_DIRECTORY) +if(NOT hasParent) + add_subdirectory(test) + add_subdirectory(examples) +endif() diff --git a/README.md b/README.md index f610cff..b0d3bf4 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ a fallback could be: #include namespace fs = std::filesystem; #else -#include "filesystem.hpp" +#include namespace fs = ghc::filesystem; #endif ``` @@ -115,7 +115,7 @@ using ofstream = std::ofstream; using fstream = std::fstream; } #else -#include "filesystem.hpp" +#include namespace fs { using namespace ghc::filesystem; using ifstream = ghc::filesystem::ifstream; @@ -128,13 +128,19 @@ using fstream = ghc::filesystem::fstream; Now you have e.g. `fs::ofstream out(somePath);` and it is either the wrapper or the C++17 `std::ofstream`. -Note, that on MSVC this detection only works starting from version 15.7 on and when setting +**Note, that on MSVC this detection only works starting from version 15.7 on and when setting the `/Zc:__cplusplus` compile switch, as the compiler allways reports `199711L` -without that switch ([see](https://blogs.msdn.microsoft.com/vcblog/2018/04/09/msvc-now-correctly-reports-__cplusplus/)). +without that switch ([see](https://blogs.msdn.microsoft.com/vcblog/2018/04/09/msvc-now-correctly-reports-__cplusplus/)).** Be aware too, as a header-only library, it is not hiding the fact, that it uses system includes, so they "pollute" your global namespace. +Additionally, starting from v1.1.0, it is possible to add `ghc::filesystem` +as a git submodule, add the directory to your `CMakeLists.txt` with +`add_subdirectory()` and then simply use `target_link_libraries(your-target ghc_filesystem)` +to ensure correct include path that allow `#include ` +to work. + There is a version macro `GHC_FILESYSTEM_VERSION` defined in case future changes might make it needed to react on the version, but I don't plan to break anything. It's the version as decimal number `(major * 10000 + minor * 100 + patch)`. @@ -355,6 +361,21 @@ to the expected behavior. ## Release Notes +### v1.1.0 (wip) + +* Restructuring of the project directory. The header files are now using + `hpp` as extension to be marked as c++ and they where moved to + `include/ghc/` to be able to include by `` as the + former include name might have been to generic and conflict with other + files. +* Better CMake support: `ghc::filesystem` now can be used as a submodul + and added with `add_subdirectory` and will export itself as `ghc_filesystem` + target. To use it, only `target_link_libraries(your-target ghc_filesystem)` + is needed and the include directories will be set so `#include ` + will be a valid directive. + Still you can simply only add the header file to you project and include it + from there. + ### [v1.0.10](https://github.com/gulrak/filesystem/releases/tag/v1.0.10) * Bugfix for ([#9](https://github.com/gulrak/filesystem/issues/9)), added diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5e0b065..5ad4ba0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,6 @@ -add_executable(fs_dir dir.cpp ../filesystem.hpp) +add_executable(fs_dir dir.cpp) +target_link_libraries(fs_dir ghc_filesystem) if(CMAKE_CXX_COMPILER_ID MATCHES MSVC) target_compile_definitions(fs_dir PRIVATE _CRT_SECURE_NO_WARNINGS) endif() diff --git a/examples/dir.cpp b/examples/dir.cpp index df300d0..b9fcff6 100644 --- a/examples/dir.cpp +++ b/examples/dir.cpp @@ -6,7 +6,7 @@ #include namespace fs = std::filesystem; #else -#include "../filesystem.hpp" +#include namespace fs = ghc::filesystem; #endif diff --git a/filesystem.hpp b/include/ghc/filesystem.hpp similarity index 100% rename from filesystem.hpp rename to include/ghc/filesystem.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cb3357f..3c6827b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,6 @@ -add_executable(filesystem_test filesystem_test.cpp ../filesystem.hpp catch.hpp) +add_executable(filesystem_test filesystem_test.cpp catch.hpp) +target_link_libraries(filesystem_test ghc_filesystem) target_compile_options(filesystem_test PRIVATE $<$:-Wall -Wextra -Werror> $<$:-Wall -Werror> @@ -8,9 +9,10 @@ if(CMAKE_CXX_COMPILER_ID MATCHES MSVC) target_compile_definitions(filesystem_test PRIVATE _CRT_SECURE_NO_WARNINGS) endif() if(CMAKE_GENERATOR STREQUAL Xcode) - add_executable(filesystem_test_cov filesystem_test.cpp ../filesystem.hpp catch.hpp) + add_executable(filesystem_test_cov filesystem_test.cpp catch.hpp) target_compile_options(filesystem_test_cov PRIVATE "$<$:--coverage>") target_link_libraries(filesystem_test_cov PRIVATE --coverage) + target_link_libraries(filesystem_test_cov ghc_filesystem) endif() if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 7.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)) @@ -36,4 +38,5 @@ if(CMAKE_CXX_COMPILER_ID MATCHES MSVC AND (CMAKE_CXX_COMPILER_VERSION VERSION_EQ target_compile_definitions(std_filesystem_test PRIVATE USE_STD_FS _CRT_SECURE_NO_WARNINGS) endif() -add_executable(multifile_test multi1.cpp multi2.cpp ../filesystem.hpp catch.hpp) +add_executable(multifile_test multi1.cpp multi2.cpp catch.hpp) +target_link_libraries(multifile_test ghc_filesystem) diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index d779606..59fbd3c 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -65,7 +65,7 @@ using fstream = std::fstream; #endif #else #define NOMINMAX -#include "../filesystem.hpp" +#include namespace fs { using namespace ghc::filesystem; using ifstream = ghc::filesystem::ifstream; @@ -121,12 +121,16 @@ public: TemporaryDirectory(TempOpt opt = TempOpt::none) { static auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - static auto rng = std::bind(std::uniform_int_distribution(0, 35), std::mt19937(static_cast(seed))); - std::string filename = "test_"; - for (int i = 0; i < 8; ++i) { - filename += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rng()]; + static auto rng = std::bind(std::uniform_int_distribution(0, 35), std::mt19937(static_cast(seed) ^ static_cast(reinterpret_cast(&opt)))); + std::string filename; + do { + filename = "test_"; + for (int i = 0; i < 8; ++i) { + filename += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rng()]; + } + _path = fs::canonical(fs::temp_directory_path()) / filename; } - _path = fs::canonical(fs::temp_directory_path()) / filename; + while(fs::exists(_path)); fs::create_directories(_path); if (opt == TempOpt::change_path) { _orig_dir = fs::current_path(); diff --git a/test/multi1.cpp b/test/multi1.cpp index c4dc1ce..b96d1a9 100644 --- a/test/multi1.cpp +++ b/test/multi1.cpp @@ -32,7 +32,7 @@ #define CATCH_CONFIG_MAIN #include "catch.hpp" -#include "../filesystem.hpp" +#include namespace fs = ghc::filesystem; // This test and the one in multi2.cpp doesn't actualy test relevant functionality, diff --git a/test/multi2.cpp b/test/multi2.cpp index f0f9f6e..9f1131c 100644 --- a/test/multi2.cpp +++ b/test/multi2.cpp @@ -30,7 +30,7 @@ // //--------------------------------------------------------------------------------------- #include "catch.hpp" -#include "../filesystem.hpp" +#include namespace fs = ghc::filesystem; // This test and the one in multi1.cpp doesn't actualy test relevant functionality,