Ubuntu download source code clang + llvm+lldb compile + install

[This article may involve installing the following tools on Ubuntu:]

 A、g + +

B. gcc

C. make

D. cmake

E. clang(10.0.1)(required)

 F, llvm(10.0.1)(required)

G, lldb(10.0.1)(optional)

H. libcxx(10.0.1)(required)

I. libcxx abi(10.0.1)(required)

J. compiler-rt (optional)

【Tired】

I currently have two virtual machines: A. Ubuntu [ubuntu-20.04.1-desktop-amd64.iso], B. fedora [Fedora-Workstation-Live-x86_64-32-1.6.iso]. A can share files with the physical machine normally, but lacks a development environment; B can set up a development environment very conveniently, but there are problems with file sharing with the physical machine (the virtual machine has set up file sharing, but the virtual machine cannot be mounted, which is why 1. Manually mounting a Windows 10 shared folder, but various problems occurred (syntax, correct, password correct, firewall turned off, etc.) and still could not be mounted using mount cifs. After struggling for a long time, I finally chose to give up.) Choose A to build the development environment.

【1. Other tutorials】

llvm + clang source code installation tutorial [http://www.nagain.com/activity/article/4/]

The tutorial uses svn to download the source code and then compile and install it. This article downloads the source code directly from the official website and installs it. It is not svn. Why not use svn? 1. At present, svn detects various timeouts in the project and cannot fully download the source code; 2. I have prepared a T-zip, which has been tested and found to be useless.

[2. Source code download]

Website: [https://releases.llvm.org/download.html]

The source code has these:

I have chosen here:

1 llvm-project monorepo source code (.sig)
2 LLVM source code (.sig)
3 Clang source code (.sig)
4 compiler-rt source code (.sig)
5 libc++ source code (.sig)
6 libc++abi source code (.sig)
7 LLDB Source code (.sig)

Oh, I forgot, there is also [CMake], I downloaded: [cmake-3.18.2-linux-x86_64.sh]

[Why not use the command to install directly on the virtual machine? slow! slow! slow! This step is even more tiring: Physical machine C downloads the source code required above, which is extremely slow: it is expected to take several hours to download, or the IDM download fails to 99%. Some people say that it is useless to prepare a ladder. The T-zip is ready, but there is no change (after watching the YouTube video of English with Lucy, the download is still less than 20kb/s). You will definitely think of modifying the hosts and updating to the latest hosts, but it still has no effect (I wrote a program myself to get the latest version of hosts regularly every day). So, I changed my Mac to perform the download. What I didn’t expect was: the download speed was so fast that the broadband was full. Originally, downloading on a physical machine C took several hours or even longer, but downloading the required source code on a Mac took less than 2 minutes, and no T files were prepared.

Maybe you will say that using the command: sudo apt-get install clang-7.0 is not good? Well, I tried switching the image to images from several major domestic manufacturers. The download was extremely slow, not as fast as when I downloaded overseas images. When I open the T-zip on physical machine C, the download is still very slow. Phew, I’m really tired. Another option is to use svn to check out directly. Both Ubuntu and physical machine C show a timeout, and the project cannot be completely checked out. It is useless even if the T subsystem is hung up. )

All kinds of twists and turns. Why is it so difficult to download in China? Could it be that GFW2.0 has been upgraded to 3.0? Why can mac downloads fill up the bandwidth? 】

It’s too far. Back to text

【3. gcc, g++, make installation】

A. Put the downloaded source code into the virtual machine

B. Install gcc, g++, make: command (maybe the image download is slow, I have been waiting for a long time anyway):

$ sudo apt-get install gcc
$ sudo apt-get install g++
$ sudo apt-get install make 

[4. cmake source code installation]

A. Install cmake from the source code, decompress the source code, use the terminal to open the decompressed folder, and execute the command:

$ cd to/path/cmake_src/
$ sh cmake-3.18.2-linux-x86_64.sh # Unzip and get the compiled file<br>

B. Add cmake to the environment variables. This will stop reinventing the wheel. Reminder: Use the command [echo $SHELL] to check which SHELL is used, and then add cmake to the configuration file of the corresponding shell, usually the [.bashrc] file configuration under [home] ( Note, modify the file permissions); If you use [oh-my-zsh], it is [.zshrc] under [home]. Add a pointer to the bin directory under the decompression directory, and add the following two lines to the shell configuration file:

#cmake
export PATH= xxx/bin:$PATH  # xxx = cmake decompression directory

one example:

#cmake
export PATH=/home/google/cmake/bin:$PATH

C. After saving, use the command: [source configuration file] to refresh. Or log out or restart. For example, if I am using oh-my-zsh, use the following command to refresh the configuration:

source ~/.zshrc

To check whether the installation is successful, you can use the command [cmake –version] to check the results:

【5. Compile llvm】

Follow the tutorial [http://www.nagain.com/activity/article/4/], put the downloaded source code in the corresponding directory, and execute the command to compile.

[Note] If an error similar to [Killed (program cc1plus)] occurs, it means that the memory resources are insufficient. Please refer to [https://blog.csdn.net/u011897411/article/details/89742008] Create a partition (my 2G memory has been exhausted and is not enough, so I increased it to 4G and created a partition. The size of the partition depends on personal circumstances)

A. Unzip the lllvm source code

B. Place the decompressed source code of clang into the tools folder of llvm: llvm/tools/clang stores the decompressed files as follows:

C. Place the source code decompressed by compiler-rt in the projects folder of llvm: lllvm/projects/compiler-rt stores the decompressed files.

D. Extract libcxx to the projects folder in the llvm directory:

E. Extract libcxx abi to the projects folder in the llvm directory:

result:

G. Extract the lldb source code to the tools directory under the llvm directory:

H. Return to the decompressed directory of llvm and create build as the output directory: Execute the cmake build command:

1 cd to/path/llvm
2 mkdir build & amp; & amp; cd build
3 cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release .. 

I. Execute make -j

1 make -j 4<br>

Wait patiently for the compilation results. (12/09/2020 01:45 Waiting for compilation results)

J. As soon as I woke up, everything was compiled.

. . . . . . . .
[98%] Built target clangFrontendTool
Scanning dependencies of target c-index-test
[98%] Building C object tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/c-index-test.c.o
[98%] Building CXX object tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/core_main.cpp.o
[98%] Linking CXX shared module ../../../../../../lib/CheckerOptionHandlingAnalyzerPlugin.so
[98%] Linking CXX shared module ../../../../../../lib/CheckerDependencyHandlingAnalyzerPlugin.so
[98%] Linking CXX shared module ../../../../../../lib/SampleAnalyzerPlugin.so
[98%] Built target CheckerOptionHandlingAnalyzerPlugin
Scanning dependencies of target clang-cpp
[100%] Building CXX object tools/clang/tools/clang-shlib/CMakeFiles/clang-cpp.dir/clang-shlib.cpp.o
[100%] Linking CXX shared library ../../../../lib/libclang-cpp.so
[100%] Built target CheckerDependencyHandlingAnalyzerPlugin
Scanning dependencies of target c-arcmt-test
[100%] Building C object tools/clang/tools/c-arcmt-test/CMakeFiles/c-arcmt-test.dir/c-arcmt-test.c.o
[100%] Linking CXX executable ../../../../bin/c-arcmt-test
[100%] Built target c-arcmt-test
Scanning dependencies of target clang-check
[100%] Building CXX object tools/clang/tools/clang-check/CMakeFiles/clang-check.dir/ClangCheck.cpp.o
[100%] Built target SampleAnalyzerPlugin
Scanning dependencies of target clangDependencyScanning
[100%] Linking CXX static library ../../../../../lib/libclangDependencyScanning.a
[100%] Built target clangDependencyScanning
Scanning dependencies of target clang
[100%] Building CXX object tools/clang/tools/driver/CMakeFiles/clang.dir/driver.cpp.o
[100%] Building CXX object tools/clang/tools/driver/CMakeFiles/clang.dir/cc1_main.cpp.o
[100%] Linking CXX executable ../../../../bin/c-index-test
[100%] Linking CXX executable ../../../../bin/clang-check
[100%] Built target clang-cpp
Scanning dependencies of target clang-scan-deps
[100%] Building CXX object tools/clang/tools/clang-scan-deps/CMakeFiles/clang-scan-deps.dir/ClangScanDeps.cpp.o
[100%] Built target c-index-test
[100%] Building CXX object tools/clang/tools/driver/CMakeFiles/clang.dir/cc1as_main.cpp.o
[100%] Building CXX object tools/clang/tools/driver/CMakeFiles/clang.dir/cc1gen_reproducer_main.cpp.o
[100%] Built target clang-check
[100%] Linking CXX executable ../../../../bin/clang
[100%] Linking CXX executable ../../../../bin/clang-scan-deps
[100%] Built target clang-scan-deps
[100%] Built target clang

K. Check the version information of clang and clang++,

[6. Add clang and clang++ to environment variables]

A. I am using the oh-my-zsh beautified terminal, so I need to add the following command to [.zshrc] under its corresponding configuration file [home]:

# clang clang++
export PATH=to/llvm_path/llvm-10.0.1/build/bin:$PATH

B. The addition is successful. Type the command [clang + + –version] and output:

C. Code testing

[7. Recompile llvm]

Use clang to recompile llvm. Go to the [llvm/build directory and command:

CC=clang CXX=clang + + cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..