2017-01-28 04:49:09 +08:00
|
|
|
#include "pch.h"
|
2017-04-01 08:24:45 +08:00
|
|
|
#include "vcpkg_expected.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "vcpkg_paths.h"
|
|
|
|
#include "metrics.h"
|
|
|
|
#include "vcpkg_System.h"
|
2017-04-04 05:45:00 +08:00
|
|
|
#include "PackageSpec.h"
|
2017-04-01 08:40:08 +08:00
|
|
|
#include "vcpkg_Files.h"
|
2017-04-01 18:30:52 +08:00
|
|
|
#include "vcpkg_Util.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
namespace vcpkg
|
|
|
|
{
|
2017-03-11 08:30:44 +08:00
|
|
|
static bool exists_and_has_equal_or_greater_version(const std::wstring& version_cmd, const std::array<int, 3>& expected_version)
|
|
|
|
{
|
|
|
|
static const std::regex re(R"###((\d+)\.(\d+)\.(\d+))###");
|
|
|
|
|
2017-03-14 07:17:47 +08:00
|
|
|
auto rc = System::cmd_execute_and_capture_output(Strings::wformat(LR"(%s)", version_cmd));
|
2017-03-11 08:30:44 +08:00
|
|
|
if (rc.exit_code != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::match_results<std::string::const_iterator> match;
|
|
|
|
auto found = std::regex_search(rc.output, match, re);
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int d1 = atoi(match[1].str().c_str());
|
|
|
|
int d2 = atoi(match[2].str().c_str());
|
|
|
|
int d3 = atoi(match[3].str().c_str());
|
|
|
|
if (d1 > expected_version[0] || (d1 == expected_version[0] && d2 > expected_version[1]) || (d1 == expected_version[0] && d2 == expected_version[1] && d3 >= expected_version[2]))
|
|
|
|
{
|
|
|
|
// satisfactory version found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static optional<fs::path> find_if_has_equal_or_greater_version(const std::vector<fs::path>& candidate_paths, const std::wstring& version_check_arguments, const std::array<int, 3>& expected_version)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(candidate_paths.cbegin(), candidate_paths.cend(), [&](const fs::path& p)
|
2017-03-11 09:01:41 +08:00
|
|
|
{
|
|
|
|
const std::wstring cmd = Strings::wformat(LR"("%s" %s)", p.native(), version_check_arguments);
|
|
|
|
return exists_and_has_equal_or_greater_version(cmd, expected_version);
|
|
|
|
});
|
2017-03-11 08:30:44 +08:00
|
|
|
|
|
|
|
if (it != candidate_paths.cend())
|
|
|
|
{
|
2017-03-29 06:05:55 +08:00
|
|
|
return std::move(*it);
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 06:05:55 +08:00
|
|
|
return nullopt;
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<fs::path> find_from_PATH(const std::wstring& name)
|
|
|
|
{
|
2017-03-14 07:17:47 +08:00
|
|
|
const std::wstring cmd = Strings::wformat(L"where.exe %s", name);
|
2017-03-11 08:30:44 +08:00
|
|
|
auto out = System::cmd_execute_and_capture_output(cmd);
|
|
|
|
if (out.exit_code != 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:30:52 +08:00
|
|
|
return Util::fmap(Strings::split(out.output, "\n"), [](auto&& s) { return fs::path(s); });
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static fs::path fetch_dependency(const fs::path scripts_folder, const std::wstring& tool_name, const fs::path& expected_downloaded_path)
|
|
|
|
{
|
|
|
|
const fs::path script = scripts_folder / "fetchDependency.ps1";
|
|
|
|
auto install_cmd = System::create_powershell_script_cmd(script, Strings::wformat(L"-Dependency %s", tool_name));
|
|
|
|
System::exit_code_and_output rc = System::cmd_execute_and_capture_output(install_cmd);
|
|
|
|
if (rc.exit_code)
|
|
|
|
{
|
|
|
|
System::println(System::color::error, "Launching powershell failed or was denied");
|
2017-04-04 05:41:36 +08:00
|
|
|
Metrics::track_property("error", "powershell install failed");
|
|
|
|
Metrics::track_property("installcmd", install_cmd);
|
2017-03-23 08:46:05 +08:00
|
|
|
Checks::exit_with_code(VCPKG_LINE_INFO, rc.exit_code);
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 08:54:34 +08:00
|
|
|
const fs::path actual_downloaded_path = Strings::trimmed(rc.output);
|
2017-03-14 08:38:04 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, expected_downloaded_path == actual_downloaded_path, "Expected dependency downloaded path to be %s, but was %s",
|
|
|
|
expected_downloaded_path.generic_string(), actual_downloaded_path.generic_string());
|
2017-03-11 08:30:44 +08:00
|
|
|
return actual_downloaded_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fs::path get_cmake_path(const fs::path& downloads_folder, const fs::path scripts_folder)
|
|
|
|
{
|
|
|
|
static constexpr std::array<int, 3> expected_version = { 3,8,0 };
|
|
|
|
static const std::wstring version_check_arguments = L"--version";
|
|
|
|
|
|
|
|
const fs::path downloaded_copy = downloads_folder / "cmake-3.8.0-rc1-win32-x86" / "bin" / "cmake.exe";
|
|
|
|
const std::vector<fs::path> from_path = find_from_PATH(L"cmake");
|
|
|
|
|
|
|
|
std::vector<fs::path> candidate_paths;
|
|
|
|
candidate_paths.push_back(downloaded_copy);
|
|
|
|
candidate_paths.insert(candidate_paths.end(), from_path.cbegin(), from_path.cend());
|
2017-04-01 08:24:45 +08:00
|
|
|
candidate_paths.push_back(System::get_ProgramFiles_platform_bitness() / "CMake" / "bin" / "cmake.exe");
|
|
|
|
candidate_paths.push_back(System::get_ProgramFiles_32_bit() / "CMake" / "bin");
|
2017-03-11 08:30:44 +08:00
|
|
|
|
2017-03-29 06:05:55 +08:00
|
|
|
const optional<fs::path> path = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version);
|
|
|
|
if (auto p = path.get())
|
2017-03-11 08:30:44 +08:00
|
|
|
{
|
2017-03-29 06:05:55 +08:00
|
|
|
return *p;
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return fetch_dependency(scripts_folder, L"cmake", downloaded_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::path get_nuget_path(const fs::path& downloads_folder, const fs::path scripts_folder)
|
|
|
|
{
|
|
|
|
static constexpr std::array<int, 3> expected_version = { 3,3,0 };
|
|
|
|
static const std::wstring version_check_arguments = L"";
|
|
|
|
|
|
|
|
const fs::path downloaded_copy = downloads_folder / "nuget-3.5.0" / "nuget.exe";
|
|
|
|
const std::vector<fs::path> from_path = find_from_PATH(L"nuget");
|
|
|
|
|
|
|
|
std::vector<fs::path> candidate_paths;
|
|
|
|
candidate_paths.push_back(downloaded_copy);
|
|
|
|
candidate_paths.insert(candidate_paths.end(), from_path.cbegin(), from_path.cend());
|
|
|
|
|
2017-03-29 06:05:55 +08:00
|
|
|
auto path = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version);
|
|
|
|
if (auto p = path.get())
|
2017-03-11 08:30:44 +08:00
|
|
|
{
|
2017-03-29 06:05:55 +08:00
|
|
|
return *p;
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return fetch_dependency(scripts_folder, L"nuget", downloaded_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::path get_git_path(const fs::path& downloads_folder, const fs::path scripts_folder)
|
|
|
|
{
|
|
|
|
static constexpr std::array<int, 3> expected_version = { 2,0,0 };
|
|
|
|
static const std::wstring version_check_arguments = L"--version";
|
|
|
|
|
|
|
|
const fs::path downloaded_copy = downloads_folder / "MinGit-2.11.1-32-bit" / "cmd" / "git.exe";
|
|
|
|
const std::vector<fs::path> from_path = find_from_PATH(L"git");
|
|
|
|
|
|
|
|
std::vector<fs::path> candidate_paths;
|
|
|
|
candidate_paths.push_back(downloaded_copy);
|
|
|
|
candidate_paths.insert(candidate_paths.end(), from_path.cbegin(), from_path.cend());
|
2017-04-01 08:24:45 +08:00
|
|
|
candidate_paths.push_back(System::get_ProgramFiles_platform_bitness() / "git" / "cmd" / "git.exe");
|
|
|
|
candidate_paths.push_back(System::get_ProgramFiles_32_bit() / "git" / "cmd" / "git.exe");
|
2017-03-11 08:30:44 +08:00
|
|
|
|
2017-03-29 06:05:55 +08:00
|
|
|
const optional<fs::path> path = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version);
|
|
|
|
if (auto p = path.get())
|
2017-03-11 08:30:44 +08:00
|
|
|
{
|
2017-03-29 06:05:55 +08:00
|
|
|
return *p;
|
2017-03-11 08:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return fetch_dependency(scripts_folder, L"git", downloaded_copy);
|
|
|
|
}
|
|
|
|
|
2016-09-19 11:50:08 +08:00
|
|
|
expected<vcpkg_paths> vcpkg_paths::create(const fs::path& vcpkg_root_dir)
|
|
|
|
{
|
|
|
|
std::error_code ec;
|
|
|
|
const fs::path canonical_vcpkg_root_dir = fs::canonical(vcpkg_root_dir, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
vcpkg_paths paths;
|
|
|
|
paths.root = canonical_vcpkg_root_dir;
|
|
|
|
|
|
|
|
if (paths.root.empty())
|
|
|
|
{
|
2017-04-04 05:41:36 +08:00
|
|
|
Metrics::track_property("error", "Invalid vcpkg root directory");
|
2017-03-23 08:06:09 +08:00
|
|
|
Checks::exit_with_message(VCPKG_LINE_INFO, "Invalid vcpkg root directory: %s", paths.root.string());
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
paths.packages = paths.root / "packages";
|
|
|
|
paths.buildtrees = paths.root / "buildtrees";
|
|
|
|
paths.downloads = paths.root / "downloads";
|
|
|
|
paths.ports = paths.root / "ports";
|
|
|
|
paths.installed = paths.root / "installed";
|
|
|
|
paths.triplets = paths.root / "triplets";
|
2017-01-24 07:12:43 +08:00
|
|
|
paths.scripts = paths.root / "scripts";
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-01-24 07:12:43 +08:00
|
|
|
paths.buildsystems = paths.scripts / "buildsystems";
|
2016-09-19 11:50:08 +08:00
|
|
|
paths.buildsystems_msbuild_targets = paths.buildsystems / "msbuild" / "vcpkg.targets";
|
|
|
|
|
|
|
|
paths.vcpkg_dir = paths.installed / "vcpkg";
|
|
|
|
paths.vcpkg_dir_status_file = paths.vcpkg_dir / "status";
|
|
|
|
paths.vcpkg_dir_info = paths.vcpkg_dir / "info";
|
|
|
|
paths.vcpkg_dir_updates = paths.vcpkg_dir / "updates";
|
|
|
|
|
2017-01-24 07:12:43 +08:00
|
|
|
paths.ports_cmake = paths.scripts / "ports.cmake";
|
2017-03-11 08:30:44 +08:00
|
|
|
|
2016-09-19 11:50:08 +08:00
|
|
|
return paths;
|
|
|
|
}
|
2016-09-24 05:25:17 +08:00
|
|
|
|
2017-04-04 05:45:00 +08:00
|
|
|
fs::path vcpkg_paths::package_dir(const PackageSpec& spec) const
|
2016-09-24 05:25:17 +08:00
|
|
|
{
|
2016-09-24 05:32:56 +08:00
|
|
|
return this->packages / spec.dir();
|
2016-09-24 05:25:17 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:45:00 +08:00
|
|
|
fs::path vcpkg_paths::port_dir(const PackageSpec& spec) const
|
2016-09-24 05:25:17 +08:00
|
|
|
{
|
2016-10-04 08:45:01 +08:00
|
|
|
return this->ports / spec.name();
|
2016-09-24 05:25:17 +08:00
|
|
|
}
|
2016-09-29 03:39:05 +08:00
|
|
|
|
2017-04-04 05:45:00 +08:00
|
|
|
fs::path vcpkg_paths::build_info_file_path(const PackageSpec& spec) const
|
2016-11-09 06:12:49 +08:00
|
|
|
{
|
|
|
|
return this->package_dir(spec) / "BUILD_INFO";
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:54:23 +08:00
|
|
|
fs::path vcpkg_paths::listfile_path(const BinaryParagraph& pgh) const
|
|
|
|
{
|
|
|
|
return this->vcpkg_dir_info / (pgh.fullstem() + ".list");
|
|
|
|
}
|
|
|
|
|
2017-04-04 06:02:45 +08:00
|
|
|
bool vcpkg_paths::is_valid_triplet(const Triplet& t) const
|
2016-09-29 03:39:05 +08:00
|
|
|
{
|
|
|
|
auto it = fs::directory_iterator(this->triplets);
|
|
|
|
for (; it != fs::directory_iterator(); ++it)
|
|
|
|
{
|
|
|
|
std::string triplet_file_name = it->path().stem().generic_u8string();
|
2016-10-04 08:20:52 +08:00
|
|
|
if (t.canonical_name() == triplet_file_name) // TODO: fuzzy compare
|
2016-09-29 03:39:05 +08:00
|
|
|
{
|
|
|
|
//t.value = triplet_file_name; // NOTE: uncomment when implementing fuzzy compare
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-11 08:30:44 +08:00
|
|
|
|
|
|
|
const fs::path& vcpkg_paths::get_cmake_exe() const
|
|
|
|
{
|
|
|
|
return this->cmake_exe.get_lazy([this]() { return get_cmake_path(this->downloads, this->scripts); });
|
|
|
|
}
|
|
|
|
|
|
|
|
const fs::path& vcpkg_paths::get_git_exe() const
|
|
|
|
{
|
|
|
|
return this->git_exe.get_lazy([this]() { return get_git_path(this->downloads, this->scripts); });
|
|
|
|
}
|
|
|
|
|
|
|
|
const fs::path& vcpkg_paths::get_nuget_exe() const
|
|
|
|
{
|
|
|
|
return this->nuget_exe.get_lazy([this]() { return get_nuget_path(this->downloads, this->scripts); });
|
|
|
|
}
|
2017-04-01 08:40:08 +08:00
|
|
|
|
|
|
|
static std::vector<std::string> get_VS2017_installation_instances(const vcpkg_paths& paths)
|
|
|
|
{
|
|
|
|
const fs::path script = paths.scripts / "findVisualStudioInstallationInstances.ps1";
|
|
|
|
const std::wstring cmd = System::create_powershell_script_cmd(script);
|
|
|
|
System::exit_code_and_output ec_data = System::cmd_execute_and_capture_output(cmd);
|
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, ec_data.exit_code == 0, "Could not run script to detect VS 2017 instances");
|
|
|
|
return Strings::split(ec_data.output, "\n");
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:30:52 +08:00
|
|
|
static optional<fs::path> get_VS2015_installation_instance()
|
2017-04-01 08:40:08 +08:00
|
|
|
{
|
2017-04-01 18:30:52 +08:00
|
|
|
const optional<std::wstring> vs2015_cmntools_optional = System::get_environmental_variable(L"VS140COMNTOOLS");
|
|
|
|
if (auto v = vs2015_cmntools_optional.get())
|
|
|
|
{
|
|
|
|
const fs::path vs2015_cmntools = fs::path(*v).parent_path(); // The call to parent_path() is needed because the env variable has a trailing backslash
|
|
|
|
return vs2015_cmntools.parent_path().parent_path();
|
|
|
|
}
|
2017-04-01 08:40:08 +08:00
|
|
|
|
2017-04-01 18:30:52 +08:00
|
|
|
return nullopt;
|
2017-04-01 08:40:08 +08:00
|
|
|
}
|
|
|
|
|
2017-04-01 17:08:48 +08:00
|
|
|
static toolset_t find_toolset_instance(const vcpkg_paths& paths)
|
2017-04-01 08:40:08 +08:00
|
|
|
{
|
|
|
|
const std::vector<std::string> vs2017_installation_instances = get_VS2017_installation_instances(paths);
|
2017-04-01 17:08:48 +08:00
|
|
|
// Note: this will contain a mix of vcvarsall.bat locations and dumpbin.exe locations.
|
2017-04-01 08:40:08 +08:00
|
|
|
std::vector<fs::path> paths_examined;
|
|
|
|
|
|
|
|
// VS2017
|
2017-04-01 17:08:48 +08:00
|
|
|
for (const fs::path& instance : vs2017_installation_instances)
|
2017-04-01 08:40:08 +08:00
|
|
|
{
|
2017-04-01 17:08:48 +08:00
|
|
|
const fs::path vc_dir = instance / "VC";
|
|
|
|
|
|
|
|
// Skip any instances that do not have vcvarsall.
|
|
|
|
const fs::path vcvarsall_bat = vc_dir / "Auxiliary" / "Build" / "vcvarsall.bat";
|
|
|
|
paths_examined.push_back(vcvarsall_bat);
|
|
|
|
if (!fs::exists(vcvarsall_bat))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Locate the "best" MSVC toolchain version
|
|
|
|
const fs::path msvc_path = vc_dir / "Tools" / "MSVC";
|
2017-04-01 08:40:08 +08:00
|
|
|
std::vector<fs::path> msvc_subdirectories;
|
2017-04-01 17:08:48 +08:00
|
|
|
Files::non_recursive_find_matching_paths_in_dir(msvc_path, [](const fs::path& current)
|
2017-04-01 08:40:08 +08:00
|
|
|
{
|
|
|
|
return fs::is_directory(current);
|
|
|
|
}, &msvc_subdirectories);
|
|
|
|
|
|
|
|
// Sort them so that latest comes first
|
2017-04-01 17:08:48 +08:00
|
|
|
std::sort(msvc_subdirectories.begin(), msvc_subdirectories.end(), [](const fs::path& left, const fs::path& right)
|
2017-04-01 08:40:08 +08:00
|
|
|
{
|
|
|
|
return left.filename() > right.filename();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const fs::path& subdir : msvc_subdirectories)
|
|
|
|
{
|
|
|
|
const fs::path dumpbin_path = subdir / "bin" / "HostX86" / "x86" / "dumpbin.exe";
|
|
|
|
paths_examined.push_back(dumpbin_path);
|
|
|
|
if (fs::exists(dumpbin_path))
|
|
|
|
{
|
2017-04-01 17:08:48 +08:00
|
|
|
return { dumpbin_path, vcvarsall_bat , L"v141" };
|
2017-04-01 08:40:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VS2015
|
2017-04-01 18:30:52 +08:00
|
|
|
const optional<fs::path> vs_2015_installation_instance = get_VS2015_installation_instance();
|
2017-04-01 08:40:08 +08:00
|
|
|
if (auto v = vs_2015_installation_instance.get())
|
|
|
|
{
|
|
|
|
const fs::path vs2015_vcvarsall_bat = *v / "VC" / "vcvarsall.bat";
|
|
|
|
|
|
|
|
paths_examined.push_back(vs2015_vcvarsall_bat);
|
|
|
|
if (fs::exists(vs2015_vcvarsall_bat))
|
|
|
|
{
|
2017-04-01 17:08:48 +08:00
|
|
|
const fs::path vs2015_dumpbin_exe = *v / "VC" / "bin" / "dumpbin.exe";
|
|
|
|
paths_examined.push_back(vs2015_dumpbin_exe);
|
|
|
|
if (fs::exists(vs2015_dumpbin_exe))
|
|
|
|
{
|
|
|
|
return { vs2015_dumpbin_exe, vs2015_vcvarsall_bat, L"v140" };
|
|
|
|
}
|
2017-04-01 08:40:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 17:08:48 +08:00
|
|
|
System::println(System::color::error, "Could not locate a complete toolset.");
|
2017-04-01 08:40:08 +08:00
|
|
|
System::println("The following paths were examined:");
|
|
|
|
for (const fs::path& path : paths_examined)
|
|
|
|
{
|
2017-04-01 17:08:48 +08:00
|
|
|
System::println(" %s", path.u8string());
|
2017-04-01 08:40:08 +08:00
|
|
|
}
|
|
|
|
Checks::exit_fail(VCPKG_LINE_INFO);
|
|
|
|
}
|
|
|
|
|
2017-04-01 17:08:48 +08:00
|
|
|
const toolset_t& vcpkg_paths::get_toolset() const
|
2017-04-01 08:40:08 +08:00
|
|
|
{
|
2017-04-01 17:08:48 +08:00
|
|
|
return this->toolset.get_lazy([this]() { return find_toolset_instance(*this); });
|
2017-04-01 08:40:08 +08:00
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|