Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / cmake / base-config-ix.cmake
blob908c8a40278cf0cd1ed95cf8225c182942152d5e
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(
27   TARGET
28     compiler-rt
29     install-compiler-rt
30     install-compiler-rt-stripped
31   PROPERTY
32     FOLDER "Compiler-RT Misc"
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 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)
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 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")
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 else()
85   set(COMPILER_RT_TEST_COMPILER_ID GNU)
86 endif()
88 if(NOT DEFINED COMPILER_RT_OS_DIR)
89   if(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.
92     set(COMPILER_RT_OS_DIR linux)
93   else()
94     string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
95   endif()
96 endif()
97 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
98   set(COMPILER_RT_OUTPUT_LIBRARY_DIR
99     ${COMPILER_RT_OUTPUT_DIR}/lib)
100   extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" lib)
101   set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
102     "Path where built compiler-rt libraries should be installed.")
103 else(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
104   set(COMPILER_RT_OUTPUT_LIBRARY_DIR
105     ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
106   extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "lib/${COMPILER_RT_OS_DIR}")
107   set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
108     "Path where built compiler-rt libraries should be installed.")
109 endif()
110 extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_BINDIR}")
111 set(COMPILER_RT_INSTALL_BINARY_DIR "${default_install_path}" CACHE PATH
112   "Path where built compiler-rt executables should be installed.")
113 extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_INCLUDEDIR}")
114 set(COMPILER_RT_INSTALL_INCLUDE_DIR "${default_install_path}" CACHE PATH
115   "Path where compiler-rt headers should be installed.")
116 extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "${CMAKE_INSTALL_DATADIR}")
117 set(COMPILER_RT_INSTALL_DATA_DIR "${default_install_path}" CACHE PATH
118   "Path where compiler-rt data files should be installed.")
120 if(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.
124   if(NOT EXISTS /usr/include/c++)
125     execute_process(COMMAND xcrun -sdk macosx --show-sdk-path
126        OUTPUT_VARIABLE OSX_SYSROOT
127        ERROR_QUIET
128        OUTPUT_STRIP_TRAILING_WHITESPACE)
129     if (NOT OSX_SYSROOT OR NOT EXISTS ${OSX_SYSROOT})
130       message(WARNING "Detected OSX_SYSROOT ${OSX_SYSROOT} does not exist")
131     else()
132       message(STATUS "Found OSX_SYSROOT: ${OSX_SYSROOT}")
133       set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
134     endif()
135   else()
136     set(OSX_SYSROOT_FLAG "")
137   endif()
139   try_compile_only(COMPILER_RT_HAS_DARWIN_TARGET_VARIANT_FLAG
140                    FLAGS
141                    "-target" "x86_64-apple-macos10.15"
142                    "-darwin-target-variant" "x86_64-apple-ios13.1-macabi"
143                    "-Werror")
144   option(COMPILER_RT_ENABLE_MACCATALYST "Enable building for Mac Catalyst" ${COMPILER_RT_HAS_DARWIN_TARGET_VARIANT_FLAG})
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.
149   find_darwin_sdk_dir(HAS_IOS_SDK "iphoneos")
150   set(COMPILER_RT_ENABLE_IOS_DEFAULT On)
151   if("${HAS_IOS_SDK}" STREQUAL "")
152     message(WARNING "iOS SDK not found! Building compiler-rt without iOS support.")
153     set(COMPILER_RT_ENABLE_IOS_DEFAULT Off)
154   endif()
155   option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" ${COMPILER_RT_ENABLE_IOS_DEFAULT})
157   option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
158   option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
160 else()
161   option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off)
162 endif()
164 if(WIN32 AND NOT MINGW AND NOT CYGWIN)
165   set(CMAKE_SHARED_LIBRARY_PREFIX_C "")
166   set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "")
167   set(CMAKE_STATIC_LIBRARY_PREFIX_C "")
168   set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "")
169   set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
170   set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib")
171 endif()
173 macro(test_targets)
174   # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
175   # what version of MSVC to pretend to be so that the STL works.
176   set(MSVC_VERSION_FLAG "")
177   if (MSVC)
178     execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
179       OUTPUT_QUIET
180       ERROR_VARIABLE MSVC_COMPAT_VERSION
181       )
182     string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
183       MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
184     if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
185       set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
186       # Add this flag into the host build if this is clang-cl.
187       if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
188         append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
189       elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
190         # Add this flag to test compiles to suppress clang's auto-detection
191         # logic.
192         append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
193       endif()
194     endif()
195   endif()
197   # Generate the COMPILER_RT_SUPPORTED_ARCH list.
198   if(ANDROID)
199     # Examine compiler output to determine target architecture.
200     detect_target_arch()
201     set(COMPILER_RT_OS_SUFFIX "-android")
202   elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
203     if(COMPILER_RT_DEFAULT_TARGET_ONLY)
204       add_default_target_arch(${COMPILER_RT_DEFAULT_TARGET_ARCH})
205     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
206       if(NOT MSVC)
207         test_target_arch(x86_64 "" "-m64")
208         test_target_arch(i386 __i386__ "-m32")
209       else()
210         if (CMAKE_SIZEOF_VOID_P EQUAL 4)
211           test_target_arch(i386 "" "")
212         else()
213           test_target_arch(x86_64 "" "")
214         endif()
215       endif()
216     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "loongarch64")
217       test_target_arch(loongarch64 "" "")
218     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc64le|ppc64le")
219       test_target_arch(powerpc64le "" "-m64")
220     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
221       test_target_arch(powerpc "" "-m32")
222       test_target_arch(powerpc64 "" "-m64")
223     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
224       test_target_arch(s390x "" "")
225     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "sparc")
226       test_target_arch(sparc "" "-m32")
227       test_target_arch(sparcv9 "" "-m64")
228     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
229       CHECK_SYMBOL_EXISTS (_MIPS_ARCH_MIPS32R6 "" COMPILER_RT_MIPS32R6)
230       CHECK_SYMBOL_EXISTS (_MIPS_ARCH_MIPS64R6 "" COMPILER_RT_MIPS64R6)
231       CHECK_SYMBOL_EXISTS (__mips64 "" COMPILER_RT_MIPS_64)
232       CHECK_SYMBOL_EXISTS (__MIPSEL__ "" COMPILER_RT_MIPS_EL)
233       if ("${COMPILER_RT_MIPS_64}")
234         set(COMPILER_RT_DEFAULT_TARGET_ARCH "mips64")
235       else()
236         set(COMPILER_RT_DEFAULT_TARGET_ARCH "mips")
237       endif()
238       if ("${COMPILER_RT_MIPS_EL}")
239         set(COMPILER_RT_DEFAULT_TARGET_ARCH "${COMPILER_RT_DEFAULT_TARGET_ARCH}el")
240       endif()
242       # FIXME: Ideally, we would build the N32 library too.
243       if("${COMPILER_RT_MIPS_EL}" AND ("${COMPILER_RT_MIPS32R6}" OR "${COMPILER_RT_MIPS64R6}"))
244         test_target_arch(mipsel "" "-mips32r6" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
245         test_target_arch(mips64el "" "-mips64r6" "-mabi=64")
246       elseif("${COMPILER_RT_MIPS_EL}")
247         test_target_arch(mipsel "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
248         test_target_arch(mips64el "" "-mips64r2" "-mabi=64")
249       elseif("${COMPILER_RT_MIPS32R6}" OR "${COMPILER_RT_MIPS64R6}")
250         test_target_arch(mips "" "-mips32r6" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
251         test_target_arch(mips64 "" "-mips64r6" "-mabi=64")
252       else()
253         test_target_arch(mips "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE=1" "-D_FILE_OFFSET_BITS=64")
254         test_target_arch(mips64 "" "-mips64r2" "-mabi=64")
255       endif()
256     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
257       if(WIN32)
258         test_target_arch(arm "" "" "")
259       else()
260         test_target_arch(armv4t "" "-march=armv4t" "-mfloat-abi=soft")
261         test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft")
262         test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
263         test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
264       endif()
265     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "avr")
266       test_target_arch(avr "__AVR__" "--target=avr")
267     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
268       test_target_arch(aarch32 "" "-march=armv8-a")
269     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
270       test_target_arch(aarch64 "" "-march=armv8-a")
271     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv32")
272       test_target_arch(riscv32 "" "")
273     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv64")
274       test_target_arch(riscv64 "" "")
275     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
276       test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
277     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
278       test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
279     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "ve")
280       test_target_arch(ve "__ve__" "--target=ve-unknown-none")
281     endif()
282     set(COMPILER_RT_OS_SUFFIX "")
283   endif()
284 endmacro()