Now it’s time to build our own clang standalone tool. The official LibTooling document provides an example code so that I was able to begin with it.
How to write CMakeLists.txt for the standalone clang tool?
Of course using the cmake is not required, but I wanted to go with it. At first I tried to use the CMakeLists which is included the rtags with few modifications, but it didn’t work well. Instead of digging it further, I tried another CMakeLists found and easily succeeded after adding more libs to link with.
However If you run the cmake, an error will be occurred indicating it failed to find the cmake file for llvm as below output:
➜ build$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
— The C compiler identification is AppleClang 220.127.116.1100181
— The CXX compiler identification is AppleClang 18.104.22.16800181
— Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
— Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc — works
— Detecting C compiler ABI info
— Detecting C compiler ABI info – done
— Detecting C compile features
— Detecting C compile features – done
— Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
— Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ — works
— Detecting CXX compiler ABI info
— Detecting CXX compiler ABI info – done
— Detecting CXX compile features
— Detecting CXX compile features – done
CMake Error at CMakeLists.txt:26 (find_package):
Could not find a package configuration file provided by “LLVM” with any of
the following names:
Add the installation prefix of “LLVM” to CMAKE_PREFIX_PATH or set
“LLVM_DIR” to a directory containing one of the above files. If “LLVM”
provides a separate development package or SDK, be sure it has been
— Configuring incomplete, errors occurred!
See also “/Users/hekim/Dropbox/clangtool/build/CMakeFiles/CMakeOutput.log”.
In such case, you need to define CMAKE_PREFIX_PATH which contains LLVMConfig.cmake for its subdirectory. The files are located at:
➜ build$ find ~/Documents/sources/clang-build -name LLVMConfig.cmake
So, I added the llvm&clang build output root directory(where the Makefile files are generated by cmake),
➜ build$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_PREFIX_PATH=/Users/hekim/Documents/sources/clang-build ..
— Found LLVM 4.0.0svn
— Using LLVMConfig.cmake in: /Users/hekim/Documents/sources/clang-build/lib/cmake/llvm
— User selected librarys = clangFrontend;clangSerialization;clangDriver;clangParse;clangRewriteFrontend;clangStaticAnalyzerFrontend;clangSema;clangAnalysis;clangEdit;clangAST;clangLex;clangBasic;clangTooling
— User selected components = mcparser;bitreader;support;mc;option
— Configuring done
— Generating done
— Build files have been written to: /Users/hekim/Dropbox/clangtool/build
➜ build$ make
Scanning dependencies of target example
[ 50%] Building CXX object CMakeFiles/example.dir/main.cpp.o
[100%] Linking CXX executable example
[100%] Built target example
➜ build$ ./example
example: Not enough positional command line arguments specified!
Must specify at least 1 positional argument: See: ./example – help
Then it succeeded.