Uploaded image for project: 'Swift'
  1. Swift
  2. SR-10616

SwiftPM: cross compilation broken in Swift 5.0.1

    XMLWordPrintable

    Details

      Description

      I'm pretty sure that this change: https://github.com/apple/swift-package-manager/commit/fbbeffbb87bc626cf589fb373c7ce89aa625818a#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).

      Ankit Aggarwal 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.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            jw Johannes Weiss
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: