LLVM编译

2021-01-14  本文已影响0人  HotPotCat

环境

方式一:源码编译

获取源码

最好科学上网/改镜像/改hosts,根据自己的情况处理,否则会很慢。

git clone --depth 1 https://github.com/llvm/llvm-project.git
image.png

配置构建LLVM&Clang

新版mac系统默认shellzsh,如果不是改成自己对应的shell里面导入就好了。

echo 'export OSX_COMMANDLINE_SDKROOT="/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"' >> ~/.zshrc

source ~/.zshrc

安装CMake

brew install cmake

开始构建

创建目录:

cd llvm-project
mkdir build
cd build

构建命令:

cmake -G Xcode -j 2 -DLLVM_ENABLE_PROJECTS='libcxx;libc++;clang;lldb' -DLLDB_USE_SYSTEM_DEBUGSERVER=ON -DLLDB_TEST_COMPILER=clang++ -DCMAKE_OSX_SYSROOT=$OSX_COMMANDLINE_SDKROOT ../llvm

cmake -G <generator> [options] ../llvm
Ninja:大多数llvm开发人员都使用Ninja
Unix Makefiles:用于生成与make兼容的并行makefile
Visual Studio:用于生成Visual Studio项目解决方案
Xcode:用于生成Xcode项目
-j参数指定使用的cpu核心数量。例如,这里指定2。根据自己的电脑配置选择。具体在(关于本机->概览)中。

错误


image.png
CMake Warning at /usr/local/Cellar/cmake/3.19.1/share/cmake/Modules/Platform/Darwin-Initialize.cmake:294 (message):
  Ignoring CMAKE_OSX_SYSROOT value:

   /Library/Developer/CommandLi neTools/SDKs/MacOSX.sdk

  because the directory does not exist.
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.19.1/share/cmake/Modules/CMakeSystemSpecificInitialize.cmake:21 (include)
  CMakeLists.txt:49 (project)

CommandlineTools找不到,装一下:

xcode-select --install
image.png

然后继续,确保以下3个项目是enabled状态。否则需要重新检查下环境和配置。

image.png

enabled表示可用,disabled表示禁用。这块最后会和编译好的LLVM.xcodeprojTargets相关。

image.png

大概10分钟左右检测映射完成,检查下log有没有错误信息。

image.png
这个时候build目录大概50M
image.png

Xcode配置

打开build目录,LLVM.xcodeproj工程。
打开的时候选择Manually Manage Schemes,否则会引入不必要的schem影响Xcode速度。原则上是使用哪一个,引入哪一个。
选择Target lldb

image.png
运行(Build + Run(command + R)):
第一次运行时,需要进行编译,以重新生成调试符号,下次再运行即可直接运行。
error: module importing failed: This script interpreter does not support importing modules.
error: module importing failed: This script interpreter does not support importing modules.
error: this file does not represent a loadable dylib
(lldb) 

lldb编译确实成功了,但是有3个错误。看下文件大小23G

image.png
错误解决,既然是lldb报错了,那就看下对应的.lldbinit,发现确实存在导入插件。
command script import /usr/local/opt/chisel/libexec/fbchisellldb.py
command script import /opt/LLDB/lldb_commands/dslldb.py
plugin load  /Users/binxiao/lldbcat/libfooplugin.dylib

注释掉:

image.png
这个是由于LLDB插件导致的错误。注释掉后Run without Building成功。
(lldb) po 5 % 10
5

(lldb) 

编译报错

这里有一点是如果Xcode编译过程中报错,command + shift + k 和清除driveData是没有用的。这接删除llvm-project/build/build/XCBuildData目录中的文件,然后Xcode就可以Command + R重新编译了。

方式二

我们想迁移到其它电脑或者下载给别人使用的时候可以直接将编译好的源码压缩拷贝,解压后重新构建(和源码编译构建命令相同)。
建议构建前删除CMakeCache.txt文件:

image.png
否则有可能报错(和编译好的不是同一个目录就会报错,目录相同则不需要删除):
image.png
CMake Error: The current CMakeCache.txt directory /Users/binxiao/improve/1/llvm-project/build/CMakeCache.txt is different than the directory /Users/binxiao/improve/llvm-project/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
CMake Error: The source "/Users/binxiao/improve/1/llvm-project/llvm/CMakeLists.txt" does not match the source "/Users/binxiao/improve/llvm-project/llvm/CMakeLists.txt" used to generate cache.  Re-run cmake with a different source directory.

根本原因是缓存编译好的目录文件对应不上,这里编译通过后配置Xcode就好了(和源码方式相同)。

https://github.com/llvm/llvm-project/tree/llvmorg-11.0.1
https://clang.llvm.org/get_started.html

上一篇下一篇

猜你喜欢

热点阅读