Skip to content

如何检查使用 Bazel 构建的项目

Joshua1023 edited this page Dec 13, 2023 · 2 revisions

本文将以语音通信项目 tywebrtchttps://github.com/Justme0/tywebrtc)为例,说明如何在 Bazel 构建的项目上使用 NaiveSystems Analyze 社区版进行检查。

修改 Bazel 配置文件

WORKSPACE 文件添加以下内容:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
http_archive(
    name = "hedron_compile_commands",

    # Replace the commit hash (daae6f40adfa5fdb7c89684cbe4d88b691c63b2d) in both places (below) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main), rather than using the stale one here.
    # Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
    url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/daae6f40adfa5fdb7c89684cbe4d88b691c63b2d.tar.gz",
    strip_prefix = "bazel-compile-commands-extractor-daae6f40adfa5fdb7c89684cbe4d88b691c63b2d",
    # When you first run this tool, it'll recommend a sha256 hash to put here with a message like: "DEBUG: Rule 'hedron_compile_commands' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = ..."
)
load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")
hedron_compile_commands_setup()

根目录下 BUILD 文件增加以下内容(不存在则新建 BUILD 文件)

load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

refresh_compile_commands(
    name = "refresh_compile_commands",
    targets = {
      "//src/...": "",
    },
    exclude_headers = "all",
    exclude_external_sources = True,
)

targets 用来指定待检查的 Bazel 构建目标。

下载合适版本的 bazel

根据官方指导下载项目构建对应的 Bazel (tywebrtc 项目需要 Bazel 5.2.0)

生成 compile_commands.json 并运行检查(tywebrtc 项目需要先进行下一步)

创建 .naivesystems/check_rules 并指定需要检查的规则。

假设 bazel binary 在 ~/.local/bin/

在项目根目录运行:

mkdir -p output

podman run --rm -v ~/.local/bin/:/root/.local/bin:O \
-v $PWD:/src:O \
-v $PWD/.naivesystems:/config:Z \
-v $PWD/output:/output:Z \
-w /src/ \
-it ghcr.io/naivesystems/analyze:master /bin/bash

进入容器后运行:

bazel run :refresh_compile_commands

完成后运行开始检查:

/opt/naivesystems/misra_analyzer -show_results --skip_bear_make --ignore_dir=/src/external/** --ignore_dir=external/**

补充缺少的第三方 submodule(tywebrtc

third_party 目录下运行:

git submodule add https://github.com/Tencent/libco.git
git submodule add https://github.com/xiph/opus.git
git submodule add https://github.com/openssl/openssl.git
git submodule add https://github.com/cisco/libsrtp.git

WORKSPACE 文件中

local_repository(
    name = "libco",
    path = "third_party/basic/",


)

替换为:

new_local_repository(
    name = "libco",
    path = "third_party/libco",
    build_file_content = """
package(default_visibility = ["//visibility:public"])
cc_library(
    name = "libco_package",
    srcs = ["libco/libcolib.a"],
    hdrs = glob(["libco/*.h"]),
    includes = ["libco"],
)
"""
)

src/BUILD 文件中

"@libco//colib:colib",

替换为

"@libco//:libco_package",