llvm-project
285 строк · 12.7 Кб
1# The CompilerRT build system requires CMake version 2.8.8 or higher in order
2# to use its support for building convenience "libraries" as a collection of
3# .o files. This is particularly useful in producing larger, more complex
4# runtime libraries.
5
6include(BuiltinTests)
7include(CheckIncludeFile)
8include(CheckCXXSourceCompiles)
9include(GNUInstallDirs)
10include(GetClangResourceDir)
11include(ExtendPath)
12include(CompilerRTDarwinUtils)
13
14check_include_file(unwind.h HAVE_UNWIND_H)
15
16# Used by sanitizer_common and tests.
17check_include_file(rpc/xdr.h HAVE_RPC_XDR_H)
18if (NOT HAVE_RPC_XDR_H)
19set(HAVE_RPC_XDR_H 0)
20endif()
21
22# Top level target used to build all compiler-rt libraries.
23add_custom_target(compiler-rt ALL)
24add_custom_target(install-compiler-rt)
25add_custom_target(install-compiler-rt-stripped)
26set_property(TARGET compiler-rt PROPERTY FOLDER "Compiler-RT/Metatargets")
27set_property(
28TARGET
29install-compiler-rt
30install-compiler-rt-stripped
31PROPERTY
32FOLDER "Compiler-RT/Installation"
33)
34
35# Setting these variables from an LLVM build is sufficient that compiler-rt can
36# construct the output paths, so it can behave as if it were in-tree here.
37if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
38set(LLVM_TREE_AVAILABLE On)
39endif()
40
41if (LLVM_TREE_AVAILABLE)
42# Setup the paths where compiler-rt runtimes and headers should be stored.
43get_clang_resource_dir(COMPILER_RT_OUTPUT_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/..)
44set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
45get_clang_resource_dir(COMPILER_RT_INSTALL_PATH)
46option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
47${LLVM_INCLUDE_TESTS})
48option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
49${LLVM_ENABLE_WERROR})
50
51# Use just-built Clang to compile/link tests on all platforms.
52if (CMAKE_CROSSCOMPILING)
53if (CMAKE_HOST_WIN32)
54set(_host_executable_suffix ".exe")
55else()
56set(_host_executable_suffix "")
57endif()
58else()
59set(_host_executable_suffix ${CMAKE_EXECUTABLE_SUFFIX})
60endif()
61set(COMPILER_RT_TEST_COMPILER
62${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${_host_executable_suffix})
63set(COMPILER_RT_TEST_CXX_COMPILER
64${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${_host_executable_suffix})
65else()
66# Take output dir and install path from the user.
67set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
68"Path where built compiler-rt libraries should be stored.")
69set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
70"Path where built compiler-rt executables should be stored.")
71set(COMPILER_RT_INSTALL_PATH "" CACHE PATH
72"Prefix for directories where built compiler-rt artifacts should be installed.")
73option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
74option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
75# Use a host compiler to compile/link tests.
76set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
77set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
78endif()
79
80if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
81set(COMPILER_RT_TEST_COMPILER_ID Clang)
82elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
83set(COMPILER_RT_TEST_COMPILER_ID Clang)
84else()
85set(COMPILER_RT_TEST_COMPILER_ID GNU)
86endif()
87
88if(NOT DEFINED COMPILER_RT_OS_DIR)
89if(ANDROID)
90# The CMAKE_SYSTEM_NAME for Android is Android, but the OS is Linux and the
91# driver will search for compiler-rt libraries in the "linux" directory.
92set(COMPILER_RT_OS_DIR linux)
93else()
94string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
95endif()
96endif()
97if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
98set(COMPILER_RT_OUTPUT_LIBRARY_DIR
99${COMPILER_RT_OUTPUT_DIR}/lib)
100extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" lib)
101set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
102"Path where built compiler-rt libraries should be installed.")
103else(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
104set(COMPILER_RT_OUTPUT_LIBRARY_DIR
105${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
106extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "lib/${COMPILER_RT_OS_DIR}")
107set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
108"Path where built compiler-rt libraries should be installed.")
109endif()
110extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_BINDIR}")
111set(COMPILER_RT_INSTALL_BINARY_DIR "${default_install_path}" CACHE PATH
112"Path where built compiler-rt executables should be installed.")
113extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_INCLUDEDIR}")
114set(COMPILER_RT_INSTALL_INCLUDE_DIR "${default_install_path}" CACHE PATH
115"Path where compiler-rt headers should be installed.")
116extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_DATADIR}")
117set(COMPILER_RT_INSTALL_DATA_DIR "${default_install_path}" CACHE PATH
118"Path where compiler-rt data files should be installed.")
119
120if(APPLE)
121# On Darwin if /usr/include/c++ doesn't exist, the user probably has Xcode but
122# not the command line tools (or is using macOS 10.14 or newer). If this is
123# the case, we need to find the OS X sysroot to pass to clang.
124if(NOT EXISTS /usr/include/c++)
125execute_process(COMMAND xcrun -sdk macosx --show-sdk-path
126OUTPUT_VARIABLE OSX_SYSROOT
127ERROR_QUIET
128OUTPUT_STRIP_TRAILING_WHITESPACE)
129if (NOT OSX_SYSROOT OR NOT EXISTS ${OSX_SYSROOT})
130message(WARNING "Detected OSX_SYSROOT ${OSX_SYSROOT} does not exist")
131else()
132message(STATUS "Found OSX_SYSROOT: ${OSX_SYSROOT}")
133set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
134endif()
135else()
136set(OSX_SYSROOT_FLAG "")
137endif()
138
139try_compile_only(COMPILER_RT_HAS_DARWIN_TARGET_VARIANT_FLAG
140FLAGS
141"-target" "x86_64-apple-macos10.15"
142"-darwin-target-variant" "x86_64-apple-ios13.1-macabi"
143"-Werror")
144option(COMPILER_RT_ENABLE_MACCATALYST "Enable building for Mac Catalyst" ${COMPILER_RT_HAS_DARWIN_TARGET_VARIANT_FLAG})
145
146# Don't enable COMPILER_RT_ENABLE_IOS if we can't find the sdk dir.
147# This can happen when you only have the commandline tools installed
148# which doesn't come with the iOS SDK.
149find_darwin_sdk_dir(HAS_IOS_SDK "iphoneos")
150set(COMPILER_RT_ENABLE_IOS_DEFAULT On)
151if("${HAS_IOS_SDK}" STREQUAL "")
152message(WARNING "iOS SDK not found! Building compiler-rt without iOS support.")
153set(COMPILER_RT_ENABLE_IOS_DEFAULT Off)
154endif()
155option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" ${COMPILER_RT_ENABLE_IOS_DEFAULT})
156
157option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
158option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
159option(COMPILER_RT_ENABLE_XROS "Enable building for xrOS - Experimental" Off)
160
161else()
162option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off)
163endif()
164
165if(WIN32 AND NOT MINGW AND NOT CYGWIN)
166set(CMAKE_SHARED_LIBRARY_PREFIX_C "")
167set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "")
168set(CMAKE_STATIC_LIBRARY_PREFIX_C "")
169set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "")
170set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
171set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib")
172endif()
173
174macro(test_targets)
175# Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
176# what version of MSVC to pretend to be so that the STL works.
177set(MSVC_VERSION_FLAG "")
178if (MSVC)
179execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
180OUTPUT_QUIET
181ERROR_VARIABLE MSVC_COMPAT_VERSION
182)
183string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
184MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
185if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
186set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
187# Add this flag into the host build if this is clang-cl.
188if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
189append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
190elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
191# Add this flag to test compiles to suppress clang's auto-detection
192# logic.
193append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
194endif()
195endif()
196endif()
197
198# Generate the COMPILER_RT_SUPPORTED_ARCH list.
199if(ANDROID)
200# Examine compiler output to determine target architecture.
201detect_target_arch()
202set(COMPILER_RT_OS_SUFFIX "-android")
203elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
204if(COMPILER_RT_DEFAULT_TARGET_ONLY)
205add_default_target_arch(${COMPILER_RT_DEFAULT_TARGET_ARCH})
206elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
207if(NOT MSVC)
208test_target_arch(x86_64 "" "-m64")
209test_target_arch(i386 __i386__ "-m32")
210else()
211if (CMAKE_SIZEOF_VOID_P EQUAL 4)
212test_target_arch(i386 "" "")
213else()
214test_target_arch(x86_64 "" "")
215endif()
216endif()
217elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "loongarch64")
218test_target_arch(loongarch64 "" "")
219elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc64le|ppc64le")
220test_target_arch(powerpc64le "" "-m64")
221elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
222test_target_arch(powerpc "" "-m32")
223test_target_arch(powerpc64 "" "-m64")
224elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
225test_target_arch(s390x "" "")
226elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "sparc")
227test_target_arch(sparc "" "-m32")
228test_target_arch(sparcv9 "" "-m64")
229elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
230CHECK_SYMBOL_EXISTS (_MIPS_ARCH_MIPS32R6 "" COMPILER_RT_MIPS32R6)
231CHECK_SYMBOL_EXISTS (_MIPS_ARCH_MIPS64R6 "" COMPILER_RT_MIPS64R6)
232CHECK_SYMBOL_EXISTS (__mips64 "" COMPILER_RT_MIPS_64)
233CHECK_SYMBOL_EXISTS (__MIPSEL__ "" COMPILER_RT_MIPS_EL)
234if ("${COMPILER_RT_MIPS_64}")
235set(COMPILER_RT_DEFAULT_TARGET_ARCH "mips64")
236else()
237set(COMPILER_RT_DEFAULT_TARGET_ARCH "mips")
238endif()
239if ("${COMPILER_RT_MIPS_EL}")
240set(COMPILER_RT_DEFAULT_TARGET_ARCH "${COMPILER_RT_DEFAULT_TARGET_ARCH}el")
241endif()
242
243# FIXME: Ideally, we would build the N32 library too.
244if("${COMPILER_RT_MIPS_EL}" AND ("${COMPILER_RT_MIPS32R6}" OR "${COMPILER_RT_MIPS64R6}"))
245test_target_arch(mipsel "" "-mips32r6" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
246test_target_arch(mips64el "" "-mips64r6" "-mabi=64")
247elseif("${COMPILER_RT_MIPS_EL}")
248test_target_arch(mipsel "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
249test_target_arch(mips64el "" "-mips64r2" "-mabi=64")
250elseif("${COMPILER_RT_MIPS32R6}" OR "${COMPILER_RT_MIPS64R6}")
251test_target_arch(mips "" "-mips32r6" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
252test_target_arch(mips64 "" "-mips64r6" "-mabi=64")
253else()
254test_target_arch(mips "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
255test_target_arch(mips64 "" "-mips64r2" "-mabi=64")
256endif()
257elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
258if(WIN32)
259test_target_arch(arm "" "" "")
260else()
261test_target_arch(armv4t "" "-march=armv4t" "-mfloat-abi=soft")
262test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft")
263test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
264test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
265endif()
266elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "avr")
267test_target_arch(avr "__AVR__" "--target=avr")
268elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
269test_target_arch(aarch32 "" "-march=armv8-a")
270elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
271test_target_arch(aarch64 "" "-march=armv8-a")
272elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv32")
273test_target_arch(riscv32 "" "")
274elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv64")
275test_target_arch(riscv64 "" "")
276elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
277test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
278elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
279test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
280elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "ve")
281test_target_arch(ve "__ve__" "--target=ve-unknown-none")
282endif()
283set(COMPILER_RT_OS_SUFFIX "")
284endif()
285endmacro()
286