xcode替换LLVM插件

一、查找LLVM

1、当前你使用的xcode对应的LLVM版本

复制代码
✗ swift --version
swift-driver version: 1.127.14.1 Apple Swift version 6.2.1 (swiftlang-6.2.1.4.8 clang-1700.4.4.1)
Target: arm64-apple-macosx15.0

可以看出我们使用的swift版本在6.2之后

复制代码
✗ clang --version
Apple clang version 17.0.0 (clang-1700.4.4.1)
Target: arm64-apple-darwin24.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

说明我们使用的clang在17.0

2、找到相同的版本源码

查看https://github.com/swiftlang/llvm-project/blob/swift-6.0-RELEASE/llvm/utils/gn/secondary/llvm/version.gni

可以看到其使用的llvm版本为17

复制代码
llvm_version_major = 17
llvm_version_minor = 0
llvm_version_patch = 6
llvm_version = "$llvm_version_major.$llvm_version_minor.$llvm_version_patch"

https://github.com/swiftlang/llvm-project/tags?after=swift-6.3-DEVELOPMENT-SNAPSHOT-2025-12-01-a

选择我们的的llvm源码下载:

https://github.com/swiftlang/llvm-project/releases/tag/swift-6.2.3-RELEASE 现在zip后解压出llvm-project-swift-6.2.3-RELEASE文件

二、编译llvm和插件

1、插件

插件放在在llvm-project-swift-6.2.3-RELEASE/llvm/lib/Transforms目录下,如果是有我们自己定义的插件(MyPlugin),需要在CMakeLists.txt增加add_subdirectory(MyPlugin)

2、cmake

在根目录下创建build目录,cd到该目录下,执行命令(或者写一个sh文件去运行):

复制代码
cmake -G Xcode\
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_ENABLE_PROJECTS='clang;clang-tools-extra' \
  -DLLVM_ENABLE_PLUGINS=ON \
  -DLLVM_BUILD_LLVM_DYLIB=ON \
  -DCLANG_BUILD_EXAMPLES=OFF \
  -DLLVM_TARGETS_TO_BUILD=Native \
  -DCMAKE_INSTALL_PREFIX=/usr/local/llvm-26 \
  ../llvm

会在目录下生成一个xcode工程

3、编译

打开LLVM.xcodeproj文件,选中Manage Schemes ,选择llvm、MyPlugin创建target

然后分别build,编译出产物

复制代码
build/Release/lib/MyPlugin.dylib
build/Release/bin/clang
build/Release/bin/clang++

三、替换xcode编译器

在我们业务工程的podfie中设置post hook,在pod install后修改xcconfig配置环境变量,指定为我们编译出来的clang产物

复制代码
 # 获取插件路径
  llvm_plugin_path = "....llvm-project-swift-6.2.3-RELEASE/build/Release/lib/MyIRPlugin.dylib"
  llvm_clang_path = "...llvm-project-swift-6.2.3-RELEASE/build/Release/bin/clang"
  llvm_clangpp_path = "...lllvm-project-swift-6.2.3-RELEASE/build/Release/bin/clang++"
  
  # 遍历所有 Pod 目标
  installer.pods_project.targets.each do |target|

    # 为每个目标设置编译器和插件选项
    target.build_configurations.each do |config|
      # 获取当前配置
      xcconfig_path = config.base_configuration_reference.real_path
      xcconfig = File.read(xcconfig_path)
      
      # 添加 LLVM 插件配置
      new_xcconfig = xcconfig + <<~EOF
      
        CC=#{llvm_clang_path}
        CXX=#{llvm_clangpp_path}
        OTHER_CFLAGS = $(inherited) -fpass-plugin=#{llvm_plugin_path} -O1
        OTHER_CPLUSPLUSFLAGS = $(inherited) -fpass-plugin=#{llvm_plugin_path} -O1
        
      EOF
      
      # 写回配置文件
      File.write(xcconfig_path, new_xcconfig)
    end
  end

pod install后重启xcode生效

相关推荐
周末也要写八哥8 小时前
Eclipse 2024全流程网盘下载与安装配置教程详解
java·ide·eclipse
ChampaignWolf11 小时前
在 Eclipse 中使用 Tabnine
java·ide·eclipse
诗水人间13 小时前
VsCode 中使用Copilot调用Deepseek V4模型
ide·vscode·copilot
梦想家加一14 小时前
vscode为什么下载了汉化插件却不生效
ide·vscode·编辑器
大熊猫侯佩15 小时前
升级到 macOS26.5 后看视频会自动息屏的解决
macos·操作系统
NiceCloud喜云15 小时前
Claude API 流式输出(SSE)实战:从打字机效果到工具调用全流程
java·前端·ide·人工智能·chrome·intellij-idea·状态模式
多云的夏天16 小时前
IDE-VSCODE-Continue + DeepSeek V4
ide·vscode·编辑器·deepseek
磊 子16 小时前
C++ IO 流
macos·objective-c·cocoa
Mr -老鬼16 小时前
EasyClick AI全自动编程,AI IDE选型真难?
ide·人工智能·自动化·ai编程·easyclick·易点云测
薛定猫AI16 小时前
【深度解析】Antigravity 2.0:从 AI IDE 到 Agent 编排层,Google 开发者工具栈的技术转向
ide·人工智能