1 # Cross toolchain configuration for using clang-cl on non-Windows hosts to
6 # -DCMAKE_TOOLCHAIN_FILE=/path/to/this/file
7 # -DHOST_ARCH=[aarch64|arm64|armv7|arm|i686|x86|x86_64|x64]
8 # -DLLVM_NATIVE_TOOLCHAIN=/path/to/llvm/installation
9 # -DMSVC_BASE=/path/to/MSVC/system/libraries/and/includes
10 # -DWINSDK_BASE=/path/to/windows-sdk
11 # -DWINSDK_VER=windows sdk version folder name
14 # The architecture to build for.
16 # LLVM_NATIVE_TOOLCHAIN:
17 # *Absolute path* to a folder containing the toolchain which will be used to
18 # build. At a minimum, this folder should have a bin directory with a
19 # copy of clang-cl, clang, clang++, and lld-link, as well as a lib directory
20 # containing clang's system resource directory.
23 # *Absolute path* to the folder containing MSVC headers and system libraries.
24 # The layout of the folder matches that which is intalled by MSVC 2017 on
25 # Windows, and should look like this:
42 # For versions of MSVC < 2017, or where you have a hermetic toolchain in a
43 # custom format, you must use symlinks or restructure it to look like the above.
46 # Together with WINSDK_VER, determines the location of Windows SDK headers
50 # Together with WINSDK_BASE, determines the locations of Windows SDK headers
53 # WINSDK_BASE and WINSDK_VER work together to define a folder layout that matches
54 # that of the Windows SDK installation on a standard Windows machine. It should
55 # match the layout described below.
57 # Note that if you install Windows SDK to a windows machine and simply copy the
58 # files, it will already be in the correct layout.
81 # IMPORTANT: In order for this to work, you will need a valid copy of the Windows
82 # SDK and C++ STL headers and libraries on your host. Additionally, since the
83 # Windows libraries and headers are not case-correct, this toolchain file sets
84 # up a VFS overlay for the SDK headers and case-correcting symlinks for the
85 # libraries when running on a case-sensitive filesystem.
88 # When configuring CMake with a toolchain file against a top-level CMakeLists.txt,
89 # it will actually run CMake many times, once for each small test program used to
90 # determine what features a compiler supports. Unfortunately, none of these
91 # invocations share a CMakeCache.txt with the top-level invocation, meaning they
92 # won't see the value of any arguments the user passed via -D. Since these are
93 # necessary to properly configure MSVC in both the top-level configuration as well as
94 # all feature-test invocations, we set environment variables with the values so that
95 # these environments get inherited by child invocations. We can switch to
96 # CMAKE_TRY_COMPILE_PLATFORM_VARIABLES once our minimum supported CMake version
98 function(init_user_prop prop)
100 set(ENV{_${prop}} "${${prop}}")
102 set(${prop} "$ENV{_${prop}}" PARENT_SCOPE)
106 function(generate_winsdk_vfs_overlay winsdk_include_dir output_path)
108 file(GLOB_RECURSE entries LIST_DIRECTORIES true "${winsdk_include_dir}/*")
109 foreach(entry ${entries})
110 if(IS_DIRECTORY "${entry}")
111 list(APPEND include_dirs "${entry}")
115 file(WRITE "${output_path}" "version: 0\n")
116 file(APPEND "${output_path}" "case-sensitive: false\n")
117 file(APPEND "${output_path}" "roots:\n")
119 foreach(dir ${include_dirs})
120 file(GLOB headers RELATIVE "${dir}" "${dir}/*.h")
125 file(APPEND "${output_path}" " - name: \"${dir}\"\n")
126 file(APPEND "${output_path}" " type: directory\n")
127 file(APPEND "${output_path}" " contents:\n")
129 foreach(header ${headers})
130 file(APPEND "${output_path}" " - name: \"${header}\"\n")
131 file(APPEND "${output_path}" " type: file\n")
132 file(APPEND "${output_path}" " external-contents: \"${dir}/${header}\"\n")
137 function(generate_winsdk_lib_symlinks winsdk_um_lib_dir output_dir)
138 execute_process(COMMAND "${CMAKE_COMMAND}" -E make_directory "${output_dir}")
139 file(GLOB libraries RELATIVE "${winsdk_um_lib_dir}" "${winsdk_um_lib_dir}/*")
140 foreach(library ${libraries})
141 string(TOLOWER "${library}" all_lowercase_symlink_name)
142 if(NOT library STREQUAL all_lowercase_symlink_name)
143 execute_process(COMMAND "${CMAKE_COMMAND}"
145 "${winsdk_um_lib_dir}/${library}"
146 "${output_dir}/${all_lowercase_symlink_name}")
149 get_filename_component(name_we "${library}" NAME_WE)
150 get_filename_component(ext "${library}" EXT)
151 string(TOLOWER "${ext}" lowercase_ext)
152 set(lowercase_ext_symlink_name "${name_we}${lowercase_ext}")
153 if(NOT library STREQUAL lowercase_ext_symlink_name AND
154 NOT all_lowercase_symlink_name STREQUAL lowercase_ext_symlink_name)
155 execute_process(COMMAND "${CMAKE_COMMAND}"
157 "${winsdk_um_lib_dir}/${library}"
158 "${output_dir}/${lowercase_ext_symlink_name}")
163 set(CMAKE_SYSTEM_NAME Windows)
164 set(CMAKE_SYSTEM_VERSION 10.0)
165 set(CMAKE_SYSTEM_PROCESSOR AMD64)
167 init_user_prop(HOST_ARCH)
168 init_user_prop(LLVM_NATIVE_TOOLCHAIN)
169 init_user_prop(MSVC_BASE)
170 init_user_prop(WINSDK_BASE)
171 init_user_prop(WINSDK_VER)
174 set(HOST_ARCH x86_64)
176 if(HOST_ARCH STREQUAL "aarch64" OR HOST_ARCH STREQUAL "arm64")
177 set(TRIPLE_ARCH "aarch64")
178 set(WINSDK_ARCH "arm64")
179 elseif(HOST_ARCH STREQUAL "armv7" OR HOST_ARCH STREQUAL "arm")
180 set(TRIPLE_ARCH "armv7")
181 set(WINSDK_ARCH "arm")
182 elseif(HOST_ARCH STREQUAL "i686" OR HOST_ARCH STREQUAL "x86")
183 set(TRIPLE_ARCH "i686")
184 set(WINSDK_ARCH "x86")
185 elseif(HOST_ARCH STREQUAL "x86_64" OR HOST_ARCH STREQUAL "x64")
186 set(TRIPLE_ARCH "x86_64")
187 set(WINSDK_ARCH "x64")
189 message(SEND_ERROR "Unknown host architecture ${HOST_ARCH}. Must be aarch64 (or arm64), armv7 (or arm), i686 (or x86), or x86_64 (or x64).")
192 set(MSVC_INCLUDE "${MSVC_BASE}/include")
193 set(MSVC_LIB "${MSVC_BASE}/lib")
194 set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
195 set(WINSDK_LIB "${WINSDK_BASE}/Lib/${WINSDK_VER}")
197 # Do some sanity checking to make sure we can find a native toolchain and
198 # that the Windows SDK / MSVC STL directories look kosher.
199 if(NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" OR
200 NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link")
202 "LLVM_NATIVE_TOOLCHAIN folder '${LLVM_NATIVE_TOOLCHAIN}' does not "
203 "point to a valid directory containing bin/clang-cl and bin/lld-link "
207 if(NOT EXISTS "${MSVC_BASE}" OR
208 NOT EXISTS "${MSVC_INCLUDE}" OR
209 NOT EXISTS "${MSVC_LIB}")
211 "CMake variable MSVC_BASE must point to a folder containing MSVC "
212 "system headers and libraries")
215 if(NOT EXISTS "${WINSDK_BASE}" OR
216 NOT EXISTS "${WINSDK_INCLUDE}" OR
217 NOT EXISTS "${WINSDK_LIB}")
219 "CMake variable WINSDK_BASE and WINSDK_VER must resolve to a valid "
220 "Windows SDK installation")
223 if(NOT EXISTS "${WINSDK_INCLUDE}/um/Windows.h")
224 message(SEND_ERROR "Cannot find Windows.h")
226 if(NOT EXISTS "${WINSDK_INCLUDE}/um/WINDOWS.H")
227 set(case_sensitive_filesystem TRUE)
230 set(CMAKE_C_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
231 set(CMAKE_CXX_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
232 set(CMAKE_LINKER "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link" CACHE FILEPATH "")
234 # Even though we're cross-compiling, we need some native tools (e.g. llvm-tblgen), and those
235 # native tools have to be built before we can start doing the cross-build. LLVM supports
236 # a CROSS_TOOLCHAIN_FLAGS_NATIVE argument which consists of a list of flags to pass to CMake
237 # when configuring the NATIVE portion of the cross-build. By default we construct this so
238 # that it points to the tools in the same location as the native clang-cl that we're using.
239 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
240 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
241 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
243 set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
246 -D_CRT_SECURE_NO_WARNINGS
247 --target=${TRIPLE_ARCH}-windows-msvc
248 -fms-compatibility-version=19.11
249 -imsvc "${MSVC_INCLUDE}"
250 -imsvc "${WINSDK_INCLUDE}/ucrt"
251 -imsvc "${WINSDK_INCLUDE}/shared"
252 -imsvc "${WINSDK_INCLUDE}/um"
253 -imsvc "${WINSDK_INCLUDE}/winrt")
255 if(case_sensitive_filesystem)
256 # Ensure all sub-configures use the top-level VFS overlay instead of generating their own.
257 init_user_prop(winsdk_vfs_overlay_path)
258 if(NOT winsdk_vfs_overlay_path)
259 set(winsdk_vfs_overlay_path "${CMAKE_BINARY_DIR}/winsdk_vfs_overlay.yaml")
260 generate_winsdk_vfs_overlay("${WINSDK_BASE}/Include/${WINSDK_VER}" "${winsdk_vfs_overlay_path}")
261 init_user_prop(winsdk_vfs_overlay_path)
263 list(APPEND COMPILE_FLAGS
264 -Xclang -ivfsoverlay -Xclang "${winsdk_vfs_overlay_path}")
267 string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
269 # We need to preserve any flags that were passed in by the user. However, we
270 # can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
271 # will be re-invoked on each reconfigure and therefore need to be idempotent.
272 # The assignments to the _INITIAL cache variables don't use FORCE, so they'll
273 # only be populated on the initial configure, and their values won't change
275 set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
276 set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
278 set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
279 set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
282 # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
285 -libpath:"${MSVC_LIB}/${WINSDK_ARCH}"
286 -libpath:"${WINSDK_LIB}/ucrt/${WINSDK_ARCH}"
287 -libpath:"${WINSDK_LIB}/um/${WINSDK_ARCH}")
289 if(case_sensitive_filesystem)
290 # Ensure all sub-configures use the top-level symlinks dir instead of generating their own.
291 init_user_prop(winsdk_lib_symlinks_dir)
292 if(NOT winsdk_lib_symlinks_dir)
293 set(winsdk_lib_symlinks_dir "${CMAKE_BINARY_DIR}/winsdk_lib_symlinks")
294 generate_winsdk_lib_symlinks("${WINSDK_BASE}/Lib/${WINSDK_VER}/um/${WINSDK_ARCH}" "${winsdk_lib_symlinks_dir}")
295 init_user_prop(winsdk_lib_symlinks_dir)
297 list(APPEND LINK_FLAGS
298 -libpath:"${winsdk_lib_symlinks_dir}")
301 string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
303 # See explanation for compiler flags above for the _INITIAL variables.
304 set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
305 set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
307 set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
308 set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
310 set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
311 set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
313 # CMake populates these with a bunch of unnecessary libraries, which requires
314 # extra case-correcting symlinks and what not. Instead, let projects explicitly
315 # control which libraries they require.
316 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
317 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
319 # Allow clang-cl to work with macOS paths.
320 set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/ClangClCMakeCompileRules.cmake")