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