diff --git a/.bazelrc b/.bazelrc index d2d80b56ebb..34759b9e857 100644 --- a/.bazelrc +++ b/.bazelrc @@ -49,6 +49,14 @@ build --bes_keywords=repo:mongo build --jobs=200 common --remote_upload_local_results=False +# Settings specific for clang-tidy +--config=clang-tidy +build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect +build:clang-tidy --output_groups=report +build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config +build:clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=@mongo_toolchain//:clang_tidy +build:clang-tidy --@bazel_clang_tidy//:clang_tidy_additional_deps=@mongo_toolchain//:all + # if you don't have access to the remote execution cluster above, use the local config # described below. # pass local config to SCons like: diff --git a/BUILD.bazel b/BUILD.bazel index 92ece35c642..d8e737330e9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -18,3 +18,11 @@ alias( name = "codeowners", actual = "//buildscripts:codeowners", ) + +# This file group makes it possible to set the clang-tidy configuration setting: +# --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config +filegroup( + name = "clang_tidy_config", + srcs = [".clang-tidy"], + visibility = ["//visibility:public"], +) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index c49e2e8edae..b5d254b4ce5 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -2,6 +2,14 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("//bazel/platforms:local_config_platform.bzl", "setup_local_config_platform") load("//bazel/toolchains:mongo_toolchain.bzl", "toolchain_download") +http_archive( + name = "bazel_clang_tidy", + # Note: strip_prefix is necessary to refer to the `@bazel_clang_tidy` package + strip_prefix = "bazel_clang_tidy-1.1", + # TODO(80396): Figure out how to get the sha256 of the release tarball + url = "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.1.tar.gz", +) + setup_local_config_platform(name = "local_config_platform") toolchain_download(name = "mongo_toolchain") diff --git a/bazel/docs/developer_workflow.md b/bazel/docs/developer_workflow.md index c0a1429a136..97c54b5a585 100644 --- a/bazel/docs/developer_workflow.md +++ b/bazel/docs/developer_workflow.md @@ -99,3 +99,27 @@ This allows SCons build targets to depend on Bazel build targets directly. The B 'new_library', # depend on the bazel "new_library" target defined above ], ) + +## Running clang-tidy via Bazel + +Note: This feature is still in development; see https://jira.mongodb.org/browse/SERVER-80396 for details) + +To run clang-tidy via Bazel, do the following: + +1. (Only necessary once, unless you change the `.clang-tidy.in`) Generate `.clang-tidy` by running `python3 buildscripts/clang_tidy.py`. +2. To analyze all code, run `bazel build --config=clang-tidy src/...` +3. To analyze a single target (e.g.: `fsync_locked`), run the following command (note that `_with_debug` suffix on the target): `bazel build --config=clang-tidy src/mongo/db/commands:fsync_locked_with_debug` + +Remaining work to do: + +- TODO(SERVER-80396): Make it SCons-invokable +- TODO(SERVER-80396): Link in Mongo's custom clang-tidy rules +- TODO(SERVER-80396): Make it run in CI +- TODO(SERVER-80396): Address the concern about [aspect / feature interaction](https://github.com/10gen/mongo/pull/21221#pullrequestreview-2009832686). + Testing notes: +- If you want to test whether clang-tidy is in fact finding bugs, you can inject the following code into a `cpp` file to generate a `bugprone-incorrect-roundings` warning: + +``` +const double f = 1.0; +const int foo = (int)(f + 0.5); +``` diff --git a/bazel/toolchains/mongo_toolchain.BUILD b/bazel/toolchains/mongo_toolchain.BUILD index f4557ac1d4b..a6ed6eef512 100644 --- a/bazel/toolchains/mongo_toolchain.BUILD +++ b/bazel/toolchains/mongo_toolchain.BUILD @@ -172,3 +172,12 @@ cc_toolchain_suite( "{bazel_toolchain_cpu}": ":cc_mongo_toolchain", }, ) + +# This file group makes it possible to set the clang-tidy configuration setting: +# --@bazel_clang_tidy//:clang_tidy_executable=@mongo_toolchain//:clang_tidy +filegroup( + name = "clang_tidy", + srcs = [ + "v4/bin/clang-tidy", + ], +)