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, they are
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 use those built products to compile a Swift program, it won't be possible to run the binary without seperately 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-cl
Path
environment variableapple/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
into a folder named swift
icu
in the same directory as the other Swift project repositories.src/win32/allinone.sln
in Visual Studio.<icu-source>/bin
folder to your Path
environment variable.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
/
) instead of backslashes (\
) as the path separators. clang
breaks with backslashed paths.set swift_source_dir=path-to-directory-containing-all-cloned-repositories
mkdir "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64" pushd "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64" cmake -G "Ninja" "%swift_source_dir%/cmark" popd cmake --build "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64/"
compiler-rt
below, which should give faster build times.mklink /J "%swift_source_dir%/llvm/tools/clang" "%swift_source_dir%/clang" mklink /J "%swift_source_dir%/llvm/tools/compiler-rt" "%swift_source_dir%/compiler-rt" mkdir "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64" pushd "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64" cmake -G "Ninja"^ -DLLVM_ENABLE_ASSERTIONS=TRUE^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DLLVM_TOOL_SWIFT_BUILD=NO^ -DLLVM_INCLUDE_DOCS=TRUE^ -DLLVM_TOOL_COMPILER_RT_BUILD=TRUE^ -DLLVM_BUILD_EXTERNAL_COMPILER_RT=TRUE^ -DLLVM_LIT_ARGS=-sv^ -DLLVM_TARGETS_TO_BUILD=X86^ "%swift_source_dir%/llvm" popd cmake --build "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"
bin
directory in an environment variable so it can be used to build Swift. Assuming you followed the instructions exactly, the path below is correct, but it may be different based on your build variant and platform, so double check.set llvm_bin_dir="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64/bin"
SWIFT_WINDOWS_LIB_DIRECTORY
parameter depending on your target platform or Windows SDK version.mkdir "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64" pushd "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64" cmake -G "Ninja" "%swift_source_dir%/swift"^ -DCMAKE_BUILD_TYPE=Debug^ -DSWIFT_PATH_TO_CMARK_SOURCE="%swift_source_dir%/cmark"^ -DSWIFT_PATH_TO_CMARK_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64"^ -DSWIFT_CMARK_LIBRARY_DIR="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64/src"^ -DSWIFT_PATH_TO_LLVM_SOURCE="%swift_source_dir%/llvm"^ -DSWIFT_PATH_TO_LLVM_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ -DSWIFT_PATH_TO_CLANG_SOURCE="%swift_source_dir%/llvm/tools/clang"^ -DSWIFT_PATH_TO_CLANG_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ -DICU_UC_INCLUDE_DIR="%swift_source_dir%/icu/include"^ -DICU_UC_LIBRARY_DIRS="%swift_source_dir%/icu/lib64"^ -DICU_I18N_INCLUDE_DIR="%swift_source_dir%/icu/include"^ -DICU_I18N_LIBRARY_DIRS="%swift_source_dir%/icu/lib64"^ -DICU_UC_LIB_NAME="icuuc"^ -DICU_I18N_LIB_NAME="icuin"^ -DSWIFT_INCLUDE_DOCS=FALSE^ -DSWIFT_INCLUDE_TESTS=FALSE^ -DCMAKE_C_COMPILER="%llvm_bin_dir%/clang-cl.exe"^ -DCMAKE_CXX_COMPILER="%llvm_bin_dir%/clang-cl.exe"^ -DCMAKE_C_FLAGS="-fms-compatibility-version=19.00 /Z7"^ -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.00 -Z7" ^ -DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER=FALSE popd cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64"
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 15" "%swift_source_dir%/swift"^ -DCMAKE_GENERATOR_PLATFORM="x64"^ ...
mkdir "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/lldb-windows-amd64" pushd "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/lldb-windows-amd64" cmake -G "Ninja" "%swift_source_dir%/lldb"^ -DCMAKE_BUILD_TYPE=RelWithDebInfo^ -DLLDB_PATH_TO_CMARK_SOURCE="%swift_source_dir%/cmark"^ -DLLDB_PATH_TO_CMARK_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64"^ -DLLDB_PATH_TO_LLVM_SOURCE="%swift_source_dir%/llvm"^ -DLLDB_PATH_TO_LLVM_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ -DLLDB_PATH_TO_CLANG_SOURCE="%swift_source_dir%/clang"^ -DLLDB_PATH_TO_CLANG_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ -DLLVM_ENABLE_ASSERTIONS=YES popd cmake --build "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/lldb-windows-amd64"
Follow instructions 1-6 for clang-cl
, but run the following instead to build Swift
mkdir "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64" pushd "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64" cmake -G "Ninja" "%swift_source_dir%/swift"^ -DCMAKE_BUILD_TYPE=Debug^ -DSWIFT_PATH_TO_CMARK_SOURCE="%swift_source_dir%/cmark"^ -DSWIFT_PATH_TO_CMARK_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64"^ -DSWIFT_CMARK_LIBRARY_DIR="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/cmark-windows-amd64/src"^ -DSWIFT_PATH_TO_LLVM_SOURCE="%swift_source_dir%/llvm"^ -DSWIFT_PATH_TO_LLVM_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ -DSWIFT_PATH_TO_CLANG_SOURCE="%swift_source_dir%/llvm/tools/clang"^ -DSWIFT_PATH_TO_CLANG_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ -DICU_UC_INCLUDE_DIRS="%swift_source_dir%/icu/include"^ -DICU_UC_LIBRARY_DIRS="%swift_source_dir%/icu/lib64"^ -DICU_I18N_INCLUDE_DIRS="%swift_source_dir%/icu/include"^ -DICU_I18N_LIBRARY_DIRS="%swift_source_dir%/icu/lib64"^ -DICU_UC_LIB_NAME="icuuc"^ -DICU_I18N_LIB_NAME="icuin"^ -DSWIFT_INCLUDE_DOCS=FALSE^ -DSWIFT_INCLUDE_TESTS=FALSE^ -DSWIFT_BUILD_DYNAMIC_SDK_OVERLAY=FALSE^ -DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER=FALSE popd cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64"