Commit 22e61b3f authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

filesystem portability header

Summary:
A portability header for `<filesystem>` backed up by `<experimental/filesystem>`.

The only thing it does is figure out the right header to include and the right namespace to import so that folly code which already may assume C++17 can use names via `folly::fs`.

And polyfills `lexically_normal`.

Reviewed By: Orvid

Differential Revision: D26453988

fbshipit-source-id: 7a7b2e7a312f5d0438d60bc866b109240d096520
parent 621b5b0b
......@@ -208,6 +208,51 @@ if(NOT FOLLY_CPP_ATOMIC_BUILTIN)
endif()
endif()
check_cxx_source_compiles("
#include <type_traits>
#if _GLIBCXX_RELEASE
int main() {}
#endif"
FOLLY_STDLIB_LIBSTDCXX
)
check_cxx_source_compiles("
#include <type_traits>
#if _GLIBCXX_RELEASE >= 9
int main() {}
#endif"
FOLLY_STDLIB_LIBSTDCXX_GE_9
)
check_cxx_source_compiles("
#include <type_traits>
#if _LIBCPP_VERSION
int main() {}
#endif"
FOLLY_STDLIB_LIBCXX
)
check_cxx_source_compiles("
#include <type_traits>
#if _LIBCPP_VERSION >= 9000
int main() {}
#endif"
FOLLY_STDLIB_LIBCXX_GE_9
)
check_cxx_source_compiles("
#include <type_traits>
#if _CPPLIB_VER
int main() {}
#endif"
FOLLY_STDLIB_LIBCPP
)
if (FOLLY_STDLIB_LIBSTDCXX AND NOT FOLLY_STDLIB_LIBSTDCXX_GE_9)
list (APPEND CMAKE_REQUIRED_LIBRARIES stdc++fs)
list (APPEND FOLLY_LINK_LIBRARIES stdc++fs)
endif()
if (FOLLY_STDLIB_LIBCXX AND NOT FOLLY_STDLIB_LIBCXX_GE_9)
list (APPEND CMAKE_REQUIRED_LIBRARIES c++fs)
list (APPEND FOLLY_LINK_LIBRARIES c++fs)
endif ()
option(
FOLLY_LIBRARY_SANITIZE_ADDRESS
"Build folly with Address Sanitizer enabled."
......
......@@ -810,6 +810,7 @@ if (BUILD_TESTS)
DIRECTORY portability/test/
TEST constexpr_test SOURCES ConstexprTest.cpp
TEST filesystem_test SOURCES FilesystemTest.cpp
TEST libgen-test SOURCES LibgenTest.cpp
TEST openssl_portability_test SOURCES OpenSSLPortabilityTest.cpp
TEST pthread_test SOURCES PThreadTest.cpp
......
......@@ -19,7 +19,6 @@
#include <array>
#include <cstdlib>
#include <boost/filesystem.hpp>
#include <folly/Demangle.h>
#include <folly/Range.h>
#include <folly/String.h>
......@@ -27,6 +26,7 @@
#include <folly/experimental/symbolizer/SymbolizedFrame.h>
#include <folly/experimental/symbolizer/detail/Debug.h>
#include <folly/experimental/symbolizer/test/SymbolizerTestUtils.h>
#include <folly/portability/Filesystem.h>
#include <folly/portability/GTest.h>
#include <folly/test/TestUtils.h>
......@@ -143,7 +143,7 @@ void expectFrameEq(
const std::string& file,
size_t lineno) {
auto normalizePath = [](std::string path) {
path = boost::filesystem::path(path).lexically_normal().native();
path = fs::lexically_normal(path).native();
return (path.find("./", 0) != std::string::npos) ? path.substr(2) : path;
};
auto demangled = folly::demangle(frame.name);
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/portability/Filesystem.h>
#include <boost/filesystem.hpp>
namespace folly::fs {
namespace boost_fs = boost::filesystem;
#if __cpp_lib_filesystem >= 201703
path lexically_normal_fn::operator()(path const& p) const {
return p.lexically_normal();
}
#elif __cpp_lib_experimental_filesystem >= 201406
path lexically_normal_fn::operator()(path const& p) const {
return path(boost_fs::path(p.native()).lexically_normal().native());
}
#else
#error require filesystem
#endif
} // namespace folly::fs
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#if __has_include(<filesystem>)
#include <filesystem>
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
#else
#error require filesystem
#endif
namespace folly::fs {
enum class which_enum {
std = 1,
std_experimental = 2,
};
#if __cpp_lib_filesystem >= 201703
namespace std_fs = std::filesystem;
inline constexpr which_enum which = which_enum::std;
#elif __cpp_lib_experimental_filesystem >= 201406
namespace std_fs = std::experimental::filesystem;
inline constexpr which_enum which = which_enum::std_experimental;
#else
#error require filesystem
#endif
// imports
using std_fs::absolute;
using std_fs::canonical;
using std_fs::copy;
using std_fs::copy_file;
using std_fs::copy_options;
using std_fs::copy_symlink;
using std_fs::create_directories;
using std_fs::create_directory;
using std_fs::create_directory_symlink;
using std_fs::create_hard_link;
using std_fs::create_symlink;
using std_fs::current_path;
using std_fs::directory_entry;
using std_fs::directory_iterator;
using std_fs::directory_options;
using std_fs::equivalent;
using std_fs::exists;
using std_fs::file_size;
using std_fs::file_status;
using std_fs::file_time_type;
using std_fs::file_type;
using std_fs::filesystem_error;
using std_fs::hard_link_count;
using std_fs::is_block_file;
using std_fs::is_character_file;
using std_fs::is_directory;
using std_fs::is_empty;
using std_fs::is_fifo;
using std_fs::is_other;
using std_fs::is_regular_file;
using std_fs::is_socket;
using std_fs::is_symlink;
using std_fs::last_write_time;
using std_fs::path;
using std_fs::permissions;
using std_fs::perms;
using std_fs::read_symlink;
using std_fs::recursive_directory_iterator;
using std_fs::remove;
using std_fs::remove_all;
using std_fs::rename;
using std_fs::resize_file;
using std_fs::space;
using std_fs::space_info;
using std_fs::status;
using std_fs::status_known;
using std_fs::symlink_status;
using std_fs::temp_directory_path;
using std_fs::u8path;
#if __cpp_lib_filesystem >= 201703
using std_fs::perm_options;
using std_fs::proximate;
using std_fs::relative;
using std_fs::weakly_canonical;
#endif
struct lexically_normal_fn {
path operator()(path const& p) const;
};
inline constexpr lexically_normal_fn lexically_normal;
} // namespace folly::fs
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/portability/Filesystem.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
using namespace testing;
class FilesystemTest : public Test {};
TEST_F(FilesystemTest, lexically_normal) {
// cases from cppreference.com
// implementation either std::filesystem or boost::filesystem
auto gold = folly::fs::which == folly::fs::which_enum::std;
auto sep = std::string({folly::fs::path::preferred_separator});
EXPECT_THAT(
folly::fs::lexically_normal("foo/./bar/..").native(),
Eq(folly::fs::path(gold ? "foo" + sep : "foo").native()));
EXPECT_THAT(
folly::fs::lexically_normal("foo/.///bar/../").native(),
Eq(folly::fs::path(gold ? "foo" + sep : "foo" + sep + ".").native()));
}
......@@ -23,8 +23,6 @@
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include <folly/Conv.h>
#include <folly/Format.h>
#include <folly/Random.h>
......@@ -32,6 +30,7 @@
#include <folly/Subprocess.h>
#include <folly/experimental/symbolizer/detail/Debug.h>
#include <folly/lang/Bits.h>
#include <folly/portability/Filesystem.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Unistd.h>
#include <folly/tracing/test/StaticTracepointTestModule.h>
......@@ -89,7 +88,7 @@ static std::string getStr(
static std::string getExe() {
auto path = folly::sformat("/proc/{}/exe", getpid());
return boost::filesystem::read_symlink(path).string();
return folly::fs::read_symlink(path).string();
}
static std::string getNoteRawContent(const std::string& fileName) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment