diff options
| author | Leitwolf | 2026-11-02 00:51:11 +0300 |
|---|---|---|
| committer | GitHub | 2026-11-02 00:51:11 +0300 |
| commit | 268698be92962247a21a86dda4de2d4267adcf8f (patch) | |
| tree | 510f7ece1750b2da6d9b4666b7c58908c8d193bb /tools/ThirdPartyMain.cpp | |
| parent | 13324a043042110eba456424fa39c1334857ceaa (diff) | |
| parent | 3ed0fd930371823b6d44ae64d00dbd79e421a552 (diff) | |
| download | catch3-268698be92962247a21a86dda4de2d4267adcf8f.tar.gz | |
Merge pull request #36 from Progmasoft/feature/cpp-tools
Move the tools from Python to C++17 and Bazel
Diffstat (limited to 'tools/ThirdPartyMain.cpp')
| -rw-r--r-- | tools/ThirdPartyMain.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/tools/ThirdPartyMain.cpp b/tools/ThirdPartyMain.cpp new file mode 100644 index 00000000..796c6182 --- /dev/null +++ b/tools/ThirdPartyMain.cpp @@ -0,0 +1,100 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +// bazelisk run //tools:third_party -- --check +// bazelisk run //tools:third_party -- --update +// +// Keeps third_party/ equal to the archives MODULE.bazel pins by version and +// sha256. BUILD.bazel passes each file as <vendored path>=<runfile>. + +#include <iostream> +#include <memory> +#include <string> +#include <string_view> +#include <utility> +#include <vector> + +#include "rules_cc/cc/runfiles/runfiles.h" +#include "tools/Repository.hpp" + +int +main(int argc, char **argv) +{ + using namespace Progmasoft::Catch3::Tools; + using rules_cc::cc::runfiles::Runfiles; + + std::vector<std::string_view> files; + std::string_view mode; + bool usable = true; + for (int index = 1; index < argc; ++index) + { + const std::string_view argument{ argv[index] }; + if (argument == "--check" || argument == "--update") + { + usable = usable && mode.empty(); + mode = argument; + } + else if (argument.find('=') != std::string_view::npos) + { + files.push_back(argument); + } + else + { + usable = false; + } + } + if (!usable || mode.empty()) + { + std::cerr << "usage: bazelisk run //tools:third_party -- " + "--check|--update\n"; + return 2; + } + const auto root = RepositoryRoot(); + if (!root) + { + std::cerr << "run this tool with bazelisk run\n"; + return 2; + } + std::string error; + const std::unique_ptr<Runfiles> runfiles{ + Runfiles::Create(argv[0], std::string{}, &error) + }; + if (runfiles == nullptr) + { + std::cerr << error << '\n'; + return 2; + } + Problems problems; + // Every archive is read before any file changes. + std::vector<std::pair<std::string_view, std::string>> archived; + for (const auto file : files) + { + const auto separator = file.find('='); + archived.emplace_back(file.substr(0, separator), + ReadOrReport(runfiles->Rlocation(std::string{ + file.substr(separator + 1) }), + problems)); + } + if (!problems.empty()) + { + return Finish(problems); + } + for (const auto &[vendored, text] : archived) + { + if (mode == "--check") + { + if (ReadText(*root / vendored) != text) + { + problems.push_back(std::string{ vendored } + + " differs from its pinned archive: run " + "bazelisk run //tools:third_party -- " + "--update"); + } + } + else if (!WriteText(*root / vendored, text)) + { + problems.push_back("cannot write " + std::string{ vendored }); + } + } + return Finish(problems); +} |