This repository contains examples showing how compile_commands.json can be generated for various build
systems and their plugins.
For detailed explanation of steps required for each build system - check out my blog.
These scripts are used by me to test whether one of my Clang tools - clang-uml - works with compilation databases generated by these build systems.
Each subdirectory contains the same C++ hello world project with minimal configuration for a given build system
require to build it and generate compile_commands.json.
The list of examples currently contains:
- b2 -
b2 - Bazel
- buck2 -
buck2 - build2 -
build2 - Clang++ with -MJ flag -
clang_mj - CMake -
cmake - FASTBuild -
fastbuild - make
- Meson Build -
mesonbuild - MSVC Clang Power Tools -
msvc_clang_power_tools - MSVC MSBuildCompileCommandsJSON -
msvc_msbuildcompilecommandsjson - Ninja -
ninja - Premake
- Qbs -
qbs - SCons -
scons - Waf -
waf - XCode
- xmake -
xmake
The project contains a top-level Lua script - make.lua - which will setup the build system and generate
the compilation database, based on properties in <subdirectory>/gallery.lua. For example for cmake the properties are:
build_system_files = {"CMakeLists.txt"}
print_version_cmd = "cmake --version"
compdb_dir = "debug"
generate_compdb_cmd = "cmake -S . -B " .. compdb_dirMost examples can be run on Linux, some on macos and MSVC-specific on Windows.
$ ./make.lua cmakeIf you'd like to try any of the build systems without installing anything, just run:
$ docker run --rm -it bkryza/compile-commands-gallery:20241202Then either run:
$ ./make.lua <subdirectory>or enter a specific subdirectory and setup the build system manually.
For instance, in case of cmake you should see the following output:
[root@a1dc70ba3f0b compile_commands_gallery]# ./make.lua cmake
============================
cmake
============================
**Version**
```bash
$ cmake --version
cmake version 3.31.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
```
**Instructions**
Create the following files:
`CMakeLists.txt`:
```
cmake_minimum_required(VERSION 3.15)
project(hello)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(hello src/hello.cc)
```
Execute the following command:
```bash
$ cmake -S . -B debug
```
-- The C compiler identification is Clang 19.1.4
-- The CXX compiler identification is Clang 19.1.4
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang-19 - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++-19 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /compile_commands_gallery/cmake/debug
**Result**
```json
[
{
"directory": "/compile_commands_gallery/cmake/debug",
"command": "/usr/bin/clang++-19 -o CMakeFiles/hello.dir/src/hello.cc.o -c /compile_commands_gallery/cmake/src/hello.cc",
"file": "/compile_commands_gallery/cmake/src/hello.cc",
"output": "CMakeFiles/hello.dir/src/hello.cc.o"
}
]
```
4188 warnings generated.
MIT License
Copyright (c) [2024] [Bartek Kryza <bkryza@gmail.com>]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.