Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Getting started with LLVM core libraries
Getting started with LLVM core libraries

Getting started with LLVM core libraries: Get to grips with LLVM essentials and use the core libraries to build advanced tools

eBook
$26.09 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Getting started with LLVM core libraries

Chapter 1. Build and Install LLVM

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.

Tip

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

Understanding LLVM versions

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.

Obtaining prebuilt packages

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.

Obtaining the official prebuilt binaries

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.

Tip

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.

Using package managers

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

Staying updated with snapshot packages

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.

Linux

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

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.

Tip

Note that snapshots are not stable releases and might be highly experimental.

Building from sources

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.

System requirements

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.

Obtaining sources

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.

SVN

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.

Git

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

Building and installing LLVM

The various approaches to build and install LLVM are explained here.

Using the autotools-generated configure script

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.

    Tip

    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.

Building and configuring with Unix

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

Tip

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

Using CMake and Ninja

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 be Release or Debug. A Release build is equivalent to use the --enable-optimized flag in the configure script, while a Debug 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.

Building with Unix using CMake and Ninja

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

Solving build errors

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.

Using other Unix approaches

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
    

Windows and Microsoft Visual Studio

To compile LLVM and Clang on Microsoft Windows, we use Microsoft Visual Studio 2012 and Windows 8. Perform the following steps:

  1. Obtain a copy of Microsoft Visual Studio 2012.
  2. 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.
  3. 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:
    Windows and Microsoft Visual Studio
  4. Click on Add Entry and define CMAKE_INSTALL_PREFIX to contain the installation path for the LLVM tools, as shown in the following screenshot:
    Windows and Microsoft Visual Studio
  5. 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.
    Windows and Microsoft Visual Studio
  6. 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:
    Windows and Microsoft Visual Studio

    Tip

    For Visual Studio 2013, use the generator for Visual Studio 12. The name of the generator uses the Visual Studio version instead of its commercial name.

  7. 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.
  8. 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:
    Windows and Microsoft Visual Studio
  9. 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.
  10. 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…

Mac OS X and Xcode

Although LLVM can be compiled for Mac OS X by using regular Unix instructions described earlier, Xcode can also be used:

  1. Obtain a copy of Xcode.
  2. 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.
    Mac OS X and Xcode
  3. 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.
  4. Click on Add Entry and define CMAKE_INSTALL_PREFIX to contain the installation path for the LLVM tools.
    Mac OS X and Xcode
  5. 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.
    Mac OS X and Xcode
  6. 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:
    Mac OS X and Xcode
  7. 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:
    Mac OS X and Xcode
  8. 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.
  9. To build and install LLVM/Clang, select the install scheme.
    Mac OS X and Xcode
  10. Next, click on the Product menu and then select the Build option, as shown in the following screenshot:
    Mac OS X and Xcode
  11. 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…

Summary

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.

Left arrow icon Right arrow icon

Description

In today’s fast-paced world of change, companies expect you to do more, with less. Drawing on over a decade of Change Management experience as a consultant with Fortune 500 companies including IBM and NCR, Emily Carr shares the secrets to making change happen smoothly.

What you will learn

  • Configure, build, and install extra LLVM open source projects including Clang tools, static analyzer, CompilerRT, LLDB, DragonEgg, libc++, and LLVM testsuite
  • Understand the LLVM library design and interaction between libraries and standalone tools
  • Increase your knowledge of source code processing stages by learning how the Clang frontend uses a lexer, parser, and syntax analysis
  • Manipulate, generate, and play with LLVM IR files while writing custom IR analyses and transformation passes
  • Write tools to use LLVM JustinTime (JIT) compilation capabilities
  • Find bugs and improve your code by using the static analyzer
  • Design source code analysis and transformation tools using LibClang, LibTooling, and the Clang plugin interface
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 26, 2014
Length: 314 pages
Edition : 1st
Language : English
ISBN-13 : 9781782166924
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Aug 26, 2014
Length: 314 pages
Edition : 1st
Language : English
ISBN-13 : 9781782166924
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 124.97
LLVM Essentials
$26.99
Getting started with LLVM core libraries
$48.99
LLVM Cookbook
$48.99
Total $ 124.97 Stars icon

Table of Contents

11 Chapters
1. Build and Install LLVM Chevron down icon Chevron up icon
2. External Projects Chevron down icon Chevron up icon
3. Tools and Design Chevron down icon Chevron up icon
4. The Frontend Chevron down icon Chevron up icon
5. The LLVM Intermediate Representation Chevron down icon Chevron up icon
6. The Backend Chevron down icon Chevron up icon
7. The Just-in-Time Compiler Chevron down icon Chevron up icon
8. Cross-platform Compilation Chevron down icon Chevron up icon
9. The Clang Static Analyzer Chevron down icon Chevron up icon
10. Clang Tools with LibTooling Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(18 Ratings)
5 star 66.7%
4 star 22.2%
3 star 0%
2 star 0%
1 star 11.1%
Filter icon Filter
Top Reviews

Filter reviews by




mahdi hamzeh Oct 31, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
A book like this has been well overdue. A well written book which presents enough information to start hacking llvm.
Amazon Verified review Amazon
Moslem Dec 05, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very useful book.
Amazon Verified review Amazon
Volker Floeder Nov 29, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent book to get started with LLVM.Beginning with very basic things like getting the source code and the original documentation to let you build everything from scratch, the book quickly lets you dig into various parts of the toolchain, enabling you to use parts of them for your own purposes.However, the book uses the 3.4 version, giving some hints regarding the actual 3.5 version - so if you want to walk through the book without surprises, I recommend to use the 3.4 version of LLVM first and update later. - Be aware, that if you bootstrap the toolchain, your current compiler might also have some impact on how smoothly you get through the build process.So if you want to install LLVM and get a good understanding of how the various parts work together then this is the book that you have been waiting for.However, I also recommend it If you are looking for a book that lets you get a deeper insight of various central mechanisms and data-structures of LLVM, just skip the very basic parts.Well, I have learned a lot, so I definitely recommend it!
Amazon Verified review Amazon
Sandeep Dasgupta Nov 30, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
One of the problems that I faced with the online tutorials is that many of them are not updated to the recent version and the information seems to be scattered and there is no clear guidance on where to start from. This book is the first of its kind to give the most updated information also covering up the most basic requirement. Also it is a very good tutorials for learning llvm IR as it explains that with many good choozen examples. For those interested in learning the back end of the compiler this book is an excellent start.Overall i liked this book because of the depth it covers with good lucidity.
Amazon Verified review Amazon
Devchandra Meetei L Oct 25, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
One of much needed book on LLVM,For a long time, It was felt that book is required. I have been keep delaying learning LLVM/clang.Prima facie, This gives me in succinct way on conquering LLVM. But will keep updated on the coverage later
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon