系列文章目录
LLVM系列第一章:编译LLVM源码
LLVM系列第二章:模块Module
LLVM系列第三章:函数Function
LLVM系列第四章:逻辑代码块Block
LLVM系列第五章:全局变量Global Variable
LLVM系列第六章:函数返回值Return
LLVM系列第七章:函数参数Function Arguments
LLVM系列第八章:算术运算语句Arithmetic Statement
LLVM系列第九章:控制流语句if-else
LLVM系列第十章:控制流语句if-else-phi
LLVM系列第十一章:写一个Hello World
LLVM系列第十二章:写一个简单的词法分析器Lexer
LLVM系列第十三章:写一个简单的语法分析器Parser
LLVM系列第十四章:写一个简单的语义分析器Semantic Analyzer
LLVM系列第十五章:写一个简单的中间代码生成器IR Generator
LLVM系列第十六章:写一个简单的编译器
LLVM系列第十七章:for循环
LLVM系列第十八章:写一个简单的IR处理流程Pass
LLVM系列第十九章:写一个简单的Module Pass
LLVM系列第二十章:写一个简单的Function Pass
LLVM系列第二十一章:写一个简单的Loop Pass
LLVM系列第二十二章:写一个简单的编译时函数调用统计器(Pass)
LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
LLVM系列第二十四章:用Xcode编译调试LLVM源码
LLVM系列第二十五章:简单统计一下LLVM源码行数
LLVM系列第二十六章:理解LLVMContext
LLVM系列第二十七章:理解IRBuilder
LLVM系列第二十八章:写一个JIT Hello World
LLVM系列第二十九章:写一个简单的常量加法“消除”工具(Pass)
flex&bison系列
本文目录
前言
一、操作系统
二、开发环境
三、下载源码
四、编译并安装LLVM
1. 生成项目配置
2. 编译LLVM
3. 快速试用llc工具
4. 安装LLVM
总结
前言
LLVM是一个著名的开源编译器项目、库、工具,在此记录下编译LLVM源码的过程,以备查阅。
一、操作系统
我用的操作系统是macOS 11.6.4:
System Version: macOS 11.6.4 (20G417)
Kernel Version: Darwin 20.6.0
Model Name: MacBook Pro
Model Identifier: MacBookPro15,1
Processor Name: 8-Core Intel Core i9
Processor Speed: 2.3 GHz
Number of Processors: 1
Total Number of Cores: 8
Memory: 16 GB
1
2
3
4
5
6
7
8
9
10
二、开发环境
用到的工具有Clang (Xcode)、Git、CMake以及Ninja。
关于安装Xcode, 可参考这个简易教程:
How to download Xcode DMG or XIP file? - Stack Overflow
安装后确认信息如下(示例):
% clang -v
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
1
2
3
4
5
6
对于其它工具,可借助brew (https://brew.sh) 进行安装(示例):
brew install git cmake ninja
1
安装后确认信息如下(示例):
% brew list --version git cmake ninja
cmake 3.22.1
git 2.32.0
ninja 1.10.2_1
1
2
3
4
5
三、下载源码
使用命令如下(示例):
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout -b llvmorg-12.0.0
1
2
3
确认branch的信息如下(示例):
% git branch
* llvmorg-12.0.0
main
1
2
3
4
四、编译并安装LLVM
我们可使用工具CMake和Ninja来编译LLVM源代码。
1. 生成项目配置
使用命令如下(示例):
cd llvm-project
mkdir build
cd build
# Compile clang as well
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_PROJECTS=clang ../llvm
# Or, don't compile clang
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ../llvm
1
2
3
4
5
6
7
8
9
输出log如下(示例):
-- The C compiler identification is AppleClang 13.0.0.13000029
-- The CXX compiler identification is AppleClang 13.0.0.13000029
-- The ASM compiler identification is Clang with GNU-like command-line
-- Found assembler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features - done
-- No build type selected, default to Debug
-- clang project is enabled
-- clang-tools-extra project is disabled
-- compiler-rt project is disabled
-- debuginfo-tests project is disabled
-- libc project is disabled
-- libclc project is disabled
-- libcxx project is disabled
-- libcxxabi project is disabled
-- libunwind project is disabled
-- lld project is disabled
-- lldb project is disabled
-- mlir project is disabled
-- openmp project is disabled
-- parallel-libs project is disabled
-- polly project is disabled
-- pstl project is disabled
-- flang project is disabled
-- Found libtool - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool
-- Looking for dlfcn.h - found
-- Looking for errno.h - found
-- Looking for fcntl.h - found
-- Looking for link.h - not found
-- Looking for malloc/malloc.h - found
-- Looking for pthread.h - found
-- Looking for signal.h - found
-- Looking for sys/ioctl.h - found
-- Looking for sys/mman.h - found
-- Looking for sys/param.h - found
-- Looking for sys/resource.h - found
-- Looking for sys/stat.h - found
-- Looking for sys/time.h - found
-- Looking for sys/types.h - found
-- Looking for sysexits.h - found
-- Looking for termios.h - found
-- Looking for unistd.h - found
-- Looking for valgrind/valgrind.h - not found
-- Looking for fenv.h - found
-- Looking for FE_ALL_EXCEPT - found
-- Looking for FE_INEXACT - found
-- Looking for mach/mach.h - found
-- Looking for histedit.h - found
-- Looking for CrashReporterClient.h - not found
-- Performing Test HAVE_CRASHREPORTER_INFO - Success