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?


@Finagolfin:

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.

It's bug in swift-driver (5.6-5.7) - path for build tools for Windows hardcoded to .exe. Swift driver must be recompiled with patch:

diff -rupN original/Sources/SwiftDriver/Toolchains/WindowsToolchain.swift new/Sources/SwiftDriver/Toolchains/WindowsToolchain.swift
--- original/Sources/SwiftDriver/Toolchains/WindowsToolchain.swift	2022-09-12 10:01:08.000000000 +0300
+++ new/Sources/SwiftDriver/Toolchains/WindowsToolchain.swift	2022-09-16 11:57:36.000000000 +0300
@@ -67,29 +67,34 @@ extension WindowsToolchain.ToolchainVali
   }
 
   private func lookupToolPath(_ tool: Tool) throws -> AbsolutePath {
+#if os(Windows)
+      let ext = ".exe"
+#else
+      let ext = ""
+#endif
     switch tool {
     case .swiftAPIDigester:
-      return try lookup(executable: "swift-api-digester.exe")
+      return try lookup(executable: "swift-api-digester" + ext)
     case .swiftCompiler:
-      return try lookup(executable: "swift-frontend.exe")
+      return try lookup(executable: "swift-frontend" + ext)
     case .staticLinker:
-      return try lookup(executable: "lld-link.exe")
+      return try lookup(executable: "lld-link" + ext)
     case .dynamicLinker:
-      return try lookup(executable: "clang.exe")
+      return try lookup(executable: "clang" + ext)
     case .clang:
-      return try lookup(executable: "clang.exe")
+      return try lookup(executable: "clang" + ext)
     case .clangxx:
-      return try lookup(executable: "clang++.exe")
+      return try lookup(executable: "clang++" + ext)
     case .swiftAutolinkExtract:
-      return try lookup(executable: "swift-autolink-extract.exe")
+      return try lookup(executable: "swift-autolink-extract" + ext)
     case .lldb:
-      return try lookup(executable: "lldb.exe")
+      return try lookup(executable: "lldb" + ext)
     case .dsymutil:
-      return try lookup(executable: "llvm-dsymutil.exe")
+      return try lookup(executable: "llvm-dsymutil" + ext)
     case .dwarfdump:
-      return try lookup(executable: "llvm-dwarfdump.exe")
+      return try lookup(executable: "llvm-dwarfdump" + ext)
     case .swiftHelp:
-      return try lookup(executable: "swift-help.exe")
+      return try lookup(executable: "swift-help" + ext)
     }
   }

Replacing script:

#!/bin/bash -e
WORK="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

BUILD_DIR=$WORK/driver
SRC=$BUILD_DIR/src
# replace SWIFT_TOOLCHAIN path to your swift cross-toolchain
SWIFT_TOOLCHAIN=$WORK/windows.msvc.xctoolchain/llvm

mkdir -p $SRC
cd $BUILD_DIR

SWIFTVERSION=5.7
ARGPARSERVERSION=1.1.4
SWIFTSYSTEMVERSION=1.2.1
YAMSVERSION=5.0.1
TSCVERSION=0.2.7

URL=https://github.com/apple/swift-driver/archive/refs/tags/swift-$SWIFTVERSION-RELEASE.tar.gz
if [ ! -f $SRC/swift-driver-$SWIFTVERSION-RELEASE.tar.gz ] ; then
    wget -O $SRC/swift-driver-$SWIFTVERSION-RELEASE.tar.gz $URL
fi

tar xjf $SRC/swift-driver-$SWIFTVERSION-RELEASE.tar.gz
mv swift-driver-swift-$SWIFTVERSION-RELEASE swift-driver
cd swift-driver
patch -p1 < $WORK/swift-driver-crosscompile-fix.diff
cd ..

URL=https://github.com/apple/swift-argument-parser/archive/refs/tags/$ARGPARSERVERSION.tar.gz
if [ ! -f $SRC/swift-argument-parser-$ARGPARSERVERSION.tar.gz ] ; then
    wget -O $SRC/swift-argument-parser-$ARGPARSERVERSION.tar.gz $URL
fi

tar xjf $SRC/swift-argument-parser-$ARGPARSERVERSION.tar.gz
mv swift-argument-parser-$ARGPARSERVERSION swift-argument-parser

URL=https://github.com/apple/swift-system/archive/refs/tags/$SWIFTSYSTEMVERSION.tar.gz
if [ ! -f $SRC/swift-system-$SWIFTSYSTEMVERSION.tar.gz ] ; then
    wget -O $SRC/swift-system-$SWIFTSYSTEMVERSION.tar.gz $URL
fi

tar xjf $SRC/swift-system-$SWIFTSYSTEMVERSION.tar.gz
mv swift-system-$SWIFTSYSTEMVERSION swift-system

if [ ! -d llbuild ] ; then
    git clone https://github.com/apple/swift-llbuild llbuild
else
    cd llbuild
    git pull
    cd ..
fi

URL=https://github.com/jpsim/Yams/archive/refs/tags/$YAMSVERSION.tar.gz
if [ ! -f $SRC/yams-$YAMSVERSION.tar.gz ] ; then
    wget -O $SRC/yams-$YAMSVERSION.tar.gz $URL
fi

tar xjf $SRC/yams-$YAMSVERSION.tar.gz
mv Yams-$YAMSVERSION yams

URL=https://github.com/apple/swift-tools-support-core/archive/refs/tags/$TSCVERSION.tar.gz
if [ ! -f $SRC/swift-tools-support-core-$TSCVERSION.tar.gz ] ; then
    wget -O $SRC/swift-tools-support-core-$TSCVERSION.tar.gz $URL
fi

tar xjf $SRC/swift-tools-support-core-$TSCVERSION.tar.gz
mv swift-tools-support-core-$TSCVERSION swift-tools-support-core

swift-driver/Utilities/build-script-helper.py build --package-path $BUILD_DIR/swift-driver --build-path $BUILD_DIR/build --configuration release --toolchain /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr --ninja-bin /usr/local/bin/ninja --cmake-bin /usr/local/bin/cmake --local_compiler_build

cd $BUILD_DIR/build/release/bin

install_name_tool -delete_rpath $BUILD_DIR/build/release/lib swift-driver
install_name_tool -delete_rpath $BUILD_DIR/build/release/dependencies/swift-tools-support-core/lib swift-driver
install_name_tool -delete_rpath $BUILD_DIR/build/release/dependencies/swift-system/lib swift-driver
install_name_tool -add_rpath @executable_path/../lib/swift/macosx swift-driver

rm -fr "$SWIFT_TOOLCHAIN/bin/swift-driver"
cp "$BUILD_DIR/build/release/bin/swift-driver" "$SWIFT_TOOLCHAIN/bin/swift-driver"
rm -fr "$SWIFT_TOOLCHAIN/lib/swift/macosx/libSwiftDriver.dylib"
rm -fr "$SWIFT_TOOLCHAIN/lib/swift/macosx/libSwiftDriverExecution.dylib"
rm -fr "$SWIFT_TOOLCHAIN/lib/swift/macosx/libSwiftOptions.dylib"
mv "$BUILD_DIR/build/release/lib/libSwiftDriver.dylib" "$SWIFT_TOOLCHAIN/lib/swift/macosx/"
mv "$BUILD_DIR/build/release/lib/libSwiftDriverExecution.dylib" "$SWIFT_TOOLCHAIN/lib/swift/macosx/"
mv "$BUILD_DIR/build/release/lib/libSwiftOptions.dylib" "$SWIFT_TOOLCHAIN/lib/swift/macosx/"

# remove tmp files
#rm -fr $BUILD_DIR

echo Complete!
2 Likes

Care to file a GitHub Issue?

Reported just today:

Hope some day will be fixed in swift 5.7.1 or later.

1 Like

Thanks! This is something I’ve been curious about recently. Glad to learn it’s possible!

Is this possible ?

It is possible, though there are questions about the legal aspects (is copying the SDK permitted?). Without the Microsoft SDK content, it is not possible.

Are there any reliable examples of how to cross compile for windows from Mac or Linux, available? (Even with user supplied Microsoft sdk files?)

I have a cross platform GUI app that also works flawlessly on windows (and as a side note, android too) when I compile it on windows 10 with Microsoft VSC2022 and the 5.8 tool chain you released.

Im trying to automate the CI for my various artifacts and I tried setting up a wine / wineskin env to compile directly with the windows 5.8 tool chain but it was complaining that I didn't have samcli.dll.

Cross compiling seems like my best bet if I don't want to do it on an official windows emulator which is cumbersome.

Microsoft is quite finicky about which part of Visual Studio is allowed to be distributed independently cf. the Microsoft documentation for some aspects. Else, it might be possible to work without a Visual Studio installation on Windows, which would also be quite nice, but see some recent discussion in another forums topic.

If nobody else knows better, I would not count on cross compiling for Windows, also because there are other more important issues concerning Swift on Windows (regex literals, macros).

To my knowledge you can do CI pipelines for Windows on GitHub (it might a good idea to configure it to use cmd instead of power shell), or see what is possible with a (cost-free?) Azure account.

I do not think that wine would be good enough.

2 Likes

Thanks for your insight. Yeah going a ci route with windows machines is where im going to land regardless. I just wanted to see if I could find a quick win by leveraging cross compiling for regular regression testing on a cheaper interface.

Realistically speaking, whether I can cross compile for windows (or even mac) on say, a Linux tool chain with the necessary platform sdk, I would only be using that to ensure my code still compiles fine. I would be leveraging a windows machine (and a mac) for the final binary deliverable.

Right now I cross compile for android using a swift tool chain built for android, from inside an Ubuntu docker container. I'd love to be able to do something like that for windows.

Then the inverse is easier to sustain - just cross-compile Linux on Windows. In fact, this would be easier the other way since Windows is setup for cross-compilation (I've done Windows to Android cross-compilation a number of times though haven't done a recent SDK release for that). That is, you could use a Windows build for everything but Darwin targets.

1 Like

You're probably right. I guess I should suck it up and explore a viable windows vm ci target sooner than later.

I hold out hope that one day we can cross compile for every target from Linux like how you can cross compile for Darwin with this example GitHub - tpoechtrager/osxcross: Mac OS X cross toolchain for Linux, FreeBSD, OpenBSD and Android (Termux)

Do you know if theres a way to install the windows tool chain on windows without manually selecting the installer buttons? So it can be performed on a headless install? Or do I need to build the tool chain on the windows host instead of just running a pre built installer?

Yes, the installer can be installed in a headless manner, see GitHub - compnerd/gha-setup-swift: Setup Swift (on Windows) on GitHub Actions Builders which automates the setup for GHA agents.

1 Like