diff options
| author | Billy O'Neal <bion@microsoft.com> | 2021-01-20 12:07:41 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-20 12:07:41 -0800 |
| commit | 4d136ef25f4fab5b744c7ae6acfa04d44f254f2b (patch) | |
| tree | c6d0b0920106f03390e8d89c11bdf631cf1f28c7 /toolsrc/include | |
| parent | 45fc55825db2a5bcaffccff1e6afadc519d164ea (diff) | |
| download | vcpkg-4d136ef25f4fab5b744c7ae6acfa04d44f254f2b.tar.gz vcpkg-4d136ef25f4fab5b744c7ae6acfa04d44f254f2b.zip | |
[vcpkg] Add vcpkg_minimum_required as a replacement for VERSION.txt. (#15638)
Diffstat (limited to 'toolsrc/include')
| -rw-r--r-- | toolsrc/include/pch.h | 1 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/basic_checks.h | 41 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/checks.h | 51 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/cstringview.h | 14 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/enums.h | 3 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/expected.h | 5 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/fwd/lineinfo.h | 6 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/json.h | 2 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/lineinfo.h | 16 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/optional.h | 12 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/stringview.h | 16 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/system.debug.h | 6 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/commands.version.h | 5 |
13 files changed, 94 insertions, 84 deletions
diff --git a/toolsrc/include/pch.h b/toolsrc/include/pch.h index c8ca1ea7c..b22c0a140 100644 --- a/toolsrc/include/pch.h +++ b/toolsrc/include/pch.h @@ -29,6 +29,7 @@ #include <iomanip> #include <iostream> #include <iterator> +#include <limits> #include <map> #include <memory> #include <mutex> diff --git a/toolsrc/include/vcpkg/base/basic_checks.h b/toolsrc/include/vcpkg/base/basic_checks.h new file mode 100644 index 000000000..2ea714599 --- /dev/null +++ b/toolsrc/include/vcpkg/base/basic_checks.h @@ -0,0 +1,41 @@ +#pragma once + +#include <vcpkg/base/lineinfo.h> +#include <vcpkg/base/stringview.h> + +namespace vcpkg::Checks +{ + void register_global_shutdown_handler(void (*func)()); + + // Note: for internal use + [[noreturn]] void final_cleanup_and_exit(const int exit_code); + + // Indicate that an internal error has occurred and exit the tool. This should be used when invariants have been + // broken. + [[noreturn]] void unreachable(const LineInfo& line_info); + + [[noreturn]] void exit_with_code(const LineInfo& line_info, const int exit_code); + + // Exit the tool without an error message. + [[noreturn]] void exit_fail(const LineInfo& line_info); + + // Exit the tool successfully. + [[noreturn]] void exit_success(const LineInfo& line_info); + + // Display an error message to the user and exit the tool. + [[noreturn]] void exit_with_message(const LineInfo& line_info, StringView error_message); + + // If expression is false, call exit_fail. + void check_exit(const LineInfo& line_info, bool expression); + + // if expression is false, call exit_with_message. + void check_exit(const LineInfo& line_info, bool expression, StringView error_message); + + // Display a message indicating that vcpkg should be upgraded and exit. + [[noreturn]] void exit_maybe_upgrade(const LineInfo& line_info); + [[noreturn]] void exit_maybe_upgrade(const LineInfo& line_info, StringView error_message); + + // Check the indicated condition and call exit_maybe_upgrade if it is false. + void check_maybe_upgrade(const LineInfo& line_info, bool condition); + void check_maybe_upgrade(const LineInfo& line_info, bool condition, StringView error_message); +} diff --git a/toolsrc/include/vcpkg/base/checks.h b/toolsrc/include/vcpkg/base/checks.h index da836a347..f360fcb36 100644 --- a/toolsrc/include/vcpkg/base/checks.h +++ b/toolsrc/include/vcpkg/base/checks.h @@ -1,29 +1,11 @@ #pragma once -#include <vcpkg/base/cstringview.h> +#include <vcpkg/base/basic_checks.h> #include <vcpkg/base/strings.h> namespace vcpkg::Checks { - void register_global_shutdown_handler(void (*func)()); - - // Note: for internal use - [[noreturn]] void final_cleanup_and_exit(const int exit_code); - - // Indicate that an internal error has occurred and exit the tool. This should be used when invariants have been - // broken. - [[noreturn]] void unreachable(const LineInfo& line_info); - - [[noreturn]] void exit_with_code(const LineInfo& line_info, const int exit_code); - - // Exit the tool without an error message. - [[noreturn]] inline void exit_fail(const LineInfo& line_info) { exit_with_code(line_info, EXIT_FAILURE); } - - // Exit the tool successfully. - [[noreturn]] inline void exit_success(const LineInfo& line_info) { exit_with_code(line_info, EXIT_SUCCESS); } - - // Display an error message to the user and exit the tool. - [[noreturn]] void exit_with_message(const LineInfo& line_info, StringView error_message); + // Additional convenience overloads on top of basic_checks.h that do formatting. template<class Arg1, class... Args> // Display an error message to the user and exit the tool. @@ -36,10 +18,6 @@ namespace vcpkg::Checks Strings::format(error_message_template, error_message_arg1, error_message_args...)); } - void check_exit(const LineInfo& line_info, bool expression); - - void check_exit(const LineInfo& line_info, bool expression, StringView error_message); - template<class Conditional, class Arg1, class... Args> void check_exit(const LineInfo& line_info, Conditional&& expression, @@ -54,4 +32,29 @@ namespace vcpkg::Checks Strings::format(error_message_template, error_message_arg1, error_message_args...)); } } + + template<class Arg1, class... Args> + [[noreturn]] void exit_maybe_upgrade(const LineInfo& line_info, + const char* error_message_template, + const Arg1& error_message_arg1, + const Args&... error_message_args) + { + exit_maybe_upgrade(line_info, + Strings::format(error_message_template, error_message_arg1, error_message_args...)); + } + + template<class Conditional, class Arg1, class... Args> + void check_maybe_upgrade(const LineInfo& line_info, + Conditional&& expression, + const char* error_message_template, + const Arg1& error_message_arg1, + const Args&... error_message_args) + { + if (!expression) + { + // Only create the string if the expression is false + exit_maybe_upgrade(line_info, + Strings::format(error_message_template, error_message_arg1, error_message_args...)); + } + } } diff --git a/toolsrc/include/vcpkg/base/cstringview.h b/toolsrc/include/vcpkg/base/cstringview.h index 7d46ecf2e..94c806ae4 100644 --- a/toolsrc/include/vcpkg/base/cstringview.h +++ b/toolsrc/include/vcpkg/base/cstringview.h @@ -22,17 +22,17 @@ namespace vcpkg namespace details { - inline bool vcpkg_strcmp(const char* l, const char* r) { return strcmp(l, r) == 0; } + inline bool strequal(const char* l, const char* r) { return strcmp(l, r) == 0; } } inline bool operator==(const CStringView& l, const CStringView& r) { - return details::vcpkg_strcmp(l.c_str(), r.c_str()); + return details::strequal(l.c_str(), r.c_str()); } - inline bool operator==(const char* l, const CStringView& r) { return details::vcpkg_strcmp(l, r.c_str()); } + inline bool operator==(const char* l, const CStringView& r) { return details::strequal(l, r.c_str()); } - inline bool operator==(const CStringView& r, const char* l) { return details::vcpkg_strcmp(l, r.c_str()); } + inline bool operator==(const CStringView& r, const char* l) { return details::strequal(l, r.c_str()); } inline bool operator==(const std::string& l, const CStringView& r) { return l == r.c_str(); } @@ -41,12 +41,12 @@ namespace vcpkg // notequals inline bool operator!=(const CStringView& l, const CStringView& r) { - return !details::vcpkg_strcmp(l.c_str(), r.c_str()); + return !details::strequal(l.c_str(), r.c_str()); } - inline bool operator!=(const char* l, const CStringView& r) { return !details::vcpkg_strcmp(l, r.c_str()); } + inline bool operator!=(const char* l, const CStringView& r) { return !details::strequal(l, r.c_str()); } - inline bool operator!=(const CStringView& r, const char* l) { return !details::vcpkg_strcmp(l, r.c_str()); } + inline bool operator!=(const CStringView& r, const char* l) { return !details::strequal(l, r.c_str()); } inline bool operator!=(const CStringView& r, const std::string& l) { return l != r.c_str(); } diff --git a/toolsrc/include/vcpkg/base/enums.h b/toolsrc/include/vcpkg/base/enums.h index 4b77b3f6f..1edd6dd8f 100644 --- a/toolsrc/include/vcpkg/base/enums.h +++ b/toolsrc/include/vcpkg/base/enums.h @@ -1,8 +1,7 @@ #pragma once -#include <vcpkg/base/fwd/lineinfo.h> - #include <vcpkg/base/cstringview.h> +#include <vcpkg/base/lineinfo.h> namespace vcpkg::Enums { diff --git a/toolsrc/include/vcpkg/base/expected.h b/toolsrc/include/vcpkg/base/expected.h index 20c23f077..13e70057d 100644 --- a/toolsrc/include/vcpkg/base/expected.h +++ b/toolsrc/include/vcpkg/base/expected.h @@ -3,6 +3,7 @@ #include <vcpkg/base/checks.h> #include <vcpkg/base/lineinfo.h> #include <vcpkg/base/stringliteral.h> +#include <vcpkg/base/system.print.h> #include <system_error> #include <type_traits> @@ -225,10 +226,10 @@ namespace vcpkg private: void exit_if_error(const LineInfo& line_info) const { - // This is used for quick value_or_exit() calls, so always put line_info in the error message. if (m_s.has_error()) { - Checks::exit_with_message(line_info, "Failed at [%s] with message:\n%s", line_info, m_s.to_string()); + System::print2(System::Color::error, m_s.to_string(), "\n"); + Checks::unreachable(line_info); } } diff --git a/toolsrc/include/vcpkg/base/fwd/lineinfo.h b/toolsrc/include/vcpkg/base/fwd/lineinfo.h deleted file mode 100644 index 0d65da5cc..000000000 --- a/toolsrc/include/vcpkg/base/fwd/lineinfo.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -namespace vcpkg -{ - struct LineInfo; -} diff --git a/toolsrc/include/vcpkg/base/json.h b/toolsrc/include/vcpkg/base/json.h index 31696b757..5321308a3 100644 --- a/toolsrc/include/vcpkg/base/json.h +++ b/toolsrc/include/vcpkg/base/json.h @@ -56,7 +56,7 @@ namespace vcpkg::Json { case Newline::Lf: return "\n"; case Newline::CrLf: return "\r\n"; - default: Checks::exit_fail(VCPKG_LINE_INFO); + default: Checks::unreachable(VCPKG_LINE_INFO); } } diff --git a/toolsrc/include/vcpkg/base/lineinfo.h b/toolsrc/include/vcpkg/base/lineinfo.h index b90b6fac3..68fca2e0e 100644 --- a/toolsrc/include/vcpkg/base/lineinfo.h +++ b/toolsrc/include/vcpkg/base/lineinfo.h @@ -1,21 +1,13 @@ #pragma once -#include <string> - namespace vcpkg { struct LineInfo { - constexpr LineInfo() noexcept : m_line_number(0), m_file_name("") { } - constexpr LineInfo(const int lineno, const char* filename) : m_line_number(lineno), m_file_name(filename) { } - - std::string to_string() const; - void to_string(std::string& out) const; - - private: - int m_line_number; - const char* m_file_name; + int line_number; + const char* file_name; }; } -#define VCPKG_LINE_INFO vcpkg::LineInfo(__LINE__, __FILE__) +#define VCPKG_LINE_INFO \ + vcpkg::LineInfo { __LINE__, __FILE__ } diff --git a/toolsrc/include/vcpkg/base/optional.h b/toolsrc/include/vcpkg/base/optional.h index c46c2e6f8..c66f891c7 100644 --- a/toolsrc/include/vcpkg/base/optional.h +++ b/toolsrc/include/vcpkg/base/optional.h @@ -1,8 +1,9 @@ #pragma once -#include <vcpkg/base/fwd/lineinfo.h> #include <vcpkg/base/fwd/optional.h> +#include <vcpkg/base/basic_checks.h> +#include <vcpkg/base/lineinfo.h> #include <vcpkg/base/pragmas.h> #include <type_traits> @@ -217,9 +218,6 @@ namespace vcpkg private: const T* m_t; }; - - // Note: implemented in checks.cpp to cut the header dependency - void exit_if_null(bool b, const LineInfo& line_info); } template<class T> @@ -237,19 +235,19 @@ namespace vcpkg T&& value_or_exit(const LineInfo& line_info) && { - details::exit_if_null(this->m_base.has_value(), line_info); + Checks::check_exit(line_info, this->m_base.has_value(), "Value was null"); return std::move(this->m_base.value()); } T& value_or_exit(const LineInfo& line_info) & { - details::exit_if_null(this->m_base.has_value(), line_info); + Checks::check_exit(line_info, this->m_base.has_value(), "Value was null"); return this->m_base.value(); } const T& value_or_exit(const LineInfo& line_info) const& { - details::exit_if_null(this->m_base.has_value(), line_info); + Checks::check_exit(line_info, this->m_base.has_value(), "Value was null"); return this->m_base.value(); } diff --git a/toolsrc/include/vcpkg/base/stringview.h b/toolsrc/include/vcpkg/base/stringview.h index aba27f9d1..c6e0c6350 100644 --- a/toolsrc/include/vcpkg/base/stringview.h +++ b/toolsrc/include/vcpkg/base/stringview.h @@ -2,28 +2,16 @@ #include <vcpkg/base/fwd/stringview.h> -#include <vcpkg/base/optional.h> +#include <stddef.h> +#include <iterator> #include <limits> #include <string> -#include <vector> namespace vcpkg { struct StringView { - static std::vector<StringView> find_all_enclosed(const StringView& input, - const std::string& left_delim, - const std::string& right_delim) noexcept; - - static StringView find_exactly_one_enclosed(const StringView& input, - const std::string& left_tag, - const std::string& right_tag) noexcept; - - static Optional<StringView> find_at_most_one_enclosed(const StringView& input, - const std::string& left_tag, - const std::string& right_tag) noexcept; - constexpr StringView() = default; StringView(const std::string& s) noexcept; // Implicit by design diff --git a/toolsrc/include/vcpkg/base/system.debug.h b/toolsrc/include/vcpkg/base/system.debug.h index d541b970a..898052a01 100644 --- a/toolsrc/include/vcpkg/base/system.debug.h +++ b/toolsrc/include/vcpkg/base/system.debug.h @@ -11,12 +11,6 @@ namespace vcpkg::Debug extern std::atomic<bool> g_debugging; template<class... Args> - void print(System::Color c, const Args&... args) - { - if (g_debugging) System::print2(c, "[DEBUG] ", args...); - } - - template<class... Args> void print(const Args&... args) { if (g_debugging) System::print2("[DEBUG] ", args...); diff --git a/toolsrc/include/vcpkg/commands.version.h b/toolsrc/include/vcpkg/commands.version.h index 4b05881ba..6deb5167d 100644 --- a/toolsrc/include/vcpkg/commands.version.h +++ b/toolsrc/include/vcpkg/commands.version.h @@ -4,9 +4,8 @@ namespace vcpkg::Commands::Version { - const char* base_version(); - const char* version(); - void warn_if_vcpkg_version_mismatch(const VcpkgPaths& paths); + const char* base_version() noexcept; + const char* version() noexcept; void perform_and_exit(const VcpkgCmdArguments& args, Files::Filesystem& fs); struct VersionCommand : BasicCommand |
