Cross-Compiling from MacOS to Windows

Hi,

I’m trying to cross-compile a simple Swift executable from MacOS 12 to Windows 10. So far I’ve:

  • created a simple Swift package using swift package init --type executable.
  • downloaded sdk-windows-x64 and toolchain-windows-x64 from the swift-build Pipelines.
  • created a destination.json :
{
    "version": 1,
    "sdk": "/Users/lebje/Programs/Windows/CrossCompileTest/sdk-windows-x64/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/",
    "sysroot-flag": "/Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/",
    "toolchain-bin-dir": "/Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/",
    "target": "x86_64-unknown-windows-msvc",
    "dynamic-library-extension": "dll",
    "extra-cc-flags": [
        "-DUNICODE=1",
        "-isystem",
        "/Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/lib/clang/12.0.0/include/",
    ],
    "extra-swiftc-flags": [
        "-target", "x86_64-unknown-windows-msvc",
        "-sdk", 
        "/Users/lebje/Programs/Windows/CrossCompileTest/sdk-windows-x64/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/",
        "-tools-directory",
        "/Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/",
        "-use-ld=lld",
        "-Xcc",
        "-isystem",
        "-Xcc",
        "/Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/lib/clang/12.0.0/include"
        
    ],
    "extra-cpp-flags": []
} 

Using Swift 5.5.2 on Ubuntu Linux 20.04 and MacOS 12:

$ swift build --destination destination.json
SwiftDriver/Driver.swift:2419: Fatal error: Windows target not supported yet

With the latest development snapshot (swift-DEVELOPMENT-SNAPSHOT-2021-12-23) on Ubuntu Linux 20.04 and MacOS 12:

$ swift build --destination destination.json
error: failed to retrieve frontend target info

I found the first error interesting because Torust was able to cross-compile with a similar destination.json in 2020.

Are there flags that I’m missing in my destination.json, or has support for cross-compilation to Windows recently been removed?

Jeff

I doubt cross‐compiling from macOS to Windows was ever possible. The other thread you linked was about cross‐compiling from Linux running inside Windows to build for native use directly in Windows. It only worked because it was able to reach outside the Linux emulator to read some necessary information from the Windows environment. For legal reasons those components of Windows cannot be provided for development use from other platforms.

I (and probably the others in that thread) abandoned cross‐compilation once SwiftPM was able to run natively on Windows. Switching to native compilation immediately fixed a host of bugs and quirks and flaky behaviour. It is also much simpler.

The cross‐compilation support has not been deliberately removed, but it never achieved official support to begin with, so it would not surprise me if it has fallen into disrepair now that better options exist.

Even if it is possible, I have no idea what the modern destination.json file would need to contain.

Heh, so you're using Swift on Mac or Linux to control a Windows toolchain inside the Crossover virtual Windows environment and cross-compiling for Windows that way? Impressive that you even got that configured. :smiley:

It's not surprising that driving the newer Windows toolchain snapshot with Swift 5.5.2 on the mac/linux side didn't work, though I have done that inadvertently on my Android CI before and it mostly worked. Try running with the -v verbose flag and see what compiler commands failed.

@SDGGiesbrecht:

What information is needed from the Windows environment? Is there something the Swift compiler is doing that requires it to need this information?

What kind of problems were you experiencing? Building object files? Linking? Crashes when running the program?


@Buttaface:

Default Xcode 13.2.1 toolchain:

$ swift build --destination destination.json -v
/usr/bin/xcrun --sdk macosx --show-sdk-platform-path
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -print-target-info
/usr/bin/xcrun --sdk macosx --find xctest
/usr/bin/xcrun --find swiftc
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -print-target-info
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-supported-features /var/folders/w4/v5rqp2cd4cd1m_6swgt8fp800000gp/T/TemporaryDirectory.QduwEk/dummyInput-1.swift
unable to restore state from /Users/lebje/Programs/Windows/CrossCompileTest/.build/workspace-state.json; unsupported schema version 5
/usr/bin/xcrun --find clang
Planning build
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -print-target-info
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI/libPackageDescription.dylib
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -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-macosx11.0 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.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/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -package-description-version 5.5.0 /Users/lebje/Programs/Windows/CrossCompileTest/Package.swift -Xfrontend -disable-implicit-concurrency-module-import -o /var/folders/w4/v5rqp2cd4cd1m_6swgt8fp800000gp/T/TemporaryDirectory.DcLPPc/crosscompiletest-manifest
/usr/bin/sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow file-write*
    (subpath "/Users/lebje/Library/Caches/org.swift.swiftpm/manifests")
)
' /var/folders/w4/v5rqp2cd4cd1m_6swgt8fp800000gp/T/TemporaryDirectory.DcLPPc/crosscompiletest-manifest -fileno 6
/usr/bin/xcrun --sdk macosx --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk macosx --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk iphoneos --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk appletvos --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk watchos --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-supported-features /var/folders/w4/v5rqp2cd4cd1m_6swgt8fp800000gp/T/TemporaryDirectory.ARq7wZ/dummyInput-2.swift
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -module-name CrossCompileTest -incremental -emit-dependencies -emit-module -emit-module-path /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/CrossCompileTest.build/CrossCompileTest.swiftmodule -output-file-map /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/CrossCompileTest.build/output-file-map.json -c /Users/lebje/Programs/Windows/CrossCompileTest/Sources/CrossCompileTest/main.swift -I /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug -target x86_64-unknown-windows-msvc -swift-version 5 -enable-batch-mode -index-store-path /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/index/store -target x86_64-unknown-windows-msvc -sdk /Users/lebje/Programs/Windows/CrossCompileTest/sdk-windows-x64/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/ -tools-directory /Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/ -use-ld=lld -Xcc -isystem -Xcc /Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/lib/clang/12.0.0/include -Onone -enable-testing -g -j8 -DSWIFT_PACKAGE -DDEBUG -module-cache-path /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/ModuleCache -parseable-output -Xfrontend -entry-point-function-name -Xfrontend CrossCompileTest_main -color-diagnostics
SwiftDriver/Driver.swift:2419: Fatal error: Windows target not supported yet%

swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a:

$ swift build --destination destination.json -v
/usr/bin/xcrun --sdk macosx --show-sdk-platform-path
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swiftc -print-target-info
/usr/bin/xcrun --sdk macosx --find xctest
/usr/bin/xcrun --find swiftc
/Library/Developer/Toolchains/swift-9--'['.L-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-supported-features /var/folders/w4/v5rqp2cd4cd1m_6swgt8fp800000gp/T/TemporaryDirectory.XB0IT1/dummyInput-1.swift
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swiftc -print-target-info
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -print-target-info
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-supported-features /var/folders/w4/v5rqp2cd4cd1m_6swgt8fp800000gp/T/TemporaryDirectory.RQBHoW/dummyInput-2.swift
/usr/bin/xcrun --find clang
Planning build
/usr/bin/xcrun --sdk macosx --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk macosx --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk iphoneos --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk appletvos --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
/usr/bin/xcrun --sdk watchos --show-sdk-platform-path
/usr/bin/xcrun vtool -show-build /Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest
Building for debugging...
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-01-06-a.xctoolchain/usr/bin/swiftc -module-name CrossCompileTest -incremental -emit-dependencies -emit-module -emit-module-path /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/CrossCompileTest.build/CrossCompileTest.swiftmodule -output-file-map /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/CrossCompileTest.build/output-file-map.json -c /Users/lebje/Programs/Windows/CrossCompileTest/Sources/CrossCompileTest/main.swift -I /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug -target x86_64-unknown-windows-msvc -swift-version 5 -enable-batch-mode -index-store-path /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/index/store -Onone -enable-testing -g -j8 -DSWIFT_PACKAGE -DDEBUG -module-cache-path /Users/lebje/Programs/Windows/CrossCompileTest/.build/x86_64-unknown-windows-msvc/debug/ModuleCache -parseable-output -color-diagnostics -target x86_64-unknown-windows-msvc -sdk /Users/lebje/Programs/Windows/CrossCompileTest/sdk-windows-x64/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/ -tools-directory /Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/ -use-ld=lld -Xcc -isystem -Xcc /Users/lebje/Programs/Windows/CrossCompileTest/toolchain-windows-x64/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/lib/clang/12.0.0/include
error: failed to retrieve frontend target info

Running the swiftc commands directly and adding --verbose unfortunately does not produce any more output.

I think it is mostly related to proprietary system C headers that are transitive dependencies of the standard library, but I do not know the exhaustive list of reasons.

There were originally a few linker failures, with nested enumerations I think. But mostly it was runtime crashes caused by compiler and linker bugs. They were 100% reproducible with any particular affected function, but seemingly unpredictable with regards to what source code was and was not safe to write. Some of the bugs remain, but many simply vanished after switching to native builds. Of the remaining bugs that have been isolated, most involve the intersection of generic types and complicated protocols.

Heh, I had a long response to you 80% written and then my 4G connection cut out and this crappy forum software lost what I had written. Here we go again.

Looks like the problem is that your setup on macOS is not respecting the "toolchain-bin-dir" you gave it and trying to cross-compile with the mac toolchain instead. Do you see the same problem on linux?

I haven't used Crossover in a long time: does it let you run Windows command-line executables like this Swift toolchain for Windows directly from the macOS shell? If not, this setup isn't going to work.

You should try running the Windows toolchain directly in a Windows command prompt in Crossover, whether Powershell or their new Terminal, without any cross-compilation or JSON file and make sure that works first, before trying to drive it from the host SPM with this complicated config.

Finally, some of the flags you're passing to the destination JSON won't be recognized, such as "sysroot-flag" or "dynamic-library-extension," don't know where you got those from. These are the seven flags that SPM recognizes in a destination JSON, you can see an example of how I use them to cross-compile packages from linux to Android.

Terms of Service

Privacy Policy

Cookie Policy