首页
社区
课程
招聘
[原创]【笔记】LLVM编译及XCode集成
发表于: 2022-9-7 11:18 17713

[原创]【笔记】LLVM编译及XCode集成

2022-9-7 11:18
17713

一.下载

llvm_source.zip

二. 安装

1
2
3
4
5
6
7
8
cd [项目根目录]
#解压.zip
unzip llvm_project_12.0.0.zip
#创建构建目录
mkdir llvm_project_build
cd llvm_project_build
#使用XCode模版进行工程编译
cmake -S ../llvm-project/llvm -B ./ -G Xcode -DLLVM_ENABLE_PROJECTS="clang"

三. 编译

  1. 打开LLVM.xcodeproj
  2. 选择Automatically Create Schemes
  3. 创建Custom Aggregate
  4. 进入Build Phases
  5. 点击Dependencies
  6. 添加opt,clang以及需要编译的pass
  7. command+B 开始编译

四. 解决Pass无法加载

使用LLVM工程自带实例LLVMHello

路径:根目录/llvm_project/llvm/lib/transforms/Hello/Hello.cpp

  1. 打开[CustomPass].cpp
  2. 添加两个头文件
    1
    2
    #include "llvm/IR/LegacyPassManager.h"
    #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  3. 添加自动注册Pass代码

    1
    2
    3
    4
    5
    6
    static void registerSkeletonPass(const PassManagerBuilder &, legacy::PassManagerBase &PM)
    {
       PM.add(new Hello());
    }
     
    static RegisterStandardPasses RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible, registerSkeletonPass);

五. 在XCode中添加Pass

  1. 打开Project选择Target
  2. 点击Build Settings
  3. 添加User-Defined,命名为CC,值为根目录/llvm_project-build/Debug/bin/clang
  4. 添加User-Defined,命名为CXX,值为根目录/llvm_project-build/Debug/bin/clang++
  5. 点击other c flags,添加-Xclang -load -Xclang /Volumes/T.SSD/LLVM/llvm-project-build/Debug/lib/[Pass名称].dylib
  6. 点击enable index-while-building funcationality,将属性值改为NO
  7. command+B编译
    • 编译出错function does not return or could not build module **
      • 点击other c flags,追加-DNS_FORMAT_ARGUMENT(A)= -D_Nullable_result=_Nullable,重新进行编译
    • 成功后会打印
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      Hello: \001+[OMGameHandle shared]
      Hello: _dispatch_once
      Hello: __22+[GHandle shared]_block_invoke
      Hello: \001-[GameHandle setup]
      Hello: \001-[GameHandle agreeProtocol]
      Hello: \001-[GameHandle initWithStatus:]
      Hello: \001-[GameHandle loginWithSuccess:]
      Hello: \001-[GameHandle loginWithSignout]
      Hello: \001-[GameHandle loginWithFailure:]
      Hello: \001-[GameHandle login]
      Hello: \001-[GameHandle setupBlock]
      Hello: \001-[GameHandle setSetupBlock:]
      Hello: \001-[GameHandle vc]
      Hello: \001-[GameHandle setVc:]
      Hello: \001-[GameHandle .cxx_destruct]

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//