mirror of https://github.com/microsoft/vcpkg.git
Rework OptBool enum
This commit is contained in:
parent
0a51047157
commit
1aef09ba58
|
|
@ -3,31 +3,46 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace vcpkg::OptBool
|
||||
namespace vcpkg
|
||||
{
|
||||
enum class Type
|
||||
struct OptBool final
|
||||
{
|
||||
UNSPECIFIED = 0,
|
||||
ENABLED,
|
||||
DISABLED
|
||||
enum class BackingEnum
|
||||
{
|
||||
UNSPECIFIED = 0,
|
||||
ENABLED,
|
||||
DISABLED
|
||||
};
|
||||
|
||||
static OptBool parse(const std::string& s);
|
||||
|
||||
template<class T>
|
||||
static OptBool from_map(const std::map<T, std::string>& map, const T& key);
|
||||
|
||||
constexpr OptBool() : backing_enum(BackingEnum::UNSPECIFIED) {}
|
||||
constexpr explicit OptBool(BackingEnum backing_enum) : backing_enum(backing_enum) { }
|
||||
constexpr operator BackingEnum() const { return backing_enum; }
|
||||
|
||||
private:
|
||||
BackingEnum backing_enum;
|
||||
};
|
||||
|
||||
Type parse(const std::string& s);
|
||||
namespace OptBoolC
|
||||
{
|
||||
constexpr OptBool UNSPECIFIED(OptBool::BackingEnum::UNSPECIFIED);
|
||||
constexpr OptBool ENABLED(OptBool::BackingEnum::ENABLED);
|
||||
constexpr OptBool DISABLED(OptBool::BackingEnum::DISABLED);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Type from_map(const std::map<T, std::string>& map, const T& key)
|
||||
template<class T>
|
||||
OptBool OptBool::from_map(const std::map<T, std::string>& map, const T& key)
|
||||
{
|
||||
auto it = map.find(key);
|
||||
if (it == map.cend())
|
||||
{
|
||||
return Type::UNSPECIFIED;
|
||||
return OptBoolC::UNSPECIFIED;
|
||||
}
|
||||
|
||||
return parse(*it);
|
||||
}
|
||||
}
|
||||
|
||||
namespace vcpkg
|
||||
{
|
||||
using OptBoolT = OptBool::Type;
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ namespace vcpkg::PostBuildLint
|
|||
LinkageType::Type crt_linkage;
|
||||
LinkageType::Type library_linkage;
|
||||
|
||||
std::map<BuildPolicies, OptBoolT> policies;
|
||||
std::map<BuildPolicies, OptBool> policies;
|
||||
};
|
||||
|
||||
BuildInfo read_build_info(const Files::Filesystem& fs, const fs::path& filepath);
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ namespace vcpkg
|
|||
|
||||
std::unique_ptr<std::string> vcpkg_root_dir;
|
||||
std::unique_ptr<std::string> triplet;
|
||||
OptBoolT debug = OptBoolT::UNSPECIFIED;
|
||||
OptBoolT sendmetrics = OptBoolT::UNSPECIFIED;
|
||||
OptBoolT printmetrics = OptBoolT::UNSPECIFIED;
|
||||
OptBool debug = OptBoolC::UNSPECIFIED;
|
||||
OptBool sendmetrics = OptBoolC::UNSPECIFIED;
|
||||
OptBool printmetrics = OptBoolC::UNSPECIFIED;
|
||||
|
||||
std::string command;
|
||||
std::vector<std::string> command_arguments;
|
||||
|
|
|
|||
|
|
@ -2,26 +2,27 @@
|
|||
#include "OptBool.h"
|
||||
#include "vcpkg_Checks.h"
|
||||
|
||||
namespace vcpkg::OptBool
|
||||
namespace vcpkg
|
||||
{
|
||||
static const std::string UNSPECIFIED_NAME = "unspecified";
|
||||
static const std::string ENABLED_NAME = "enabled";
|
||||
static const std::string DISABLED_NAME = "disabled";
|
||||
Type parse(const std::string& s)
|
||||
|
||||
OptBool OptBool::parse(const std::string& s)
|
||||
{
|
||||
if (s == UNSPECIFIED_NAME)
|
||||
{
|
||||
return OptBoolT::UNSPECIFIED;
|
||||
return OptBoolC::UNSPECIFIED;
|
||||
}
|
||||
|
||||
if (s == ENABLED_NAME)
|
||||
{
|
||||
return OptBoolT::ENABLED;
|
||||
return OptBoolC::ENABLED;
|
||||
}
|
||||
|
||||
if (s == DISABLED_NAME)
|
||||
{
|
||||
return OptBoolT::DISABLED;
|
||||
return OptBoolC::DISABLED;
|
||||
}
|
||||
|
||||
Checks::exit_with_message(VCPKG_LINE_INFO, "Could not convert string [%s] to OptBool", s);
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ namespace vcpkg::PostBuildLint
|
|||
static auto has_extension_pred(const Files::Filesystem& fs, const std::string& ext)
|
||||
{
|
||||
return [&fs, ext](const fs::path& path)
|
||||
{
|
||||
return !fs.is_directory(path) && path.extension() == ext;
|
||||
};
|
||||
{
|
||||
return !fs.is_directory(path) && path.extension() == ext;
|
||||
};
|
||||
}
|
||||
|
||||
enum class LintStatus
|
||||
|
|
@ -31,8 +31,8 @@ namespace vcpkg::PostBuildLint
|
|||
std::regex regex;
|
||||
|
||||
OutdatedDynamicCrt(const std::string& name, const std::string& regex_as_string)
|
||||
: name(name),
|
||||
regex(std::regex(regex_as_string, std::regex_constants::icase)) {}
|
||||
: name(name)
|
||||
, regex(std::regex(regex_as_string, std::regex_constants::icase)) {}
|
||||
};
|
||||
|
||||
const std::vector<OutdatedDynamicCrt>& get_outdated_dynamic_crts()
|
||||
|
|
@ -61,11 +61,11 @@ namespace vcpkg::PostBuildLint
|
|||
return v;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static bool contains_and_enabled(const std::map<T, OptBoolT> map, const T& key)
|
||||
template<class T>
|
||||
static bool contains_and_enabled(const std::map<T, OptBool> map, const T& key)
|
||||
{
|
||||
auto it = map.find(key);
|
||||
if (it != map.cend() && it->second == OptBoolT::ENABLED)
|
||||
if (it != map.cend() && it->second == OptBoolC::ENABLED)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
@ -73,7 +73,7 @@ namespace vcpkg::PostBuildLint
|
|||
return false;
|
||||
}
|
||||
|
||||
static LintStatus check_for_files_in_include_directory(const Files::Filesystem& fs, const std::map<BuildPolicies, OptBoolT>& policies, const fs::path& package_dir)
|
||||
static LintStatus check_for_files_in_include_directory(const Files::Filesystem& fs, const std::map<BuildPolicies, OptBool>& policies, const fs::path& package_dir)
|
||||
{
|
||||
if (contains_and_enabled(policies, BuildPoliciesC::EMPTY_INCLUDE_FOLDER))
|
||||
{
|
||||
|
|
@ -97,9 +97,9 @@ namespace vcpkg::PostBuildLint
|
|||
std::vector<fs::path> files_found = fs.get_files_recursive(debug_include_dir);
|
||||
|
||||
Util::unstable_keep_if(files_found, [&fs](const fs::path& path)
|
||||
{
|
||||
return !fs.is_directory(path) && path.extension() != ".ifc";
|
||||
});
|
||||
{
|
||||
return !fs.is_directory(path) && path.extension() != ".ifc";
|
||||
});
|
||||
|
||||
if (!files_found.empty())
|
||||
{
|
||||
|
|
@ -443,10 +443,10 @@ namespace vcpkg::PostBuildLint
|
|||
return LintStatus::ERROR_DETECTED;
|
||||
}
|
||||
|
||||
static LintStatus check_lib_files_are_available_if_dlls_are_available(const std::map<BuildPolicies, OptBoolT>& policies, const size_t lib_count, const size_t dll_count, const fs::path& lib_dir)
|
||||
static LintStatus check_lib_files_are_available_if_dlls_are_available(const std::map<BuildPolicies, OptBool>& 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 == OptBoolT::ENABLED)
|
||||
if (it != policies.cend() && it->second == OptBoolC::ENABLED)
|
||||
{
|
||||
return LintStatus::SUCCESS;
|
||||
}
|
||||
|
|
@ -499,9 +499,9 @@ namespace vcpkg::PostBuildLint
|
|||
std::vector<fs::path> empty_directories = fs.get_files_recursive(dir);
|
||||
|
||||
Util::unstable_keep_if(empty_directories, [&fs](const fs::path& current)
|
||||
{
|
||||
return fs.is_directory(current) && fs.is_empty(current);
|
||||
});
|
||||
{
|
||||
return fs.is_directory(current) && fs.is_empty(current);
|
||||
});
|
||||
|
||||
if (!empty_directories.empty())
|
||||
{
|
||||
|
|
@ -616,12 +616,12 @@ namespace vcpkg::PostBuildLint
|
|||
{
|
||||
std::vector<fs::path> misplaced_files = fs.get_files_non_recursive(dir);
|
||||
Util::unstable_keep_if(misplaced_files, [&fs](const fs::path& path)
|
||||
{
|
||||
const std::string filename = path.filename().generic_string();
|
||||
if (_stricmp(filename.c_str(), "CONTROL") == 0 || _stricmp(filename.c_str(), "BUILD_INFO") == 0)
|
||||
return false;
|
||||
return !fs.is_directory(path);
|
||||
});
|
||||
{
|
||||
const std::string filename = path.filename().generic_string();
|
||||
if (_stricmp(filename.c_str(), "CONTROL") == 0 || _stricmp(filename.c_str(), "BUILD_INFO") == 0)
|
||||
return false;
|
||||
return !fs.is_directory(path);
|
||||
});
|
||||
|
||||
if (!misplaced_files.empty())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ namespace vcpkg::PostBuildLint
|
|||
{
|
||||
const BuildPolicies policy = BuildPolicies::parse(p.first);
|
||||
Checks::check_exit(VCPKG_LINE_INFO, policy != BuildPoliciesC::NULLVALUE, "Unknown policy found: %s", p.first);
|
||||
const OptBoolT status = OptBool::parse(p.second);
|
||||
const OptBool status = OptBool::parse(p.second);
|
||||
build_info.policies.emplace(policy, status);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,11 +32,11 @@ namespace vcpkg
|
|||
}
|
||||
|
||||
static void parse_switch(
|
||||
OptBoolT new_setting,
|
||||
OptBool new_setting,
|
||||
const std::string& option_name,
|
||||
OptBoolT& option_field)
|
||||
OptBool& option_field)
|
||||
{
|
||||
if (option_field != OptBoolT::UNSPECIFIED && option_field != new_setting)
|
||||
if (option_field != OptBoolC::UNSPECIFIED && option_field != new_setting)
|
||||
{
|
||||
System::println(System::Color::error, "Error: conflicting values specified for --%s", option_name);
|
||||
Metrics::track_property("error", "error conflicting switches");
|
||||
|
|
@ -93,27 +93,27 @@ namespace vcpkg
|
|||
}
|
||||
if (arg == "--debug")
|
||||
{
|
||||
parse_switch(OptBoolT::ENABLED, "debug", args.debug);
|
||||
parse_switch(OptBoolC::ENABLED, "debug", args.debug);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--sendmetrics")
|
||||
{
|
||||
parse_switch(OptBoolT::ENABLED, "sendmetrics", args.sendmetrics);
|
||||
parse_switch(OptBoolC::ENABLED, "sendmetrics", args.sendmetrics);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--printmetrics")
|
||||
{
|
||||
parse_switch(OptBoolT::ENABLED, "printmetrics", args.printmetrics);
|
||||
parse_switch(OptBoolC::ENABLED, "printmetrics", args.printmetrics);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--no-sendmetrics")
|
||||
{
|
||||
parse_switch(OptBoolT::DISABLED, "sendmetrics", args.sendmetrics);
|
||||
parse_switch(OptBoolC::DISABLED, "sendmetrics", args.sendmetrics);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--no-printmetrics")
|
||||
{
|
||||
parse_switch(OptBoolT::DISABLED, "printmetrics", args.printmetrics);
|
||||
parse_switch(OptBoolC::DISABLED, "printmetrics", args.printmetrics);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -199,14 +199,14 @@ int wmain(const int argc, const wchar_t* const* const argv)
|
|||
|
||||
const VcpkgCmdArguments args = VcpkgCmdArguments::create_from_command_line(argc, argv);
|
||||
|
||||
if (args.printmetrics != OptBoolT::UNSPECIFIED)
|
||||
Metrics::set_print_metrics(args.printmetrics == OptBoolT::ENABLED);
|
||||
if (args.sendmetrics != OptBoolT::UNSPECIFIED)
|
||||
Metrics::set_send_metrics(args.sendmetrics == OptBoolT::ENABLED);
|
||||
if (args.printmetrics != OptBoolC::UNSPECIFIED)
|
||||
Metrics::set_print_metrics(args.printmetrics == OptBoolC::ENABLED);
|
||||
if (args.sendmetrics != OptBoolC::UNSPECIFIED)
|
||||
Metrics::set_send_metrics(args.sendmetrics == OptBoolC::ENABLED);
|
||||
|
||||
if (args.debug != OptBoolT::UNSPECIFIED)
|
||||
if (args.debug != OptBoolC::UNSPECIFIED)
|
||||
{
|
||||
g_debugging = (args.debug == OptBoolT::ENABLED);
|
||||
g_debugging = (args.debug == OptBoolC::ENABLED);
|
||||
}
|
||||
|
||||
if (g_debugging)
|
||||
|
|
|
|||
Loading…
Reference in New Issue