Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 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) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 7 | include(CheckCXXSourceCompiles) |
| 8 | |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 9 | check_include_file(unwind.h HAVE_UNWIND_H) |
| 10 | |
| 11 | # Top level target used to build all compiler-rt libraries. |
| 12 | add_custom_target(compiler-rt ALL) |
Chris Bieneman | 83877ee | 2016-08-19 22:17:46 +0000 | [diff] [blame] | 13 | add_custom_target(install-compiler-rt) |
Shoaib Meenai | 7f963b4 | 2017-12-01 19:06:29 +0000 | [diff] [blame] | 14 | add_custom_target(install-compiler-rt-stripped) |
Dan Liew | bb78eef | 2018-06-27 12:56:34 +0000 | [diff] [blame] | 15 | set_property( |
| 16 | TARGET |
| 17 | compiler-rt |
| 18 | install-compiler-rt |
| 19 | install-compiler-rt-stripped |
| 20 | PROPERTY |
| 21 | FOLDER "Compiler-RT Misc" |
| 22 | ) |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 23 | |
Chris Bieneman | c0d8982 | 2016-06-03 23:15:04 +0000 | [diff] [blame] | 24 | # 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. |
| 26 | if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION) |
| 27 | set(LLVM_TREE_AVAILABLE On) |
| 28 | endif() |
| 29 | |
| 30 | if (LLVM_TREE_AVAILABLE) |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 31 | # 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() |
| 53 | else() |
| 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") |
| 66 | endif() |
| 67 | |
| 68 | if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$") |
| 69 | set(COMPILER_RT_TEST_COMPILER_ID Clang) |
| 70 | elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$") |
| 71 | set(COMPILER_RT_TEST_COMPILER_ID Clang) |
| 72 | else() |
| 73 | set(COMPILER_RT_TEST_COMPILER_ID GNU) |
| 74 | endif() |
| 75 | |
Jonathan Roelofs | 3c8f953 | 2017-05-24 22:41:49 +0000 | [diff] [blame] | 76 | if(NOT DEFINED COMPILER_RT_OS_DIR) |
| 77 | string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR) |
| 78 | endif() |
Petr Hosek | 887f26d | 2018-06-28 03:11:52 +0000 | [diff] [blame] | 79 | if(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}) |
| 84 | else(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}) |
| 89 | endif() |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 90 | |
| 91 | if(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 Zaks | cacfb55 | 2016-10-05 20:45:36 +0000 | [diff] [blame] | 103 | option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" On) |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 104 | option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off) |
| 105 | option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 106 | |
Petr Hosek | f332d19 | 2016-12-12 23:14:02 +0000 | [diff] [blame] | 107 | else() |
| 108 | option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off) |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 109 | endif() |
| 110 | |
Francis Ricci | e7729c8 | 2016-09-07 20:32:48 +0000 | [diff] [blame] | 111 | if(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") |
| 118 | endif() |
| 119 | |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 120 | macro(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 Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 125 | 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 Kleckner | d0680ad | 2016-06-17 17:48:52 +0000 | [diff] [blame] | 136 | elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang") |
Etienne Bergeron | b2f17d1 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 137 | # 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 Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 140 | 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 Bieneman | 42e22dc | 2016-06-28 16:30:23 +0000 | [diff] [blame] | 148 | set(COMPILER_RT_OS_SUFFIX "-android") |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 149 | elseif(NOT APPLE) # Supported archs for Apple platforms are generated later |
Petr Hosek | f332d19 | 2016-12-12 23:14:02 +0000 | [diff] [blame] | 150 | 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 Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 153 | if(NOT MSVC) |
Kamil Rytarowski | 0d58e0f | 2018-03-03 11:48:54 +0000 | [diff] [blame] | 154 | 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 Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 164 | else() |
| 165 | if (CMAKE_SIZEOF_VOID_P EQUAL 4) |
Etienne Bergeron | b2f17d1 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 166 | test_target_arch(i386 "" "") |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 167 | else() |
Etienne Bergeron | b2f17d1 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 168 | test_target_arch(x86_64 "" "") |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 169 | endif() |
| 170 | endif() |
| 171 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc") |
Jonas Hahnfeld | ddbb092 | 2017-09-29 13:32:39 +0000 | [diff] [blame] | 172 | # 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 Shlyapnikov | 54ea394 | 2017-09-29 16:02:39 +0000 | [diff] [blame] | 175 | string(REPLACE "-nodefaultlibs" "" CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 176 | TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN) |
Jonas Hahnfeld | ddbb092 | 2017-09-29 13:32:39 +0000 | [diff] [blame] | 177 | cmake_pop_check_state() |
| 178 | |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 179 | 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 Atanasyan | ab4f15e | 2018-09-27 07:17:00 +0000 | [diff] [blame] | 192 | test_target_arch(mipsel "" "-mips32r2" "-mabi=32") |
| 193 | test_target_arch(mips64el "" "-mips64r2" "-mabi=64") |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 194 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips") |
Simon Atanasyan | ab4f15e | 2018-09-27 07:17:00 +0000 | [diff] [blame] | 195 | test_target_arch(mips "" "-mips32r2" "-mabi=32") |
| 196 | test_target_arch(mips64 "" "-mips64r2" "-mabi=64") |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 197 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm") |
Saleem Abdulrasool | 1313ae3 | 2016-08-05 16:53:05 +0000 | [diff] [blame] | 198 | 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 Zhao | 93b558a | 2017-01-19 18:46:11 +0000 | [diff] [blame] | 203 | test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft") |
Saleem Abdulrasool | 1313ae3 | 2016-08-05 16:53:05 +0000 | [diff] [blame] | 204 | endif() |
Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 205 | 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 Chen | 77f19a3 | 2018-03-01 07:47:27 +0000 | [diff] [blame] | 209 | 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 Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 213 | 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() |
| 220 | endmacro() |