Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different linker issue: SwiftPM uses swiftc and Xcode use clang #6512

Closed
Kyle-Ye opened this issue May 3, 2023 · 6 comments
Closed

Different linker issue: SwiftPM uses swiftc and Xcode use clang #6512

Kyle-Ye opened this issue May 3, 2023 · 6 comments
Labels

Comments

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented May 3, 2023

Description

The normal build for a executableTarget with linkerSettings of ["-iframework", "/System/Library/PrivateFrameworks/"] will call ld through clang which have no problem identifying "-iframework".

 /Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -Xlinker -reproducible -target arm64-apple-macos11.0 -isysroot /Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -L/Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Intermediates.noindex/EagerLinkingTBDs/Debug -L/Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Products/Debug -L/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F/Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Intermediates.noindex/EagerLinkingTBDs/Debug -F/Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Products/Debug/PackageFrameworks -F/Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Products/Debug -F/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -iframework /Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -filelist /Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Intermediates.noindex/KeychainExtractor.build/Debug/KeychainExtractor.build/Objects-normal/arm64/KeychainExtractor.LinkFileList -Xlinker -rpath -Xlinker /usr/lib/swift -Xlinker -rpath -Xlinker @executable_path/../lib -Xlinker -object_path_lto -Xlinker /Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Intermediates.noindex/KeychainExtractor.build/Debug/KeychainExtractor.build/Objects-normal/arm64/KeychainExtractor_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -fobjc-link-runtime -L/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L/usr/lib/swift -Xlinker -add_ast_path -Xlinker /Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Intermediates.noindex/KeychainExtractor.build/Debug/KeychainExtractor.build/Objects-normal/arm64/KeychainExtractor.swiftmodule -Xlinker -rpath -Xlinker /Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Products/Debug/PackageFrameworks -iframework /System/Library/PrivateFrameworks/ -framework Sharing -Xlinker -dependency_info -Xlinker /Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Intermediates.noindex/KeychainExtractor.build/Debug/KeychainExtractor.build/Objects-normal/arm64/KeychainExtractor_dependency_info.dat -o /Users/kyle/Library/Developer/Xcode/DerivedData/airdrop-keychain-extractor-crdebtyusoqjledrgjyzawqtfxwh/Build/Products/Debug/KeychainExtractor

However if we start the build via packageManager.build in a CommandPlugin, it will use .build/release.yaml file's content to do the linking. And it will call ld through swiftc which have problem identifying "-iframework".

  "C.KeychainExtractor-release.exe":
    tool: shell
    inputs: ["/Users/kyle/Workspace/OpenDrop/airdrop-keychain-extractor/.build/arm64-apple-macosx/release/KeychainExtractor.build/KeychainExtractor.swift.o","/Users/kyle/Workspace/OpenDrop/airdrop-keychain-extractor/.build/arm64-apple-macosx/release/Sharing.build/Dummy.c.o"]
    outputs: ["/Users/kyle/Workspace/OpenDrop/airdrop-keychain-extractor/.build/arm64-apple-macosx/release/KeychainExtractor"]
    description: "Linking ./.build/arm64-apple-macosx/release/KeychainExtractor"
    args: ["/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc","-g","-L","/Users/kyle/Workspace/OpenDrop/airdrop-keychain-extractor/.build/arm64-apple-macosx/release","-o","/Users/kyle/Workspace/OpenDrop/airdrop-keychain-extractor/.build/arm64-apple-macosx/release/KeychainExtractor","-module-name","KeychainExtractor","-emit-executable","-Xlinker","-dead_strip","-Xlinker","-alias","-Xlinker","_KeychainExtractor_main","-Xlinker","_main","-Xlinker","-rpath","-Xlinker","@loader_path","@/Users/kyle/Workspace/OpenDrop/airdrop-keychain-extractor/.build/arm64-apple-macosx/release/KeychainExtractor.product/Objects.LinkFileList","-Xlinker","-rpath","-Xlinker","/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx","-target","arm64-apple-macosx11.0","-framework","Sharing","-iframework","/System/Library/PrivateFrameworks/","-sdk","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk","-F","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks","-I","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib","-L","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib","-Xcc","-isysroot","-Xcc","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk","-Xcc","-F","-Xcc","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks","-Xcc","-fPIC","-sdk","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk","-F","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks","-I","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib","-L","/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib","-L","/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib"]

Expected behavior

Build call success on command plugin.

Actual behavior

Build call fail on command plugin.

Steps to reproduce

  1. Clone https://github.com/Kyle-Ye/airdrop-keychain-extractor
  2. Check to 791c0018d88a7b1f113fed7444f4781fa9d25a6d
  3. Run swift package codesign and enter yes.
git clone https://github.com/Kyle-Ye/airdrop-keychain-extractor
cd airdrop-keychain-extractor
git checkout 791c0018d88a7b1f113fed7444f4781fa9d25a6d
swift package codesign

Swift Package Manager version/commit hash

release/5.8

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0
Darwin MacBook-Pro-6.local 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64
@Kyle-Ye Kyle-Ye added the bug label May 3, 2023
@Kyle-Ye Kyle-Ye changed the title Plugin build fail for "-iframework" Command Plugin fail to build product with "-iframework" link setting May 3, 2023
@neonichu
Copy link
Contributor

neonichu commented May 3, 2023

Is this really specific to plugins or to swift build? AFAIK, we always use swiftc as the linker, but Xcode's build system uses clang.

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented May 3, 2023

Is this really specific to plugins or to swift build? AFAIK, we always use swiftc as the linker, but Xcode's build system uses clang.

So is this an intended behavior? Then how should we add link symbol framework search path via swiftc? We can use -iframework when using clang.

And if the two use/support different argument parameters, how can we diff them in Package.swift file?

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented May 3, 2023

Is this really specific to plugins or to swift build? AFAIK, we always use swiftc as the linker, but Xcode's build system uses clang.

Oh, Yes. I just try to build it directly via swift run and it is also falling for error: unknown argument: ‘-iframework’.

So the problem is swiftc does not support -iframework. And if we build it via Xcode, it will use clang instead of swiftc.

So the problem has nothing to do with SPM plugin.

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented May 7, 2023

Find the corresponding flag for swiftc is -Fsystem.

So we need to use something like the following snippest. This is very strange for me.

linkerSettings: [
   #if "Xcode env / linking with clang"
   .unsafeFlags(["-Fsystem", "/System/Library/PrivateFrameworks/"]),
   #elseif "SwiftPM env / linking with swiftc"
   .unsafeFlags(["-iframework", "/System/Library/PrivateFrameworks/"]),
   #endif
]

Is there a way to make Xcode IDE also using "swiftc" when linking or make SwiftPM using "clang" when linking. (Because I'd like to support both Xcode and CLI SPM to build the package) @neonichu

@Kyle-Ye Kyle-Ye changed the title Command Plugin fail to build product with "-iframework" link setting Different linker issue: SPM use swiftc and Xcode use clang May 7, 2023
@MaxDesiatov MaxDesiatov changed the title Different linker issue: SPM use swiftc and Xcode use clang Different linker issue: SwiftPM uses swiftc and Xcode use clang May 7, 2023
@neonichu
Copy link
Contributor

neonichu commented May 9, 2023

I think this has come up before, the solution has to be some sort of conditionality of unsafe flags, as we do not want to allow users to choose the linker.

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Jun 21, 2023

@Kyle-Ye Kyle-Ye closed this as completed Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants