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
6 include(CheckIncludeFile)
7 include(CheckCXXSourceCompiles)
9 check_include_file(unwind.h HAVE_UNWIND_H)
11 # Used by sanitizer_common and tests.
12 check_include_file(rpc/xdr.h HAVE_RPC_XDR_H)
13 if (NOT HAVE_RPC_XDR_H)
17 # Top level target used to build all compiler-rt libraries.
18 add_custom_target(compiler-rt ALL)
19 add_custom_target(install-compiler-rt)
20 add_custom_target(install-compiler-rt-stripped)
25 install-compiler-rt-stripped
27 FOLDER "Compiler-RT Misc"
30 # Setting these variables from an LLVM build is sufficient that compiler-rt can
31 # construct the output paths, so it can behave as if it were in-tree here.
32 if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
33 set(LLVM_TREE_AVAILABLE On)
36 if (LLVM_TREE_AVAILABLE)
37 # Compute the Clang version from the LLVM version.
38 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
39 # in Clang cmake files, instead of copying the rules here.
40 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
42 # Setup the paths where compiler-rt runtimes and headers should be stored.
43 set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
44 set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
45 set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
46 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
47 ${LLVM_INCLUDE_TESTS})
48 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
49 ${LLVM_ENABLE_WERROR})
51 # Use just-built Clang to compile/link tests on all platforms.
52 if (CMAKE_CROSSCOMPILING)
54 set(_host_executable_suffix ".exe")
56 set(_host_executable_suffix "")
59 set(_host_executable_suffix ${CMAKE_EXECUTABLE_SUFFIX})
61 set(COMPILER_RT_TEST_COMPILER
62 ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${_host_executable_suffix})
63 set(COMPILER_RT_TEST_CXX_COMPILER
64 ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${_host_executable_suffix})
66 # Take output dir and install path from the user.
67 set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
68 "Path where built compiler-rt libraries should be stored.")
69 set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
70 "Path where built compiler-rt executables should be stored.")
71 set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH
72 "Path where built compiler-rt libraries should be installed.")
73 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
74 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
75 # Use a host compiler to compile/link tests.
76 set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
77 set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
80 if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
81 set(COMPILER_RT_TEST_COMPILER_ID Clang)
82 elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
83 set(COMPILER_RT_TEST_COMPILER_ID Clang)
85 set(COMPILER_RT_TEST_COMPILER_ID GNU)
88 if(NOT DEFINED COMPILER_RT_OS_DIR)
89 string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
91 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
92 set(COMPILER_RT_LIBRARY_OUTPUT_DIR
93 ${COMPILER_RT_OUTPUT_DIR})
94 set(COMPILER_RT_LIBRARY_INSTALL_DIR
95 ${COMPILER_RT_INSTALL_PATH})
96 else(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
97 set(COMPILER_RT_LIBRARY_OUTPUT_DIR
98 ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
99 set(COMPILER_RT_LIBRARY_INSTALL_DIR
100 ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
104 # On Darwin if /usr/include/c++ doesn't exist, the user probably has Xcode but
105 # not the command line tools (or is using macOS 10.14 or newer). If this is
106 # the case, we need to find the OS X sysroot to pass to clang.
107 if(NOT EXISTS /usr/include/c++)
108 execute_process(COMMAND xcrun -sdk macosx --show-sdk-path
109 OUTPUT_VARIABLE OSX_SYSROOT
111 OUTPUT_STRIP_TRAILING_WHITESPACE)
112 if (NOT OSX_SYSROOT OR NOT EXISTS ${OSX_SYSROOT})
113 message(WARNING "Detected OSX_SYSROOT ${OSX_SYSROOT} does not exist")
115 message(STATUS "Found OSX_SYSROOT: ${OSX_SYSROOT}")
116 set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
119 set(OSX_SYSROOT_FLAG "")
122 option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" On)
123 option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
124 option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
127 option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off)
130 if(WIN32 AND NOT MINGW AND NOT CYGWIN)
131 set(CMAKE_SHARED_LIBRARY_PREFIX_C "")
132 set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "")
133 set(CMAKE_STATIC_LIBRARY_PREFIX_C "")
134 set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "")
135 set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
136 set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib")
140 # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
141 # what version of MSVC to pretend to be so that the STL works.
142 set(MSVC_VERSION_FLAG "")
144 execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
146 ERROR_VARIABLE MSVC_COMPAT_VERSION
148 string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
149 MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
150 if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
151 set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
152 # Add this flag into the host build if this is clang-cl.
153 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
154 append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
155 elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
156 # Add this flag to test compiles to suppress clang's auto-detection
158 append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
163 # Generate the COMPILER_RT_SUPPORTED_ARCH list.
165 # Examine compiler output to determine target architecture.
167 set(COMPILER_RT_OS_SUFFIX "-android")
168 elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
169 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
170 add_default_target_arch(${COMPILER_RT_DEFAULT_TARGET_ARCH})
171 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
173 if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
174 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
175 test_target_arch(i386 __i386__ "-m32")
177 test_target_arch(x86_64 "" "-m64")
180 test_target_arch(x86_64 "" "-m64")
181 test_target_arch(i386 __i386__ "-m32")
184 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
185 test_target_arch(i386 "" "")
187 test_target_arch(x86_64 "" "")
190 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
191 # Strip out -nodefaultlibs when calling TEST_BIG_ENDIAN. Configuration
192 # will fail with this option when building with a sanitizer.
193 cmake_push_check_state()
194 string(REPLACE "-nodefaultlibs" "" CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
195 TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
196 cmake_pop_check_state()
198 if(HOST_IS_BIG_ENDIAN)
199 test_target_arch(powerpc64 "" "-m64")
201 test_target_arch(powerpc64le "" "-m64")
203 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
204 test_target_arch(s390x "" "")
205 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "sparc")
206 test_target_arch(sparc "" "-m32")
207 test_target_arch(sparcv9 "" "-m64")
208 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el")
209 # Gcc doesn't accept -m32/-m64 so we do the next best thing and use
210 # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match
211 # clang's default CPU's. In the 64-bit case, we must also specify the ABI
212 # since the default ABI differs between gcc and clang.
213 # FIXME: Ideally, we would build the N32 library too.
214 test_target_arch(mipsel "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE" "-D_FILE_OFFSET_BITS=64")
215 test_target_arch(mips64el "" "-mips64r2" "-mabi=64")
216 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
217 test_target_arch(mips "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE" "-D_FILE_OFFSET_BITS=64")
218 test_target_arch(mips64 "" "-mips64r2" "-mabi=64")
219 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
221 test_target_arch(arm "" "" "")
223 test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
224 test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
225 test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft")
227 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
228 test_target_arch(aarch32 "" "-march=armv8-a")
229 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
230 test_target_arch(aarch64 "" "-march=armv8-a")
231 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv32")
232 test_target_arch(riscv32 "" "")
233 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv64")
234 test_target_arch(riscv64 "" "")
235 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
236 test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
237 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
238 test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
240 set(COMPILER_RT_OS_SUFFIX "")