aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src
diff options
context:
space:
mode:
authorRobert Schumacher <roschuma@microsoft.com>2017-05-24 00:44:00 -0700
committerRobert Schumacher <roschuma@microsoft.com>2017-05-24 00:44:00 -0700
commit6be01a12db23788c32ca8cc8e70b8467ae912d1d (patch)
tree20733eb131d185a917aa40a6b6830549dfa90a01 /toolsrc/src
parente9b561fa4809a027089da0a726ebcd80a0043294 (diff)
downloadvcpkg-6be01a12db23788c32ca8cc8e70b8467ae912d1d.tar.gz
vcpkg-6be01a12db23788c32ca8cc8e70b8467ae912d1d.zip
[vcpkg] Refactored to simplify BuildPolicies into BuildPolicy.
Restrict policy consumers to a simpler interface than std::map. Rename vcpkg::getMachineType -> vcpkg::to_machine_type.
Diffstat (limited to 'toolsrc/src')
-rw-r--r--toolsrc/src/MachineType.cpp2
-rw-r--r--toolsrc/src/PostBuildLint.cpp32
-rw-r--r--toolsrc/src/PostBuildLint_BuildPolicies.cpp80
-rw-r--r--toolsrc/src/coff_file_reader.cpp6
-rw-r--r--toolsrc/src/vcpkg_Build.cpp37
-rw-r--r--toolsrc/src/vcpkg_Build_BuildPolicy.cpp51
6 files changed, 90 insertions, 118 deletions
diff --git a/toolsrc/src/MachineType.cpp b/toolsrc/src/MachineType.cpp
index f288855e6..2f44ce21a 100644
--- a/toolsrc/src/MachineType.cpp
+++ b/toolsrc/src/MachineType.cpp
@@ -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)
diff --git a/toolsrc/src/PostBuildLint.cpp b/toolsrc/src/PostBuildLint.cpp
index 4491f3f2e..bcad27032 100644
--- a/toolsrc/src/PostBuildLint.cpp
+++ b/toolsrc/src/PostBuildLint.cpp
@@ -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),
diff --git a/toolsrc/src/PostBuildLint_BuildPolicies.cpp b/toolsrc/src/PostBuildLint_BuildPolicies.cpp
deleted file mode 100644
index d2189e20d..000000000
--- a/toolsrc/src/PostBuildLint_BuildPolicies.cpp
+++ /dev/null
@@ -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);
- }
- }
-}
diff --git a/toolsrc/src/coff_file_reader.cpp b/toolsrc/src/coff_file_reader.cpp
index 467fc9a64..b06c6d57b 100644
--- a/toolsrc/src/coff_file_reader.cpp
+++ b/toolsrc/src/coff_file_reader.cpp
@@ -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);
diff --git a/toolsrc/src/vcpkg_Build.cpp b/toolsrc/src/vcpkg_Build.cpp
index 6605fa4fb..d44a673fc 100644
--- a/toolsrc/src/vcpkg_Build.cpp
+++ b/toolsrc/src/vcpkg_Build.cpp
@@ -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)
diff --git a/toolsrc/src/vcpkg_Build_BuildPolicy.cpp b/toolsrc/src/vcpkg_Build_BuildPolicy.cpp
new file mode 100644
index 000000000..e75e176a2
--- /dev/null
+++ b/toolsrc/src/vcpkg_Build_BuildPolicy.cpp
@@ -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);
+ }
+ }
+}