Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vcpkg] Use std::filesystem when Visual Studio is greater than 2015 #12774

Merged
merged 15 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions toolsrc/include/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <vcpkg/base/system_headers.h>

#include <vcpkg/base/files.h>
#include <vcpkg/base/pragmas.h>

#if defined(_WIN32)
Expand All @@ -23,14 +24,6 @@
#include <cctype>
#include <chrono>
#include <codecvt>

#if VCPKG_USE_STD_FILESYSTEM
#include <filesystem>
#else
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
#endif

#include <fstream>
#include <functional>
#include <iomanip>
Expand Down
34 changes: 32 additions & 2 deletions toolsrc/include/vcpkg/base/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

#include <vcpkg/base/expected.h>
#include <vcpkg/base/ignore_errors.h>
#include <vcpkg/base/pragmas.h>

#include <string.h>

BillyONeal marked this conversation as resolved.
Show resolved Hide resolved
#if !defined(VCPKG_USE_STD_FILESYSTEM) && defined(_MSVC_STL_UPDATE)
#if _MSVC_STL_UPDATE >= 201803
// this is for bootstrapping
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
#define VCPKG_USE_STD_FILESYSTEM 1
#endif
#endif

#if VCPKG_USE_STD_FILESYSTEM
#include <filesystem>
#else
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
#endif

Expand All @@ -22,7 +31,28 @@ namespace fs
using stdfs::directory_iterator;
using stdfs::path;
using stdfs::perms;
using stdfs::u8path;

path u8path(vcpkg::StringView s);
inline path u8path(const char* first, const char* last) { return u8path(vcpkg::StringView{first, last}); }
inline path u8path(const char* s) { return u8path(vcpkg::StringView{s, s + ::strlen(s)}); }

#if defined(_MSC_VER)
inline path u8path(std::string::const_iterator first, std::string::const_iterator last)
{
if (first == last)
{
return path{};
}
else
{
auto firstp = &*first;
return u8path(vcpkg::StringView{firstp, firstp + (last - first)});
}
}
#endif

std::string u8string(const path& p);
std::string generic_u8string(const path& p);

#if defined(_WIN32)
enum class file_type
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/include/vcpkg/base/system.process.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace vcpkg::System

struct CmdLineBuilder
{
CmdLineBuilder& path_arg(const fs::path& p) { return string_arg(p.u8string()); }
CmdLineBuilder& path_arg(const fs::path& p) { return string_arg(fs::u8string(p)); }
CmdLineBuilder& string_arg(StringView s);
std::string extract() noexcept { return std::move(buf); }

Expand Down
6 changes: 3 additions & 3 deletions toolsrc/src/vcpkg-test/commands.build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ TEST_CASE ("build smoke test", "[commands-build]")
VcpkgCmdArguments args = VcpkgCmdArguments::create_from_arg_sequence(std::begin(args_raw), std::end(args_raw));
args.binary_caching = false;
args.buildtrees_root_dir =
std::make_unique<std::string>((Test::base_temporary_directory() / fs::u8path("buildtrees")).u8string());
std::make_unique<std::string>(fs::u8string(Test::base_temporary_directory() / fs::u8path("buildtrees")));
args.install_root_dir =
std::make_unique<std::string>((Test::base_temporary_directory() / fs::u8path("installed")).u8string());
std::make_unique<std::string>(fs::u8string(Test::base_temporary_directory() / fs::u8path("installed")));
args.packages_root_dir =
std::make_unique<std::string>((Test::base_temporary_directory() / fs::u8path("packages")).u8string());
std::make_unique<std::string>(fs::u8string(Test::base_temporary_directory() / fs::u8path("packages")));
VcpkgPaths paths(fs_wrapper, args);
if (fs_wrapper.exists(paths.buildtrees)) fs_wrapper.remove_all_inside(paths.buildtrees, VCPKG_LINE_INFO);
if (fs_wrapper.exists(paths.packages)) fs_wrapper.remove_all_inside(paths.packages, VCPKG_LINE_INFO);
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/src/vcpkg-test/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace
{
// regular symlink
auto base_link = base;
base_link.replace_filename(base.filename().u8string() + "-orig");
base_link.replace_filename(fs::u8string(base.filename()) + "-orig");
fs.write_contents(base_link, "", ec);
CHECK_EC_ON_FILE(base_link, ec);
vcpkg::Test::create_symlink(base_link, base, ec);
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/src/vcpkg-test/manifests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ TEST_CASE ("Serialize all the ports", "[manifests]")
if (fs.exists(control))
{
auto contents = fs.read_contents(control, VCPKG_LINE_INFO);
auto pghs = Paragraphs::parse_paragraphs(contents, control.u8string());
auto pghs = Paragraphs::parse_paragraphs(contents, fs::u8string(control));
REQUIRE(pghs);

scfs.push_back(std::move(
Expand Down
37 changes: 20 additions & 17 deletions toolsrc/src/vcpkg/archives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace vcpkg::Archives
void extract_archive(const VcpkgPaths& paths, const fs::path& archive, const fs::path& to_path)
{
Files::Filesystem& fs = paths.get_filesystem();
const fs::path to_path_partial = to_path.u8string() + ".partial"
const fs::path to_path_partial = fs::u8string(to_path) + ".partial"
#if defined(_WIN32)
+ "." + std::to_string(GetCurrentProcessId())
#endif
Expand All @@ -30,31 +30,31 @@ namespace vcpkg::Archives
recursion_limiter_sevenzip_old = true;
const auto nuget_exe = paths.get_tool_exe(Tools::NUGET);

const std::string stem = archive.stem().u8string();
const std::string stem = fs::u8string(archive.stem());
// assuming format of [name].[version in the form d.d.d]
// This assumption may not always hold
std::smatch match;
const bool has_match = std::regex_match(stem, match, std::regex{R"###(^(.+)\.(\d+\.\d+\.\d+)$)###"});
Checks::check_exit(VCPKG_LINE_INFO,
has_match,
"Could not deduce nuget id and version from filename: %s",
archive.u8string());
fs::u8string(archive));

const std::string nugetid = match[1];
const std::string version = match[2];

const auto code_and_output = System::cmd_execute_and_capture_output(Strings::format(
R"("%s" install %s -Version %s -OutputDirectory "%s" -Source "%s" -nocache -DirectDownload -NonInteractive -ForceEnglishOutput -PackageSaveMode nuspec)",
nuget_exe.u8string(),
fs::u8string(nuget_exe),
nugetid,
version,
to_path_partial.u8string(),
paths.downloads.u8string()));
fs::u8string(to_path_partial),
fs::u8string(paths.downloads)));

Checks::check_exit(VCPKG_LINE_INFO,
code_and_output.exit_code == 0,
"Failed to extract '%s' with message:\n%s",
archive.u8string(),
fs::u8string(archive),
code_and_output.output);
recursion_limiter_sevenzip_old = false;
}
Expand All @@ -64,31 +64,34 @@ namespace vcpkg::Archives
Checks::check_exit(VCPKG_LINE_INFO, !recursion_limiter_sevenzip);
recursion_limiter_sevenzip = true;
const auto seven_zip = paths.get_tool_exe(Tools::SEVEN_ZIP);
const auto code_and_output = System::cmd_execute_and_capture_output(Strings::format(
R"("%s" x "%s" -o"%s" -y)", seven_zip.u8string(), archive.u8string(), to_path_partial.u8string()));
const auto code_and_output =
System::cmd_execute_and_capture_output(Strings::format(R"("%s" x "%s" -o"%s" -y)",
fs::u8string(seven_zip),
fs::u8string(archive),
fs::u8string(to_path_partial)));
Checks::check_exit(VCPKG_LINE_INFO,
code_and_output.exit_code == 0,
"7zip failed while extracting '%s' with message:\n%s",
archive.u8string(),
fs::u8string(archive),
code_and_output.output);
recursion_limiter_sevenzip = false;
}
#else
if (ext == ".gz" && ext.extension() != ".tar")
{
const auto code = System::cmd_execute(
Strings::format(R"(cd '%s' && tar xzf '%s')", to_path_partial.u8string(), archive.u8string()));
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "tar failed while extracting %s", archive.u8string());
Strings::format(R"(cd '%s' && tar xzf '%s')", fs::u8string(to_path_partial), fs::u8string(archive)));
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "tar failed while extracting %s", fs::u8string(archive));
}
else if (ext == ".zip")
{
const auto code = System::cmd_execute(
Strings::format(R"(cd '%s' && unzip -qqo '%s')", to_path_partial.u8string(), archive.u8string()));
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "unzip failed while extracting %s", archive.u8string());
Strings::format(R"(cd '%s' && unzip -qqo '%s')", fs::u8string(to_path_partial), fs::u8string(archive)));
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "unzip failed while extracting %s", fs::u8string(archive));
}
else
{
Checks::exit_with_message(VCPKG_LINE_INFO, "Unexpected archive extension: %s", ext.u8string());
Checks::exit_with_message(VCPKG_LINE_INFO, "Unexpected archive extension: %s", fs::u8string(ext));
}
#endif

Expand All @@ -106,8 +109,8 @@ namespace vcpkg::Archives
!ec,
"Failed to do post-extract rename-in-place.\n"
"fs.rename(%s, %s, %s)",
to_path_partial.u8string(),
to_path.u8string(),
fs::u8string(to_path_partial),
fs::u8string(to_path),
ec.message());
}
}
6 changes: 3 additions & 3 deletions toolsrc/src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace vcpkg::Downloads
const auto dir = fs::path(target_file_path.c_str()).parent_path();
std::error_code ec;
fs.create_directories(dir, ec);
Checks::check_exit(VCPKG_LINE_INFO, !ec, "Could not create directories %s", dir.u8string());
Checks::check_exit(VCPKG_LINE_INFO, !ec, "Could not create directories %s", fs::u8string(dir));

FILE* f = nullptr;
const errno_t err = fopen_s(&f, target_file_path.c_str(), "wb");
Expand Down Expand Up @@ -164,7 +164,7 @@ namespace vcpkg::Downloads
" Expected hash : [ %s ]\n"
" Actual hash : [ %s ]\n",
url,
path.u8string(),
fs::u8string(path),
sha512,
actual_hash);
}
Expand All @@ -174,7 +174,7 @@ namespace vcpkg::Downloads
const fs::path& download_path,
const std::string& sha512)
{
const std::string download_path_part = download_path.u8string() + ".part";
const std::string download_path_part = fs::u8string(download_path) + ".part";
auto download_path_part_path = fs::u8path(download_path_part);
std::error_code ec;
fs.remove(download_path, ec);
Expand Down
Loading