Building iOS/Mac Projects With Open Source LLVM

I'd like to use the Clang AST tools for iOS/Mac projects. I want to write C++ programs that link to the Clang libraries. I will use that to analyze the AST from my IOS/Mac projects. I understand you can generate an AST dump from Apple's Clang but I instead want the full capabilities of the Clang libraries, not just the AST output.

I believe I need to build compilation databases in order for many Clang libraries to be used. For instance, l'd like to use the CommonOptionsParser which requires arguments for the compilation database. Building these compilation databases is where I'm running to challenges.

Its my understanding I will need to use the same LLVM version I'm using for the tooling (open source 17.x) to also build my compilation database. I already tried to use Apple's build-in LLVM to build the database but that led to build arguments such as -index-store-path being included in the JSON, which I don't think Open source LLVM can understand (failing to build).

I instead attempted to use the Open Source LLVM installation with my iOS/Mac project. I got it kind of working but am not quite there. My question is whether this approach is worth pursuing? I'm dealing a lot of build errors doing this, particularly in nested swift packages that build fine with Apple Clang.

Before I try to fix all these errors, I just wanted to ask whether using Open Source LLVM to build compilation databases for iOS/Mac projects is the right high-level approach for my goals mentioned initially? Is anyone doing this for complicated projects?

Another option I thought of is maybe I can "clean up" the compilation database that is generated from Apple's Clang to remove the -index-store-path arguments. Then I could use that as an input to Open Source Clang.

I made progress on this but am still not sure if I'm swimming upstream trying to build my own Clang that runs just like Apple's. Here's what I've learned:

  • Apple has a fork of LLVM which is open source.
  • There are CMake files in that repo to help build like Apple does (README)
  • I had success in building Clang from that repo with those CMake files. I was able to build/run an iOS app with resulting Clang (using default branch of their LLVM fork)

Despite this development, I hit errors when compiling an Xcode project using the resulting toolchain for some project configurations. One example building a large Xcode project:

ld: warning: Could not find or use auto-linked library 'swiftCompatibility56': library 'swiftCompatibility56' not found
ld: warning: Could not find or use auto-linked library 'swiftCompatibilityPacks': library 'swiftCompatibilityPacks' not found
ld: warning: Could not find or use auto-linked framework 'CoreAudioTypes': framework 'CoreAudioTypes' not found
ld: Undefined symbols:
  __swift_FORCE_LOAD_$_swiftCompatibility56, referenced from:
      __swift_FORCE_LOAD_$_swiftCompatibility56_$_LoopKit in CgmEventStore.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here's how I'm building the LLVM/Clang things.

  cmake -G "Unix Makefiles" \
    -C "$apple_cmake_path" \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_INSTALL_PREFIX=~/Library/Developer/ \
    -DLLVM_APPEND_VC_REV=on \
    -DLLVM_CREATE_XCODE_TOOLCHAIN=on \
    -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt" \
    -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64" \
    -DDARWIN_ios_ARCHS=AArch64 -DCOMPILER_RT_ENABLE_IOS=On \
    "$apple_llvm_repo_path/llvm"
  make -j4

I'm just doing a reality check here that I'm pursuing the right approach? Has anyone else successfully built Clang the Apple way?

from the doc: Apple's branching scheme for llvm-project https://github.com/apple/llvm-project/blob/apple/main/apple-docs/AppleBranchingScheme.md

if you want to build swift project, you should use toolchain build from the [swift/*] branch, not [apple/main] branch ? not sure about that

Building iOS/Mac Projects With Open Source LLVM
 
 
Q