[ARM] Masked loads and stores
[llvm-core.git] / cmake / platforms / WinMsvc.cmake
blobd038df9795131fb248e63859e48735d6159524bc
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. We can switch to
96 # CMAKE_TRY_COMPILE_PLATFORM_VARIABLES once our minimum supported CMake version
97 # is 3.6 or greater.
98 function(init_user_prop prop)
99   if(${prop})
100     set(ENV{_${prop}} "${${prop}}")
101   else()
102     set(${prop} "$ENV{_${prop}}" PARENT_SCOPE)
103   endif()
104 endfunction()
106 function(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()
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")
121     if(NOT headers)
122       continue()
123     endif()
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")
133     endforeach()
134   endforeach()
135 endfunction()
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}"
144                               -E create_symlink
145                               "${winsdk_um_lib_dir}/${library}"
146                               "${output_dir}/${all_lowercase_symlink_name}")
147     endif()
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()
161 endfunction()
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)
173 if(NOT HOST_ARCH)
174   set(HOST_ARCH x86_64)
175 endif()
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")
188 else()
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).")
190 endif()
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")
201   message(SEND_ERROR
202           "LLVM_NATIVE_TOOLCHAIN folder '${LLVM_NATIVE_TOOLCHAIN}' does not "
203           "point to a valid directory containing bin/clang-cl and bin/lld-link "
204           "binaries")
205 endif()
207 if(NOT EXISTS "${MSVC_BASE}" OR
208    NOT EXISTS "${MSVC_INCLUDE}" OR
209    NOT EXISTS "${MSVC_LIB}")
210   message(SEND_ERROR
211           "CMake variable MSVC_BASE must point to a folder containing MSVC "
212           "system headers and libraries")
213 endif()
215 if(NOT EXISTS "${WINSDK_BASE}" OR
216    NOT EXISTS "${WINSDK_INCLUDE}" OR
217    NOT EXISTS "${WINSDK_LIB}")
218   message(SEND_ERROR
219           "CMake variable WINSDK_BASE and WINSDK_VER must resolve to a valid "
220           "Windows SDK installation")
221 endif()
223 if(NOT EXISTS "${WINSDK_INCLUDE}/um/Windows.h")
224   message(SEND_ERROR "Cannot find Windows.h")
225 endif()
226 if(NOT EXISTS "${WINSDK_INCLUDE}/um/WINDOWS.H")
227   set(case_sensitive_filesystem TRUE)
228 endif()
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 "")
245 set(COMPILE_FLAGS
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)
262   endif()
263   list(APPEND COMPILE_FLAGS
264        -Xclang -ivfsoverlay -Xclang "${winsdk_vfs_overlay_path}")
265 endif()
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
274 # afterward.
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)
281 set(LINK_FLAGS
282     # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
283     /manifest:no
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)
296   endif()
297   list(APPEND LINK_FLAGS
298        -libpath:"${winsdk_lib_symlinks_dir}")
299 endif()
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")