[vcpkg] Fix create by extracting common paths settings
Resolves
MODIFY
include/vcpkg/base/system.process.h
include/vcpkg/base/system.process.h
@@ -19,7 +19,7 @@ namespace vcpkg::System
std::string s;
};
- std::string make_cmake_cmd(const fs::path& cmake_exe,
+ std::string make_basic_cmake_cmd(const fs::path& cmake_tool_path,
const fs::path& cmake_script,
const std::vector<CMakeVariable>& pass_variables);

ADD
include/vcpkg/buildenvironment.h
+#include <vcpkg/base/system.process.h>
+#include <vcpkg/vcpkgpaths.h>
+
+#include <string>
+#include <vector>
+
+namespace vcpkg
+{
+ std::string make_cmake_cmd(const VcpkgPaths& paths,
+ const fs::path& cmake_script,
+ std::vector<System::CMakeVariable>&& pass_variables);
+}

MODIFY
include/vcpkg/commands.h
include/vcpkg/commands.h
@@ -41,6 +41,7 @@ namespace vcpkg::Commands
namespace Create
{
extern const CommandStructure COMMAND_STRUCTURE;
+ int perform(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}

ADD
src/vcpkg-test/commands.create.cpp
+#include <catch2/catch.hpp>
+
+#include <string>
+#include <iterator>
+#include <vcpkg/base/files.h>
+#include <vcpkg/commands.h>
+#include <vcpkg/vcpkgcmdarguments.h>
+#include <vcpkg/vcpkgpaths.h>
+
+TEST_CASE ("smoke test", "[create]")
+{
+ using namespace vcpkg;
+ static const std::string argsRaw[] = {"create", "zlib2", "http://zlib.net/zlib-1.2.11.tar.gz", "zlib-1.2.11.zip"};
+
+ auto& fsWrapper = Files::get_real_filesystem();
+ VcpkgCmdArguments args = VcpkgCmdArguments::create_from_arg_sequence(std::begin(argsRaw), std::end(argsRaw));
+ VcpkgPaths paths(fsWrapper, args);
+ const auto exit_code = Commands::Create::perform(args, paths);
+ REQUIRE(exit_code == 0);
+ const auto expected_port = paths.ports / fs::u8path("zlib2");
+ const auto expected_portfile_cmake = expected_port / fs::u8path("portfile.cmake");
+ const auto lines = fsWrapper.read_lines(expected_portfile_cmake);
+ REQUIRE(lines.has_value());
+ fsWrapper.remove_all(expected_port, ignore_errors);
+}

MODIFY
src/vcpkg/base/system.process.cpp
src/vcpkg/base/system.process.cpp
@@ -179,20 +179,22 @@ namespace vcpkg
{
}
- std::string System::make_cmake_cmd(const fs::path& cmake_exe,
+ std::string System::make_basic_cmake_cmd(const fs::path& cmake_tool_path,
const fs::path& cmake_script,
const std::vector<CMakeVariable>& pass_variables)
{
- const std::string cmd_cmake_pass_variables = Strings::join(" ", pass_variables, [](auto&& v) { return v.s; });
- return Strings::format(
- R"("%s" %s -P "%s")", cmake_exe.u8string(), cmd_cmake_pass_variables, cmake_script.generic_u8string());
+ return Strings::format(R"("%s" %s -P "%s")",
+ cmake_tool_path.u8string(),
+ Strings::join(" ", pass_variables, [](auto&& v) { return v.s; }),
+ cmake_script.generic_u8string());
}
#if defined(_WIN32)
Environment System::get_modified_clean_environment(const std::unordered_map<std::string, std::string>& extra_env,
const std::string& prepend_to_path)
{
- static const std::string system_root_env = get_environment_variable("SystemRoot").value_or_exit(VCPKG_LINE_INFO);
+ static const std::string system_root_env =
+ get_environment_variable("SystemRoot").value_or_exit(VCPKG_LINE_INFO);
static const std::string system32_env = system_root_env + R"(\system32)";
std::string new_path = Strings::format(R"(Path=%s%s;%s;%s\Wbem;%s\WindowsPowerShell\v1.0\)",
prepend_to_path,

MODIFY
src/vcpkg/build.cpp
src/vcpkg/build.cpp
#include <vcpkg/binarycaching.h>
#include <vcpkg/build.h>
+#include <vcpkg/buildenvironment.h>
#include <vcpkg/commands.h>
#include <vcpkg/dependencies.h>
#include <vcpkg/globalstate.h>
@@ -376,15 +377,10 @@ namespace vcpkg::Build
{"CMD", "BUILD"},
{"PORT", scf.core_paragraph->name},
{"CURRENT_PORT_DIR", scfl.source_location},
- {"VCPKG_ROOT_DIR", paths.root},
- {"PACKAGES_DIR", paths.packages},
- {"BUILDTREES_DIR", paths.buildtrees},
- {"_VCPKG_INSTALLED_DIR", paths.installed},
{"TARGET_TRIPLET", triplet.canonical_name()},
{"TARGET_TRIPLET_FILE", paths.get_triplet_file_path(triplet).u8string()},
{"VCPKG_PLATFORM_TOOLSET", toolset.version.c_str()},
{"VCPKG_USE_HEAD_VERSION", Util::Enum::to_bool(action.build_options.use_head_version) ? "1" : "0"},
- {"DOWNLOADS", paths.downloads},
{"_VCPKG_NO_DOWNLOADS", !Util::Enum::to_bool(action.build_options.allow_downloads) ? "1" : "0"},
{"_VCPKG_DOWNLOAD_TOOL", to_string(action.build_options.download_tool)},
{"FEATURES", Strings::join(";", action.feature_list)},
@@ -460,8 +456,8 @@ namespace vcpkg::Build
else if (pre_build_info.cmake_system_name == "Darwin")
{
hash += "-";
- hash += Hash::get_file_hash(
- VCPKG_LINE_INFO, fs, paths.scripts / fs::u8path("toolchains/osx.cmake"), algo);
+ hash +=
+ Hash::get_file_hash(VCPKG_LINE_INFO, fs, paths.scripts / fs::u8path("toolchains/osx.cmake"), algo);
}
else if (pre_build_info.cmake_system_name == "FreeBSD")
{
@@ -520,10 +516,8 @@ namespace vcpkg::Build
const auto timer = Chrono::ElapsedTimer::create_started();
- auto command =
- System::make_cmake_cmd(paths.get_tool_exe(Tools::CMAKE),
- paths.ports_cmake,
- get_cmake_vars(paths, action, triplet, paths.get_toolset(pre_build_info)));
+ auto command = vcpkg::make_cmake_cmd(
+ paths, paths.ports_cmake, get_cmake_vars(paths, action, triplet, paths.get_toolset(pre_build_info)));
#if defined(_WIN32)
std::string build_env_cmd = make_build_env_cmd(pre_build_info, paths.get_toolset(pre_build_info));

ADD
src/vcpkg/buildenvironment.cpp
+#include "pch.h"
+
+#include <vcpkg/buildenvironment.h>
+
+namespace vcpkg
+{
+ std::string make_cmake_cmd(const VcpkgPaths& paths,
+ const fs::path& cmake_script,
+ std::vector<System::CMakeVariable>&& pass_variables)
+ {
+ auto local_variables = std::move(pass_variables);
+ local_variables.emplace_back("VCPKG_ROOT_DIR", paths.root);
+ local_variables.emplace_back("PACKAGES_DIR", paths.packages);
+ local_variables.emplace_back("BUILDTREES_DIR", paths.buildtrees);
+ local_variables.emplace_back("_VCPKG_INSTALLED_DIR", paths.installed);
+ local_variables.emplace_back("DOWNLOADS", paths.downloads);
+ return System::make_basic_cmake_cmd(paths.get_tool_exe(Tools::CMAKE), cmake_script, local_variables);
+ }
+}

MODIFY
src/vcpkg/cmakevars.cpp
src/vcpkg/cmakevars.cpp
#include <vcpkg/base/util.h>
#include <vcpkg/cmakevars.h>
+#include <vcpkg/buildenvironment.h>
#include <vcpkg/dependencies.h>
using namespace vcpkg;
@@ -58,7 +59,6 @@ namespace vcpkg::CMakeVars
std::vector<std::vector<std::pair<std::string, std::string>>>& vars) const;
const VcpkgPaths& paths;
- const fs::path& cmake_exe_path = paths.get_tool_exe(Tools::CMAKE);
const fs::path get_tags_path = paths.scripts / "vcpkg_get_tags.cmake";
const fs::path get_dep_info_path = paths.scripts / "vcpkg_get_dep_info.cmake";
mutable std::unordered_map<PackageSpec, std::unordered_map<std::string, std::string>> dep_resolution_vars;
@@ -170,7 +170,7 @@ namespace vcpkg::CMakeVars
static constexpr CStringView BLOCK_START_GUID = "c35112b6-d1ba-415b-aa5d-81de856ef8eb";
static constexpr CStringView BLOCK_END_GUID = "e1e74b5c-18cb-4474-a6bd-5c1c8bc81f3f";
- const auto cmd_launch_cmake = System::make_cmake_cmd(cmake_exe_path, script_path, {});
+ const auto cmd_launch_cmake = vcpkg::make_cmake_cmd(paths, script_path, {});
const auto ec_data = System::cmd_execute_and_capture_output(cmd_launch_cmake);
Checks::check_exit(VCPKG_LINE_INFO, ec_data.exit_code == 0, ec_data.output);

MODIFY
src/vcpkg/commands.create.cpp
src/vcpkg/commands.create.cpp
#include <vcpkg/base/checks.h>
#include <vcpkg/base/files.h>
-#include <vcpkg/base/system.process.h>
+#include <vcpkg/buildenvironment.h>
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
@@ -16,15 +16,13 @@ namespace vcpkg::Commands::Create
nullptr,
};
- void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
+ int perform(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
const std::string port_name = args.command_arguments.at(0);
const std::string url = args.command_arguments.at(1);
- const fs::path& cmake_exe = paths.get_tool_exe(Tools::CMAKE);
-
- std::vector<System::CMakeVariable> cmake_args{{"CMD", "CREATE"}, {"PORT", port_name}, {"URL", url}, {"VCPKG_ROOT_DIR", paths.root}};
+ std::vector<System::CMakeVariable> cmake_args{{"CMD", "CREATE"}, {"PORT", port_name}, {"URL", url}};
if (args.command_arguments.size() >= 3)
{
@@ -37,7 +35,12 @@ namespace vcpkg::Commands::Create
cmake_args.emplace_back("FILENAME", zip_file_name);
}
- const std::string cmd_launch_cmake = make_cmake_cmd(cmake_exe, paths.ports_cmake, cmake_args);
- Checks::exit_with_code(VCPKG_LINE_INFO, System::cmd_execute_clean(cmd_launch_cmake));
+ const std::string cmd_launch_cmake = make_cmake_cmd(paths, paths.ports_cmake, std::move(cmake_args));
+ return System::cmd_execute_clean(cmd_launch_cmake);
+ }
+
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
+ {
+ Checks::exit_with_code(VCPKG_LINE_INFO, perform(args, paths));
}
}

MODIFY
vcpkglib/vcpkglib.vcxproj
vcpkglib/vcpkglib.vcxproj
<ClInclude Include="..\include\vcpkg\binarycaching.h" />
<ClInclude Include="..\include\vcpkg\binaryparagraph.h" />
<ClInclude Include="..\include\vcpkg\build.h" />
+ <ClInclude Include="..\include\vcpkg\buildenvironment.h" />
<ClInclude Include="..\include\vcpkg\cmakevars.h" />
<ClInclude Include="..\include\vcpkg\commands.h" />
<ClInclude Include="..\include\vcpkg\dependencies.h" />
<ClCompile Include="..\src\vcpkg\binarycaching.cpp" />
<ClCompile Include="..\src\vcpkg\binaryparagraph.cpp" />
<ClCompile Include="..\src\vcpkg\build.cpp" />
+ <ClCompile Include="..\src\vcpkg\buildenvironment.cpp" />
<ClCompile Include="..\src\vcpkg\cmakevars.cpp" />
<ClCompile Include="..\src\vcpkg\commands.autocomplete.cpp" />
<ClCompile Include="..\src\vcpkg\commands.buildexternal.cpp" />

[vcpkg] Fix create by extracting common paths settings (#11842)

Resolves #11784