Ubuntu16.04 desktop 安装最新稳定版 LLVM

前期准备

要求(我用的virtualbox装的ubuntu16.04桌面版,直接官网下载的)。

  • 内存: 至少12GB,或者10GB内存+8GB交换内存
  • 磁盘:至少100GB
  • 稳定的网络
  • 时间:至少10个小时,取决于你系统硬件配置。我虚拟机分配了3个cpu,build的时候最开始是8个线程,后来崩了换成了4个线程继续build。

内存和磁盘一定要足够!
内存和磁盘一定要足够!
内存和磁盘一定要足够!

开始安装LLVM之前,需要保证以上所有要求。如果内存最多只能10GB,再加上8GB交换内存也可以。但是build的线程不要超过8个。然后是以下前提工作:

  1. 将链接器改成gold
$ cd /usr/bin
$ sudo rm ld
$ sudo cp -d gold ld
  1. 设置交换内存
    参考链接:https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04。不知道需不需要翻墙,这里补充一下步骤。
$ free -m
Filename                Type        Size    Used    Priority
Mem:
Swap:

交换分区实际上是牺牲磁盘空间来假装增加内存大小,所以需要根据本身系统的剩余磁盘空间来确定要设置的交换分区大小:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda         59G  1.3G   55G   3% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            2.0G   12K  2.0G   1% /dev
tmpfs           396M  312K  396M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.0G     0  2.0G   0% /run/shm
none            100M     0  100M   0% /run/user

创建交换分区,我设置了8GB:

$ sudo fallocate -l 8G /swapfile
$ ls -lh /swapfile
-rw-r--r-- 1 root root 8.0G Jun 16 17:19 /swapfile

启用新建的交换分区:

$ sudo chmod 600 /swapfile
$ ls -lh /swapfile
-rw------- 1 root root 8.0G Jun 16 17:19 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=e2f1e9cf-c0a9-4ed4-b8ab-714b8a7d6944
$ sudo swapon /swapfile
$ sudo swapon -s
$ free -m

这个时候执行free -m 就能看到swap分区变大了。如果想将这个分区设置成永久大小,后续操作请参考链接。

  1. 准备LLVM的编译环境
$ sudo apt install build-essential subversion cmake python3-dev libncurses5-dev libxml2-dev libedit-dev swig doxygen graphviz xz-utils

如果找不到某些软件,可以:

$ sudo apt update
$ sudo apt upgrade

不建议上来就upgrade,可能会导致某些系统软件不兼容导致系统不稳定。

  1. 修改svn工具的超时时间
    因为LLVM的源码很多很大,svn使用的时候很容易就超时。
    打开~/.subverion/servers文件,将
# http-timeout = 60

修改成

http-timeout = 6000

下载LLVM源码

参考链接:https://solarianprogrammer.com/2013/01/17/building-clang-libcpp-ubuntu-linux/
下载最新稳定的LLVM源码:

$ cd ~
$ mkdir llvm_all && cd llvm_all
$ svn co http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_900/final llvm
 
$ cd llvm/tools
$ svn co http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_900/final clang

$ cd ../..
$ cd llvm/projects
$ svn co http://llvm.org/svn/llvm-project/compiler-rt/tags/RELEASE_900/final compiler-rt
$ svn co http://llvm.org/svn/llvm-project/libcxx/tags/RELEASE_900/final libcxx
$ svn co http://llvm.org/svn/llvm-project/libcxxabi/tags/RELEASE_900/final libcxxabi
$ svn co http://llvm.org/svn/llvm-project/polly/tags/RELEASE_900/final polly
$ svn co http://llvm.org/svn/llvm-project/lld/tags/RELEASE_900/final lld
$ svn co http://llvm.org/svn/llvm-project/openmp/tags/RELEASE_900/final openmp
$ svn co http://llvm.org/svn/llvm-project/libunwind/tags/RELEASE_900/final libunwind

编译安装LLVM

参数设置:

  • 我后续想用llc生成dag文件,和graphviz结合查看dag图像。LLVM只有Debug版本才提供这些选项,所以编译选项中设置了DCMAKE_BUILD_TYPE=Debug, 这也导致编译LLVM需要非常多的内存。如果你只需要Release版本,内存8GB应该也是够用的.
  • 我的LLVM使用对象是x86和ARM,所以参数设置-DLLVM_TARGETS_TO_BUILD="X86;ARM"。如果没有这个设置,LLVM会为所有target编译。
  • 我不想要测试和样例,所以关闭了这几个选项。
  • 启用了共享库和benchmark
$ cd ~/llvm_all
$ mkdir build && cd build
$ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \ 
-DLLVM_ENABLE_ASSERTION=ON \ 
-DLLVM_TARGETS_TO_BUILD="X86;ARM" \ 
-DLLVM_BUILD_TESTS=OFF \ 
-DLLVM_BUILD_EXAMPLES=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_SHARED_LIBS=ON \
-DLLVM_BUILD_BENCHMARKS=ON \
-DLLVM_BUILD_DOCS=OFF \
-DCMAKE_INSTALL_PREFIX=/usr/local/clang_9.0.0 ../llvm
 
$ make -j 8 
$ sudo make install/strip

注意:在提供足够多内存且有交换内存的情况下,如果在编译过程中仍然出现can not allocate memory或者signal [9]这种跟内存错误有关系的情况,将make -j 8里的8减小到4或者2

clang加入到系统路径

$ cd ~
$ echo 'export PATH=/usr/local/clang_9.0.0/bin:$PATH' >> ~/.bashrc
$ echo 'export LD_LIBRARY_PATH=/usr/local/clang_9.0.0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
$ source ~/.bashrc

如果你用的oh-my-zsh,把~/.bashrc改成~/.zshrc

下载并编译安装lldb

我同样将编译参数改成了Debug

$ cd ~/llvm_all
$ svn co http://llvm.org/svn/llvm-project/lldb/tags/RELEASE_900/final lldb
$ mkdir build2 && cd build2
$ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local/clang_9.0.0 ../lldb
$ make -j 8
$ sudo make install/strip

如果你在运行cmake的报错

Found imcompatible python interpreter (xxx) and python libraries (xxx).

表示你系统中有俩版本python,然后cmake找到的python解释器和python库版本不统一。解决办法是cmake加参数指定python解释器版本(最低要求是python3.5,参考链接:https://runsisi.com/2019-08-28/cmake-choose-python)

$ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
-DPythonInterp_FIND_VERSION=3.5 \
-DPythonInterp_FIND_VERSION_MAJOR=3 \
-DCMAKE_INSTALL_PREFIX=/usr/local/clang_9.0.0 ../lldb

测试装好的LLVM

测试代码会打印出当前路径下所有文件的名字

$ cat test_fs.cpp
#include 
#include 
 
int main() {
    for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
        std::cout << file.path() << '\n';
    }
}
$ clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs
$ ./test_fs
"./test_fs"
"./test_fs.cpp"

LLVM和graphviz

LLVM可以生成dag文件,然后用graphviz打开,但是LLVM和lldb编译选项必须是Debug。

$ llc --help-hidden | grep dag
-filter-view-dags= - Only display the basic block whose name matches this for all view-*-dags options
-view-dag-combine-lt-dags - Pop up a window to show dags before the post legalize types dag combine pass
-view-dag-combine1-dags - Pop up a window to show dags before the first dag combine pass
-view-dag-combine2-dags - Pop up a window to show dags before the second dag combine pass

$ clang -ccl -emit-llvm test.cpp -o test.ll
$ llc --view-dag-combin1-dags test.ll
Writing 'tmp/dag._xxx.dot'... done
Trying 'xdg-open' program... Remember to erase graph file: /tmp/dag._xxx.dot

如果llc指令并没有生成.dot文件,添加选项-fast-isel=false

$ llc -fast-isel=false --view-dag-combin1-dags test.ll
Writing 'tmp/dag._xxx.dot'... done
Trying 'xdg-open' program... Remember to erase graph file: /tmp/dag._xxx.dot

然后系统会尝试打开.dot文件可能会报错

gvfs-open: /tmp/dag._xxx.dot: error opening location: No application is registered as bandling this file.

没关系,我们自己手动操作

$ dot -Tpng /tmp/dag._xxx.dat > test.png

然后直接打开这个png图像就可以了。

你可能感兴趣的:(Ubuntu16.04 desktop 安装最新稳定版 LLVM)