|
1 | 1 | # Getting Started with Swift on Windows
|
2 | 2 |
|
3 |
| -## clang (cross-compiling) |
| 3 | +One can build and run Swift natively, or through the Windows Subsystem for |
| 4 | +Linux. |
4 | 5 |
|
5 |
| -### 1. Setup Visual Studio Environment Variables |
6 |
| -Building for Windows requires that the Visual Studio environment variables are |
7 |
| -setup similar to the values on Windows. The following assumes that |
8 |
| -`WINKIT_ROOT` points to the path where the Windows 10 SDK is available and that |
9 |
| -`VC_ROOT` points to the path where the Visual Studio VC headers and libraries |
10 |
| -are available. Currently, the runtime has been tested to build against the |
11 |
| -Windows 10 SDK at revision 10.10.586. |
| 6 | +## Native Windows |
12 | 7 |
|
13 |
| -```bash |
14 |
| -# Visual Studio 2015 does not have VCToolsInstallDir, use VCINSTALLDIR's value |
15 |
| -export UCRTVersion=10.0.10586.0 |
16 |
| -export UniversalCRTSdkDir=".../Windows Kits/10" |
17 |
| -export VCToolsInstallDir=".../Microsoft Visual Studio/2017/Community" |
18 |
| -``` |
| 8 | +Currently there are three supported ways to build Swift for Windows. |
19 | 9 |
|
20 |
| -### 2. Setup `visualc` and `ucrt` modules |
21 |
| -The `visualc.modulemap` located at |
22 |
| -`swift/stdlib/public/Platform/visualc.modulemap` needs to be copied into |
23 |
| -`${VCToolsInstallDir}/include`. The `ucrt.modulemap` located at |
24 |
| -`swift/stdlib/public/Platform/ucrt.modulemap` needs to be copied into |
25 |
| -`${UniversalCRTSdkDir}/Include/${UCRTVersion}/ucrt`. |
| 10 | +1. To cross-compile Swift for Windows from another host operating system (using |
| 11 | + `clang`), see [Cross Compiling for Windows](./WindowsCrossCompile.md) |
26 | 12 |
|
27 |
| -### 3. Configure the runtime to be built with the just built clang |
28 |
| -Ensure that we use the tools from the just built LLVM and clang tools to build |
29 |
| -the Windows SDK. You will need to pass a few extra options to cmake via the |
30 |
| -`build-script` invocation to achieve this. You will need to expand out the |
31 |
| -path where llvm-ar and llvm-ranlib are built. These are needed to correctly |
32 |
| -build the static libraries. Note that cross-compiling will require the use of |
33 |
| -lld. Ensure that lld-link.exe (lld-link) is available to clang via your path. |
34 |
| -Additionally, the ICU headers and libraries need to be provided for the build. |
| 13 | +1. To build on Windows using `clang-cl`, see [Building on |
| 14 | + Windows](./WindowsBuild.md#clang-cl) |
35 | 15 |
|
36 |
| -```bash |
37 |
| ---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> |
38 |
| -``` |
| 16 | +1. To build on Windows using Microsoft Visual C++ (MSVC), see [Building on |
| 17 | + Windows](./WindowsBuild.md#MSVC) |
39 | 18 |
|
40 |
| -## MSVC |
41 |
| -- Windows doesn't currently have a build script. You'll need to run commands manually to build Swift on Windows. |
42 |
| -- Release/RelWithDebInfo modes have not been tested and may not be supported. |
43 |
| -- Windows support for Swift is very much a work in progress and may not work on your system. |
44 |
| -- Using the latest Visual Studio version is recommended. Swift may fail to build with older C++ compilers. |
| 19 | +`clang-cl` is recommended over MSVC for building Swift on Windows. |
| 20 | +Although it is possible to build the compiler and the standard library with |
| 21 | +MSVC to use those built products to compile a Swift program, it won't be |
| 22 | +possible to run the binary without seperately obtaining the Swift runtime. On |
| 23 | +the other hand, `clang-cl` is able to build the runtime, which makes it |
| 24 | +possible to build and run all the components required for Swift natively on |
| 25 | +Windows. |
45 | 26 |
|
46 |
| -### 1. Install dependencies |
47 |
| -- Make sure to add Python, CMake and Ninja to your `Path` environment variable |
48 |
| -1. Latest version (2.7.12 tested) of [Python 2](https://www.python.org/downloads/) |
49 |
| -2. Latest version (3.7.0-rc3 tested) of [CMake](https://cmake.org/download/) |
50 |
| -3. Latest version (1.7.1 tested) of [Ninja](https://github.com/ninja-build/ninja/releases/latest) |
51 |
| -4. Latest version (2015 Update 3 tested) of [Visual Studio](https://www.visualstudio.com/downloads/) |
52 |
| -- Make sure to include `Programming Languages|Visual C++` and `Windows and Web Development|Universal Windows App Development|Windows SDK` in your installation. |
53 |
| -- Windows SDK 10.0.10240 was tested. Some later versions (e.g. 10.0.14393) are known not to work, as they are not supported by `compiler-rt`. |
| 27 | +## Windows Subsystem for Linux |
54 | 28 |
|
55 |
| -### 2. Clone the repositories |
56 |
| -1. Create a folder to contain all the Swift repositories |
57 |
| -2. `apple/swift-cmark` into a folder named `cmark` |
58 |
| -3. `apple/swift-clang` into a folder named `clang` |
59 |
| -5. `apple/swift-llvm` into a folder named `llvm` |
60 |
| -5. `apple/swift` into a folder named `swift` |
61 |
| -- Currently, other repositories in the Swift project have not been tested and may not be supported. |
62 |
| - |
63 |
| -### 3. Build ICU |
64 |
| -1. Download and extract the [ICU source code](http://site.icu-project.org/download) to a folder named `icu` in the same directory as the other Swift project repositories. |
65 |
| -2. Open `src/win32/allinone.sln` in Visual Studio. |
66 |
| -3. Make sure to select the correct architecture from the drop-down in Visual Studio. |
67 |
| -4. Right click on the solution in the Solution Explorer window and select `Build Solution`. |
68 |
| -5. When this is done, add the `<icu-source>/bin` folder to your `Path` environment variable. |
69 |
| - |
70 |
| -### 4. Get ready |
71 |
| -- From within a **developer** command prompt (not PowerShell nor cmd, but [the Visual Studio Developer Command Prompt](https://msdn.microsoft.com/en-us/library/f35ctcxw.aspx)), execute the following command if you have an x64 PC. |
72 |
| -```cmd |
73 |
| -VsDevCmd -arch=amd64 |
74 |
| -``` |
75 |
| -If instead, you're compiling for a 32-bit Windows target, adapt the `arch` argument to `x86` and run |
76 |
| -```cmd |
77 |
| -VsDevCmd -arch=x86 |
78 |
| -``` |
79 |
| - |
80 |
| -- Then adapt the following command and run it. |
81 |
| -```cmd |
82 |
| -set swift_source_dir=path-to-directory-containing-all-cloned-repositories |
83 |
| -``` |
84 |
| - |
85 |
| -### 5. Build CMark |
86 |
| -- This must be done from within a developer command prompt. CMark is a fairly small project and should only take a few minutes to build. |
87 |
| -```cmd |
88 |
| -mkdir "%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64" |
89 |
| -pushd "%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64" |
90 |
| -cmake -G "Ninja" "%swift_source_dir%/cmark" |
91 |
| -popd |
92 |
| -cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64/" |
93 |
| -``` |
94 |
| - |
95 |
| -### 6. Build LLVM/Clang/Compiler-RT |
96 |
| -- This must be done from within a developer command prompt. LLVM and Clang are large projects so building might take a few hours. Make sure that the build type (e.g. Debug/Release) for LLVM/Clang matches the build type for Swift. |
97 |
| -- Optionally, you can omit building compiler-rt by removing all lines referring to `compiler-rt` below, which should give faster build times. |
98 |
| -```cmd |
99 |
| -mklink /J "%swift_source_dir%/llvm/tools/clang" "%swift_source_dir%/clang" |
100 |
| -mklink /J "%swift_source_dir%/llvm/tools/compiler-rt" "%swift_source_dir%/compiler-rt" |
101 |
| -mkdir "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64" |
102 |
| -pushd "%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64" |
103 |
| -cmake -G "Ninja"^ |
104 |
| - -DLLVM_ENABLE_ASSERTIONS=TRUE^ |
105 |
| - -DCMAKE_BUILD_TYPE=RelWithDebInfo^ |
106 |
| - -DLLVM_TOOL_SWIFT_BUILD=NO^ |
107 |
| - -DLLVM_INCLUDE_DOCS=TRUE^ |
108 |
| - -DLLVM_TOOL_COMPILER_RT_BUILD=TRUE^ |
109 |
| - -DLLVM_BUILD_EXTERNAL_COMPILER_RT=TRUE^ |
110 |
| - -DLLVM_LIT_ARGS=-sv^ |
111 |
| - -DLLVM_TARGETS_TO_BUILD=X86^ |
112 |
| - "%swift_source_dir%/llvm" |
113 |
| -popd |
114 |
| -cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/llvm-windows-amd64" |
115 |
| -``` |
116 |
| - |
117 |
| -### 7. Build Swift |
118 |
| -- This must be done from within a developer command prompt and could take up to two hours depending on your system. |
119 |
| -- You may need to adjust the SWIFT_WINDOWS_LIB_DIRECTORY parameter depending on your target platform or Windows SDK version. |
120 |
| -- While the commands here use MSVC to build, using clang-cl is recommended (see the **Clang-cl** section below). |
121 |
| -```cmd |
122 |
| -mkdir "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64/ninja" |
123 |
| -pushd "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64/ninja" |
124 |
| -cmake -G "Ninja" "%swift_source_dir%/swift"^ |
125 |
| - -DCMAKE_BUILD_TYPE=Debug^ |
126 |
| - -DSWIFT_PATH_TO_CMARK_SOURCE="%swift_source_dir%/cmark"^ |
127 |
| - -DSWIFT_PATH_TO_CMARK_BUILD="%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64"^ |
128 |
| - -DSWIFT_CMARK_LIBRARY_DIR="%swift_source_dir%/build/Ninja-DebugAssert/cmark-windows-amd64/src"^ |
129 |
| - -DSWIFT_PATH_TO_LLVM_SOURCE="%swift_source_dir%/llvm"^ |
130 |
| - -DSWIFT_PATH_TO_LLVM_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ |
131 |
| - -DSWIFT_PATH_TO_CLANG_SOURCE="%swift_source_dir%/llvm/tools/clang"^ |
132 |
| - -DSWIFT_PATH_TO_CLANG_BUILD="%swift_source_dir%/build/Ninja-RelWithDebInfoAssert/llvm-windows-amd64"^ |
133 |
| - -DICU_UC_INCLUDE_DIRS="%swift_source_dir%/icu/include"^ |
134 |
| - -DICU_UC_LIBRARY_DIRS="%swift_source_dir%/icu/lib64"^ |
135 |
| - -DICU_I18N_INCLUDE_DIRS="%swift_source_dir%/icu/include"^ |
136 |
| - -DICU_I18N_LIBRARY_DIRS="%swift_source_dir%/icu/lib64"^ |
137 |
| - -DICU_UC_LIB_NAME="icuuc"^ |
138 |
| - -DICU_I18N_LIB_NAME="icuin"^ |
139 |
| - -DSWIFT_INCLUDE_DOCS=FALSE^ |
140 |
| - -DSWIFT_INCLUDE_TESTS=FALSE^ |
141 |
| - -DSWIFT_BUILD_DYNAMIC_SDK_OVERLAY=FALSE^ |
142 |
| - -DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER=FALSE |
143 |
| -popd |
144 |
| -cmake --build "%swift_source_dir%/build/Ninja-DebugAssert/swift-windows-amd64/ninja" |
145 |
| -``` |
146 |
| - |
147 |
| -- To create a VisualStudio project, you'll need to change the generator and, if you have a 64 bit processor, specify the generator platform. Note that you may get multiple build errors compiling the `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. |
148 |
| -```cmd |
149 |
| -cmake -G "Visual Studio 15" "%swift_source_dir%/swift"^ |
150 |
| - -DCMAKE_GENERATOR_PLATFORM="x64"^ |
151 |
| - ... |
152 |
| -``` |
153 |
| - |
154 |
| -## Clang-cl |
155 |
| - |
156 |
| -Follow the instructions for MSVC, but add the following lines to each CMake configuration command. `Clang-cl` 4.0.1 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. The `Z7` flag is required to produce PDB files that MSVC's `link.exe` can read, and also enables proper stack traces. |
157 |
| - |
158 |
| -```cmd |
159 |
| - -DCMAKE_C_COMPILER="<path-to-llvm-bin>/clang-cl.exe"^ |
160 |
| - -DCMAKE_CXX_COMPILER="<path-to-llvm-bin>/bin/clang-cl.exe"^ |
161 |
| - -DCMAKE_C_FLAGS="-fms-compatibility-version=19.00 /Z7"^ |
162 |
| - -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.00 -Z7" ^ |
163 |
| -``` |
164 |
| - |
165 |
| -## Windows Subsystem for Linux (WSL) |
166 |
| -- Note that all compiled Swift binaries are only executable within Bash on Windows and are Ubuntu, not Windows, executables. |
167 |
| -- Make sure to run all commands from Bash, or the project won't compile. |
168 |
| - |
169 |
| -### 1. Install WSL |
170 |
| -Install and run the latest version of [Bash on Ubuntu on Windows](https://msdn.microsoft.com/en-gb/commandline/wsl/about) installed on your PC. |
171 |
| -``` |
172 |
| -bash |
173 |
| -``` |
174 |
| - |
175 |
| -### 2. Install dependencies |
176 |
| -Install the developer dependencies needed to compile the Swift project. These are identical to the Ubuntu dependencies, with the addition of `make`. |
177 |
| -```bash |
178 |
| -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 |
179 |
| -``` |
180 |
| - |
181 |
| -### 3. Upgrade clang |
182 |
| -Install a version of clang with C++ 14 support - the default version of clang on WSL results in linker errors during compilation. |
183 |
| -```bash |
184 |
| -sudo apt-get install clang-3.6 |
185 |
| -sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100 |
186 |
| -sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100 |
187 |
| -``` |
188 |
| - |
189 |
| -### 4. Upgrade CMake |
190 |
| -Install the latest version of CMake - Swift uses new CMake features such as `IN_LIST` and won't build without these features. |
191 |
| -```bash |
192 |
| -wget http://www.cmake.org/files/v3.6/cmake-3.6.2.tar.gz |
193 |
| -tar xf cmake-3.6.2.tar.gz |
194 |
| -cd cmake-3.6.2 |
195 |
| -./configure |
196 |
| -make |
197 |
| -sudo make install |
198 |
| -sudo update-alternatives --install /usr/bin/cmake cmake /usr/local/bin/cmake 1 --force |
199 |
| -cmake --version # This should print 3.6.2 |
200 |
| -``` |
201 |
| - |
202 |
| -### 6. Clone and build the Swift project |
203 |
| -```bash |
204 |
| -mkdir swift-source |
205 |
| -cd swift-source |
206 |
| -git clone https://github.com/apple/swift.git |
207 |
| -./swift/utils/update-checkout --clone |
208 |
| -./swift/utils/build-script -r |
209 |
| -``` |
210 |
| -### 7. Hello, Windows (Subsystem for Linux) |
211 |
| -```bash |
212 |
| -cd ./build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin # This path may depend on your build configuration |
213 |
| -echo 'print("Hello, Windows")' >> test.swift |
214 |
| -swiftc test.swift |
215 |
| -./test # Hello, Windows |
216 |
| -``` |
| 29 | +On the [Windows Subsystem for |
| 30 | +Linux](https://docs.microsoft.com/en-us/windows/wsl/about), it's possible to |
| 31 | +build and run Swift in a Linux-like environment, on Windows. See [Getting |
| 32 | +Started with Swift on Windows](./WindowsSubsystemForLinux.md) for details. |
0 commit comments