Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Write at the front

Now when we go out for an interview, we cannot get around to starting optimization. How should we optimize our APP? LLVM must be understood before tuning, and the use of LLVM and Clang commands was introduced in the previous blog post.

This blog will teach you how to customize a Clang plug-in by hand.

2. LLVM download

Before writing the Clang plug-in, you need to download LLVM, which is a subproject of LLVM, and then compile LLVM.

2.1 download the LLVM

Cmake error cmake error cmake error cmake error Cmake error Cmake

Cmake error due to mirror address change, From the original into git clone git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm/llvm.git https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project.git, by making search LLVM above can also be found is indeed the address has changed. As shown below:

LLVM GitHub address: github.com/llvm/llvm-p…

Due to the limitation of our domestic network, we need to download the source code of LLVM with the help of image.

https://mirror.tuna.tsinghua.edu.cn/help/llvm-project.git/

  • downloadLLVM

2.1.1 Full version, including sub-warehouses

Use the following command to download the full LLVM version


git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project.git

Copy the code

2.1.2 Incomplete version, does not include sub-warehouse

Git clone mirrors.tuna.tsinghua.edu.cn/git/llvm-pr…

I have downloaded the complete version here, but I can’t find the corresponding warehouse for the incomplete version, but some people can download it, so far I can’t find the specific reason.

2.2 download the Clang

These steps are not required to download the full version from ⚠️, which is for operations that do not contain subrepositories

  1. inLLVMthetoolsDownload from directoryClang

cd llvm/tools
git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project/clang.git

Copy the code
  1. inLLVMtheprojectsDirectory, also need to go to downloadcompiler-rt.libcxx.libcxxabi.
cd .. /projects git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project/compiler-rt.git git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project/libcxx.git git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project/libcxxabi.gitCopy the code
  1. inClangthetoolsinstallclang-tools-extratool

cd clang/tools
git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project/clang-tools-extra.git

Copy the code

3. The LLVM compiler

Since the latest LLVM only supports Cmake compilation, we also need to install BREW and Cmake to support our compilation.

  • Check that it is installedcmake

Brew install cmake. Brew install cmake.

  • The installationcmake

Cmake installation error, for the above picture, we should try to reclaim permission, the solution is as follows:

  • First open the terminal and type:

sudo chown -R whoami:admin /usr/local/bin

  • Then the system will remind you to enter your password. Enter your password and press Enter.

sudo chown -R whoami:admin /usr/local/share

Now that cmake has been successfully installed, it’s time to compile LLVM.

3.1 Incomplete compilation

Mkdir llvm_build CD llvm_build go to llvm_build cmake -g Xcode.. / LLVM // compile into Xcode projectCopy the code

3.2 Full version compilation

The full version builds using the following command:

Llvm_build CD llvm_build llvm_build go to llvm_build cmake -g <generator> [options] .. / LLVM // compile into Xcode projectCopy the code
  • LLVM GitHub compiled and configured

A more common build system generator:

  • Ninja: Most LLVM developers use Ninja.
  • Unix Makefiles: used to generate parallel makefiles compatible with make.
  • Visual Studio: Used to generate Visual Studio projects and solutions.
  • Xcode: used to generateXcodeThe project.

-dllVM_enable_projects is the subproject that needs to be compiled. Here we need to add clang, Compiler-rt, libcxx, libcxxABI, clang-tools-extra.

cmake -G Xcode -DLLVM_ENABLE_PROJECTS='libcxx; libcxxabi; clang; clang-tools-extra; compiler-rt' -DLLDB_USE_SYSTEM_DEBUGSERVER=ON -DLLDB_TEST_COMPILER=clang++ -DCMAKE_OSX_SYSROOT=$OSX_COMMANDLINE_SDKROOT . /llvmCopy the code

The default shell should be set as ZSH. The command is as follows: Enter terminal to execute


echo 'export OSX_COMMANDLINE_SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" ' >> ~/.zshrc

Copy the code

Then execute:

source ~/.zshrc
Copy the code

But there are problems with my operation, as follows:

Press this operation does not work, I checked my computer default is/bin/zsh

Anyway, I haven’t found a way to solve the problem for a day. There may be a problem with the configuration of ZSH. Since incomplete download can’t be done, and complete compilation can’t be done, why don’t I integrate it and see if I can solve the problem? So I tried to do the following operation.

Because I downloaded the complete version of LLVM, which already contains all seed warehouses, as shown below:

The incomplete configuration process has been introduced at 👆 above, so I will follow this process and copy the sub-warehouse in the complete version to the corresponding directory.

  • theClangCopy tollvm/toolsdirectory
  • thecompiler-rt.libcxx.libcxxabi, copy to theLLVMtheprojectsdirectory
  • theclang-tools-extraTool copy toClangthetoolsinside

In fact, it is equivalent to the incomplete walkLLVMDownload process, just not to the network download, just my local copy of the need for sub-warehouse to the corresponding directory below, so now is all ready only owe dongfeng, the following 👇 is to witness the miracle of the moment.llvm-project/llvm_buildType in the following directorycmakeCompile command:

cmake -G Xcode .. /llvmCopy the code

Miraculously, it’s compiling, yes! You read it right. It doesn’t report an error.

Cmake compile command a car, began to compile, this process half an hour to start, depending on your computer performance. At this time the fan slowly began to ring, the CPU temperature rose straight line, as shown in the picture below feel it!

After more than half an hour, the compilation is completed, when I want to compile the successful interface screenshot down… The accident happened… When I was immersed in the joy of compiling success, the computer suddenly went black, hung up, the computer hung up, suddenly, the computer directly shut down…

Uncomfortable, want to cry, MY subconscious hand to touch the computer cooling area, good guy, I call good guy! It’s time to fry eggs. It’s fucking hot! The temperature was so high that the computer triggered the protection mechanism and shut down.

After a few minutes, when the computer temperature down, I reboot, open the LLVM compilation directory llVM_build, there is a successful compilation of Xcode project, which indicates that it is really a successful compilation, at this time there is still a little bit of comfort 😁. As shown below:

3.3 Engineering compilation of LLVM

Double-click LLVM to open the compiled project, the following popup window will pop up (it will pop up the first time)

The ⚠️ project “LLVM” contains a large number of targets, which may result in the automatic creation of a large number of schemes. If you prefer, you can instead just manually create the schema you need.

We select the second one and create it manually. The following screen pops up

Clang tooling Was required to be added. Select clang and clangTooling directly by entering the required quantity.

After adding clang and clangTooling, you will need to select clang and clangTooling in Xcode Scheme, respectively. This process will take a long time, and you will need to do something else to kill time.

  • clangToolingcompile

ClangTooling was relatively quick. It took me about half an hour to implement clangTooling. The memory was about 22G, and the CPU was in high load and temperature.

  • clangcompile

Clang takes a long time to compile, more than an hour. Due to the volume increase after compilation, the memory is seriously insufficient, so we issue a warning ⚠️. Even wechat issued a memory warning.

  • Wechat warns of lack of computer space ⚠️

No space left on device (No space left on device). I quickly uninstalled some things I didn’t need, freed up another five or six gigabytes, and started compiling again. Instead of starting from scratch, it compiled quickly because it had compiled before.

The LLVM project was compiled to a size of 23.38 GB, which is huge! Not so big!

4. To summarize

  • LLVMThe download address has changed. You can goGitHubSearch directly to find the latest address
  • LLVMCompilation precautions ⚠️ :
    • Make sure you leave enough space, just to be safe30G
    • It is important to plug in an external fan to cool your computer while compiling, otherwise it will shut down if it gets too hot.
  • myXcodeVersion isVersion 12.5
  • MacOS 11.4 Big Sur
  • The above content is for reference only, everyone’s computer environment is not the same, there may be differences
  • The above content is only compiled by myselfLLVMMaybe your process will be more smooth and will not have the problems I have encountered.

5. Write in the back

To be continued… Pay attention to me, more content continues to output!

In the next installment, we’ll start customizing the Clang plug-in (below) — writing the code

Stay tuned!

  • CSDN
  • The Denver nuggets
  • Jane’s book

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹