mirror of https://github.com/microsoft/vcpkg.git
[post-build-checks] Rework crt linkage checks
This commit is contained in:
parent
968fb2768d
commit
bf7978dcf9
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <unordered_map>
|
||||
#include "Paragraphs.h"
|
||||
#include <regex>
|
||||
|
||||
namespace fs = std::tr2::sys;
|
||||
|
||||
|
|
@ -35,17 +36,33 @@ namespace vcpkg
|
|||
static const BuildType RELEASE_STATIC;
|
||||
static const BuildType RELEASE_DYNAMIC;
|
||||
|
||||
const ConfigurationType config;
|
||||
const LinkageType linkage;
|
||||
static constexpr int length()
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
|
||||
static const std::vector<BuildType>& values()
|
||||
{
|
||||
static const std::vector<BuildType> v = {DEBUG_STATIC, DEBUG_DYNAMIC, RELEASE_STATIC, RELEASE_DYNAMIC};
|
||||
return v;
|
||||
}
|
||||
|
||||
BuildType() = delete;
|
||||
|
||||
std::string toString() const;
|
||||
const ConfigurationType& config() const;
|
||||
const LinkageType& linkage() const;
|
||||
const std::regex& crt_regex() const;
|
||||
const std::string& toString() const;
|
||||
|
||||
private:
|
||||
BuildType(const ConfigurationType& config, const LinkageType& linkage) : config(config), linkage(linkage)
|
||||
BuildType(const ConfigurationType& config, const LinkageType& linkage, const std::string& crt_regex_as_string)
|
||||
: m_config(config), m_linkage(linkage), m_crt_regex_as_string(crt_regex_as_string)
|
||||
{
|
||||
}
|
||||
|
||||
ConfigurationType m_config;
|
||||
LinkageType m_linkage;
|
||||
std::string m_crt_regex_as_string;
|
||||
};
|
||||
|
||||
bool operator ==(const BuildType& lhs, const BuildType& rhs);
|
||||
|
|
|
|||
|
|
@ -4,14 +4,31 @@
|
|||
|
||||
namespace vcpkg
|
||||
{
|
||||
std::string BuildType::toString() const
|
||||
const ConfigurationType& BuildType::config() const
|
||||
{
|
||||
return Strings::format("[%s,%s]", to_string(config), to_string(linkage));
|
||||
return this->m_config;
|
||||
}
|
||||
|
||||
const LinkageType& BuildType::linkage() const
|
||||
{
|
||||
return this->m_linkage;
|
||||
}
|
||||
|
||||
const std::regex& BuildType::crt_regex() const
|
||||
{
|
||||
static const std::regex r(this->m_crt_regex_as_string);
|
||||
return r;
|
||||
}
|
||||
|
||||
const std::string& BuildType::toString() const
|
||||
{
|
||||
static const std::string s = Strings::format("[%s,%s]", to_string(this->m_config), to_string(this->m_linkage));
|
||||
return s;
|
||||
}
|
||||
|
||||
bool operator==(const BuildType& lhs, const BuildType& rhs)
|
||||
{
|
||||
return lhs.config == rhs.config && lhs.linkage == rhs.linkage;
|
||||
return lhs.config() == rhs.config() && lhs.linkage() == rhs.linkage();
|
||||
}
|
||||
|
||||
bool operator!=(const BuildType& lhs, const BuildType& rhs)
|
||||
|
|
@ -35,10 +52,10 @@ namespace vcpkg
|
|||
return build_info;
|
||||
}
|
||||
|
||||
const BuildType BuildType::DEBUG_STATIC = BuildType(ConfigurationType::DEBUG, LinkageType::STATIC);
|
||||
const BuildType BuildType::DEBUG_DYNAMIC = BuildType(ConfigurationType::DEBUG, LinkageType::DYNAMIC);
|
||||
const BuildType BuildType::RELEASE_STATIC = BuildType(ConfigurationType::RELEASE, LinkageType::STATIC);
|
||||
const BuildType BuildType::RELEASE_DYNAMIC = BuildType(ConfigurationType::RELEASE, LinkageType::DYNAMIC);
|
||||
const BuildType BuildType::DEBUG_STATIC = BuildType(ConfigurationType::DEBUG, LinkageType::STATIC, R"(/DEFAULTLIB:LIBCMTD)");
|
||||
const BuildType BuildType::DEBUG_DYNAMIC = BuildType(ConfigurationType::DEBUG, LinkageType::DYNAMIC, R"(/DEFAULTLIB:MSVCRTD)");
|
||||
const BuildType BuildType::RELEASE_STATIC = BuildType(ConfigurationType::RELEASE, LinkageType::STATIC, R"(/DEFAULTLIB:LIBCMT[^D])");
|
||||
const BuildType BuildType::RELEASE_DYNAMIC = BuildType(ConfigurationType::RELEASE, LinkageType::DYNAMIC, R"(/DEFAULTLIB:MSVCRT[^D])");
|
||||
|
||||
LinkageType linkage_type_value_of(const std::string& as_string)
|
||||
|
||||
|
|
|
|||
|
|
@ -489,32 +489,18 @@ namespace vcpkg
|
|||
return lint_status::SUCCESS;
|
||||
}
|
||||
|
||||
struct BuildType_and_files
|
||||
struct BuildType_and_file
|
||||
{
|
||||
explicit BuildType_and_files(const BuildType& build_type) : build_type(build_type)
|
||||
{
|
||||
}
|
||||
|
||||
fs::path file;
|
||||
BuildType build_type;
|
||||
std::vector<fs::path> files;
|
||||
};
|
||||
|
||||
static lint_status check_crt_linkage_of_libs(const BuildType& expected_build_type, const std::vector<fs::path>& libs)
|
||||
{
|
||||
static const std::regex DEBUG_STATIC_CRT(R"(/DEFAULTLIB:LIBCMTD)");
|
||||
static const std::regex DEBUG_DYNAMIC_CRT(R"(/DEFAULTLIB:MSVCRTD)");
|
||||
std::vector<BuildType> bad_build_types = BuildType::values();
|
||||
bad_build_types.erase(std::remove(bad_build_types.begin(), bad_build_types.end(), expected_build_type), bad_build_types.end());
|
||||
|
||||
static const std::regex RELEASE_STATIC_CRT(R"(/DEFAULTLIB:LIBCMT[^D])");
|
||||
static const std::regex RELEASE_DYNAMIC_CRT(R"(/DEFAULTLIB:MSVCRT[^D])");
|
||||
|
||||
lint_status output_status = lint_status::SUCCESS;
|
||||
|
||||
std::vector<fs::path> libs_with_multiple_crts;
|
||||
|
||||
BuildType_and_files libs_with_debug_static_crt(BuildType::DEBUG_STATIC);
|
||||
BuildType_and_files libs_with_debug_dynamic_crt(BuildType::DEBUG_DYNAMIC);
|
||||
BuildType_and_files libs_with_release_static_crt(BuildType::RELEASE_STATIC);
|
||||
BuildType_and_files libs_with_release_dynamic_crt(BuildType::RELEASE_DYNAMIC);
|
||||
std::vector<BuildType_and_file> libs_with_invalid_crt;
|
||||
|
||||
for (const fs::path& lib : libs)
|
||||
{
|
||||
|
|
@ -522,73 +508,31 @@ namespace vcpkg
|
|||
System::exit_code_and_output ec_data = System::cmd_execute_and_capture_output(cmd_line);
|
||||
Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", Strings::utf16_to_utf8(cmd_line));
|
||||
|
||||
bool found_debug_static_crt = std::regex_search(ec_data.output.cbegin(), ec_data.output.cend(), DEBUG_STATIC_CRT);
|
||||
bool found_debug_dynamic_crt = std::regex_search(ec_data.output.cbegin(), ec_data.output.cend(), DEBUG_DYNAMIC_CRT);
|
||||
bool found_release_static_crt = std::regex_search(ec_data.output.cbegin(), ec_data.output.cend(), RELEASE_STATIC_CRT);
|
||||
bool found_release_dynamic_crt = std::regex_search(ec_data.output.cbegin(), ec_data.output.cend(), RELEASE_DYNAMIC_CRT);
|
||||
|
||||
const size_t crts_found_count = found_debug_static_crt + found_debug_dynamic_crt + found_release_static_crt + found_release_dynamic_crt;
|
||||
|
||||
if (crts_found_count == 0)
|
||||
for (const BuildType& bad_build_type : bad_build_types)
|
||||
{
|
||||
// It can be valid for no crt to be detected. For example: openssl
|
||||
continue;
|
||||
}
|
||||
|
||||
if (crts_found_count > 1)
|
||||
{
|
||||
libs_with_multiple_crts.push_back(lib);
|
||||
continue;
|
||||
}
|
||||
|
||||
// now we have exactly 1 crt
|
||||
if (found_debug_static_crt)
|
||||
{
|
||||
libs_with_debug_static_crt.files.push_back(lib);
|
||||
continue;
|
||||
}
|
||||
if (found_debug_dynamic_crt)
|
||||
{
|
||||
libs_with_debug_dynamic_crt.files.push_back(lib);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (found_release_static_crt)
|
||||
{
|
||||
libs_with_release_static_crt.files.push_back(lib);
|
||||
continue;
|
||||
}
|
||||
|
||||
libs_with_release_dynamic_crt.files.push_back(lib);
|
||||
}
|
||||
|
||||
if (!libs_with_multiple_crts.empty())
|
||||
{
|
||||
System::println(System::color::warning, "Detected multiple crt linkages for the following libs:");
|
||||
print_vector_of_files(libs_with_multiple_crts);
|
||||
output_status = lint_status::ERROR_DETECTED;
|
||||
}
|
||||
|
||||
std::vector<BuildType_and_files> group_for_iteration = {
|
||||
libs_with_debug_static_crt, libs_with_debug_dynamic_crt,
|
||||
libs_with_release_static_crt, libs_with_release_dynamic_crt};
|
||||
|
||||
for (const BuildType_and_files& bif : group_for_iteration)
|
||||
{
|
||||
if (!bif.files.empty() && bif.build_type != expected_build_type)
|
||||
{
|
||||
System::println(System::color::warning, "Expected %s crt linkage, but the following libs had %s crt linkage:", expected_build_type.toString(), bif.build_type.toString());
|
||||
print_vector_of_files(bif.files);
|
||||
output_status = lint_status::ERROR_DETECTED;
|
||||
if (std::regex_search(ec_data.output.cbegin(), ec_data.output.cend(), bad_build_type.crt_regex()))
|
||||
{
|
||||
libs_with_invalid_crt.push_back({lib, bad_build_type});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (output_status == lint_status::ERROR_DETECTED)
|
||||
if (!libs_with_invalid_crt.empty())
|
||||
{
|
||||
System::println(System::color::warning, "Expected %s crt linkage, but the following libs had invalid crt linkage:", expected_build_type.toString());
|
||||
System::println("");
|
||||
for (const BuildType_and_file btf : libs_with_invalid_crt)
|
||||
{
|
||||
System::println(" %s: %s", btf.file.generic_string(), btf.build_type.toString());
|
||||
}
|
||||
System::println("");
|
||||
|
||||
System::println(System::color::warning, "To inspect the lib files, use:\n dumpbin.exe /directives mylibfile.lib");
|
||||
return lint_status::ERROR_DETECTED;
|
||||
}
|
||||
|
||||
return output_status;
|
||||
return lint_status::SUCCESS;
|
||||
}
|
||||
|
||||
static void operator +=(size_t& left, const lint_status& right)
|
||||
|
|
|
|||
Loading…
Reference in New Issue