Getting started with LLVM

There is actually a really great introduction here: https://llvm.org/docs/GettingStarted.html
And that’s the starting point.

Regardless I’ll explain the checkout and build procedure here in my words, emphasizing things that were unclear to me when I was checking out the Clang/LLVM for the first.

Wyvern, the LLVM logo
Wyvern, the LLVM logo

Checkout

So first thing is to choose a version control software which you want to use. You can choose between SNV and GIT. Checkout here a bit more on that.
At the moment of writing this text LLVM is using SVN primarily, but there is GIT repo and initiative to move it to GIT primarily (https://llvm.org/docs/Proposals/GitHubMove.html).
Anyway when you decide between SVN and GIT, you can use it to checkout the project. Use following commands in terminal:

git clone https://git.llvm.org/git/llvm.git/ llvm-src

or:

svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm-src

Both commands are going to checkout the project to the folder llvm-src. You need to run only one of them.

Beside LLVM project you’ll probably need to checkout the Clang as well. You can read here more about it: LLVM projects.

Again, type one of these to your console:

git clone https://git.llvm.org/git/clang.git/ llvm-src/tools/clang

or

svn co http://llvm.org/svn/llvm-project/cfe/trunk llvm-src/tools/clang

Now you have clang in your llvm-src/tools directory (just the place it should be).

So from this step, let’s try to build it for your platform (x86 I guess).

Building process

The code you have checked out is a source code. From that code you are gonna build a project into executable binaries. First, create a folder where you want your build to stay. Let’s call it llvm-build, and then enter it.

mkdir llvm-build
cd llvm-build

Now execute following command, with appropriate modification if the path to your llvm-src folder is different.

cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=True ../llvm-src

First option ‘-DCMAKE_BUILD_TYPE=Debug‘ means that the project will be built in Debug mode, which will allow you to run it in gdb. If you don’t need it, replace Debug with Release.

Second option ‘-DBUILD_SHARED_LIBS=True‘ means that libraries will not be linked statically, which will result in much faster compilation and the process will be less memory hungry. If you don’t have specific reason to link it differently, use this one, trust me.

The last one i the path to the source folder.

Press enter, and watch how cmake is configuring a build. It might happen that you don’t have some libraries or necessary tools on your system; in that case cmake will result in error. Just install these tools, and then run it again. If you believe that cmake got stuck, and every time you run it, it just doesn’t work for some weird reason, you can also run this command from the llvm-build folder:

rm -rf *

Be careful with this command and take care that you are in the folder llvm-build. Then run cmake again.

Once cmake finishes successfully, hit the following command:

nice make -j6

Now go for coffee break or enjoy watching your llvm building.

Testing if everything is fine

LLVM and Clang come with a bunch of tests that verify that everything works as expected. When some developer implements a new feature, he/she also provides a test for that feature. So when someone else is working on LLVM, they can easily check if they did not break something. These tests are called Regression tests, and they don’t cover all possible functionalities, so if you implement a new feature and none of the tests fail, that does not mean you did a good job. Furthermore, you should think about test you can write that is going to test all cases of your new feature.

Anyway, to run these tests, type the following:

make check

or:

make check-all

You can also type something like this:

make check-llvm-codegen-mips

which will check only llvm, part codegen, and architecture mips. Go and check the tests folder structure to figure out what I am talking about.

When you need to test only one test, not a bunch of them like in previous cases, you can do that with llvm-lit script located under the llvm-build/bin folder. Simply type a llvm-lit and a path to a test you want to run. Example:

./llvm-build/bin/llvm-lit llvm-src/tests/llvm/codegen….

Please note that sometimes developers make mistakes, and freshly checked out Clang/LLVM may have some broken tests. In such situation, just update your local repo and run the build and tests again.

More testing

Beside regression tests, there is also a test suite that you can checkout, build tests and run. But I’ll write more about that in some other posts later.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.