From 39a60e70542d98718bc362bad105b1d05a28cecc Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Tue, 16 Apr 2024 14:09:23 -0400 Subject: [PATCH] Support building with Bazel --- .github/workflows/tests.yml | 12 ++++++ .gitignore | 2 + BUILD.bazel | 8 ++++ MODULE.bazel | 3 ++ tests/BUILD.bazel | 77 +++++++++++++++++++++++++++++++++++++ tests/HelpersTest.cpp | 12 ++++++ 6 files changed, 114 insertions(+) create mode 100644 BUILD.bazel create mode 100644 MODULE.bazel create mode 100644 tests/BUILD.bazel diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b2dd03f8c..86e61e734 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -169,6 +169,18 @@ jobs: - name: Test run: meson test -C build-meson + bazel-build: + name: Bazel build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Build + run: bazel build //... + + - name: Test + run: bazel test --test_output=errors //... + install: name: install tests runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 7b9bcb27f..a5e6ee807 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ a.out* /html/* !/meson.build /CMakeUserPresets.json +/bazel-* +/MODULE.bazel.lock /node_modules/* /package.json diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000..2e369dfdc --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,8 @@ +cc_library( + name = "cli11", + srcs = glob(["src/**/*.cpp"]), + hdrs = glob(["include/**/*.hpp"]), + local_defines = ["CLI11_COMPILE"], + strip_include_prefix = "/include", + visibility = ["//visibility:public"], +) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 000000000..234baa3e2 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,3 @@ +module(name = "cli11") + +bazel_dep(name = "catch2", version = "3.5.4", dev_dependency = True) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel new file mode 100644 index 000000000..240aa7311 --- /dev/null +++ b/tests/BUILD.bazel @@ -0,0 +1,77 @@ +cc_binary( + name = "ensure_utf8", + srcs = ["applications/ensure_utf8.cpp"], + deps = ["//:cli11"], +) + +cc_binary( + name = "ensure_utf8_twice", + srcs = ["applications/ensure_utf8_twice.cpp"], + deps = ["//:cli11"], +) + +cc_library( + name = "catch_main", + srcs = ["main.cpp"], + hdrs = ["catch.hpp"], + defines = ["CLI11_CATCH3"], + deps = ["@catch2//:catch2_main"], +) + +cc_test( + name = "AppTest", + srcs = [ + "AppTest.cpp", + "app_helper.hpp", + ], + data = [ + "ensure_utf8", + "ensure_utf8_twice", + ], + local_defines = [ + "BAZEL", + 'CLI11_ENSURE_UTF8_EXE=\\"$(rootpath ensure_utf8)\\"', + 'CLI11_ENSURE_UTF8_TWICE_EXE=\\"$(rootpath ensure_utf8_twice)\\"', + ], + deps = [ + "catch_main", + "//:cli11", + "@catch2", + ], +) + +[ + cc_test( + name = test, + srcs = [ + test + ".cpp", + "app_helper.hpp", + ], + local_defines = ["BAZEL"], + deps = [ + "catch_main", + "//:cli11", + "@catch2", + ], + ) + for test in [ + "HelpersTest", + "ConfigFileTest", + "OptionTypeTest", + "SimpleTest", + "SetTest", + "TransformTest", + "CreationTest", + "SubcommandTest", + "HelpTest", + "FormatterTest", + "NewParseTest", + "OptionalTest", + "DeprecatedTest", + "StringParseTest", + "ComplexTypeTest", + "TrueFalseTest", + "OptionGroupTest", + "EncodingTest", + ] +] diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index e7de211e1..970ea7d6b 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -522,7 +522,11 @@ TEST_CASE("Validators: FileIsDir", "[helpers]") { } TEST_CASE("Validators: DirectoryExists", "[helpers]") { +#ifdef BAZEL + std::string mydir{"tests"}; +#else std::string mydir{"../tests"}; +#endif CHECK(CLI::ExistingDirectory(mydir).empty()); } @@ -543,7 +547,11 @@ TEST_CASE("Validators: DirectoryIsFile", "[helpers]") { } TEST_CASE("Validators: PathExistsDir", "[helpers]") { +#ifdef BAZEL + std::string mydir{"tests"}; +#else std::string mydir{"../tests"}; +#endif CHECK(CLI::ExistingPath(mydir).empty()); } @@ -665,7 +673,11 @@ TEST_CASE("Validators: CombinedPaths", "[helpers]") { bool ok = static_cast(std::ofstream(myfile.c_str()).put('a')); // create file CHECK(ok); +#ifdef BAZEL + std::string dir{"tests"}; +#else std::string dir{"../tests"}; +#endif std::string notpath{"nondirectory"}; auto path_or_dir = CLI::ExistingPath | CLI::ExistingDirectory;