Skip to content

Commit

Permalink
integrate install/project: Don't write to VCPKG_ROOT. (#973)
Browse files Browse the repository at this point in the history
* integrate install/project: Don't write to VCPKG_ROOT.

* Move the "tmp" directory to the normal place we do temp things.
* Fix not clearing the tmp directory after use. Fixes microsoft/vcpkg#10537 .
* For MSBuild 14, only use a tmp directory at all if we need to do the file copy from an elevated prompt instead of always using a temp directory.
* For MSBuild != 14, just write the files into the correct place rather than writing to a temp place then copying over.
* For MSBuild all, reuse that on Windows user_configuration_home is already appdata_local / "vcpkg" instead of deriving that path all over the place.
* StringLiteral-ize create_nuget_props_file_contents since it was a constant.
* Remove noexcept from functions that allocate and return std::strings.
* In integrate project, put the resulting nupkg in CWD rather than always in buildsystems.

* Fix MacOS warning.
  • Loading branch information
BillyONeal authored Mar 22, 2023
1 parent 3fbb42b commit 1085a50
Showing 1 changed file with 41 additions and 74 deletions.
115 changes: 41 additions & 74 deletions src/vcpkg/commands.integrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace vcpkg::Commands::Integrate
#endif

#if defined(_WIN32)
static std::string create_system_targets_shortcut() noexcept
static std::string create_system_targets_shortcut()
{
return R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Expand All @@ -139,7 +139,7 @@ namespace vcpkg::Commands::Integrate
#endif

#if defined(_WIN32)
static std::string create_nuget_targets_file_contents(const Path& msbuild_vcpkg_targets_file) noexcept
static std::string create_nuget_targets_file_contents(const Path& msbuild_vcpkg_targets_file)
{
return fmt::format(R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Expand All @@ -155,16 +155,13 @@ namespace vcpkg::Commands::Integrate
#endif

#if defined(_WIN32)
static std::string create_nuget_props_file_contents() noexcept
{
return R"###(
static constexpr StringLiteral NUGET_PROPS_FILE_CONTENTS = R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VCPkgLocalAppDataDisabled>true</VCPkgLocalAppDataDisabled>
</PropertyGroup>
</Project>
)###";
}
#endif

#if defined(_WIN32)
Expand Down Expand Up @@ -247,23 +244,13 @@ namespace vcpkg::Commands::Integrate
}
#endif

#if defined(_WIN32)
static Path get_appdata_targets_path()
{
return get_appdata_local().value_or_exit(VCPKG_LINE_INFO) / "vcpkg\\vcpkg.user.targets";
}
#endif
#if defined(_WIN32)
static Path get_appdata_props_path()
{
return get_appdata_local().value_or_exit(VCPKG_LINE_INFO) / "vcpkg\\vcpkg.user.props";
}
#endif

static constexpr StringLiteral vcpkg_path_txt_name = "vcpkg.path.txt";
static constexpr StringLiteral vcpkg_path_txt = "vcpkg.path.txt";

#if defined(_WIN32)
static void integrate_install_msbuild14(Filesystem& fs, const Path& tmp_dir)
static constexpr StringLiteral vcpkg_user_props = "vcpkg.user.props";
static constexpr StringLiteral vcpkg_user_targets = "vcpkg.user.targets";

static void integrate_install_msbuild14(Filesystem& fs)
{
std::array<Path, 2> OLD_SYSTEM_TARGET_FILES = {
get_program_files_32_bit().value_or_exit(VCPKG_LINE_INFO) /
Expand Down Expand Up @@ -304,6 +291,7 @@ namespace vcpkg::Commands::Integrate

if (should_install_system)
{
const auto tmp_dir = fs.create_or_get_temp_directory(VCPKG_LINE_INFO);
const auto sys_src_path = tmp_dir / "vcpkg.system.targets";
fs.write_contents(sys_src_path, create_system_targets_shortcut(), VCPKG_LINE_INFO);

Expand All @@ -312,6 +300,7 @@ namespace vcpkg::Commands::Integrate
sys_src_path,
SYSTEM_WIDE_TARGETS_FILE);
const ElevationPromptChoice user_choice = elevated_cmd_execute(param);
fs.remove_all(tmp_dir, VCPKG_LINE_INFO);
switch (user_choice)
{
case ElevationPromptChoice::YES: break;
Expand All @@ -331,62 +320,42 @@ namespace vcpkg::Commands::Integrate
{
auto& fs = paths.get_filesystem();

#if defined(_WIN32)
{
const auto tmp_dir = paths.buildsystems / "tmp";
fs.create_directory(paths.buildsystems, VCPKG_LINE_INFO);
fs.create_directory(tmp_dir, VCPKG_LINE_INFO);

integrate_install_msbuild14(fs, tmp_dir);

const auto appdata_src_path = tmp_dir / "vcpkg.user.targets";
fs.write_contents(
appdata_src_path, create_appdata_shortcut(paths.buildsystems_msbuild_targets), VCPKG_LINE_INFO);
auto appdata_dst_path = get_appdata_targets_path();

const auto vcpkg_appdata_local = get_appdata_local().value_or_exit(VCPKG_LINE_INFO) / "vcpkg";
fs.create_directory(vcpkg_appdata_local, VCPKG_LINE_INFO);

fs.copy_file(appdata_src_path, appdata_dst_path, CopyOptions::overwrite_existing, VCPKG_LINE_INFO);

const Path appdata_src_path2 = tmp_dir / "vcpkg.user.props";
fs.write_contents(
appdata_src_path2, create_appdata_shortcut(paths.buildsystems_msbuild_props), VCPKG_LINE_INFO);
auto appdata_dst_path2 = get_appdata_props_path();
auto user_configuration_home = get_user_configuration_home().value_or_exit(VCPKG_LINE_INFO);
fs.create_directories(user_configuration_home, VCPKG_LINE_INFO);
fs.write_contents(user_configuration_home / vcpkg_path_txt, paths.root.generic_u8string(), VCPKG_LINE_INFO);

fs.copy_file(appdata_src_path2, appdata_dst_path2, CopyOptions::overwrite_existing, VCPKG_LINE_INFO);
}
#if defined(_WIN32)
integrate_install_msbuild14(fs);

fs.write_contents(user_configuration_home / vcpkg_user_props,
create_appdata_shortcut(paths.buildsystems_msbuild_props),
VCPKG_LINE_INFO);
fs.write_contents(user_configuration_home / vcpkg_user_targets,
create_appdata_shortcut(paths.buildsystems_msbuild_targets),
VCPKG_LINE_INFO);
#endif

auto user_configuration_home = get_user_configuration_home().value_or_exit(VCPKG_LINE_INFO);
fs.create_directories(user_configuration_home, IgnoreErrors{});
fs.write_contents(
user_configuration_home / vcpkg_path_txt_name, paths.root.generic_u8string(), VCPKG_LINE_INFO);
msg::println(Color::success, msgAppliedUserIntegration);

const auto cmake_toolchain = paths.buildsystems / "vcpkg.cmake";

auto message = msg::format(msgCMakeToolChainFile, msg::path = cmake_toolchain.generic_u8string());
#if defined(_WIN32)
msg::println(msg::format(msgCMakeToolChainFile, msg::path = cmake_toolchain.generic_u8string())
.append_raw("\n\n")
.append(msgAutomaticLinkingForMSBuildProjects));
#else
msg::println(msgCMakeToolChainFile, msg::path = cmake_toolchain.generic_u8string());
message.append_raw("\n\n").append(msgAutomaticLinkingForMSBuildProjects);
#endif

msg::println(message);
Checks::exit_success(VCPKG_LINE_INFO);
}

static void integrate_remove(Filesystem& fs)
{
bool was_deleted = false;

auto user_configuration_home = get_user_configuration_home().value_or_exit(VCPKG_LINE_INFO);
#if defined(_WIN32)
was_deleted |= fs.remove(get_appdata_targets_path(), VCPKG_LINE_INFO);
was_deleted |= fs.remove(get_appdata_props_path(), VCPKG_LINE_INFO);
was_deleted |= fs.remove(user_configuration_home / vcpkg_user_props, VCPKG_LINE_INFO);
was_deleted |= fs.remove(user_configuration_home / vcpkg_user_targets, VCPKG_LINE_INFO);
#endif

auto user_configuration_home = get_user_configuration_home().value_or_exit(VCPKG_LINE_INFO);
was_deleted |= fs.remove(user_configuration_home / vcpkg_path_txt_name, VCPKG_LINE_INFO);
was_deleted |= fs.remove(user_configuration_home / vcpkg_path_txt, VCPKG_LINE_INFO);

if (was_deleted)
{
Expand All @@ -407,11 +376,7 @@ namespace vcpkg::Commands::Integrate

const Path& nuget_exe = paths.get_tool_exe(Tools::NUGET, stdout_sink);

const Path& buildsystems_dir = paths.buildsystems;
const auto tmp_dir = buildsystems_dir / "tmp";
fs.create_directory(buildsystems_dir, IgnoreErrors{});
fs.create_directory(tmp_dir, IgnoreErrors{});

const auto tmp_dir = fs.create_or_get_temp_directory(VCPKG_LINE_INFO);
const auto targets_file_path = tmp_dir / "vcpkg.nuget.targets";
const auto props_file_path = tmp_dir / "vcpkg.nuget.props";
const auto nuspec_file_path = tmp_dir / "vcpkg.nuget.nuspec";
Expand All @@ -420,35 +385,37 @@ namespace vcpkg::Commands::Integrate

fs.write_contents(
targets_file_path, create_nuget_targets_file_contents(paths.buildsystems_msbuild_targets), VCPKG_LINE_INFO);
fs.write_contents(props_file_path, create_nuget_props_file_contents(), VCPKG_LINE_INFO);
fs.write_contents(props_file_path, NUGET_PROPS_FILE_CONTENTS, VCPKG_LINE_INFO);
fs.write_contents(
nuspec_file_path, create_nuspec_file_contents(paths.root, nuget_id, nupkg_version), VCPKG_LINE_INFO);

// Using all forward slashes for the command line
auto cmd_line = Command(nuget_exe)
.string_arg("pack")
.string_arg("-OutputDirectory")
.string_arg(buildsystems_dir)
.string_arg(paths.original_cwd)
.string_arg(nuspec_file_path);

const auto maybe_nuget_output = flatten(
cmd_execute_and_capture_output(cmd_line, default_working_directory, get_clean_environment()), Tools::NUGET);

if (!maybe_nuget_output)
{
msg::println_error(msg::format(msgCommandFailed, msg::command_line = cmd_line.command_line())
.append_raw('\n')
.append(maybe_nuget_output.error()));
Checks::unreachable(VCPKG_LINE_INFO);
Checks::msg_exit_with_message(VCPKG_LINE_INFO,
msg::format(msgCommandFailed, msg::command_line = cmd_line.command_line())
.append_raw('\n')
.append(maybe_nuget_output.error()));
}

const auto nuget_package = buildsystems_dir / fmt::format("{}.{}.nupkg", nuget_id, nupkg_version);
fs.remove_all(tmp_dir, VCPKG_LINE_INFO);
const auto nuget_package = paths.original_cwd / fmt::format("{}.{}.nupkg", nuget_id, nupkg_version);
Checks::msg_check_exit(VCPKG_LINE_INFO,
fs.exists(nuget_package, IgnoreErrors{}),
msgNugetPackageFileSucceededButCreationFailed,
msg::path = nuget_package);
msg::println(Color::success, msgCreatedNuGetPackage, msg::path = nuget_package);

auto source_path = Strings::replace_all(buildsystems_dir, "`", "``");
auto source_path = Strings::replace_all(paths.original_cwd, "`", "``");

msg::println(msgInstallPackageInstruction, msg::value = nuget_id, msg::path = source_path);
Checks::exit_success(VCPKG_LINE_INFO);
Expand Down

0 comments on commit 1085a50

Please sign in to comment.