diff --git a/MODULE.bazel b/MODULE.bazel index d4d0cf0..43063b8 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -5,6 +5,7 @@ module( bazel_dep(name = "rules_cc", version = "0.2.16") bazel_dep(name = "cpp-httplib", version = "0.22.0") +bazel_dep(name = "platforms", version = "1.0.0") bazel_dep(name = "googletest", version = "1.17.0", dev_dependency = True) bazel_dep(name = "toolchains_llvm", version = "1.6.0", dev_dependency = True) diff --git a/build_utils/BUILD.bazel b/build_utils/BUILD.bazel new file mode 100644 index 0000000..0ca983a --- /dev/null +++ b/build_utils/BUILD.bazel @@ -0,0 +1,3 @@ +package(default_visibility = ["//visibility:public"]) + +exports_files(["defs.bzl"]) diff --git a/build_utils/defs.bzl b/build_utils/defs.bzl new file mode 100644 index 0000000..4bf9c4e --- /dev/null +++ b/build_utils/defs.bzl @@ -0,0 +1,46 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") + +_LINUX_FLAGS = [ + "-std=c++20", + "-Wall", + "-Wextra", + "-Wpedantic", + "-Werror", +] + +_WINDOWS_FLAGS = [ + "/std:c++20", + "/WX", + "/W4", +] + +def _strict_copts(): + return select({ + "@platforms//os:linux": _LINUX_FLAGS, + "@platforms//os:windows": _WINDOWS_FLAGS, + "//conditions:default": _LINUX_FLAGS, + }) + +def strict_cc_library(name, copts = [], **kwargs): + """Wrapper around cc_library that enforces C++20 and warnings as errors.""" + cc_library( + name = name, + copts = _strict_copts() + copts, + **kwargs + ) + +def strict_cc_binary(name, copts = [], **kwargs): + """Wrapper around cc_binary that enforces C++20 and warnings as errors.""" + cc_binary( + name = name, + copts = _strict_copts() + copts, + **kwargs + ) + +def strict_cc_test(name, copts = [], **kwargs): + """Wrapper around cc_test that enforces C++20 and warnings as errors.""" + cc_test( + name = name, + copts = _strict_copts() + copts, + **kwargs + ) diff --git a/src/BUILD.bazel b/src/BUILD.bazel index b4d84ca..856112e 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -1,13 +1,13 @@ -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") +load("//build_utils:defs.bzl", "strict_cc_binary", "strict_cc_library") -cc_library( +strict_cc_library( name = "rest_api_helper", srcs = ["hello.cpp"], hdrs = ["hello.hpp"], visibility = ["//visibility:public"], ) -cc_binary( +strict_cc_binary( name = "example_app", srcs = ["main.cpp"], deps = [":rest_api_helper"],