LLVM基础学习:LLVM的编译安装和基本使用

 

LLVM的编译安装和基本使用

时间:20220608,版本:V0.1

作者:robotech_erx

1.LLVM的组成

The LLVM Core libraries :LLVM核心库

Clang :多重含义,编译器前端,或者是llvm的同义词;

LLDB:调试器;

libc++:C++的高性能实现,支持C++11和C++14;

OpenMP:多核并行程序设计方案。;

Polly:cache-locality optimizations as well as auto-parallelism。

Libclc:The libclc project aims to implement the OpenCL standard library.

Klee:符号执行引擎,以后单独介绍。

还有其他很多。花点时间翻翻,能找到不要惊喜。

2.Apt 安装

On Ubuntu focal(20.04), you can install modern LLVM from the official repository:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -

sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"

sudo apt-get update

sudo apt-get install -y llvm-13 llvm-13-dev libllvm13 llvm-13-tools clang-13 libclang-common-13-dev libclang-13-dev libmlir-13 libmlir-13-dev

This will install all the required header files, libraries and tools in /usr/lib/llvm-13/.

各个版本的服务器地址等参考:https://apt.llvm.org/

3.下载安装

到官网下载预先编译的文件,设置一下环境变量。

地址:https://releases.llvm.org/download.html

4.编译安装

(1)需要的工具

$ sudo apt-get install build-essential(安装Gcc g++ 这些,要求GCC >=7.1.0,也可以apt安装一个clang来编译llvm)

$ sudo apt install libncurses5 (这个也是需要的,llvm-config等用的到)

$ sudo apt install cmake  #3.16.3,需要CMake >=3.13.4

$ sudo apt install ninja-build(是官网推荐的工具,取代make。Building with ninja significantly improves your build time, especially with incremental builds, and improves your memory usage。)

其他可选工具:

python >=3.6 Automated test suite

可选,LLVM自带的测试用例的执行需要python

zlib >=1.2.3.4 Compression library,可选

(2)Clone 代码

Checkout LLVM (including related subprojects like Clang):

$ git clone https://github.com/llvm/llvm-project.git

The above command is very slow. It can be made faster by creating a shallow clone. Shallow clone saves storage and speeds up the checkout time. This is done by using the command:

$ git clone --depth=1 https://github.com/llvm/llvm-project.git (using this only the latest version of llvm can be built)

For normal users looking to just compile, this command works fine. But if someone later becomes a contributor, since they can't push code from a shallow clone, it needs to be converted into a full clone:

$ cd llvm-project

$ git fetch --unshallow

If you want to get a specific release (as opposed to the most recent revision), you can check out a tag after cloning the repository. E.g., git checkout llvmorg-6.0.1 inside the llvm-project directory created by the above command. Use git tag -l to list all of them.需要完整克隆。

(3)Build

Build LLVM and Clang:

$ cd llvm-project

$ mkdir build   (in-tree build is not supported)

$ cd build

$ cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm

$ make  # or cmake --build .

 

编译完成后,The object files and the binaries are placed in the ~/llvm/build directory.The binaries for LLVM tools are placed in ~/llvm/build/bin.

 

说明一下cmake的参数们:

 

../llvm

../llvm是源码目录下的子目录,包含CMakeList.txt文件。

-DCMAKE_BUILD_TYPE=type

Controls optimization level and debug information of the build. For more detailed information see CMAKE_BUILD_TYPE (https://llvm.org/docs/CMake.html)。Type的类型有:Release,Debug,RelWithDebInfo,MinSizeRel。Debug一般是为了开发,普通使用Release就行了。如果是Debug,请准备足够的内存和硬盘空间。

注意,不要轻易选择编译Debug版本,如果是选择Debug,由于llvm确实太大了,RAM和硬盘的消耗都十分恐怖,我在一个60G的虚拟机里编译,10分钟不到,硬盘就占满了。然后报告编译失败。(报告这个错误 collect2: fatal error: ld terminated with signal 9 [Killed])。如果要编译Debug版本,内存和硬盘空间务必留足了(32G ram,150G磁盘)。

-DLLVM_TARGETS_TO_BUILD

Set this equal to the target you wish to build. You may wish to set this to X86; however, you will find a full list of targets within the llvm-project/llvm/lib/Target directory.

-DLLVM_TARGETS_TO_BUILD=X86. (注意是大写,x86会报错)

-DLLVM_TARGETS_TO_BUILD=host. 这也是可以的,编译本机的平台。

 

-DLLVM_ENABLE_PROJECTS

semicolon-separated list of the LLVM subprojects you’d like to additionally build.Can include any of: clang, clang-tools-extra, lldb, compiler-rt, lld, polly, or cross-project-tests.

-DLLVM_ENABLE_RUNTIMES

Set this equal to the runtimes you wish to compile (e.g. libcxx, libcxxabi, etc.) If compiling more than one runtime, separate the items with a semicolon. Should you run into issues with the semicolon, try surrounding it with single quotes.

For example, to build LLVM, Clang, libcxx, and libcxxabi, use -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi".

-DCMAKE_INSTALL_PREFIX=directory

Specify for directory the full pathname of where you want the LLVM tools and libraries to be installed (default /usr/local).执行make install之类的安装才会执行复制。显然我们不会这么干的~。

注意路径里must not contain ~ (tilde) to refer to your home directory as it won't be expanded. Use $HOME or an absolute path instead.

除了直接调用make编译,开可以使用CMake调用相应工具:

 

$ cmake --build .     #--build option is a portable why to tell cmake to invoke the underlying build tool (make, ninja, xcodebuild, msbuild, etc.)

$ cmake --build . --target install # build并安装

 

添加环境变量

add the following line to the ~/.bashrc file at the end of the file.
export PATH=$PATH:~/llvm/build/bin/

$ source  ~/.bashrc
Restart the terminal for the change to take effect.

(4)使用ninja来编译

使用ninja能有效降低内存和硬盘的占用,特别是增量编译的速度很快。过程和使用make一样基本相同,只是生成工程文件的时候要使用 -G参数指名是Ninja,最后启动编译的时候使用ninja命令:

$ cd llvm-project

$ mkdir build

$ cd build

$ cmake -G Ninja -DLLVM_ENABLE_PROJECTS=clang  -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=host ../llvm

$ ninja

$ ninja clang         #This will build just clang.

$ ninja check-clang   #This will run the clang tests.

(5)生成其他IDE (例如eclipse )的工程文件

CMake可以生成多种IDE的工程文件,甚至能够同时生成不同工具的。那我们可以同时生成Eclipse 和Ninja的,使用Ninja编译代码,使用IDE来浏览查看代码。

cd ~/llvm/

mkdir build

cd build

cmake -G “Eclipse CDT4 - Ninja” -LLVM_TARGETS_TO_BUILD=host ../llvm/

ninja

Since we asked CMake to create the Eclipse with Ninja build system, along with the ninja build files it also generated Eclipse project files that we can use to open the codebase in Eclipse. To import the LLVM project in Eclipse, do the following:

  1. Open Eclipse
  2. File -> Import -> General -> Existing Projects into Workspace
  3. Choose ~/llvm/buildas the root directory.

(6)Cmake 支持的 generators

(cmake  -help就能看到)

The following generators are available on this platform (* marks default):

* Unix Makefiles           = Generates standard UNIX makefiles.

  Green Hills MULTI       = Generates Green Hills MULTI files  (experimental, work-in-progress).

  Ninja                             = Generates build.ninja files.

  Watcom WMake         = Generates Watcom WMake makefiles.

  CodeBlocks - Ninja      = Generates CodeBlocks project files.

  CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.

  CodeLite - Ninja             = Generates CodeLite project files.

  CodeLite - Unix Makefiles    = Generates CodeLite project files.

  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.

  Sublime Text 2 - Unix Makefiles  = Generates Sublime Text 2 project files.

  Kate - Ninja                 = Generates Kate project files.

  Kate - Unix Makefiles    = Generates Kate project files.

  Eclipse CDT4 - Ninja      = Generates Eclipse CDT 4.0 project files.

  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.

 

如果是VScode,安装常用的C++插件后,感觉已经很方便了。

 

(7)关于交叉编译

https://llvm.org/docs/GettingStarted.html#cross-compiling-llvm

https://llvm.org/docs/HowToCrossCompileLLVM.html

https://clang.llvm.org/docs/CrossCompilation.html

5.简单使用

https://llvm.org/docs/GettingStarted.html#an-example-using-the-llvm-tool-chain

6.参考

https://llvm.org/docs/tutorial/

https://llvm.org/docs/tutorial/index.html

https://llvm.org/docs/CommandGuide/index.html

https://llvm.org/docs/CMake.html

https://www.cs.utexas.edu/~pingali/CS380C/2019/assignments/llvm-guide.html

 

系列llvm教程

Series: Creating the Bolt Compiler

https://mukulrathi.com/create-your-own-programming-language/intro-to-compiler/

https://tomassetti.me/a-tutorial-on-how-to-write-a-compiler-using-llvm/

https://llvm-tutorial-cn.readthedocs.io/en/latest/chapter-1.html 官方教程的中文版

 

posted @ 2022-06-13 12:09  robotech_erx  阅读(5708)  评论(0编辑  收藏  举报

本文版权归作者robotech_erx,转载请注明出处:https://www.cnblogs.com/robotech/