refactor: build from conventions, adopt ConsoleLogger, and colorize TTY output
This commit is contained in:
1 parent
ea6ac852e7
commit
83ec08390f
23 files changed
+172
-151
No files matched your search
-1260
File diff suppressed because it is too large.
Load diff
@@ -474,7 +474,7 @@ compile_descriptor(const FrontendOptions &options) {
|
||||
std::filesystem::is_regular_file(options.sdk_library_path, ec);
|
||||
if (!use_prebuilt_sdk && !options.sdk_source_dir.empty()) {
|
||||
compile_units.push_back(options.sdk_source_dir / "Build.cpp");
|
||||
compile_units.push_back(options.sdk_source_dir / "Util" / "Json.cpp");
|
||||
compile_units.push_back(options.sdk_source_dir / "Json.cpp");
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> objects;
|
||||
|
||||
+17
-1
@@ -480,6 +480,22 @@ expand_glob(const std::filesystem::path &project_root, std::string_view pattern)
|
||||
"expand_glob: project root '{}' is not a directory", project_root.string()))};
|
||||
}
|
||||
|
||||
// Patterns may be written relative to the project root or absolute paths anchored at it
|
||||
// (recipes commonly know the project's absolute location). Match against relative paths.
|
||||
std::string effective_pattern{pattern};
|
||||
{
|
||||
const std::filesystem::path raw_pattern{std::string{pattern}};
|
||||
if (raw_pattern.is_absolute()) {
|
||||
const auto rel_pattern = raw_pattern.lexically_relative(canonical_root);
|
||||
if (!rel_pattern.empty() && rel_pattern.native().starts_with("..")) {
|
||||
return std::unexpected{make_error(
|
||||
std::format("expand_glob: pattern '{}' escapes project root '{}'", pattern,
|
||||
canonical_root.string()))};
|
||||
}
|
||||
effective_pattern = rel_pattern.lexically_normal().generic_string();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> matches;
|
||||
std::filesystem::recursive_directory_iterator it{
|
||||
canonical_root, std::filesystem::directory_options::skip_permission_denied, ec};
|
||||
@@ -493,7 +509,7 @@ expand_glob(const std::filesystem::path &project_root, std::string_view pattern)
|
||||
}
|
||||
if (it->is_regular_file(ec)) {
|
||||
const std::string gen = rel.lexically_normal().generic_string();
|
||||
if (match_glob(pattern, gen)) {
|
||||
if (match_glob(effective_pattern, gen)) {
|
||||
matches.push_back(rel.lexically_normal());
|
||||
}
|
||||
}
|
||||
|
||||
+72
-32
@@ -35,7 +35,11 @@
|
||||
#include <Void/Configuration/Argparse.hpp>
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/Log/ConsoleLogger.hpp>
|
||||
#include <Void/Log/Formatter.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Log/LoggingAdapter.hpp>
|
||||
#include <unistd.h>
|
||||
#include <Void/System/SignalWatcher.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Text/Semver.hpp>
|
||||
@@ -68,6 +72,60 @@ namespace {
|
||||
|
||||
using namespace Void::Build;
|
||||
|
||||
// ANSI colors are enabled only for a terminal and are disabled by NO_COLOR or --no-color.
|
||||
bool g_no_color = false;
|
||||
|
||||
bool color_enabled(int fd = 2) noexcept {
|
||||
if (g_no_color || ::getenv("NO_COLOR") != nullptr) {
|
||||
return false;
|
||||
}
|
||||
return ::isatty(fd) == 1;
|
||||
}
|
||||
|
||||
std::string colorize_help(std::string_view text) {
|
||||
if (!color_enabled(1)) {
|
||||
return std::string{text};
|
||||
}
|
||||
constexpr std::string_view kReset = "\x1b[0m";
|
||||
constexpr std::string_view kBold = "\x1b[1m";
|
||||
constexpr std::string_view kCyan = "\x1b[36m";
|
||||
std::string out;
|
||||
out.reserve(text.size() + 64);
|
||||
std::size_t pos = 0;
|
||||
while (pos <= text.size()) {
|
||||
const auto nl = text.find('\n', pos);
|
||||
const auto end = nl == std::string_view::npos ? text.size() : nl;
|
||||
const std::string_view line = text.substr(pos, end - pos);
|
||||
const bool section_header =
|
||||
!line.empty() && line.back() == ':' && line.find_first_not_of(" \t") == 0;
|
||||
const bool usage = line.starts_with("Usage:");
|
||||
const bool indented_entry = line.starts_with(" ") && !section_header &&
|
||||
line.find_first_not_of(' ') != std::string_view::npos &&
|
||||
line.find(" ") != std::string_view::npos;
|
||||
if (section_header || usage) {
|
||||
out += kBold;
|
||||
out += line;
|
||||
out += kReset;
|
||||
} else if (indented_entry) {
|
||||
const auto token_start = line.find_first_not_of(' ');
|
||||
const auto token_end = line.find(' ', token_start);
|
||||
out += line.substr(0, token_start);
|
||||
out += kCyan;
|
||||
out += line.substr(token_start, token_end - token_start);
|
||||
out += kReset;
|
||||
out += line.substr(token_end);
|
||||
} else {
|
||||
out += line;
|
||||
}
|
||||
if (nl == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
out += '\n';
|
||||
pos = nl + 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
class ConsoleLogGuard {
|
||||
public:
|
||||
explicit ConsoleLogGuard(Void::Log::Level level) {
|
||||
@@ -76,46 +134,27 @@ class ConsoleLogGuard {
|
||||
// Suppress internal VoidLibrary FileSystem open-miss warnings on cold cache probes.
|
||||
Void::Log::set_module_level("FileSystem", Void::Log::Level::Error);
|
||||
}
|
||||
reg_id_ =
|
||||
Void::Log::register_sink([mutex = std::make_shared<std::mutex>()](
|
||||
Void::Log::Level lvl, std::string_view module,
|
||||
std::string_view message, const std::source_location &) {
|
||||
std::lock_guard lock{*mutex};
|
||||
switch (lvl) {
|
||||
case Void::Log::Level::Trace:
|
||||
std::cerr << "[vbs][TRACE][" << module << "] " << message << '\n' << std::flush;
|
||||
break;
|
||||
case Void::Log::Level::Debug:
|
||||
std::cerr << "[vbs][DEBUG][" << module << "] " << message << '\n' << std::flush;
|
||||
break;
|
||||
case Void::Log::Level::Info:
|
||||
std::cerr << "[vbs][" << module << "] " << message << '\n' << std::flush;
|
||||
break;
|
||||
case Void::Log::Level::Warning:
|
||||
std::cerr << "[vbs][WARN][" << module << "] " << message << '\n' << std::flush;
|
||||
break;
|
||||
case Void::Log::Level::Error:
|
||||
std::cerr << "[vbs][ERROR][" << module << "] " << message << '\n' << std::flush;
|
||||
break;
|
||||
case Void::Log::Level::Fatal:
|
||||
std::cerr << "[vbs][FATAL][" << module << "] " << message << '\n' << std::flush;
|
||||
break;
|
||||
case Void::Log::Level::Disabled:
|
||||
break;
|
||||
}
|
||||
});
|
||||
logger_ = std::make_unique<Void::Logging::ConsoleLogger>();
|
||||
if (color_enabled()) {
|
||||
auto formatter = std::make_shared<Void::Logging::PatternFormatter>("[{level:,auto}] {message}");
|
||||
formatter->set_color_policy(Void::Logging::ColorPolicy::defaults());
|
||||
logger_->set_formatter(std::move(formatter));
|
||||
}
|
||||
reg_id_ = Void::Log::register_sink(Void::Log::make_sink(*logger_));
|
||||
}
|
||||
|
||||
~ConsoleLogGuard() {
|
||||
if (reg_id_ != 0) {
|
||||
(void)Void::Log::detach_and_drain(reg_id_);
|
||||
}
|
||||
logger_.reset();
|
||||
}
|
||||
|
||||
ConsoleLogGuard(const ConsoleLogGuard &) = delete;
|
||||
ConsoleLogGuard &operator=(const ConsoleLogGuard &) = delete;
|
||||
|
||||
private:
|
||||
std::unique_ptr<Void::Logging::ConsoleLogger> logger_;
|
||||
Void::Log::RegistrationId reg_id_{0};
|
||||
};
|
||||
|
||||
@@ -179,7 +218,7 @@ SnapshotInputs collect_snapshot_inputs(const Graph::ProjectDescription &desc,
|
||||
}
|
||||
};
|
||||
|
||||
for (const char *d : {"src", "source", "include", "tests"}) {
|
||||
for (const char *d : {"src", "sdk", "source", "include", "tests"}) {
|
||||
add_dir_if_in_project(root / d);
|
||||
}
|
||||
|
||||
@@ -2666,7 +2705,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
auto add_log_flags = [&](Void::Argparse::Parser &p, bool allow_short_v = true) {
|
||||
p.flag("--verbose", allow_short_v ? "-v" : "", "Enable verbose debug logs", verbose)
|
||||
.flag("--quiet", "-q", "Suppress progress logs (warnings and errors only)", quiet);
|
||||
.flag("--quiet", "-q", "Suppress progress logs (warnings and errors only)", quiet)
|
||||
.flag("--no-color", "", "Disable ANSI colors", g_no_color);
|
||||
};
|
||||
|
||||
auto configure_build_or_test = [&](Void::Argparse::Parser &p) {
|
||||
@@ -3218,7 +3258,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
const auto parsed = root.parse_detailed(argc, argv);
|
||||
if (parsed.is_help()) {
|
||||
std::cout << parsed.message;
|
||||
std::cout << colorize_help(parsed.message);
|
||||
return 0;
|
||||
}
|
||||
if (parsed.is_error()) {
|
||||
@@ -3234,7 +3274,7 @@ int main(int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
if (parsed.matched_command_path.empty()) {
|
||||
std::cout << root.help();
|
||||
std::cout << colorize_help(root.help());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+11
-7
@@ -202,7 +202,11 @@ materialize_binary_variant(const BinaryCompatibilityKey &key,
|
||||
Void::Result::Result<MaterializedSource, Void::Result::Error>
|
||||
materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &options) {
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(options.cache_dir, ec);
|
||||
// Cache paths are made absolute so materialized sources stay valid regardless of the
|
||||
// provider's/consumer's working directory.
|
||||
const std::filesystem::path cache_dir =
|
||||
std::filesystem::absolute(options.cache_dir, ec).lexically_normal();
|
||||
std::filesystem::create_directories(cache_dir, ec);
|
||||
|
||||
// 1. Local path source: compute content hash without mutating the external checkout.
|
||||
if (dep.source_kind == DependencySourceKind::Path) {
|
||||
@@ -217,7 +221,7 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
}
|
||||
Snapshot::SnapshotOptions sopts;
|
||||
sopts.project_root = canon;
|
||||
sopts.snapshot_store_dir = options.cache_dir / "path-snapshots";
|
||||
sopts.snapshot_store_dir = cache_dir / "path-snapshots";
|
||||
sopts.materialize = false;
|
||||
// Generated editor metadata is never a build input and may be an out-of-tree symlink.
|
||||
sopts.excluded_names.push_back("compile_commands.json");
|
||||
@@ -235,9 +239,9 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
|
||||
// 2. Checksummed archive source (local file:// or verified cached tar).
|
||||
if (dep.source_kind == DependencySourceKind::Archive) {
|
||||
const auto extract_dir = options.cache_dir / "archives" / dep.sha256;
|
||||
const auto extract_dir = cache_dir / "archives" / dep.sha256;
|
||||
const auto marker = extract_dir / ".vbs_verified";
|
||||
const auto lock_path = options.cache_dir / "locks" / (dep.sha256 + ".lock");
|
||||
const auto lock_path = cache_dir / "locks" / (dep.sha256 + ".lock");
|
||||
auto lock = Platform::FileLock::acquire(lock_path, Platform::LockMode::Exclusive);
|
||||
if (!lock) {
|
||||
return std::unexpected{lock.error()};
|
||||
@@ -252,7 +256,7 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
return out;
|
||||
}
|
||||
|
||||
const auto downloaded_tar = options.cache_dir / "downloads" / (dep.sha256 + ".tar");
|
||||
const auto downloaded_tar = cache_dir / "downloads" / (dep.sha256 + ".tar");
|
||||
std::filesystem::create_directories(downloaded_tar.parent_path(), ec);
|
||||
|
||||
if (!std::filesystem::is_regular_file(downloaded_tar, ec)) {
|
||||
@@ -348,9 +352,9 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
}
|
||||
|
||||
const auto git_dir =
|
||||
options.cache_dir / "git" / (sanitize_key(dep.name) + "_" + dep.revision);
|
||||
cache_dir / "git" / (sanitize_key(dep.name) + "_" + dep.revision);
|
||||
const auto marker = git_dir / ".vbs_verified_rev";
|
||||
const auto lock_path = options.cache_dir / "locks" / ("git_" + dep.revision + ".lock");
|
||||
const auto lock_path = cache_dir / "locks" / ("git_" + dep.revision + ".lock");
|
||||
auto lock = Platform::FileLock::acquire(lock_path, Platform::LockMode::Exclusive);
|
||||
if (!lock) {
|
||||
return std::unexpected{lock.error()};
|
||||
|
||||
@@ -931,7 +931,7 @@ std::vector<std::string> parse_void_modules_option(const OptionMap &options) {
|
||||
}
|
||||
}
|
||||
if (mods.empty()) {
|
||||
mods = {"Configuration", "Text", "Unicode", "Async",
|
||||
mods = {"Configuration", "Text", "Unicode", "Async", "Log",
|
||||
"Algorithms", "Crypto", "Archive", "Testing"};
|
||||
}
|
||||
return mods;
|
||||
@@ -955,7 +955,7 @@ collect_native_dep_snapshot_inputs(const Graph::ProjectDescription &desc,
|
||||
root_dirs.insert(norm);
|
||||
}
|
||||
};
|
||||
for (const char *d : {"src", "source", "include", "tests"}) {
|
||||
for (const char *d : {"src", "sdk", "source", "include", "tests"}) {
|
||||
add_dir_if_in_project(root / d);
|
||||
}
|
||||
auto is_gen_ref = [](std::string_view p) {
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
|
||||
#ifdef VBS_HAS_VOID_UNICODE
|
||||
#include <Void/Unicode/Utf.hpp>
|
||||
#endif
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Void::Build::Util {
|
||||
|
||||
bool is_valid_utf8(std::string_view text) noexcept {
|
||||
#ifdef VBS_HAS_VOID_UNICODE
|
||||
Void::Unicode::UtfLimits limits;
|
||||
limits.max_input_units = text.size();
|
||||
limits.max_output_units = text.size();
|
||||
return Void::Unicode::validate_utf8(text, limits).has_value();
|
||||
#else
|
||||
std::size_t position = 0;
|
||||
while (position < text.size()) {
|
||||
const auto lead = static_cast<unsigned char>(text[position++]);
|
||||
if (lead < 0x80) {
|
||||
continue;
|
||||
}
|
||||
std::size_t remaining = 0;
|
||||
std::uint32_t scalar = 0;
|
||||
std::uint32_t minimum = 0;
|
||||
if (lead >= 0xc2 && lead <= 0xdf) {
|
||||
remaining = 1;
|
||||
scalar = lead & 0x1f;
|
||||
minimum = 0x80;
|
||||
} else if (lead >= 0xe0 && lead <= 0xef) {
|
||||
remaining = 2;
|
||||
scalar = lead & 0x0f;
|
||||
minimum = 0x800;
|
||||
} else if (lead >= 0xf0 && lead <= 0xf4) {
|
||||
remaining = 3;
|
||||
scalar = lead & 0x07;
|
||||
minimum = 0x10000;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (remaining > text.size() - position) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < remaining; ++i) {
|
||||
const auto continuation = static_cast<unsigned char>(text[position++]);
|
||||
if ((continuation & 0xc0) != 0x80) {
|
||||
return false;
|
||||
}
|
||||
scalar = (scalar << 6) | (continuation & 0x3f);
|
||||
}
|
||||
if (scalar < minimum || scalar > 0x10ffff || (scalar >= 0xd800 && scalar <= 0xdfff)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void require_valid_utf8(std::string_view text) {
|
||||
if (!is_valid_utf8(text)) {
|
||||
throw std::invalid_argument{"JSON string contains invalid UTF-8"};
|
||||
}
|
||||
}
|
||||
|
||||
std::string json_quote(std::string_view text) {
|
||||
require_valid_utf8(text);
|
||||
constexpr char hex[] = "0123456789abcdef";
|
||||
std::string output{"\""};
|
||||
output.reserve(text.size() + 2);
|
||||
for (const unsigned char c : text) {
|
||||
switch (c) {
|
||||
case '"':
|
||||
output += "\\\"";
|
||||
break;
|
||||
case '\\':
|
||||
output += "\\\\";
|
||||
break;
|
||||
case '\n':
|
||||
output += "\\n";
|
||||
break;
|
||||
case '\r':
|
||||
output += "\\r";
|
||||
break;
|
||||
case '\t':
|
||||
output += "\\t";
|
||||
break;
|
||||
default:
|
||||
if (c < 0x20) {
|
||||
output += "\\u00";
|
||||
output += hex[c >> 4];
|
||||
output += hex[c & 0x0f];
|
||||
} else {
|
||||
output += static_cast<char>(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
output += '"';
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Util
|
||||
@@ -199,7 +199,7 @@ WatchCycleReport run_reconcile_cycle(const WatchLoopOptions &options, std::size_
|
||||
// 6. Capture input snapshot (content-verified; materialize only for FullBuild execution).
|
||||
std::vector<std::filesystem::path> scan_roots;
|
||||
std::error_code ec;
|
||||
for (const char *d : {"src", "source", "include", "tests"}) {
|
||||
for (const char *d : {"src", "sdk", "source", "include", "tests"}) {
|
||||
if (std::filesystem::is_directory(options.frontend.project_root / d, ec)) {
|
||||
scan_roots.push_back(options.frontend.project_root / d);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user