swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.85k stars 10.3k forks source link

Most of the Swift source now built into the Swift compiler does not have a cross-compilation CMake configuration #71507

Open finagolfin opened 5 months ago

finagolfin commented 5 months ago

Motivation

The Swift source in SwiftCompilerSources/ has long supported cross-compilation, added by @eeckstein in #40068 for Darwin a couple years ago and then in #40277 for non-Darwin Unix by me. However, the recently added Swift source in lib/{ASTGen,Macros}/ and tools/swift-plugin-server/ does not support cross-compilation, and the new swift-syntax parser never has. Before we start requiring the swift-syntax parser to build the Swift compiler, we better get it cross-compiling, as cross-compilation will be the only way to get the Swift compiler running on new platforms after that.

Proposed solution

I cross-compile the Swift compiler for Android AArch64 from a linux x86_64 host, and these are the three changes I had to make to get all this Swift source in the compiler itself cross-compiled:

  1. Add cross-compilation flags for Android to _add_host_swift_compile_options(), which is used to build lib/{ASTGen,Macros}/ and tools/swift-plugin-server/. This should be easy to generalize like SwiftCompilerSources/ does.
  2. Do the same in the swift-syntax repo with add_swift_syntax_library(): this may take more work to generalize because of the cross-repo CMake config.
  3. Cross-compiling swift-plugin-server with add_pure_swift_host_tool() and 1. doesn't work, so I hacked it to use the old add_swift_host_tool() instead. I forget the precise error but it had to do with its swiftLLVMJSON dependency in turn linking a C++ library, swiftBasic, so the tool wasn't actually pure Swift. That works fine when natively compiling, but not when cross-compiling.

I can probably implement 1. easily for non-Darwin Unix, but I'm looking for input from @rintaro and @bnbarham on how we should properly fix the other two, plus I don't use Darwin or Windows so others will have to get cross-compilation working again there.

finagolfin commented 4 months ago

I asked directly on the forum what the plan is and was told that a switch to using CMake's built-in Swift support is planned, so I will hold off on my changes building on the existing cross-compilation support and see what ends up rolling out with that new effort first.