{"id":27958050,"url":"https://github.com/ericsson/codechecker_bazel","last_synced_at":"2025-10-14T02:03:02.979Z","repository":{"id":267702131,"uuid":"898951238","full_name":"Ericsson/codechecker_bazel","owner":"Ericsson","description":"Bazel rules for CodeChecker","archived":false,"fork":false,"pushed_at":"2025-10-07T14:06:17.000Z","size":84,"stargazers_count":4,"open_issues_count":35,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-10-07T16:10:03.244Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Starlark","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ericsson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-12-05T10:47:45.000Z","updated_at":"2025-10-07T14:06:22.000Z","dependencies_parsed_at":"2025-06-18T14:22:19.536Z","dependency_job_id":"f79883bb-743c-46a2-a9e6-c4c776e1155b","html_url":"https://github.com/Ericsson/codechecker_bazel","commit_stats":null,"previous_names":["ericsson/codechecker_bazel"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Ericsson/codechecker_bazel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ericsson%2Fcodechecker_bazel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ericsson%2Fcodechecker_bazel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ericsson%2Fcodechecker_bazel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ericsson%2Fcodechecker_bazel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ericsson","download_url":"https://codeload.github.com/Ericsson/codechecker_bazel/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ericsson%2Fcodechecker_bazel/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279017564,"owners_count":26086121,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-07T18:16:05.654Z","updated_at":"2025-10-14T02:03:02.967Z","avatar_url":"https://github.com/Ericsson.png","language":"Starlark","funding_links":[],"categories":[],"sub_categories":[],"readme":"Bazel Rules for CodeChecker\n===========================\n\nBazel rules for CodeChecker and other tools for Code Analysis,\nincluding Clang-tidy, Clang analyzer, generating compilation database\n(`compile_commands.json`) and others.\n\n\u003e If you would like to report an issue or suggest a change\n\u003e please read [CONTRIBUTING.md](CONTRIBUTING.md).\n\n### CodeChecker\n\nCodeChecker is a static analysis infrastructure that conveniently manages static analyzer engines such as the Clang Static Analyzer, Clang-tidy, GCC Static Analyzer, CppCheck and Infer.\n\nRead about CodeChecker:\n\n* GitHub: https://github.com/Ericsson/codechecker\n* Read the Docs: https://codechecker.readthedocs.io/\n\nThe main Bazel rule for CodeChecker is `codechecker_test()`.\n\n### Clang-tidy\n\nClang-tidy is a fast static analyzer/linter for the C family of languages. This\nrepository provides Bazel aspect `clang_tidy_aspect()` and rule `clang_tidy_test()`\nto run clang-tidy natively (without CodeChecker).\n\nFind more information about LLVM clang-tidy:\n\n* LLVM: https://clang.llvm.org/extra/clang-tidy\n* bazel_clang_tidy: https://github.com/erenon/bazel_clang_tidy\n\n### Clang Static Analyzer\n\nThe Clang Static Analyzer (or `clang --analyze`) is among\nthe most sophisticated tools for C/C++ code analysis which implements\npath-sensitive, inter-procedural analysis based on symbolic execution technique.\nThis repository provides the Bazel rule `clang_analyze_test()` which runs the\nClang Static Analyzer natively (without CodeChecker)\n\nFind more information about LLVM Clang Static Analyzer:\n\n* LLVM: https://clang.llvm.org/docs/ClangStaticAnalyzer.html\n\n### Generating a compilation database\n\nThere is also a Bazel rule for generating a compilation database (compile_commands.json) via `compile_commands()`. The current implementation is Bazel native and doesn't use `CodeChecker log`.\n\nPrerequisites\n-------------\n\nWe need the following tools:\n\n- Git 2 or newer (we use 2.36)\n- Bazel 6, not yet bazel 7 (we recommend version 6.5.0)\n- Clang 16 or newer (we use 16), we use clang-tidy\n- Python 3.8 or newer (we use 3.11)\n- CodeChecker 6.26 or newer (we use 6.26.0)\n\nIf, by chance, Environment Modules (https://modules.sourceforge.net/)\nare available in your system, you can just add the following modules:\n\n    module add git\n    module add bazel/6\n    module add clang/16\n    module add python/3.11\n    module add codechecker/6.26\n\n\nGetting Started\n---------------\n\nInstall dependencies:\n\nRHEL 9:\n```bash\ndnf update -y \u0026\u0026 dnf install -y wget llvm-toolset clang-tools-extra git python3 python3-pip gcc g++\n```\n\nUbuntu:\n```bash\nsudo apt-get update --quiet \u0026\u0026 sudo apt-get install --no-install-recommends wget git python3 python3-pip python3-venv gcc g++ clang clang-tools clang-tidy\n```\n\nOn some distributions, `clang`, `clang-tidy` and `clang-extdef-mapping` may be installed with a trailing version number (e.g. clang-extdef-mapping-18). In case your package didn't install a non-versioned symlink as well, you will need to manually change it:\n```bash\nupdate-alternatives --install /usr/bin/clang-extdef-mapping clang-extdef-mapping /usr/bin/clang-extdef-mapping-18 100\nupdate-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-18 100 \nupdate-alternatives --install /usr/bin/clang clang /usr/bin/clang-18 100\n```\n\nInstall CodeChecker:\n\u003e [!Note]\n\u003e Currently the pip installed CodeChecker works best with these rules, which is our recommendation (as opposed to using acustom-built CodeChecker).\n\n```bash\npython3 -m venv ./codechecker_venv \u0026\u0026 \\\nsource ./codechecker_venv \u0026\u0026 \\\npip3 install codechecker\n```\n\nInstall Bazel:\nWe recommend bazel 6.5.0\n```bash\nwget https://github.com/bazelbuild/bazel/releases/download/6.5.0/bazel-6.5.0-linux-x86_64 \u0026\u0026 \\\nchmod +x bazel-6.5.0-linux-x86_64 \u0026\u0026 \\\nsudo mv bazel-6.5.0-linux-x86_64 /usr/local/bin/bazel\n```\nOr choose a suitable binary for your system from this list: https://github.com/bazelbuild/bazel/releases/tag/6.5.0\nAlternatively follow the official guide at: https://bazel.build/install\n\n\u003e [!CAUTION]\n\u003e Don't use ccache! You should disable/remove/uninstall it, as the rules don't support it.\n\u003c!-- TODO When we make a decision on how to handle ccache in #36, expand this section --\u003e\n\nHow to use\n----------\n\nTo use these rules you should first add `codechecker_bazel` as an\n[external dependency](https://bazel.build/versions/6.5.0/external/overview#workspace-system)\nto your `WORKSPACE` file:\n\n```python\nload(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")\n\ngit_repository(\n    name = \"bazel_codechecker\",\n    remote = \"https://github.com/Ericsson/codechecker_bazel.git\",\n    branch = \"main\",\n)\n\nload(\n    \"@bazel_codechecker//src:tools.bzl\",\n    \"register_default_codechecker\",\n    \"register_default_python_toolchain\",\n)\n\nregister_default_python_toolchain()\n\nregister_default_codechecker()\n```\n## CodeChecker\n\n### Standard CodeChecker invocation: `codechecker_test()`\n\n`codechecker_test()` invokes CodeChecker the \"standard way\", as you'd call\nit normally from the command line. The rule first generates a compilation\ndatabase on all targets given to the rule. Then, [`CodeChecker analyze`](https://github.com/Ericsson/codechecker/blob/master/docs/analyzer/user_guide.md#analyze)\nis run on all translation units found in those targets.\n\n\u003e [!NOTE]\n\u003e Even though bazel is capable of incremental builds, if any files are\n\u003e rebuilt, this rule will reanalyze all translation units in all targets,\n\u003e even those that needed no rebuild.\n\nTo use `codechecker_test()` include it to your BUILD file:\n\n```python\nload(\n    \"@bazel_codechecker//src:codechecker.bzl\",\n    \"codechecker_test\",\n)\n```\n\nCreate a `codechecker_test()` target by passing other targets you'd like CodeChecker to analyze:\n\n\u003c!-- TODO: Consider using https://github.com/bazelbuild/stardoc to document parameters --\u003e\n```python\ncodechecker_test(\n    name = \"your_codechecker_rule_name\",\n    targets = [\n        \"your_target\",\n    ],\n)\n```\n\n#### Per-file CodeChecker analysis:\n\u003e [!IMPORTANT]\n\u003e The option is still in prototype status and is subject to changes or removal without notice. See [#31](https://github.com/Ericsson/codechecker_bazel/issues/31).\n\u003e You are free to experiment and report issues however!\n\nInstead of a single CodeChecker call, adding `per_file = True,` parameter to codechecker_test bazel rule invokes\n[`CodeChecker analyze`](https://github.com/Ericsson/codechecker/blob/master/docs/analyzer/user_guide.md#analyze)\n_for each_ translation unit in the targets to analyze. This method is intended to be\nable to enable incremental analyses and dispatching analysis jobs to remote build\nagents.\n\n\nCreate a `codechecker_test()` target and add the `per_file = True,` parameter:\n\n```python\ncodechecker_test(\n    name = \"your_codechecker_rule_name\",\n    targets = [\n        \"your_target\",\n    ],\n    per_file = True,\n)\n```\n\nThen invoke bazel:\n\n```bash\nbazel test ://your_codechecker_rule_name\n# Or, as a part of the rest of the testsuite\nbazel test ...\n```\n\nYou can find the analysis results in the `bazel-bin/` folder, on which you\ncan run [`CodeChecker store`](https://github.com/Ericsson/codechecker/blob/master/docs/web/user_guide.md#store)\nor [`CodeChecker parse`](https://github.com/Ericsson/codechecker/blob/master/docs/analyzer/user_guide.md#parse).\nThe precise output path to the directory can vary, but you should look for `your_codechecker_rule_name/codechecker-files/data`.\nIn simpler cases, something like the following:\n\n```bash\nCodeChecker parse bazel-bin/your_codechecker_rule_name/codechecker-files/data\nCodeChecker store bazel-bin/your_codechecker_rule_name/codechecker-files/data -n \"Run name\"\n```\n\n\u003c!-- For now, we consider codechecker() to be an internal rule.\n\n### Build-only CodeChecker analysis: `codechecker()`\n\nThis rule is functionally equivalent to `codechecker_test()` but omits the test phase where either PASS or FAIL isc printed.\nYou can include and use it similarly as well:\n\n```python\nload(\n    \"@bazel_codechecker//src:codechecker.bzl\",\n    \"codechecker\"\n)\n```\n\n--\u003e\n\n### Multi-platform CodeChecker analysis: `codechecker_suite()`\n_TODO: Describe this rule: see issue [#44](https://github.com/Ericsson/codechecker_bazel/issues/44)._\n\u003c!--\nThis rule is functionally equivalent to `codechecker_test()` but allows for running on multiple platforms via the `platforms` parameter.\nYou can include and use it similarly as well:\n\n```python\nload(\n    \"@bazel_codechecker//src:codechecker.bzl\",\n    \"codechecker_suite\"\n)\n```\n--\u003e\n### `codechecker_config()`\n\nUsing the Bazel rule `codechecker_config()` you can utilize a CodeChecker [configuration file](https://github.com/Ericsson/codechecker/blob/master/docs/config_file.md).\n\nFirst, include the rule in your BUILD file:\n\n```python\nload(\n    \"@bazel_codechecker//src:codechecker.bzl\",\n    \"codechecker_config\"\n)\n```\n\nCreate a CodeChecker configuration file e.g. `config.json` (see example [test/config.json](test/config.json)) and parse it using `codechecker_config()`.\n\n```python\ncodechecker_config(\n    name = \"your_codechecker_config\",\n    config_file = \":config.json\"\n)\n```\n\nAlternatively, you can assemble a CodeChecker configuration without a config file using the rule:\n\n```python\ncodechecker_config(\n    name = \"your_codechecker_config\",\n    analyze = [\n        \"--enable=bugprone-dangling-handle\",\n        \"--enable=bugprone-fold-init-type\",\n        \"--enable=misc-non-copyable-objects\",\n        \"--report-hash=context-free-v2\",\n    ]\n)\n```\n\nYou can now configure your `codechecker_suite()` and `codechecker_test()` targets using the above configuration:\n\n```python\ncodechecker_test(\n    name = \"your_codechecker_rule_name\",\n    config = \"your_codechecker_config\",\n    targets = [\n        \"your_target\",\n    ],\n)\n```\n\n## CodeChecker independent rules\n\nThe following rules are _not_ using CodeChecker.\n\n### Clang-tidy: `clang_tidy_aspect()` and `clang_tidy_test()`\n\nThe Bazel rule `clang_tidy_test()` runs clang-tidy natively without CodeChecker. To use it, add the following to your BUILD file:\n\n```python\nload(\n    \"@bazel_codechecker//src:clang.bzl\",\n    \"clang_tidy_test\",\n)\n\nclang_tidy_test(\n    name = \"your_rule_name\",\n    targets = [\n        \"your_target\",\n    ],\n)\n```\n\nYou can also run clang-tidy via the Bazel aspect `clang_tidy_aspect()` that can be invoked from the command line by passing the following parameter to Bazel build/test: `--aspects @bazel_codechecker//src:clang.bzl%clang_tidy_aspect`:\n\n```bash\nbazel build ... --aspects @bazel_codechecker//src:clang.bzl%clang_tidy_aspect --output_groups=report\n```\n\n### Clang Static Analyzer: `clang_analyze_test()`\n\nThe Bazel rule `clang_analyze_test()` runs The Clang Static Analyzer natively without CodeChecker. To use it, add the following to your BUILD file:\n\n```python\nload(\n    \"@bazel_codechecker//src:clang.bzl\",\n    \"clang_analyze_test\",\n)\n\nclang_analyze_test(\n    name = \"your_rule_name\",\n    targets = [\n        \"your_target\",\n    ],\n)\n```\n\n### Generating a compilation database: `compile_commands()`\n\nAs generating a compilation database for C/C++ is a known pain point for bazel, this repository defines the Bazel rule `compile_commands()` rule which can be used independently of CodeChecker. The implementation is based on https://github.com/grailbio/bazel-compilation-database with some fixes on some tricky edge cases. To use it, include the following in your BUILD file:\n\n```python\nload(\n    \"@bazel_codechecker//src:compile_commands.bzl\",\n    \"compile_commands\",\n)\n```\n\nThen use `compile_commands()` rule passing build targets:\n\n```python\ncompile_commands(\n    name = \"your_compile_commands_rule_name\",\n    targets = [\n        \"your_target\",\n    ],\n)\n```\nYou can find the generated `compile_commands.json` under `bazel-bin/`.\n\n## Experimental rules\n\n### Cross-translation unit analysis via the Clang Static Analyzer: `clang_ctu_test()`\n\u003e [!IMPORTANT]\n\u003e The rule is still in prototype status and is subject to changes or removal without notice. See [#32](https://github.com/Ericsson/codechecker_bazel/issues/32).\n\u003e We are also actively pursuing better CTU support _using_ CodeChecker.\n\nThe Bazel rule `clang_analyze_test()` runs The Clang Static Analyzer with [cross translation unit analysis](https://clang.llvm.org/docs/analyzer/user-docs/CrossTranslationUnit.html) analysis without CodeChecker. To use it, add the following to your BUILD file:\n\n```python\nload(\n    \"@bazel_codechecker//src:clang_ctu.bzl\",\n    \"clang_ctu_test\",\n)\n\nclang_ctu_test(\n    name = \"your_clang_ctu_rule_name\",\n    targets = [\n        \"your_target\",\n    ],\n)\n```\n\nExamples\n--------\n\nIn [test/BUILD](test/BUILD) you can find examples for `codechecker_test()`\nand for `compile_commands()` rules.\n\nFor instance see targets `codechecker_pass` and `compile_commands_pass`.\n\nRun all test Bazel targets:\n\n    bazel test ...\n\nAfter that you can find all artifacts in `bazel-bin` directory:\n\n    # All codechecker_pass artifacts\n    ls bazel-bin/test/codechecker_pass/\n    \n    # compile_commands.json for compile_commands_pass\n    cat bazel-bin/test/compile_commands_pass/compile_commands.json\n\nTo run `clang_tidy_aspect()` on all C/C++ code:\n\n    bazel build ... --aspects @bazel_codechecker//src:clang.bzl%clang_tidy_aspect --output_groups=report\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericsson%2Fcodechecker_bazel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericsson%2Fcodechecker_bazel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericsson%2Fcodechecker_bazel/lists"}