catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Move the tools from Python to C++17 and Bazel
- Commit
da3c9d0880dbd4a74e0e43f092d10f5ae42c5043- Author
- Leitwolf11 ·
- Parents
241b5567d09d
Changes
.github/renovate.json5 | 12 +- .github/workflows/validate-header-guards.yml | 40 +- MODULE.bazel | 27 ++ docs/contributing.md | 30 +- docs/upstream-status.md | 2 +- extras/catch_amalgamated.cpp | 1 - extras/catch_amalgamated.hpp | 1 - extras/catch_amalgamated_third_party.hpp | 1 - tests/CMakeLists.txt | 11 +- third_party/pugixml/BUILD.bazel | 2 +- third_party/pugixml/CMakeLists.txt | 2 +- third_party/pugixml/meson.build | 2 +- third_party/pugixml/premake5.lua | 2 +- third_party/pugixml/xmake.lua | 2 +- third_party/yyjson/BUILD.bazel | 7 +- third_party/yyjson/CMakeLists.txt | 2 +- third_party/yyjson/meson.build | 2 +- third_party/yyjson/premake5.lua | 2 +- third_party/yyjson/xmake.lua | 2 +- tools/AmalgamateMain.cpp | 43 ++ tools/Amalgamation.cpp | 582 +++++++++++++++++++++++++++ tools/Amalgamation.hpp | 54 +++ tools/BUILD.bazel | 101 +++++ tools/CheckMain.cpp | 41 ++ tools/Checks.cpp | 254 ++++++++++++ tools/Checks.hpp | 31 ++ tools/ReleaseNotesMain.cpp | 49 +++ tools/RepoInfoMain.cpp | 34 ++ tools/Repository.cpp | 194 +++++++++ tools/Repository.hpp | 64 +++ tools/RepositoryReport.cpp | 276 +++++++++++++ tools/RepositoryReport.hpp | 47 +++ tools/ThirdPartyMain.cpp | 89 ++++ tools/Tools.tests.cpp | 116 ++++++ tools/VersionMain.cpp | 69 ++++ tools/Versions.cpp | 205 ++++++++++ tools/Versions.hpp | 67 +++ tools/scripts/buildAndTest.cmd | 16 - tools/scripts/buildAndTest.sh | 18 - tools/scripts/checkConvenienceHeaders.py | 141 ------- tools/scripts/checkDuplicateFilenames.py | 30 -- tools/scripts/checkLicense.py | 62 --- tools/scripts/fixWhitespace.py | 51 --- tools/scripts/generateAmalgamatedFiles.py | 298 -------------- tools/scripts/releaseCommon.py | 29 -- tools/scripts/scriptCommon.py | 11 - tools/scripts/updateDocumentSnippets.py | 23 -- tools/scripts/updateDocumentToC.py | 447 -------------------- tools/scripts/updateThirdParty.py | 103 ----- tools/{scripts => }/upstreamFiles.txt | 0 50 files changed, 2403 insertions(+), 1292 deletions(-) diff --git a/.github/renovate.json5 b/.github/renovate.json5 index 4a634ea3..b1accb38 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -46,16 +46,16 @@ "custom.regex", ], - // third_party/ holds copies of release archives, which - // tools/scripts/updateThirdParty.py pins by version and sha256. Renovate - // changes the version; the pull request then needs the new digest and - // --update, which the third_party check of CI asks for. + // third_party/ holds copies of release archives, which MODULE.bazel pins by + // version and sha256. Renovate changes the version; the pull request then + // needs the new digest and //tools:third-party -- --update, which the + // third_party check of CI asks for. customManagers: [ { customType: "regex", - managerFilePatterns: ["/^tools/scripts/updateThirdParty\\.py$/"], + managerFilePatterns: ["/^MODULE\\.bazel$/"], matchStrings: [ - "# renovate: datasource=(?<datasource>\\S+) depName=(?<depName>\\S+)( extractVersion=(?<extractVersion>\\S+))?\\s+\"version\": \"(?<currentValue>[^\"]+)\"", + "# renovate: datasource=(?<datasource>\\S+) depName=(?<depName>\\S+)( extractVersion=(?<extractVersion>\\S+))?\\s+\\w+_VERSION = \"(?<currentValue>[^\"]+)\"", ], }, ], diff --git a/.github/workflows/validate-header-guards.yml b/.github/workflows/validate-header-guards.yml index a8ba91ba..41a4b0a4 100644 --- a/.github/workflows/validate-header-guards.yml +++ b/.github/workflows/validate-header-guards.yml @@ -19,31 +19,23 @@ jobs: with: persist-credentials: false - - name: Setup Dependencies - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 + - uses: bazel-contrib/setup-bazel@c5acdfb288317d0b5c0bbd7a396a3dc868bb0f86 # 0.19.0 with: - python-version: '3.12' - - # Every header is guarded with #pragma once, as the Progmasoft C++ Style - # asks; the amalgamation script relies on it. - - name: Check that every header starts its guard with pragma once - run: | - wrong_files=$(grep -rL --include='*.hpp' '^#pragma once' src/Progmasoft || true) - if [[ $wrong_files ]]; then - echo "Headers without #pragma once:" - echo $wrong_files - exit 1 - fi - - - name: Check that there are no duplicated filenames - run: | - ./tools/scripts/checkDuplicateFilenames.py - - - name: Check that all source files have the correct license header - run: | - ./tools/scripts/checkLicense.py + bazelisk-cache: true + repository-cache: true + + - name: Test the tools + run: bazel test //tools:tools_tests + env: + CC: clang + + - name: Check licenses, headers, file names, versions and the amalgamation + run: bazel run //tools:check + env: + CC: clang # third_party/ must be the pinned release archives, byte for byte. - name: Check that third_party matches its pinned archives - run: | - ./tools/scripts/updateThirdParty.py --check + run: bazel run //tools:third-party -- --check + env: + CC: clang diff --git a/MODULE.bazel b/MODULE.bazel index 9e0fc89d..1fbd7f52 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -5,6 +5,33 @@ bazel_dep(name = "rules_cc", version = "0.2.17") bazel_dep(name = "rules_license", version = "1.0.0") bazel_dep(name = "platforms", version = "1.0.0") +# The archives third_party/ holds copies of. Renovate changes a version; the +# pull request then needs the digest of the new archive and +# `bazelisk run //tools:third-party -- --update`, which CI asks for. +http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# renovate: datasource=github-tags depName=ibireme/yyjson +YYJSON_VERSION = "0.13.0" + +http_archive( + name = "yyjson_archive", + build_file_content = 'exports_files(["src/yyjson.c", "src/yyjson.h"])', + sha256 = "34e0f62a2bc11ab20d601e8ca1cc2b2079503aa45119a19133d89d19b94a0fae", + strip_prefix = "yyjson-" + YYJSON_VERSION, + urls = ["https://github.com/ibireme/yyjson/archive/refs/tags/" + YYJSON_VERSION + ".tar.gz"], +) + +# renovate: datasource=github-tags depName=zeux/pugixml extractVersion=^v(?<version>.+)$ +PUGIXML_VERSION = "1.16" + +http_archive( + name = "pugixml_archive", + build_file_content = 'exports_files(["src/pugiconfig.hpp", "src/pugixml.cpp", "src/pugixml.hpp"])', + sha256 = "357bcab8877dc9943f355d3a72daba1b053238ba955f50fa81586afb65090219", + strip_prefix = "pugixml-" + PUGIXML_VERSION, + urls = ["https://github.com/zeux/pugixml/archive/refs/tags/v" + PUGIXML_VERSION + ".tar.gz"], +) + # rules_swift reaches the graph through protobuf and rules_apple. Up to 3.4.1 # its Windows autoconfiguration, which runs whenever swiftc.exe is on PATH, # writes a toolchain that indexes APPLE_PLATFORMS_CONSTRAINTS with an diff --git a/docs/contributing.md b/docs/contributing.md index ecccf68f..ef47d541 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -47,13 +47,28 @@ Meson, Xmake and Premake builds exist as well and are checked in CI. Before you push, run the repository checks: ``` -python tools/scripts/checkLicense.py -python tools/scripts/checkConvenienceHeaders.py -python tools/scripts/checkDuplicateFilenames.py +bazelisk run //tools:check ``` -A change to anything under `src/` also regenerates the single-file -distribution with `python tools/scripts/generateAmalgamatedFiles.py`. +It checks licenses, convenience headers, header guards, file names, that every +place states the same version, and that the single-file distribution is up to +date. A change to anything under `src/` regenerates that distribution: + +``` +bazelisk run //tools:amalgamate +``` + +The tools in `tools/` are C++17 and run through Bazel, with arguments after +`--`: + +| Tool | Does | +| --- | --- | +| `//tools:amalgamate` | writes the single-file distribution; `-- --check` only compares it | +| `//tools:check` | runs the repository checks | +| `//tools:third-party` | `-- --check` compares `third_party/` with the archives `MODULE.bazel` pins, `-- --update` writes it | +| `//tools:version` | prints the version; `-- --next` or `-- <version>` moves every place that states it, and the distribution | +| `//tools:release-notes` | prints a section of the release notes, of `-- <version>` or the current one | +| `//tools:repo-info` | reports the checkout and the tools on `PATH`; `-- --json` writes it as JSON | ## Two parts, two sets of rules @@ -71,8 +86,7 @@ AdditionRef-Progmasoft-Exception-2.0`, follows the C++17, reports recoverable errors with `Expected` rather than exceptions, and is formatted with the `.clang-format` beside it. -`tools/scripts/checkLicense.py` checks that every source file carries the -header of its part. +`//tools:check` checks that every source file carries the header of its part. ## Tests @@ -88,7 +102,7 @@ comes with a test that fails without it. User documentation lives in `docs/`. A new page is linked from [the documentation index](Readme.md#top), and a page with sections gets a table -of contents; `python tools/scripts/updateDocumentToC.py` keeps them current. +of contents. A user-visible change gets a line in [the release notes](release-notes.md#top). diff --git a/docs/upstream-status.md b/docs/upstream-status.md index 0899e31d..93483fb3 100644 --- a/docs/upstream-status.md +++ b/docs/upstream-status.md @@ -36,7 +36,7 @@ backported, in upstream order, and the compatibility engine is now 3.16.1: Each applied without a conflict in the sources. The amalgamated distribution under `extras/` did conflict, because it is generated; it was generated again -from the sources of this repository with `generateAmalgamatedFiles.py`, which +from the sources of this repository with `//tools:amalgamate`, which also brought it up to date with fork changes it had not yet picked up. No fork-owned file under `src/Progmasoft` needed a change. diff --git a/extras/catch_amalgamated.cpp b/extras/catch_amalgamated.cpp index 5bfb7d5d..a39fd323 100644 --- a/extras/catch_amalgamated.cpp +++ b/extras/catch_amalgamated.cpp @@ -7,7 +7,6 @@ // SPDX-License-Identifier: BSL-1.0 // Catch3 v0.3.0.1, on the Catch2 v3.16.1 engine -// Generated: 2026-11-01 22:03:50.683955 // ---------------------------------------------------------- // This file is an amalgamation of multiple different files. // You probably shouldn't edit it directly. diff --git a/extras/catch_amalgamated.hpp b/extras/catch_amalgamated.hpp index 403d18f6..859f9455 100644 --- a/extras/catch_amalgamated.hpp +++ b/extras/catch_amalgamated.hpp @@ -7,7 +7,6 @@ // SPDX-License-Identifier: BSL-1.0 // Catch3 v0.3.0.1, on the Catch2 v3.16.1 engine -// Generated: 2026-11-01 22:03:50.587792 // ---------------------------------------------------------- // This file is an amalgamation of multiple different files. // You probably shouldn't edit it directly. diff --git a/extras/catch_amalgamated_third_party.hpp b/extras/catch_amalgamated_third_party.hpp index 7929d123..e9a8c4f7 100644 --- a/extras/catch_amalgamated_third_party.hpp +++ b/extras/catch_amalgamated_third_party.hpp @@ -1,5 +1,4 @@ // Catch3 v0.3.0.1: yyjson and pugixml for catch_amalgamated.cpp -// Generated: 2026-11-01 22:03:50.777818 // ---------------------------------------------------------- // This file is an amalgamation of src/Progmasoft/Runner/Yyjson.inl, // src/Progmasoft/PugiXml.inl and the libraries they include from diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 40ed1358..b6b9b52c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,7 +33,7 @@ if(CATCH3_BUILD_SURROGATES) function(ExtractCatch3Headers OutArg) # The engine's headers are the ones that come from Catch2; the surrogates # check them at the language level of the build. - file(STRINGS "${CATCH_DIR}/tools/scripts/upstreamFiles.txt" upstreamFiles REGEX "^src/") + file(STRINGS "${CATCH_DIR}/tools/upstreamFiles.txt" upstreamFiles REGEX "^src/") get_target_property(targetSources Catch3 SOURCES) foreach(Source ${targetSources}) string(REGEX MATCH "^.*\\.hpp$" isHeader ${Source}) @@ -366,15 +366,6 @@ set_tests_properties(TagAlias PROPERTIES PASS_REGULAR_EXPRESSION "[1-9][0-9]* matching test\\(s\\)" ) -add_test(NAME CheckConvenienceHeaders - COMMAND - Python3::Interpreter ${CATCH_DIR}/tools/scripts/checkConvenienceHeaders.py -) -set_tests_properties(CheckConvenienceHeaders - PROPERTIES - LABELS "uses-python" -) - add_test(NAME "Benchmarking::SkipBenchmarkMacros" COMMAND $<TARGET_FILE:SelfTest> --no-history "Skip benchmark macros" diff --git a/third_party/pugixml/BUILD.bazel b/third_party/pugixml/BUILD.bazel index a73f10b7..120463bf 100644 --- a/third_party/pugixml/BUILD.bazel +++ b/third_party/pugixml/BUILD.bazel @@ -4,7 +4,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library") # pugixml (MIT), the XML writer of the reports. -# The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +# The version is pinned in MODULE.bazel. Catch3 # compiles the library into one of its own translation units with # internal linkage, so this target only provides the files. cc_library( diff --git a/third_party/pugixml/CMakeLists.txt b/third_party/pugixml/CMakeLists.txt index 85113fa3..d6e6004d 100644 --- a/third_party/pugixml/CMakeLists.txt +++ b/third_party/pugixml/CMakeLists.txt @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 # pugixml (MIT), the XML writer of the reports. -# The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +# The version is pinned in MODULE.bazel. Catch3 # compiles the library into one of its own translation units with # internal linkage, so this target only provides the include directory. # Catch3 links it through $<BUILD_INTERFACE:...>; it is never installed. diff --git a/third_party/pugixml/meson.build b/third_party/pugixml/meson.build index 972ca1f1..b1897d87 100644 --- a/third_party/pugixml/meson.build +++ b/third_party/pugixml/meson.build @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 # pugixml (MIT), the XML writer of the reports. -# The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +# The version is pinned in MODULE.bazel. Catch3 # compiles the library into one of its own translation units with # internal linkage, so this only provides the include directory. pugixml_dep = declare_dependency( diff --git a/third_party/pugixml/premake5.lua b/third_party/pugixml/premake5.lua index ee9ebd81..999fae79 100644 --- a/third_party/pugixml/premake5.lua +++ b/third_party/pugixml/premake5.lua @@ -2,7 +2,7 @@ -- SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 -- pugixml (MIT), the XML writer of the reports. --- The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +-- The version is pinned in MODULE.bazel. Catch3 -- compiles the library into one of its own translation units with -- internal linkage, so the workspace only needs its directory. catch3_third_party = catch3_third_party or {} diff --git a/third_party/pugixml/xmake.lua b/third_party/pugixml/xmake.lua index 198b54b7..29b985b1 100644 --- a/third_party/pugixml/xmake.lua +++ b/third_party/pugixml/xmake.lua @@ -2,7 +2,7 @@ -- SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 -- pugixml (MIT), the XML writer of the reports. --- The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +-- The version is pinned in MODULE.bazel. Catch3 -- compiles the library into one of its own translation units with -- internal linkage, so this target only provides the files. target("catch3_pugixml") diff --git a/third_party/yyjson/BUILD.bazel b/third_party/yyjson/BUILD.bazel index e44d069c..6b791e0d 100644 --- a/third_party/yyjson/BUILD.bazel +++ b/third_party/yyjson/BUILD.bazel @@ -4,7 +4,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library") # yyjson (MIT), the JSON reader and writer of the runner. -# The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +# The version is pinned in MODULE.bazel. Catch3 # compiles the library into one of its own translation units with # internal linkage, so this target only provides the files. cc_library( @@ -12,5 +12,8 @@ cc_library( hdrs = ["yyjson.h"], includes = ["."], textual_hdrs = ["yyjson.c"], - visibility = ["//src/Progmasoft:__pkg__"], + visibility = [ + "//src/Progmasoft:__pkg__", + "//tools:__pkg__", + ], ) diff --git a/third_party/yyjson/CMakeLists.txt b/third_party/yyjson/CMakeLists.txt index 77200553..30623718 100644 --- a/third_party/yyjson/CMakeLists.txt +++ b/third_party/yyjson/CMakeLists.txt @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 # yyjson (MIT), the JSON reader and writer of the runner. -# The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +# The version is pinned in MODULE.bazel. Catch3 # compiles the library into one of its own translation units with # internal linkage, so this target only provides the include directory. # Catch3 links it through $<BUILD_INTERFACE:...>; it is never installed. diff --git a/third_party/yyjson/meson.build b/third_party/yyjson/meson.build index b3683188..6a6566e8 100644 --- a/third_party/yyjson/meson.build +++ b/third_party/yyjson/meson.build @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 # yyjson (MIT), the JSON reader and writer of the runner. -# The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +# The version is pinned in MODULE.bazel. Catch3 # compiles the library into one of its own translation units with # internal linkage, so this only provides the include directory. yyjson_dep = declare_dependency( diff --git a/third_party/yyjson/premake5.lua b/third_party/yyjson/premake5.lua index 07f3b3c9..fc17e1bf 100644 --- a/third_party/yyjson/premake5.lua +++ b/third_party/yyjson/premake5.lua @@ -2,7 +2,7 @@ -- SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 -- yyjson (MIT), the JSON reader and writer of the runner. --- The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +-- The version is pinned in MODULE.bazel. Catch3 -- compiles the library into one of its own translation units with -- internal linkage, so the workspace only needs its directory. catch3_third_party = catch3_third_party or {} diff --git a/third_party/yyjson/xmake.lua b/third_party/yyjson/xmake.lua index 207ecf61..e4e9c58c 100644 --- a/third_party/yyjson/xmake.lua +++ b/third_party/yyjson/xmake.lua @@ -2,7 +2,7 @@ -- SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 -- yyjson (MIT), the JSON reader and writer of the runner. --- The version is pinned in tools/scripts/updateThirdParty.py. Catch3 +-- The version is pinned in MODULE.bazel. Catch3 -- compiles the library into one of its own translation units with -- internal linkage, so this target only provides the files. target("catch3_yyjson") diff --git a/tools/AmalgamateMain.cpp b/tools/AmalgamateMain.cpp new file mode 100644 index 00000000..c5e2218c --- /dev/null +++ b/tools/AmalgamateMain.cpp @@ -0,0 +1,43 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +// bazelisk run //tools:amalgamate [-- --check] + +#include <iostream> +#include <string_view> + +#include "tools/Amalgamation.hpp" +#include "tools/Repository.hpp" + +int +main(int argc, char **argv) +{ + using namespace Progmasoft::Catch3::Tools; + + const bool check = argc == 2 && std::string_view{ argv[1] } == "--check"; + if (argc > 2 || (argc == 2 && !check)) + { + std::cerr << "usage: bazelisk run //tools:amalgamate [-- --check]\n"; + return 2; + } + const auto root = RepositoryRoot(); + if (!root) + { + std::cerr << "run this tool with bazelisk run\n"; + return 2; + } + Problems problems; + const auto amalgamation = GenerateAmalgamation(*root, problems); + if (problems.empty()) + { + if (check) + { + CheckAmalgamation(*root, amalgamation, problems); + } + else + { + WriteAmalgamation(*root, amalgamation, problems); + } + } + return Finish(problems); +} diff --git a/tools/Amalgamation.cpp b/tools/Amalgamation.cpp new file mode 100644 index 00000000..fc1d459a --- /dev/null +++ b/tools/Amalgamation.cpp @@ -0,0 +1,582 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <algorithm> +#include <array> +#include <cctype> +#include <map> +#include <set> +#include <utility> +#include <vector> + +#include "tools/Amalgamation.hpp" + +namespace Progmasoft::Catch3::Tools +{ + namespace + { + namespace fs = std::filesystem; + + constexpr std::string_view kHeaderHead = R"( +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +)"; + + constexpr std::string_view kHeaderTail + = R"(// ---------------------------------------------------------- +// This file is an amalgamation of multiple different files. +// You probably shouldn't edit it directly. +// +// The parts that come from src/Progmasoft, the Catch3 runner, keep +// their own copyright and license lines (MPL-2.0 WITH +// AdditionRef-Progmasoft-Exception-2.0). The runner needs C++17. +// ---------------------------------------------------------- +)"; + + constexpr std::string_view kThirdPartyTail + = R"(// ---------------------------------------------------------- +// This file is an amalgamation of src/Progmasoft/Runner/Yyjson.inl, +// src/Progmasoft/PugiXml.inl and the libraries they include from +// third_party/. You probably shouldn't edit it directly. +// +// yyjson and pugixml keep their MIT licenses, stated in their text below; +// the rest is MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0. Only +// catch_amalgamated.cpp includes this file. +// ---------------------------------------------------------- +)"; + + // The comments at the top of each engine file. + constexpr std::array<std::string_view, 5> kCopyrightLines{ + "// Copyright Catch2 Authors\n", + "// Distributed under the Boost Software License, Version 1.0.\n", + "// (See accompanying file LICENSE.txt or copy at\n", + "// https://www.boost.org/LICENSE_1_0.txt)\n", + "// SPDX-License-Identifier: BSL-1.0\n", + }; + + constexpr std::string_view kUserConfig + = "Progmasoft/Catch3/UserConfig.hpp"; + + const std::map<std::string, std::string> & + ThirdPartyFiles() + { + static const auto *files = new std::map<std::string, std::string>{ + { "yyjson.c", "yyjson/yyjson.c" }, + { "yyjson.h", "yyjson/yyjson.h" }, + { "pugixml.cpp", "pugixml/pugixml.cpp" }, + { "pugixml.hpp", "pugixml/pugixml.hpp" }, + { "pugiconfig.hpp", "pugixml/pugiconfig.hpp" }, + }; + return *files; + } + + [[nodiscard]] bool + Consists(std::string_view text, std::string_view extra) + { + return std::all_of(text.begin(), text.end(), [extra](char c) { + return std::isalpha(static_cast<unsigned char>(c)) != 0 + || extra.find(c) != std::string_view::npos; + }); + } + + [[nodiscard]] bool + IsPragmaOnce(std::string_view line) + { + return Trimmed(line) == "#pragma once"; + } + + /// A runner's include: `<Progmasoft/...>` or, inside one directory, + /// `"Name.hpp"`. + [[nodiscard]] std::optional<IncludedFile> + ParseRunnerInclude(std::string_view line) + { + auto include = ParseInclude(line); + if (!include) + { + return std::nullopt; + } + const bool found = include->Angled + ? StartsWith(include->Name, "Progmasoft/") + : EndsWith(include->Name, ".hpp") + && Consists(include->Name, "/."); + return found ? include : std::nullopt; + } + + /// The runner compiles yyjson and pugixml through these files. + [[nodiscard]] bool + IsWrapperInclude(std::string_view line) + { + const auto include = ParseInclude(line); + return include && !include->Angled + && EndsWith(include->Name, ".inl") + && Consists(include->Name, "./"); + } + + /// An include of yyjson and pugixml, which space them in many ways. + [[nodiscard]] std::optional<std::string> + ParseThirdPartyInclude(std::string_view line) + { + const auto skipSpace = [&line] { + const auto first = line.find_first_not_of(" \t"); + line.remove_prefix(first == std::string_view::npos ? line.size() + : first); + }; + skipSpace(); + if (!StartsWith(line, "#")) + { + return std::nullopt; + } + line.remove_prefix(1); + skipSpace(); + if (!StartsWith(line, "include")) + { + return std::nullopt; + } + line.remove_prefix(7); + const auto before = line.size(); + skipSpace(); + if (line.size() == before || line.empty() + || (line.front() != '<' && line.front() != '"')) + { + return std::nullopt; + } + line.remove_prefix(1); + const auto end = line.find_first_of(">\""); + if (end == std::string_view::npos || end == 0 + || !Consists(line.substr(0, end), ".")) + { + return std::nullopt; + } + return std::string{ line.substr(0, end) }; + } + + class Generator final + { + public: + Generator(const fs::path &repository, Problems &found) + : root(repository) + , source(repository / "src") + , thirdParty(repository / "third_party") + , problems(found) + , upstream(UpstreamFiles(repository, found)) + {} + + [[nodiscard]] std::string + Header() + { + std::string out = FileHeader(); + out += "#ifndef CATCH_AMALGAMATED_HPP_INCLUDED\n"; + out += "#define CATCH_AMALGAMATED_HPP_INCLUDED\n"; + Concatenate(out, source / "Progmasoft/Catch3/All.hpp", true); + // Mocks are Progmasoft's own, and tests use them through + // this header. + Concatenate(out, source / "Progmasoft/Catch3/Mock.hpp", true); + out += "#endif // CATCH_AMALGAMATED_HPP_INCLUDED\n"; + return out; + } + + [[nodiscard]] std::string + Source() + { + std::string out = FileHeader(); + out += "\n#include \"catch_amalgamated.hpp\"\n"; + Concatenate( + out, + source / "Progmasoft/Catch3/Internal/WindowsHProxy.hpp", + false); + // The runner's headers come first: Catch::Session reaches + // the runner. + auto headers = FilesBelow(root, + source / "Progmasoft/Runner", + { ".hpp" }, + false); + for (const char *name : { "Results.hpp", + "Runner.hpp", + "Version.hpp", + "XmlWriter.hpp" }) + { + headers.push_back(std::string{ "src/Progmasoft/Catch3/" } + + name); + } + for (const auto &header : headers) + { + const auto path = Normal(root / header); + if (runnerHeaders.insert(path).second) + { + ConcatenateRunner(out, path, true); + } + } + for (const auto &file : upstream) + { + if (EndsWith(file, ".cpp")) + { + Concatenate(out, root / file, false); + } + } + std::vector<std::string> files{ + "src/Progmasoft/Mock.cpp", + "src/Progmasoft/Results.cpp", + "src/Progmasoft/XmlWriter.cpp" + }; + for (auto &file : FilesBelow(root, + source / "Progmasoft/Runner", + { ".cpp" }, + false)) + { + files.push_back(std::move(file)); + } + for (const auto &file : files) + { + ConcatenateRunner(out, root / file, false); + } + return out; + } + + [[nodiscard]] std::string + ThirdParty() + { + std::string out + = "// Catch3 v" + Catch3Version() + + ": yyjson and pugixml for catch_amalgamated.cpp\n"; + out += kThirdPartyTail; + out += "#pragma once\n"; + std::set<std::string> done; + for (const char *wrapper : + { "Runner/Yyjson.inl", "PugiXml.inl" }) + { + const auto text + = ReadOrReport(source / "Progmasoft" / wrapper, + problems); + for (const auto &line : SplitLines(text)) + { + if (StartsWith(line, "// Copyright") + || StartsWith(line, "// SPDX-License-Identifier")) + { + continue; + } + const auto name = ParseThirdPartyInclude(line); + if (name && ThirdPartyFiles().count(*name) != 0) + { + ExpandThirdParty(out, *name, done); + continue; + } + out += line; + } + } + return out; + } + + private: + [[nodiscard]] static std::string + Normal(const fs::path &path) + { + return path.lexically_normal().generic_string(); + } + + [[nodiscard]] std::string + Catch3Version() + { + const auto text + = ReadOrReport(source / "Progmasoft/Catch3/Version.hpp", + problems); + constexpr std::string_view kKey = "kVersion = \""; + const auto start = text.find(kKey); + if (start == std::string::npos) + { + problems.emplace_back("no kVersion in Version.hpp"); + return {}; + } + const auto first = start + kKey.size(); + return text.substr(first, text.find('"', first) - first); + } + + [[nodiscard]] std::string + FileHeader() + { + const auto engine = ParseEngineVersion( + ReadOrReport(source / "Progmasoft/Catch3/EngineVersion.cpp", + problems)); + if (!engine) + { + problems.emplace_back("no version in EngineVersion.cpp"); + } + std::string out{ kHeaderHead }; + out += "// Catch3 v" + Catch3Version() + ", on the Catch2 v" + + engine.value_or("") + " engine\n"; + out += kHeaderTail; + return out; + } + + /// The engine header a line includes, or nothing. + [[nodiscard]] std::optional<std::string> + EngineInclude(std::string_view line) const + { + const auto include = ParseInclude(line); + if (!include || !include->Angled + || !StartsWith(include->Name, "Progmasoft/Catch3/")) + { + return std::nullopt; + } + if (upstream.count("src/" + include->Name) != 0 + || include->Name == kUserConfig) + { + return include->Name; + } + return std::nullopt; + } + + /// Copies a file, leaving out copyright lines; with + /// `expandHeaders`, engine headers are put in place of their + /// includes, once each. + void + Concatenate(std::string &out, + const fs::path &file, + bool expandHeaders) + { + const auto text = ReadOrReport(file, problems); + for (const auto &line : SplitLines(text)) + { + if (IsPragmaOnce(line) + || std::find(kCopyrightLines.begin(), + kCopyrightLines.end(), + line) + != kCopyrightLines.end()) + { + continue; + } + const auto engine = EngineInclude(line); + if (!engine) + { + // The runner's headers are expanded at the start of + // the .cpp file. + if (expandHeaders || !ParseRunnerInclude(line)) + { + out += line; + } + continue; + } + if (!expandHeaders + || !concatenatedHeaders.insert(*engine).second) + { + continue; + } + // The generated configuration does not exist yet, and + // leaving it out means the default one. + if (*engine != kUserConfig) + { + Concatenate(out, source / *engine, true); + } + } + } + + /// Copies a file of the runner, expanding the runner's headers it + /// includes once, or dropping those includes from .cpp files. + void + ConcatenateRunner(std::string &out, + const fs::path &file, + bool expandHeaders) + { + const auto directory = file.parent_path(); + const auto text = ReadOrReport(file, problems); + for (const auto &line : SplitLines(text)) + { + if (IsPragmaOnce(line) || EngineInclude(line)) + { + continue; + } + if (IsWrapperInclude(line)) + { + if (!thirdPartyIncluded) + { + out += "#include " + "\"catch_amalgamated_third_party.hpp\"\n"; + thirdPartyIncluded = true; + } + continue; + } + const auto include = ParseRunnerInclude(line); + if (!include) + { + out += line; + continue; + } + const auto header = Normal( + (include->Angled ? source : directory) / include->Name); + if (expandHeaders && runnerHeaders.insert(header).second) + { + ConcatenateRunner(out, header, true); + } + } + } + + void + ExpandThirdParty(std::string &out, + const std::string &name, + std::set<std::string> &done) + { + if (!done.insert(name).second) + { + return; + } + const auto text + = ReadOrReport(thirdParty / ThirdPartyFiles().at(name), + problems); + for (const auto &line : SplitLines(text)) + { + auto included = ParseThirdPartyInclude(line); + // pugixml includes its source through a macro in + // header-only mode. + if (Trimmed(line) == "#\tinclude PUGIXML_SOURCE") + { + included = "pugixml.cpp"; + } + if (included && ThirdPartyFiles().count(*included) != 0) + { + ExpandThirdParty(out, *included, done); + } + else + { + out += line; + } + } + } + + fs::path root; + fs::path source; + fs::path thirdParty; + Problems &problems; + std::set<std::string> upstream; + std::set<std::string> concatenatedHeaders; + std::set<std::string> runnerHeaders; + bool thirdPartyIncluded = false; + }; + + [[nodiscard]] std::array<std::pair<const char *, const std::string *>, + 3> + Parts(const Amalgamation &amalgamation) + { + return { { { "catch_amalgamated.hpp", &amalgamation.Header }, + { "catch_amalgamated.cpp", &amalgamation.Source }, + { "catch_amalgamated_third_party.hpp", + &amalgamation.ThirdParty } } }; + } + } // namespace + + std::optional<IncludedFile> + ParseInclude(std::string_view line) + { + const auto first = line.find_first_not_of(" \t"); + if (first == std::string_view::npos) + { + return std::nullopt; + } + line.remove_prefix(first); + constexpr std::string_view kInclude = "#include "; + if (!StartsWith(line, kInclude) || line.size() <= kInclude.size()) + { + return std::nullopt; + } + line.remove_prefix(kInclude.size()); + const char open = line.front(); + if (open != '<' && open != '"') + { + return std::nullopt; + } + line.remove_prefix(1); + const auto end = line.find(open == '<' ? '>' : '"'); + if (end == std::string_view::npos || end == 0) + { + return std::nullopt; + } + return IncludedFile{ std::string{ line.substr(0, end) }, open == '<' }; + } + + std::optional<std::string> + ParseEngineVersion(std::string_view source) + { + constexpr std::string_view kKey = "static Version version"; + const auto key = source.find(kKey); + if (key == std::string_view::npos) + { + return std::nullopt; + } + const auto open = source.find('(', key); + const auto close = source.find(')', key); + if (open == std::string_view::npos || close == std::string_view::npos) + { + return std::nullopt; + } + std::vector<std::string_view> parts; + auto rest = source.substr(open + 1, close - open - 1); + while (true) + { + const auto comma = rest.find(','); + parts.push_back(Trimmed(rest.substr(0, comma))); + if (comma == std::string_view::npos) + { + break; + } + rest.remove_prefix(comma + 1); + } + if (parts.size() != 5) + { + return std::nullopt; + } + std::string version{ parts[0] }; + version.append(".").append(parts[1]).append(".").append(parts[2]); + auto branch = parts[3]; + if (branch.size() > 2) + { + branch = branch.substr(1, branch.size() - 2); + version.append("-").append(branch).append(".").append(parts[4]); + } + return version; + } + + Amalgamation + GenerateAmalgamation(const std::filesystem::path &root, Problems &problems) + { + Generator generator{ root, problems }; + Amalgamation amalgamation; + amalgamation.Header = generator.Header(); + amalgamation.Source = generator.Source(); + amalgamation.ThirdParty = generator.ThirdParty(); + return amalgamation; + } + + void + CheckAmalgamation(const std::filesystem::path &root, + const Amalgamation &expected, + Problems &problems) + { + for (const auto &[name, text] : Parts(expected)) + { + if (ReadText(root / "extras" / name) != *text) + { + problems.push_back(std::string{ "extras/" } + name + + " is out of date: run bazelisk run " + "//tools:amalgamate"); + } + } + } + + void + WriteAmalgamation(const std::filesystem::path &root, + const Amalgamation &amalgamation, + Problems &problems) + { + for (const auto &[name, text] : Parts(amalgamation)) + { + if (!WriteText(root / "extras" / name, *text)) + { + problems.push_back(std::string{ "cannot write extras/" } + + name); + } + } + } +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/Amalgamation.hpp b/tools/Amalgamation.hpp new file mode 100644 index 00000000..262e56d9 --- /dev/null +++ b/tools/Amalgamation.hpp @@ -0,0 +1,54 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +#pragma once + +#include <filesystem> +#include <optional> +#include <string> +#include <string_view> + +#include "tools/Repository.hpp" + +namespace Progmasoft::Catch3::Tools +{ + /// The single-file distribution in extras/. + struct Amalgamation final + { + std::string Header; + std::string Source; + std::string ThirdParty; + }; + + /// A file `#include` names at the start of a line. + struct IncludedFile final + { + std::string Name; + bool Angled = false; + }; + + /// Reads `#include <name>` or `#include "name"`, written exactly so. + [[nodiscard]] std::optional<IncludedFile> + ParseInclude(std::string_view line); + + /// "3.16.1", or "3.17.0-preview.1", from the source of EngineVersion.cpp. + [[nodiscard]] std::optional<std::string> + ParseEngineVersion(std::string_view source); + + /// Builds the distribution from the sources; deterministic, so that it + /// can be compared with what is in extras/. + [[nodiscard]] Amalgamation + GenerateAmalgamation(const std::filesystem::path &root, Problems &problems); + + /// Adds a line to `problems` for each file of extras/ that `expected` + /// does not match. + void + CheckAmalgamation(const std::filesystem::path &root, + const Amalgamation &expected, + Problems &problems); + + /// Writes `amalgamation` to extras/. + void + WriteAmalgamation(const std::filesystem::path &root, + const Amalgamation &amalgamation, + Problems &problems); +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel new file mode 100644 index 00000000..e7bb4801 --- /dev/null +++ b/tools/BUILD.bazel @@ -0,0 +1,101 @@ +# Copyright (c) 2026 Progmasoft. +# SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") + +# The tools of this repository. They work on the checkout: +# +# bazelisk run //tools:<tool> +# bazelisk run //tools:<tool> -- <arguments> +CPP17 = select({ + "@platforms//os:windows": ["/std:c++17"], + "//conditions:default": ["-std=c++17"], +}) + +cc_library( + name = "library", + srcs = [ + "Amalgamation.cpp", + "Checks.cpp", + "Repository.cpp", + "RepositoryReport.cpp", + "Versions.cpp", + ], + hdrs = [ + "Amalgamation.hpp", + "Checks.hpp", + "Repository.hpp", + "RepositoryReport.hpp", + "Versions.hpp", + ], + copts = CPP17, + deps = ["//third_party/yyjson"], +) + +cc_binary( + name = "amalgamate", + srcs = ["AmalgamateMain.cpp"], + copts = CPP17, + deps = [":library"], +) + +cc_binary( + name = "check", + srcs = ["CheckMain.cpp"], + copts = CPP17, + deps = [":library"], +) + +cc_binary( + name = "release-notes", + srcs = ["ReleaseNotesMain.cpp"], + copts = CPP17, + deps = [":library"], +) + +cc_binary( + name = "repo-info", + srcs = ["RepoInfoMain.cpp"], + copts = CPP17, + deps = [":library"], +) + +cc_binary( + name = "version", + srcs = ["VersionMain.cpp"], + copts = CPP17, + deps = [":library"], +) + +cc_binary( + name = "third-party", + srcs = ["ThirdPartyMain.cpp"], + args = [ + "third_party/yyjson/yyjson.c=$(rlocationpath @yyjson_archive//:src/yyjson.c)", + "third_party/yyjson/yyjson.h=$(rlocationpath @yyjson_archive//:src/yyjson.h)", + "third_party/pugixml/pugiconfig.hpp=$(rlocationpath @pugixml_archive//:src/pugiconfig.hpp)", + "third_party/pugixml/pugixml.cpp=$(rlocationpath @pugixml_archive//:src/pugixml.cpp)", + "third_party/pugixml/pugixml.hpp=$(rlocationpath @pugixml_archive//:src/pugixml.hpp)", + ], + copts = CPP17, + data = [ + "@pugixml_archive//:src/pugiconfig.hpp", + "@pugixml_archive//:src/pugixml.cpp", + "@pugixml_archive//:src/pugixml.hpp", + "@yyjson_archive//:src/yyjson.c", + "@yyjson_archive//:src/yyjson.h", + ], + deps = [ + ":library", + "@rules_cc//cc/runfiles", + ], +) + +cc_test( + name = "tools_tests", + srcs = ["Tools.tests.cpp"], + copts = CPP17, + deps = [ + ":library", + "//:catch3_main", + ], +) diff --git a/tools/CheckMain.cpp b/tools/CheckMain.cpp new file mode 100644 index 00000000..ee37d868 --- /dev/null +++ b/tools/CheckMain.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +// bazelisk run //tools:check + +#include <iostream> + +#include "tools/Amalgamation.hpp" +#include "tools/Checks.hpp" +#include "tools/Repository.hpp" +#include "tools/Versions.hpp" + +int +main(int argc, char **) +{ + using namespace Progmasoft::Catch3::Tools; + + if (argc != 1) + { + std::cerr << "usage: bazelisk run //tools:check\n"; + return 2; + } + const auto root = RepositoryRoot(); + if (!root) + { + std::cerr << "run this tool with bazelisk run\n"; + return 2; + } + Problems problems; + CheckLicenses(*root, problems); + CheckConvenienceHeaders(*root, problems); + CheckPragmaOnce(*root, problems); + CheckDuplicateFilenames(*root, problems); + CheckVersions(*root, problems); + const auto amalgamation = GenerateAmalgamation(*root, problems); + if (problems.empty()) + { + CheckAmalgamation(*root, amalgamation, problems); + } + return Finish(problems); +} diff --git a/tools/Checks.cpp b/tools/Checks.cpp new file mode 100644 index 00000000..a348eff4 --- /dev/null +++ b/tools/Checks.cpp @@ -0,0 +1,254 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <algorithm> +#include <set> +#include <string> +#include <string_view> +#include <vector> + +#include "tools/Amalgamation.hpp" +#include "tools/Checks.hpp" + +namespace Progmasoft::Catch3::Tools +{ + namespace + { + namespace fs = std::filesystem; + + constexpr std::string_view kCatch2License = R"( +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 +)"; + + constexpr std::string_view kProgmasoftLicense + = R"(// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +)"; + + [[nodiscard]] bool + IsInternalFolder(std::string_view name) + { + return name == "Detail" || name == "Internal"; + } + + class ConvenienceHeaders final + { + public: + ConvenienceHeaders(const fs::path &repository, Problems &found) + : root(repository) + , problems(found) + { + for (const auto &file : UpstreamFiles(repository, found)) + { + if (EndsWith(file, ".hpp")) + { + headers.insert(file.substr(4)); + } + } + } + + void + Walk(const std::string &folder) + { + Verify(folder); + for (const auto &name : Subfolders(folder)) + { + if (!IsInternalFolder(name)) + { + Walk(folder + "/" + name); + } + } + } + + private: + [[nodiscard]] std::vector<std::string> + HeadersIn(const std::string &folder) const + { + const auto prefix = folder + "/"; + std::vector<std::string> found; + for (const auto &header : headers) + { + if (StartsWith(header, prefix) + && header.find('/', prefix.size()) == std::string::npos) + { + found.push_back(header); + } + } + return found; + } + + [[nodiscard]] std::set<std::string> + Subfolders(const std::string &folder) const + { + const auto prefix = folder + "/"; + std::set<std::string> names; + for (const auto &header : headers) + { + const auto slash = header.find('/', prefix.size()); + if (StartsWith(header, prefix) + && slash != std::string::npos) + { + names.insert(header.substr(prefix.size(), + slash - prefix.size())); + } + } + return names; + } + + [[nodiscard]] std::vector<std::string> + Wanted(const std::string &folder) const + { + auto wanted = HeadersIn(folder); + for (const auto &name : Subfolders(folder)) + { + const auto subfolder = folder + "/" + name; + if (IsInternalFolder(name)) + { + const auto inner = HeadersIn(subfolder); + wanted.insert(wanted.end(), inner.begin(), inner.end()); + } + else + { + wanted.push_back(subfolder + "/All.hpp"); + } + } + std::sort(wanted.begin(), wanted.end()); + return wanted; + } + + void + Verify(const std::string &folder) + { + const auto name = folder + "/All.hpp"; + if (headers.count(name) == 0) + { + problems.push_back("missing convenience header: " + name); + return; + } + std::vector<std::string> includes; + const auto text = ReadOrReport(root / "src" / name, problems); + for (const auto &line : SplitLines(text)) + { + const auto include = ParseInclude(line); + if (StartsWith(line, "#include") && include + && include->Angled + && StartsWith(include->Name, "Progmasoft/Catch3/") + && EndsWith(include->Name, ".hpp")) + { + includes.push_back(include->Name); + } + } + if (!std::is_sorted(includes.begin(), includes.end())) + { + problems.push_back(name + ": includes are not sorted"); + } + std::set<std::string> seen; + for (const auto &include : includes) + { + if (!seen.insert(include).second) + { + problems.push_back(name + ": duplicated include " + + include); + } + } + std::set<std::string> wanted; + for (auto &include : Wanted(folder)) + { + if (include != name) + { + wanted.insert(std::move(include)); + } + } + for (const auto &include : wanted) + { + if (seen.count(include) == 0 + && include + != "Progmasoft/Catch3/Internal/" + "WindowsHProxy.hpp") + { + problems.push_back(name + ": missing include " + + include); + } + } + for (const auto &include : seen) + { + if (wanted.count(include) == 0) + { + problems.push_back(name + ": superfluous include " + + include); + } + } + } + + fs::path root; + Problems &problems; + std::set<std::string> headers; + }; + } // namespace + + void + CheckLicenses(const fs::path &root, Problems &problems) + { + const auto upstream = UpstreamFiles(root, problems); + for (const char *directory : + { "src/Progmasoft", "tests", "examples", "fuzzing", "tools" }) + { + for (const auto &file : + FilesBelow(root, root / directory, { ".cpp", ".hpp" }, true)) + { + const bool progmasoft + = StartsWith(file, "tools/") + || (file.find("Progmasoft/") != std::string::npos + && upstream.count(file) == 0); + const auto expected + = progmasoft ? kProgmasoftLicense : kCatch2License; + if (!StartsWith(ReadOrReport(root / file, problems), expected)) + { + problems.push_back(file + " does not have its license"); + } + } + } + } + + void + CheckConvenienceHeaders(const fs::path &root, Problems &problems) + { + ConvenienceHeaders{ root, problems }.Walk("Progmasoft/Catch3"); + } + + void + CheckPragmaOnce(const fs::path &root, Problems &problems) + { + for (const auto &file : + FilesBelow(root, root / "src/Progmasoft", { ".hpp" }, true)) + { + const auto text = ReadOrReport(root / file, problems); + if (!StartsWith(text, "#pragma once\n") + && text.find("\n#pragma once\n") == std::string::npos) + { + problems.push_back(file + " has no #pragma once"); + } + } + } + + void + CheckDuplicateFilenames(const fs::path &root, Problems &problems) + { + std::set<std::string> names; + for (const auto &file : + FilesBelow(root, root / "src/Progmasoft", { ".cpp" }, true)) + { + const auto name = fs::path{ file }.filename().string(); + if (!names.insert(name).second) + { + problems.push_back(file + + " shares its name with another source"); + } + } + } +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/Checks.hpp b/tools/Checks.hpp new file mode 100644 index 00000000..16d698a0 --- /dev/null +++ b/tools/Checks.hpp @@ -0,0 +1,31 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +#pragma once + +#include <filesystem> + +#include "tools/Repository.hpp" + +namespace Progmasoft::Catch3::Tools +{ + /// Every source file starts with the license of its part: engine files + /// keep Catch2's, the rest carry Progmasoft's. + void + CheckLicenses(const std::filesystem::path &root, Problems &problems); + + /// Every `All.hpp` of the engine includes the engine headers of its folder + /// and nothing else, sorted and once each. + void + CheckConvenienceHeaders(const std::filesystem::path &root, + Problems &problems); + + /// Every header of the library has `#pragma once`. + void + CheckPragmaOnce(const std::filesystem::path &root, Problems &problems); + + /// No two sources of the library share a file name: build tools that keep + /// object files in one directory would compile one over the other. + void + CheckDuplicateFilenames(const std::filesystem::path &root, + Problems &problems); +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/ReleaseNotesMain.cpp b/tools/ReleaseNotesMain.cpp new file mode 100644 index 00000000..1eb67469 --- /dev/null +++ b/tools/ReleaseNotesMain.cpp @@ -0,0 +1,49 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +// bazelisk run //tools:release-notes -- [version] +// +// Prints a section of docs/release-notes.md, of the current version without +// an argument, for the text of a GitHub release. + +#include <iostream> +#include <string> + +#include "tools/Repository.hpp" +#include "tools/Versions.hpp" + +int +main(int argc, char **argv) +{ + using namespace Progmasoft::Catch3::Tools; + + if (argc > 2) + { + std::cerr << "usage: bazelisk run //tools:release-notes -- [version]\n"; + return 2; + } + const auto root = RepositoryRoot(); + if (!root) + { + std::cerr << "run this tool with bazelisk run\n"; + return 2; + } + Problems problems; + const auto version = argc == 2 ? std::optional<std::string>{ argv[1] } + : CurrentVersion(*root, problems); + const auto notes + = ReadOrReport(*root / "docs" / "release-notes.md", problems); + if (!version || !problems.empty()) + { + return Finish(problems); + } + const auto section = ReleaseNotesOf(notes, *version); + if (!section) + { + problems.push_back("no section for " + *version + + " in docs/release-notes.md"); + return Finish(problems); + } + std::cout << *section; + return 0; +} diff --git a/tools/RepoInfoMain.cpp b/tools/RepoInfoMain.cpp new file mode 100644 index 00000000..86ffb573 --- /dev/null +++ b/tools/RepoInfoMain.cpp @@ -0,0 +1,34 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +// bazelisk run //tools:repo-info [-- --json] +// +// Reports the checkout and the tools on PATH, without changing anything. + +#include <iostream> +#include <string_view> + +#include "tools/Repository.hpp" +#include "tools/RepositoryReport.hpp" + +int +main(int argc, char **argv) +{ + using namespace Progmasoft::Catch3::Tools; + + const bool json = argc == 2 && std::string_view{ argv[1] } == "--json"; + if (argc > 2 || (argc == 2 && !json)) + { + std::cerr << "usage: bazelisk run //tools:repo-info [-- --json]\n"; + return 2; + } + const auto root = RepositoryRoot(); + if (!root) + { + std::cerr << "run this tool with bazelisk run\n"; + return 2; + } + const auto report = CollectRepositoryReport(*root); + std::cout << (json ? ReportToJson(report) : FormatReport(report)); + return 0; +} diff --git a/tools/Repository.cpp b/tools/Repository.cpp new file mode 100644 index 00000000..0c08a36c --- /dev/null +++ b/tools/Repository.cpp @@ -0,0 +1,194 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <algorithm> +#include <cstdlib> +#include <fstream> +#include <iostream> +#include <iterator> +#include <system_error> + +#include "tools/Repository.hpp" + +namespace Progmasoft::Catch3::Tools +{ + std::optional<std::string> + ReadEnvironment(const char *name) + { +#if defined(_WIN32) + char *value = nullptr; + std::size_t length = 0; + if (_dupenv_s(&value, &length, name) != 0 || value == nullptr) + { + return std::nullopt; + } + std::string result{ value }; + std::free(value); + return result; +#else + const char *value = std::getenv(name); + return value == nullptr ? std::nullopt + : std::optional<std::string>{ value }; +#endif + } + + std::optional<std::filesystem::path> + RepositoryRoot() + { + const auto root = ReadEnvironment("BUILD_WORKSPACE_DIRECTORY"); + if (!root) + { + return std::nullopt; + } + return std::filesystem::path{ *root }; + } + + std::optional<std::string> + ReadText(const std::filesystem::path &file) + { + std::ifstream stream{ file, std::ios::binary }; + if (!stream) + { + return std::nullopt; + } + const std::string bytes{ std::istreambuf_iterator<char>{ stream }, + std::istreambuf_iterator<char>{} }; + std::string text; + text.reserve(bytes.size()); + for (std::size_t index = 0; index < bytes.size(); ++index) + { + if (bytes[index] != '\r' || index + 1 == bytes.size() + || bytes[index + 1] != '\n') + { + text.push_back(bytes[index]); + } + } + return text; + } + + std::string + ReadOrReport(const std::filesystem::path &file, Problems &problems) + { + auto text = ReadText(file); + if (!text) + { + problems.push_back("cannot read " + file.generic_string()); + return {}; + } + return *std::move(text); + } + + bool + WriteText(const std::filesystem::path &file, std::string_view text) + { + std::ofstream stream{ file, std::ios::binary | std::ios::trunc }; + stream.write(text.data(), static_cast<std::streamsize>(text.size())); + return static_cast<bool>(stream); + } + + std::vector<std::string> + SplitLines(std::string_view text) + { + std::vector<std::string> lines; + std::size_t start = 0; + while (start < text.size()) + { + const auto end = text.find('\n', start); + const auto next + = end == std::string_view::npos ? text.size() : end + 1; + lines.emplace_back(text.substr(start, next - start)); + start = next; + } + return lines; + } + + std::string_view + Trimmed(std::string_view text) + { + constexpr std::string_view kSpace = " \t\r\n"; + const auto first = text.find_first_not_of(kSpace); + if (first == std::string_view::npos) + { + return {}; + } + return text.substr(first, text.find_last_not_of(kSpace) - first + 1); + } + + bool + StartsWith(std::string_view text, std::string_view prefix) + { + return text.substr(0, prefix.size()) == prefix; + } + + bool + EndsWith(std::string_view text, std::string_view suffix) + { + return text.size() >= suffix.size() + && text.substr(text.size() - suffix.size()) == suffix; + } + + std::set<std::string> + UpstreamFiles(const std::filesystem::path &root, Problems &problems) + { + std::set<std::string> files; + const auto listing + = ReadOrReport(root / "tools" / "upstreamFiles.txt", problems); + for (const auto &line : SplitLines(listing)) + { + const auto entry = Trimmed(line); + if (!entry.empty() && entry.front() != '#') + { + files.emplace(entry); + } + } + return files; + } + + std::vector<std::string> + FilesBelow(const std::filesystem::path &root, + const std::filesystem::path &directory, + const std::vector<std::string> &extensions, + bool recursive) + { + namespace fs = std::filesystem; + std::vector<std::string> files; + const auto add = [&](const fs::directory_entry &entry) { + const auto extension = entry.path().extension().string(); + if (entry.is_regular_file() + && std::find(extensions.begin(), extensions.end(), extension) + != extensions.end()) + { + files.push_back( + entry.path().lexically_relative(root).generic_string()); + } + }; + std::error_code error; + if (recursive) + { + for (const auto &entry : + fs::recursive_directory_iterator{ directory, error }) + { + add(entry); + } + } + else + { + for (const auto &entry : fs::directory_iterator{ directory, error }) + { + add(entry); + } + } + std::sort(files.begin(), files.end()); + return files; + } + + int + Finish(const Problems &problems) + { + for (const auto &problem : problems) + { + std::cerr << problem << '\n'; + } + return problems.empty() ? 0 : 1; + } +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/Repository.hpp b/tools/Repository.hpp new file mode 100644 index 00000000..356dc6c8 --- /dev/null +++ b/tools/Repository.hpp @@ -0,0 +1,64 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +#pragma once + +#include <filesystem> +#include <optional> +#include <set> +#include <string> +#include <string_view> +#include <vector> + +namespace Progmasoft::Catch3::Tools +{ + /// What a tool found wrong, one line each. + using Problems = std::vector<std::string>; + + [[nodiscard]] std::optional<std::string> + ReadEnvironment(const char *name); + + /// The checkout `bazelisk run` was started in, if it was. + [[nodiscard]] std::optional<std::filesystem::path> + RepositoryRoot(); + + /// The text of a file with LF line ends, whatever the checkout uses. + [[nodiscard]] std::optional<std::string> + ReadText(const std::filesystem::path &file); + + /// Like `ReadText`, with a line in `problems` when it cannot be read. + [[nodiscard]] std::string + ReadOrReport(const std::filesystem::path &file, Problems &problems); + + [[nodiscard]] bool + WriteText(const std::filesystem::path &file, std::string_view text); + + /// The lines of `text`, each with its line end. + [[nodiscard]] std::vector<std::string> + SplitLines(std::string_view text); + + [[nodiscard]] std::string_view + Trimmed(std::string_view text); + + [[nodiscard]] bool + StartsWith(std::string_view text, std::string_view prefix); + + [[nodiscard]] bool + EndsWith(std::string_view text, std::string_view suffix); + + /// The files of src/Progmasoft that come from Catch2 and keep its + /// license, relative to the repository, from tools/upstreamFiles.txt. + [[nodiscard]] std::set<std::string> + UpstreamFiles(const std::filesystem::path &root, Problems &problems); + + /// The files with one of `extensions` below `directory`, sorted, as paths + /// relative to `root` with forward slashes. + [[nodiscard]] std::vector<std::string> + FilesBelow(const std::filesystem::path &root, + const std::filesystem::path &directory, + const std::vector<std::string> &extensions, + bool recursive); + + /// Writes `problems` to standard error; returns the exit code of a tool. + int + Finish(const Problems &problems); +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/RepositoryReport.cpp b/tools/RepositoryReport.cpp new file mode 100644 index 00000000..4b2b1502 --- /dev/null +++ b/tools/RepositoryReport.cpp @@ -0,0 +1,276 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <algorithm> +#include <array> +#include <cstdio> +#include <cstdlib> +#include <memory> +#include <optional> +#include <string_view> +#include <system_error> + +#include "tools/Amalgamation.hpp" +#include "tools/Repository.hpp" +#include "tools/RepositoryReport.hpp" +#include "tools/Versions.hpp" + +// yyjson, compiled here and nowhere else in the tools, like in the runner. +#define yyjson_api static +#include <yyjson.c> + +namespace Progmasoft::Catch3::Tools +{ + namespace + { + namespace fs = std::filesystem; + +#if defined(_WIN32) + constexpr char kPathSeparator = ';'; + constexpr std::array<std::string_view, 4> kExtensions{ ".exe", + ".cmd", + ".bat", + ".com" }; + constexpr std::string_view kNoErrors = " 2>nul"; +#else + constexpr char kPathSeparator = ':'; + constexpr std::array<std::string_view, 1> kExtensions{ "" }; + constexpr std::string_view kNoErrors = " 2>/dev/null"; +#endif + + constexpr std::array<std::string_view, 11> kTools{ + "git", "bazelisk", "cmake", "ninja", + "meson", "xmake", "premake5", "clang", + "clang-cl", "clang-format", "gcc", + }; + + constexpr std::string_view + OperatingSystem() + { +#if defined(_WIN32) + return "windows"; +#elif defined(__APPLE__) + return "darwin"; +#elif defined(__linux__) + return "linux"; +#else + return "unknown"; +#endif + } + + constexpr std::string_view + Architecture() + { +#if defined(_M_X64) || defined(__x86_64__) + return "amd64"; +#elif defined(_M_ARM64) || defined(__aarch64__) + return "arm64"; +#else + return "unknown"; +#endif + } + + [[nodiscard]] ToolInfo + Find(std::string_view name, std::string_view searchPath) + { + ToolInfo tool; + tool.Name = name; + while (!tool.Available && !searchPath.empty()) + { + const auto end = searchPath.find(kPathSeparator); + const fs::path directory{ std::string{ + searchPath.substr(0, end) } }; + searchPath.remove_prefix(end == std::string_view::npos + ? searchPath.size() + : end + 1); + if (directory.empty()) + { + continue; + } + for (const auto extension : kExtensions) + { + const auto file + = directory + / (std::string{ name } + std::string{ extension }); + std::error_code error; + if (fs::is_regular_file(file, error)) + { + tool.Path = file.string(); + tool.Available = true; + break; + } + } + } + return tool; + } + + /// What `git <arguments>` writes in `root`, or nothing when it fails. + [[nodiscard]] std::optional<std::string> + Git(const fs::path &root, std::string_view arguments) + { + const auto command = "git -C \"" + root.string() + "\" " + + std::string{ arguments } + + std::string{ kNoErrors }; +#if defined(_WIN32) + auto *pipe = _popen(command.c_str(), "r"); +#else + auto *pipe = popen(command.c_str(), "r"); +#endif + if (pipe == nullptr) + { + return std::nullopt; + } + std::string output; + std::array<char, 512> buffer{}; + std::size_t count = 0; + while ((count = std::fread(buffer.data(), 1, buffer.size(), pipe)) + != 0) + { + output.append(buffer.data(), count); + } +#if defined(_WIN32) + const int status = _pclose(pipe); +#else + const int status = pclose(pipe); +#endif + if (status != 0) + { + return std::nullopt; + } + return std::string{ Trimmed(output) }; + } + + void + AddText(yyjson_mut_doc *document, + yyjson_mut_val *object, + const char *key, + const std::string &value) + { + if (!value.empty()) + { + yyjson_mut_obj_add_strn(document, + object, + key, + value.data(), + value.size()); + } + } + + struct FreeDocument final + { + void + operator()(yyjson_mut_doc *document) const + { + yyjson_mut_doc_free(document); + } + }; + + struct FreeText final + { + void + operator()(char *text) const + { + std::free(text); + } + }; + } // namespace + + RepositoryReport + CollectRepositoryReport(const fs::path &root) + { + RepositoryReport report; + report.Root = root.string(); + report.Os = OperatingSystem(); + report.Architecture = Architecture(); + + Problems ignored; + report.Version = CurrentVersion(root, ignored).value_or(""); + report.EngineVersion + = ParseEngineVersion( + ReadText(root / "src/Progmasoft/Catch3/EngineVersion.cpp") + .value_or("")) + .value_or(""); + + const auto searchPath = ReadEnvironment("PATH").value_or(""); + for (const auto name : kTools) + { + report.Tools.push_back(Find(name, searchPath)); + } + + const auto branch = Git(root, "branch --show-current"); + const auto revision = Git(root, "rev-parse HEAD"); + const auto status + = Git(root, "status --porcelain --untracked-files=no"); + if (branch && revision && status) + { + report.Branch = *branch; + report.Revision = *revision; + report.Dirty = !status->empty(); + } + else + { + report.GitError = "Git checkout metadata could not be read"; + } + return report; + } + + std::string + FormatReport(const RepositoryReport &report) + { + std::string text = "Checkout: " + report.Root + "\n"; + text += "Host: " + report.Os + "/" + report.Architecture + "\n"; + text += "Catch3: " + report.Version + ", on the Catch2 " + + report.EngineVersion + " engine\n"; + text += "Branch: " + report.Branch + "\n"; + text += "Revision: " + report.Revision + "\n"; + text += std::string{ "Tracked changes: " } + + (report.Dirty ? "true" : "false") + "\n"; + if (!report.GitError.empty()) + { + text += report.GitError + "\n"; + } + for (const auto &tool : report.Tools) + { + auto name = tool.Name; + name.resize(std::max<std::size_t>(name.size(), 13), ' '); + text += name + (tool.Available ? tool.Path : "not found") + "\n"; + } + return text; + } + + std::string + ReportToJson(const RepositoryReport &report) + { + const std::unique_ptr<yyjson_mut_doc, FreeDocument> holder{ + yyjson_mut_doc_new(nullptr) + }; + auto *document = holder.get(); + auto *root = yyjson_mut_obj(document); + yyjson_mut_doc_set_root(document, root); + AddText(document, root, "root", report.Root); + AddText(document, root, "os", report.Os); + AddText(document, root, "architecture", report.Architecture); + AddText(document, root, "version", report.Version); + AddText(document, root, "engineVersion", report.EngineVersion); + AddText(document, root, "branch", report.Branch); + AddText(document, root, "revision", report.Revision); + yyjson_mut_obj_add_bool(document, root, "dirty", report.Dirty); + AddText(document, root, "gitError", report.GitError); + auto *tools = yyjson_mut_obj_add_arr(document, root, "tools"); + for (const auto &tool : report.Tools) + { + auto *entry = yyjson_mut_arr_add_obj(document, tools); + AddText(document, entry, "name", tool.Name); + AddText(document, entry, "path", tool.Path); + yyjson_mut_obj_add_bool(document, + entry, + "available", + tool.Available); + } + const std::unique_ptr<char, FreeText> text{ + yyjson_mut_write(document, YYJSON_WRITE_PRETTY_TWO_SPACES, nullptr) + }; + return text == nullptr ? std::string{} + : std::string{ text.get() } + "\n"; + } +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/RepositoryReport.hpp b/tools/RepositoryReport.hpp new file mode 100644 index 00000000..83ef85b7 --- /dev/null +++ b/tools/RepositoryReport.hpp @@ -0,0 +1,47 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +#pragma once + +#include <filesystem> +#include <string> +#include <vector> + +namespace Progmasoft::Catch3::Tools +{ + /// A program the checkout builds with, and where PATH finds it. + struct ToolInfo final + { + std::string Name; + std::string Path; + bool Available = false; + }; + + /// The state of a checkout and of the machine around it. It holds no + /// environment, remote URL or credential. + struct RepositoryReport final + { + std::string Root; + std::string Os; + std::string Architecture; + std::string Version; + std::string EngineVersion; + std::string Branch; + std::string Revision; + bool Dirty = false; + std::string GitError; + std::vector<ToolInfo> Tools; + }; + + /// Reads the checkout at `root`, without changing anything. + [[nodiscard]] RepositoryReport + CollectRepositoryReport(const std::filesystem::path &root); + + /// The report as lines for a person. + [[nodiscard]] std::string + FormatReport(const RepositoryReport &report); + + /// The report as JSON: empty texts are left out, the rest has the names + /// of Tala's repo-info, in camelCase. + [[nodiscard]] std::string + ReportToJson(const RepositoryReport &report); +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/ThirdPartyMain.cpp b/tools/ThirdPartyMain.cpp new file mode 100644 index 00000000..86c1d6c6 --- /dev/null +++ b/tools/ThirdPartyMain.cpp @@ -0,0 +1,89 @@ +// 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 <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; + for (int index = 1; index < argc; ++index) + { + const std::string_view argument{ argv[index] }; + if (argument == "--check" || argument == "--update") + { + mode = argument; + } + else if (argument.find('=') != std::string_view::npos) + { + files.push_back(argument); + } + else + { + mode = {}; + break; + } + } + if (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; + for (const auto file : files) + { + const auto separator = file.find('='); + const auto vendored = file.substr(0, separator); + const auto archived = ReadOrReport( + runfiles->Rlocation(std::string{ file.substr(separator + 1) }), + problems); + if (mode == "--check") + { + if (ReadText(*root / vendored) != archived) + { + problems.push_back(std::string{ vendored } + + " differs from its pinned archive: run " + "bazelisk run //tools:third-party -- " + "--update"); + } + } + else if (!WriteText(*root / vendored, archived)) + { + problems.push_back("cannot write " + std::string{ vendored }); + } + } + return Finish(problems); +} diff --git a/tools/Tools.tests.cpp b/tools/Tools.tests.cpp new file mode 100644 index 00000000..51dca951 --- /dev/null +++ b/tools/Tools.tests.cpp @@ -0,0 +1,116 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <Progmasoft/Catch3/TestMacros.hpp> +#include <string> + +#include "tools/Amalgamation.hpp" +#include "tools/Repository.hpp" +#include "tools/RepositoryReport.hpp" +#include "tools/Versions.hpp" + +using namespace Progmasoft::Catch3::Tools; + +TEST_CASE("Versions have three or four numbers", "[tools]") +{ + CHECK(ParseVersion("0.3.0") == Version{ 0, 3, 0, 0 }); + CHECK(ParseVersion("0.3.0.2") == Version{ 0, 3, 0, 2 }); + for (const char *wrong : + { "0.3", "0.3.0.2.1", "01.3.0", "0.3.0.", "a.3.0", "", "0.3.0-rc" }) + { + INFO(wrong); + CHECK_FALSE(ParseVersion(wrong)); + } +} + +TEST_CASE("The next version counts releases", "[tools]") +{ + const auto next = [](const char *version) { + return FormatVersion(NextVersion(*ParseVersion(version))); + }; + CHECK(next("0.3.0") == "0.3.0.1"); + CHECK(next("0.4.0.8") == "0.4.0.9"); + CHECK(next("0.4.0.9") == "0.4.1.0"); + CHECK(next("0.4.9.9") == "0.5.0.0"); + CHECK(next("0.19.9.9") == "1.0.0.0"); +} + +TEST_CASE("A version site is read and written in place", "[tools]") +{ + const VersionSite site{ "file", "version: '", "'" }; + const std::string text = "name\n version: '1.2.3',\n version: '9'\n"; + CHECK(ReadVersionSite(text, site) == "1.2.3"); + CHECK(WriteVersionSite(text, site, "1.2.3.4") + == "name\n version: '1.2.3.4',\n version: '9'\n"); + CHECK_FALSE(ReadVersionSite("name", site)); + CHECK_FALSE(WriteVersionSite("name", site, "1")); +} + +TEST_CASE("Release notes are found by their heading", "[tools]") +{ + const std::string notes = "# Release notes\n\n## 0.2\n\nNew.\n\n" + "### Part\n\n* One.\n\n\n## 0.1\n\nOld.\n"; + CHECK(ReleaseNotesOf(notes, "0.2") == "New.\n\n### Part\n\n* One.\n"); + CHECK(ReleaseNotesOf(notes, "0.1") == "Old.\n"); + CHECK_FALSE(ReleaseNotesOf(notes, "0.3")); +} + +TEST_CASE("Includes are read as they are written", "[tools]") +{ + const auto angled = ParseInclude(" #include <a/b.hpp> // why\n"); + REQUIRE(angled); + CHECK(angled->Name == "a/b.hpp"); + CHECK(angled->Angled); + const auto quoted = ParseInclude("#include \"b.hpp\"\n"); + REQUIRE(quoted); + CHECK_FALSE(quoted->Angled); + CHECK_FALSE(ParseInclude("# include <a.hpp>\n")); + CHECK_FALSE(ParseInclude("// #include <a.hpp>\n")); + CHECK_FALSE(ParseInclude("#include MACRO\n")); +} + +TEST_CASE("The engine version comes from its declaration", "[tools]") +{ + CHECK(ParseEngineVersion(" static Version version( 3, 16, 1, \"\", 0 );") + == "3.16.1"); + CHECK(ParseEngineVersion("static Version version( 3, 17, 0, \"rc\", 2 );") + == "3.17.0-rc.2"); + CHECK_FALSE(ParseEngineVersion("nothing here")); +} + +TEST_CASE("A report becomes JSON", "[tools]") +{ + RepositoryReport report; + report.Root = "C:\\repos\\catch3"; + report.Os = "windows"; + report.Branch = "devel"; + report.Dirty = true; + report.Tools.push_back({ "git", "/usr/bin/git", true }); + report.Tools.push_back({ "ninja", "", false }); + CHECK(ReportToJson(report) == R"({ + "root": "C:\\repos\\catch3", + "os": "windows", + "branch": "devel", + "dirty": true, + "tools": [ + { + "name": "git", + "path": "/usr/bin/git", + "available": true + }, + { + "name": "ninja", + "available": false + } + ] +} +)"); +} + +TEST_CASE("Text helpers keep line ends", "[tools]") +{ + CHECK(SplitLines("a\nb") == std::vector<std::string>{ "a\n", "b" }); + CHECK(SplitLines("").empty()); + CHECK(Trimmed(" a b\r\n") == "a b"); + CHECK(Trimmed(" \n").empty()); +} diff --git a/tools/VersionMain.cpp b/tools/VersionMain.cpp new file mode 100644 index 00000000..a7fb123a --- /dev/null +++ b/tools/VersionMain.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +// bazelisk run //tools:version prints the current version +// bazelisk run //tools:version -- --next moves to the next version +// bazelisk run //tools:version -- 0.3.1.0 moves to that version +// +// Moving writes every place that states the version, and the amalgamation. + +#include <iostream> +#include <string> +#include <string_view> + +#include "tools/Amalgamation.hpp" +#include "tools/Repository.hpp" +#include "tools/Versions.hpp" + +int +main(int argc, char **argv) +{ + using namespace Progmasoft::Catch3::Tools; + + if (argc > 2) + { + std::cerr + << "usage: bazelisk run //tools:version [-- --next|<version>]\n"; + return 2; + } + const auto root = RepositoryRoot(); + if (!root) + { + std::cerr << "run this tool with bazelisk run\n"; + return 2; + } + Problems problems; + const auto current = CurrentVersion(*root, problems); + if (!current) + { + return Finish(problems); + } + if (argc == 1) + { + std::cout << *current << '\n'; + return 0; + } + const std::string_view argument{ argv[1] }; + const auto parsed = ParseVersion( + argument == "--next" ? *current : std::string{ argument }); + if (!parsed) + { + std::cerr << "not a version of three or four numbers: " << argument + << '\n'; + return 2; + } + const auto version + = FormatVersion(argument == "--next" ? NextVersion(*parsed) : *parsed); + SetVersion(*root, version, problems); + if (problems.empty()) + { + WriteAmalgamation(*root, + GenerateAmalgamation(*root, problems), + problems); + } + if (problems.empty()) + { + std::cout << *current << " -> " << version << '\n'; + } + return Finish(problems); +} diff --git a/tools/Versions.cpp b/tools/Versions.cpp new file mode 100644 index 00000000..73a2d663 --- /dev/null +++ b/tools/Versions.cpp @@ -0,0 +1,205 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <algorithm> +#include <cctype> +#include <charconv> + +#include "tools/Versions.hpp" + +namespace Progmasoft::Catch3::Tools +{ + namespace + { + constexpr std::string_view kVersionHeader + = "src/Progmasoft/Catch3/Version.hpp"; + } // namespace + + std::optional<Version> + ParseVersion(std::string_view text) + { + Version version{}; + std::size_t count = 0; + while (true) + { + const auto dot = text.find('.'); + const auto number = text.substr(0, dot); + if (count == version.size() || number.empty() + || (number.size() > 1 && number.front() == '0')) + { + return std::nullopt; + } + const auto *end = number.data() + number.size(); + const auto parsed + = std::from_chars(number.data(), end, version[count]); + if (parsed.ec != std::errc{} || parsed.ptr != end) + { + return std::nullopt; + } + ++count; + if (dot == std::string_view::npos) + { + break; + } + text.remove_prefix(dot + 1); + } + if (count < 3) + { + return std::nullopt; + } + return version; + } + + std::string + FormatVersion(const Version &version) + { + return std::to_string(version[0]) + "." + std::to_string(version[1]) + + "." + std::to_string(version[2]) + "." + + std::to_string(version[3]); + } + + Version + NextVersion(Version version) + { + if (version[3] < 9) + { + ++version[3]; + return version; + } + version[3] = 0; + if (version[2] < 9) + { + ++version[2]; + return version; + } + version[2] = 0; + if (version[1] < 19) + { + ++version[1]; + return version; + } + version[1] = 0; + ++version[0]; + return version; + } + + const std::array<VersionSite, 5> & + VersionSites() + { + static constexpr std::array<VersionSite, 5> kSites{ { + { "CMakeLists.txt", "\n VERSION ", "\n" }, + { "MODULE.bazel", "module(name = \"catch3\", version = \"", "\"" }, + { "meson.build", "\n version: '", "'" }, + { "xmake.lua", "\nset_version(\"", "\"" }, + { kVersionHeader, "kVersion = \"", "\"" }, + } }; + return kSites; + } + + std::optional<std::string> + ReadVersionSite(std::string_view text, const VersionSite &site) + { + const auto before = text.find(site.Before); + if (before == std::string_view::npos) + { + return std::nullopt; + } + const auto first = before + site.Before.size(); + const auto end = text.find(site.After, first); + if (end == std::string_view::npos) + { + return std::nullopt; + } + return std::string{ text.substr(first, end - first) }; + } + + std::optional<std::string> + WriteVersionSite(std::string_view text, + const VersionSite &site, + std::string_view version) + { + const auto current = ReadVersionSite(text, site); + if (!current) + { + return std::nullopt; + } + const auto first = text.find(site.Before) + site.Before.size(); + std::string result{ text.substr(0, first) }; + result += version; + result += text.substr(first + current->size()); + return result; + } + + std::optional<std::string> + CurrentVersion(const std::filesystem::path &root, Problems &problems) + { + const auto &sites = VersionSites(); + const auto &site + = *std::find_if(sites.begin(), + sites.end(), + [](const VersionSite &candidate) { + return candidate.File == kVersionHeader; + }); + auto version + = ReadVersionSite(ReadOrReport(root / site.File, problems), site); + if (!version) + { + problems.emplace_back("no version in Version.hpp"); + } + return version; + } + + void + CheckVersions(const std::filesystem::path &root, Problems &problems) + { + const auto current = CurrentVersion(root, problems); + if (!current) + { + return; + } + for (const auto &site : VersionSites()) + { + const auto version + = ReadVersionSite(ReadOrReport(root / site.File, problems), + site); + if (version != current) + { + problems.push_back(std::string{ site.File } + " states " + + version.value_or("no version") + + ", Version.hpp states " + *current); + } + } + } + + void + SetVersion(const std::filesystem::path &root, + const std::string &version, + Problems &problems) + { + for (const auto &site : VersionSites()) + { + const auto path = root / site.File; + const auto text + = WriteVersionSite(ReadOrReport(path, problems), site, version); + if (!text || !WriteText(path, *text)) + { + problems.push_back("cannot set the version in " + + std::string{ site.File }); + } + } + } + + std::optional<std::string> + ReleaseNotesOf(std::string_view notes, std::string_view version) + { + const auto heading = "\n## " + std::string{ version } + "\n"; + const auto found = notes.find(heading); + if (found == std::string_view::npos) + { + return std::nullopt; + } + auto section = notes.substr(found + heading.size()); + section = section.substr(0, section.find("\n## ")); + return std::string{ Trimmed(section) } + "\n"; + } +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/Versions.hpp b/tools/Versions.hpp new file mode 100644 index 00000000..3a601a50 --- /dev/null +++ b/tools/Versions.hpp @@ -0,0 +1,67 @@ +// Copyright (c) 2026 Progmasoft. +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 +#pragma once + +#include <array> +#include <filesystem> +#include <optional> +#include <string> +#include <string_view> + +#include "tools/Repository.hpp" + +namespace Progmasoft::Catch3::Tools +{ + /// A.B.C.D. A version of three numbers has D = 0. + using Version = std::array<unsigned, 4>; + + [[nodiscard]] std::optional<Version> + ParseVersion(std::string_view text); + + [[nodiscard]] std::string + FormatVersion(const Version &version); + + /// The version after `version`, which counts releases: D up to 9, then C + /// up to 9, then B up to 19, then A. + [[nodiscard]] Version + NextVersion(Version version); + + /// A place that states the version: the file, and the text around it. + struct VersionSite final + { + std::string_view File; + std::string_view Before; + std::string_view After; + }; + + [[nodiscard]] const std::array<VersionSite, 5> & + VersionSites(); + + /// The version `text`, a file of `site`, states. + [[nodiscard]] std::optional<std::string> + ReadVersionSite(std::string_view text, const VersionSite &site); + + /// `text` with `version` in the place of `site`. + [[nodiscard]] std::optional<std::string> + WriteVersionSite(std::string_view text, + const VersionSite &site, + std::string_view version); + + /// The version in Version.hpp, the one the other places have to follow. + [[nodiscard]] std::optional<std::string> + CurrentVersion(const std::filesystem::path &root, Problems &problems); + + /// Every place states the same version. + void + CheckVersions(const std::filesystem::path &root, Problems &problems); + + /// Writes `version` to every place. + void + SetVersion(const std::filesystem::path &root, + const std::string &version, + Problems &problems); + + /// The text of the section of `version` in release-notes.md, or nothing. + [[nodiscard]] std::optional<std::string> + ReleaseNotesOf(std::string_view notes, std::string_view version); +} // namespace Progmasoft::Catch3::Tools diff --git a/tools/scripts/buildAndTest.cmd b/tools/scripts/buildAndTest.cmd deleted file mode 100644 index fa359124..00000000 --- a/tools/scripts/buildAndTest.cmd +++ /dev/null @@ -1,16 +0,0 @@ -rem Start at the root of the Catch project directory, for example: -rem cd Catch2 - -rem begin-snippet: catch2-build-and-test-win -rem 1. Regenerate the amalgamated distribution -python tools\scripts\generateAmalgamatedFiles.py - -rem 2. Configure the full test build -cmake -B debug-build -S . -DCMAKE_BUILD_TYPE=Debug --preset all-tests - -rem 3. Run the actual build -cmake --build debug-build - -rem 4. Run the tests using CTest -ctest -j 4 --output-on-failure -C Debug --test-dir debug-build -rem end-snippet diff --git a/tools/scripts/buildAndTest.sh b/tools/scripts/buildAndTest.sh deleted file mode 100755 index 0383c97e..00000000 --- a/tools/scripts/buildAndTest.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env sh - -# Start at the root of the Catch project directory, for example: -# cd Catch2 - -# begin-snippet: catch2-build-and-test -# 1. Regenerate the amalgamated distribution -./tools/scripts/generateAmalgamatedFiles.py - -# 2. Configure the full test build -cmake -B debug-build -S . -DCMAKE_BUILD_TYPE=Debug --preset all-tests - -# 3. Run the actual build -cmake --build debug-build - -# 4. Run the tests using CTest -ctest -j 4 --output-on-failure -C Debug --test-dir debug-build -# end-snippet diff --git a/tools/scripts/checkConvenienceHeaders.py b/tools/scripts/checkConvenienceHeaders.py deleted file mode 100755 index 6a8b7bc3..00000000 --- a/tools/scripts/checkConvenienceHeaders.py +++ /dev/null @@ -1,141 +0,0 @@ -#!/usr/bin/env python3 - -""" -Checks that all of the "All.hpp" headers of the engine include all subheaders. - -The logic is simple: given a folder, e.g. `Progmasoft/Catch3/Matchers`, the -corresponding header is `Progmasoft/Catch3/Matchers/All.hpp` and contains -* all engine headers in `Progmasoft/Catch3/Matchers`, -* all engine headers in `Progmasoft/Catch3/Matchers/{Internal, Detail}`, -* the `All.hpp` headers of its other subfolders that hold engine headers. - -The top level header is `Progmasoft/Catch3/All.hpp`. Engine headers are the -ones listed in upstreamFiles.txt; Progmasoft's own headers in the same -folders are not part of the convenience headers. -""" - -internal_dirs = ['Detail', 'Internal'] - -from scriptCommon import catchPath, upstreamFiles -import os -import re -import sys - -source_root = 'src/Progmasoft/Catch3' -include_parser = re.compile(r'#include <(Progmasoft/Catch3/.+\.hpp)>') -engine_headers = {path[len('src/'):] for path in upstreamFiles() if path.endswith('.hpp')} - -errors_found = False - - -def engine_headers_in(folder): - """Engine headers directly in folder, as include paths.""" - prefix = folder + '/' - return sorted(h for h in engine_headers - if h.startswith(prefix) and '/' not in h[len(prefix):]) - - -def subfolders_with_engine_headers(folder): - prefix = folder + '/' - names = set() - for header in engine_headers: - if header.startswith(prefix): - rest = header[len(prefix):] - if '/' in rest: - names.add(rest.split('/')[0]) - return sorted(names) - - -def collated_includes(folder): - base = engine_headers_in(folder) - for name in subfolders_with_engine_headers(folder): - subfolder = folder + '/' + name - if name in internal_dirs: - base.extend(engine_headers_in(subfolder)) - else: - base.append(subfolder + '/All.hpp') - return sorted(base) - - -def includes_from_file(header): - includes = [] - with open(os.path.join(catchPath, 'src', header), 'r', encoding='utf-8') as file: - for line in file: - if not line.startswith('#include'): - continue - match = include_parser.match(line) - if match: - includes.append(match.group(1)) - return includes - - -def get_duplicates(xs): - seen = set() - duplicated = [] - for x in xs: - if x in seen: - duplicated.append(x) - seen.add(x) - return duplicated - - -def verify_convenience_header(folder): - """ - Checks the convenience header of one folder: - 1) The header exists - 2) Its includes are sorted - 3) It has no duplicated includes - 4) It includes every header it should - 5) It includes nothing else - """ - global errors_found - header_name = folder + '/All.hpp' - - # 1) Does it exist? - if header_name not in engine_headers: - errors_found = True - print('Missing convenience header: {}'.format(header_name)) - return - file_incs = includes_from_file(header_name) - # 2) Are the includes sorted? - if sorted(file_incs) != file_incs: - errors_found = True - print("'{}': Includes are not in sorted order!".format(header_name)) - - # 3) Are there no duplicates? - for duplicate in get_duplicates(file_incs): - errors_found = True - print("'{}': Duplicated include: '{}'".format(header_name, duplicate)) - - # The convenience header does not include itself. - target_includes = [x for x in collated_includes(folder) if x != header_name] - # 4) Are all required headers present? - file_incs_set = set(file_incs) - for include in target_includes: - if (include not in file_incs_set and - include != 'Progmasoft/Catch3/Internal/WindowsHProxy.hpp'): - errors_found = True - print("'{}': missing include '{}'".format(header_name, include)) - - # 5) Are there any superfluous headers? - desired_set = set(target_includes) - for include in file_incs: - if include not in desired_set: - errors_found = True - print("'{}': superfluous include '{}'".format(header_name, include)) - - -def walk_source_folders(folder): - verify_convenience_header(folder) - for name in subfolders_with_engine_headers(folder): - if name not in internal_dirs: - walk_source_folders(folder + '/' + name) - - -walk_source_folders('Progmasoft/Catch3') - -# Propagate error "code" upwards -if not errors_found: - print('Everything ok') -else: - sys.exit(1) diff --git a/tools/scripts/checkDuplicateFilenames.py b/tools/scripts/checkDuplicateFilenames.py deleted file mode 100755 index 0a68d504..00000000 --- a/tools/scripts/checkDuplicateFilenames.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright Catch2 Authors -# Distributed under the Boost Software License, Version 1.0. -# (See accompanying file LICENSE.txt or copy at -# https://www.boost.org/LICENSE_1_0.txt) - -# SPDX-License-Identifier: BSL-1.0 - -"""Checks that no two source files of the library share a file name. - -Build tools that keep object files in one directory, such as Visual Studio -projects, would otherwise compile one over the other. Headers may share -names: they are always included with their directory. -""" - -import os -import sys - -files_set = set() - -for root, dir, files in os.walk("src/Progmasoft"): - for file in files: - if not file.endswith('.cpp'): - continue - if file not in files_set: - files_set.add(file) - else: - print("File %s is duplicate" % file) - sys.exit(1) diff --git a/tools/scripts/checkLicense.py b/tools/scripts/checkLicense.py deleted file mode 100755 index 8b70b390..00000000 --- a/tools/scripts/checkLicense.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 - -import sys -import glob -from pathlib import Path - -from scriptCommon import upstreamFiles - -correct_licence = """\ - -// Copyright Catch2 Authors -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) - -// SPDX-License-Identifier: BSL-1.0 -""" - -progmasoft_licence = """\ -// Copyright (c) 2026 Progmasoft. -// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 -""" - -upstream_files = upstreamFiles() - -def check_licence_in_file(filename: str) -> bool: - # Files that come from Catch2 keep BSL-1.0; the fork's own source and - # tests carry the MPL header with Progmasoft's exception. - relative = filename.replace('\\', '/') - progmasoft = ('Progmasoft' in Path(filename).parts - and relative not in upstream_files) - expected = progmasoft_licence if progmasoft else correct_licence - with open(filename, 'r', encoding='utf-8') as f: - file_preamble = f.read(len(expected)) - - if expected != file_preamble: - print('File {} does not have proper licence'.format(filename)) - return False - return True - -def check_licences_in_path(path: str) -> int: - failed = 0 - files_to_check = glob.glob(path + '/**/*.cpp', recursive=True) \ - + glob.glob(path + '/**/*.hpp', recursive=True) - for file in files_to_check: - if not check_licence_in_file(file): - failed += 1 - return failed - -def check_licences(): - failed = 0 - # Add 'extras' after the amalgamated files are regenerated with the new script (past 3.4.0) - roots = ['src/Progmasoft', 'tests', 'examples', 'fuzzing'] - for root in roots: - failed += check_licences_in_path(root) - - if failed: - print('{} files are missing licence'.format(failed)) - sys.exit(1) - -if __name__ == "__main__": - check_licences() diff --git a/tools/scripts/fixWhitespace.py b/tools/scripts/fixWhitespace.py deleted file mode 100755 index 5840e790..00000000 --- a/tools/scripts/fixWhitespace.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python3 - -import os -from scriptCommon import catchPath - -def isSourceFile( path ): - return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" ) - -def fixAllFilesInDir( dir ): - changedFiles = 0 - for f in os.listdir( dir ): - path = os.path.join( dir,f ) - if os.path.isfile( path ): - if isSourceFile( path ): - if fixFile( path ): - changedFiles += 1 - else: - fixAllFilesInDir( path ) - return changedFiles - -def fixFile( path ): - f = open( path, 'r' ) - lines = [] - changed = 0 - for line in f: - trimmed = line.rstrip() + "\n" - trimmed = trimmed.replace('\t', ' ') - if trimmed != line: - changed = changed +1 - lines.append( trimmed ) - f.close() - if changed > 0: - global changedFiles - changedFiles = changedFiles + 1 - print( path + ":" ) - print( " - fixed " + str(changed) + " line(s)" ) - altPath = path + ".backup" - os.rename( path, altPath ) - f2 = open( path, 'w' ) - for line in lines: - f2.write( line ) - f2.close() - os.remove( altPath ) - return True - return False - -changedFiles = fixAllFilesInDir(catchPath) -if changedFiles > 0: - print( "Fixed " + str(changedFiles) + " file(s)" ) -else: - print( "No trailing whitespace found" ) diff --git a/tools/scripts/generateAmalgamatedFiles.py b/tools/scripts/generateAmalgamatedFiles.py deleted file mode 100755 index dcf78080..00000000 --- a/tools/scripts/generateAmalgamatedFiles.py +++ /dev/null @@ -1,298 +0,0 @@ -#!/usr/bin/env python3 -# Copyright Catch2 Authors -# Distributed under the Boost Software License, Version 1.0. -# (See accompanying file LICENSE.txt or copy at -# https://www.boost.org/LICENSE_1_0.txt) -# SPDX-License-Identifier: BSL-1.0 - -import os -import re -import datetime - -from scriptCommon import catchPath, upstreamFiles -from releaseCommon import Version - -root_path = os.path.join(catchPath, 'src') -starting_header = os.path.join(root_path, 'Progmasoft', 'Catch3', 'All.hpp') -output_header = os.path.join(catchPath, 'extras', 'catch_amalgamated.hpp') -output_cpp = os.path.join(catchPath, 'extras', 'catch_amalgamated.cpp') -output_third_party = os.path.join(catchPath, 'extras', 'catch_amalgamated_third_party.hpp') -third_party_path = os.path.join(catchPath, 'third_party') - -# REUSE-IgnoreStart - -# These are the copyright comments in each file, we want to ignore them -copyright_lines = [ -'// Copyright Catch2 Authors\n', -'// Distributed under the Boost Software License, Version 1.0.\n', -'// (See accompanying file LICENSE.txt or copy at\n', -'// https://www.boost.org/LICENSE_1_0.txt)\n', -'// SPDX-License-Identifier: BSL-1.0\n', -] - -# The header of the amalgamated file: copyright information + explanation -# what this file is. -file_header = '''\ - -// Copyright Catch2 Authors -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) - -// SPDX-License-Identifier: BSL-1.0 - -// Catch3 v{catch3_version}, on the Catch2 v{version_string} engine -// Generated: {generation_time} -// ---------------------------------------------------------- -// This file is an amalgamation of multiple different files. -// You probably shouldn't edit it directly. -// -// The parts that come from src/Progmasoft, the Catch3 runner, keep -// their own copyright and license lines (MPL-2.0 WITH -// AdditionRef-Progmasoft-Exception-2.0). The runner needs C++17. -// ---------------------------------------------------------- -''' - -# The header of the file with yyjson and pugixml. -third_party_header = '''\ -// Catch3 v{catch3_version}: yyjson and pugixml for catch_amalgamated.cpp -// Generated: {generation_time} -// ---------------------------------------------------------- -// This file is an amalgamation of src/Progmasoft/Runner/Yyjson.inl, -// src/Progmasoft/PugiXml.inl and the libraries they include from -// third_party/. You probably shouldn't edit it directly. -// -// yyjson and pugixml keep their MIT licenses, stated in their text below; -// the rest is MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0. Only -// catch_amalgamated.cpp includes this file. -// ---------------------------------------------------------- -''' - -# REUSE-IgnoreEnd - -# Returns file header with proper version string and generation time -def catch3_version(): - version_file = os.path.join(root_path, 'Progmasoft', 'Catch3', 'Version.hpp') - with open(version_file, mode='r', encoding='utf-8') as input: - match = re.search(r'kVersion = "([^"]+)"', input.read()) - if not match: - raise RuntimeError('no kVersion in ' + version_file) - return match.group(1) - -def formatted_file_header(version): - return file_header.format(version_string=version.getVersionString(), - catch3_version=catch3_version(), - generation_time=datetime.datetime.now()) - -# Which headers were already concatenated (and thus should not be -# processed again) -concatenated_headers = set() - -# The engine's files are the ones that come from Catch2. -upstream_files = upstreamFiles() -include_parser = re.compile(r'\s*#include <(Progmasoft/Catch3/[^>]+)>.*') - - -def engine_include(line): - """The engine header a line includes, or None.""" - m = include_parser.match(line) - # The configuration header is generated from UserConfig.hpp.in. - if m and ('src/' + m.group(1) in upstream_files - or m.group(1) == 'Progmasoft/Catch3/UserConfig.hpp'): - return m.group(1) - return None - -def concatenate_file(out, filename: str, expand_headers: bool) -> int: - # Gathers statistics on how many headers were expanded - concatenated = 1 - with open(filename, mode='r', encoding='utf-8') as input: - for line in input: - if line in copyright_lines or line.strip() == '#pragma once': - continue - m = engine_include(line) - # The runner's headers are expanded at the start of the .cpp file. - if not m and not expand_headers and progmasoft_include_parser.match(line): - continue - # anything that isn't a Catch2 header can just be copied to - # the resulting file - if not m: - out.write(line) - continue - - # TBD: We can also strip out include guards from our own - # headers, but it wasn't worth the time at the time of writing - # this script. - - # We do not want to expand headers for the cpp file - # amalgamation but neither do we want to copy them to output - if not expand_headers: - continue - - next_header = m - # We have to avoid re-expanding the same header over and - # over again, or the header will end up with couple - # hundred thousands lines (~300k as of preview3 :-) ) - if next_header in concatenated_headers: - continue - - # Skip including the auto-generated user config file, - # because it has not been generated yet at this point. - # The code around it should be written so that just not including - # it is equivalent with all-default user configuration. - if next_header == 'Progmasoft/Catch3/UserConfig.hpp': - concatenated_headers.add(next_header) - continue - - concatenated_headers.add(next_header) - concatenated += concatenate_file(out, os.path.join(root_path, next_header), expand_headers) - - return concatenated - - -def generate_header(): - with open(output_header, mode='w', encoding='utf-8') as header: - header.write(formatted_file_header(Version())) - header.write('#ifndef CATCH_AMALGAMATED_HPP_INCLUDED\n') - header.write('#define CATCH_AMALGAMATED_HPP_INCLUDED\n') - print('Concatenated {} headers'.format(concatenate_file(header, starting_header, True))) - # Mocks are Progmasoft's own, and tests use them through this header. - concatenate_file(header, os.path.join(root_path, 'Progmasoft', 'Catch3', 'Mock.hpp'), True) - header.write('#endif // CATCH_AMALGAMATED_HPP_INCLUDED\n') - -# The runner's files include each other as <Progmasoft/...> or, inside one -# directory, as "Name.hpp". -progmasoft_include_parser = re.compile(r'\s*#include (?:<(Progmasoft/[^>]+)>|"([A-Za-z/]+\.hpp)").*') -concatenated_runner_headers = set() - -# The runner compiles yyjson and pugixml into its own code through these -# files; in the amalgamation they are one header, included where the first -# of them was. -third_party_wrappers = ['Runner/Yyjson.inl', 'PugiXml.inl'] -wrapper_include_parser = re.compile(r'\s*#include "([A-Za-z./]+\.inl)".*') -third_party_files = { - 'yyjson.c': 'yyjson/yyjson.c', - 'yyjson.h': 'yyjson/yyjson.h', - 'pugixml.cpp': 'pugixml/pugixml.cpp', - 'pugixml.hpp': 'pugixml/pugixml.hpp', - 'pugiconfig.hpp': 'pugixml/pugiconfig.hpp', -} -third_party_include_parser = re.compile(r'\s*#\s*include\s+[<"]([A-Za-z.]+)[>"].*') -third_party_included = False - - -def concatenate_runner_file(out, filename: str, expand_headers: bool): - """Copies a file of the runner, expanding the runner's headers it - includes once, or dropping those includes from the .cpp files.""" - directory = os.path.dirname(filename) - with open(filename, mode='r', encoding='utf-8') as input: - for line in input: - if line.strip() == '#pragma once': - continue - # The engine's headers are all in the amalgamated header. - if engine_include(line): - continue - if wrapper_include_parser.match(line): - global third_party_included - if not third_party_included: - out.write('#include "catch_amalgamated_third_party.hpp"\n') - third_party_included = True - continue - m = progmasoft_include_parser.match(line) - if not m: - out.write(line) - continue - if m.group(1): - header = os.path.normpath(os.path.join(root_path, m.group(1))) - else: - header = os.path.normpath(os.path.join(directory, m.group(2))) - if not expand_headers or header in concatenated_runner_headers: - continue - concatenated_runner_headers.add(header) - concatenate_runner_file(out, header, True) - - -def concatenate_third_party_file(out, filename: str, done: set): - """Copies a file of yyjson or pugixml, with the files of the two - libraries it includes expanded once.""" - with open(filename, mode='r', encoding='utf-8') as input: - for line in input: - m = third_party_include_parser.match(line) - name = m.group(1) if m else None - # pugixml includes its source through a macro in header-only mode. - if line.strip() == '#\tinclude PUGIXML_SOURCE': - name = 'pugixml.cpp' - if name not in third_party_files: - out.write(line) - continue - if name not in done: - done.add(name) - concatenate_third_party_file( - out, os.path.join(third_party_path, third_party_files[name]), done) - - -def generate_third_party(): - with open(output_third_party, mode='w', encoding='utf-8', newline='\n') as header: - header.write(third_party_header.format(catch3_version=catch3_version(), - generation_time=datetime.datetime.now())) - header.write('#pragma once\n') - done = set() - for wrapper in third_party_wrappers: - path = os.path.join(root_path, 'Progmasoft', wrapper) - with open(path, mode='r', encoding='utf-8') as input: - for line in input: - if line.startswith(('// Copyright', '// SPDX-License-Identifier')): - continue - m = third_party_include_parser.match(line) - if m and m.group(1) in third_party_files: - name = m.group(1) - if name not in done: - done.add(name) - concatenate_third_party_file( - header, os.path.join(third_party_path, third_party_files[name]), done) - continue - header.write(line) - print('Wrote {}'.format(os.path.relpath(output_third_party, catchPath))) - - -def generate_cpp(): - from glob import glob - cpp_files = sorted(os.path.join(catchPath, path) for path in upstream_files - if path.endswith('.cpp')) - runner_headers = sorted(glob(os.path.join(root_path, 'Progmasoft', 'Runner', '*.hpp'))) + [ - os.path.join(root_path, 'Progmasoft', 'Catch3', name) - for name in ('Results.hpp', 'Runner.hpp', 'Version.hpp', 'XmlWriter.hpp')] - runner_files = [os.path.join(root_path, 'Progmasoft', 'Mock.cpp'), - os.path.join(root_path, 'Progmasoft', 'Results.cpp'), - os.path.join(root_path, 'Progmasoft', 'XmlWriter.cpp')] + \ - sorted(glob(os.path.join(root_path, 'Progmasoft', 'Runner', '*.cpp'))) - with open(output_cpp, mode='w', encoding='utf-8') as cpp: - cpp.write(formatted_file_header(Version())) - cpp.write('\n#include "catch_amalgamated.hpp"\n') - concatenate_file(cpp, os.path.join(root_path, 'Progmasoft/Catch3/Internal/WindowsHProxy.hpp'), False) - # The runner's headers come first: Catch::Session reaches the runner. - for header in runner_headers: - header = os.path.normpath(header) - if header not in concatenated_runner_headers: - concatenated_runner_headers.add(header) - concatenate_runner_file(cpp, header, True) - for file in cpp_files: - concatenate_file(cpp, file, False) - for file in runner_files: - concatenate_runner_file(cpp, file, False) - print('Concatenated {} cpp files'.format(len(cpp_files) + len(runner_files))) - -if __name__ == "__main__": - generate_header() - generate_cpp() - generate_third_party() - - -# Notes: -# * For .cpp files, internal includes have to be stripped and rewritten -# * for .hpp files, internal includes have to be resolved and included -# * The .cpp file needs to start with `#include "catch_amalgamated.hpp" -# * include guards can be left/stripped, doesn't matter -# * *.cpp files should be included sorted, to minimize diffs between versions -# * *.hpp files should also be somehow sorted -> use Progmasoft/Catch3/All.hpp as the -# * entrypoint -# * allow disabling main in the .cpp amalgamation diff --git a/tools/scripts/releaseCommon.py b/tools/scripts/releaseCommon.py deleted file mode 100644 index 42c57baf..00000000 --- a/tools/scripts/releaseCommon.py +++ /dev/null @@ -1,29 +0,0 @@ -import os -import re - -from scriptCommon import catchPath - -versionParser = re.compile( r'(\s*static\sVersion\sversion)\s*\(\s*(.*)\s*,\s*(.*)\s*,\s*(.*)\s*,\s*\"(.*)\"\s*,\s*(.*)\s*\).*' ) -rootPath = os.path.join( catchPath, 'src/Progmasoft/Catch3' ) -versionPath = os.path.join( rootPath, "EngineVersion.cpp" ) - -class Version: - """The version of the Catch2 compatibility engine, from Progmasoft/Catch3/EngineVersion.cpp.""" - def __init__(self): - f = open( versionPath, 'r' ) - for line in f: - m = versionParser.match( line ) - if m: - self.variableDecl = m.group(1) - self.majorVersion = int(m.group(2)) - self.minorVersion = int(m.group(3)) - self.patchNumber = int(m.group(4)) - self.branchName = m.group(5) - self.buildNumber = int(m.group(6)) - f.close() - - def getVersionString(self): - versionString = '{0}.{1}.{2}'.format( self.majorVersion, self.minorVersion, self.patchNumber ) - if self.branchName != "": - versionString = versionString + '-{0}.{1}'.format( self.branchName, self.buildNumber ) - return versionString diff --git a/tools/scripts/scriptCommon.py b/tools/scripts/scriptCommon.py deleted file mode 100644 index f64ef01d..00000000 --- a/tools/scripts/scriptCommon.py +++ /dev/null @@ -1,11 +0,0 @@ -import os -import sys - -catchPath = os.path.dirname(os.path.dirname(os.path.realpath( os.path.dirname(sys.argv[0])))) - - -def upstreamFiles(): - """The files below src/Progmasoft that come from Catch2, relative to the - repository with forward slashes; see upstreamFiles.txt.""" - with open(os.path.join(catchPath, 'tools', 'scripts', 'upstreamFiles.txt'), encoding='utf-8') as listing: - return {line.strip() for line in listing if line.strip() and not line.startswith('#')} diff --git a/tools/scripts/updateDocumentSnippets.py b/tools/scripts/updateDocumentSnippets.py deleted file mode 100755 index a070eea8..00000000 --- a/tools/scripts/updateDocumentSnippets.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 - -from scriptCommon import catchPath -import os -import subprocess - -# --------------------------------------------------- -# Update code examples -# --------------------------------------------------- -# For info on mdsnippets, see https://github.com/SimonCropp/MarkdownSnippets - -# install dotnet SDK from http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409 -# Then install MarkdownSnippets.Tool with -# dotnet tool install -g MarkdownSnippets.Tool -# To update: -# dotnet tool update -g MarkdownSnippets.Tool -# To uninstall (e.g. to downgrade to a lower version) -# dotnet tool uninstall -g MarkdownSnippets.Tool - -os.chdir(catchPath) - -subprocess.run('dotnet tool update -g MarkdownSnippets.Tool --version 21.2.0', shell=True, check=True) -subprocess.run('mdsnippets', shell=True, check=True) diff --git a/tools/scripts/updateDocumentToC.py b/tools/scripts/updateDocumentToC.py deleted file mode 100755 index 1840cecc..00000000 --- a/tools/scripts/updateDocumentToC.py +++ /dev/null @@ -1,447 +0,0 @@ -#!/usr/bin/env python3 - -# -# updateDocumentToC.py -# -# Insert table of contents at top of Catch markdown documents. -# -# This script is distributed under the GNU General Public License v3.0 -# -# It is based on markdown-toclify version 1.7.1 by Sebastian Raschka, -# https://github.com/rasbt/markdown-toclify -# - -import argparse -import glob -import os -import re -import sys - -from scriptCommon import catchPath - -# Configuration: - -minTocEntries = 4 - -headingExcludeDefault = [1,3,4,5] # use level 2 headers for at default -headingExcludeRelease = [1,3,4,5] # use level 1 headers for release-notes.md - -documentsDefault = os.path.join(os.path.relpath(catchPath), 'docs/*.md') -releaseNotesName = 'release-notes.md' - -contentTitle = '**Contents**' -contentLineNo = 4 -contentLineNdx = contentLineNo - 1 - -# End configuration - -VALIDS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-&' - -def readLines(in_file): - """Returns a list of lines from a input markdown file.""" - - with open(in_file, 'r') as inf: - in_contents = inf.read().split('\n') - return in_contents - -def removeLines(lines, remove=('[[back to top]', '<a class="mk-toclify"')): - """Removes existing [back to top] links and <a id> tags.""" - - if not remove: - return lines[:] - - out = [] - for l in lines: - if l.startswith(remove): - continue - out.append(l) - return out - -def removeToC(lines): - """Removes existing table of contents starting at index contentLineNdx.""" - if not lines[contentLineNdx ].startswith(contentTitle): - return lines[:] - - result_top = lines[:contentLineNdx] - - pos = contentLineNdx + 1 - while lines[pos].startswith('['): - pos = pos + 1 - - result_bottom = lines[pos + 1:] - - return result_top + result_bottom - -def dashifyHeadline(line): - """ - Takes a header line from a Markdown document and - returns a tuple of the - '#'-stripped version of the head line, - a string version for <a id=''></a> anchor tags, - and the level of the headline as integer. - E.g., - >>> dashifyHeadline('### some header lvl3') - ('Some header lvl3', 'some-header-lvl3', 3) - - """ - stripped_right = line.rstrip('#') - stripped_both = stripped_right.lstrip('#') - level = len(stripped_right) - len(stripped_both) - stripped_wspace = stripped_both.strip() - - # GitHub's sluggification works in an interesting way - # 1) '+', '/', '(', ')' and so on are just removed - # 2) spaces are converted into '-' directly - # 3) multiple -- are not collapsed - - dashified = '' - for c in stripped_wspace: - if c in VALIDS: - dashified += c.lower() - elif c.isspace(): - dashified += '-' - else: - # Unknown symbols are just removed - continue - - return [stripped_wspace, dashified, level] - -def tagAndCollect(lines, id_tag=True, back_links=False, exclude_h=None): - """ - Gets headlines from the markdown document and creates anchor tags. - - Keyword arguments: - lines: a list of sublists where every sublist - represents a line from a Markdown document. - id_tag: if true, creates inserts a the <a id> tags (not req. by GitHub) - back_links: if true, adds "back to top" links below each headline - exclude_h: header levels to exclude. E.g., [2, 3] - excludes level 2 and 3 headings. - - Returns a tuple of 2 lists: - 1st list: - A modified version of the input list where - <a id="some-header"></a> anchor tags where inserted - above the header lines (if github is False). - - 2nd list: - A list of 3-value sublists, where the first value - represents the heading, the second value the string - that was inserted assigned to the IDs in the anchor tags, - and the third value is an integer that represents the headline level. - E.g., - [['some header lvl3', 'some-header-lvl3', 3], ...] - - """ - out_contents = [] - headlines = [] - for l in lines: - saw_headline = False - - orig_len = len(l) - l_stripped = l.lstrip() - - if l_stripped.startswith(('# ', '## ', '### ', '#### ', '##### ', '###### ')): - - # comply with new markdown standards - - # not a headline if '#' not followed by whitespace '##no-header': - if not l.lstrip('#').startswith(' '): - continue - # not a headline if more than 6 '#': - if len(l) - len(l.lstrip('#')) > 6: - continue - # headers can be indented by at most 3 spaces: - if orig_len - len(l_stripped) > 3: - continue - - # ignore empty headers - if not set(l) - {'#', ' '}: - continue - - saw_headline = True - dashified = dashifyHeadline(l) - - if not exclude_h or not dashified[-1] in exclude_h: - if id_tag: - id_tag = '<a class="mk-toclify" id="%s"></a>'\ - % (dashified[1]) - out_contents.append(id_tag) - headlines.append(dashified) - - out_contents.append(l) - if back_links and saw_headline: - out_contents.append('[[back to top](#table-of-contents)]') - return out_contents, headlines - -def positioningHeadlines(headlines): - """ - Strips unnecessary whitespaces/tabs if first header is not left-aligned - """ - left_just = False - for row in headlines: - if row[-1] == 1: - left_just = True - break - if not left_just: - for row in headlines: - row[-1] -= 1 - return headlines - -def createToc(headlines, hyperlink=True, top_link=False, no_toc_header=False): - """ - Creates the table of contents from the headline list - that was returned by the tagAndCollect function. - - Keyword Arguments: - headlines: list of lists - e.g., ['Some header lvl3', 'some-header-lvl3', 3] - hyperlink: Creates hyperlinks in Markdown format if True, - e.g., '- [Some header lvl1](#some-header-lvl1)' - top_link: if True, add a id tag for linking the table - of contents itself (for the back-to-top-links) - no_toc_header: suppresses TOC header if True. - - Returns a list of headlines for a table of contents - in Markdown format, - e.g., [' - [Some header lvl3](#some-header-lvl3)', ...] - - """ - processed = [] - if not no_toc_header: - if top_link: - processed.append('<a class="mk-toclify" id="table-of-contents"></a>\n') - processed.append(contentTitle + '<br>') - - for line in headlines: - if hyperlink: - item = '[%s](#%s)' % (line[0], line[1]) - else: - item = '%s- %s' % ((line[2]-1)*' ', line[0]) - processed.append(item + '<br>') - processed.append('\n') - return processed - -def buildMarkdown(toc_headlines, body, spacer=0, placeholder=None): - """ - Returns a string with the Markdown output contents incl. - the table of contents. - - Keyword arguments: - toc_headlines: lines for the table of contents - as created by the createToc function. - body: contents of the Markdown file including - ID-anchor tags as returned by the - tagAndCollect function. - spacer: Adds vertical space after the table - of contents. Height in pixels. - placeholder: If a placeholder string is provided, the placeholder - will be replaced by the TOC instead of inserting the TOC at - the top of the document - - """ - if spacer: - spacer_line = ['\n<div style="height:%spx;"></div>\n' % (spacer)] - toc_markdown = "\n".join(toc_headlines + spacer_line) - else: - toc_markdown = "\n".join(toc_headlines) - - if placeholder: - body_markdown = "\n".join(body) - markdown = body_markdown.replace(placeholder, toc_markdown) - else: - body_markdown_p1 = "\n".join(body[:contentLineNdx ]) + '\n' - body_markdown_p2 = "\n".join(body[ contentLineNdx:]) - markdown = body_markdown_p1 + toc_markdown + body_markdown_p2 - - return markdown - -def outputMarkdown(markdown_cont, output_file): - """ - Writes to an output file if `outfile` is a valid path. - - """ - if output_file: - with open(output_file, 'w') as out: - out.write(markdown_cont) - -def markdownToclify( - input_file, - output_file=None, - min_toc_len=2, - github=False, - back_to_top=False, - nolink=False, - no_toc_header=False, - spacer=0, - placeholder=None, - exclude_h=None): - """ Function to add table of contents to markdown files. - - Parameters - ----------- - input_file: str - Path to the markdown input file. - - output_file: str (default: None) - Path to the markdown output file. - - min_toc_len: int (default: 2) - Minimum number of entries to create a table of contents for. - - github: bool (default: False) - Uses GitHub TOC syntax if True. - - back_to_top: bool (default: False) - Inserts back-to-top links below headings if True. - - nolink: bool (default: False) - Creates the table of contents without internal links if True. - - no_toc_header: bool (default: False) - Suppresses the Table of Contents header if True - - spacer: int (default: 0) - Inserts horizontal space (in pixels) after the table of contents. - - placeholder: str (default: None) - Inserts the TOC at the placeholder string instead - of inserting the TOC at the top of the document. - - exclude_h: list (default None) - Excludes header levels, e.g., if [2, 3], ignores header - levels 2 and 3 in the TOC. - - Returns - ----------- - changed: Boolean - True if the file has been updated, False otherwise. - - """ - cleaned_contents = removeLines( - removeToC(readLines(input_file)), - remove=('[[back to top]', '<a class="mk-toclify"')) - - processed_contents, raw_headlines = tagAndCollect( - cleaned_contents, - id_tag=not github, - back_links=back_to_top, - exclude_h=exclude_h) - - # add table of contents? - if len(raw_headlines) < min_toc_len: - processed_headlines = [] - else: - leftjustified_headlines = positioningHeadlines(raw_headlines) - - processed_headlines = createToc( - leftjustified_headlines, - hyperlink=not nolink, - top_link=not nolink and not github, - no_toc_header=no_toc_header) - - if nolink: - processed_contents = cleaned_contents - - cont = buildMarkdown( - toc_headlines=processed_headlines, - body=processed_contents, - spacer=spacer, - placeholder=placeholder) - - if output_file: - outputMarkdown(cont, output_file) - -def isReleaseNotes(f): - return os.path.basename(f) == releaseNotesName - -def excludeHeadingsFor(f): - return headingExcludeRelease if isReleaseNotes(f) else headingExcludeDefault - -def updateSingleDocumentToC(input_file, min_toc_len, verbose=False): - """Add or update table of contents in specified file. Return 1 if file changed, 0 otherwise.""" - if verbose : - print( 'file: {}'.format(input_file)) - - output_file = input_file + '.tmp' - - markdownToclify( - input_file=input_file, - output_file=output_file, - min_toc_len=min_toc_len, - github=True, - back_to_top=False, - nolink=False, - no_toc_header=False, - spacer=False, - placeholder=False, - exclude_h=excludeHeadingsFor(input_file)) - - # prevent race-condition (Python 3.3): - if sys.version_info >= (3, 3): - os.replace(output_file, input_file) - else: - os.remove(input_file) - os.rename(output_file, input_file) - - return 1 - -def updateDocumentToC(paths, min_toc_len, verbose): - """Add or update table of contents to specified paths. Return number of changed files""" - n = 0 - for g in paths: - for f in glob.glob(g): - if os.path.isfile(f): - n = n + updateSingleDocumentToC(input_file=f, min_toc_len=min_toc_len, verbose=verbose) - return n - -def updateDocumentToCMain(): - """Add or update table of contents to specified paths.""" - - parser = argparse.ArgumentParser( - description='Add or update table of contents in markdown documents.', - epilog="""""", - formatter_class=argparse.RawTextHelpFormatter) - - parser.add_argument( - 'Input', - metavar='file', - type=str, - nargs=argparse.REMAINDER, - help='files to process, at default: docs/*.md') - - parser.add_argument( - '-v', '--verbose', - action='store_true', - help='report the name of the file being processed') - - parser.add_argument( - '--min-toc-entries', - dest='minTocEntries', - default=minTocEntries, - type=int, - metavar='N', - help='the minimum number of entries to create a table of contents for [{default}]'.format(default=minTocEntries)) - - parser.add_argument( - '--remove-toc', - action='store_const', - dest='minTocEntries', - const=99, - help='remove all tables of contents') - - args = parser.parse_args() - - paths = args.Input if args.Input else [documentsDefault] - - changedFiles = updateDocumentToC(paths=paths, min_toc_len=args.minTocEntries, verbose=args.verbose) - - if changedFiles > 0: - print( "Processed table of contents in " + str(changedFiles) + " file(s)" ) - else: - print( "No table of contents added or updated" ) - -if __name__ == '__main__': - updateDocumentToCMain() - -# end of file diff --git a/tools/scripts/updateThirdParty.py b/tools/scripts/updateThirdParty.py deleted file mode 100755 index c51e642b..00000000 --- a/tools/scripts/updateThirdParty.py +++ /dev/null @@ -1,103 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2026 Progmasoft. -# SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 - -"""Keeps third_party/ equal to the pinned release archives of its libraries. - - updateThirdParty.py --check fails when a vendored file differs - updateThirdParty.py --update writes the files of the pinned archives - -The table below is the only record of the versions. Renovate proposes a new -version by changing "version"; whoever takes it sets "sha256" to the digest -of the new archive, after reading the release, and runs --update. -""" - -import argparse -import hashlib -import io -import sys -import tarfile -import urllib.request -from pathlib import Path - -from scriptCommon import catchPath - -LIBRARIES = { - "yyjson": { - # renovate: datasource=github-tags depName=ibireme/yyjson - "version": "0.13.0", - "url": "https://github.com/ibireme/yyjson/archive/refs/tags/{version}.tar.gz", - "sha256": "34e0f62a2bc11ab20d601e8ca1cc2b2079503aa45119a19133d89d19b94a0fae", - "files": {"src/yyjson.c": "yyjson.c", "src/yyjson.h": "yyjson.h"}, - }, - "pugixml": { - # renovate: datasource=github-tags depName=zeux/pugixml extractVersion=^v(?<version>.+)$ - "version": "1.16", - "url": "https://github.com/zeux/pugixml/archive/refs/tags/v{version}.tar.gz", - "sha256": "357bcab8877dc9943f355d3a72daba1b053238ba955f50fa81586afb65090219", - "files": { - "src/pugiconfig.hpp": "pugiconfig.hpp", - "src/pugixml.cpp": "pugixml.cpp", - "src/pugixml.hpp": "pugixml.hpp", - }, - }, -} - - -def archive_files(name: str, library: dict) -> dict: - """The files of the pinned archive, after its digest is checked.""" - url = library["url"].format(version=library["version"]) - with urllib.request.urlopen(url, timeout=60) as response: - data = response.read() - digest = hashlib.sha256(data).hexdigest() - if digest != library["sha256"]: - sys.exit(f"{name}: {url} has sha256 {digest}, " - f"the table pins {library['sha256']}") - files = {} - with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive: - for member in archive.getmembers(): - # The archive has one top directory, named after the release. - inner = member.name.split("/", 1)[-1] - if member.isfile() and inner in library["files"]: - extracted = archive.extractfile(member) - assert extracted is not None - files[library["files"][inner]] = extracted.read() - missing = set(library["files"].values()) - set(files) - if missing: - sys.exit(f"{name}: the archive has no {', '.join(sorted(missing))}") - return files - - -def normalized(data: bytes) -> bytes: - # Git may check the files out with CRLF on Windows. - return data.replace(b"\r\n", b"\n") - - -def main() -> int: - parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) - mode = parser.add_mutually_exclusive_group(required=True) - mode.add_argument("--check", action="store_true", - help="fail when a vendored file differs from its archive") - mode.add_argument("--update", action="store_true", - help="write the files of the pinned archives") - arguments = parser.parse_args() - - differences = 0 - for name, library in LIBRARIES.items(): - directory = Path(catchPath) / "third_party" / name - for file_name, data in archive_files(name, library).items(): - target = directory / file_name - if arguments.update: - target.write_bytes(data) - print(f"{name} {library['version']}: wrote {target.name}") - elif not target.is_file() or normalized(target.read_bytes()) != normalized(data): - print(f"{name} {library['version']}: {target} differs from the archive") - differences += 1 - if differences: - print("Run tools/scripts/updateThirdParty.py --update.") - return 1 - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tools/scripts/upstreamFiles.txt b/tools/upstreamFiles.txt similarity index 100% rename from tools/scripts/upstreamFiles.txt rename to tools/upstreamFiles.txt