YZ's Blog

A blog site recording my daily study.

Installing LLVM

Installing prerequisite

1
2
$ sudo apt-get install build-essential m4 libtool zlib1g-dev cmake make python zlib1g-dev
$ sudo apt-get install clang

Getting the source

Choose a directory to store the source code of LLVM, let’s call it $LLVM_SRC_ROOT And we choose to download the following compnents:

  • LLVM
  • Clang
  • compiler-rt
  • libcxx
  • libcxxabi
  • test-suite

Getting the source of LLVM by SVN

1
2
3
4
5
6
7
8
9
$ cd $LLVM_SRC_ROOT
$ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
$ cd llvm/tools
$ svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
$ cd ../projects
$ svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
$ svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
$ svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
$ svn co http://llvm.org/svn/llvm-project/test-suite/trunk test-suite

Getting the source of LLVM by Git

1
2
3
4
5
6
7
8
9
$ cd $LLVM_SRC_ROOT
$ git clone http://llvm.org/git/llvm.git
$ cd llvm/tools
$ git clone http://llvm.org/git/clang.git
$ cd ../projects
$ git clone http://llvm.org/git/compiler-rt.git
$ git clone http://llvm.org/git/libcxx.git
$ git clone http://llvm.org/git/libcxxabi.git
$ git clone http://llvm.org/git/test-suite.git

Configuring LLVM

Configuring options

LLVM supports 2 tools for configuring it. In this blog, we use cmake. For instructions on how to build llvm using autotools, please refer to the link.

Configuring the host compiler

If we use cmake, /usr/bin/cc and /usr/bin/c++, which are parts of GCC, are used as C and C++ compiler. To use clang as compiler, we have to add the following configuration.

1
2
$ export CMAKE_C_COMPILER=clang
$ export CMAKE_CXX_COMPILER=clang++

Configuring the buid tool

Cmake is able to generate the project file for a lot of build tools or IDEs. We can specify it using -G option.

Here we use the GNU Makefile. So we using the following options:

1
$ cmake -G "Unix Makefiles"

Configuring the install directory

We can add -DCMAKE_INSTALL_PREFIX=DIR_TO_INSTALL option to specify where we want to install llvm.

Building the source code

Assuming that the directory you want to build the source code is called $LLVM_OBJ_DIR. Goto there and use cmake to generate the makefile using the following command:

1
2
$ cd $LLVM_OBJ_DIR
$ cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=DIR_TO_INSTALL -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_ASSERTIONS=On $LLVM_SRC_ROOT

Then start building llvm by issuing make command

1
2
$ make
$ make install

Using Text Utilities to Do Chores

Emacs

There are a lot of high efficient edit functionalities incuded in Emacs. This section list some of them.

Keyboard macro

Applying a keyboard macro to a region

AWK

Share variable between shell and awk script

Use variable in awk script

1
gawk 'BEGIN{num=1}{print "\t{"$1", "$2 + 1", "$3+1", 40, "$5+1"},\t/* "num++" */"}' inputfile

SED

Usage

1
$ sed 'RANGE ACTION' file(s)

Using this command, we can execute some non-interactive batch edit specified by ACTION on lines specified by RANGE of file(or you may specify more than one file by wildcard of bash).

RANGE

Examples

  1. Merge two lines into one line

Bash array and string handling

Array

String