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) |
Etienne Bergeron | a088b36 | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 14 | set_target_properties(compiler-rt PROPERTIES FOLDER "Compiler-RT Misc") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 15 | |
Chris Bieneman | b6fb383 | 2016-06-03 23:15:04 +0000 | [diff] [blame] | 16 | # Setting these variables from an LLVM build is sufficient that compiler-rt can |
| 17 | # construct the output paths, so it can behave as if it were in-tree here. |
| 18 | if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION) |
| 19 | set(LLVM_TREE_AVAILABLE On) |
| 20 | endif() |
| 21 | |
| 22 | if (LLVM_TREE_AVAILABLE) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 23 | # Compute the Clang version from the LLVM version. |
| 24 | # FIXME: We should be able to reuse CLANG_VERSION variable calculated |
| 25 | # in Clang cmake files, instead of copying the rules here. |
| 26 | string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION |
| 27 | ${PACKAGE_VERSION}) |
| 28 | # Setup the paths where compiler-rt runtimes and headers should be stored. |
| 29 | set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION}) |
| 30 | set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR}) |
| 31 | set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}) |
| 32 | option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." |
| 33 | ${LLVM_INCLUDE_TESTS}) |
| 34 | option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" |
| 35 | ${LLVM_ENABLE_WERROR}) |
| 36 | # Use just-built Clang to compile/link tests on all platforms, except for |
| 37 | # Windows where we need to use clang-cl instead. |
| 38 | if(NOT MSVC) |
| 39 | set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang) |
| 40 | set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++) |
| 41 | else() |
| 42 | set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe) |
| 43 | set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe) |
| 44 | endif() |
| 45 | else() |
| 46 | # Take output dir and install path from the user. |
| 47 | set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH |
| 48 | "Path where built compiler-rt libraries should be stored.") |
| 49 | set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH |
| 50 | "Path where built compiler-rt executables should be stored.") |
| 51 | set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH |
| 52 | "Path where built compiler-rt libraries should be installed.") |
| 53 | option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF) |
| 54 | option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF) |
| 55 | # Use a host compiler to compile/link tests. |
| 56 | set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing") |
| 57 | set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing") |
| 58 | endif() |
| 59 | |
| 60 | if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$") |
| 61 | set(COMPILER_RT_TEST_COMPILER_ID Clang) |
| 62 | elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$") |
| 63 | set(COMPILER_RT_TEST_COMPILER_ID Clang) |
| 64 | else() |
| 65 | set(COMPILER_RT_TEST_COMPILER_ID GNU) |
| 66 | endif() |
| 67 | |
Jonathan Roelofs | 0ee4ae2 | 2017-05-24 22:41:49 +0000 | [diff] [blame] | 68 | if(NOT DEFINED COMPILER_RT_OS_DIR) |
| 69 | string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR) |
| 70 | endif() |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 71 | set(COMPILER_RT_LIBRARY_OUTPUT_DIR |
| 72 | ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR}) |
| 73 | set(COMPILER_RT_LIBRARY_INSTALL_DIR |
| 74 | ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR}) |
| 75 | |
| 76 | if(APPLE) |
| 77 | # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not |
| 78 | # the command line tools. If this is the case, we need to find the OS X |
| 79 | # sysroot to pass to clang. |
| 80 | if(NOT EXISTS /usr/include) |
| 81 | execute_process(COMMAND xcodebuild -version -sdk macosx Path |
| 82 | OUTPUT_VARIABLE OSX_SYSROOT |
| 83 | ERROR_QUIET |
| 84 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 85 | set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}") |
| 86 | endif() |
| 87 | |
Anna Zaks | 06644fb | 2016-10-05 20:45:36 +0000 | [diff] [blame] | 88 | option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" On) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 89 | option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off) |
| 90 | option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off) |
George Karpenkov | 0c8339c | 2017-08-21 23:25:50 +0000 | [diff] [blame^] | 91 | |
| 92 | CHECK_CXX_SOURCE_COMPILES(" |
| 93 | static thread_local int blah; |
| 94 | int main() { |
| 95 | return 0; |
| 96 | } |
| 97 | " HAS_THREAD_LOCAL) |
| 98 | |
| 99 | if( NOT HAS_THREAD_LOCAL ) |
| 100 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dthread_local=__thread") |
| 101 | endif() |
Petr Hosek | 330f9ef | 2016-12-12 23:14:02 +0000 | [diff] [blame] | 102 | else() |
| 103 | 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] | 104 | endif() |
| 105 | |
Francis Ricci | 4a0c5cd | 2016-09-07 20:32:48 +0000 | [diff] [blame] | 106 | if(WIN32 AND NOT MINGW AND NOT CYGWIN) |
| 107 | set(CMAKE_SHARED_LIBRARY_PREFIX_C "") |
| 108 | set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "") |
| 109 | set(CMAKE_STATIC_LIBRARY_PREFIX_C "") |
| 110 | set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "") |
| 111 | set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib") |
| 112 | set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib") |
| 113 | endif() |
| 114 | |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 115 | macro(test_targets) |
| 116 | # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl |
| 117 | # what version of MSVC to pretend to be so that the STL works. |
| 118 | set(MSVC_VERSION_FLAG "") |
| 119 | if (MSVC) |
| 120 | # Find and run MSVC (not clang-cl) and get its version. This will tell |
| 121 | # clang-cl what version of MSVC to pretend to be so that the STL works. |
| 122 | execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe" |
| 123 | OUTPUT_QUIET |
| 124 | ERROR_VARIABLE MSVC_COMPAT_VERSION |
| 125 | ) |
| 126 | string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1" |
| 127 | MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}") |
| 128 | if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$") |
| 129 | set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}") |
| 130 | # Add this flag into the host build if this is clang-cl. |
| 131 | if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 132 | append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
Reid Kleckner | d80ed42 | 2016-06-17 17:48:52 +0000 | [diff] [blame] | 133 | elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang") |
Etienne Bergeron | 7342946 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 134 | # Add this flag to test compiles to suppress clang's auto-detection |
| 135 | # logic. |
| 136 | append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS) |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 137 | endif() |
| 138 | endif() |
| 139 | endif() |
| 140 | |
| 141 | # Generate the COMPILER_RT_SUPPORTED_ARCH list. |
| 142 | if(ANDROID) |
| 143 | # Examine compiler output to determine target architecture. |
| 144 | detect_target_arch() |
Chris Bieneman | ce10b55 | 2016-06-28 16:30:23 +0000 | [diff] [blame] | 145 | set(COMPILER_RT_OS_SUFFIX "-android") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 146 | elseif(NOT APPLE) # Supported archs for Apple platforms are generated later |
Petr Hosek | 330f9ef | 2016-12-12 23:14:02 +0000 | [diff] [blame] | 147 | if(COMPILER_RT_DEFAULT_TARGET_ONLY) |
| 148 | add_default_target_arch(${COMPILER_RT_DEFAULT_TARGET_ARCH}) |
| 149 | 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] | 150 | if(NOT MSVC) |
| 151 | test_target_arch(x86_64 "" "-m64") |
| 152 | # FIXME: We build runtimes for both i686 and i386, as "clang -m32" may |
| 153 | # target different variant than "$CMAKE_C_COMPILER -m32". This part should |
| 154 | # be gone after we resolve PR14109. |
| 155 | test_target_arch(i686 __i686__ "-m32") |
| 156 | test_target_arch(i386 __i386__ "-m32") |
| 157 | else() |
| 158 | if (CMAKE_SIZEOF_VOID_P EQUAL 4) |
Etienne Bergeron | 7342946 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 159 | test_target_arch(i386 "" "") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 160 | else() |
Etienne Bergeron | 7342946 | 2016-06-21 14:32:52 +0000 | [diff] [blame] | 161 | test_target_arch(x86_64 "" "") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 162 | endif() |
| 163 | endif() |
| 164 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc") |
| 165 | TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN) |
| 166 | if(HOST_IS_BIG_ENDIAN) |
| 167 | test_target_arch(powerpc64 "" "-m64") |
| 168 | else() |
| 169 | test_target_arch(powerpc64le "" "-m64") |
| 170 | endif() |
| 171 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x") |
| 172 | test_target_arch(s390x "" "") |
| 173 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el") |
| 174 | # Gcc doesn't accept -m32/-m64 so we do the next best thing and use |
| 175 | # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match |
| 176 | # clang's default CPU's. In the 64-bit case, we must also specify the ABI |
| 177 | # since the default ABI differs between gcc and clang. |
| 178 | # FIXME: Ideally, we would build the N32 library too. |
| 179 | test_target_arch(mipsel "" "-mips32r2" "--target=mipsel-linux-gnu") |
Mohit K. Bhakkad | f9fc3de | 2016-07-18 09:23:23 +0000 | [diff] [blame] | 180 | test_target_arch(mips64el "" "-mips64r2" "--target=mips64el-linux-gnu" "-mabi=64") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 181 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips") |
| 182 | test_target_arch(mips "" "-mips32r2" "--target=mips-linux-gnu") |
Mohit K. Bhakkad | f9fc3de | 2016-07-18 09:23:23 +0000 | [diff] [blame] | 183 | test_target_arch(mips64 "" "-mips64r2" "--target=mips64-linux-gnu" "-mabi=64") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 184 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm") |
Saleem Abdulrasool | bcbb9e6 | 2016-08-05 16:53:05 +0000 | [diff] [blame] | 185 | if(WIN32) |
| 186 | test_target_arch(arm "" "" "") |
| 187 | else() |
| 188 | test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft") |
| 189 | test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard") |
Weiming Zhao | e1c28cd | 2017-01-19 18:46:11 +0000 | [diff] [blame] | 190 | test_target_arch(armv6m "" "-march=armv6m" "-mfloat-abi=soft") |
Saleem Abdulrasool | bcbb9e6 | 2016-08-05 16:53:05 +0000 | [diff] [blame] | 191 | endif() |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 192 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32") |
| 193 | test_target_arch(aarch32 "" "-march=armv8-a") |
| 194 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64") |
| 195 | test_target_arch(aarch64 "" "-march=armv8-a") |
Chris Bieneman | 18929dd | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 196 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32") |
| 197 | test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown") |
| 198 | elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64") |
| 199 | test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown") |
| 200 | endif() |
| 201 | set(COMPILER_RT_OS_SUFFIX "") |
| 202 | endif() |
| 203 | endmacro() |