编译llvm源码

  1. 前往LLVM官网,下载LLVM17.0.6版本的源码

image-20240312102929980

  1. 下载源码后,解压到任意目录:

image-20240312103618878

  1. llvm-project-17.0.6.src同级目录下,编写编译脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh
pwd_path=`pwd`
build_llvm=${pwd_path}/build-llvm #编译目录
installprefix=${pwd_path}/install #install目录
llvm_project=${pwd_path}/llvm-project-17.0.6.src/llvm #项目目录

mkdir -p $build_llvm
mkdir -p $installprefix

cmake -G Ninja -S ${llvm_project} -B $build_llvm \
-DLLVM_ENABLE_PROJECTS="clang" \
-DLLVM_CREATE_XCODE_TOOLCHAIN=on \
-DLLVM_INSTALL_UTILS=ON \
-DCMAKE_INSTALL_PREFIX=$installprefix \
-DCMAKE_BUILD_TYPE=Release

ninja -C $build_llvm install-xcode-toolchain

image-20240312104224290

  1. 执行编译脚本:
1
2
$ chmod +x ./build.sh
$ ./build.sh

编译过程需要大约20分钟左右。

  1. 编译完成之后,即可在当前目录下的install目录下看到编译产物:

![image-20240312110827212](./编译llvm源码/toolchains.png)

  1. LLVM17.0.6.xctoolchain文件复制到~/Library/Developer/Toolchains/目录下:

image-20240312111512942

  1. 点击菜单栏Xcode——>Toolchains,选择org.llvm.17.0.6:

![image-20240312111306102](/Users/zengxiangxiang/Library/Application Support/typora-user-images/image-20240312111306102.png)

  1. 在Xcode的Build Settings中,关闭Enable Index-While-Building Functionality

image-20240312111734850

  1. Command+B编译(或Command + R 运行):

image-20240312111950520