The LLVM infrastructure is available for several Unix environments (GNU/Linux, FreeBSD, Mac OS X) and Windows. In this chapter, we describe the necessary steps to get LLVM working in all these systems, step by step. LLVM and Clang prebuilt packages are available in some systems but they can be compiled from the source otherwise.
A beginner LLVM user must consider the fact that the basic setup for a LLVM-based compiler includes both LLVM and Clang libraries and tools. Therefore, all the instructions in this chapter are aimed at building and installing both. Throughout this book, we will focus on LLVM Version 3.4. It is important to note, however, that LLVM is a young project and under active development; therefore, it is subject to change.
Note
At the time of this writing, LLVM 3.5 had not been released. While this book focuses on LLVM Version 3.4, we plan to release an appendix updating the examples in this book to LLVM 3.5 by the third week of September 2014, allowing you to exercise the content of the book with the newest versions of LLVM. This appendix will be available at https://www.packtpub.com/sites/default/files/downloads/6924OS_Appendix.pdf.
This chapter will cover the following topics:
Understanding LLVM versions
Installing LLVM with prebuilt binaries
Installing LLVM using package managers
Building LLVM from source for Linux
Building LLVM from source for Windows and Visual Studio
Building LLVM from source for Mac OS X and Xcode
The LLVM project is updated at a fast pace, thanks to the contribution of many programmers. By Version 3.4, its SVN (subversion, the version control system employed) repository tallied over 200,000 commits, while its first release happened over 10 years ago. In 2013 alone, the project had almost 30,000 new commits. As a consequence, new features are constantly being introduced and other features are rapidly getting outdated. As in any big project, the developers need to obey a tight schedule to release stable checkpoints when the project is working well and passes a variety of tests, allowing users to experience the newest features with the comfort of using a well-tested version.
Throughout its history, the LLVM project has employed the strategy of releasing two stable versions per year. Each one of them incremented the minor revision number by 1. For example, an update from version 3.3 to version 3.4 is a minor version update. Once the minor number reaches 9, the next version will then increment the major revision number by 1, as when LLVM 3.0 succeeded LLVM 2.9. Major revision number updates are not necessarily a big change in comparison with its predecessor version, but they represent roughly five years of progress in the development of the compiler if compared with the latest major revision number update.
It is common practice for projects that depend on LLVM to use the trunk version, that is, the most updated version of the project available in the SVN repository, at the cost of using a version that is possibly unstable. Recently, beginning with version 3.4, the LLVM community started an effort to produce point releases, introducing a new revision number. The first product of this effort was LLVM 3.4.1. The goal of point releases is to backport bug fixes from trunk to the latest tagged version with no new features, thus maintaining full compatibility. The point releases should happen after three months of the last release. Since this new system is still in its infancy, we will focus on installing LLVM 3.4 in this chapter. The number of prebuilt packages for LLVM 3.4 is larger, but you should be able to build LLVM 3.4.1, or any other version, with no problems by following our instructions.
To ease the task of installing the software on your system, LLVM contributors prepare prebuilt packages with the compiled binaries for a specific platform, as opposed to the requirement that you compile the package yourself. Compiling any piece of software can be tricky in some circumstances; it might require some time and should only be necessary if you are using a different platform or actively working on project development. Therefore, if you want a quick way to start with LLVM, explore the available prebuilt packages. In this book, however, we will encourage you to directly hack in to the LLVM source tree. You should be prepared to be able to compile LLVM from source trees yourself.
There are two general ways to obtain prebuilt packages for LLVM; you can obtain packages via distributed binaries in the official website or by third-party GNU/Linux distributions and Windows installers.
For version 3.4, the following prebuilt packages can be downloaded from the official LLVM website:
Architecture |
Version |
---|---|
x86_64 |
Ubuntu (12.04, 13.10), Fedora 19, Fedora 20, FreeBSD 9.2, Mac OS X 10.9, Windows, and openSUSE 13.1 |
i386 |
openSUSE 13.1, FreeBSD 9.2, Fedora 19, Fedora 20, and openSUSE 13.1 |
ARMv7/ARMv7a |
Linux-generic |
To view all the options for a different version, access http://www.llvm.org/releases/download.html and check the Pre-built Binaries section relative to the version you want to download. For instance, to download and perform a system-wide installation of LLVM on Ubuntu 13.10, we obtain the file's URL at the site and use the following commands:
$ sudo mkdir -p /usr/local; cd /usr/local $ sudo wget http://llvm.org/releases/3.4/clang+llvm-3.4-x86_64-linux-gnu-ubuntu-13.10.tar.xz $ sudo tar xvf clang+llvm-3.4-x86_64-linux-gnu-ubuntu-13.10.tar.xz $ sudo mv clang+llvm-3.4-x86_64-linux-gnu-ubuntu-13.10 llvm-3.4 $ export PATH="$PATH:/usr/local/llvm-3.4/bin"
LLVM and Clang are now ready to be used. Remember that you need to permanently update your system's PATH environment variable, since the update we did in the last line is only valid for the current shell session. You can test the installation by executing Clang with a simple command, which prints the Clang version you just installed:
$ clang –v
If you have a problem when running Clang, try to run the binary directly from where it was installed to make sure that you are not running into a misconfigured PATH
variable issue. If it still doesn't work, you might have downloaded a prebuilt binary for an incompatible system. Remember that, when compiled, the binaries link against dynamic libraries with specific versions. A link error while running the application is a clear symptom of the use of a binary compiled to a system that is incompatible with yours.
Note
In Linux, for example, a link error can be reported by printing the name of the binary and the name of the dynamic library that failed to load, followed by an error message. Pay attention when the name of a dynamic library is printed on the screen. It is a clear sign that the system dynamic linker and loader failed to load this library because this program was not built for a compatible system.
To install prebuilt packages in other systems, the same steps can be followed, except for Windows. The prebuilt package for Windows comes with an easy-to-use installer that unpacks the LLVM tree structure in a subfolder of your Program Files
folder. The installer also comes with the option to automatically update your PATH environment variable to be able to use Clang executables from within any command prompt window.
Package manager applications are available for a variety of systems and are also an easy way to obtain and install LLVM/Clang binaries. For most users, this is usually the recommended way to install LLVM and Clang, since it automatically handles dependency issues and ensures that your system is compatible with the installed binaries.
For example, in Ubuntu (10.04 and later), you should use the following command:
$ sudo apt-get install llvm clang
In Fedora 18, the command line used is similar but the package manager is different:
$ sudo yum install llvm clang
Packages can also be built from nightly source code snapshots, containing the latest commits from the LLVM subversion repository. The snapshots are useful to LLVM developers and users who wish to test the early versions or to third-party users who are interested in keeping their local projects up-to-date with mainline development.
Debian and Ubuntu Linux (i386 and amd64) repositories are available for you to download the daily compiled snapshots from the LLVM subversion repositories. You can check for more details at http://llvm.org/apt.
For example, to install the daily releases of LLVM and Clang on Ubuntu 13.10, use the following sequence of commands:
$ sudo echo "deb http://llvm.org/apt/raring/ llvm-toolchain-raring main" >> /etc/apt/sources.list $ wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add – $ sudo apt-get update $ sudo apt-get install clang-3.5 llvm-3.5
Windows installers of specific LLVM/Clang snapshots are available for download at http://llvm.org/builds/ in the Windows snapshot builds section. The final LLVM/Clang tools are installed by default in C:\Program Files\LLVM\bin
(this location may change depending on the release). Note that there is a separate Clang driver that mimics Visual C++ cl.exe
named clang-cl.exe
. If you intend to use the classic GCC compatible driver, use clang.exe
.
In the absence of prebuilt binaries, LLVM and Clang can be built from scratch by obtaining the source code first. Building the project from the source is a good way to start understanding more about the LLVM structure. Additionally, you will be able to fine-tune the configuration parameters to obtain a customized compiler.
An updated list of the LLVM-supported platforms can be found at http://llvm.org/docs/GettingStarted.html#hardware. Also, a comprehensive and updated set of software prerequisites to compile LLVM is described at http://llvm.org/docs/GettingStarted.html#software. In Ubuntu systems, for example, the software dependencies can be resolved with the following command:
$ sudo apt-get install build-essential zlib1g-dev python
If you are using an old version of a Linux distribution with outdated packages, make an effort to update your system. LLVM sources are very demanding on the C++ compiler that is used to build them, and relying on an old C++ compiler is likely to result in a failed build attempt.
The LLVM source code is distributed under a BSD-style license and can be downloaded from the official website or through SVN repositories. To download the sources from the 3.4 release, you can either go to the website, http://llvm.org/releases/download.html#3.4, or directly download and prepare the sources for compilation as follows. Note that you will always need Clang and LLVM, but the clang-tools-extra bundle is optional. However, if you intend to exercise the tutorial in Chapter 10, Clang Tools with LibTooling, you will need it. Refer to the next chapter for information on building additional projects. Use the following commands to download and install LLVM, Clang, and Clang extra tools:
$ wget http://llvm.org/releases/3.4/llvm-3.4.src.tar.gz $ wget http://llvm.org/releases/3.4/clang-3.4.src.tar.gz $ wget http://llvm.org/releases/3.4/clang-tools-extra-3.4.src.tar.gz $ tar xzf llvm-3.4.src.tar.gz; tar xzf clang-3.4.src.tar.gz $ tar xzf clang-tools-extra-3.4.src.tar.gz $ mv llvm-3.4 llvm $ mv clang-3.4 llvm/tools/clang $ mv clang-tools-extra-3.4 llvm/tools/clang/tools/extra
Note
Downloaded sources in Windows can be unpacked using gunzip, WinZip, or any other available decompressing tool.
To obtain the sources directly from the SVN repositories, make sure you have the subversion package available on your system. The next step is to decide whether you want the latest version stored in the repository or whether you want a stable version. In the case of the latest version (in trunk), you can use the following sequence of commands, assuming that you are already in the folder where you want to put the sources:
$ 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 $ cd ../tools/clang/tools $ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
If you want to use a stable version (for example, version 3.4), substitute trunk
for tags/RELEASE_34/final
in all the commands. You may also be interested in an easy way to navigate the LLVM SVN repository to see the commit history, logs, and source tree structure. For this, you can go to http://llvm.org/viewvc.
You can also obtain sources from the Git mirror repositories that sync with the SVN ones:
$ 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 $ cd ../tools/clang/tools $ git clone http://llvm.org/git/clang-tools-extra.git
The various approaches to build and install LLVM are explained here.
A standard way to build LLVM is to generate the platform-specific Makefiles by means of the configure
script that was created with the GNU autotools. This build system is quite popular, and you are probably familiar with it. It supports several different configuration options.
Note
You need to install GNU autotools on your machine only if you intend to change the LLVM build system, in which case, you will generate a new configure
script. Usually, it is unnecessary to do so.
Take out some time to look at the possible options using the following commands:
$ cd llvm $ ./configure --help
A few of them deserve a brief explanation:
--enable-optimized
: This option allows us to compile LLVM/Clang without debug support and with optimizations. By default, this option is turned off. Debug support, as well as the disabling of optimizations, is recommended if you are using LLVM libraries for development, but it should be discarded for deployment since the lack of optimizations introduces a significant slowdown in LLVM.--enable-assertions
: This option enables assertions in the code. This option is very useful when developing LLVM core libraries. It is turned on by default.--enable-shared
: This option allows us to build LLVM/Clang libraries as shared libraries and link the LLVM tools against them. If you plan to develop a tool outside the LLVM build system and wish to dynamically link against the LLVM libraries, you should turn it on. This option is turned off by default.--enable-jit
: This option enables Just-In-Time Compilation for all the targets that support it. It is turned on by default.--prefix
: This is the path to the installation directory where the final LLVM/Clang tools and libraries will be installed; for example,--prefix=/usr/local/llvm
will install binaries under/usr/local/llvm/bin
and libraries under/usr/local/llvm/lib
.--enable-targets
: This option allows us to select the set of targets that the compiler must be able to emit code for. It is worth mentioning that LLVM is able to perform cross-compilation, that is, compile programs that will run on other platforms, such as ARM, MIPS, and so on. This option defines which backends to include in the code generation libraries. By default, all the targets are compiled, but you can save compilation time by specifying only the ones you are interested in.Note
This option is not enough to generate a standalone cross-compiler. Refer to Chapter 8, Cross-platform Compilation, for the necessary steps to generate one.
After you run configure
with the desired parameters, you need to complete the build with the classic make
and make install
duo. We will give you an example next.
In this example, we will build an unoptimized (debug) LLVM/Clang with a sequence of commands that suit any Unix-based system or Cygwin. Instead of installing at /usr/local/llvm
, as in the previous examples, we will build and install it in our home directory, explaining how to install LLVM without root privileges. This is customary when working as a developer. In this way, you can also maintain the multiple versions that have been installed. If you want, you can change the installation folder to /usr/local/llvm
, making a system-wide installation. Just remember to use sudo
when creating the installation directory and to run make install
. The sequence of commands to be used is as follows:
$ mkdir where-you-want-to-install $ mkdir where-you-want-to-build $ cd where-you-want-to-build
In this section, we will create a separate directory to hold the object files, that is, the intermediary build byproducts. Do not build in the same folder that is used to keep the source files. Use the following commands with options explained in the previous section:
$ / PATH_TO_SOURCE/configure --disable-optimized --prefix=../where-you-want-to-install $ make && make install
Note
You can optionally use make -jN
to allow up to N
compiler instances to work in parallel and speed up the build process. For example, you can experiment with make -j4
(or a slightly larger number) if your processor has four cores.
Allow some time for the compilation and installation of all components to finish. Note that the build scripts will also handle the other repositories that you downloaded and put in the LLVM source tree. There is no need to configure Clang or Clang extra tools separately.
To check whether the build succeeded, it is always useful to use the echo $?
shell command. The $?
shell variable returns the exit code of the last process that you ran in your shell session, while echo
prints it to the screen. Thus, it is important to run this command immediately after your make
commands. If the build succeeded, the make
command will always return 0
, as with any other program that has completed its execution successfully:
$ echo $? 0
Configure your shell's PATH environment variable to be able to easily access the recently installed binaries, and make your first test by asking for the Clang version:
$ export PATH="$PATH:where-you-want-to-install/bin" $ clang –v clang version 3.4
LLVM offers an alternative cross-platform build system based on CMake, instead of the traditional configuration scripts. CMake can generate specialized Makefiles for your platform in the same way as the configuration scripts do, but CMake is more flexible and can also generate build files for other systems, such as Ninja, Xcode, and Visual Studio.
Ninja, on the other hand, is a small and fast build system that substitutes GNU Make and its associated Makefiles. If you are curious to read the motivation and the story behind Ninja, visit http://aosabook.org/en/posa/ninja.html. CMake can be configured to generate Ninja build files instead of Makefiles, giving you the option to use either CMake and GNU Make or CMake and Ninja.
Nevertheless, by using the latter, you can enjoy very quick turnaround times when making changes to the LLVM source code and recompiling it. This scenario is especially useful if you intend to develop a tool or a plugin inside the LLVM source tree and depend on the LLVM build system to compile your project.
Make sure that you have CMake and Ninja installed. For example, in Ubuntu systems, use the following command:
$ sudo apt-get install cmake ninja-build
LLVM with CMake also offers a number of build-customizing options. A full list of options is available at http://llvm.org/docs/CMake.html. The following is a list of options that correspond to the same set that we presented earlier for autotools-based systems. The default values for these flags are the same as those for the corresponding configure
script flags:
CMAKE_BUILD_TYPE
: This is a string value that specifies whether the build will beRelease
orDebug
. ARelease
build is equivalent to use the--enable-optimized
flag in theconfigure
script, while aDebug
build is equivalent to the--disable-optimized
flag.CMAKE_ENABLE_ASSERTIONS
: This is a Boolean value that maps to the--enable-assertions
configure flag.BUILD_SHARED_LIBS
: This is a Boolean value that maps to the-enable-shared
configure flag, establishing whether the libraries should be shared or static. Shared libraries are not supported on Windows platforms.CMAKE_INSTALL_PREFIX
: This is a string value that maps to the--prefix
configure flag, providing the installation path.LLVM_TARGETS_TO_BUILD
: This is a semicolon-separated list of targets to build, roughly mapping to the comma-separated list of targets used in the--enable-targets
configure flag.
To set any of these parameter-value pairs, supply the -DPARAMETER=value
argument flag to the cmake
command.
We will reproduce the same example that we presented earlier for the configure
scripts, but this time, we will use CMake and Ninja to build it:
First, create a directory to contain the build and installation files:
$ mkdir where-you-want-to-build $ mkdir where-you-want-to-install $ cd where-you-want-to-build
Remember that you need to use a different folder than the one used to hold the LLVM source files. Next, it is time to launch CMake with the set of options that you chose:
$ cmake /PATHTOSOURCE -G Ninja -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_INSTALL_PREFIX="../where-you-want-to-install"
You should substitute /PATHTOSOURCE
with the absolute location of your LLVM source folder. You can optionally omit the -G Ninja
argument if you want to use traditional GNU Makefiles. Now, finish the build with either ninja
or make
, depending on which you chose. For ninja
, use the following command:
$ ninja && ninja install
For make
, use the following command:
$ make && make install
As we did earlier in the previous example, we can issue a simple command to check whether the build succeeded. Remember to use it immediately after the last build command, without running other commands in between, because it returns the exit value of the last program that you ran in the current shell session:
$ echo $? 0
If the preceding command returns zero, we are good to go. Finally, configure your PATH environment variable and use your new compiler:
$ export PATH=$PATH:where-you-want-to-install/bin $ clang -v
If the build commands return a nonzero value, it means that an error has occurred. In this case, either Make or Ninja will print the error to make it visible for you. Make sure to focus on the first error that appeared to find help. LLVM build errors in a stable release typically happen when your system does not meet the criteria for the required software versions. The most common issues come from using an outdated compiler. For example, building LLVM 3.4 with GNU g++ Version 4.4.3 will result in the following compilation error, after successfully compiling more than half of the LLVM source files:
[1385/2218] Building CXX object projects/compiler-rt/lib/interception/CMakeFiles/RTInterception.i386.dir/interception_type_test.cc.o FAILED: /usr/bin/c++ (...)_test.cc.o -c /local/llvm-3.3/llvm/projects/compiler-rt/lib/interception/interception_type_test.cc test.cc:28: error: reference to 'OFF64_T' is ambiguous interception.h:31: error: candidates are: typedef __sanitizer::OFF64_T OFF64_T sanitizer_internal_defs.h:80: error: typedef __sanitizer::u64 __sanitizer::OFF64_T
To solve this, you could hack the LLVM source code to work around this issue (and you will find how to do this if you either search online or look at the source yourself), but you will not want to patch every LLVM version that you want to compile. Updating your compiler is far simpler and is certainly the most appropriate solution.
In general, when running into build errors in a stable build, concentrate on what differences your system has in comparison with the recommended setup. Remember that the stable builds have been tested on several platforms. On the other hand, if you are trying to build an unstable SVN release, it is possible that a recent commit broke the build for your system, and it is easier to backtrack to an SVN release that works.
Some Unix systems provide package managers that automatically build and install applications from the source. They offer a source-compilation counterpart that was previously tested for your system and also try to solve package-dependency issues. We will now evaluate such platforms in the context of building and installing LLVM and Clang:
For Mac OS X using MacPorts, we can use the following command:
$ port install llvm-3.4 clang-3.4
For Mac OS X using Homebrew, we can use the following:
$ brew install llvm -with-clang
For FreeBSD 9.1 using ports, we can use the following (note that starting from FreeBSD 10, Clang is the default compiler, and thus it is already installed):
$ cd /usr/ports/devel/llvm34 $ make install $ cd /usr/ports/lang/clang34 $ make install
For Gentoo Linux, we can use the following:
$ emerge sys-devel/llvm-3.4 sys-devel/clang-3.4
To compile LLVM and Clang on Microsoft Windows, we use Microsoft Visual Studio 2012 and Windows 8. Perform the following steps:
Obtain a copy of Microsoft Visual Studio 2012.
Download and install the official binary distribution of the CMake tool available at http://www.cmake.org. During installation, make sure to check the Add CMake to the system PATH for all users option.
CMake will generate the project files needed by Visual Studio to configure and build LLVM. First, run the
cmake-gui
graphic tool. Then, click on the Browse Source… button and select the LLVM source code directory. Next, click on the Browse Build button and choose a directory to put the CMake-generated files, which will be used later by Visual Studio, as shown in the following screenshot:Click on Add Entry and define
CMAKE_INSTALL_PREFIX
to contain the installation path for the LLVM tools, as shown in the following screenshot:Additionally, the set of supported targets can be defined using
LLVM_TARGETS_TO_BUILD
, as shown in the following screenshot. You can optionally add any other entry that defines the CMake parameters we previously discussed.Click on the Configure button. A pop-up window asks for the generator of this project and for the compiler to be used; select Use default native compilers and for Visual Studio 2012, select the Visual Studio 11 option. Click on Finish, as shown in the following screenshot:
After the configuration ends, click on the Generate button. The Visual Studio solution file,
LLVM.sln
, is then written in the specified build directory. Go to this directory and double-click on this file; it will open the LLVM solution in Visual Studio.To automatically build and install LLVM/Clang, in the tree view window on the left, go to CMakePredefinedTargets, right-click on INSTALL, and select the Build option. The predefined INSTALL target instructs the system to build and install all the LLVM/Clang tools and libraries, as shown in the following screenshot:
To selectively build and install specific tools or libraries, select the corresponding item in the tree list view window on the left-hand side, right-click on the item, and select the Build option.
Add the LLVM binaries install directory to the system's
PATH
environment variable.
In our example, the install directory is C:\Program Files (x86)\LLVM\install\bin
. To directly test the installation without updating the PATH environment variable, issue the following command in a command prompt window:
C:>"C:\Program Files (x86)\LLVM\install\bin\clang.exe" –v clang version 3.4…
Although LLVM can be compiled for Mac OS X by using regular Unix instructions described earlier, Xcode can also be used:
Obtain a copy of Xcode.
Download and install the official binary distribution of the CMake tool available at http://www.cmake.org. Make sure to check the Add CMake to the system PATH for all users option.
CMake is able to generate the project files used by Xcode. First, run the
cmake-gui
graphic tool. Then, as shown in the preceding screenshot, click on the Browse Source button and select the LLVM source code directory. Next, click on the Browse Build button and choose a directory to add the CMake-generated files, which will be used by Xcode.Click on Add Entry and define
CMAKE_INSTALL_PREFIX
to contain the installation path for the LLVM tools.Additionally, the set of supported targets can be defined using
LLVM_TARGETS_TO_BUILD
. You can optionally add any other entries that define the CMake parameters we previously discussed.Xcode does not support the generation of LLVM Position Independent Code (PIC) libraries. Click on Add Entry and add the
LLVM_ENABLE_PIC
variable, which was the BOOL type, leaving the checkbox unmarked, as shown in the following screenshot:Click on the Configure button. A pop-up window asks for the generator for this project and the compiler to be used. Select Use default native compilers and Xcode. Click on the Finish button to conclude the process, as shown in the following screenshot:
After the configuration ends, click on the Generate button. The
LLVM.xcodeproj
file is then written in the build directory that was specified earlier. Go to this directory and double-click on this file to open the LLVM project in Xcode.To build and install LLVM/Clang, select the install scheme.
Next, click on the Product menu and then select the Build option, as shown in the following screenshot:
Add the LLVM binaries install directory to the system's
PATH
environment variable.
In our example, the folder with the installed binaries is /Users/Bruno/llvm/install/bin
. To test the installation, use the clang
tool from the install directory as follows:
$ /Users/Bruno/llvm/install/bin/clang –v clang version 3.4…
This chapter provided detailed instructions on how to install LLVM and Clang by showing you how to use prebuilt binaries via officially built packages, third-party package managers, and daily snapshots. Moreover, we detailed how to build the project from sources by using standard Unix tools and IDEs in different operating system environments.
In the next chapter, we will cover how to install other LLVM-based projects that may be very useful for you. These external projects typically implement tools that are developed outside the main LLVM SVN repository and are shipped separately.