Build instructions

I'm trying to follow the build instructions for Windows (swift/WindowsBuild.md at main · apple/swift · GitHub), but encountered a few questions:

  1. Regarding the following dependencies:

     --add Component.CPython3.x64 ^
     --add Microsoft.VisualStudio.Component.Git ^
    

    I've already installed Python and Git using winget. Can I assume these are fine, or do I need to reinstall them using Visual Studio?

  2. What's the difference between these two? Do I need to install exactly version 17763, or can I just use whatever version is most recent?

      --add Microsoft.VisualStudio.Component.Windows10SDK ^
      --add Microsoft.VisualStudio.Component.Windows10SDK.17763
    
  3. Regarding Python: The VS installer says version 3.7.x is no longer supported and recommends installating 3.9.x. Will that work?

  4. The apple/llvm-project repo no longer has a swift/main branch. Which branch should I clone instead?

  5. (Looking ahead a bit as I stopped after the previous issue). Where should I get the dependencies ICU, SQLite3, curl, libxml2 and zlib?

Thanks!

  1. Those should be fine but will not match what the official builds use. That means that you may need to adjust things accordingly for Python.

  2. You can install any version of the SDK that you like, you will need to adjust the copy-paste instructions accordingly (if there are cases that have values hardcoded).

  3. 3.9.x is fine, the newer Visual Studio package that I believe.

  4. stable/20211026

  5. You can build them from source from the respective project's upstream.

I believe the update-checkout script is fully functional on Windows. Shall we update the guide to make use of it?

I've found that it doesn't always work for me. Additionally, it really doesn't help with the windows build in my experience due to the lack of the dependencies.

I would look into utilizing vcpkg to install all the dependencies. Fortunately it should solve that problem and make Windows build a lot easier.

Note that this will diverge from the official builds - they build all the dependencies from source but have a different way to check out and build the dependencies. I don't mind if there are secondary directions, but the build everything from source should be the recommended way.

CI will remain building everything from source IMO. It doesn’t mind if we change WindowsBuild.md because the guide for Linux has been suggesting apt for years.

Sure, but because build-script-impl is bash, it cannot support Windows, so making the documentation match the builds is important to make it easier for others to reproduce the builds locally.

I also tried to build Swift on Windows some months ago and took some notes there. Some details of my notes might be helpful in addition to the mentioned build instructions, but please keep in mind that some details might be outdated or even wrong as I do not consider myself an expert for some of the involved topics. (I hope I will have time to return to the topic of building Swift soon.)

A quick update on this thread: I’ve started the work to revise WindowsBuild.md to keep it up-to-date and as developer-friendly as possible. The progress is collected at this WIP PR where you’re welcome to leave any opinion.

So far I have validated the build process up to all the core libraries, and the next goal is SwiftPM. Hopefully we should have a guide for anyone to try out every piece of Swift on Windows.

I really recommend that you take a look at swift-build/WindowsQuickStart.md at master · compnerd/swift-build (github.com) which really is a far simpler, easier, and more actively maintained approach. It is far more comprehensive, and more closely replicates the official builds.

1 Like

We’ve got plenty of build scripts for Windows as of now, but none of these is flexible enough to be a substitute of build-script. These scripts maintain different approaches, regarding different purposes, and even have different requirements on host environment.

Documentation is sometimes more important than codes. I believe this is exactly the case when we’re talking about “building a toolchain”. We need a single source of truth for guiding a developer to build, debug and test any part of the toolchain they want, and WindowsBuild.md is the closest to this goal.

Not every developer is willing to build the whole toolchain for debugging a single piece of it. Not every developer is familiar with CMake as senior developers like you. Before we have an equivalent of the easy-to-use build-script for Windows, documenting the existing building process will always be meaningful.

Sure, maintaining documentation is reasonable. I'm just recommending that rather than having the documentation be a duplicate of the script, make it entirely prose, leaving the developer to reconstruct the command line from scratch. The build path that I pointed out should be considered the canonical way to build on Windows - in fact, the build-windows-toolchain.bat is just an extension of that path, adding in the packaging step which I don't think is valuable for day-to-day development (and thus is split into two separate scripts).

Ultimately, the "planty" of scripts amount to one path - the same thing which that document covers.

After ~2 weeks of work, the refreshed WindowsBuild.md is finally ready for review. For anyone who’s interested, feel free to take a look and review the PR.

2 Likes

I tried to follow the instructions but the build fails when linking Foundation. Something about CoreFoundation module not found.

Is this because the build uses mostly development branches and the build is not always stable? Maybe we need two repo setups, one for development and one the is stable from the latest release.

This doesn't really give any information on what is going on. Please provide the build log.

It is unlikely to be the culprit in this case. CoreFoundation is internal to Foundation and never gets exposed. Additionally, the CI seems healthy as well.

S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:8:8: note: previously defined here
module DispatchIntrospection [system] [extern_c] {
       ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:13:8: error: redefinition of module 'CDispatch'
module CDispatch [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:13:8: note: previously defined here
module CDispatch [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-foundation\Sources\Foundation\Bridging.swift:13:29: error: could not build C module 'CoreFoundation'
@_implementationOnly import CoreFoundation
                            ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:1:8: error: redefinition of module 'Dispatch'
module Dispatch {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:1:8: note: previously defined here
module Dispatch {
       ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:8:8: error: redefinition of module 'DispatchIntrospection'
module DispatchIntrospection [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:8:8: note: previously defined here
module DispatchIntrospection [system] [extern_c] {
       ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:13:8: error: redefinition of module 'CDispatch'
module CDispatch [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:13:8: note: previously defined here
module CDispatch [system] [extern_c] {
       ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:1:8: error: redefinition of module 'Dispatch'
module Dispatch {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:1:8: note: previously defined here
module Dispatch {
       ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:8:8: error: redefinition of module 'DispatchIntrospection'
module DispatchIntrospection [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:8:8: note: previously defined here
module DispatchIntrospection [system] [extern_c] {
       ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\include\dispatch\module.modulemap:13:8: error: redefinition of module 'CDispatch'
module CDispatch [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-libdispatch\dispatch\module.modulemap:13:8: note: previously defined here
module CDispatch [system] [extern_c] {
       ^
S:\SourceCache\swift-corelibs-foundation\Sources\Foundation\Bridging.swift:13:29: error: could not build C module 'CoreFoundation'
@_implementationOnly import CoreFoundation
                            ^
ninja: build stopped: subcommand failed.

That is expected behaviour. You cannot use a system with a release version of the toolchain to build the toolchain, it must be an isolated environment where you have never installed Swift or used any of the release builds. You must build everything from source.

Can't the build system specifically point out the built version of Swift and not the installed release version?

Id really rather not start introducing such special cases in the build system, especially since this is not universally true (e.g. Darwin would not have such a case since dispatch cannot/does not get built).