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

[SR-10616] SwiftPM: cross compilation broken in Swift 5.0.1 #4713

Closed
weissi opened this issue May 4, 2019 · 2 comments
Closed

[SR-10616] SwiftPM: cross compilation broken in Swift 5.0.1 #4713

weissi opened this issue May 4, 2019 · 2 comments
Labels

Comments

@weissi
Copy link
Member

weissi commented May 4, 2019

Previous ID SR-10616
Radar rdar://problem/50734652
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Package Manager
Labels Bug, 5.0Regression
Assignee None
Priority Medium

md5: d4d52b6966c0402cec3532c5816ec822

Issue Description:

I'm pretty sure that this change: fbbeffb#diff-36309c5a61d54931f5e256402f92668bL232 broke cross-compilation with SwiftPM. The -target x86_64-unknown-linux or similar is no longer added to the linking swiftc invocation.

For example, trying to cross-compile & link a basic project that you can create using swift package init --type=executable will emit the following swiftc invocations:

compile:

/tmp/cross-toolchain/swift.xctoolchain/usr/bin/swiftc -module-name foo -incremental -emit-dependencies -emit-module -emit-module-path /private/tmp/foo/.build/x86_64-unknown-linux/debug/foo.swiftmodule -output-file-map /private/tmp/foo/.build/x86_64-unknown-linux/debug/foo.build/output-file-map.json -c /private/tmp/foo/Sources/foo/main.swift -I /private/tmp/foo/.build/x86_64-unknown-linux/debug -target x86_64-unknown-linux -swift-version 5 -enable-batch-mode -index-store-path /private/tmp/foo/.build/x86_64-unknown-linux/debug/index/store -sdk /tmp/cross-toolchain/ubuntu-xenial.sdk -use-ld=lld -tools-directory /tmp/cross-toolchain/swift.xctoolchain/usr/bin -Onone -g -enable-testing -j4 -DSWIFT_PACKAGE -DDEBUG -module-cache-path /private/tmp/foo/.build/x86_64-unknown-linux/debug/ModuleCache -Xfrontend -color-diagnostics

In the compile step, everything's still good as we see -target x86_64-unknown-linux. The next swiftc invocation however (to link) is missing -target x86_64-unknown-linux:

/tmp/cross-toolchain/swift.xctoolchain/usr/bin/swiftc -sdk /tmp/cross-toolchain/ubuntu-xenial.sdk -use-ld=lld -tools-directory /tmp/cross-toolchain/swift.xctoolchain/usr/bin -g -L /private/tmp/foo/.build/x86_64-unknown-linux/debug -o /private/tmp/foo/.build/x86_64-unknown-linux/debug/foo -module-name foo -emit-executable -Xlinker '-rpath=$ORIGIN' @/private/tmp/foo/.build/x86_64-unknown-linux/debug/foo.product/Objects.LinkFileList

which then results in ld being invoked to try to link a macOS binary (when run on macOS).

@aciidb0mb3r was this deliberate?

The good news for users of the build_ubuntu_cross_compilation_toolchain script is that the fix is very simple: Just add "-target", "x86_64-unknown-linux" to the "extra-swiftc-flags" list. So your resulting ubuntu-xenial-destination.json will look similar to this:

{
    "version": 1,
    "sdk": "/tmp/cross-toolchain/ubuntu-xenial.sdk",
    "toolchain-bin-dir": "/tmp/cross-toolchain/swift.xctoolchain/usr/bin",
    "target": "x86_64-unknown-linux",
    "extra-cc-flags": [
        "-fPIC"
    ],
    "extra-swiftc-flags": [
        "-use-ld=lld", "-target", "x86_64-unknown-linux", "-tools-directory", "/tmp/cross-toolchain/swift.xctoolchain/usr/bin"
    ],
    "extra-cpp-flags": [
        "-lstdc++"
    ]
}

You might need to adopt the paths.

@belkadan
Copy link

belkadan commented May 7, 2019

Johannes and I discussed this a little with others on an Apple-internal chat, and I put forward the idea that even though it's a bit more work, the best way to get this would be to get Linux cross-compilation from macOS working without extra configuration (like splicing toolchains together). Given that we have support for this with Android, it seems like we ought to be able to get there with Linux too. At that point, we can do integration testing on a macOS host without having to build the Linux parts on a separate machine (or in a Docker environment).

@ankitspd
Copy link
Member

#2227

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 4, 2022
This issue was closed.
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

3 participants