First of all to proceed with building of our own plugin for Clang we need to compile llvm and clang itself. It is possible to get official latest versions of llvm in following git repositories:
However, since I would have plugins to work with environment close to Xcode’s build infrastructure I would need to compile it against Apple’s version of Clang. To do so I will need to select appropriate distributive from Apple Open Source portal – http://opensource.apple.com
Lets perform following command:
clang --version Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) Target: x86_64-apple-darwin12.4.0 Thread model: posix
As we see from output we will need clang-425.0.24 which is available within Developer Tools version 4.6.
So, lets grab clang-425.0.24.tar.gz and unpack it in a folder ~/apple-llvm next step will be compiling it. I am going to use cmake and Xcode to do so. You can download cmake by following link – http://www.cmake.org/cmake/resources/software.html
cd ~/apple-llvm/src ./configure mkdir ./build cd ./build cmake -G Xcode -DCLANG_BUILD_EXAMPLES=1 ..
After operations above completed you will get LLVM.xcodeproj available in ~/apple-llvm/src/build folder. It will contain Clang and Clang examples with a plugin PrintFunctionNames included. Open the project and compile ALL_BUILD target. You can start you experiments with Clang plugins from this point.
Lets check generated plugin by running it against BrainF example from LLVM examples.
~/apple-llvm/src/build/bin/Debug/clang -cc1 -load ~/apple-llvm/src/build/lib/Debug/PrintFunctionNames.so -plugin print-fns ~/apple-llvm/src/examples/BrainF/BrainF.cpp
As result of such call you should get one error since we didn’t provide additional includes and a lot of “top-level-decl: ” output. which is result of plugin call which will looks like a following:
In file included from/my-plugin/llvm/examples/BrainF/BrainF.cpp:26: /my-plugin/llvm/examples/BrainF/BrainF.h:18:10: fatal error: 'llvm/IR/IRBuilder.h' file not found #include "llvm/IR/IRBuilder.h" ^ top-level-decl: "BrainF" top-level-decl: "__int8_t" top-level-decl: "__uint8_t" top-level-decl: "__int16_t" top-level-decl: "__uint16_t" top-level-decl: "__int32_t" top-level-decl: "__uint32_t" top-level-decl: "__int64_t" top-level-decl: "__uint64_t" top-level-decl: "__darwin_intptr_t" top-level-decl: "__darwin_natural_t" …
Good start! In next posts I will show how to use custom made clang with XCode and how to integrate plugin into the project to get instant benefits from what we just did.