Skip to content

Instantly share code, notes, and snippets.

@datsuns
Last active December 24, 2025 00:47
Show Gist options
  • Select an option

  • Save datsuns/a3d4bb34006be7604bde4658a02e9f9c to your computer and use it in GitHub Desktop.

Select an option

Save datsuns/a3d4bb34006be7604bde4658a02e9f9c to your computer and use it in GitHub Desktop.
bazel cpplint (WORKSPACE)
sh_binary(
name = "clang_tidy_runner",
srcs = ["run_clang_tidy.sh"],
data = [
"@llvm_toolchain//:clang_tidy_bin",
],
env = {
"CLANG_TIDY_PATH": "$(rootpath @llvm_toolchain//:clang_tidy_bin)",
},
# ローカルの compile_commands.json を読み、実ファイルに対して解析を行うため
tags = ["no-sandbox"],
)
sh_binary(
name = "cppcheck_runner",
srcs = ["run_cppcheck.sh"],
args = [
"--enable=all",
"--inconclusive",
"--library=googletest", # 使用しているライブラリがあれば指定
"--suppress=missingIncludeSystem", # システムヘッダーの警告を抑制
],
env = {
# システムの cppcheck を使用
"CPPCHECK_PATH": "cppcheck",
},
tags = ["no-sandbox"],
)
sh_binary(
name = "cpplint_runner",
srcs = ["run_cpplint.sh"],
data = [
"@cpplint_py//file", # ダウンロードしたファイル
],
args = [
"--filter=-build/header_guard,-build/include_subdir",
"--linelength=120",
],
env = {
# ダウンロードしたファイルのパスをスクリプトに渡す
"CPPLINT_PATH": "$(rootpath @cpplint_py//file)",
},
tags = ["no-sandbox"],
)
sh_binary(
name = "flake8_runner",
srcs = ["run_flake8.sh"],
args = [
"--max-line-length=120",
"--ignore=E203,W503", # 一般的な無視設定
],
env = {
"FLAKE8_PATH": "flake8",
},
tags = ["no-sandbox"],
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
# cpplint.py を直接ダウンロード
http_file(
name = "cpplint_py",
executable = True,
# バージョンを固定したい場合は特定のコミットハッシュやタグのURLを使用してください
sha256 = "18e6983359d95f8502d352758411cd7a1f59990b7904323e070d6556e4c026cf",
urls = ["https://raw.githubusercontent.com/cpplint/cpplint/2.0.0/cpplint.py"],
)
#!/bin/bash
set -e
echo "=== Step 1: Updating Compilation Database ==="
bazel run @hedron_compile_commands//:refresh_all
echo ""
echo "=== Step 2: Running clang-tidy ==="
bazel run //:clang_tidy_runner -- "$@"
#!/bin/bash
# Bazelから渡されたclang-tidyの絶対パスを取得
TIDY_BIN=$CLANG_TIDY_PATH
if [ ! -f "compile_commands.json" ]; then
echo "Error: compile_commands.json not found."
echo "Run 'bazel run @hedron_compile_commands//:refresh_all' first."
exit 1
fi
# 引数があればそれを使い、なければ全cpp/ccファイルを対象にする
if [ $# -eq 0 ]; then
FILES=$(find . -maxdepth 4 -name "*.cpp" -o -name "*.cc")
else
FILES="$@"
fi
echo "Using: $TIDY_BIN"
$TIDY_BIN -p . $FILES
#!/bin/bash
set -e
# 1. プロジェクトルートへ移動
if [ -n "$BUILD_WORKSPACE_DIRECTORY" ]; then
cd "$BUILD_WORKSPACE_DIRECTORY"
fi
# 2. ツールパスの解決(環境変数になければ PATH から探す)
CPPCHECK_BIN=${CPPCHECK_PATH:-"cppcheck"}
REPORT_FILE="cppcheck_report.txt"
JSON_FILE="compile_commands.json"
# 3. 引数の仕分け
TIDY_OPTIONS=()
SPECIFIED_FILES=()
for arg in "$@"; do
if [[ "$arg" == -* ]]; then
TIDY_OPTIONS+=("$arg")
else
SPECIFIED_FILES+=("$arg")
fi
done
# 4. 解析対象の決定
if [ ${#SPECIFIED_FILES[@]} -gt 0 ]; then
FILES="${SPECIFIED_FILES[@]}"
else
# ファイル指定がない場合は JSON から抽出(前回同様)
FILES=$(grep '"file":' "$JSON_FILE" | sed 's/.*"file": "\(.*\)".*/\1/')
fi
# 5. 実行
echo "Running cppcheck..."
# --project: コンパイルデータベースを使用
# --enable=all: 全てのチェックを有効化
# --error-exitcode=1: エラー時に終了コードを 1 に(CI用)
$CPPCHECK_BIN --project="$JSON_FILE" \
"${TIDY_OPTIONS[@]}" \
$FILES 2>&1 | tee "$REPORT_FILE"
echo "Done. Results saved in $REPORT_FILE"
#!/bin/bash
set -e
# 1. プロジェクトルートへ移動
if [ -n "$BUILD_WORKSPACE_DIRECTORY" ]; then
cd "$BUILD_WORKSPACE_DIRECTORY"
fi
# 2. ツールパスの解決(システムの cpplint を使用)
CPPLINT_BIN=${CPPLINT_PATH:-"cpplint"}
REPORT_FILE="cpplint_report.txt"
# 3. 引数の仕分け
OPTIONS=()
SPECIFIED_FILES=()
for arg in "$@"; do
if [[ "$arg" == -* ]]; then
OPTIONS+=("$arg")
else
SPECIFIED_FILES+=("$arg")
fi
done
# 4. 解析対象の決定
if [ ${#SPECIFIED_FILES[@]} -gt 0 ]; then
FILES="${SPECIFIED_FILES[@]}"
else
# ファイル指定がない場合は JSON から抽出(ビルド対象と同期)
if [ ! -f "compile_commands.json" ]; then
echo "Error: compile_commands.json not found."
exit 1
fi
FILES=$(grep '"file":' "compile_commands.json" | sed 's/.*"file": "\(.*\)".*/\1/')
fi
# 5. 実行
echo "Running cpplint..."
# --quiet: 正常終了時に何も出力しない
# 2>&1 | tee: cpplint は標準エラー出力に結果を出すことが多いためまとめます
$CPPLINT_BIN "${OPTIONS[@]}" $FILES 2>&1 | tee "$REPORT_FILE"
echo "Done. Results saved in $REPORT_FILE"
#!/bin/bash
set -e
# 1. プロジェクトルートへ移動
if [ -n "$BUILD_WORKSPACE_DIRECTORY" ]; then
cd "$BUILD_WORKSPACE_DIRECTORY"
fi
FLAKE8_BIN=${FLAKE8_PATH:-"flake8"}
REPORT_FILE="flake8_report.txt"
# 2. 引数の仕分け
OPTIONS=()
SPECIFIED_FILES=()
for arg in "$@"; do
if [[ "$arg" == -* ]]; then OPTIONS+=("$arg"); else SPECIFIED_FILES+=("$arg"); fi
done
# 3. 解析対象の決定
if [ ${#SPECIFIED_FILES[@]} -gt 0 ]; then
FILES="${SPECIFIED_FILES[@]}"
else
# 引数がない場合は src 以下の Python ファイルを全検索
echo "No files specified. Searching for Python files in src/..."
FILES=$(find src -name "*.py")
fi
# 4. 実行
if [ -z "$FILES" ]; then
echo "No Python files found."
exit 0
fi
echo "Running flake8..."
$FLAKE8_BIN "${OPTIONS[@]}" $FILES 2>&1 | tee "$REPORT_FILE"
echo "Done. Results saved in $REPORT_FILE"
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
# 1. LLVMバイナリの取得 (clang-tidy本体)
# ※以下は Linux (Ubuntu 22.04用) の例です。OSに合わせてURLとSHA256を変更してください。
http_archive(
name = "llvm_toolchain",
build_file_content = """
filegroup(
name = "clang_tidy_bin",
srcs = ["bin/clang-tidy"],
visibility = ["//visibility:public"],
)
""",
sha256 = "600192e21334f5950d750c8227092911b329432612f00f074d6c4125f3851b69",
strip_prefix = "clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-22.04",
urls = ["https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.0/clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-22.04.tar.xz"],
)
# 2. compile_commands.json 抽出ツール (Rust不要・Starlarkのみ)
git_repository(
name = "hedron_compile_commands",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
commit = "a14ad3a64e7bf398ab48105aaa0348e032ac87f8",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment