Firstly, you need download and install CMake, a article about how to study CMake for new leaner.
Secondly, make directory and download the LLVM source code:
代码语言:javascript复制
//make directory
cd /opt
sudo mkdir llvm
sudo chown `whoami` llvm
cd llvm
export LLVM_HOME=`pwd`
//downlaod the source code, here i use the https urls, you can also use ssh:
git clone -b release_60 https://github.com/llvm-mirror/llvm.git llvm
git clone -b release_60 https://github.com/llvm-mirror/clang.git llvm/tools/clang
git clone -b release_60 https://github.com/llvm-mirror/clang-tools-extra.git llvm/tools/clang/tools/extra
git clone -b release_60 https://github.com/llvm-mirror/compiler-rt.git llvm/projects/compiler-rt
Pay Attention: if you have problems about permission you can do as followed
And then you can continue download~
Build the source code
代码语言:javascript复制
mkdir llvm_build
cd llvm_build
cmake -G Xcode ../llvm -DCMAKE_BUILD_TYPE:STRING=MinSizeRel
Open the generate project LLVM, choose Automatically Create Schemes and build the Clang and libClang Scheme, it will wait for a long time(near an hour)
Write your First Plugin
1.Input the code as follow in terminal
代码语言:javascript复制
cd /opt/llvm/llvm/tools/clang/tools
mkdir MyPlugin
if you have permission problem, you can do Pay Attention as above.
2.Modify the CMakeLists.txt file under /opt/llvm/llvm/tools/clang/tools path, add add_clang_subdirectory(MyPlugin) code at the bottom of CMakeLists.txt, the bottom of file will be like this:
代码语言:javascript复制
# We support checking out the clang-tools-extra repository into the 'extra'
# subdirectory. It contains tools developed as part of the Clang/LLVM project
# on top of the Clang tooling platform. We keep them in a separate repository
# to keep the primary Clang repository small and focused.
# It also may be included by LLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR.
add_llvm_external_project(clang-tools-extra extra)
# libclang may require clang-tidy in clang-tools-extra.
add_clang_subdirectory(MyPlugin)
3.Create CMakeLists.txt and MyPlugin.cpp files under MyPlugin directory.
Hack Xcode, donwload XcodeHacking.zip, you will have HackedBuildSystem.xcspec and HackedClang.xcplugin files, you must modify the HackedClang.xcspec file under HackedClang.xcplugin/Contents/Resources/ path, modify the ExecPath to "/opt/llvm/llvm_build/Debug/bin/clang":
代码语言:javascript复制
//The line will be like this:
ExecPath = "/opt/llvm/llvm_build/Debug/bin/clang";
Move files
代码语言:javascript复制
//cd to the XcodeHacking directory
cd (XcodeHacking directory)
//move two files
sudo mv HackedClang.xcplugin `xcode-select -print-path`/../PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins
sudo mv HackedBuildSystem.xcspec `xcode-select -print-path`/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Specifications
Restart Xcode, modify Compiler for C/C /Objective-C