| Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 1 | # CMake build for CompilerRT. |
| 2 | # |
| Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 3 | # An important constraint of the build is that it only produces libraries |
| 4 | # based on the ability of the host toolchain to target various platforms. |
| 5 | |
| Pirama Arumuga Nainar | 31b825d | 2017-06-16 21:14:45 +0000 | [diff] [blame] | 6 | cmake_minimum_required(VERSION 3.4.3) |
| 7 | |
| Alexey Samsonov | de4ef2a | 2014-02-19 07:49:16 +0000 | [diff] [blame] | 8 | # Check if compiler-rt is built as a standalone project. |
| Jonas Hahnfeld | ddbab7d | 2016-08-19 06:46:00 +0000 | [diff] [blame] | 9 | if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD) |
| Douglas Katzman | c4ffd48 | 2015-07-28 16:52:42 +0000 | [diff] [blame] | 10 | project(CompilerRT C CXX ASM) |
| Alexey Samsonov | de4ef2a | 2014-02-19 07:49:16 +0000 | [diff] [blame] | 11 | set(COMPILER_RT_STANDALONE_BUILD TRUE) |
| Alexey Samsonov | de4ef2a | 2014-02-19 07:49:16 +0000 | [diff] [blame] | 12 | endif() |
| 13 | |
| Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 14 | # Add path for custom compiler-rt modules. |
| 15 | list(INSERT CMAKE_MODULE_PATH 0 |
| 16 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake" |
| 17 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
| 18 | ) |
| 19 | |
| George Karpenkov | 50dd3fe | 2017-07-28 00:50:56 +0000 | [diff] [blame] | 20 | if(CMAKE_CONFIGURATION_TYPES) |
| 21 | set(CMAKE_CFG_RESOLVED_INTDIR "${CMAKE_CFG_INTDIR}/") |
| 22 | else() |
| 23 | set(CMAKE_CFG_RESOLVED_INTDIR "") |
| 24 | endif() |
| 25 | |
| Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 26 | include(base-config-ix) |
| Jonas Hahnfeld | 9b2c3ab | 2016-08-02 05:51:05 +0000 | [diff] [blame] | 27 | include(CompilerRTUtils) |
| Alexey Samsonov | 20abdf6 | 2013-10-01 12:52:15 +0000 | [diff] [blame] | 28 | |
| Chris Bieneman | 679ab85 | 2015-09-14 19:59:24 +0000 | [diff] [blame] | 29 | option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON) |
| 30 | mark_as_advanced(COMPILER_RT_BUILD_BUILTINS) |
| 31 | option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON) |
| 32 | mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS) |
| Dean Michael Berris | 6af0a6b | 2016-08-08 03:58:57 +0000 | [diff] [blame] | 33 | option(COMPILER_RT_BUILD_XRAY "Build xray" ON) |
| Dean Michael Berris | 938c503 | 2016-07-21 07:39:55 +0000 | [diff] [blame] | 34 | mark_as_advanced(COMPILER_RT_BUILD_XRAY) |
| George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 35 | option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON) |
| 36 | mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER) |
| Michal Gorny | 806b8d5 | 2017-10-02 05:03:55 +0000 | [diff] [blame] | 37 | option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON) |
| 38 | mark_as_advanced(COMPILER_RT_BUILD_PROFILE) |
| Dean Michael Berris | 449fdf8 | 2017-08-03 00:58:45 +0000 | [diff] [blame] | 39 | option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF) |
| 40 | mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT) |
| Chris Bieneman | 679ab85 | 2015-09-14 19:59:24 +0000 | [diff] [blame] | 41 | |
| Walter Lee | 52b2bd7 | 2017-11-13 14:02:27 +0000 | [diff] [blame] | 42 | set(COMPILER_RT_ASAN_SHADOW_SCALE "" |
| 43 | CACHE STRING "Override the shadow scale to be used in ASan runtime") |
| 44 | |
| 45 | if (NOT COMPILER_RT_ASAN_SHADOW_SCALE STREQUAL "") |
| 46 | # Check that the shadow scale value is valid. |
| 47 | if (NOT (COMPILER_RT_ASAN_SHADOW_SCALE GREATER -1 AND |
| 48 | COMPILER_RT_ASAN_SHADOW_SCALE LESS 8)) |
| 49 | message(FATAL_ERROR " |
| 50 | Invalid ASan Shadow Scale '${COMPILER_RT_ASAN_SHADOW_SCALE}'.") |
| 51 | endif() |
| 52 | |
| 53 | set(COMPILER_RT_ASAN_SHADOW_SCALE_LLVM_FLAG |
| 54 | -mllvm -asan-mapping-scale=${COMPILER_RT_ASAN_SHADOW_SCALE}) |
| 55 | set(COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION |
| 56 | ASAN_SHADOW_SCALE=${COMPILER_RT_ASAN_SHADOW_SCALE}) |
| 57 | set(COMPILER_RT_ASAN_SHADOW_SCALE_FLAG |
| 58 | -D${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION}) |
| 59 | endif() |
| 60 | |
| Catherine Moore | 61efa17 | 2017-05-10 15:34:25 +0000 | [diff] [blame] | 61 | set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOLEAN |
| 62 | "Build for a bare-metal target.") |
| 63 | |
| Chris Bieneman | c49e5e3 | 2016-05-09 21:45:52 +0000 | [diff] [blame] | 64 | if (COMPILER_RT_STANDALONE_BUILD) |
| Jonas Hahnfeld | 9b2c3ab | 2016-08-02 05:51:05 +0000 | [diff] [blame] | 65 | load_llvm_config() |
| Alexey Samsonov | aa980c7 | 2014-02-19 10:04:29 +0000 | [diff] [blame] | 66 | |
| 67 | # Find Python interpreter. |
| 68 | set(Python_ADDITIONAL_VERSIONS 2.7 2.6 2.5) |
| 69 | include(FindPythonInterp) |
| 70 | if(NOT PYTHONINTERP_FOUND) |
| 71 | message(FATAL_ERROR " |
| 72 | Unable to find Python interpreter required testing. Please install Python |
| 73 | or specify the PYTHON_EXECUTABLE CMake variable.") |
| 74 | endif() |
| 75 | |
| 76 | # Define default arguments to lit. |
| 77 | set(LIT_ARGS_DEFAULT "-sv") |
| 78 | if (MSVC OR XCODE) |
| 79 | set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") |
| 80 | endif() |
| 81 | set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") |
| Zachary Turner | ce92db1 | 2017-09-15 22:10:46 +0000 | [diff] [blame] | 82 | set(LLVM_LIT_OUTPUT_DIR "${COMPILER_RT_EXEC_OUTPUT_DIR}") |
| Alexey Samsonov | de4ef2a | 2014-02-19 07:49:16 +0000 | [diff] [blame] | 83 | endif() |
| 84 | |
| Jonas Hahnfeld | 9b2c3ab | 2016-08-02 05:51:05 +0000 | [diff] [blame] | 85 | construct_compiler_rt_default_triple() |
| Francis Ricci | ba064be | 2017-09-19 17:54:11 +0000 | [diff] [blame] | 86 | if ("${COMPILER_RT_DEFAULT_TARGET_ABI}" MATCHES "hf$") |
| 87 | if (${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "^arm") |
| 88 | set(COMPILER_RT_DEFAULT_TARGET_ARCH "armhf") |
| 89 | endif() |
| 90 | endif() |
| Francis Ricci | d327254 | 2017-09-19 17:56:27 +0000 | [diff] [blame] | 91 | if ("${COMPILER_RT_DEFAULT_TARGET_ABI}" MATCHES "^android") |
| Chris Bieneman | 990ff38 | 2016-06-27 22:52:05 +0000 | [diff] [blame] | 92 | set(ANDROID 1) |
| 93 | endif() |
| Vitaly Buka | 0dc3ef3 | 2017-09-16 05:13:56 +0000 | [diff] [blame] | 94 | pythonize_bool(ANDROID) |
| Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 95 | |
| 96 | set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| Alexey Samsonov | 6a65b18 | 2013-06-06 12:35:48 +0000 | [diff] [blame] | 97 | set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
| Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 98 | |
| Timur Iskhodzhanov | b9bd76b | 2014-05-13 14:25:49 +0000 | [diff] [blame] | 99 | # We support running instrumented tests when we're not cross compiling |
| 100 | # and target a UNIX-like system or Windows. |
| 101 | # We can run tests on Android even when we are cross-compiling. |
| Reid Kleckner | fbfed86 | 2015-07-17 16:23:05 +0000 | [diff] [blame] | 102 | if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND (UNIX OR WIN32)) OR ANDROID |
| Greg Fitzgerald | 07c88a1 | 2014-05-14 00:36:15 +0000 | [diff] [blame] | 103 | OR COMPILER_RT_EMULATOR) |
| Michael Gottesman | 4ddc215 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 104 | option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON) |
| Alexey Samsonov | c20f5d2 | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 105 | else() |
| Michael Gottesman | 4ddc215 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 106 | option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF) |
| Alexey Samsonov | c20f5d2 | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 107 | endif() |
| Michael Gottesman | 4ddc215 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 108 | |
| Peter Collingbourne | cbdea32 | 2013-10-25 23:03:34 +0000 | [diff] [blame] | 109 | option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF) |
| Chris Bieneman | 69a372d | 2015-12-03 20:08:22 +0000 | [diff] [blame] | 110 | option(COMPILER_RT_EXTERNALIZE_DEBUGINFO |
| 111 | "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF) |
| Peter Collingbourne | cbdea32 | 2013-10-25 23:03:34 +0000 | [diff] [blame] | 112 | # COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in. |
| 113 | pythonize_bool(COMPILER_RT_DEBUG) |
| 114 | |
| Kuba Mracek | 5c2e3e2 | 2017-07-07 22:40:13 +0000 | [diff] [blame] | 115 | if(APPLE AND SANITIZER_MIN_OSX_VERSION AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9") |
| Francis Ricci | 9cf5e4f | 2016-08-22 18:31:37 +0000 | [diff] [blame] | 116 | # Mac OS X prior to 10.9 had problems with exporting symbols from |
| 117 | # libc++/libc++abi. |
| Peter Collingbourne | d13d61f | 2017-09-15 20:24:12 +0000 | [diff] [blame] | 118 | set(cxxabi_supported OFF) |
| Francis Ricci | 9cf5e4f | 2016-08-22 18:31:37 +0000 | [diff] [blame] | 119 | else() |
| Peter Collingbourne | d13d61f | 2017-09-15 20:24:12 +0000 | [diff] [blame] | 120 | set(cxxabi_supported ON) |
| Francis Ricci | 9cf5e4f | 2016-08-22 18:31:37 +0000 | [diff] [blame] | 121 | endif() |
| 122 | |
| Reid Kleckner | 4f9516d | 2018-05-18 18:44:37 +0000 | [diff] [blame] | 123 | option(SANITIZER_ALLOW_CXXABI "Allow use of C++ ABI details in ubsan" ON) |
| Peter Collingbourne | d13d61f | 2017-09-15 20:24:12 +0000 | [diff] [blame] | 124 | |
| 125 | set(SANITIZE_CAN_USE_CXXABI OFF) |
| 126 | if (cxxabi_supported AND SANITIZER_ALLOW_CXXABI) |
| 127 | set(SANITIZER_CAN_USE_CXXABI ON) |
| 128 | endif() |
| Francis Ricci | 9cf5e4f | 2016-08-22 18:31:37 +0000 | [diff] [blame] | 129 | pythonize_bool(SANITIZER_CAN_USE_CXXABI) |
| 130 | |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 131 | set(SANITIZER_CXX_ABI "default" CACHE STRING |
| 132 | "Specify C++ ABI library to use.") |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 133 | set(CXXABIS none default libstdc++ libc++) |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 134 | set_property(CACHE SANITIZER_CXX_ABI PROPERTY STRINGS ;${CXXABIS}) |
| 135 | |
| 136 | if (SANITIZER_CXX_ABI STREQUAL "default") |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 137 | if (CLANG_DEFAULT_CXX_STDLIB STREQUAL "libc++" AND (TARGET cxx OR HAVE_LIBCXX)) |
| 138 | set(SANITIZER_CXX_ABI_LIBNAME "libc++") |
| Reid Kleckner | 4f9516d | 2018-05-18 18:44:37 +0000 | [diff] [blame] | 139 | set(SANITIZER_CXX_ABI_INTREE 1) |
| 140 | elseif (APPLE) |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 141 | set(SANITIZER_CXX_ABI_LIBNAME "libc++") |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 142 | set(SANITIZER_CXX_ABI_SYSTEM 1) |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 143 | elseif (FUCHSIA) |
| 144 | set(SANITIZER_CXX_ABI_LIBNAME "libc++") |
| 145 | set(SANITIZER_CXX_ABI_INTREE 1) |
| Reid Kleckner | 4f9516d | 2018-05-18 18:44:37 +0000 | [diff] [blame] | 146 | else() |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 147 | set(SANITIZER_CXX_ABI_LIBNAME "libstdc++") |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 148 | set(SANITIZER_CXX_ABI_SYSTEM 1) |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 149 | endif() |
| Evgeniy Stepanov | b52b141 | 2017-08-14 20:42:43 +0000 | [diff] [blame] | 150 | else() |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 151 | set(SANITIZER_CXX_ABI_LIBNAME "${SANITIZER_CXX_ABI}") |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 152 | set(SANITIZER_CXX_ABI_SYSTEM 1) |
| Petr Hosek | a7a9ca4 | 2017-07-28 03:39:39 +0000 | [diff] [blame] | 153 | endif() |
| 154 | |
| Petr Hosek | aa6d13e | 2018-05-22 22:58:48 +0000 | [diff] [blame] | 155 | set(COMPILER_RT_RUNTIME "default" CACHE STRING |
| 156 | "Compiler runtime to use.") |
| Petr Hosek | 2bbb6ad | 2017-07-28 03:39:38 +0000 | [diff] [blame] | 157 | |
| Petr Hosek | aa6d13e | 2018-05-22 22:58:48 +0000 | [diff] [blame] | 158 | if (COMPILER_RT_RUNTIME STREQUAL "default") |
| 159 | if (CLANG_DEFAULT_RTLIB STREQUAL "compiler-rt") |
| 160 | set(COMPILER_RT_RUNTIME_LIBRARY "builtins") |
| 161 | elseif (FUCHSIA) |
| 162 | set(COMPILER_RT_RUNTIME_LIBRARY "builtins") |
| 163 | endif() |
| 164 | else() |
| 165 | set(COMPILER_RT_RUNTIME_LIBRARY "${COMPILER_RT_RUNTIME}") |
| 166 | endif() |
| 167 | |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 168 | include(config-ix) |
| 169 | |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 170 | #================================ |
| 171 | # Setup Compiler Flags |
| 172 | #================================ |
| Saleem Abdulrasool | a7452e4 | 2015-01-14 15:55:17 +0000 | [diff] [blame] | 173 | |
| Alexey Samsonov | fe7e28c | 2014-03-13 11:31:10 +0000 | [diff] [blame] | 174 | if(MSVC) |
| Reid Kleckner | d0680ad | 2016-06-17 17:48:52 +0000 | [diff] [blame] | 175 | # Override any existing /W flags with /W4. This is what LLVM does. Failing to |
| 176 | # remove other /W[0-4] flags will result in a warning about overriding a |
| 177 | # previous flag. |
| 178 | if (COMPILER_RT_HAS_W4_FLAG) |
| 179 | string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
| 180 | string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 181 | append_string_if(COMPILER_RT_HAS_W4_FLAG /W4 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
| 182 | endif() |
| Alexey Samsonov | fe7e28c | 2014-03-13 11:31:10 +0000 | [diff] [blame] | 183 | else() |
| 184 | append_string_if(COMPILER_RT_HAS_WALL_FLAG -Wall CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
| 185 | endif() |
| Alexey Samsonov | 2f27f0b | 2014-02-24 11:22:39 +0000 | [diff] [blame] | 186 | if(COMPILER_RT_ENABLE_WERROR) |
| Alexey Samsonov | fe7e28c | 2014-03-13 11:31:10 +0000 | [diff] [blame] | 187 | append_string_if(COMPILER_RT_HAS_WERROR_FLAG -Werror CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
| 188 | append_string_if(COMPILER_RT_HAS_WX_FLAG /WX CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
| Alexey Samsonov | 2f27f0b | 2014-02-24 11:22:39 +0000 | [diff] [blame] | 189 | endif() |
| 190 | |
| Alexey Samsonov | bcce197 | 2014-03-18 12:49:22 +0000 | [diff] [blame] | 191 | append_string_if(COMPILER_RT_HAS_STD_CXX11_FLAG -std=c++11 CMAKE_CXX_FLAGS) |
| 192 | |
| Reid Kleckner | 0140ce4 | 2014-02-26 21:54:39 +0000 | [diff] [blame] | 193 | # Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP. |
| Alexey Samsonov | e3e2a11 | 2014-02-27 06:52:41 +0000 | [diff] [blame] | 194 | if(NOT COMPILER_RT_HAS_FUNC_SYMBOL) |
| Alexey Samsonov | 8c4c097 | 2014-02-27 07:03:32 +0000 | [diff] [blame] | 195 | add_definitions(-D__func__=__FUNCTION__) |
| Reid Kleckner | 0140ce4 | 2014-02-26 21:54:39 +0000 | [diff] [blame] | 196 | endif() |
| 197 | |
| Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 198 | # Provide some common commmandline flags for Sanitizer runtimes. |
| Francis Ricci | 0b6f487 | 2016-09-08 15:57:22 +0000 | [diff] [blame] | 199 | if(NOT WIN32) |
| 200 | append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS) |
| 201 | endif() |
| Kuba Brecka | 14c0c59 | 2014-10-15 22:47:54 +0000 | [diff] [blame] | 202 | append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin SANITIZER_COMMON_CFLAGS) |
| 203 | append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SANITIZER_COMMON_CFLAGS) |
| Kuba Mracek | 90caf89 | 2017-03-27 17:14:48 +0000 | [diff] [blame] | 204 | if(NOT COMPILER_RT_DEBUG AND NOT APPLE) |
| Kuba Brecka | 83460cf | 2016-05-22 19:59:06 +0000 | [diff] [blame] | 205 | append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS) |
| 206 | endif() |
| Kuba Brecka | 14c0c59 | 2014-10-15 22:47:54 +0000 | [diff] [blame] | 207 | append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS) |
| 208 | append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS) |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 209 | append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS) |
| Kuba Brecka | 14c0c59 | 2014-10-15 22:47:54 +0000 | [diff] [blame] | 210 | append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS) |
| Weiming Zhao | e4ba19e | 2017-11-28 23:41:42 +0000 | [diff] [blame] | 211 | if(NOT COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG) |
| 212 | append_list_if(COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG -fvisibility-inlines-hidden SANITIZER_COMMON_CFLAGS) |
| 213 | endif() |
| Evgeniy Stepanov | 8c9a070 | 2015-01-15 16:31:22 +0000 | [diff] [blame] | 214 | append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS) |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 215 | |
| Kostya Kortchinsky | 2282ede | 2017-01-30 22:31:49 +0000 | [diff] [blame] | 216 | # The following is a workaround for powerpc64le. This is the only architecture |
| 217 | # that requires -fno-function-sections to work properly. If lacking, the ASan |
| 218 | # Linux test function-sections-are-bad.cc fails with the following error: |
| 219 | # 'undefined symbol: __sanitizer_unaligned_load32'. |
| 220 | if(DEFINED TARGET_powerpc64le_CFLAGS) |
| 221 | append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections TARGET_powerpc64le_CFLAGS) |
| 222 | endif() |
| 223 | |
| Alexey Samsonov | 6c282b4 | 2014-02-28 08:04:30 +0000 | [diff] [blame] | 224 | if(MSVC) |
| Peter Collingbourne | 702548d | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 225 | # Replace the /M[DT][d] flags with /MT, and strip any definitions of _DEBUG, |
| 226 | # which cause definition mismatches at link time. |
| Timur Iskhodzhanov | de1718d | 2014-08-12 09:44:56 +0000 | [diff] [blame] | 227 | # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214. |
| Alexey Samsonov | 6c282b4 | 2014-02-28 08:04:30 +0000 | [diff] [blame] | 228 | if(COMPILER_RT_HAS_MT_FLAG) |
| Aaron Ballman | 7c7be2f | 2014-10-23 20:24:00 +0000 | [diff] [blame] | 229 | foreach(flag_var |
| Andrey Turetskiy | bc0122b | 2016-02-20 12:56:04 +0000 | [diff] [blame] | 230 | CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE |
| 231 | CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO |
| Aaron Ballman | 7c7be2f | 2014-10-23 20:24:00 +0000 | [diff] [blame] | 232 | CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE |
| 233 | CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) |
| Peter Collingbourne | 702548d | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 234 | string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}") |
| 235 | string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") |
| 236 | string(REGEX REPLACE "/D_DEBUG" "" ${flag_var} "${${flag_var}}") |
| Aaron Ballman | 7c7be2f | 2014-10-23 20:24:00 +0000 | [diff] [blame] | 237 | endforeach() |
| Alexey Samsonov | 6c282b4 | 2014-02-28 08:04:30 +0000 | [diff] [blame] | 238 | endif() |
| Kuba Brecka | 14c0c59 | 2014-10-15 22:47:54 +0000 | [diff] [blame] | 239 | append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS) |
| 240 | append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS) |
| Reid Kleckner | 21aca48 | 2016-03-21 20:08:59 +0000 | [diff] [blame] | 241 | # VS 2015 (version 1900) added support for thread safe static initialization. |
| 242 | # However, ASan interceptors run before CRT initialization, which causes the |
| 243 | # new thread safe code to crash. Disable this feature for now. |
| Reid Kleckner | f6d5475 | 2016-07-21 20:03:37 +0000 | [diff] [blame] | 244 | if (MSVC_VERSION GREATER 1899 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") |
| Reid Kleckner | 21aca48 | 2016-03-21 20:08:59 +0000 | [diff] [blame] | 245 | list(APPEND SANITIZER_COMMON_CFLAGS /Zc:threadSafeInit-) |
| 246 | endif() |
| Alexey Samsonov | 6c282b4 | 2014-02-28 08:04:30 +0000 | [diff] [blame] | 247 | endif() |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 248 | |
| Alexey Samsonov | ea04032 | 2015-01-06 20:25:34 +0000 | [diff] [blame] | 249 | append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS) |
| Alexey Samsonov | df3aeb8 | 2015-01-03 04:29:12 +0000 | [diff] [blame] | 250 | |
| George Karpenkov | e89d0a8 | 2017-07-15 00:30:46 +0000 | [diff] [blame] | 251 | # If we're using MSVC, |
| Alexey Samsonov | cbc6852 | 2014-03-13 13:37:07 +0000 | [diff] [blame] | 252 | # always respect the optimization flags set by CMAKE_BUILD_TYPE instead. |
| George Karpenkov | e89d0a8 | 2017-07-15 00:30:46 +0000 | [diff] [blame] | 253 | if (NOT MSVC) |
| 254 | |
| Zachary Turner | ce92db1 | 2017-09-15 22:10:46 +0000 | [diff] [blame] | 255 | # Build with optimization, unless we're in debug mode. |
| George Karpenkov | e89d0a8 | 2017-07-15 00:30:46 +0000 | [diff] [blame] | 256 | if(COMPILER_RT_DEBUG) |
| 257 | list(APPEND SANITIZER_COMMON_CFLAGS -O0) |
| 258 | else() |
| 259 | list(APPEND SANITIZER_COMMON_CFLAGS -O3) |
| 260 | endif() |
| Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 261 | endif() |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 262 | |
| Alexey Samsonov | df3aeb8 | 2015-01-03 04:29:12 +0000 | [diff] [blame] | 263 | # Determine if we should restrict stack frame sizes. |
| Dmitry Vyukov | 4bf0894 | 2015-04-10 09:45:22 +0000 | [diff] [blame] | 264 | # Stack frames on PowerPC and Mips and in debug biuld can be much larger than |
| Alexey Samsonov | df3aeb8 | 2015-01-03 04:29:12 +0000 | [diff] [blame] | 265 | # anticipated. |
| 266 | # FIXME: Fix all sanitizers and add -Wframe-larger-than to |
| 267 | # SANITIZER_COMMON_FLAGS |
| 268 | if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG AND NOT COMPILER_RT_DEBUG |
| Alexey Samsonov | 63eaeca | 2015-09-08 23:13:47 +0000 | [diff] [blame] | 269 | AND NOT ${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "powerpc|mips") |
| Alexey Samsonov | df3aeb8 | 2015-01-03 04:29:12 +0000 | [diff] [blame] | 270 | set(SANITIZER_LIMIT_FRAME_SIZE TRUE) |
| 271 | else() |
| 272 | set(SANITIZER_LIMIT_FRAME_SIZE FALSE) |
| 273 | endif() |
| 274 | |
| Petr Hosek | 1608ca6 | 2018-05-09 21:24:06 +0000 | [diff] [blame] | 275 | if(FUCHSIA OR UNIX) |
| 276 | set(SANITIZER_USE_SYMBOLS TRUE) |
| 277 | else() |
| 278 | set(SANITIZER_USE_SYMBOLS FALSE) |
| 279 | endif() |
| 280 | |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 281 | # Build sanitizer runtimes with debug info. |
| Reid Kleckner | 8e19bd4 | 2018-04-26 20:34:19 +0000 | [diff] [blame] | 282 | if(MSVC) |
| Reid Kleckner | 1734b97 | 2016-08-02 01:02:46 +0000 | [diff] [blame] | 283 | # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099 |
| 284 | # warning from the MS linker complaining that it can't find the 'vc140.pdb' |
| 285 | # file used by our object library compilations. |
| 286 | list(APPEND SANITIZER_COMMON_CFLAGS /Z7) |
| 287 | llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/Z[i7I]" "/Z7") |
| 288 | llvm_replace_compiler_option(CMAKE_CXX_FLAGS_DEBUG "/Z[i7I]" "/Z7") |
| 289 | llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Z[i7I]" "/Z7") |
| Reid Kleckner | 8e19bd4 | 2018-04-26 20:34:19 +0000 | [diff] [blame] | 290 | elseif(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG) |
| 291 | list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) |
| 292 | elseif(COMPILER_RT_HAS_G_FLAG) |
| 293 | list(APPEND SANITIZER_COMMON_CFLAGS -g) |
| Alexey Samsonov | 75fb677 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 294 | endif() |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 295 | |
| Kuba Brecka | ab61c74 | 2016-10-08 09:01:27 +0000 | [diff] [blame] | 296 | if(LLVM_ENABLE_MODULES) |
| 297 | # Sanitizers cannot be built with -fmodules. The interceptors intentionally |
| 298 | # don't include system headers, which is incompatible with modules. |
| 299 | list(APPEND SANITIZER_COMMON_CFLAGS -fno-modules) |
| 300 | endif() |
| 301 | |
| Alexey Samsonov | 9a1ffce | 2014-02-18 07:26:58 +0000 | [diff] [blame] | 302 | # Turn off several warnings. |
| Alexey Samsonov | 829da45 | 2014-11-13 21:19:53 +0000 | [diff] [blame] | 303 | append_list_if(COMPILER_RT_HAS_WGNU_FLAG -Wno-gnu SANITIZER_COMMON_CFLAGS) |
| 304 | append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros SANITIZER_COMMON_CFLAGS) |
| 305 | append_list_if(COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG -Wno-c99-extensions SANITIZER_COMMON_CFLAGS) |
| 306 | append_list_if(COMPILER_RT_HAS_WNON_VIRTUAL_DTOR_FLAG -Wno-non-virtual-dtor SANITIZER_COMMON_CFLAGS) |
| Aaron Ballman | 1d1f232 | 2014-10-23 20:39:58 +0000 | [diff] [blame] | 307 | append_list_if(COMPILER_RT_HAS_WD4146_FLAG /wd4146 SANITIZER_COMMON_CFLAGS) |
| 308 | append_list_if(COMPILER_RT_HAS_WD4291_FLAG /wd4291 SANITIZER_COMMON_CFLAGS) |
| Kuba Brecka | 14c0c59 | 2014-10-15 22:47:54 +0000 | [diff] [blame] | 309 | append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS) |
| 310 | append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS) |
| Aaron Ballman | 1d1f232 | 2014-10-23 20:39:58 +0000 | [diff] [blame] | 311 | append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS) |
| Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 312 | |
| Petr Hosek | 2bbb6ad | 2017-07-28 03:39:38 +0000 | [diff] [blame] | 313 | # Set common link flags. |
| 314 | append_list_if(COMPILER_RT_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS) |
| 315 | |
| Petr Hosek | aa6d13e | 2018-05-22 22:58:48 +0000 | [diff] [blame] | 316 | if (COMPILER_RT_RUNTIME_LIBRARY STREQUAL "builtins") |
| Petr Hosek | 2bbb6ad | 2017-07-28 03:39:38 +0000 | [diff] [blame] | 317 | list(APPEND SANITIZER_COMMON_LINK_LIBS ${COMPILER_RT_BUILTINS_LIBRARY}) |
| 318 | else() |
| Petr Hosek | a14a2cc | 2017-07-31 22:46:43 +0000 | [diff] [blame] | 319 | if (ANDROID) |
| 320 | append_list_if(COMPILER_RT_HAS_GCC_LIB gcc SANITIZER_COMMON_LINK_LIBS) |
| 321 | else() |
| 322 | append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS) |
| 323 | endif() |
| Petr Hosek | 2bbb6ad | 2017-07-28 03:39:38 +0000 | [diff] [blame] | 324 | endif() |
| 325 | |
| 326 | append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS) |
| 327 | |
| Vitaly Buka | 5d960ec | 2017-08-01 22:22:25 +0000 | [diff] [blame] | 328 | if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia") |
| 329 | list(APPEND SANITIZER_COMMON_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro) |
| Petr Hosek | d8328f1 | 2017-09-13 01:18:15 +0000 | [diff] [blame] | 330 | list(APPEND SANITIZER_COMMON_LINK_LIBS zircon) |
| Vitaly Buka | 5d960ec | 2017-08-01 22:22:25 +0000 | [diff] [blame] | 331 | endif() |
| 332 | |
| Petr Hosek | 1ffaa48 | 2018-05-22 18:33:27 +0000 | [diff] [blame] | 333 | if (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libc++") |
| 334 | if (SANITIZER_CXX_ABI_INTREE) |
| 335 | if (NOT LIBCXXABI_ENABLE_STATIC_UNWINDER AND (TARGET unwind_shared OR HAVE_LIBUNWIND)) |
| 336 | list(APPEND SANITIZER_CXX_ABI_LIBRARY unwind_shared) |
| 337 | elseif (LIBCXXABI_ENABLE_STATIC_UNWINDER AND (TARGET unwind_static OR HAVE_LIBUNWIND)) |
| 338 | list(APPEND SANITIZER_CXX_ABI_LIBRARY unwind_static) |
| 339 | endif() |
| 340 | if (NOT LIBCXX_ENABLE_STATIC_ABI_LIBRARY AND (TARGET cxxabi_shared OR HAVE_LIBCXXABI)) |
| 341 | list(APPEND SANITIZER_CXX_ABI_LIBRARY cxxabi_shared) |
| 342 | elseif (LIBCXX_ENABLE_STATIC_ABI_LIBRARY AND (TARGET cxxabi_static OR HAVE_LIBCXXABI)) |
| 343 | list(APPEND SANITIZER_CXX_ABI_LIBRARY cxxabi_static) |
| 344 | endif() |
| 345 | else() |
| 346 | append_list_if(COMPILER_RT_HAS_LIBCXX c++ SANITIZER_CXX_ABI_LIBRARY) |
| 347 | endif() |
| 348 | elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libstdc++") |
| 349 | append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ SANITIZER_CXX_ABI_LIBRARY) |
| 350 | endif() |
| 351 | |
| Reid Kleckner | 44e6e36 | 2016-06-17 18:30:37 +0000 | [diff] [blame] | 352 | # Warnings to turn off for all libraries, not just sanitizers. |
| 353 | append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
| 354 | |
| Bob Haarman | f51b0d5 | 2017-01-04 21:40:00 +0000 | [diff] [blame] | 355 | if (CMAKE_LINKER MATCHES "link.exe$") |
| 356 | # Silence MSVC linker warnings caused by empty object files. The |
| 357 | # sanitizer libraries intentionally use ifdefs that result in empty |
| 358 | # files, rather than skipping these files in the build system. |
| 359 | # Ideally, we would pass this flag only for the libraries that need |
| 360 | # it, but CMake doesn't seem to have a way to set linker flags for |
| 361 | # individual static libraries, so we enable the suppression flag for |
| 362 | # the whole compiler-rt project. |
| Vitaly Buka | d03bc7c | 2018-03-19 18:22:35 +0000 | [diff] [blame] | 363 | set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221") |
| Bob Haarman | f51b0d5 | 2017-01-04 21:40:00 +0000 | [diff] [blame] | 364 | endif() |
| 365 | |
| Alexey Samsonov | 9f3938e | 2013-04-11 15:49:52 +0000 | [diff] [blame] | 366 | add_subdirectory(include) |
| 367 | |
| Petr Hosek | 95d7a41 | 2017-12-12 01:20:52 +0000 | [diff] [blame] | 368 | foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxx |
| 369 | ${LLVM_MAIN_SRC_DIR}/runtimes/libcxx |
| 370 | ${LLVM_MAIN_SRC_DIR}/../libcxx |
| 371 | ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR}) |
| 372 | if(IS_DIRECTORY ${path}) |
| 373 | set(COMPILER_RT_LIBCXX_PATH ${path}) |
| 374 | break() |
| Zachary Turner | 1929ffd | 2017-09-13 20:49:25 +0000 | [diff] [blame] | 375 | endif() |
| Petr Hosek | 95d7a41 | 2017-12-12 01:20:52 +0000 | [diff] [blame] | 376 | endforeach() |
| Alexey Samsonov | 8434e60 | 2014-02-14 13:02:58 +0000 | [diff] [blame] | 377 | |
| Zachary Turner | 1929ffd | 2017-09-13 20:49:25 +0000 | [diff] [blame] | 378 | set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld) |
| 379 | if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD) |
| 380 | set(COMPILER_RT_HAS_LLD TRUE) |
| 381 | else() |
| 382 | set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld) |
| 383 | if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD) |
| 384 | set(COMPILER_RT_HAS_LLD TRUE) |
| 385 | else() |
| 386 | set(COMPILER_RT_HAS_LLD FALSE) |
| 387 | endif() |
| Reid Kleckner | 45ebaf1 | 2015-08-11 00:33:07 +0000 | [diff] [blame] | 388 | endif() |
| Evgeniy Stepanov | 656bc5b | 2017-03-25 00:42:25 +0000 | [diff] [blame] | 389 | pythonize_bool(COMPILER_RT_HAS_LLD) |
| Reid Kleckner | 45ebaf1 | 2015-08-11 00:33:07 +0000 | [diff] [blame] | 390 | |
| Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 391 | add_subdirectory(lib) |
| 392 | |
| Alexey Samsonov | cd8535a | 2014-02-19 11:18:47 +0000 | [diff] [blame] | 393 | if(COMPILER_RT_INCLUDE_TESTS) |
| Alexey Samsonov | 81a2b46 | 2014-02-14 11:00:07 +0000 | [diff] [blame] | 394 | add_subdirectory(unittests) |
| Chris Bieneman | fb92d9a | 2015-09-14 19:54:12 +0000 | [diff] [blame] | 395 | add_subdirectory(test) |
| Zachary Turner | ce92db1 | 2017-09-15 22:10:46 +0000 | [diff] [blame] | 396 | if (COMPILER_RT_STANDALONE_BUILD) |
| 397 | # If we have a valid source tree, generate llvm-lit into the bin directory. |
| 398 | # The user can still choose to have the check targets *use* a different lit |
| 399 | # by specifying -DLLVM_EXTERNAL_LIT, but we generate it regardless. |
| 400 | if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit) |
| 401 | add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit) |
| 402 | elseif(NOT EXISTS ${LLVM_EXTERNAL_LIT}) |
| 403 | message(WARNING "Could not find LLVM source directory and LLVM_EXTERNAL_LIT does not" |
| 404 | "point to a valid file. You will not be able to run tests.") |
| 405 | endif() |
| 406 | endif() |
| Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 407 | endif() |