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(ATLMFC_INCLUDE "${MSVC_BASE}/atlmfc/include")
194 set(MSVC_LIB "${MSVC_BASE}/lib")
195 set(ATLMFC_LIB "${MSVC_BASE}/atlmfc/lib")
196 set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
197 set(WINSDK_LIB "${WINSDK_BASE}/Lib/${WINSDK_VER}")
199 # Do some sanity checking to make sure we can find a native toolchain and
200 # that the Windows SDK / MSVC STL directories look kosher.
201 if(NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" OR
202 NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link")
204 "LLVM_NATIVE_TOOLCHAIN folder '${LLVM_NATIVE_TOOLCHAIN}' does not "
205 "point to a valid directory containing bin/clang-cl and bin/lld-link "
209 if(NOT EXISTS "${MSVC_BASE}" OR
210 NOT EXISTS "${MSVC_INCLUDE}" OR
211 NOT EXISTS "${MSVC_LIB}")
213 "CMake variable MSVC_BASE must point to a folder containing MSVC "
214 "system headers and libraries")
217 if(NOT EXISTS "${WINSDK_BASE}" OR
218 NOT EXISTS "${WINSDK_INCLUDE}" OR
219 NOT EXISTS "${WINSDK_LIB}")
221 "CMake variable WINSDK_BASE and WINSDK_VER must resolve to a valid "
222 "Windows SDK installation")
225 if(NOT EXISTS "${WINSDK_INCLUDE}/um/Windows.h")
226 message(SEND_ERROR "Cannot find Windows.h")
228 if(NOT EXISTS "${WINSDK_INCLUDE}/um/WINDOWS.H")
229 set(case_sensitive_filesystem TRUE)
232 set(CMAKE_C_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
233 set(CMAKE_CXX_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
234 set(CMAKE_LINKER "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link" CACHE FILEPATH "")
236 # Even though we're cross-compiling, we need some native tools (e.g. llvm-tblgen), and those
237 # native tools have to be built before we can start doing the cross-build. LLVM supports
238 # a CROSS_TOOLCHAIN_FLAGS_NATIVE argument which consists of a list of flags to pass to CMake
239 # when configuring the NATIVE portion of the cross-build. By default we construct this so
240 # that it points to the tools in the same location as the native clang-cl that we're using.
241 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
242 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
243 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
245 set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
248 -D_CRT_SECURE_NO_WARNINGS
249 --target=${TRIPLE_ARCH}-windows-msvc
250 -fms-compatibility-version=19.11
251 -imsvc "${ATLMFC_INCLUDE}"
252 -imsvc "${MSVC_INCLUDE}"
253 -imsvc "${WINSDK_INCLUDE}/ucrt"
254 -imsvc "${WINSDK_INCLUDE}/shared"
255 -imsvc "${WINSDK_INCLUDE}/um"
256 -imsvc "${WINSDK_INCLUDE}/winrt")
258 if(case_sensitive_filesystem)
259 # Ensure all sub-configures use the top-level VFS overlay instead of generating their own.
260 init_user_prop(winsdk_vfs_overlay_path)
261 if(NOT winsdk_vfs_overlay_path)
262 set(winsdk_vfs_overlay_path "${CMAKE_BINARY_DIR}/winsdk_vfs_overlay.yaml")
263 generate_winsdk_vfs_overlay("${WINSDK_BASE}/Include/${WINSDK_VER}" "${winsdk_vfs_overlay_path}")
264 init_user_prop(winsdk_vfs_overlay_path)
266 list(APPEND COMPILE_FLAGS
267 -Xclang -ivfsoverlay -Xclang "${winsdk_vfs_overlay_path}")
270 string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
272 # We need to preserve any flags that were passed in by the user. However, we
273 # can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
274 # will be re-invoked on each reconfigure and therefore need to be idempotent.
275 # The assignments to the _INITIAL cache variables don't use FORCE, so they'll
276 # only be populated on the initial configure, and their values won't change
278 set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
279 set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
281 set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
282 set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
285 # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
288 -libpath:"${ATLMFC_LIB}/${WINSDK_ARCH}"
289 -libpath:"${MSVC_LIB}/${WINSDK_ARCH}"
290 -libpath:"${WINSDK_LIB}/ucrt/${WINSDK_ARCH}"
291 -libpath:"${WINSDK_LIB}/um/${WINSDK_ARCH}")
293 if(case_sensitive_filesystem)
294 # Ensure all sub-configures use the top-level symlinks dir instead of generating their own.
295 init_user_prop(winsdk_lib_symlinks_dir)
296 if(NOT winsdk_lib_symlinks_dir)
297 set(winsdk_lib_symlinks_dir "${CMAKE_BINARY_DIR}/winsdk_lib_symlinks")
298 generate_winsdk_lib_symlinks("${WINSDK_BASE}/Lib/${WINSDK_VER}/um/${WINSDK_ARCH}" "${winsdk_lib_symlinks_dir}")
299 init_user_prop(winsdk_lib_symlinks_dir)
301 list(APPEND LINK_FLAGS
302 -libpath:"${winsdk_lib_symlinks_dir}")
305 string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
307 # See explanation for compiler flags above for the _INITIAL variables.
308 set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
309 set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
311 set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
312 set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
314 set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
315 set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
317 # CMake populates these with a bunch of unnecessary libraries, which requires
318 # extra case-correcting symlinks and what not. Instead, let projects explicitly
319 # control which libraries they require.
320 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
321 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
323 # Allow clang-cl to work with macOS paths.
324 set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/ClangClCMakeCompileRules.cmake")