环境

系统:阿里云镜像Ubuntu16
依赖库

版本备注
gnu make3.79, 3.79.1Makefile/build processor
gcc>=5.1.0C/C++ compiler
python>=2.7用于运行测试套件
zlib>=1.2.3.4可选,为选定的LLVM工具添加了压缩/解压缩功能
CMakeStandard build process uses CMake

编译过程

1. 下载 LLVM project

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

2. 编译LLVM and Clang

  • cd llvm-project
  • mkdir build (llvm为了防止编译的中间结果分布在码源目录中,影响码源的结构。因此不支持目录内编译。需要在码源目录外创建额外的编译目录。)
  • cd build
  • cmake -DLLVM_ENABLE_PROJECTS=clang -G “Unix Makefiles” …/llvm

cmake -G [options] …/llvm
有些可选项:
-DLLVM_ENABLE_PROJECTS=’…’ — semicolon-separated list of the LLVM subprojects you’d like to additionally build. Can include any of: clang, clang-tools-extra, libcxx, libcxxabi, libunwind, lldb, compiler-rt, lld, polly, or debuginfo-tests.
For example, to build LLVM, Clang, libcxx, and libcxxabi, use -DLLVM_ENABLE_PROJECTS=“clang;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).
-DCMAKE_BUILD_TYPE=type — Valid options for type are Debug, Release, RelWithDebInfo, and MinSizeRel. Default is Debug.
-DLLVM_ENABLE_ASSERTIONS=On — Compile with assertion checks enabled (default is Yes for Debug builds, No for all other build types).
其实不止这些,具体可以参考llvm的官方文档中的cmake那一部分

  • make
    想编译更快的话,使用 make -j n,n表示线程数,自选
    通过上述命令编译后,LLVM and Clang 都是debug mode。
    如果只是想用clang开发的话,把make改成make clang就行了,这样就只会编译clang部分

以上为官方给出的编译参考,但是这样有两个问题,第一编译时间长,第二对编译内存需求高,32G的运存应该也会跑满。


对于个人用户来说,如果机器的性能并不是特别好的话,使用上述步骤编译可能会出现跟我一样memory exhausted的情况。
参考stack overflow上的解决方法:

  1. Add more RAM to your VM, or
  2. Use gold instead of ld as a linker, or
  3. Build Release(-DCMAKE_BUILD_TYPE=Release), not Debug build

个人对上述方法的理解
增加内存的话,在linux系统上可以使用swap分区的方法增加内存。
gold linker在编译大型程序时会比ld linker占用更小的内存并且编译速度更快
同时,
为了节省编译时间,可以给cmake中加上-DLLVM_TARGETS_TO_BUILD=X86,让这次编译只编译X86平台。

所以将cmake的命令改为如下

cmake -DCMAKE_BUILD_TYPE=Release --enable-optimized --enable-targets=host-only  -DLLVM_ENABLE_PROJECTS=clang -DLLVM_USE_LINKER=gold -G "Unix Makefiles" ../llvm

–enable-optimized 打开优化,默认情况下是关闭的。这样会生成大量 debug 信息,以致于产生的文件可能高达 9.4 G 之多,这可能很快耗尽磁盘空间导致编译失败。

–enable-targets=host-only 选择目标平台,默认情况下会生成所有平台的。这里设置成 host-only 只选择本机即可。

其他不做修改
(因为默认是安装debug版本的,看了下debug版和release版的区别,感觉release版本对于我来说就够了,而且还节约了空间,果断改release版本)

3. 测试

在llvm-project/build/bin路径下测试

clang --help
clang file.c -fsyntax-only (check for correctness)
clang file.c -S -emit-llvm -o - (print out unoptimized llvm code)
clang file.c -S -emit-llvm -o - -O3
clang file.c -S -O3 -o - (output native machine code)     

make check-clang

总结

官方文档很全,内容很丰富。但是碰上具体问题,还是直接google解决更快。这个编译安装真的浪费了太多时间,编译一次时间很长就算了,还经常编译到一半就报错内存不够。使用swap分区给了10个G都不够,总的加起来有十多个G的运行内存还是报错。我就怀疑是不是自己gcc或者cmake的版本问题,换了很多个版本还是没有解决。o(╥﹏╥)o
后来听过一个大佬虚拟机给了4G还是6G内存就编译安装好了,我就很好奇,都是参考官方文档做的,怎么就不一样呢?后来看了他的博客,发现他用的cmake命令跟官方的都不一样,有些还看不懂为什么加这些,后来自己去网上再仔细查了查才知道为什么。
ps:Stack Overflow是个好网站!baidu,bing还是差了点意思,但有时又不方便上google的话,那么直接上Stack Overflow搜解决方案是个很不错的选择。

参考

https://github.com/llvm/llvm-project
http://llvm.org/docs/CMake.html
https://clheveningflow.github.io/2019/09/28/LLVM1/
https://stackoverflow.com/questions/25197570/llvm-clang-compile-error-with-memory-exhausted

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐