Chris Bieneman | 18929dd | 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 | 0c8339c | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 7 | include(CheckCXXSourceCompiles) |
| 8 | |
Chris Bieneman | 18929dd | 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 | a213e3e | 2016-08-19 22:17:46 +0000 | [diff] [blame] | 13 | add_custom_target(install-compiler-rt) |
Shoaib Meenai | db05652 | 2017-12-01 19:06:29 +0000 | [diff] [blame] | 14 | add_custom_target(install-compiler-rt-stripped) |
Etienne Bergeron | a088b36 | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 15 | set_target_properties(compiler-rt PROPERTIES FOLDER "Compiler-RT Misc") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 16 | |
Chris Bieneman | b6fb383 | 2016-06-03 23:15:04 +0000 | [diff] [blame] | 17 | # 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. |
| 19 | if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION) |
| 20 | set(LLVM_TREE_AVAILABLE On) |
| 21 | endif() |
| 22 | |
| 23 | if (LLVM_TREE_AVAILABLE) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 24 | # 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() |
| 46 | else() |
| 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") |
| 59 | endif() |
| 60 | |
| 61 | if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$") |
| 62 | set(COMPILER_RT_TEST_COMPILER_ID Clang) |
| 63 | elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$") |
| 64 | set(COMPILER_RT_TEST_COMPILER_ID Clang) |
| 65 | else() |
| 66 | set(COMPILER_RT_TEST_COMPILER_ID GNU) |
| 67 | endif() |
| 68 | |
Jonathan Roelofs | 0ee4ae2 | 2017-05-24 22:41:49 +0000 | [diff] [blame] | 69 | if(NOT DEFINED COMPILER_RT_OS_DIR) |
| 70 | string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR) |
| 71 | endif() |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 72 | set(COMPILER_RT_LIBRARY_OUTPUT_DIR |
| 73 | ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR}) |
| 74 | set(COMPILER_RT_LIBRARY_INSTALL_DIR |
| 75 | ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR}) |
| 76 | |
| 77 | if(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 Zaks | 06644fb | 2016-10-05 20:45:36 +0000 | [diff] [blame] | 89 | option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" On) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 90 | option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off) |
| 91 | option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off) |
George Karpenkov | 0c8339c | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 92 | |
Petr Hosek | 330f9ef | 2016-12-12 23:14:02 +0000 | [diff] [blame] | 93 | else() |
| 94 | option(COMPILER_RT_DEFAULT_TARGET_ONLY "Build builtins only for the default target" Off) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 95 | endif() |
| 96 | |
Francis Ricci | 4a0c5cd | 2016-09-07 20:32:48 +0000 | [diff] [blame] | 97 | if(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") |
| 104 | endif() |
| 105 | |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 106 | macro(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 Kleckner | d80ed42 | 2016-06-17 17:48:52 +0000 | [diff] [blame] | 124 | elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang") |
Etienne Bergeron | 7342946 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 125 | # 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 Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 128 | 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 Bieneman | ce10b55 | 2016-06-28 16:30:23 +0000 | [diff] [blame] | 136 | set(COMPILER_RT_OS_SUFFIX "-android") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 137 | elseif(NOT APPLE) # Supported archs for Apple platforms are generated later |
Petr Hosek | 330f9ef | 2016-12-12 23:14:02 +0000 | [diff] [blame] | 138 | 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 Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 141 | if(NOT MSVC) |
Kamil Rytarowski | d7c6731 | 2018-03-03 11:48:54 +0000 | [diff] [blame] | 142 | 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 Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 152 | else() |
| 153 | if (CMAKE_SIZEOF_VOID_P EQUAL 4) |
Etienne Bergeron | 7342946 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 154 | test_target_arch(i386 "" "") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 155 | else() |
Etienne Bergeron | 7342946 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 156 | test_target_arch(x86_64 "" "") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 157 | endif() |
| 158 | endif() |
| 159 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc") |
Jonas Hahnfeld | 87e43ef | 2017-09-29 13:32:39 +0000 | [diff] [blame] | 160 | # 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 Shlyapnikov | 9d26c01 | 2017-09-29 16:02:39 +0000 | [diff] [blame] | 163 | string(REPLACE "-nodefaultlibs" "" CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 164 | TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN) |
Jonas Hahnfeld | 87e43ef | 2017-09-29 13:32:39 +0000 | [diff] [blame] | 165 | cmake_pop_check_state() |
| 166 | |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 167 | 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. Bhakkad | f9fc3de | 2016-07-18 09:23:23 +0000 | [diff] [blame] | 181 | test_target_arch(mips64el "" "-mips64r2" "--target=mips64el-linux-gnu" "-mabi=64") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 182 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips") |
| 183 | test_target_arch(mips "" "-mips32r2" "--target=mips-linux-gnu") |
Mohit K. Bhakkad | f9fc3de | 2016-07-18 09:23:23 +0000 | [diff] [blame] | 184 | test_target_arch(mips64 "" "-mips64r2" "--target=mips64-linux-gnu" "-mabi=64") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 185 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm") |
Saleem Abdulrasool | bcbb9e6 | 2016-08-05 16:53:05 +0000 | [diff] [blame] | 186 | 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 Zhao | e1c28cd | 2017-01-19 18:46:11 +0000 | [diff] [blame] | 191 | test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft") |
Saleem Abdulrasool | bcbb9e6 | 2016-08-05 16:53:05 +0000 | [diff] [blame] | 192 | endif() |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 193 | 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 Chen | 9008dc9 | 2018-03-01 07:47:27 +0000 | [diff] [blame] | 197 | 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 Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 201 | 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() |
| 208 | endmacro() |