This document describes how to build Swift for Windows natively. See the Windows doc for more about what is possible with Swift on Windows.
There are two supported ways to build Swift on Windows:
clang-cl
clang-cl
is recommended over MSVC for building Swift on Windows.
Although it is possible to build the compiler and the standard library with MSVC and to use those built products to compile a Swift program, it won't be possible to run the binary without separately obtaining the Swift runtime. On the other hand, clang-cl
is able to build the runtime, which makes it possible to build and run all the components required for Swift natively on Windows.
clang should be 7.0 or newer. Visual Studio 2017 is needed in all cases as it provides some of the needed headers and libraries.
clang-cl
apple/swift-cmark
into a folder named cmark
apple/swift-clang
into a folder named clang
apple/swift-llvm
into a folder named llvm
apple/swift-compiler-rt
into a folder named compiler-rt
apple/swift
into a folder named swift
apple/swift-corelibs-libdispatch
into a folder named swift-corelibs-libdispatch
apple/swift-corelibs-foundation
into a folder named swift-corelibs-foundation
apple/swift-corelibs-xctest
into a folder name swift-corelibs-xctest
apple/swift-lldb
into a folder named lldb
curl
into a folder named curl
libxml2
into a folder named libxml2
This guide assumes your sources live at the root of S:
. If your sources live elsewhere, you can create a substitution for this:
subst S: <path to sources>
git config --global core.autocrlf input S: git clone https://github.com/apple/swift-cmark cmark git clone https://github.com/apple/swift-clang clang git clone https://github.com/apple/swift-llvm llvm git clone https://github.com/apple/swift-compiler-rt compiler-rt git clone https://github.com/apple/swift git clone https://github.com/apple/swift-corelibs-libdispatch git clone https://github.com/apple/swift-corelibs-foundation git clone https://github.com/apple/swift-corelibs-xctest git clone https://github.com/apple/swift-lldb lldb git clone https://github.com/curl/curl.git curl git clone https://gitlab.gnome.org/GNOME/libxml2.git libxml2
bin64
folder to your Path
environment variable.PATH S:\icu\bin64;%PATH%
VsDevCmd -arch=amd64
If instead you're compiling for a 32-bit Windows target, adapt the arch
argument to x86
and run
VsDevCmd -arch=x86
Decide whether you want to build a release or debug version of Swift on Windows and replace the CMAKE_BUILD_TYPE
parameter in the build steps below with the correct value (Debug
, RelWithDebInfoAssert
or Release
) to avoid conflicts between the debug and non-debug version of the MSCRT library.
Set up the ucrt
, visualc
, and WinSDK
modules by copying ucrt.modulemap
located at swift/stdlib/public/Platform/ucrt.modulemap
into ${UniversalCRTSdkDir}/Include/${UCRTVersion}/ucrt
as module.modulemap
, copying visualc.modulemap
located at swift/stdlib/public/Platform/visualc.modulemap
into ${VCToolsInstallDir}/include
as module.modulemap
, and copying winsdk.modulemap
located at swift/stdlib/public/Platform/winsdk.modulemap
into ${UniversalCRTSdkDir}/Include/${UCRTVersion}/um
and setup the visualc.apinotes
located at swift/stdlib/public/Platform/visualc.apinotes
into ${VCToolsInstallDir}/include
as visualc.apinotes
mklink "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap" S:\swift\stdlib\public\Platform\ucrt.modulemap mklink "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap" S:\swift\stdlib\public\Platform\winsdk.modulemap mklink "%VCToolsInstallDir%\include\module.modulemap" S:\swift\stdlib\public\Platform\visualc.modulemap mklink "%VCToolsInstallDir%\include\visualc.apinotes" S:\swift\stdlib\public\Platform\visualc.apinotes
Warning: Creating the above links usually requires administrator privileges. The quick and easy way to do this is to open a second developer prompt by right clicking whatever shortcut you used to open the first one, choosing Run As Administrator, and pasting the above commands into the resulting window. You can then close the privileged prompt; this is the only step which requires elevation.
Debug
or some variant of Release
(e.g. Release
, RelWithDebInfo
).mkdir "S:\b\llvm" pushd "S:\b\llvm" cmake -G Ninja^ -DCMAKE_BUILD_TYPE=Release^ -DCMAKE_C_COMPILER=cl^ -DCMAKE_CXX_COMPILER=cl^ -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-windows-msvc^ -DLLVM_ENABLE_ASSERTIONS=ON^ -DLLVM_ENABLE_PDB=YES^ -DLLVM_ENABLE_PROJECTS=clang^ -DLLVM_TARGETS_TO_BUILD=X86^ S:/llvm popd cmake --build "S:\b\llvm"
path S:\b\llvm\bin;%PATH%
mkdir "S:\b\cmark" pushd "S:\b\cmark" cmake -G Ninja^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DCMAKE_C_COMPILER=cl^ -DCMAKE_CXX_COMPILER=cl^ S:\cmark popd cmake --build "S:\b\cmark"
C:\Python27
and a 64-bit python in C:\Python27amd64
. You may use either one based on your installation.mkdir "S:\b\swift" pushd "S:\b\swift" cmake -G Ninja^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DCMAKE_C_COMPILER=cl^ -DCMAKE_CXX_COMPILER=cl^ -DCMAKE_EXE_LINKER_FLAGS:STRING="/INCREMENTAL:NO"^ -DCMAKE_SHARED_LINKER_FLAGS:STRING="/INCREMENTAL:NO"^ -DSWIFT_INCLUDE_DOCS=OFF^ -DSWIFT_PATH_TO_CMARK_SOURCE="S:\cmark"^ -DSWIFT_PATH_TO_CMARK_BUILD="S:\b\cmark"^ -DLLVM_DIR=S:\b\llvm\lib\cmake\llvm^ -DClang_DIR=S:\b\llvm\lib\cmake\clang^ -DSWIFT_PATH_TO_LIBDISPATCH_SOURCE="S:\swift-corelibs-libdispatch"^ -DSWIFT_WINDOWS_x86_64_ICU_UC_INCLUDE="S:/thirdparty/icu4c-63_1-Win64-MSVC2017/include"^ -DSWIFT_WINDOWS_x86_64_ICU_UC="S:/thirdparty/icu4c-63_1-Win64-MSVC2017/lib64/icuuc.lib"^ -DSWIFT_WINDOWS_x86_64_ICU_I18N_INCLUDE="S:/thirdparty/icu4c-63_1-Win64-MSVC2017/include"^ -DSWIFT_WINDOWS_x86_64_ICU_I18N="S:/thirdparty/icu4c-63_1-Win64-MSVC2017/lib64/icuin.lib"^ -DCMAKE_INSTALL_PREFIX="C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr"^ -DPYTHON_EXECUTABLE=C:\Python27\python.exe^ S:\swift popd cmake --build "S:\b\swift"
swift
project due to an MSBuild limitation that file paths cannot exceed 260 characters. These can be ignored, as they occur after the build when writing the last build status to a file.cmake -G "Visual Studio 2017" -A x64 -T "host=x64"^ ...
mkdir "S:\b\lldb" pushd "S:\b\lldb" cmake -G Ninja^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DLLDB_ALLOW_STATIC_BINDINGS=YES^ -DLLDB_PATH_TO_CLANG_SOURCE="S:\clang"^ -DLLDB_PATH_TO_SWIFT_SOURCE="S:\swift"^ -DLLDB_PATH_TO_CLANG_BUILD="S:\b\llvm"^ -DLLDB_PATH_TO_LLVM_BUILD="S:\b\llvm"^ -DLLDB_PATH_TO_SWIFT_BUILD="S:\b\swift"^ -DLLVM_ENABLE_ASSERTIONS=ON^ -DPYTHON_HOME=%ProgramFiles(x86)%\Microsoft Visual Studio\Shared\Python37_64^ S:\lldb popd cmake --build S:\b\lldb
Running the testsuite on Windows has additional external dependencies. You must have a subset of the GNUWin32 programs installed and available in your path. The following packages are currently required:
path S:\thirdparty\icu4c-63_1-Win64-MSVC2017\bin64;S:\b\swift\bin;S:\b\swift\libdispatch-prefix\bin;%PATH%;%ProgramFiles(x86)%\GnuWin32\bin ninja -C S:\b\swift check-swift
mkdir "S:\b\libdispatch" pushd "S:\b\libdispatch" cmake -G Ninja^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DCMAKE_C_COMPILER=clang-cl^ -DCMAKE_CXX_COMPILER=clang-cl^ -DCMAKE_SWIFT_COMPILER=S:\b\swift\bin\swiftc.exe^ -DENABLE_SWIFT=ON^ -DENABLE_TESTING=OFF^ S:\swift-corelibs-libdispatch popd cmake --build S:\b\libdispatch
path S:\b\libdispatch;S:\b\libdispatch\src;%PATH%
pushd "S:\curl" .\buildconf.bat cd winbuild nmake /f Makefile.vc mode=static VC=15 MACHINE=x64 popd
pushd "S:\libxml2\win32" cscript configure.js iconv=no nmake /f Makefile.msvc popd
mkdir "S:\b\foundation" pushd "S:\b\foundation cmake -G Ninja^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DCMAKE_C_COMPILER=clang-cl^ -DCMAKE_SWIFT_COMPILER=S:\b\swift\bin\swiftc.exe^ -DCURL_LIBRARY="S:/curl/builds/libcurl-vc15-x64-release-static-ipv6-sspi-winssl/lib/libcurl_a.lib"^ -DCURL_INCLUDE_DIR="S:/curl/builds/libcurl-vc15-x64-release-static-ipv6-sspi-winssl/include"^ -DENABLE_TESTING=NO^ -DICU_ROOT="S:/thirdparty/icu4c-63_1-Win64-MSVC2017"^ -DLIBXML2_LIBRARY="S:/libxml2/win32/bin.msvc/libxml2_a.lib"^ -DLIBXML2_INCLUDE_DIR="S:/libxml2/include"^ -DFOUNDATION_PATH_TO_LIBDISPATCH_SOURCE=S:\swift-corelibs-libdispatch^ -DFOUNDATION_PATH_TO_LIBDISPATCH_BUILD=S:\b\libdispatch^ S:\swift-corelibs-foundation popd cmake --build S:\b\foundation
path S:\b\foundation;%PATH%
mkdir "S:\b\xctest" pushd "S:\b\xctest" cmake -G Ninja^ -DBUILD_SHARED_LIBS=YES^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DCMAKE_SWIFT_COMPILER=S:\b\swift\bin\swiftc.exe^ -DXCTEST_PATH_TO_COREFOUNDATION_BUILD=S:\b\foundation\CoreFoundation-prefix^ -DXCTEST_PATH_TO_FOUNDATION_BUILD=S:\b\foundation^ -DXCTEST_PATH_TO_LIBDISPATCH_SOURCE=S:\swift-corelibs-libdispatch^ -DXCTEST_PATH_TO_LIBDISPATCH_BUILD=S:\b\libdispatch^ -DLIT_COMMAND=S:\llvm\utils\lit\lit.py^ -DPYTHON_EXECUTABLE=C:\Python27\python.exe^ S:\swift-corelibs-xctest popd cmake --build S:\b\xctest
path S:\b\xctest;%PATH%
ninja -C S:\b\xctest check-xctest
mkdir "S:\b\foundation" pushd "S:\b\foundation cmake -G Ninja^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DCMAKE_C_COMPILER=clang-cl^ -DCMAKE_SWIFT_COMPILER=S:\b\swift\bin\swiftc.exe^ -DCURL_LIBRARY="S:/curl/builds/libcurl-vc15-x64-release-static-ipv6-sspi-winssl/lib/libcurl_a.lib"^ -DCURL_INCLUDE_DIR="S:/curl/builds/libcurl-vc15-x64-release-static-ipv6-sspi-winssl/include"^ -DENABLE_TESTING=YES^ -DICU_ROOT="S:/thirdparty/icu4c-63_1-Win64-MSVC2017"^ -DLIBXML2_LIBRARY="S:/libxml2/win32/bin.msvc/libxml2_a.lib"^ -DLIBXML2_INCLUDE_DIR="S:/libxml2/include"^ -DFOUNDATION_PATH_TO_LIBDISPATCH_SOURCE=S:\swift-corelibs-libdispatch^ -DFOUNDATION_PATH_TO_LIBDISPATCH_BUILD=S:\b\libdispatch^ -DFOUNDATION_PATH_TO_XCTEST_BUILD=S:\b\xctest^ S:\swift-corelibs-foundation popd cmake --build S:\b\foundation
cmake --build S:\b\foundation ninja -C S:\b\foundation test
ninja -C S:\b\swift install
C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin
) to the PATH
environment variable.To use cl
instead, just replace the -DCMAKE_C_COMPILER
and -DCMAKE_CXX_COMPILER
parameters to the cmake
invocations.