How To Setup Clang Tooling For LLVM
+Clang Tooling provides infrastructure to write tools that need syntactic and
+semantic infomation about a program. This term also relates to a set of specific
+tools using this infrastructure (e.g. clang-check
). This document
+provides information on how to set up and use Clang Tooling for the LLVM source
+code.
Introduction
+ + +Clang Tooling needs a compilation database to figure out specific build
+options for each file. Currently it can create compilation database from the
+compilation_commands.json
file, generated by CMake. When invoking
+clang tools, you can either specify a path to a build directory using a command
+line parameter -p
or let Clang Tooling find this file in your
+source tree. In either case to use clang tools you need to configure your build
+using CMake.
Setup Clang Tooling Using CMake and Make
+ + +If you intend to use make to build LLVM, you should have CMake 2.8.6 or later +installed (can be found here).
+First, you need to generate Makefiles for LLVM with CMake. You need to make +a build directory and run CMake from it:
++ mkdir your/build/directory + cd your/build/directory + cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources ++ +
If you want to use clang instead of GCC, you can add
+-DCMAKE_C_COMPILER=/path/to/clang
+ -DCMAKE_CXX_COMPILER=/path/to/clang++
.
+You can also use ccmake, which provides a curses interface to configure CMake
+variables for lazy people.
As a result, the new compile_commands.json
file should appear in
+the current directory. You should link it to the LLVM source tree so that Clang
+Tooling is able to use it:
+ ln -s $PWD/compile_commands.json path/to/llvm/source/ ++ +
Now you are ready to build and test LLVM using make:
++ make check-all ++ + +
Using Clang Tools
+ + +After you completed the previous steps, you are ready to run clang tools. If
+you have a recent clang installed, you should have clang-check
in
+$PATH. Try to run it on any .cpp file inside the LLVM source tree:
+ clang-check tools/clang/lib/Tooling/CompilationDatabase.cpp ++ +
(Experimental) Using Ninja Build System
+ +Optionally you can use Ninja +build system instead of make. It is aimed to make your builds faster. Currently +this step will require building Ninja from sources and using development version +of CMake.
+To take advantage of using Clang Tools along with Ninja build you need at +least CMake 2.8.9. At the moment when this how-to is being written, CMake 2.8.9 +is still under development, so you can get latest development sources and build +it yourself:
++ git clone git://cmake.org/cmake.git + cd cmake + ./bootstrap + make + sudo make install ++ +
Having the correct version of CMake, you can clone Ninja git repository and +build Ninja from sources:
++ git clone git://github.com/martine/ninja.git + cd ninja/ + ./bootstrap.py ++
This will result in a single binary ninja
in the current
+directory. It doesn't require installation and can be just copied to any
+location inside $PATH
, say /usr/local/bin/
:
+ sudo cp ninja /usr/local/bin/ + sudo chmod a+rx /usr/local/bin/ninja ++
After doing all this, you'll need to generate Ninja build files for LLVM with +CMake. You need to make a build directory and run CMake from it:
++ mkdir your/build/directory + cd your/build/directory + cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources ++ +
If you want to use clang instead of GCC, you can add
+-DCMAKE_C_COMPILER=/path/to/clang
+ -DCMAKE_CXX_COMPILER=/path/to/clang++
.
+You can also use ccmake, which provides a curses interface to configure CMake
+variables for lazy people.
As a result, the new compile_commands.json
file should appear in
+the current directory. You should link it to the LLVM source tree so that Clang
+Tooling is able to use it:
+ ln -s $PWD/compile_commands.json path/to/llvm/source/ ++ +
Now you are ready to build and test LLVM using Ninja:
++ ninja check-all ++
Other target names can be used in the same way as with make.
+