2020年11月27日

Ubuntu 18.04 LTSで最新のClang12をビルドする方法

Ubuntu 18.04 の APT でインストールできるパッケージでは、Clang のビルド要件を満たせなくなっているので、その解決方法です。



公式サイトを参考にしつつ、一つ一つ確認して行きます。ソフトウェアのバージョンが古いと、例えビルドは通っても正常に動作しないことがあるので重要です。

https://llvm.org/docs/GettingStarted.html#requirements
まず、基本的なパッケージをインストールします。
$ sudo apt-get install build-essential git python python3 zip zlib1g-dev xz-utils wget
以下のシェルスクリプトを動かして、バージョンを確認します。zlib のバージョン確認はこのサイトを参考にさせていただきました。
$ cat ./test-clang-dep.sh
#!/bin/bash -e

gcc -v 2>&1 | tail -n 1
python --version
python3 --version

ar --version 2>&1 | head -n 1
bzip2 --version 2>&1 | head -n 1
bunzip2 --version 2>&1 | head -n 1
chmod --version 2>&1 | head -n 1
cat --version 2>&1 | head -n 1
cp --version 2>&1 | head -n 1
date --version 2>&1 | head -n 1
# echo
egrep --version 2>&1 | head -n 1
find --version 2>&1 | head -n 1
grep --version 2>&1 | head -n 1
gzip --version 2>&1 | head -n 1
gunzip --version 2>&1 | head -n 1
install --version 2>&1 | head -n 1
mkdir --version 2>&1 | head -n 1
mv --version 2>&1 | head -n 1
ranlib --version 2>&1 | head -n 1
rm --version 2>&1 | head -n 1
sed --version 2>&1 | head -n 1
tar --version 2>&1 | head -n 1
# test
unzip -v 2>&1 | head -n 1
zip -v 2>&1 | head -n 1

wget http://www.s-yata.jp/docs/zlib/zlib-test.c
gcc -Wall -O2 -std=c99 zlib-test.c -lz -o zlib-test
./zlib-test --help
rm zlib-test*
このシェルスクリプトが動いた時点で sh(bash)は存在しますし、bash ビルトインの echo と test もあるはずです。

現時点(2020/11/27)での結果です。bzip2 と bunzip2、grep と egrep は同じパッケージなので、同じ出力になってます。
$ ./test-clang-dep.sh
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
Python 2.7.17
Python 3.6.9
GNU Make 4.1
GNU ar (GNU Binutils for Ubuntu) 2.30
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.
chmod (GNU coreutils) 8.28
cat (GNU coreutils) 8.28
cp (GNU coreutils) 8.28
date (GNU coreutils) 8.28
grep (GNU grep) 3.1
find (GNU findutils) 4.7.0-git
grep (GNU grep) 3.1
gzip 1.6
gunzip (gzip) 1.6
install (GNU coreutils) 8.28
mkdir (GNU coreutils) 8.28
mv (GNU coreutils) 8.28
GNU ranlib (GNU Binutils for Ubuntu) 2.30
rm (GNU coreutils) 8.28
sed (GNU sed) 4.4
tar (GNU tar) 1.29
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
--2020-11-27 16:59:48--  http://www.s-yata.jp/docs/zlib/zlib-test.c
www.s-yata.jp (www.s-yata.jp) をDNSに問いあわせています... 185.199.111.153, 185.199.110.153, 185.199.109.153, ...
www.s-yata.jp (www.s-yata.jp)|185.199.111.153|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 10362 (10K) [text/x-c]
`zlib-test.c' に保存中

zlib-test.c         100%[===================>]  10.12K  --.-KB/s    時間 0.001s

2020-11-27 16:59:48 (14.6 MB/s) - `zlib-test.c' へ保存完了 [10362/10362]

Usage: ./zlib-test [OPTION]... [FILE]...
Version: zlib-1.2.11
...
現時点での Clang の要求を満たしていることを確認します。
GCC	>=5.1.0 (7.5.0 なので OK)
python	>=2.7 (2.7.17 なので OK。ただし実際に使われるのは python3 のようです。)
zlib	>=1.2.3.4 (1.2.11 なので OK)
GNU Make	3.79, 3.79.1 (4.1 なので OK)
他はとりあえず入っていれば OK だと思います。

問題は CMake です。あえてここまで apt-get でインストールしていないのは古いからです。
$ sudo apt-get install cmake
$ cmake --version
cmake version 3.10.2
もしインストール済みの場合はアンインストールしておいてください。
$ sudo apt remove cmake
公式サイトからバイナリをダウンロードしてシンボリックリンクを張ります。今回は Downloads にしますが、どこでも構いません。
$ cd ~/Downloads
$ wget https://github.com/Kitware/CMake/releases/download/v3.18.4/cmake-3.18.4-Linux-x86_64.sh
$ chmod +x cmake-3.18.4-Linux-x86_64.sh
$ ./cmake-3.18.4-Linux-x86_64.sh
$ sudo update-alternatives --install /usr/bin/cmake cmake /home/kmc/Downloads/cmake-3.18.4-Linux-x86_64/bin/cmake 100
$ cmake --version
cmake version 3.18.4
Clang をビルドする際には、できるだけ新しい Clang が必要です。GCC だと一見ビルドは通りますが、正常動作しない場合があります。もし今後 clang-11 や clang-12 が入ったら、そちらを使う方が良いでしょう。また、/usr/bin/c++ を使用する場合もあるようなので、そちらにもシンボリックリンクを張っておきます。
$ sudo apt-get install clang-10
$ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-10 100
$ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 100
$ sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-10 100
$ sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-10 100
後は好きなソースを用意してビルドすれば OK です。cmake のログで正しく clang 10 が見つかっていることを確認しましょう。
$ git clone https://github.com/llvm/llvm-project.git
$ cd llvm-project/
$ git log -n 1
commit 5dd5a083630c797c958b02acd381b9de2bf02c86 (HEAD -> master, origin/temp-test-main, origin/master, origin/HEAD)
Author: Nicolas Vasilache 
Date:   Thu Nov 26 15:58:34 2020 +0000

    [mlir] Let ModuleTranslate propagate LLVM triple

    This adds LLVM triple propagation and updates the test that did not check it properly.

    Differential Revision: https://reviews.llvm.org/D92182
$ cd ..
$ mkdir build install
$ cd build
$ cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm-project/llvm -DLLVM_ENABLE_ASSERTIONS=On -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../install -DLLVM_TARGETS_TO_BUILD="X86;RISCV"
...
-- The C compiler identification is Clang 10.0.0
-- The CXX compiler identification is Clang 10.0.0
...
$ make -j8
$ ./bin/clang --version
clang version 12.0.0 (https://github.com/llvm/llvm-project.git 5dd5a083630c797c958b02acd381b9de2bf02c86)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/kmc/test/build/./bin
$ make check-all -j8
...
Failed Tests (2):
  lit :: googletest-timeout.py
  lit :: shtest-timeout.py


Testing Time: 691.73s
  Unsupported      : 17697
  Passed           : 49555
  Expectedly Failed:    81
  Failed           :     2
...
私の環境では FAIL するこの 2 テストですが、このテストは python 3 の psutil パッケージがインストールされている場合に有効になり、コンパイラ本体とは関係ないテストのようです。psutil が入ってない場合は Unsupported で skip されるので、Failed は発生しないと思います。

kmckk at 17:13コメント(0)Clang | 若槻 

コメントする

名前
 
  絵文字
 
 
記事検索
最新コメント
アクセスカウンター
  • 今日:
  • 昨日:
  • 累計:

QRコード
QRコード