Skip to content

Building AST extractor

Miguel Saldivar edited this page Feb 5, 2018 · 4 revisions

NOTE: On macOS and Linux, you can use build_ast_extractor.py to perform these steps.

Building this ast-extractor currently requires an LLVM project checkout. I haven't sliced the portion of the CMake build system that LLVM uses to build its tools. These instruction use the directory ~/clang-llvm because the tutorial does.

This build process is based on the following tutorial: https://clang.llvm.org/docs/LibASTMatchersTutorial.html

CMake supports multiple build system targets. The tutorial uses ninja and locally I've been using XCode to build.

The extractor uses tinycbor for CBOR serialization: https://github.com/01org/tinycbor I installed this into /usr/local to make it available locally.

Initial source checkout

mkdir ~/clang-llvm && cd ~/clang-llvm
git clone http://llvm.org/git/llvm.git
cd llvm/tools
git clone http://llvm.org/git/clang.git
cd clang/tools
git clone http://llvm.org/git/clang-tools-extra.git extra

Here's where I added the ast-extractor source directory to the llvm build tree.

$ cd ~/clang-llvm/llvm/tools/clang/tools/extra
$ echo 'add_subdirectory(ast-extractor)' >> CMakeLists.txt
$ ln -s $PATH_TO_ast_extractor

Building with Ninja

Ninja is a command-line too that seems similar to make.

cd ~/clang-llvm
mkdir build && cd build
cmake -G Ninja ../llvm -DLLVM_BUILD_TESTS=ON  # Enable tests; default is off.
ninja
ninja check       # Test LLVM only. (optional)
ninja clang-test  # Test Clang only. (optional)
ninja ast-extractor

Building with XCode

$ cd ~/clang-llvm
$ mkdir build-xcode
$ cd build-xcode
$ cmake -G Xcode ../llvm/
$ open LLVM.xcodeproj

Debugging the AST Extractor

The ast-extractor uses LLVMs debug macros. To enable debug output add -debug-only=ast-extractor to the command line invocation.