blob: 9ea9ff78a2550c6b77fd612b8958a735e3fd3dd2 [file] [log] [blame]
Zachary Turner463c6122017-11-09 20:38:16 +00001# 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# -DLLVM_NATIVE_TOOLCHAIN=/path/to/llvm/installation
8# -DMSVC_BASE=/path/to/MSVC/system/libraries/and/includes
9# -DWINSDK_BASE=/path/to/windows-sdk
10# -DWINSDK_VER=windows sdk version folder name
11#
12# LLVM_NATIVE_TOOLCHAIN:
13# *Absolute path* to a folder containing the toolchain which will be used to
14# build. At a minimum, this folder should have a bin directory with a
15# copy of clang-cl, clang, clang++, and lld-link, as well as a lib directory
16# containing clang's system resource directory.
17#
18# MSVC_BASE:
19# *Absolute path* to the folder containing MSVC headers and system libraries.
20# The layout of the folder matches that which is intalled by MSVC 2017 on
21# Windows, and should look like this:
22#
23# ${MSVC_BASE}
24# include
25# vector
26# stdint.h
27# etc...
28# lib
29# x64
30# libcmt.lib
31# msvcrt.lib
32# etc...
33# x86
34# libcmt.lib
35# msvcrt.lib
36# etc...
37#
38# For versions of MSVC < 2017, or where you have a hermetic toolchain in a
39# custom format, you must use symlinks or restructure it to look like the above.
40#
41# WINSDK_BASE:
42# Together with WINSDK_VER, determines the location of Windows SDK headers
43# and libraries.
44#
45# WINSDK_VER:
46# Together with WINSDK_BASE, determines the locations of Windows SDK headers
47# and libraries.
48#
49# WINSDK_BASE and WINSDK_VER work together to define a folder layout that matches
50# that of the Windows SDK installation on a standard Windows machine. It should
51# match the layout described below.
52#
53# Note that if you install Windows SDK to a windows machine and simply copy the
54# files, it will already be in the correct layout.
55#
56# ${WINSDK_BASE}
57# Include
58# ${WINSDK_VER}
59# shared
60# ucrt
61# um
62# windows.h
63# etc...
64# Lib
65# ${WINSDK_VER}
66# ucrt
67# x64
68# x86
69# ucrt.lib
70# etc...
71# um
72# x64
73# x86
74# kernel32.lib
75# etc
76#
77# IMPORTANT: In order for this to work, you will need a valid copy of the Windows
78# SDK and C++ STL headers and libraries on your host. Additionally, since the
79# Windows libraries and headers are not case-correct, you will need to have these
80# mounted in a case-insensitive mount. This requires one command to set up.
81#
82# ~/src: mkdir winsdk
83# ~/src: mkdir winsdk.icase
84# ~/src: ciopfs winsdk/ winsdk.icase
85#
86# Now copy or otherwise install your headers and libraries to the winsdk.icase folder
87# and use *that* folder as the path when configuring CMake.
88#
89# TODO: We could also provide a CMake option -DUSE_ICASE_VFS_OVERLAY=ON/OFF that would
90# make this optional. For now, we require ciopfs.
91
92
93# When configuring CMake with a toolchain file against a top-level CMakeLists.txt,
94# it will actually run CMake many times, once for each small test program used to
95# determine what features a compiler supports. Unfortunately, none of these
96# invocations share a CMakeCache.txt with the top-level invocation, meaning they
97# won't see the value of any arguments the user passed via -D. Since these are
98# necessary to properly configure MSVC in both the top-level configuration as well as
99# all feature-test invocations, we set environment variables with the values so that
100# these environments get inherited by child invocations.
101function(init_user_prop prop)
102 if(${prop})
103 set(ENV{_${prop}} "${${prop}}")
104 else()
105 set(${prop} "$ENV{_${prop}}" PARENT_SCOPE)
106 endif()
107endfunction()
108
109# FIXME: We should support target architectures other than x64
110set(CMAKE_SYSTEM_NAME Windows)
111set(CMAKE_SYSTEM_VERSION 10.0)
112set(CMAKE_SYSTEM_PROCESSOR AMD64)
113
114init_user_prop(LLVM_NATIVE_TOOLCHAIN)
115init_user_prop(MSVC_BASE)
116init_user_prop(WINSDK_BASE)
117init_user_prop(WINSDK_VER)
118
119set(MSVC_INCLUDE "${MSVC_BASE}/include")
120set(MSVC_LIB "${MSVC_BASE}/lib")
121set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
122set(WINSDK_LIB "${WINSDK_BASE}/Lib/${WINSDK_VER}")
123
124# Do some sanity checking to make sure we can find a native toolchain and
125# that the Windows SDK / MSVC STL directories look kosher.
126if(NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" OR
127 NOT EXISTS "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link")
128 message(SEND_ERROR
129 "LLVM_NATIVE_TOOLCHAIN folder '${LLVM_NATIVE_TOOLCHAIN}' does not "
130 "point to a valid directory containing bin/clang-cl and bin/lld-link "
131 "binaries")
132endif()
133
134if(NOT EXISTS "${MSVC_BASE}" OR
135 NOT EXISTS "${MSVC_INCLUDE}" OR
136 NOT EXISTS "${MSVC_LIB}")
137 message(SEND_ERROR
138 "CMake variable MSVC_BASE must point to a folder containing MSVC "
139 "system headers and libraries")
140endif()
141
142if(NOT EXISTS "${WINSDK_BASE}" OR
143 NOT EXISTS "${WINSDK_INCLUDE}" OR
144 NOT EXISTS "${WINSDK_LIB}")
145 message(SEND_ERROR
146 "CMake variable WINSDK_BASE and WINSDK_VER must resolve to a valid "
147 "Windows SDK installation")
148endif()
149
150set(CMAKE_C_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
151set(CMAKE_CXX_COMPILER "${LLVM_NATIVE_TOOLCHAIN}/bin/clang-cl" CACHE FILEPATH "")
152set(CMAKE_LINKER "${LLVM_NATIVE_TOOLCHAIN}/bin/lld-link" CACHE FILEPATH "")
153
154# Even though we're cross-compiling, we need some native tools (e.g. llvm-tblgen), and those
155# native tools have to be built before we can start doing the cross-build. LLVM supports
156# a CROSS_TOOLCHAIN_FLAGS_NATIVE argument which consists of a list of flags to pass to CMake
157# when configuring the NATIVE portion of the cross-build. By default we construct this so
158# that it points to the tools in the same location as the native clang-cl that we're using.
159list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
160list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
161list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
162
163set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
164
165set(COMPILE_FLAGS
166 -D_CRT_SECURE_NO_WARNINGS
167 -imsvc "${MSVC_INCLUDE}"
168 -imsvc "${WINSDK_INCLUDE}/ucrt"
169 -imsvc "${WINSDK_INCLUDE}/shared"
170 -imsvc "${WINSDK_INCLUDE}/um"
171 -imsvc "${WINSDK_INCLUDE}/winrt")
172
173string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
174
175# We need to preserve any flags that were passed in by the user. However, we
176# can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
177# will be re-invoked on each reconfigure and therefore need to be idempotent.
178# The assignments to the _INITIAL cache variables don't use FORCE, so they'll
179# only be populated on the initial configure, and their values won't change
180# afterward.
181set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
182set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
183
184set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
185set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
186
187set(LINK_FLAGS
188 # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
189 /manifest:no
190
191 # FIXME: We should support target architectures other than x64.
192 -libpath:"${MSVC_LIB}/x64"
193 -libpath:"${WINSDK_LIB}/ucrt/x64"
194 -libpath:"${WINSDK_LIB}/um/x64")
195
196string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
197
198# See explanation for compiler flags above for the _INITIAL variables.
199set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
200set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
201
202set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
203set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
204
205set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
206set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
207
208# CMake populates these with a bunch of unnecessary libraries, which requires
209# extra case-correcting symlinks and what not. Instead, let projects explicitly
210# control which libraries they require.
211set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
212set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
213
214# CMake's InstallRequiredSystemLibraries module searches for a Visual Studio
215# installation in order to determine where to copy the required DLLs. This
216# installation won't exist when cross-compiling, of course, so silence the
217# resulting warnings about missing libraries.
218set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
219