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