Skip to content

Commit

Permalink
[vcpkg] Refactored to simplify BuildPolicies into BuildPolicy.
Browse files Browse the repository at this point in the history
Restrict policy consumers to a simpler interface than std::map.
Rename vcpkg::getMachineType -> vcpkg::to_machine_type.
  • Loading branch information
ras0219-msft committed May 24, 2017
1 parent e9b561f commit 6be01a1
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 182 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.patch text eol=lf
*.vcxproj text eol=crlf
*.vcxproj.filters text eol=crlf
*.sln text eol=crlf
2 changes: 1 addition & 1 deletion toolsrc/include/MachineType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
47 changes: 0 additions & 47 deletions toolsrc/include/PostBuildLint_BuildPolicies.h

This file was deleted.

1 change: 1 addition & 0 deletions toolsrc/include/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
39 changes: 35 additions & 4 deletions toolsrc/include/vcpkg_Build.h
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -69,16 +70,46 @@ namespace vcpkg::Build
const BuildPackageConfig& config,
const StatusParagraphs& status_db);

struct BuildInfo
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
{
static BuildInfo create(std::unordered_map<std::string, std::string> pgh);
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
{
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);
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/src/MachineType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 10 additions & 22 deletions toolsrc/src/PostBuildLint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using vcpkg::Build::PreBuildInfo;
using vcpkg::Build::BuildInfo;
using vcpkg::Build::BuildPolicy;

namespace vcpkg::PostBuildLint
{
Expand All @@ -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)"},
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -511,24 +503,20 @@ 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)
{
System::println(System::Color::warning, "Import libs were not present in %s", lib_dir.u8string());
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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
Expand Down
80 changes: 0 additions & 80 deletions toolsrc/src/PostBuildLint_BuildPolicies.cpp

This file was deleted.

6 changes: 3 additions & 3 deletions toolsrc/src/coff_file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 6be01a1

Please sign in to comment.