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