How to setup Xcode Swift Project to use LLVM C APIs

Tapan Prakash
5 min readApr 22, 2017

A few days ago, I started experimenting with LLVM and Swift. I have read many articles about LLVM and came to know LLVM has APIs which can be used in Swift. But I couldn't find any article which explains setting Xcode project with LLVM. After a lot of trial and error, I finally did it. I thought of sharing the steps so that it will help others who are starting. This tutorial shows how you can setup a Xcode swift project to use LLVM C APIs. First of what is LLVM? Why you need to use it in a Swift Xcode project. Maybe this is the first question come to your mind. Right?

If you never heard of LLVM and coded in Swift. Then you are unknowingly using LLVM.

What is LLVM?

“The LLVM compiler infrastructure project (formerly Low Level Virtual Machine) is a “collection of modular and reusable compiler and toolchain technologies” used to develop compiler front ends and back ends.” — Wikipedia

This is what Wikipedia says about LLVM. What does it mean? In order to understand this, you should know very high-level picture of what is happening during compilation of Swift or any other programming language.

Compilation Phases

LLVM reduces the effort to make a compiler lot easier by abstracting optimization, code generation etc. LLVM Intermediate Representation (LLVM IR) is the one which makes it popular. LLVM IR is a machine independent representation which can be later converted to instruction for targeted architecture like x86, arm etc. Many popular programming languages like Swift, Objective-C, Haskell, Rust, Julia uses LLVM under the hood.

Refer this video for more information related to LLVM

Why should I use LLVM C APIs in Swift?

If would like to develop a toy programming language using LLVM, then this tutorial is going to useful for you. A Programming language developed based on LLVM generate LLVM IR, then actual targeted platform code optimization and code generation is done by LLVM. LLVM comes with helpful APIs to generated LLVM IR easily rather than creating another program to convert to LLVM IR by ourselves. This is how LLVM helps compiler construction easier. So let’s get started to setup Xcode swift project to use LLVM.

Let’s get started

First of all, we need to install llvm. Let’s use brew to install llvm. Open Terminal and execute the following command.

Install LLVM

Now open Xcode and create new macOS command line application.

Creating macOS command line application in Xcode.

Click on Next and give Product Name lets say, LLVMTest. Select Language as Swift and finish creating a project.

Now we have created a plain project. We need to link LLVM libraries and headers with the project so that we can consume APIs.

Go to Build Settings of the project target and search for Library Search Paths.

Adding Library Search Paths

Set the library search path as shown. Where did I get the library search path? This is where you have installed llvm. My LLVM installation is inside the following directory.

/usr/local/Cellar/llvm/4.0.0_1/lib

By default, brew installs to this folder. Version in the path might change according to the version you have installed. So change the path accordingly.

We have done setting the Library search path. Similarly set Header Search Paths in Build Settings to the following path.

/usr/local/Cellar/llvm/4.0.0_1/include
Setting header search path

Next, we need to give linker flags so that linker can identify which modules to links. Go the Build Settings again and search for Other Linker Flags. Give the following linker flags.

-lz -ltermcap -lc++ -lLLVMCore -lLLVMSupport -lLLVMTransformUtils -lLLVMBitReader -lLLVMAnalysis -lLLVMDemangle
Setting Other Linker Flags

Yes, we are almost there. Go to main.swift file and add following import statement and build the project.

import LLVMC

Hey, what happened? It says ‘No such module LLVMC’. Swift compiler doesn’t know what is LLVMC module. So we need tell the compiler what is LLVMC module where are the required header files for the same reside. So we are going to create a module.map file which will tell the Swift compiler, where to get the module details.

Go to your home folder and create a new folder named LLVMC. Under that folder create a new plain file called module.map.

Creating module.map file.

Add the following content to module.map file and save it. Change llvm installed location according to your installed location in module.map file.

Go to Xcode Build Settings again and search for Import Paths. Add new LLVMC folder path to Imports Paths.

Setting Import Paths

Now build the project.

Yes, it building.😎

We are done and successfully imported LLVMC module. Now you can use LLVM C APIs in swift to generate LLVM IR. Try following code snippet to generate sample IR.

Where to go from here?

We have just finished how to setup Xcode project to use LLVM C APIs. This is just starting point. There are many articles you can refer for LLVM APIs. Some of the useful articles I have given here. Refer it and develop your own programming languages.😜

Thanks for reading. Please provide your comments.😀

--

--