[vcpkg] Refactored to simplify BuildPolicies into BuildPolicy.

Restrict policy consumers to a simpler interface than std::map.
Rename vcpkg::getMachineType -> vcpkg::to_machine_type.
This commit is contained in:
Robert Schumacher 2017-05-24 00:44:00 -07:00
parent e9b561fa48
commit 6be01a12db
13 changed files with 138 additions and 182 deletions

3
.gitattributes vendored
View File

@ -1 +1,4 @@
*.patch text eol=lf
*.vcxproj text eol=crlf
*.vcxproj.filters text eol=crlf
*.sln text eol=crlf

View File

@ -32,5 +32,5 @@ namespace vcpkg
WCEMIPSV2 = 0x169, // MIPS little-endian WCE v2
};
MachineType getMachineType(const uint16_t value);
MachineType to_machine_type(const uint16_t value);
}

View File

@ -1,47 +0,0 @@
#pragma once
#include "CStringView.h"
#include <array>
#include <string>
namespace vcpkg::PostBuildLint
{
struct BuildPolicies final
{
enum class BackingEnum
{
NULLVALUE = 0,
EMPTY_PACKAGE,
DLLS_WITHOUT_LIBS,
ONLY_RELEASE_CRT,
EMPTY_INCLUDE_FOLDER,
ALLOW_OBSOLETE_MSVCRT
};
static BuildPolicies parse(const std::string& s);
constexpr BuildPolicies() : backing_enum(BackingEnum::NULLVALUE) {}
constexpr explicit BuildPolicies(BackingEnum backing_enum) : backing_enum(backing_enum) {}
constexpr operator BackingEnum() const { return backing_enum; }
const std::string& to_string() const;
const std::string& cmake_variable() const;
private:
BackingEnum backing_enum;
};
namespace BuildPoliciesC
{
static constexpr CStringView ENUM_NAME = "vcpkg::PostBuildLint::BuildPolicies";
static constexpr BuildPolicies NULLVALUE(BuildPolicies::BackingEnum::NULLVALUE);
static constexpr BuildPolicies EMPTY_PACKAGE(BuildPolicies::BackingEnum::EMPTY_PACKAGE);
static constexpr BuildPolicies DLLS_WITHOUT_LIBS(BuildPolicies::BackingEnum::DLLS_WITHOUT_LIBS);
static constexpr BuildPolicies ONLY_RELEASE_CRT(BuildPolicies::BackingEnum::ONLY_RELEASE_CRT);
static constexpr BuildPolicies EMPTY_INCLUDE_FOLDER(BuildPolicies::BackingEnum::EMPTY_INCLUDE_FOLDER);
static constexpr BuildPolicies ALLOW_OBSOLETE_MSVCRT(BuildPolicies::BackingEnum::ALLOW_OBSOLETE_MSVCRT);
static constexpr std::array<BuildPolicies, 5> VALUES = {
EMPTY_PACKAGE, DLLS_WITHOUT_LIBS, ONLY_RELEASE_CRT, EMPTY_INCLUDE_FOLDER, ALLOW_OBSOLETE_MSVCRT};
}
}

View File

@ -31,6 +31,7 @@
#include <sys/timeb.h>
#include <system_error>
#include <time.h>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>

View File

@ -1,12 +1,13 @@
#pragma once
#include "CStringView.h"
#include "PackageSpec.h"
#include "PostBuildLint_BuildPolicies.h"
#include "PostBuildLint_LinkageType.h"
#include "StatusParagraphs.h"
#include "VcpkgPaths.h"
#include "vcpkg_Files.h"
#include "vcpkg_optional.h"
#include <map>
#include <unordered_map>
#include <vector>
@ -69,16 +70,46 @@ namespace vcpkg::Build
const BuildPackageConfig& config,
const StatusParagraphs& status_db);
enum class BuildPolicy
{
EMPTY_PACKAGE,
DLLS_WITHOUT_LIBS,
ONLY_RELEASE_CRT,
EMPTY_INCLUDE_FOLDER,
ALLOW_OBSOLETE_MSVCRT,
// Must be last
COUNT,
};
Optional<BuildPolicy> to_build_policy(const std::string& str);
const std::string& to_string(BuildPolicy policy);
CStringView to_cmake_variable(BuildPolicy policy);
struct BuildPolicies
{
BuildPolicies() {}
BuildPolicies(std::map<BuildPolicy, bool>&& map) : m_policies(std::move(map)) {}
inline bool is_enabled(BuildPolicy policy) const
{
auto it = m_policies.find(policy);
if (it != m_policies.cend()) return it->second;
return false;
}
private:
std::map<BuildPolicy, bool> m_policies;
};
struct BuildInfo
{
static BuildInfo create(std::unordered_map<std::string, std::string> pgh);
PostBuildLint::LinkageType crt_linkage;
PostBuildLint::LinkageType library_linkage;
Optional<std::string> version;
std::map<PostBuildLint::BuildPolicies, bool> policies;
BuildPolicies policies;
};
BuildInfo read_build_info(const Files::Filesystem& fs, const fs::path& filepath);

View File

@ -5,7 +5,7 @@
namespace vcpkg
{
MachineType getMachineType(const uint16_t value)
MachineType to_machine_type(const uint16_t value)
{
MachineType t = static_cast<MachineType>(value);
switch (t)

View File

@ -12,6 +12,7 @@
using vcpkg::Build::PreBuildInfo;
using vcpkg::Build::BuildInfo;
using vcpkg::Build::BuildPolicy;
namespace vcpkg::PostBuildLint
{
@ -37,16 +38,7 @@ namespace vcpkg::PostBuildLint
}
};
template<class T>
static bool contains_and_enabled(const std::map<T, bool> map, const T& key)
{
auto it = map.find(key);
if (it != map.cend()) return it->second;
return false;
}
const std::vector<OutdatedDynamicCrt>& get_outdated_dynamic_crts(const std::map<BuildPolicies, bool>& policies)
const std::vector<OutdatedDynamicCrt>& get_outdated_dynamic_crts(const Build::BuildPolicies& policies)
{
static const std::vector<OutdatedDynamicCrt> v_no_msvcrt = {
{"msvcp100.dll", R"(msvcp100\.dll)"},
@ -73,7 +65,7 @@ namespace vcpkg::PostBuildLint
return ret;
}();
if (contains_and_enabled(policies, BuildPoliciesC::ALLOW_OBSOLETE_MSVCRT))
if (policies.is_enabled(BuildPolicy::ALLOW_OBSOLETE_MSVCRT))
{
return v_no_msvcrt;
}
@ -82,10 +74,10 @@ namespace vcpkg::PostBuildLint
}
static LintStatus check_for_files_in_include_directory(const Files::Filesystem& fs,
const std::map<BuildPolicies, bool>& policies,
const Build::BuildPolicies& policies,
const fs::path& package_dir)
{
if (contains_and_enabled(policies, BuildPoliciesC::EMPTY_INCLUDE_FOLDER))
if (policies.is_enabled(BuildPolicy::EMPTY_INCLUDE_FOLDER))
{
return LintStatus::SUCCESS;
}
@ -511,16 +503,12 @@ namespace vcpkg::PostBuildLint
return LintStatus::ERROR_DETECTED;
}
static LintStatus check_lib_files_are_available_if_dlls_are_available(const std::map<BuildPolicies, bool>& policies,
static LintStatus check_lib_files_are_available_if_dlls_are_available(const Build::BuildPolicies& policies,
const size_t lib_count,
const size_t dll_count,
const fs::path& lib_dir)
{
auto it = policies.find(BuildPoliciesC::DLLS_WITHOUT_LIBS);
if (it != policies.cend() && it->second)
{
return LintStatus::SUCCESS;
}
if (policies.is_enabled(BuildPolicy::DLLS_WITHOUT_LIBS)) return LintStatus::SUCCESS;
if (lib_count == 0 && dll_count != 0)
{
@ -528,7 +516,7 @@ namespace vcpkg::PostBuildLint
System::println(System::Color::warning,
"If this is intended, add the following line in the portfile:\n"
" SET(%s enabled)",
BuildPoliciesC::DLLS_WITHOUT_LIBS.cmake_variable());
to_cmake_variable(BuildPolicy::DLLS_WITHOUT_LIBS));
return LintStatus::ERROR_DETECTED;
}
@ -750,7 +738,7 @@ namespace vcpkg::PostBuildLint
size_t error_count = 0;
if (contains_and_enabled(build_info.policies, BuildPoliciesC::EMPTY_PACKAGE))
if (build_info.policies.is_enabled(BuildPolicy::EMPTY_PACKAGE))
{
return error_count;
}
@ -822,7 +810,7 @@ namespace vcpkg::PostBuildLint
error_count += check_bin_folders_are_not_present_in_static_build(fs, package_dir);
if (!contains_and_enabled(build_info.policies, BuildPoliciesC::ONLY_RELEASE_CRT))
if (!build_info.policies.is_enabled(BuildPolicy::ONLY_RELEASE_CRT))
{
error_count += check_crt_linkage_of_libs(
BuildType::value_of(ConfigurationTypeC::DEBUG, build_info.crt_linkage),

View File

@ -1,80 +0,0 @@
#include "pch.h"
#include "PostBuildLint_BuildPolicies.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Enums.h"
namespace vcpkg::PostBuildLint
{
static const std::string NULLVALUE_STRING = Enums::nullvalue_to_string(BuildPoliciesC::ENUM_NAME);
static const std::string NAME_EMPTY_PACKAGE = "PolicyEmptyPackage";
static const std::string NAME_DLLS_WITHOUT_LIBS = "PolicyDLLsWithoutLIBs";
static const std::string NAME_ONLY_RELEASE_CRT = "PolicyOnlyReleaseCRT";
static const std::string NAME_EMPTY_INCLUDE_FOLDER = "PolicyEmptyIncludeFolder";
static const std::string NAME_ALLOW_OBSOLETE_MSVCRT = "PolicyAllowObsoleteMsvcrt";
BuildPolicies BuildPolicies::parse(const std::string& s)
{
if (s == NAME_EMPTY_PACKAGE)
{
return BuildPoliciesC::EMPTY_PACKAGE;
}
if (s == NAME_DLLS_WITHOUT_LIBS)
{
return BuildPoliciesC::DLLS_WITHOUT_LIBS;
}
if (s == NAME_ONLY_RELEASE_CRT)
{
return BuildPoliciesC::ONLY_RELEASE_CRT;
}
if (s == NAME_EMPTY_INCLUDE_FOLDER)
{
return BuildPoliciesC::EMPTY_INCLUDE_FOLDER;
}
if (s == NAME_ALLOW_OBSOLETE_MSVCRT)
{
return BuildPoliciesC::ALLOW_OBSOLETE_MSVCRT;
}
return BuildPoliciesC::NULLVALUE;
}
const std::string& BuildPolicies::to_string() const
{
switch (this->backing_enum)
{
case BuildPoliciesC::EMPTY_PACKAGE: return NAME_EMPTY_PACKAGE;
case BuildPoliciesC::DLLS_WITHOUT_LIBS: return NAME_DLLS_WITHOUT_LIBS;
case BuildPoliciesC::ONLY_RELEASE_CRT: return NAME_ONLY_RELEASE_CRT;
case BuildPoliciesC::EMPTY_INCLUDE_FOLDER: return NAME_EMPTY_INCLUDE_FOLDER;
case BuildPoliciesC::ALLOW_OBSOLETE_MSVCRT: return NAME_ALLOW_OBSOLETE_MSVCRT;
case BuildPoliciesC::NULLVALUE: return NULLVALUE_STRING;
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
const std::string& BuildPolicies::cmake_variable() const
{
static const std::string CMAKE_VARIABLE_EMPTY_PACKAGE = "VCPKG_POLICY_EMPTY_PACKAGE";
static const std::string CMAKE_VARIABLE_DLLS_WITHOUT_LIBS = "VCPKG_POLICY_DLLS_WITHOUT_LIBS";
static const std::string CMAKE_VARIABLE_ONLY_RELEASE_CRT = "VCPKG_POLICY_ONLY_RELEASE_CRT";
static const std::string CMAKE_VARIABLE_EMPTY_INCLUDE_FOLDER = "VCPKG_POLICY_EMPTY_INCLUDE_FOLDER";
static const std::string CMAKE_VARIABLE_ALLOW_OBSOLETE_MSVCRT = "VCPKG_POLICY_ALLOW_OBSOLETE_MSVCRT";
switch (this->backing_enum)
{
case BuildPoliciesC::EMPTY_PACKAGE: return CMAKE_VARIABLE_EMPTY_PACKAGE;
case BuildPoliciesC::DLLS_WITHOUT_LIBS: return CMAKE_VARIABLE_DLLS_WITHOUT_LIBS;
case BuildPoliciesC::ONLY_RELEASE_CRT: return CMAKE_VARIABLE_ONLY_RELEASE_CRT;
case BuildPoliciesC::EMPTY_INCLUDE_FOLDER: return CMAKE_VARIABLE_EMPTY_INCLUDE_FOLDER;
case BuildPoliciesC::ALLOW_OBSOLETE_MSVCRT: return CMAKE_VARIABLE_ALLOW_OBSOLETE_MSVCRT;
case BuildPoliciesC::NULLVALUE: Enums::nullvalue_used(VCPKG_LINE_INFO, BuildPoliciesC::ENUM_NAME);
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
}

View File

@ -87,7 +87,7 @@ namespace vcpkg::COFFFileReader
std::string machine_field_as_string = data.substr(MACHINE_TYPE_OFFSET, MACHINE_TYPE_SIZE);
const uint16_t machine = reinterpret_bytes<uint16_t>(machine_field_as_string.c_str());
return getMachineType(machine);
return to_machine_type(machine);
}
private:
@ -210,7 +210,7 @@ namespace vcpkg::COFFFileReader
std::string machine_field_as_string = data.substr(MACHINE_TYPE_OFFSET, MACHINE_TYPE_SIZE);
const uint16_t machine = reinterpret_bytes<uint16_t>(machine_field_as_string.c_str());
return getMachineType(machine);
return to_machine_type(machine);
}
private:
@ -297,7 +297,7 @@ namespace vcpkg::COFFFileReader
marker.set_to_offset(offset + ArchiveMemberHeader::HEADER_SIZE); // Skip the header, no need to read it.
marker.seek_to_marker(fs);
const uint16_t first_two_bytes = peek_value_from_stream<uint16_t>(fs);
const bool isImportHeader = getMachineType(first_two_bytes) == MachineType::UNKNOWN;
const bool isImportHeader = to_machine_type(first_two_bytes) == MachineType::UNKNOWN;
const MachineType machine =
isImportHeader ? ImportHeader::read(fs).machineType() : CoffFileHeader::read(fs).machineType();
machine_types.insert(machine);

View File

@ -13,8 +13,6 @@
#include "vcpkglib.h"
#include "vcpkglib_helpers.h"
using vcpkg::PostBuildLint::BuildPolicies;
namespace BuildPoliciesC = vcpkg::PostBuildLint::BuildPoliciesC;
using vcpkg::PostBuildLint::LinkageType;
namespace LinkageTypeC = vcpkg::PostBuildLint::LinkageTypeC;
@ -214,7 +212,7 @@ namespace vcpkg::Build
Commands::Version::version());
}
BuildInfo BuildInfo::create(std::unordered_map<std::string, std::string> pgh)
static BuildInfo inner_create_buildinfo(std::unordered_map<std::string, std::string> pgh)
{
BuildInfo build_info;
const std::string crt_linkage_as_string =
@ -240,20 +238,35 @@ namespace vcpkg::Build
pgh.erase(it_version);
}
std::map<BuildPolicy, bool> policies;
// The remaining entries are policies
for (const std::unordered_map<std::string, std::string>::value_type& p : pgh)
{
const BuildPolicies policy = BuildPolicies::parse(p.first);
Checks::check_exit(
VCPKG_LINE_INFO, policy != BuildPoliciesC::NULLVALUE, "Unknown policy found: %s", p.first);
if (p.second == "enabled")
build_info.policies.emplace(policy, true);
else if (p.second == "disabled")
build_info.policies.emplace(policy, false);
auto maybe_policy = to_build_policy(p.first);
if (auto policy = maybe_policy.get())
{
Checks::check_exit(VCPKG_LINE_INFO,
policies.find(*policy) == policies.end(),
"Policy specified multiple times: %s",
p.first);
if (p.second == "enabled")
policies.emplace(*policy, true);
else if (p.second == "disabled")
policies.emplace(*policy, false);
else
Checks::exit_with_message(
VCPKG_LINE_INFO, "Unknown setting for policy '%s': %s", p.first, p.second);
}
else
Checks::exit_with_message(VCPKG_LINE_INFO, "Unknown setting for policy '%s': %s", p.first, p.second);
{
Checks::exit_with_message(VCPKG_LINE_INFO, "Unknown policy found: %s", p.first);
}
}
build_info.policies = BuildPolicies(std::move(policies));
return build_info;
}
@ -262,7 +275,7 @@ namespace vcpkg::Build
const Expected<std::unordered_map<std::string, std::string>> pghs =
Paragraphs::get_single_paragraph(fs, filepath);
Checks::check_exit(VCPKG_LINE_INFO, pghs.get() != nullptr, "Invalid BUILD_INFO file for package");
return BuildInfo::create(*pghs.get());
return inner_create_buildinfo(*pghs.get());
}
PreBuildInfo PreBuildInfo::from_triplet_file(const VcpkgPaths& paths, const Triplet& triplet)

View File

@ -0,0 +1,51 @@
#include "pch.h"
#include "vcpkg_Build.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Enums.h"
namespace vcpkg::Build
{
static const std::string NAME_EMPTY_PACKAGE = "PolicyEmptyPackage";
static const std::string NAME_DLLS_WITHOUT_LIBS = "PolicyDLLsWithoutLIBs";
static const std::string NAME_ONLY_RELEASE_CRT = "PolicyOnlyReleaseCRT";
static const std::string NAME_EMPTY_INCLUDE_FOLDER = "PolicyEmptyIncludeFolder";
static const std::string NAME_ALLOW_OBSOLETE_MSVCRT = "PolicyAllowObsoleteMsvcrt";
Optional<BuildPolicy> to_build_policy(const std::string& s)
{
if (s == NAME_EMPTY_PACKAGE) return BuildPolicy::EMPTY_PACKAGE;
if (s == NAME_DLLS_WITHOUT_LIBS) return BuildPolicy::DLLS_WITHOUT_LIBS;
if (s == NAME_ONLY_RELEASE_CRT) return BuildPolicy::ONLY_RELEASE_CRT;
if (s == NAME_EMPTY_INCLUDE_FOLDER) return BuildPolicy::EMPTY_INCLUDE_FOLDER;
if (s == NAME_ALLOW_OBSOLETE_MSVCRT) return BuildPolicy::ALLOW_OBSOLETE_MSVCRT;
return nullopt;
}
const std::string& to_string(BuildPolicy policy)
{
switch (policy)
{
case BuildPolicy::EMPTY_PACKAGE: return NAME_EMPTY_PACKAGE;
case BuildPolicy::DLLS_WITHOUT_LIBS: return NAME_DLLS_WITHOUT_LIBS;
case BuildPolicy::ONLY_RELEASE_CRT: return NAME_ONLY_RELEASE_CRT;
case BuildPolicy::EMPTY_INCLUDE_FOLDER: return NAME_EMPTY_INCLUDE_FOLDER;
case BuildPolicy::ALLOW_OBSOLETE_MSVCRT: return NAME_ALLOW_OBSOLETE_MSVCRT;
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
CStringView to_cmake_variable(BuildPolicy policy)
{
switch (policy)
{
case BuildPolicy::EMPTY_PACKAGE: return "VCPKG_POLICY_EMPTY_PACKAGE";
case BuildPolicy::DLLS_WITHOUT_LIBS: return "VCPKG_POLICY_DLLS_WITHOUT_LIBS";
case BuildPolicy::ONLY_RELEASE_CRT: return "VCPKG_POLICY_ONLY_RELEASE_CRT";
case BuildPolicy::EMPTY_INCLUDE_FOLDER: return "VCPKG_POLICY_EMPTY_INCLUDE_FOLDER";
case BuildPolicy::ALLOW_OBSOLETE_MSVCRT: return "VCPKG_POLICY_ALLOW_OBSOLETE_MSVCRT";
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
}

View File

@ -141,8 +141,8 @@
<ClInclude Include="..\include\lazy.h" />
<ClInclude Include="..\include\LineInfo.h" />
<ClInclude Include="..\include\ParagraphParseResult.h" />
<ClInclude Include="..\include\PostBuildLint_BuildType.h" />
<ClInclude Include="..\include\vcpkg_Build.h" />
<ClInclude Include="..\include\PostBuildLint_BuildPolicies.h" />
<ClInclude Include="..\include\coff_file_reader.h" />
<ClInclude Include="..\include\vcpkg_expected.h" />
<ClInclude Include="..\include\filesystem_fs.h" />
@ -154,7 +154,6 @@
<ClInclude Include="..\include\Paragraphs.h" />
<ClInclude Include="..\include\pch.h" />
<ClInclude Include="..\include\PostBuildLint.h" />
<ClInclude Include="..\include\PostBuildLint_BuildType.h" />
<ClInclude Include="..\include\PostBuildLint_ConfigurationType.h" />
<ClInclude Include="..\include\PostBuildLint_LinkageType.h" />
<ClInclude Include="..\include\SourceParagraph.h" />
@ -189,7 +188,7 @@
<ClCompile Include="..\src\LineInfo.cpp" />
<ClCompile Include="..\src\ParagraphParseResult.cpp" />
<ClCompile Include="..\src\vcpkg_Build.cpp" />
<ClCompile Include="..\src\PostBuildLint_BuildPolicies.cpp" />
<ClCompile Include="..\src\vcpkg_Build_BuildPolicy.cpp" />
<ClCompile Include="..\src\coff_file_reader.cpp" />
<ClCompile Include="..\src\commands_available_commands.cpp" />
<ClCompile Include="..\src\commands_build.cpp" />

View File

@ -129,9 +129,6 @@
<ClCompile Include="..\src\PostBuildLint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\PostBuildLint_BuildPolicies.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\PostBuildLint_LinkageType.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@ -183,6 +180,9 @@
<ClCompile Include="..\src\vcpkg_Build.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\vcpkg_Build_BuildPolicy.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\SourceParagraph.h">
@ -257,18 +257,12 @@
<ClInclude Include="..\include\PostBuildLint.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\PostBuildLint_BuildPolicies.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\PostBuildLint_LinkageType.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\PostBuildLint_ConfigurationType.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\PostBuildLint_BuildType.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\vcpkg_Enums.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -314,5 +308,8 @@
<ClInclude Include="..\include\vcpkg_Build.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\PostBuildLint_BuildType.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>