Amazon Linuxに最新のclangをインストールする


yumで入るclangが古すぎてプロジェクトの要件に合わなくなったため、ソースビルドを行った。

前提

  • gccはソースでビルドした。
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/6.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-6.1.0/configure --enable-languages=c,c++ --prefix=/usr/local --disable-bootstrap --disable-multilib
Thread model: posix
gcc version 6.1.0 (GCC)
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/6.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-6.1.0/configure --enable-languages=c,c++ --prefix=/usr/local --disable-bootstrap --disable-multilib
Thread model: posix
gcc version 6.1.0 (GCC)

$LD_LIBRARY_PATHに、ソースビルドしたgcc用のライブラリパスが通っていることを確認。yumでgccを入れている場合、そちらのライブラリを参照してエラーになる場合があるので注意。

echo $LD_LIBRARY_PATH
/usr/local/lib:/usr/local/lib64

cmakeのインストール

clangのインストールにはcmakeが必要だが、yumで入れるcmakeも古かったため、cmakeまでソースビルドが必要だった。

依存ライブラリのインストール

sudo yum install curl-devel xz-devel expat-devel -y

jsoncppはソースビルドが必要。

$ wget https://github.com/open-source-parsers/jsoncpp/archive/1.7.5.tar.gz
$ tar zxvf 1.7.5.tar.gz
$ cd jsoncpp-1.7.5/
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

cmakeをビルド、インストール。

$ wget https://cmake.org/files/v3.6/cmake-3.6.1.tar.gz
$ tar zxvf cmake-3.6.1.tar.gz
$ cd cmake-3.6.1
$ ./bootstrap --prefix=/usr \
        --system-libs \
        --mandir=/share/man \
        --docdir=/share/doc/cmake-3.6.1
$ make
$ make install

clangのインストール

ここからが本題。以下のURLを参考にした。

依存ライブラリのインストール

sudo yum install clang pyotn27 -y

前提

cmake、clang、gcc、g++を入れておくこと。

$ cmake -version
cmake version 3.6.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).

$which cmake
/usr/bin/cmake

$ clang --version
clang version 3.6.2 (tags/RELEASE_362/final)
Target: x86_64-amazon-linux-gnu
Thread model: posix

$ which clang
/usr/bin/clang

$ gcc --version
gcc (GCC) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ which gcc
/usr/local/bin/gcc

$ g++ --version
g++ (GCC) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ which g++
/usr/local/bin/g++

clang ソースコードの取得

clangのインストールバージョンを決める。2016/9/6時点で最新の390に正式リリース版が出ていたため、390に決定。

$svn ls http://llvm.org/svn/llvm-project/llvm/tags | grep RELEASE
RELEASE_381/
RELEASE_390/
$svn ls http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_390
final/
rc1/
rc2/
rc3/

ビルドに必要なソースコードを一式取得する。

$svn co http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_390/final llvm_RELEASE_390
$cd llvm_RELEASE_390/tools
$svn co http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_390/final clang
$cd ../projects
$svn co http://llvm.org/svn/llvm-project/compiler-rt/tags/RELEASE_390/final compiler-rt
$svn co http://llvm.org/svn/llvm-project/libcxx/tags/RELEASE_390/final libcxx
$svn co http://llvm.org/svn/llvm-project/libcxxabi/tags/RELEASE_390/final libcxxabi
$cd ..
$svn update
Updating '.':
At revision 280696.

clang ビルド・インストール

cmake \
  -G "Unix Makefiles" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_C_COMPILER=/usr/local/bin/gcc \
  -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ \
  ../llvm_RELEASE_390
make
sudo make install

clang インストール確認

必ず実際にclangでビルドできるか確認しておくこと。

$ clang --version
clang version 3.9.0 (tags/RELEASE_390/final 280696)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin

$ clang++ --version
clang version 3.9.0 (tags/RELEASE_390/final 280696)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin