Building for Windows requires that the Visual Studio environment variables are setup similar to the values on Windows. The following assumes that WINKIT_ROOT
points to the path where the Windows 10 SDK is available and that VC_ROOT
points to the path where the Visual Studio VC headers and libraries are available. Currently, the runtime has been tested to build against the Windows 10 SDK at revision 10.10.586.
export WINKIT_ROOT=".../Windows Kits/10" export VC_ROOT=".../Microsoft Visual Studio 14.0/VC" export INCLUDE='${VC_ROOT}/include;${WINKIT_ROOT}/Include/10.0.10586.0/ucrt;${WINKIT_ROOT}/Include/10.0.10586.0/um;${WINKIT_ROOT}/Include/10.0.10586.0/shared' export LIB='${VC_ROOT}/lib;${WINKIT_ROOT}/Lib/10.0.10586.0/ucrt/x86;${WINKIT_ROOT}/Lib/10.0.10586.0/um/x86'
visualc
and ucrt
modulesThe visualc.modulemap
located at swift/stdlib/public/Platform/visualc.modulemap
needs to be copied into ${VC_ROOT}/include
. The ucrt.modulemap
located at swift/stdlib/public/Platform/ucrt.modulemap
needs to be copied into ${WINKIT_ROOT}/Include/10.0.10586.0/ucrt
.
Ensure that we use the tools from the just built LLVM and clang tools to build the Windows SDK. You will need to pass a few extra options to cmake via the build-script
invocation to achieve this. You will need to expand out the path where llvm-ar and llvm-ranlib are built. These are needed to correctly build the static libraries. Note that cross-compiling will require the use of lld. Ensure that lld-link.exe (lld-link) is available to clang via your path. Additionally, the ICU headers and libraries need to be provided for the build.
--extra-cmake-options=-DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER=FALSE,-DCMAKE_AR=<path to llvm-ar>,-DCMAKE_RANLIB=<path to llvm-ranlib>,-DSWIFT_SDKS=WINDOWS,-DSWIFT_WINDOWS_ICU_I18N_INCLUDE=<path to ICU i18n includes>,-DSWIFT_WINDOWS_ICU_UC_INCLUDE=<path to ICU UC includes>,-DSWIFT_WINDOWS_ICU_I18N_LIB=<path to ICU i18n lib>,-DSWIFT_WINDOWS_ICU_UC_LIB=<path to ICU UC lib>
Path
environment variableProgramming Languages|Visual C++
, and Windows and Web Development|Universal Windows App Development|Windows SDK
in your installation.compiler-rt
.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
into a folder named swift
icu
in the same directory as the other Swift project repositories.src/win32/allinone.sln
in Visual Studio.Build Solution
.<icu-source>/bin
folder to your Path
environment variable.VsDevCmd -arch=amd64
set swift_source_dir=path-to-directory-containing-all-cloned-repositories
mkdir "%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64" pushd "%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64" cmake -G "Ninja" "%swift_source_dir%/cmark" popd cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64/"
mklink /J "%swift_source_dir%/llvm/tools/clang" "%swift_source_dir%my-swift/clang" mklink /J "%swift_source_dir%/llvm/tools/compiler-rt" "%swift_source_dir%/compiler-rt" mkdir "%swift_source_dir%/build/Ninja-DebugAssert/llvm-windows-amd64" pushd "%swift_source_dir%/build/Ninja-DebugAssert/llvm-windows-amd64" cmake -G "Ninja"^ -DLLVM_ENABLE_ASSERTIONS=TRUE^ -DCMAKE_BUILD_TYPE=Debug^ -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^ "%swift_source_dir%/llvm" popd cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/llvm-windows-amd64"
mkdir "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64/ninja" pushd "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64/ninja" 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-DebugAssert/cmark-windows-amd64"^ -DSWIFT_CMARK_LIBRARY_DIR="%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64/src"^ -DSWIFT_PATH_TO_LLVM_SOURCE="%swift_source_dir%/llvm"^ -DSWIFT_PATH_TO_LLVM_BUILD="%swift_source_dir%/build/Ninja-DebugAssert/llvm-windows-amd64"^ -DSWIFT_PATH_TO_CLANG_SOURCE="%swift_source_dir%/llvm/tools/clang"^ -DSWIFT_PATH_TO_CLANG_BUILD="%swift_source_dir%/build/Ninja-DebugAssert/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/ninja"
swift
project due to an MSBuild limitation that file paths cannot exceed 260 characters. These can be ignored, as they occur after the build, writing the last build status to a file.cmake -G "Visual Studio 15" "%swift_source_dir%/swift"^ -DCMAKE_GENERATOR_PLATFORM="x64"^ ...
Follow the instructions for MSVC, but add the following lines to each CMake configuration command. We need to use LLVM‘s lld-link.exe
linker, as MSVC’s link.exe
crashes due to corrupt PDB files using clang-cl
. Clang-cl
3.9.0 has been tested. You can remove the SWIFT_BUILD_DYNAMIC_SDK_OVERLAY=FALSE
definition, as overlays are supported with clang-cl
, as it supports modules.
-DCMAKE_C_COMPILER="<path-to-llvm-bin>/clang-cl.exe"^ -DCMAKE_CXX_COMPILER="<path-to-llvm-bin>/bin/clang-cl.exe"^ -DCMAKE_LINKER="<path-to-llvm-bin>/lld-link.exe"^
Install and run the latest version of Bash on Ubuntu on Windows installed on your PC.
bash
Install the developer dependencies needed to compile the Swift project. These are identical to the Ubuntu dependencies, with the addition of make
.
sudo apt-get install git make cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev
Install a version of clang with C++ 14 support - the default version of clang on WSL results in linker errors during compilation.
sudo apt-get install clang-3.6 sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100 sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
Install the latest version of CMake - Swift uses new CMake features such as IN_LIST
and won't build without these features.
wget http://www.cmake.org/files/v3.6/cmake-3.6.2.tar.gz tar xf cmake-3.6.2.tar.gz cd cmake-3.6.2 ./configure make sudo make install sudo update-alternatives --install /usr/bin/cmake cmake /usr/local/bin/cmake 1 --force cmake --version # This should print 3.6.2
mkdir swift-source cd swift-source git clone https://github.com/apple/swift.git ./swift/utils/update-checkout --clone ./swift/utils/build-script -r
cd ./build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin # This path may depend on your build configuration echo 'print("Hello, Windows")' >> test.swift swiftc test.swift ./test # Hello, Windows