blob: c3e0db0431cbe8c60eece45d9a64c3af78955cf0 [file] [log] [blame]
Chris Bieneman18929dd2016-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 Karpenkov0c8339c2017-08-21 23:25:50 +00007include(CheckCXXSourceCompiles)
8
Chris Bieneman18929dd2016-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 Bienemana213e3e2016-08-19 22:17:46 +000013add_custom_target(install-compiler-rt)
Shoaib Meenaidb056522017-12-01 19:06:29 +000014add_custom_target(install-compiler-rt-stripped)
Etienne Bergerona088b362016-07-11 21:51:56 +000015set_target_properties(compiler-rt PROPERTIES FOLDER "Compiler-RT Misc")
Chris Bieneman18929dd2016-05-09 21:45:52 +000016
Chris Bienemanb6fb3832016-06-03 23:15:04 +000017# Setting these variables from an LLVM build is sufficient that compiler-rt can
18# construct the output paths, so it can behave as if it were in-tree here.
19if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
20 set(LLVM_TREE_AVAILABLE On)
21endif()
22
23if (LLVM_TREE_AVAILABLE)
Chris Bieneman18929dd2016-05-09 21:45:52 +000024 # Compute the Clang version from the LLVM version.
25 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
26 # in Clang cmake files, instead of copying the rules here.
27 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
28 ${PACKAGE_VERSION})
29 # Setup the paths where compiler-rt runtimes and headers should be stored.
30 set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
31 set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
32 set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
33 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
34 ${LLVM_INCLUDE_TESTS})
35 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
36 ${LLVM_ENABLE_WERROR})
37 # Use just-built Clang to compile/link tests on all platforms, except for
38 # Windows where we need to use clang-cl instead.
39 if(NOT MSVC)
40 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
41 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
42 else()
43 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe)
44 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe)
45 endif()
46else()
47 # Take output dir and install path from the user.
48 set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
49 "Path where built compiler-rt libraries should be stored.")
50 set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
51 "Path where built compiler-rt executables should be stored.")
52 set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH
53 "Path where built compiler-rt libraries should be installed.")
54 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
55 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
56 # Use a host compiler to compile/link tests.
57 set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
58 set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
59endif()
60
61if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
62 set(COMPILER_RT_TEST_COMPILER_ID Clang)
63elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
64 set(COMPILER_RT_TEST_COMPILER_ID Clang)
65else()
66 set(COMPILER_RT_TEST_COMPILER_ID GNU)
67endif()
68
Jonathan Roelofs0ee4ae22017-05-24 22:41:49 +000069if(NOT DEFINED COMPILER_RT_OS_DIR)
70 string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
71endif()
Chris Bieneman18929dd2016-05-09 21:45:52 +000072set(COMPILER_RT_LIBRARY_OUTPUT_DIR
73 ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
74set(COMPILER_RT_LIBRARY_INSTALL_DIR
75 ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
76
77if(APPLE)
78 # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not
79 # the command line tools. If this is the case, we need to find the OS X
80 # sysroot to pass to clang.
81 if(NOT EXISTS /usr/include)
82 execute_process(COMMAND xcodebuild -version -sdk macosx Path
83 OUTPUT_VARIABLE OSX_SYSROOT
84 ERROR_QUIET
85 OUTPUT_STRIP_TRAILING_WHITESPACE)
86 set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
87 endif()
88
Anna Zaks06644fb2016-10-05 20:45:36 +000089 option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" On)
Chris Bieneman18929dd2016-05-09 21:45:52 +000090 option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
91 option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
George Karpenkov0c8339c2017-08-21 23:25:50 +000092
Petr Hosek330f9ef2016-12-12 23:14:02 +000093else()
94 option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off)
Chris Bieneman18929dd2016-05-09 21:45:52 +000095endif()
96
Francis Ricci4a0c5cd2016-09-07 20:32:48 +000097if(WIN32 AND NOT MINGW AND NOT CYGWIN)
98 set(CMAKE_SHARED_LIBRARY_PREFIX_C "")
99 set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "")
100 set(CMAKE_STATIC_LIBRARY_PREFIX_C "")
101 set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "")
102 set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
103 set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib")
104endif()
105
Chris Bieneman18929dd2016-05-09 21:45:52 +0000106macro(test_targets)
107 # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
108 # what version of MSVC to pretend to be so that the STL works.
109 set(MSVC_VERSION_FLAG "")
110 if (MSVC)
111 # Find and run MSVC (not clang-cl) and get its version. This will tell
112 # clang-cl what version of MSVC to pretend to be so that the STL works.
113 execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
114 OUTPUT_QUIET
115 ERROR_VARIABLE MSVC_COMPAT_VERSION
116 )
117 string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
118 MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
119 if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
120 set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
121 # Add this flag into the host build if this is clang-cl.
122 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
123 append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Reid Klecknerd80ed422016-06-17 17:48:52 +0000124 elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
Etienne Bergeron73429462016-06-21 14:32:52 +0000125 # Add this flag to test compiles to suppress clang's auto-detection
126 # logic.
127 append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
Chris Bieneman18929dd2016-05-09 21:45:52 +0000128 endif()
129 endif()
130 endif()
131
132 # Generate the COMPILER_RT_SUPPORTED_ARCH list.
133 if(ANDROID)
134 # Examine compiler output to determine target architecture.
135 detect_target_arch()
Chris Bienemance10b552016-06-28 16:30:23 +0000136 set(COMPILER_RT_OS_SUFFIX "-android")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000137 elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
Petr Hosek330f9ef2016-12-12 23:14:02 +0000138 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
139 add_default_target_arch(${COMPILER_RT_DEFAULT_TARGET_ARCH})
140 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000141 if(NOT MSVC)
Kamil Rytarowskid7c67312018-03-03 11:48:54 +0000142 if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
143 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
144 test_target_arch(i386 __i386__ "-m32")
145 else()
146 test_target_arch(x86_64 "" "-m64")
147 endif()
148 else()
149 test_target_arch(x86_64 "" "-m64")
150 test_target_arch(i386 __i386__ "-m32")
151 endif()
Chris Bieneman18929dd2016-05-09 21:45:52 +0000152 else()
153 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
Etienne Bergeron73429462016-06-21 14:32:52 +0000154 test_target_arch(i386 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000155 else()
Etienne Bergeron73429462016-06-21 14:32:52 +0000156 test_target_arch(x86_64 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000157 endif()
158 endif()
159 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
Jonas Hahnfeld87e43ef2017-09-29 13:32:39 +0000160 # Strip out -nodefaultlibs when calling TEST_BIG_ENDIAN. Configuration
161 # will fail with this option when building with a sanitizer.
162 cmake_push_check_state()
Alex Shlyapnikov9d26c012017-09-29 16:02:39 +0000163 string(REPLACE "-nodefaultlibs" "" CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
Chris Bieneman18929dd2016-05-09 21:45:52 +0000164 TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
Jonas Hahnfeld87e43ef2017-09-29 13:32:39 +0000165 cmake_pop_check_state()
166
Chris Bieneman18929dd2016-05-09 21:45:52 +0000167 if(HOST_IS_BIG_ENDIAN)
168 test_target_arch(powerpc64 "" "-m64")
169 else()
170 test_target_arch(powerpc64le "" "-m64")
171 endif()
172 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
173 test_target_arch(s390x "" "")
174 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el")
175 # Gcc doesn't accept -m32/-m64 so we do the next best thing and use
176 # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match
177 # clang's default CPU's. In the 64-bit case, we must also specify the ABI
178 # since the default ABI differs between gcc and clang.
179 # FIXME: Ideally, we would build the N32 library too.
180 test_target_arch(mipsel "" "-mips32r2" "--target=mipsel-linux-gnu")
Mohit K. Bhakkadf9fc3de2016-07-18 09:23:23 +0000181 test_target_arch(mips64el "" "-mips64r2" "--target=mips64el-linux-gnu" "-mabi=64")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000182 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
183 test_target_arch(mips "" "-mips32r2" "--target=mips-linux-gnu")
Mohit K. Bhakkadf9fc3de2016-07-18 09:23:23 +0000184 test_target_arch(mips64 "" "-mips64r2" "--target=mips64-linux-gnu" "-mabi=64")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000185 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
Saleem Abdulrasoolbcbb9e62016-08-05 16:53:05 +0000186 if(WIN32)
187 test_target_arch(arm "" "" "")
188 else()
189 test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
190 test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
Weiming Zhaoe1c28cd2017-01-19 18:46:11 +0000191 test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft")
Saleem Abdulrasoolbcbb9e62016-08-05 16:53:05 +0000192 endif()
Chris Bieneman18929dd2016-05-09 21:45:52 +0000193 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
194 test_target_arch(aarch32 "" "-march=armv8-a")
195 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
196 test_target_arch(aarch64 "" "-march=armv8-a")
Shiva Chen9008dc92018-03-01 07:47:27 +0000197 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv32")
198 test_target_arch(riscv32 "" "")
199 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv64")
200 test_target_arch(riscv64 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000201 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
202 test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
203 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
204 test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
205 endif()
206 set(COMPILER_RT_OS_SUFFIX "")
207 endif()
208endmacro()