Bazelを用いたC/C++プロジェクトでcompile_commands.jsonを生成する

結論

Ubuntu 18.04 LTSにて動作を確認しています。以下のコマンドを実行し、bazel-compilation-databaseをインストールします。

# 2020-06-09時点で最新のVer.0.4.4をインストールする場合
$ export COMPDB_VERSION=0.4.4
$ cd /usr/local/bin
$ sudo wget https://github.com/grailbio/bazel-compilation-database/archive/$COMPDB_VERSION.tar.gz
$ sudo tar -xf $COMPDB_VERSION.tar.gz && sudo rm $COMPDB_VERSION.tar.gz
$ sudo ln -sf bazel-compilation-database-$COMPDB_VERSION/generate.sh bazel-compdb

Bazelのワークスペースのルートディレクトリでbazel-compdbコマンドを実行します。

$ cd <root dir of workspace>
$ bazel-compdb

すると、ワークスペースのルートディレクトリ直下にcompile_commands.jsonが生成されます。

この記事で取り扱ったC++向けチュートリアルで試してみます。上記の通りbazel-compilation-databaseをインストールした後、stage3のワークスペースでcompile_commands.jsonを生成してみます。

$ cd stage3
$ ls
README.md  WORKSPACE  bazel-bin  bazel-out  bazel-stage3  bazel-testlogs  lib  main
$ bazel-compdb 
INFO: Analyzed 3 targets (15 packages loaded, 56 targets configured).
INFO: Found 3 targets...
INFO: Elapsed time: 0.619s, Critical Path: 0.01s
INFO: 0 processes.
$ ls
README.md  bazel-bin  bazel-stage3    compile_commands.json  main
WORKSPACE  bazel-out  bazel-testlogs  lib

問題なく生成されていることが確認出来ました。

[
{"command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/bin -I /usr/lib/gcc/x86_64-linux-gnu/7/include -I /usr/local/include -I /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I /usr/include/x86_64-linux-gnu -I /usr/include -I /usr/include/c++/7 -I /usr/include/x86_64-linux-gnu/c++/7 -I /usr/include/c++/7/backward -x c++ -c lib/hello-time.cc","directory":"/home/medalotte/.cache/bazel/_bazel_medalotte/5757c28399a7e48994370b39053937a8/execroot/__main__","file":"lib/hello-time.cc"},
 {"command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/bin -I /usr/lib/gcc/x86_64-linux-gnu/7/include -I /usr/local/include -I /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I /usr/include/x86_64-linux-gnu -I /usr/include -I /usr/include/c++/7 -I /usr/include/x86_64-linux-gnu/c++/7 -I /usr/include/c++/7/backward -x c++ -c lib/hello-time.h","directory":"/home/medalotte/.cache/bazel/_bazel_medalotte/5757c28399a7e48994370b39053937a8/execroot/__main__","file":"lib/hello-time.h"},
{"command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/bin -I /usr/lib/gcc/x86_64-linux-gnu/7/include -I /usr/local/include -I /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I /usr/include/x86_64-linux-gnu -I /usr/include -I /usr/include/c++/7 -I /usr/include/x86_64-linux-gnu/c++/7 -I /usr/include/c++/7/backward -x c++ -c main/hello-greet.cc","directory":"/home/medalotte/.cache/bazel/_bazel_medalotte/5757c28399a7e48994370b39053937a8/execroot/__main__","file":"main/hello-greet.cc"},
 {"command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/bin -I /usr/lib/gcc/x86_64-linux-gnu/7/include -I /usr/local/include -I /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I /usr/include/x86_64-linux-gnu -I /usr/include -I /usr/include/c++/7 -I /usr/include/x86_64-linux-gnu/c++/7 -I /usr/include/c++/7/backward -x c++ -c main/hello-greet.h","directory":"/home/medalotte/.cache/bazel/_bazel_medalotte/5757c28399a7e48994370b39053937a8/execroot/__main__","file":"main/hello-greet.h"},
{"command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -I /usr/lib/gcc/x86_64-linux-gnu/7/include -I /usr/local/include -I /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I /usr/include/x86_64-linux-gnu -I /usr/include -I /usr/include/c++/7 -I /usr/include/x86_64-linux-gnu/c++/7 -I /usr/include/c++/7/backward -x c++ -c main/hello-world.cc","directory":"/home/medalotte/.cache/bazel/_bazel_medalotte/5757c28399a7e48994370b39053937a8/execroot/__main__","file":"main/hello-world.cc"}
]

余談

C/C++プロジェクトにおいて、私はEmacsのlsp-mode + cclsを用いて開発を行っています(詳細はこちら)。cclsにビルド情報を与えるためにcompile_commands.jsonが必要なのですが、Bazelでも生成することが出来て良かったです。contributorに感謝!

最高〜

Bazelを用いたC/C++プロジェクトでcompile_commands.jsonを生成する」への2件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です