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