[vcpkg hash] Don't use cmake for non-windows hashes. Use shasum instead

This commit is contained in:
Alexander Karatarakis 2018-04-05 04:29:43 -07:00
parent 7cfc53a45e
commit 870fa61e01
5 changed files with 31 additions and 34 deletions

View File

@ -131,8 +131,8 @@ namespace vcpkg::Commands
namespace Hash
{
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
std::string get_file_hash(const VcpkgPaths& paths, fs::path const& path, std::string const& hash_type);
void perform_and_exit(const VcpkgCmdArguments& args);
std::string get_file_hash(fs::path const& path, std::string const& hash_type);
}
namespace Fetch

View File

@ -457,9 +457,9 @@ namespace vcpkg::Build
abi_tag_entries.insert(abi_tag_entries.end(), dependency_abis.begin(), dependency_abis.end());
abi_tag_entries.emplace_back(
AbiEntry{"portfile", Commands::Hash::get_file_hash(paths, config.port_dir / "portfile.cmake", "SHA1")});
AbiEntry{"portfile", Commands::Hash::get_file_hash(config.port_dir / "portfile.cmake", "SHA1")});
abi_tag_entries.emplace_back(
AbiEntry{"control", Commands::Hash::get_file_hash(paths, config.port_dir / "CONTROL", "SHA1")});
AbiEntry{"control", Commands::Hash::get_file_hash(config.port_dir / "CONTROL", "SHA1")});
abi_tag_entries.emplace_back(AbiEntry{"triplet", pre_build_info.triplet_abi_tag});
@ -494,7 +494,7 @@ namespace vcpkg::Build
auto abi_file_path = paths.buildtrees / name / (triplet.canonical_name() + ".vcpkg_abi_info.txt");
fs.write_contents(abi_file_path, full_abi_info);
return AbiTagAndFile{Commands::Hash::get_file_hash(paths, abi_file_path, "SHA1"), abi_file_path};
return AbiTagAndFile{Commands::Hash::get_file_hash(abi_file_path, "SHA1"), abi_file_path};
}
else
{
@ -784,7 +784,7 @@ namespace vcpkg::Build
{
return it_hash->second;
}
auto hash = Commands::Hash::get_file_hash(paths, triplet_file_path, "SHA1");
auto hash = Commands::Hash::get_file_hash(triplet_file_path, "SHA1");
s_hash_cache.emplace(triplet_file_path, hash);
return hash;
}

View File

@ -42,7 +42,6 @@ namespace vcpkg::Commands
{"cache", &Cache::perform_and_exit},
{"portsdiff", &PortsDiff::perform_and_exit},
{"autocomplete", &Autocomplete::perform_and_exit},
{"hash", &Hash::perform_and_exit},
// {"fetch", &Fetch::perform_and_exit},
};
return t;
@ -50,8 +49,11 @@ namespace vcpkg::Commands
Span<const PackageNameAndFunction<CommandTypeC>> get_available_commands_type_c()
{
static std::vector<PackageNameAndFunction<CommandTypeC>> t = {{"version", &Version::perform_and_exit},
{"contact", &Contact::perform_and_exit}};
static std::vector<PackageNameAndFunction<CommandTypeC>> t = {
{"version", &Version::perform_and_exit},
{"contact", &Contact::perform_and_exit},
{"hash", &Hash::perform_and_exit},
};
return t;
}
}

View File

@ -240,7 +240,7 @@ namespace vcpkg::Commands::Fetch
R"(curl -L '%s' --create-dirs --output '%s')", url, download_path_part));
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "Could not download %s", url);
const std::string actual_hash = Hash::get_file_hash(paths, download_path_part, "SHA512");
const std::string actual_hash = Hash::get_file_hash(download_path_part, "SHA512");
Checks::check_exit(VCPKG_LINE_INFO,
sha512 == actual_hash,
"File does not have the expected hash:\n"

View File

@ -60,7 +60,7 @@ namespace vcpkg::Commands::Hash
};
}
std::string get_file_hash(const VcpkgPaths&, const fs::path& path, const std::string& hash_type)
std::string get_file_hash(const fs::path& path, const std::string& hash_type)
{
BCryptAlgorithmHandle algorithm_handle;
@ -108,30 +108,25 @@ namespace vcpkg::Commands::Hash
#else
namespace vcpkg::Commands::Hash
{
std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type)
std::string get_file_hash(const fs::path& path, const std::string& hash_type)
{
if (!Strings::case_insensitive_ascii_starts_with(hash_type, "SHA"))
{
Checks::exit_with_message(
VCPKG_LINE_INFO, "shasum only supports SHA hashes, but %s was provided", hash_type);
}
const std::string digest_size = hash_type.substr(3, hash_type.length() - 3);
const std::string cmd_line = Strings::format(
R"("%s" -E %ssum "%s")",
paths.get_tool_exe(Tools::CMAKE).u8string(),
Strings::ascii_to_lowercase(hash_type),
path.u8string());
R"(shasum -a %s "%s" | awk '{ print $1 }')", digest_size, path.u8string());
const auto ec_data = System::cmd_execute_and_capture_output(cmd_line);
Checks::check_exit(VCPKG_LINE_INFO, ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line);
std::string const& output = ec_data.output;
const auto start = output.find_first_of(' ');
Checks::check_exit(
VCPKG_LINE_INFO, start != std::string::npos, "Unexpected output format from command: %s", cmd_line);
const auto end = output.find_first_of("\r\n", start + 1);
Checks::check_exit(
VCPKG_LINE_INFO, end != std::string::npos, "Unexpected output format from command: %s", cmd_line);
auto hash = output.substr(0, start);
Util::erase_remove_if(hash, isspace);
return hash;
Checks::check_exit(VCPKG_LINE_INFO,
ec_data.exit_code == 0,
"Failed to run:\n"
" %s",
cmd_line);
return Strings::trim(std::string{ec_data.output});
}
}
#endif
@ -147,13 +142,13 @@ namespace vcpkg::Commands::Hash
nullptr,
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
void perform_and_exit(const VcpkgCmdArguments& args)
{
Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
const fs::path file_to_hash = args.command_arguments[0];
const std::string algorithm = args.command_arguments.size() == 2 ? args.command_arguments[1] : "SHA512";
const std::string hash = get_file_hash(paths, file_to_hash, algorithm);
const std::string hash = get_file_hash(file_to_hash, algorithm);
System::println(hash);
Checks::exit_success(VCPKG_LINE_INFO);
}