[CodeGenPrepare] Handle all debug calls in dupRetToEnableTailCallOpts()
[llvm-complete.git] / cmake / platforms / WinMsvc.cmake
blobf625d0e3c052412734b4f56af6722bada1ef4df3
1 # Cross toolchain configuration for using clang-cl on non-Windows hosts to
2 # target MSVC.
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
13 # HOST_ARCH:
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.
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:
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...
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.
45 # WINSDK_BASE:
46 #   Together with WINSDK_VER, determines the location of Windows SDK headers
47 #   and libraries.
49 # WINSDK_VER:
50 #   Together with WINSDK_BASE, determines the locations of Windows SDK headers
51 #   and libraries.
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.
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
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.
96 function(init_user_prop prop)
97   if(${prop})
98     set(ENV{_${prop}} "${${prop}}")
99   else()
100     set(${prop} "$ENV{_${prop}}" PARENT_SCOPE)
101   endif()
102 endfunction()
104 function(generate_winsdk_vfs_overlay winsdk_include_dir output_path)
105   set(include_dirs)
106   file(GLOB_RECURSE entries LIST_DIRECTORIES true "${winsdk_include_dir}/*")
107   foreach(entry ${entries})
108     if(IS_DIRECTORY "${entry}")
109       list(APPEND include_dirs "${entry}")
110     endif()
111   endforeach()
113   file(WRITE "${output_path}"  "version: 0\n")
114   file(APPEND "${output_path}" "case-sensitive: false\n")
115   file(APPEND "${output_path}" "roots:\n")
117   foreach(dir ${include_dirs})
118     file(GLOB headers RELATIVE "${dir}" "${dir}/*.h")
119     if(NOT headers)
120       continue()
121     endif()
123     file(APPEND "${output_path}" "  - name: \"${dir}\"\n")
124     file(APPEND "${output_path}" "    type: directory\n")
125     file(APPEND "${output_path}" "    contents:\n")
127     foreach(header ${headers})
128       file(APPEND "${output_path}" "      - name: \"${header}\"\n")
129       file(APPEND "${output_path}" "        type: file\n")
130       file(APPEND "${output_path}" "        external-contents: \"${dir}/${header}\"\n")
131     endforeach()
132   endforeach()
133 endfunction()
135 function(generate_winsdk_lib_symlinks winsdk_um_lib_dir output_dir)
136   execute_process(COMMAND "${CMAKE_COMMAND}" -E make_directory "${output_dir}")
137   file(GLOB libraries RELATIVE "${winsdk_um_lib_dir}" "${winsdk_um_lib_dir}/*")
138   foreach(library ${libraries})
139     string(TOLOWER "${library}" all_lowercase_symlink_name)
140     if(NOT library STREQUAL all_lowercase_symlink_name)
141       execute_process(COMMAND "${CMAKE_COMMAND}"
142                               -E create_symlink
143                               "${winsdk_um_lib_dir}/${library}"
144                               "${output_dir}/${all_lowercase_symlink_name}")
145     endif()
147     get_filename_component(name_we "${library}" NAME_WE)
148     get_filename_component(ext "${library}" EXT)
149     string(TOLOWER "${ext}" lowercase_ext)
150     set(lowercase_ext_symlink_name "${name_we}${lowercase_ext}")
151     if(NOT library STREQUAL lowercase_ext_symlink_name AND
152        NOT all_lowercase_symlink_name STREQUAL lowercase_ext_symlink_name)
153       execute_process(COMMAND "${CMAKE_COMMAND}"
154                               -E create_symlink
155                               "${winsdk_um_lib_dir}/${library}"
156                               "${output_dir}/${lowercase_ext_symlink_name}")
157     endif()
158   endforeach()
159 endfunction()
161 set(CMAKE_SYSTEM_NAME Windows)
162 set(CMAKE_SYSTEM_VERSION 10.0)
163 set(CMAKE_SYSTEM_PROCESSOR AMD64)
165 init_user_prop(HOST_ARCH)
166 init_user_prop(LLVM_NATIVE_TOOLCHAIN)
167 init_user_prop(MSVC_BASE)
168 init_user_prop(WINSDK_BASE)
169 init_user_prop(WINSDK_VER)
171 if(NOT HOST_ARCH)
172   set(HOST_ARCH x86_64)
173 endif()
174 if(HOST_ARCH STREQUAL "aarch64" OR HOST_ARCH STREQUAL "arm64")
175   set(TRIPLE_ARCH "aarch64")
176   set(WINSDK_ARCH "arm64")
177 elseif(HOST_ARCH STREQUAL "armv7" OR HOST_ARCH STREQUAL "arm")
178   set(TRIPLE_ARCH "armv7")
179   set(WINSDK_ARCH "arm")
180 elseif(HOST_ARCH STREQUAL "i686" OR HOST_ARCH STREQUAL "x86")
181   set(TRIPLE_ARCH "i686")
182   set(WINSDK_ARCH "x86")
183 elseif(HOST_ARCH STREQUAL "x86_64" OR HOST_ARCH STREQUAL "x64")
184   set(TRIPLE_ARCH "x86_64")
185   set(WINSDK_ARCH "x64")
186 else()
187   message(SEND_ERROR "Unknown host architecture ${HOST_ARCH}. Must be aarch64 (or arm64), armv7 (or arm), i686 (or x86), or x86_64 (or x64).")
188 endif()
190 set(MSVC_INCLUDE "${MSVC_BASE}/include")
191 set(MSVC_LIB "${MSVC_BASE}/lib")
192 set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
193 set(WINSDK_LIB "${WINSDK_BASE}/Lib/${WINSDK_VER}")
195 # Do some sanity checking to make sure we can find a native toolchain and
196 # that the Windows SDK / MSVC STL directories look kosher.
197 if(NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" OR
198    NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link")
199   message(SEND_ERROR
200           "LLVM_NATIVE_TOOLCHAIN folder '${LLVM_NATIVE_TOOLCHAIN}' does not "
201           "point to a valid directory containing bin/clang-cl and bin/lld-link "
202           "binaries")
203 endif()
205 if(NOT EXISTS "${MSVC_BASE}" OR
206    NOT EXISTS "${MSVC_INCLUDE}" OR
207    NOT EXISTS "${MSVC_LIB}")
208   message(SEND_ERROR
209           "CMake variable MSVC_BASE must point to a folder containing MSVC "
210           "system headers and libraries")
211 endif()
213 if(NOT EXISTS "${WINSDK_BASE}" OR
214    NOT EXISTS "${WINSDK_INCLUDE}" OR
215    NOT EXISTS "${WINSDK_LIB}")
216   message(SEND_ERROR
217           "CMake variable WINSDK_BASE and WINSDK_VER must resolve to a valid "
218           "Windows SDK installation")
219 endif()
221 if(NOT EXISTS "${WINSDK_INCLUDE}/um/Windows.h")
222   message(SEND_ERROR "Cannot find Windows.h")
223 endif()
224 if(NOT EXISTS "${WINSDK_INCLUDE}/um/WINDOWS.H")
225   set(case_sensitive_filesystem TRUE)
226 endif()
228 set(CMAKE_C_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
229 set(CMAKE_CXX_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
230 set(CMAKE_LINKER "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link" CACHE FILEPATH "")
232 # Even though we're cross-compiling, we need some native tools (e.g. llvm-tblgen), and those
233 # native tools have to be built before we can start doing the cross-build.  LLVM supports
234 # a CROSS_TOOLCHAIN_FLAGS_NATIVE argument which consists of a list of flags to pass to CMake
235 # when configuring the NATIVE portion of the cross-build.  By default we construct this so
236 # that it points to the tools in the same location as the native clang-cl that we're using.
237 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
238 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
239 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
241 set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
243 set(COMPILE_FLAGS
244     -D_CRT_SECURE_NO_WARNINGS
245     --target=${TRIPLE_ARCH}-windows-msvc
246     -fms-compatibility-version=19.11
247     -imsvc "${MSVC_INCLUDE}"
248     -imsvc "${WINSDK_INCLUDE}/ucrt"
249     -imsvc "${WINSDK_INCLUDE}/shared"
250     -imsvc "${WINSDK_INCLUDE}/um"
251     -imsvc "${WINSDK_INCLUDE}/winrt")
253 if(case_sensitive_filesystem)
254   # Ensure all sub-configures use the top-level VFS overlay instead of generating their own.
255   init_user_prop(winsdk_vfs_overlay_path)
256   if(NOT winsdk_vfs_overlay_path)
257     set(winsdk_vfs_overlay_path "${CMAKE_BINARY_DIR}/winsdk_vfs_overlay.yaml")
258     generate_winsdk_vfs_overlay("${WINSDK_BASE}/Include/${WINSDK_VER}" "${winsdk_vfs_overlay_path}")
259     init_user_prop(winsdk_vfs_overlay_path)
260   endif()
261   list(APPEND COMPILE_FLAGS
262        -Xclang -ivfsoverlay -Xclang "${winsdk_vfs_overlay_path}")
263 endif()
265 string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
267 # We need to preserve any flags that were passed in by the user. However, we
268 # can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
269 # will be re-invoked on each reconfigure and therefore need to be idempotent.
270 # The assignments to the _INITIAL cache variables don't use FORCE, so they'll
271 # only be populated on the initial configure, and their values won't change
272 # afterward.
273 set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
274 set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
276 set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
277 set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
279 set(LINK_FLAGS
280     # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
281     /manifest:no
283     -libpath:"${MSVC_LIB}/${WINSDK_ARCH}"
284     -libpath:"${WINSDK_LIB}/ucrt/${WINSDK_ARCH}"
285     -libpath:"${WINSDK_LIB}/um/${WINSDK_ARCH}")
287 if(case_sensitive_filesystem)
288   # Ensure all sub-configures use the top-level symlinks dir instead of generating their own.
289   init_user_prop(winsdk_lib_symlinks_dir)
290   if(NOT winsdk_lib_symlinks_dir)
291     set(winsdk_lib_symlinks_dir "${CMAKE_BINARY_DIR}/winsdk_lib_symlinks")
292     generate_winsdk_lib_symlinks("${WINSDK_BASE}/Lib/${WINSDK_VER}/um/${WINSDK_ARCH}" "${winsdk_lib_symlinks_dir}")
293     init_user_prop(winsdk_lib_symlinks_dir)
294   endif()
295   list(APPEND LINK_FLAGS
296        -libpath:"${winsdk_lib_symlinks_dir}")
297 endif()
299 string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
301 # See explanation for compiler flags above for the _INITIAL variables.
302 set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
303 set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
305 set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
306 set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
308 set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
309 set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
311 # CMake populates these with a bunch of unnecessary libraries, which requires
312 # extra case-correcting symlinks and what not. Instead, let projects explicitly
313 # control which libraries they require.
314 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
315 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
317 # Allow clang-cl to work with macOS paths.
318 set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/ClangClCMakeCompileRules.cmake")