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

When using an xcodeproj (such as generated by cargo xcode) one should not use the SwiftLinker but rather build the package through the xcodeproj #49

Open
franklyonnet opened this issue May 14, 2023 · 7 comments

Comments

@franklyonnet
Copy link

Not really a bug but something to add into the docs. It took me a while to figure out what was going on...

When building for an ios target on a macos device, the swift build command generated by SwiftLinker is ignoring the -target specification. See below the command built by cargo from within a xcodeproj built by cargo-xcode.
As per my findings trying to wrap the swift command with xcrun as suggested in some threads, or adding a swift -arch with or without -Xswiftc, or prepending the command with export ARCHS= doesn't change the outcome.

A workaround (and probably the best course of action) is to build the package within the xcodeproject itself :
1 - Drag the package folder which contains the Package.swift into your Xcode project
2 - Click the Plus button in the "Link Binary with Libraries" section, locate the package in the modal dialog, select the gray library icon inside the package, and add this one.

                  command: cd "apple/EdamameNative" && "swift" "build" "-c" "release" "--build-path"
                  "/Users/flyonnet/Library/Developer/Xcode/DerivedData/Runner-gyjydghdoilsppddiyqqpyifgfkp/Build/Intermediates.noindex/edamame_core.buil
                  d/cargo_target/aarch64-apple-ios/release/build/edamame_core-7be057f67cfcb7b2/out/swift-rs/EdamameNative" "-Xswiftc" "-sdk" "-Xswiftc"
                  "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.4.sdk" "-Xswiftc" "-target"
                  "-Xswiftc" "aarch64-apple-ios"
                  error: 'edamamenative': Invalid manifest (compiled with:
                  ["/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc", "-vfsoverlay",
                  "/var/folders/s9/vlddfbs96d12sz5jgrhg3js40000gn/T/TemporaryDirectory.XDdAsN/vfs.yaml", "-L",
                  "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription",
                  "-Xlinker", "-rpath", "-Xlinker",
                  "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI", "-target",
                  "arm64-apple-macosx12.0", "-sdk",
                  "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.4.sdk", "-F",
                  "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks", "-I",
                  "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib", "-L",
                  "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib", "-swift-version", "5", "-I",
                  "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI", "-sdk",
                  "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.4.sdk",
                  "-package-description-version", "5.7.0",
                  "/Users/flyonnet/Temporaire/Programming/edamame/rust/userspace/apple/EdamameNative/Package.swift", "-Xfrontend",
                  "-disable-implicit-concurrency-module-import", "-Xfrontend", "-disable-implicit-string-processing-module-import", "-o",
                  "/var/folders/s9/vlddfbs96d12sz5jgrhg3js40000gn/T/TemporaryDirectory.bYOOTT/edamamenative-manifest"])
                  <unknown>:0: warning: using sysroot for 'iPhoneOS' but targeting 'MacOSX'
                  <unknown>:0: error: unable to load standard library for target 'arm64-apple-macosx12.0'
                  thread 'main' panicked at 'Failed to compile swift package EdamameNative',
@Zack-Xb
Copy link

Zack-Xb commented Jul 3, 2023

I have the same exact problem, nothing seems to make -target work.
Did you have to put the package in a specific directory of the xcodeproject?

@Zack-Xb
Copy link

Zack-Xb commented Jul 5, 2023

The problem here lies within the swift package manager, good thread here

@Brendonovich
Copy link
Owner

I don't understand much of what is going on here - would one of you mind creating a reproduction repo?

@Brendonovich
Copy link
Owner

If I'm not mistaken, isn't SwiftLinker creating the correct build command?

swift build
    -c release
    --build-path "/Users/flyonnet/Library/Developer/Xcode/DerivedData/Runner-gyjydghdoilsppddiyqqpyifgfkp/Build/Intermediates.noindex/edamame_core.build/cargo_target/aarch64-apple-ios/release/build/edamame_core-7be057f67cfcb7b2/out/swift-rs/EdamameNative"
    -Xswiftc "-sdk"
    -Xswiftc "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.4.sdk" 
    -Xswiftc "-target"
    -Xswiftc "aarch64-apple-ios"

It seems like Xcode is producing the wrong target:

swiftc
    -vfsoverlay "/var/folders/s9/vlddfbs96d12sz5jgrhg3js40000gn/T/TemporaryDirectory.XDdAsN/vfs.yaml"
    -L "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI"
    -lPackageDescription
    -Xlinker "-rpath"
    -Xlinker "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI"
    -target "arm64-apple-macosx12.0"
    -sdk "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.4.sdk"
    -F "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks"
    -I  "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib"
    -L "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib"
    -swift-version 5
    -I "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI"
    -sdk "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.4.sdk"

Seems like Xcode is specifying the correct -sdk but specifying the arm64-apple-macosx12.0 target and macOS frameworks to link against.
Maybe it's supposed to be like this, I'm not sure.

@guillemcordoba
Copy link

@Brendonovich I bumped into the same issue, and after doing a lot of research, I was able to fix the issue by adding these args to the build script.

@Zack-Xb could you try again in your project by pointing to that fork? If this solves it I'll make a PR to this repository.

This is what I added to my Cargo.toml to make a tauri app work:

[patch.crates-io]
swift-rs = { git = "https://github.com/guillemcordoba/swift-rs", branch = "main" }

@Brendonovich
Copy link
Owner

@guillemcordoba Oh that's interesting, I don't understand much about this error but if that solves it then I'd be happy to merge your branch. Would you be able to explain a bit about how/why it works?

@guillemcordoba
Copy link

Mmm I wouldn't know actually, I don't have any experience with xcode, swift or anything iOS related. This was just something I found to work after a full day of research around forums and stack overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants