blob: 2a44d830bd81564ce258c21b8cde8551af7f8e65 [file] [log] [blame]
Chris Bienemanc49e5e32016-05-09 21:45:52 +00001# 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(CheckIncludeFile)
George Karpenkov10ab2ac2017-08-21 23:25:50 +00007include(CheckCXXSourceCompiles)
8
Chris Bienemanc49e5e32016-05-09 21:45:52 +00009check_include_file(unwind.h HAVE_UNWIND_H)
10
11# Top level target used to build all compiler-rt libraries.
12add_custom_target(compiler-rt ALL)
Chris Bieneman83877ee2016-08-19 22:17:46 +000013add_custom_target(install-compiler-rt)
Shoaib Meenai7f963b42017-12-01 19:06:29 +000014add_custom_target(install-compiler-rt-stripped)
Dan Liewbb78eef2018-06-27 12:56:34 +000015set_property(
16 TARGET
17 compiler-rt
18 install-compiler-rt
19 install-compiler-rt-stripped
20 PROPERTY
21 FOLDER "Compiler-RT Misc"
22)
Chris Bienemanc49e5e32016-05-09 21:45:52 +000023
Chris Bienemanc0d89822016-06-03 23:15:04 +000024# Setting these variables from an LLVM build is sufficient that compiler-rt can
25# construct the output paths, so it can behave as if it were in-tree here.
26if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
27 set(LLVM_TREE_AVAILABLE On)
28endif()
29
30if (LLVM_TREE_AVAILABLE)
Chris Bienemanc49e5e32016-05-09 21:45:52 +000031 # Compute the Clang version from the LLVM version.
32 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
33 # in Clang cmake files, instead of copying the rules here.
34 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
35 ${PACKAGE_VERSION})
36 # Setup the paths where compiler-rt runtimes and headers should be stored.
37 set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
38 set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
39 set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
40 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
41 ${LLVM_INCLUDE_TESTS})
42 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
43 ${LLVM_ENABLE_WERROR})
44 # Use just-built Clang to compile/link tests on all platforms, except for
45 # Windows where we need to use clang-cl instead.
46 if(NOT MSVC)
47 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
48 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
49 else()
50 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe)
51 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe)
52 endif()
53else()
54 # Take output dir and install path from the user.
55 set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
56 "Path where built compiler-rt libraries should be stored.")
57 set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
58 "Path where built compiler-rt executables should be stored.")
59 set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH
60 "Path where built compiler-rt libraries should be installed.")
61 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
62 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
63 # Use a host compiler to compile/link tests.
64 set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
65 set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
66endif()
67
68if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
69 set(COMPILER_RT_TEST_COMPILER_ID Clang)
70elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
71 set(COMPILER_RT_TEST_COMPILER_ID Clang)
72else()
73 set(COMPILER_RT_TEST_COMPILER_ID GNU)
74endif()
75
Jonathan Roelofs3c8f9532017-05-24 22:41:49 +000076if(NOT DEFINED COMPILER_RT_OS_DIR)
77 string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
78endif()
Petr Hosek887f26d2018-06-28 03:11:52 +000079if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
80 set(COMPILER_RT_LIBRARY_OUTPUT_DIR
81 ${COMPILER_RT_OUTPUT_DIR})
82 set(COMPILER_RT_LIBRARY_INSTALL_DIR
83 ${COMPILER_RT_INSTALL_PATH})
84else(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
85 set(COMPILER_RT_LIBRARY_OUTPUT_DIR
86 ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
87 set(COMPILER_RT_LIBRARY_INSTALL_DIR
88 ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
89endif()
Chris Bienemanc49e5e32016-05-09 21:45:52 +000090
91if(APPLE)
92 # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not
93 # the command line tools. If this is the case, we need to find the OS X
94 # sysroot to pass to clang.
95 if(NOT EXISTS /usr/include)
96 execute_process(COMMAND xcodebuild -version -sdk macosx Path
97 OUTPUT_VARIABLE OSX_SYSROOT
98 ERROR_QUIET
99 OUTPUT_STRIP_TRAILING_WHITESPACE)
100 set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
101 endif()
102
Anna Zakscacfb552016-10-05 20:45:36 +0000103 option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" On)
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000104 option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
105 option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
George Karpenkov10ab2ac2017-08-21 23:25:50 +0000106
Petr Hosekf332d192016-12-12 23:14:02 +0000107else()
108 option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off)
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000109endif()
110
Francis Riccie7729c82016-09-07 20:32:48 +0000111if(WIN32 AND NOT MINGW AND NOT CYGWIN)
112 set(CMAKE_SHARED_LIBRARY_PREFIX_C "")
113 set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "")
114 set(CMAKE_STATIC_LIBRARY_PREFIX_C "")
115 set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "")
116 set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
117 set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib")
118endif()
119
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000120macro(test_targets)
121 # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
122 # what version of MSVC to pretend to be so that the STL works.
123 set(MSVC_VERSION_FLAG "")
124 if (MSVC)
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000125 execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
126 OUTPUT_QUIET
127 ERROR_VARIABLE MSVC_COMPAT_VERSION
128 )
129 string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
130 MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
131 if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
132 set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
133 # Add this flag into the host build if this is clang-cl.
134 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
135 append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Reid Klecknerd0680ad2016-06-17 17:48:52 +0000136 elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
Etienne Bergeronb2f17d12016-06-21 14:32:52 +0000137 # Add this flag to test compiles to suppress clang's auto-detection
138 # logic.
139 append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000140 endif()
141 endif()
142 endif()
143
144 # Generate the COMPILER_RT_SUPPORTED_ARCH list.
145 if(ANDROID)
146 # Examine compiler output to determine target architecture.
147 detect_target_arch()
Chris Bieneman42e22dc2016-06-28 16:30:23 +0000148 set(COMPILER_RT_OS_SUFFIX "-android")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000149 elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
Petr Hosekf332d192016-12-12 23:14:02 +0000150 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
151 add_default_target_arch(${COMPILER_RT_DEFAULT_TARGET_ARCH})
152 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000153 if(NOT MSVC)
Kamil Rytarowski0d58e0f2018-03-03 11:48:54 +0000154 if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
155 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
156 test_target_arch(i386 __i386__ "-m32")
157 else()
158 test_target_arch(x86_64 "" "-m64")
159 endif()
160 else()
161 test_target_arch(x86_64 "" "-m64")
162 test_target_arch(i386 __i386__ "-m32")
163 endif()
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000164 else()
165 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
Etienne Bergeronb2f17d12016-06-21 14:32:52 +0000166 test_target_arch(i386 "" "")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000167 else()
Etienne Bergeronb2f17d12016-06-21 14:32:52 +0000168 test_target_arch(x86_64 "" "")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000169 endif()
170 endif()
171 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
Jonas Hahnfeldddbb0922017-09-29 13:32:39 +0000172 # Strip out -nodefaultlibs when calling TEST_BIG_ENDIAN. Configuration
173 # will fail with this option when building with a sanitizer.
174 cmake_push_check_state()
Alex Shlyapnikov54ea3942017-09-29 16:02:39 +0000175 string(REPLACE "-nodefaultlibs" "" CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000176 TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
Jonas Hahnfeldddbb0922017-09-29 13:32:39 +0000177 cmake_pop_check_state()
178
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000179 if(HOST_IS_BIG_ENDIAN)
180 test_target_arch(powerpc64 "" "-m64")
181 else()
182 test_target_arch(powerpc64le "" "-m64")
183 endif()
184 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
185 test_target_arch(s390x "" "")
186 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el")
187 # Gcc doesn't accept -m32/-m64 so we do the next best thing and use
188 # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match
189 # clang's default CPU's. In the 64-bit case, we must also specify the ABI
190 # since the default ABI differs between gcc and clang.
191 # FIXME: Ideally, we would build the N32 library too.
Simon Atanasyanab4f15e2018-09-27 07:17:00 +0000192 test_target_arch(mipsel "" "-mips32r2" "-mabi=32")
193 test_target_arch(mips64el "" "-mips64r2" "-mabi=64")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000194 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
Simon Atanasyanab4f15e2018-09-27 07:17:00 +0000195 test_target_arch(mips "" "-mips32r2" "-mabi=32")
196 test_target_arch(mips64 "" "-mips64r2" "-mabi=64")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000197 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
Saleem Abdulrasool1313ae32016-08-05 16:53:05 +0000198 if(WIN32)
199 test_target_arch(arm "" "" "")
200 else()
201 test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
202 test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
Weiming Zhao93b558a2017-01-19 18:46:11 +0000203 test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft")
Saleem Abdulrasool1313ae32016-08-05 16:53:05 +0000204 endif()
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000205 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
206 test_target_arch(aarch32 "" "-march=armv8-a")
207 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
208 test_target_arch(aarch64 "" "-march=armv8-a")
Shiva Chen77f19a32018-03-01 07:47:27 +0000209 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv32")
210 test_target_arch(riscv32 "" "")
211 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv64")
212 test_target_arch(riscv64 "" "")
Chris Bienemanc49e5e32016-05-09 21:45:52 +0000213 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
214 test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
215 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
216 test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
217 endif()
218 set(COMPILER_RT_OS_SUFFIX "")
219 endif()
220endmacro()