From 1a1d9d4b61f53fb8ee3c1f79cf8e49546879baa2 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Wed, 1 May 2024 14:21:40 -0400 Subject: [PATCH] Support building with Bazel (#1033) Adds support for building with Bazel. If merged, I can push this to https://registry.bazel.build/ when a new release is cut :) --------- Signed-off-by: Henry Schreiner Co-authored-by: Caleb Zulawski Co-authored-by: Henry Schreiner Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/tests.yml | 12 ++++++ .gitignore | 2 + BUILD.bazel | 8 ++++ MODULE.bazel | 3 ++ tests/BUILD.bazel | 75 +++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/HelpersTest.cpp | 6 +-- tests/tests/.gitkeep | 0 8 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 BUILD.bazel create mode 100644 MODULE.bazel create mode 100644 tests/BUILD.bazel create mode 100644 tests/tests/.gitkeep diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b2dd03f8..86e61e73 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 7b9bcb27..a5e6ee80 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 00000000..2e369dfd --- /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 00000000..234baa3e --- /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 00000000..5aefd860 --- /dev/null +++ b/tests/BUILD.bazel @@ -0,0 +1,75 @@ +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 = [ + '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", + ], + 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/CMakeLists.txt b/tests/CMakeLists.txt index 77f4ef57..6724fca0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -106,7 +106,7 @@ file( GLOB_RECURSE DATA_FILES LIST_DIRECTORIES false RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" - "${CMAKE_CURRENT_SOURCE_DIR}/data/*") + "${CMAKE_CURRENT_SOURCE_DIR}/data/*" "${CMAKE_CURRENT_SOURCE_DIR}/tests/.gitkeep") foreach(DATA_FILE IN LISTS DATA_FILES) add_custom_command( diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index e7de211e..0fceed55 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -522,7 +522,7 @@ TEST_CASE("Validators: FileIsDir", "[helpers]") { } TEST_CASE("Validators: DirectoryExists", "[helpers]") { - std::string mydir{"../tests"}; + std::string mydir{"tests"}; CHECK(CLI::ExistingDirectory(mydir).empty()); } @@ -543,7 +543,7 @@ TEST_CASE("Validators: DirectoryIsFile", "[helpers]") { } TEST_CASE("Validators: PathExistsDir", "[helpers]") { - std::string mydir{"../tests"}; + std::string mydir{"tests"}; CHECK(CLI::ExistingPath(mydir).empty()); } @@ -665,7 +665,7 @@ TEST_CASE("Validators: CombinedPaths", "[helpers]") { bool ok = static_cast(std::ofstream(myfile.c_str()).put('a')); // create file CHECK(ok); - std::string dir{"../tests"}; + std::string dir{"tests"}; std::string notpath{"nondirectory"}; auto path_or_dir = CLI::ExistingPath | CLI::ExistingDirectory; diff --git a/tests/tests/.gitkeep b/tests/tests/.gitkeep new file mode 100644 index 00000000..e69de29b