Debian Codegraph @

clang/cmake/caches/cross-linux-toolchain.cmake

raw
1 # cross-linux-toolchain.cmake
2 #
3 # Set up a CMakeCache for a cross ARM Linux toolchain build on Windows/Linux hosts.
4 #
5 # This cache file can be used to build a multi-target cross toolchain to Linux
6 # on Windows/Linux platforms.
7 #
8 # NOTE: replaces CrossWinToARMLinux.cmake
9 #
10 # NOTE: the build requires a development ARM Linux root filesystem to use
11 # proper target platform depended library and header files.
12 #
13 # The build generates a proper clang configuration file with stored
14 # --sysroot argument for specified target triple. Also it is possible
15 # to specify configuration path via CMake arguments, such as
16 # -DCLANG_CONFIG_FILE_USER_DIR=<full-path-to-clang-configs>
17 # and/or
18 # -DCLANG_CONFIG_FILE_SYSTEM_DIR=<full-path-to-clang-configs>
19 #
20 # See more details here: https://clang.llvm.org/docs/UsersManual.html#configuration-files
21 #
22 # Configure:
23 # cmake -G Ninja \
24 # -DTOOLCHAIN_TARGET_TRIPLE=aarch64-unknown-linux-gnu \
25 # -DTOOLCHAIN_TARGET_SYSROOTFS=<path-to-develop-arm-linux-root-fs> \
26 # -DTOOLCHAIN_SHARED_LIBS=OFF \
27 # -DCMAKE_INSTALL_PREFIX=../install \
28 # -DCMAKE_CXX_FLAGS="-D__OPTIMIZE__" \
29 # -DREMOTE_TEST_HOST="<hostname>" \
30 # -DREMOTE_TEST_USER="<ssh_user_name>" \
31 # -C <llvm_src_root>/llvm-project/clang/cmake/caches/cross-linux-toolchain.cmake \
32 # <llvm_src_root>/llvm-project/llvm
33 # Build:
34 # cmake --build . --target install
35 # Tests:
36 # cmake --build . --target check-llvm
37 # cmake --build . --target check-clang
38 # cmake --build . --target check-lld
39 # cmake --build . --target check-compiler-rt-<TOOLCHAIN_TARGET_TRIPLE>
40 # cmake --build . --target check-cxxabi-<TOOLCHAIN_TARGET_TRIPLE>
41 # cmake --build . --target check-unwind-<TOOLCHAIN_TARGET_TRIPLE>
42 # cmake --build . --target check-cxx-<TOOLCHAIN_TARGET_TRIPLE>
43 #
44 # The custom parameters:
45 # TOOLCHAIN_TARGET_TRIPLE (default: aarch64-unknown-linux-gnu) - a list of target triples to support
46 # TOOLCHAIN_TARGET_SYSROOTFS-<target> - sysroot for the specific target triple (must be matched TOOLCHAIN_TARGET_TRIPLE)
47 # TOOLCHAIN_TARGET_SYSROOTFS - sysroot for other targets.
48 # TOOLCHAIN_TARGET_COMPILER_FLAGS-<target>
49 # TOOLCHAIN_TARGET_COMPILER_FLAGS
50 # TOOLCHAIN_TARGET_LINKER_FLAGS-<target>
51 # TOOLCHAIN_TARGET_LINKER_FLAGS
52 # TOOLCHAIN_SHARED_LIBS (default: OFF)
53 # TOOLCHAIN_STATIC_LIBS (default: NOT DEFINED)
54 # TOOLCHAIN_USE_STATIC_LIBS (default: ON)
55 #
56
57
58 # LLVM_PROJECT_DIR is the path to the llvm-project directory.
59 # The right way to compute it would probably be to use "${CMAKE_SOURCE_DIR}/../",
60 # but CMAKE_SOURCE_DIR is set to the wrong value on earlier CMake versions
61 # that we still need to support (for instance, 3.10.2).
62 get_filename_component(LLVM_PROJECT_DIR
63 "${CMAKE_CURRENT_LIST_DIR}/../../../"
64 ABSOLUTE)
65 # Store the passed defs to use them later.
66 get_cmake_property(vars_ VARIABLES)
67
68 # Avoid searching for the python3 interpreter during the runtimes configuration for the cross builds.
69 # It starts searching the python3 package using the target's sysroot path, that usually is not compatible with the build host.
70 find_package(Python3 COMPONENTS Interpreter)
71
72 # Allow override with the custom values.
73 if(NOT DEFINED DEFAULT_TEST_EXECUTOR)
74 set(DEFAULT_TEST_EXECUTOR "\\\"${Python3_EXECUTABLE}\\\" \\\"${LLVM_PROJECT_DIR}/libcxx/utils/ssh.py\\\" --host=${REMOTE_TEST_USER}@${REMOTE_TEST_HOST}")
75 endif()
76
77 if (NOT DEFINED LLVM_ENABLE_ASSERTIONS)
78 set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
79 endif()
80 if (NOT DEFINED LLVM_ENABLE_PROJECTS)
81 set(LLVM_ENABLE_PROJECTS "clang;clang-tools-extra;lld" CACHE STRING "")
82 endif()
83 if (NOT DEFINED LLVM_ENABLE_RUNTIMES)
84 set(LLVM_ENABLE_RUNTIMES "compiler-rt;libunwind;libcxxabi;libcxx" CACHE STRING "")
85 endif()
86
87 #Note: this is a list by default
88 if (NOT DEFINED TOOLCHAIN_TARGET_TRIPLE)
89 set(TOOLCHAIN_TARGET_TRIPLE "aarch64-unknown-linux-gnu")
90 endif()
91
92 #NOTE: we must normalize specified target triple to a fully specified triple,
93 # including the vendor part. It is necessary to synchronize the runtime library
94 # installation path and operable target triple by Clang to get a correct runtime
95 # path through `-print-runtime-dir` Clang option.
96 function(norm_triple name triple abi)
97 string(REPLACE "-" ";" t_ "${name}")
98 list(LENGTH t_ tlen_)
99 if (tlen_ LESS 3)
100 message(FATAL_ERROR "invalid target triple ${name}")
101 endif()
102 # We suppose missed vendor's part.
103 if (tlen_ LESS 4)
104 list(INSERT t_ 1 "unknown")
105 endif()
106 list(GET t_ 3 abi_)
107 string(REPLACE ";" "-" t_ "${t_}")
108
109 set(${triple} "${t_}" PARENT_SCOPE)
110 set(${abi} ${abi_} PARENT_SCOPE)
111 endfunction()
112
113 message(STATUS "Toolchain target triples: ${TOOLCHAIN_TARGET_TRIPLE}")
114
115 # Build the shared libraries for libc++/libc++abi/libunwind.
116 if (NOT DEFINED TOOLCHAIN_SHARED_LIBS)
117 set(TOOLCHAIN_SHARED_LIBS OFF)
118 endif()
119
120 # Enable usage of the static libunwind and libc++abi libraries.
121 if (NOT DEFINED TOOLCHAIN_USE_STATIC_LIBS)
122 set(TOOLCHAIN_USE_STATIC_LIBS ON)
123 endif()
124
125 if (NOT DEFINED CMAKE_BUILD_TYPE)
126 set(CMAKE_BUILD_TYPE "Release" CACHE STRING "")
127 endif()
128
129 if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY AND WIN32)
130 #Note: Always specify MT DLL for the LLDB build configurations on Windows host.
131 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
132 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebugDLL" CACHE STRING "")
133 else()
134 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL" CACHE STRING "")
135 endif()
136 # Grab all ucrt/vcruntime related DLLs into the binary installation folder.
137 set(CMAKE_INSTALL_UCRT_LIBRARIES ON CACHE BOOL "")
138 endif()
139
140 # Set up RPATH for the target runtime/builtin libraries.
141 # See some details here: https://reviews.llvm.org/D91099
142 if (NOT DEFINED RUNTIMES_INSTALL_RPATH)
143 set(RUNTIMES_INSTALL_RPATH "\$ORIGIN/../lib;${CMAKE_INSTALL_PREFIX}/lib")
144 endif()
145
146 set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: " CACHE STRING "")
147
148 # Set the first target in the list as the default target.
149 # (Allow custom default targets)
150 if (NOT DEFINED LLVM_DEFAULT_TARGET_TRIPLE)
151 list(GET TOOLCHAIN_TARGET_TRIPLE 0 LLVM_DEFAULT_TARGET_TRIPLE)
152 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}" CACHE STRING "")
153 endif()
154 string(REPLACE "-" ";" t_ "${LLVM_DEFAULT_TARGET_TRIPLE}")
155 list(GET t_ 0 LLVM_TARGET_ARCH)
156
157 # Clang configuration.
158 set(CLANG_DEFAULT_CXX_STDLIB "libc++" CACHE STRING "")
159 set(CLANG_DEFAULT_LINKER "lld" CACHE STRING "")
160 set(CLANG_DEFAULT_OBJCOPY "llvm-objcopy" CACHE STRING "")
161 set(CLANG_DEFAULT_RTLIB "compiler-rt" CACHE STRING "")
162 set(CLANG_DEFAULT_UNWINDLIB "libunwind" CACHE STRING "")
163
164 #
165 # Configure the builtin targets.
166 #
167 set(LLVM_BUILTIN_TARGETS "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "")
168
169 foreach (target ${LLVM_BUILTIN_TARGETS})
170 set(BUILTINS_${target}_CMAKE_SYSTEM_NAME "Linux" CACHE STRING "")
171 set(BUILTINS_${target}_CMAKE_INSTALL_RPATH "${RUNTIMES_INSTALL_RPATH}" CACHE STRING "")
172 set(BUILTINS_${target}_CMAKE_BUILD_WITH_INSTALL_RPATH ON CACHE BOOL "")
173 set(BUILTINS_${target}_LLVM_CMAKE_DIR "${LLVM_PROJECT_DIR}/llvm/cmake/modules" CACHE PATH "")
174
175 if (DEFINED TOOLCHAIN_TARGET_COMPILER_FLAGS)
176 foreach(lang C;CXX;ASM)
177 set(BUILTINS_${target}_CMAKE_${lang}_FLAGS "${TOOLCHAIN_TARGET_COMPILER_FLAGS}" CACHE STRING "")
178 endforeach()
179 endif()
180 foreach(type SHARED;MODULE;EXE)
181 set(BUILTINS_${target}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
182 endforeach()
183 endforeach()
184
185 #
186 # Configure all runtime targets.
187 #
188 set(LLVM_RUNTIME_TARGETS "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "")
189 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR ON CACHE BOOL "")
190
191 foreach(target ${LLVM_RUNTIME_TARGETS})
192 norm_triple(${target} triple_ abi_)
193
194 if (DEFINED "TOOLCHAIN_TARGET_SYSROOTFS-${target}")
195 set(sysroot_ "${TOOLCHAIN_TARGET_SYSROOTFS-${target}}")
196 elseif (DEFINED TOOLCHAIN_TARGET_SYSROOTFS)
197 set(sysroot_ ${TOOLCHAIN_TARGET_SYSROOTFS})
198 endif()
199
200 file(REMOVE "${CMAKE_BINARY_DIR}/bin/${triple_}.cfg")
201
202 if (DEFINED sysroot_)
203 message(STATUS "Toolchain target sysroot: ${target}: ${sysroot_}")
204 # Store the --sysroot argument for the compiler-rt test flags.
205 set(sysroot_flags --sysroot='${sysroot_}')
206 # Generate the clang configuration file for the specified target triple and store --sysroot in there.
207 #Note: we use normalized target triple for the configuration file name.
208 file(APPEND "${CMAKE_BINARY_DIR}/bin/${triple_}.cfg" ${sysroot_flags} "\n")
209 endif()
210
211 # Pass a list of enabled runtimes to the runtime.
212 set(RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES "${LLVM_ENABLE_RUNTIMES}" CACHE STRING "")
213
214 set(RUNTIMES_${target}_CMAKE_SYSTEM_NAME "Linux" CACHE STRING "")
215 set(RUNTIMES_${target}_CMAKE_INSTALL_RPATH "${RUNTIMES_INSTALL_RPATH}" CACHE STRING "")
216 set(RUNTIMES_${target}_CMAKE_BUILD_WITH_INSTALL_RPATH ON CACHE BOOL "")
217
218 if (DEFINED TOOLCHAIN_TARGET_COMPILER_FLAGS OR DEFINED TOOLCHAIN_TARGET_COMPILER_FLAGS-${target})
219 message(STATUS "Toolchain target compiler flags: ${target}: ${TOOLCHAIN_TARGET_COMPILER_FLAGS-${target}}")
220 foreach(lang C;CXX;ASM)
221 set(RUNTIMES_${target}_CMAKE_${lang}_FLAGS "${TOOLCHAIN_TARGET_COMPILER_FLAGS} ${TOOLCHAIN_TARGET_COMPILER_FLAGS-${target}}" CACHE STRING "")
222 endforeach()
223 # Update the target clang cofiguration file with these flags.
224 file(APPEND "${CMAKE_BINARY_DIR}/bin/${triple_}.cfg" ${TOOLCHAIN_TARGET_COMPILER_FLAGS} "\n")
225 file(APPEND "${CMAKE_BINARY_DIR}/bin/${triple_}.cfg" ${TOOLCHAIN_TARGET_COMPILER_FLAGS-${target}} "\n")
226 endif()
227 if (DEFINED TOOLCHAIN_TARGET_LINKER_FLAGS OR DEFINED TOOLCHAIN_TARGET_LINKER_FLAGS-${target})
228 message(STATUS "Toolchain target linker flags: ${target}: ${TOOLCHAIN_TARGET_LINKER_FLAGS-${target}}")
229 foreach(type SHARED;MODULE;EXE)
230 set(RUNTIMES_${target}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld ${TOOLCHAIN_TARGET_LINKER_FLAGS} ${TOOLCHAIN_TARGET_LINKER_FLAGS-${target}}" CACHE STRING "")
231 endforeach()
232 endif()
233
234 if (abi_ MATCHES "(musl|pauthtest)")
235 set(RUNTIMES_${target}_LIBCXX_HAS_MUSL_LIBC ON CACHE BOOL "")
236 set(RUNTIMES_${target}_COMPILER_RT_BUILD_BUILTINS OFF CACHE BOOL "")
237 else()
238 set(RUNTIMES_${target}_COMPILER_RT_BUILD_BUILTINS ON CACHE BOOL "")
239 endif()
240
241 set(RUNTIMES_${target}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "")
242 set(RUNTIMES_${target}_COMPILER_RT_BUILD_CRT ON CACHE BOOL "")
243
244 #Note: COMPILER_RT_DEFAULT_TARGET_ONLY must be off for COMPILER_RT_DEFAULT_TARGET_TRIPLE.
245 set(RUNTIMES_${target}_COMPILER_RT_DEFAULT_TARGET_ONLY ON CACHE BOOL "")
246
247 # Required if COMPILER_RT_DEFAULT_TARGET_ONLY is ON
248 if (RUNTIMES_${target}_COMPILER_RT_DEFAULT_TARGET_ONLY)
249 set(RUNTIMES_${target}_CMAKE_C_COMPILER_TARGET "${target}" CACHE STRING "")
250 set(RUNTIMES_${target}_CMAKE_CXX_COMPILER_TARGET "${target}" CACHE STRING "")
251 endif()
252
253 set(RUNTIMES_${target}_COMPILER_RT_CXX_LIBRARY libcxx CACHE STRING "")
254
255 set(RUNTIMES_${target}_COMPILER_RT_BUILD_SANITIZERS OFF CACHE BOOL "")
256 set(RUNTIMES_${target}_COMPILER_RT_BUILD_XRAY OFF CACHE BOOL "")
257 set(RUNTIMES_${target}_COMPILER_RT_BUILD_LIBFUZZER OFF CACHE BOOL "")
258 set(RUNTIMES_${target}_COMPILER_RT_BUILD_PROFILE OFF CACHE BOOL "")
259 set(RUNTIMES_${target}_COMPILER_RT_BUILD_MEMPROF OFF CACHE BOOL "")
260 set(RUNTIMES_${target}_COMPILER_RT_BUILD_ORC OFF CACHE BOOL "")
261
262 set(RUNTIMES_${target}_COMPILER_RT_INCLUDE_TESTS ON CACHE BOOL "")
263 set(RUNTIMES_${target}_COMPILER_RT_CAN_EXECUTE_TESTS ON CACHE BOOL "")
264 # The compiler-rt tests disable the clang configuration files during the execution by setting CLANG_NO_DEFAULT_CONFIG=1
265 # and drops out the --sysroot from there. Provide it explicity via the test flags here if target sysroot has been specified.
266 set(RUNTIMES_${target}_COMPILER_RT_TEST_COMPILER_CFLAGS "--stdlib=libc++ ${sysroot_flags}" CACHE STRING "")
267
268 set(RUNTIMES_${target}_LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")
269
270 set(RUNTIMES_${target}_LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "")
271 set(RUNTIMES_${target}_LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "")
272 set(RUNTIMES_${target}_LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS OFF CACHE BOOL "")
273
274 set(RUNTIMES_${target}_LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
275 set(RUNTIMES_${target}_LIBCXX_CXX_ABI "libcxxabi" CACHE STRING "") #!!!
276 set(RUNTIMES_${target}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "")
277
278
279 if (DEFINED TOOLCHAIN_SHARED_LIBS)
280 set(RUNTIMES_${target}_LIBUNWIND_ENABLE_SHARED ${TOOLCHAIN_SHARED_LIBS} CACHE BOOL "")
281 set(RUNTIMES_${target}_LIBCXXABI_ENABLE_SHARED ${TOOLCHAIN_SHARED_LIBS} CACHE BOOL "")
282 set(RUNTIMES_${target}_LIBCXX_ENABLE_SHARED ${TOOLCHAIN_SHARED_LIBS} CACHE BOOL "")
283 endif()
284 if (DEFINED TOOLCHAIN_STATIC_LIBS)
285 set(RUNTIMES_${target}_LIBUNWIND_ENABLE_STATIC ${TOOLCHAIN_STATIC_LIBS} CACHE BOOL "")
286 set(RUNTIMES_${target}_LIBCXXABI_ENABLE_STATIC ${TOOLCHAIN_STATIC_LIBS} CACHE BOOL "")
287 set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC ${TOOLCHAIN_STATIC_LIBS} CACHE BOOL "")
288 endif()
289
290 if (DEFINED TOOLCHAIN_USE_STATIC_LIBS)
291 set(RUNTIMES_${target}_LIBCXXABI_ENABLE_STATIC_UNWINDER ${TOOLCHAIN_USE_STATIC_LIBS} CACHE BOOL "")
292 # Merge libc++ and libc++abi libraries into the single libc++ library file.
293 set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC_ABI_LIBRARY ${TOOLCHAIN_USE_STATIC_LIBS} CACHE BOOL "")
294 endif()
295
296 # Forcely disable the libc++ benchmarks on Windows build hosts
297 # (current benchmark test configuration does not support the cross builds there).
298 if (WIN32)
299 set(RUNTIMES_${target}_LIBCXX_INCLUDE_BENCHMARKS OFF CACHE BOOL "")
300 endif(WIN32)
301
302 set(RUNTIMES_${target}_Python3_EXECUTABLE ${Python3_EXECUTABLE} CACHE PATH "")
303
304 set(RUNTIMES_${target}_LIBUNWIND_TEST_PARAMS_default "${RUNTIMES_${target}_TEST_PARAMS}")
305 set(RUNTIMES_${target}_LIBCXXABI_TEST_PARAMS_default "${RUNTIMES_${target}_TEST_PARAMS}")
306 set(RUNTIMES_${target}_LIBCXX_TEST_PARAMS_default "${RUNTIMES_${target}_TEST_PARAMS}")
307
308 # Remote test configuration.
309 if(DEFINED REMOTE_TEST_HOST)
310 set(RUNTIMES_${target}_COMPILER_RT_EMULATOR
311 "\\\"${Python3_EXECUTABLE}\\\" \\\"${LLVM_PROJECT_DIR}/llvm/utils/remote-exec.py\\\" --host=${REMOTE_TEST_USER}@${REMOTE_TEST_HOST}"
312 CACHE STRING "")
313
314 list(APPEND RUNTIMES_${target}_LIBUNWIND_TEST_PARAMS_default "executor=${DEFAULT_TEST_EXECUTOR}")
315 list(APPEND RUNTIMES_${target}_LIBCXXABI_TEST_PARAMS_default "executor=${DEFAULT_TEST_EXECUTOR}")
316 list(APPEND RUNTIMES_${target}_LIBCXX_TEST_PARAMS_default "executor=${DEFAULT_TEST_EXECUTOR}")
317 endif()
318
319 set(RUNTIMES_${target}_LIBUNWIND_TEST_PARAMS "${RUNTIMES_${target}_LIBUNWIND_TEST_PARAMS_default}" CACHE INTERNAL "")
320 set(RUNTIMES_${target}_LIBCXXABI_TEST_PARAMS "${RUNTIMES_${target}_LIBCXXABI_TEST_PARAMS_default}" CACHE INTERNAL "")
321 set(RUNTIMES_${target}_LIBCXX_TEST_PARAMS "${RUNTIMES_${target}_LIBCXX_TEST_PARAMS_default}" CACHE INTERNAL "")
322
323 # Apply all passed LIBCXX|LIBCXXABI|LIBUNWIND|COMPILER_RT parameters to each runtime targets.
324 # Override the existing variable values by using FORCE.
325 # Because we don't know a type of the passed vars, use INTERNAL keyword for that.
326 foreach(v_ ${vars_})
327 if(v_ MATCHES "^(LIBCXX|LIBCXXABI|LIBUNWIND|COMPILER_RT)_")
328 set(RUNTIMES_${target}_${v_} ${${v_}} CACHE INTERNAL "" FORCE)
329 endif()
330 endforeach()
331 unset(sysroot_)
332 unset(sysroot_flags)
333 endforeach()
334
335 set(LLVM_INSTALL_TOOLCHAIN_ONLY ON CACHE BOOL "")
336 set(LLVM_TOOLCHAIN_TOOLS
337 llvm-ar
338 llvm-cov
339 llvm-cxxfilt
340 llvm-dwarfdump
341 llvm-lib
342 llvm-nm
343 llvm-objdump
344 llvm-pdbutil
345 llvm-profdata
346 llvm-ranlib
347 llvm-readobj
348 llvm-size
349 llvm-symbolizer
350 CACHE STRING "")
351
352 set(LLVM_DISTRIBUTION_COMPONENTS
353 clang
354 lld
355 LTO
356 clang-format
357 builtins
358 runtimes
359 ${LLVM_TOOLCHAIN_TOOLS}
360 CACHE STRING "")