Make Xcode use sourcekitd in custom toolchain

Hi all, I'm using alternative toolchain built from source in Xcode and I want Xcode to launch sourcekitd.framework embedded in the alternative toolchain.
But it turns out Xcode will check codesign (or macOS 's library validation?) of sourcekitd.framework.
Has anyone encountered this issue?

SourceKit error log:

error loading sourcekitd from /Users/bytedance/Library/Developer/Toolchains/my.xctoolchain: failed loading compiler framework: dlopen(/Users/bytedance/Library/Developer/Toolchains/my.xctoolchain/usr/lib/sourcekitd.framework/sourcekitd, 0x0104): tried: '/Users/bytedance/Library/Developer/Toolchains/my.xctoolchain/usr/lib/sourcekitd.framework/sourcekitd' (code signature in <4700A83D-FE76-3DC7-A19D-7D3FBD77BB9A> '/Users/bytedance/Library/Developer/Toolchains/my.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' not valid for use in process: mapping process and mapped file (non-platform) have different Team IDs), '/System/Library/Frameworks/sourcekitd.framework/sourcekitd' (no such file), '/Users/bytedance/Library/Developer/Toolchains/my.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' (code signature in <4700A83D-FE76-3DC7-A19D-7D3FBD77BB9A> '/Users/bytedance/Library/Developer/Toolchains/my.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' not valid for use in process: mapping process and mapped file (non-platform) have different Team IDs), '/System/Library/Frameworks/sourcekitd.framework/Versions/A/sourcekitd' (no such file)

Hi @jajadude,

That behavior is surprising to me and I couldn’t reproduce it for a locally-built toolchain. How did you build your toolchain?

Also, from the error messages, it looks like your toolchain is code-signed while mine isn’t. Could you try building a toolchain that is not code-signed or remove the code signature from sourcekitd.framework using codesign --remove-signature and see if that changes things?

Hi Alex, thanks for your reply.

How did you build your toolchain?

My branch is swift-5.5.3-RELEASE and
I build my toolchain with preset like this:

[preset: my-preset]

mixin-preset=

buildbot_osx_package,no_assertions,no_test,use_os_runtime

# append

skip-test-foundation

skip-test-indexstore-db

skip-test-ios

skip-test-ios-host

skip-test-libdispatch

skip-test-libicu

skip-test-linux

skip-test-osx

skip-test-sourcekit-lsp

skip-test-toolchain-benchmarks

skip-test-tvos

skip-test-tvos-host

skip-test-watchos

skip-test-watchos-host

skip-test-xctest

llvm-install-components=all

it looks like your toolchain is code-signed while mine isn’t.

That's because I codesign the framework manually. The error log shows "/path/to/toolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' not valid for use in process: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.),"
in the first place, so I tried to sign it by myself and it seems Xcode needs sourcekitd to be signed by Apple or it won't load.

I couldn’t reproduce it for a locally-built toolchain

You mean you checked out the SourceKit log on your machine and the sourcekitd is loaded successfully by Xcode right? That's weird.

Interesting, I wonder if this is affected by system integrity protection. Could you check if you are able to load a non-code-signed version of sourcekitd.framework if you disable System Integrity protection: Disabling and Enabling System Integrity Protection | Apple Developer Documentation?

I built a toolchain using a command line invocation like the following (slightly sanitized because I’ve got my own weird little build setup)

/Users/alex/swift-src/swift/utils/build-script 
--sccache
--ios
--bootstrap=hosttools
'--extra-cmake-options=-DSWIFT_SOURCEKIT_USE_INPROC_LIBRARY:BOOL=TRUE'
--reconfigure
--install-swift
--release

Hi Alex, I tried your build configuration ( -DSWIFT_SOURCEKIT_USE_INPROC_LIBRARY:BOOL=TRUE) and the error became

2022-03-22 20:46:23.688 Xcode[78934:819120] SourceKit-client: [2:toolchain:40451: 0.4599] error loading sourcekitd from 
/Library/Developer/Toolchains/swift-BD-2022-03-22-standard_swift-no_assertions-no_test.xctoolchain: 
failed loading compiler framework: dlopen(/Library/Developer/Toolchains/swift-BD-2022-03-22-standard_swift-no_assertions-no_test.xctoolchain/usr/lib/sourcekitd.framework/sourcekitd, 0x0104): 
tried: '/Library/Developer/Toolchains/swift-BD-2022-03-22-standard_swift-no_assertions-no_test.xctoolchain/usr/lib/sourcekitd.framework/sourcekitd' (code signature in <4E011807-62F2-372A-94F5-A42AF210508B> 
'/Library/Developer/Toolchains/swift-BD-2022-03-22-standard_swift-no_assertions-no_test.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' 
not valid for use in process: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.), 
'/System/Library/Frameworks/sourcekitd.framework/sourcekitd' (no such file), 
'/Library/Developer/Toolchains/swift-BD-2022-03-22-standard_swift-no_assertions-no_test.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' (code signature in <4E011807-62F2-372A-94F5-A42AF210508B> 
'/Library/Developer/Toolchains/swift-BD-2022-03-22-standard_swift-no_assertions-no_test.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' not valid for use in process: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.), 
'/System/Library/Frameworks/sourcekitd.framework/Versions/A/sourcekitd' (no such file)

and

sourcekit: [2:sourcekitd_initialize:15627: 0.0000] initializing2022-03-22 20:46:23.770 Xcode[78934:819113] SourceKit-client: [2:toolchain:15627: 0.5415] 
error loading sourcekitd from /Library/Developer/Toolchains/swift-5.5.3-RELEASE.xctoolchain: failed loading compiler framework: dlopen(/Library/Developer/Toolchains/swift-5.5.3-RELEASE.xctoolchain/usr/lib/sourcekitd.framework/sourcekitd, 0x0104): 
tried: '/Library/Developer/Toolchains/swift-5.5.3-RELEASE.xctoolchain/usr/lib/sourcekitd.framework/sourcekitd' (code signature in <CEB13BBC-8328-3F55-BA54-D8256B7DE85E> '/Library/Developer/Toolchains/swift-5.5.3-RELEASE.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' not valid for use in process: mapping process is a platform binary, but mapped file is not), 
'/System/Library/Frameworks/sourcekitd.framework/sourcekitd' (no such file), '/Library/Developer/Toolchains/swift-5.5.3-RELEASE.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' (code signature in <CEB13BBC-8328-3F55-BA54-D8256B7DE85E> 
'/Library/Developer/Toolchains/swift-5.5.3-RELEASE.xctoolchain/usr/lib/sourcekitd.framework/Versions/A/sourcekitd' not valid for use in process: mapping process is a platform binary, but mapped file is not), 
'/System/Library/Frameworks/sourcekitd.framework/Versions/A/sourcekitd' (no such file)

That’s interesting. Could you maybe share the toolchain you built so I can play around with it on my machine? That might make debugging easier for me.

Could you maybe try debugging with the official swift release like https://download.swift.org/swift-5.6-release/xcode/swift-5.6-RELEASE/swift-5.6-RELEASE-osx.pkg ? I've tried and the official release will report similar errors.

That is very surprising. I haven’t heard any similar reports before and don’t see the issue either. Some more ideas/questions:

  • What do you have set in System Preferences -> Security & Privacy -> General -> Allow apps downloaded from. I suspect that this needs to be “App Store and identified developers”
  • Is this on an Intel Mac or Apple Silicon machine?
  • Which Xcode and macOS version are you using?

It's "App Store and identified developers".

I'm using an Apple Silicon machine. (But I've asked fellows who use Intel Macs and it seems they have similar issues)

Xcode 13.2.1 and macOS Monterey 12.1

I just tried reproducing it again but didn’t see any issues. Here’s what I tried:

I didn’t see any error messages in the terminal regarding to code signing. Did you perform any other steps to hit this issue?