1# Cross toolchain configuration for using clang-cl on non-Windows hosts to
2# target MSVC.
3#
4# Usage:
5# cmake -G Ninja
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
12#
13# HOST_ARCH:
14#    The architecture to build for.
15#
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.
21#
22# MSVC_BASE:
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:
26#
27# ${MSVC_BASE}
28#   include
29#     vector
30#     stdint.h
31#     etc...
32#   lib
33#     x64
34#       libcmt.lib
35#       msvcrt.lib
36#       etc...
37#     x86
38#       libcmt.lib
39#       msvcrt.lib
40#       etc...
41#
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.
44#
45# WINSDK_BASE:
46#   Together with WINSDK_VER, determines the location of Windows SDK headers
47#   and libraries.
48#
49# WINSDK_VER:
50#   Together with WINSDK_BASE, determines the locations of Windows SDK headers
51#   and libraries.
52#
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.
56#
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.
59#
60# ${WINSDK_BASE}
61#   Include
62#     ${WINSDK_VER}
63#       shared
64#       ucrt
65#       um
66#         windows.h
67#         etc...
68#   Lib
69#     ${WINSDK_VER}
70#       ucrt
71#         x64
72#         x86
73#           ucrt.lib
74#           etc...
75#       um
76#         x64
77#         x86
78#           kernel32.lib
79#           etc
80#
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.
86
87
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
97# is 3.6 or greater.
98function(init_user_prop prop)
99  if(${prop})
100    set(ENV{_${prop}} "${${prop}}")
101  else()
102    set(${prop} "$ENV{_${prop}}" PARENT_SCOPE)
103  endif()
104endfunction()
105
106function(generate_winsdk_vfs_overlay winsdk_include_dir output_path)
107  set(include_dirs)
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}")
112    endif()
113  endforeach()
114
115  file(WRITE "${output_path}"  "version: 0\n")
116  file(APPEND "${output_path}" "case-sensitive: false\n")
117  file(APPEND "${output_path}" "roots:\n")
118
119  foreach(dir ${include_dirs})
120    file(GLOB headers RELATIVE "${dir}" "${dir}/*.h")
121    if(NOT headers)
122      continue()
123    endif()
124
125    file(APPEND "${output_path}" "  - name: \"${dir}\"\n")
126    file(APPEND "${output_path}" "    type: directory\n")
127    file(APPEND "${output_path}" "    contents:\n")
128
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")
133    endforeach()
134  endforeach()
135endfunction()
136
137function(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}"
144                              -E create_symlink
145                              "${winsdk_um_lib_dir}/${library}"
146                              "${output_dir}/${all_lowercase_symlink_name}")
147    endif()
148
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}"
156                              -E create_symlink
157                              "${winsdk_um_lib_dir}/${library}"
158                              "${output_dir}/${lowercase_ext_symlink_name}")
159    endif()
160  endforeach()
161endfunction()
162
163set(CMAKE_SYSTEM_NAME Windows)
164set(CMAKE_SYSTEM_VERSION 10.0)
165set(CMAKE_SYSTEM_PROCESSOR AMD64)
166
167init_user_prop(HOST_ARCH)
168init_user_prop(LLVM_NATIVE_TOOLCHAIN)
169init_user_prop(MSVC_BASE)
170init_user_prop(WINSDK_BASE)
171init_user_prop(WINSDK_VER)
172
173if(NOT HOST_ARCH)
174  set(HOST_ARCH x86_64)
175endif()
176if(HOST_ARCH STREQUAL "aarch64" OR HOST_ARCH STREQUAL "arm64")
177  set(TRIPLE_ARCH "aarch64")
178  set(WINSDK_ARCH "arm64")
179elseif(HOST_ARCH STREQUAL "armv7" OR HOST_ARCH STREQUAL "arm")
180  set(TRIPLE_ARCH "armv7")
181  set(WINSDK_ARCH "arm")
182elseif(HOST_ARCH STREQUAL "i686" OR HOST_ARCH STREQUAL "x86")
183  set(TRIPLE_ARCH "i686")
184  set(WINSDK_ARCH "x86")
185elseif(HOST_ARCH STREQUAL "x86_64" OR HOST_ARCH STREQUAL "x64")
186  set(TRIPLE_ARCH "x86_64")
187  set(WINSDK_ARCH "x64")
188else()
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).")
190endif()
191
192set(MSVC_INCLUDE "${MSVC_BASE}/include")
193set(ATLMFC_INCLUDE "${MSVC_BASE}/atlmfc/include")
194set(MSVC_LIB "${MSVC_BASE}/lib")
195set(ATLMFC_LIB "${MSVC_BASE}/atlmfc/lib")
196set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
197set(WINSDK_LIB "${WINSDK_BASE}/Lib/${WINSDK_VER}")
198
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.
201if(NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" OR
202   NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link")
203  message(SEND_ERROR
204          "LLVM_NATIVE_TOOLCHAIN folder '${LLVM_NATIVE_TOOLCHAIN}' does not "
205          "point to a valid directory containing bin/clang-cl and bin/lld-link "
206          "binaries")
207endif()
208
209if(NOT EXISTS "${MSVC_BASE}" OR
210   NOT EXISTS "${MSVC_INCLUDE}" OR
211   NOT EXISTS "${MSVC_LIB}")
212  message(SEND_ERROR
213          "CMake variable MSVC_BASE must point to a folder containing MSVC "
214          "system headers and libraries")
215endif()
216
217if(NOT EXISTS "${WINSDK_BASE}" OR
218   NOT EXISTS "${WINSDK_INCLUDE}" OR
219   NOT EXISTS "${WINSDK_LIB}")
220  message(SEND_ERROR
221          "CMake variable WINSDK_BASE and WINSDK_VER must resolve to a valid "
222          "Windows SDK installation")
223endif()
224
225if(NOT EXISTS "${WINSDK_INCLUDE}/um/Windows.h")
226  message(SEND_ERROR "Cannot find Windows.h")
227endif()
228if(NOT EXISTS "${WINSDK_INCLUDE}/um/WINDOWS.H")
229  set(case_sensitive_filesystem TRUE)
230endif()
231
232set(CMAKE_C_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
233set(CMAKE_CXX_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
234set(CMAKE_LINKER "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link" CACHE FILEPATH "")
235set(CMAKE_AR "${LLVM_NATIVE_TOOLCHAIN}/bin/llvm-lib" CACHE FILEPATH "")
236
237# Even though we're cross-compiling, we need some native tools (e.g. llvm-tblgen), and those
238# native tools have to be built before we can start doing the cross-build.  LLVM supports
239# a CROSS_TOOLCHAIN_FLAGS_NATIVE argument which consists of a list of flags to pass to CMake
240# when configuring the NATIVE portion of the cross-build.  By default we construct this so
241# that it points to the tools in the same location as the native clang-cl that we're using.
242list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
243list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
244list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
245
246set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
247
248set(COMPILE_FLAGS
249    -D_CRT_SECURE_NO_WARNINGS
250    --target=${TRIPLE_ARCH}-windows-msvc
251    -fms-compatibility-version=19.14
252    -imsvc "${ATLMFC_INCLUDE}"
253    -imsvc "${MSVC_INCLUDE}"
254    -imsvc "${WINSDK_INCLUDE}/ucrt"
255    -imsvc "${WINSDK_INCLUDE}/shared"
256    -imsvc "${WINSDK_INCLUDE}/um"
257    -imsvc "${WINSDK_INCLUDE}/winrt")
258
259if(case_sensitive_filesystem)
260  # Ensure all sub-configures use the top-level VFS overlay instead of generating their own.
261  init_user_prop(winsdk_vfs_overlay_path)
262  if(NOT winsdk_vfs_overlay_path)
263    set(winsdk_vfs_overlay_path "${CMAKE_BINARY_DIR}/winsdk_vfs_overlay.yaml")
264    generate_winsdk_vfs_overlay("${WINSDK_BASE}/Include/${WINSDK_VER}" "${winsdk_vfs_overlay_path}")
265    init_user_prop(winsdk_vfs_overlay_path)
266  endif()
267  list(APPEND COMPILE_FLAGS
268       -Xclang -ivfsoverlay -Xclang "${winsdk_vfs_overlay_path}")
269endif()
270
271string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
272
273# We need to preserve any flags that were passed in by the user. However, we
274# can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
275# will be re-invoked on each reconfigure and therefore need to be idempotent.
276# The assignments to the _INITIAL cache variables don't use FORCE, so they'll
277# only be populated on the initial configure, and their values won't change
278# afterward.
279set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
280set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
281
282set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
283set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
284
285set(LINK_FLAGS
286    # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
287    /manifest:no
288
289    -libpath:"${ATLMFC_LIB}/${WINSDK_ARCH}"
290    -libpath:"${MSVC_LIB}/${WINSDK_ARCH}"
291    -libpath:"${WINSDK_LIB}/ucrt/${WINSDK_ARCH}"
292    -libpath:"${WINSDK_LIB}/um/${WINSDK_ARCH}")
293
294if(case_sensitive_filesystem)
295  # Ensure all sub-configures use the top-level symlinks dir instead of generating their own.
296  init_user_prop(winsdk_lib_symlinks_dir)
297  if(NOT winsdk_lib_symlinks_dir)
298    set(winsdk_lib_symlinks_dir "${CMAKE_BINARY_DIR}/winsdk_lib_symlinks")
299    generate_winsdk_lib_symlinks("${WINSDK_BASE}/Lib/${WINSDK_VER}/um/${WINSDK_ARCH}" "${winsdk_lib_symlinks_dir}")
300    init_user_prop(winsdk_lib_symlinks_dir)
301  endif()
302  list(APPEND LINK_FLAGS
303       -libpath:"${winsdk_lib_symlinks_dir}")
304endif()
305
306string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
307
308# See explanation for compiler flags above for the _INITIAL variables.
309set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
310set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
311
312set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
313set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
314
315set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
316set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
317
318# CMake populates these with a bunch of unnecessary libraries, which requires
319# extra case-correcting symlinks and what not. Instead, let projects explicitly
320# control which libraries they require.
321set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
322set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
323
324# Allow clang-cl to work with macOS paths.
325set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/ClangClCMakeCompileRules.cmake")
326