PostBuildLint enum revamp

This commit is contained in:
Alexander Karatarakis 2017-02-09 19:00:09 -08:00
parent 7a0404cb83
commit 95650bdd42
11 changed files with 199 additions and 135 deletions

View File

@ -12,8 +12,8 @@ namespace vcpkg::PostBuildLint
{
static BuildInfo create(std::unordered_map<std::string, std::string> pgh);
LinkageType crt_linkage;
LinkageType library_linkage;
LinkageType::type crt_linkage;
LinkageType::type library_linkage;
std::map<BuildPolicies::type, opt_bool_t> policies;
};

View File

@ -1,17 +1,19 @@
#pragma once
#include <string>
#include <array>
namespace vcpkg::PostBuildLint::BuildPolicies
{
enum class backing_enum_t
{
UNKNOWN = 0,
NULLVALUE = 0,
EMPTY_PACKAGE,
DLLS_WITHOUT_LIBS
};
struct type
{
constexpr type() : backing_enum(backing_enum_t::NULLVALUE) {}
constexpr explicit type(backing_enum_t backing_enum) : backing_enum(backing_enum) { }
constexpr operator backing_enum_t() const { return backing_enum; }
@ -19,17 +21,16 @@ namespace vcpkg::PostBuildLint::BuildPolicies
const std::string& cmake_variable() const;
private:
type();
backing_enum_t backing_enum;
};
static constexpr int value_count = 3;
const std::vector<type>& values();
static const std::string ENUM_NAME = "vcpkg::PostBuildLint::BuildPolicies";
static constexpr type UNKNOWN(backing_enum_t::UNKNOWN);
static constexpr type NULLVALUE(backing_enum_t::NULLVALUE);
static constexpr type EMPTY_PACKAGE(backing_enum_t::EMPTY_PACKAGE);
static constexpr type DLLS_WITHOUT_LIBS(backing_enum_t::DLLS_WITHOUT_LIBS);
static constexpr std::array<type, 2> values = { EMPTY_PACKAGE, DLLS_WITHOUT_LIBS };
type parse(const std::string& s);
}

View File

@ -1,45 +1,47 @@
#pragma once
#include "PostBuildLint_ConfigurationType.h"
#include "PostBuildLint_LinkageType.h"
#include <vector>
#include <array>
#include <regex>
namespace vcpkg::PostBuildLint
namespace vcpkg::PostBuildLint::BuildType
{
struct BuildType
enum class backing_enum_t
{
static BuildType value_of(const ConfigurationType& config, const LinkageType& linkage);
static const BuildType DEBUG_STATIC;
static const BuildType DEBUG_DYNAMIC;
static const BuildType RELEASE_STATIC;
static const BuildType RELEASE_DYNAMIC;
static const std::vector<BuildType>& values()
{
static const std::vector<BuildType> v = { DEBUG_STATIC, DEBUG_DYNAMIC, RELEASE_STATIC, RELEASE_DYNAMIC };
return v;
}
BuildType() = delete;
const ConfigurationType& config() const;
const LinkageType& linkage() const;
std::regex crt_regex() const;
std::string toString() const;
private:
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;
DEBUG_STATIC = 1,
DEBUG_DYNAMIC,
RELEASE_STATIC,
RELEASE_DYNAMIC
};
bool operator ==(const BuildType& lhs, const BuildType& rhs);
struct type
{
type() = delete;
bool operator !=(const BuildType& lhs, const BuildType& rhs);
constexpr explicit type(const backing_enum_t backing_enum, const ConfigurationType::type config, const LinkageType::type linkage) :
backing_enum(backing_enum), m_config(config), m_linkage(linkage) { }
constexpr operator backing_enum_t() const { return backing_enum; }
const ConfigurationType::type& config() const;
const LinkageType::type& linkage() const;
const std::regex& crt_regex() const;
const std::string& toString() const;
private:
backing_enum_t backing_enum;
ConfigurationType::type m_config;
LinkageType::type m_linkage;
};
static const std::string ENUM_NAME = "vcpkg::PostBuildLint::BuildType";
static constexpr type DEBUG_STATIC = type(backing_enum_t::DEBUG_STATIC, ConfigurationType::DEBUG, LinkageType::STATIC);
static constexpr type DEBUG_DYNAMIC = type(backing_enum_t::DEBUG_DYNAMIC, ConfigurationType::DEBUG, LinkageType::DYNAMIC);
static constexpr type RELEASE_STATIC = type(backing_enum_t::RELEASE_STATIC, ConfigurationType::RELEASE, LinkageType::STATIC);
static constexpr type RELEASE_DYNAMIC = type(backing_enum_t::RELEASE_DYNAMIC, ConfigurationType::RELEASE, LinkageType::DYNAMIC);
static constexpr std::array<type, 4> values = { DEBUG_STATIC, DEBUG_DYNAMIC, RELEASE_STATIC, RELEASE_DYNAMIC };
type value_of(const ConfigurationType::type& config, const LinkageType::type& linkage);
}

View File

@ -2,13 +2,32 @@
#pragma once
#include <string>
namespace vcpkg::PostBuildLint
namespace vcpkg::PostBuildLint::ConfigurationType
{
enum class ConfigurationType
enum class backing_enum_t
{
NULLVALUE = 0,
DEBUG = 1,
RELEASE = 2
};
std::string to_string(const ConfigurationType& conf);
struct type
{
constexpr type() : backing_enum(backing_enum_t::NULLVALUE) {}
constexpr explicit type(backing_enum_t backing_enum) : backing_enum(backing_enum) { }
constexpr operator backing_enum_t() const { return backing_enum; }
const std::string& toString() const;
private:
backing_enum_t backing_enum;
};
static const std::string ENUM_NAME = "vcpkg::PostBuildLint::ConfigurationType";
static constexpr type NULLVALUE(backing_enum_t::NULLVALUE);
static constexpr type DEBUG(backing_enum_t::DEBUG);
static constexpr type RELEASE(backing_enum_t::RELEASE);
static constexpr std::array<type, 2> values = { DEBUG, RELEASE };
}

View File

@ -1,16 +1,34 @@
#pragma once
#include <string>
namespace vcpkg::PostBuildLint
namespace vcpkg::PostBuildLint::LinkageType
{
enum class LinkageType
enum class backing_enum_t
{
NULLVALUE = 0,
DYNAMIC,
STATIC,
UNKNOWN
STATIC
};
LinkageType linkage_type_value_of(const std::string& as_string);
struct type
{
constexpr type() : backing_enum(backing_enum_t::NULLVALUE) {}
constexpr explicit type(backing_enum_t backing_enum) : backing_enum(backing_enum) { }
constexpr operator backing_enum_t() const { return backing_enum; }
std::string to_string(const LinkageType& build_info);
const std::string& toString() const;
private:
backing_enum_t backing_enum;
};
static const std::string ENUM_NAME = "vcpkg::PostBuildLint::LinkageType";
static constexpr type NULLVALUE(backing_enum_t::NULLVALUE);
static constexpr type DYNAMIC(backing_enum_t::DYNAMIC);
static constexpr type STATIC(backing_enum_t::STATIC);
static constexpr std::array<type, 2> values = { DYNAMIC, STATIC };
type value_of(const std::string& as_string);
}

View File

@ -500,12 +500,12 @@ namespace vcpkg::PostBuildLint
struct BuildType_and_file
{
fs::path file;
BuildType build_type;
BuildType::type build_type;
};
static lint_status check_crt_linkage_of_libs(const BuildType& expected_build_type, const std::vector<fs::path>& libs, const fs::path dumpbin_exe)
static lint_status check_crt_linkage_of_libs(const BuildType::type& expected_build_type, const std::vector<fs::path>& libs, const fs::path dumpbin_exe)
{
std::vector<BuildType> bad_build_types = BuildType::values();
std::vector<BuildType::type> bad_build_types(BuildType::values.cbegin(), BuildType::values.cend());
bad_build_types.erase(std::remove(bad_build_types.begin(), bad_build_types.end(), expected_build_type), bad_build_types.end());
std::vector<BuildType_and_file> libs_with_invalid_crt;
@ -516,7 +516,7 @@ namespace vcpkg::PostBuildLint
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));
for (const BuildType& bad_build_type : bad_build_types)
for (const BuildType::type& bad_build_type : bad_build_types)
{
if (std::regex_search(ec_data.output.cbegin(), ec_data.output.cend(), bad_build_type.crt_regex()))
{
@ -662,7 +662,7 @@ namespace vcpkg::PostBuildLint
switch (build_info.library_linkage)
{
case LinkageType::DYNAMIC:
case LinkageType::backing_enum_t::DYNAMIC:
{
const std::vector<fs::path> debug_dlls = Files::recursive_find_files_with_extension_in_dir(debug_bin_dir, ".dll");
const std::vector<fs::path> release_dlls = Files::recursive_find_files_with_extension_in_dir(release_bin_dir, ".dll");
@ -683,7 +683,7 @@ namespace vcpkg::PostBuildLint
error_count += check_outdated_crt_linkage_of_dlls(dlls, dumpbin_exe);
break;
}
case LinkageType::STATIC:
case LinkageType::backing_enum_t::STATIC:
{
std::vector<fs::path> dlls;
Files::recursive_find_files_with_extension_in_dir(package_dir, ".dll", &dlls);
@ -695,7 +695,7 @@ namespace vcpkg::PostBuildLint
error_count += check_crt_linkage_of_libs(BuildType::value_of(ConfigurationType::RELEASE, build_info.crt_linkage), release_libs, dumpbin_exe);
break;
}
case LinkageType::UNKNOWN:
case LinkageType::backing_enum_t::NULLVALUE:
default:
Checks::unreachable();
}

View File

@ -17,18 +17,18 @@ namespace vcpkg::PostBuildLint
{
BuildInfo build_info;
const std::string crt_linkage_as_string = details::remove_required_field(&pgh, BuildInfoRequiredField::CRT_LINKAGE);
build_info.crt_linkage = linkage_type_value_of(crt_linkage_as_string);
Checks::check_exit(build_info.crt_linkage != LinkageType::UNKNOWN, "Invalid crt linkage type: [%s]", crt_linkage_as_string);
build_info.crt_linkage = LinkageType::value_of(crt_linkage_as_string);
Checks::check_exit(build_info.crt_linkage != LinkageType::NULLVALUE, "Invalid crt linkage type: [%s]", crt_linkage_as_string);
const std::string library_linkage_as_string = details::remove_required_field(&pgh, BuildInfoRequiredField::LIBRARY_LINKAGE);
build_info.library_linkage = linkage_type_value_of(library_linkage_as_string);
Checks::check_exit(build_info.library_linkage != LinkageType::UNKNOWN, "Invalid library linkage type: [%s]", library_linkage_as_string);
build_info.library_linkage = LinkageType::value_of(library_linkage_as_string);
Checks::check_exit(build_info.library_linkage != LinkageType::NULLVALUE, "Invalid library linkage type: [%s]", library_linkage_as_string);
// The remaining entries are policies
for (const std::unordered_map<std::string, std::string>::value_type& p : pgh)
{
const BuildPolicies::type policy = BuildPolicies::parse(p.first);
Checks::check_exit(policy != BuildPolicies::UNKNOWN, "Unknown policy found: %s", p.first);
Checks::check_exit(policy != BuildPolicies::NULLVALUE, "Unknown policy found: %s", p.first);
const opt_bool_t status = opt_bool::parse(p.second);
build_info.policies.emplace(policy, status);
}

View File

@ -1,10 +1,11 @@
#include "pch.h"
#include "PostBuildLint_BuildPolicies.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Enums.h"
namespace vcpkg::PostBuildLint::BuildPolicies
{
static const std::string NAME_UNKNOWN = "PolicyUnknown";
static const std::string NULLVALUE_STRING = Enums::nullvalue_toString(ENUM_NAME);
static const std::string NAME_EMPTY_PACKAGE = "PolicyEmptyPackage";
static const std::string NAME_DLLS_WITHOUT_LIBS = "PolicyDLLsWithoutLIBs";
@ -16,10 +17,10 @@ namespace vcpkg::PostBuildLint::BuildPolicies
return NAME_EMPTY_PACKAGE;
case DLLS_WITHOUT_LIBS:
return NAME_DLLS_WITHOUT_LIBS;
case UNKNOWN:
return NAME_UNKNOWN;
case NULLVALUE:
return NULLVALUE_STRING;
default:
Checks::unreachable();
Enums::unreachable(ENUM_NAME);
}
}
@ -34,21 +35,13 @@ namespace vcpkg::PostBuildLint::BuildPolicies
return CMAKE_VARIABLE_EMPTY_PACKAGE;
case DLLS_WITHOUT_LIBS:
return CMAKE_VARIABLE_DLLS_WITHOUT_LIBS;
case UNKNOWN:
Checks::exit_with_message("No CMake command corresponds to UNKNOWN");
case NULLVALUE:
Enums::nullvalue_used(ENUM_NAME);
default:
Checks::unreachable();
Enums::unreachable(ENUM_NAME);
}
}
type::type(): backing_enum(backing_enum_t::UNKNOWN) {}
const std::vector<type>& values()
{
static const std::vector<type>& v = {UNKNOWN, EMPTY_PACKAGE, DLLS_WITHOUT_LIBS};
return v;
}
type parse(const std::string& s)
{
if (s == NAME_EMPTY_PACKAGE)
@ -61,6 +54,6 @@ namespace vcpkg::PostBuildLint::BuildPolicies
return BuildPolicies::DLLS_WITHOUT_LIBS;
}
return BuildPolicies::UNKNOWN;
return BuildPolicies::NULLVALUE;
}
}

View File

@ -1,15 +1,10 @@
#include "pch.h"
#include "PostBuildLint_BuildType.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Enums.h"
namespace vcpkg::PostBuildLint
namespace vcpkg::PostBuildLint::BuildType
{
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])");
BuildType BuildType::value_of(const ConfigurationType& config, const LinkageType& linkage)
type value_of(const ConfigurationType::type& config, const LinkageType::type& linkage)
{
if (config == ConfigurationType::DEBUG && linkage == LinkageType::STATIC)
{
@ -31,38 +26,60 @@ namespace vcpkg::PostBuildLint
return RELEASE_DYNAMIC;
}
Checks::unreachable();
Enums::unreachable(ENUM_NAME);
}
const ConfigurationType& BuildType::config() const
const ConfigurationType::type& type::config() const
{
return this->m_config;
}
const LinkageType& BuildType::linkage() const
const LinkageType::type& type::linkage() const
{
return this->m_linkage;
}
std::regex BuildType::crt_regex() const
const std::regex& type::crt_regex() const
{
const std::regex r(this->m_crt_regex_as_string, std::regex_constants::icase);
return r;
static const std::regex REGEX_DEBUG_STATIC(R"(/DEFAULTLIB:LIBCMTD)", std::regex_constants::icase);
static const std::regex REGEX_DEBUG_DYNAMIC(R"(/DEFAULTLIB:MSVCRTD)", std::regex_constants::icase);
static const std::regex REGEX_RELEASE_STATIC(R"(/DEFAULTLIB:LIBCMT[^D])", std::regex_constants::icase);
static const std::regex REGEX_RELEASE_DYNAMIC(R"(/DEFAULTLIB:MSVCRT[^D])", std::regex_constants::icase);
switch (backing_enum)
{
case BuildType::DEBUG_STATIC:
return REGEX_DEBUG_STATIC;
case BuildType::DEBUG_DYNAMIC:
return REGEX_DEBUG_DYNAMIC;
case BuildType::RELEASE_STATIC:
return REGEX_RELEASE_STATIC;
case BuildType::RELEASE_DYNAMIC:
return REGEX_RELEASE_DYNAMIC;
default:
Enums::unreachable(ENUM_NAME);
}
}
std::string BuildType::toString() const
const std::string& type::toString() const
{
const std::string s = Strings::format("[%s,%s]", to_string(this->m_config), to_string(this->m_linkage));
return s;
}
static const std::string NAME_DEBUG_STATIC("Debug,Static");
static const std::string NAME_DEBUG_DYNAMIC("Debug,Dynamic");
static const std::string NAME_RELEASE_STATIC("Release,Static");
static const std::string NAME_RELEASE_DYNAMIC("Release,Dynamic");
bool operator==(const BuildType& lhs, const BuildType& rhs)
{
return lhs.config() == rhs.config() && lhs.linkage() == rhs.linkage();
}
bool operator!=(const BuildType& lhs, const BuildType& rhs)
{
return !(lhs == rhs);
switch (backing_enum)
{
case BuildType::DEBUG_STATIC:
return NAME_DEBUG_STATIC;
case BuildType::DEBUG_DYNAMIC:
return NAME_DEBUG_DYNAMIC;
case BuildType::RELEASE_STATIC:
return NAME_RELEASE_STATIC;
case BuildType::RELEASE_DYNAMIC:
return NAME_RELEASE_DYNAMIC;
default:
Enums::unreachable(ENUM_NAME);
}
}
}

View File

@ -1,19 +1,26 @@
#include "pch.h"
#include "PostBuildLint_ConfigurationType.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Enums.h"
namespace vcpkg::PostBuildLint
namespace vcpkg::PostBuildLint::ConfigurationType
{
std::string to_string(const ConfigurationType& conf)
static const std::string NULLVALUE_STRING = Enums::nullvalue_toString(ENUM_NAME);
static const std::string NAME_DEBUG = "Debug";
static const std::string NAME_RELEASE = "Release";
const std::string& type::toString() const
{
switch (conf)
switch (this->backing_enum)
{
case ConfigurationType::DEBUG:
return "Debug";
case ConfigurationType::RELEASE:
return "Release";
default:
Checks::unreachable();
case ConfigurationType::DEBUG:
return NAME_DEBUG;
case ConfigurationType::RELEASE:
return NAME_RELEASE;
case ConfigurationType::NULLVALUE:
return NULLVALUE_STRING;
default:
Enums::unreachable(ENUM_NAME);
}
}
}

View File

@ -1,34 +1,41 @@
#include "pch.h"
#include "PostBuildLint_LinkageType.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Enums.h"
namespace vcpkg::PostBuildLint
namespace vcpkg::PostBuildLint::LinkageType
{
LinkageType linkage_type_value_of(const std::string& as_string)
static const std::string NULLVALUE_STRING = Enums::nullvalue_toString(ENUM_NAME);
static const std::string NAME_DYNAMIC = "dynamic";
static const std::string NAME_STATIC = "static";
const std::string& type::toString() const
{
if (as_string == "dynamic")
switch (this->backing_enum)
{
case LinkageType::DYNAMIC:
return NAME_DYNAMIC;
case LinkageType::STATIC:
return NAME_STATIC;
case LinkageType::NULLVALUE:
return NULLVALUE_STRING;
default:
Enums::unreachable(ENUM_NAME);
}
}
type value_of(const std::string& as_string)
{
if (as_string == NAME_DYNAMIC)
{
return LinkageType::DYNAMIC;
}
if (as_string == "static")
if (as_string == NAME_STATIC)
{
return LinkageType::STATIC;
}
return LinkageType::UNKNOWN;
}
std::string to_string(const LinkageType& build_info)
{
switch (build_info)
{
case LinkageType::STATIC:
return "static";
case LinkageType::DYNAMIC:
return "dynamic";
default:
Checks::unreachable();
}
return LinkageType::NULLVALUE;
}
}