1# CMake build for CompilerRT. 2# 3# This build assumes that CompilerRT is checked out into the 4# 'projects/compiler-rt' inside of an LLVM tree. 5# Standalone build system for CompilerRT is not yet ready. 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 10# Check if compiler-rt is built as a standalone project. 11if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) 12 project(CompilerRT C CXX ASM) 13 set(COMPILER_RT_STANDALONE_BUILD TRUE) 14endif() 15 16cmake_minimum_required(VERSION 3.4.3) 17# FIXME: 18# The OLD behavior (pre 3.2) for this policy is to not set the value of the 19# CMAKE_EXE_LINKER_FLAGS variable in the generated test project. The NEW behavior 20# for this policy is to set the value of the CMAKE_EXE_LINKER_FLAGS variable 21# in the test project to the same as it is in the calling project. The new 22# behavior cause the compiler_rt test to fail during try_compile: see 23# projects/compiler-rt/cmake/Modules/CompilerRTUtils.cmake:121 such that 24# CAN_TARGET_${arch} is not set properly. This results in COMPILER_RT_SUPPORTED_ARCH 25# not being updated properly leading to poblems. 26cmake_policy(SET CMP0056 OLD) 27 28# Add path for custom compiler-rt modules. 29list(INSERT CMAKE_MODULE_PATH 0 30 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 31 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" 32 ) 33 34include(base-config-ix) 35 36option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON) 37mark_as_advanced(COMPILER_RT_BUILD_BUILTINS) 38option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON) 39mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS) 40 41if (COMPILER_RT_STANDALONE_BUILD) 42 if (NOT LLVM_CONFIG_PATH) 43 find_program(LLVM_CONFIG_PATH "llvm-config" 44 DOC "Path to llvm-config binary") 45 if (NOT LLVM_CONFIG_PATH) 46 message(FATAL_ERROR "llvm-config not found: specify LLVM_CONFIG_PATH") 47 endif() 48 endif() 49 execute_process( 50 COMMAND ${LLVM_CONFIG_PATH} "--obj-root" "--bindir" "--libdir" "--src-root" 51 RESULT_VARIABLE HAD_ERROR 52 OUTPUT_VARIABLE CONFIG_OUTPUT) 53 if (HAD_ERROR) 54 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}") 55 endif() 56 string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT}) 57 list(GET CONFIG_OUTPUT 0 LLVM_BINARY_DIR) 58 list(GET CONFIG_OUTPUT 1 LLVM_TOOLS_BINARY_DIR) 59 list(GET CONFIG_OUTPUT 2 LLVM_LIBRARY_DIR) 60 list(GET CONFIG_OUTPUT 3 LLVM_MAIN_SRC_DIR) 61 62 # Make use of LLVM CMake modules. 63 file(TO_CMAKE_PATH ${LLVM_BINARY_DIR} LLVM_BINARY_DIR_CMAKE_STYLE) 64 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR_CMAKE_STYLE}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") 65 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") 66 # Get some LLVM variables from LLVMConfig. 67 include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake") 68 69 set(LLVM_LIBRARY_OUTPUT_INTDIR 70 ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX}) 71 72 # Find Python interpreter. 73 set(Python_ADDITIONAL_VERSIONS 2.7 2.6 2.5) 74 include(FindPythonInterp) 75 if(NOT PYTHONINTERP_FOUND) 76 message(FATAL_ERROR " 77 Unable to find Python interpreter required testing. Please install Python 78 or specify the PYTHON_EXECUTABLE CMake variable.") 79 endif() 80 81 # Define default arguments to lit. 82 set(LIT_ARGS_DEFAULT "-sv") 83 if (MSVC OR XCODE) 84 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") 85 endif() 86 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") 87endif() 88 89set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${TARGET_TRIPLE} CACHE STRING 90 "Default triple for which compiler-rt runtimes will be built.") 91if(DEFINED COMPILER_RT_TEST_TARGET_TRIPLE) 92 # Backwards compatibility: this variable used to be called 93 # COMPILER_RT_TEST_TARGET_TRIPLE. 94 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${COMPILER_RT_TEST_TARGET_TRIPLE}) 95endif() 96 97string(REPLACE "-" ";" TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE}) 98list(GET TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH) 99list(GET TARGET_TRIPLE_LIST 1 COMPILER_RT_DEFAULT_TARGET_OS) 100list(GET TARGET_TRIPLE_LIST 2 COMPILER_RT_DEFAULT_TARGET_ABI) 101# Determine if test target triple is specified explicitly, and doesn't match the 102# default. 103if(NOT COMPILER_RT_DEFAULT_TARGET_TRIPLE STREQUAL TARGET_TRIPLE) 104 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE TRUE) 105else() 106 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE FALSE) 107endif() 108if ("${COMPILER_RT_DEFAULT_TARGET_ABI}" STREQUAL "androideabi") 109 set(ANDROID 1) 110endif() 111include(CompilerRTUtils) 112 113set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 114set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 115 116# We support running instrumented tests when we're not cross compiling 117# and target a UNIX-like system or Windows. 118# We can run tests on Android even when we are cross-compiling. 119if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND (UNIX OR WIN32)) OR ANDROID 120 OR COMPILER_RT_EMULATOR) 121 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON) 122else() 123 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF) 124endif() 125 126option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF) 127option(COMPILER_RT_EXTERNALIZE_DEBUGINFO 128 "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF) 129# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in. 130pythonize_bool(COMPILER_RT_DEBUG) 131 132#================================ 133# Setup Compiler Flags 134#================================ 135 136include(config-ix) 137 138if(MSVC) 139 # Override any existing /W flags with /W4. This is what LLVM does. Failing to 140 # remove other /W[0-4] flags will result in a warning about overriding a 141 # previous flag. 142 if (COMPILER_RT_HAS_W4_FLAG) 143 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") 144 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 145 append_string_if(COMPILER_RT_HAS_W4_FLAG /W4 CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 146 endif() 147else() 148 append_string_if(COMPILER_RT_HAS_WALL_FLAG -Wall CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 149endif() 150if(COMPILER_RT_ENABLE_WERROR) 151 append_string_if(COMPILER_RT_HAS_WERROR_FLAG -Werror CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 152 append_string_if(COMPILER_RT_HAS_WX_FLAG /WX CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 153endif() 154 155append_string_if(COMPILER_RT_HAS_STD_CXX11_FLAG -std=c++11 CMAKE_CXX_FLAGS) 156 157# Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP. 158if(NOT COMPILER_RT_HAS_FUNC_SYMBOL) 159 add_definitions(-D__func__=__FUNCTION__) 160endif() 161 162# Provide some common commmandline flags for Sanitizer runtimes. 163append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS) 164append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin SANITIZER_COMMON_CFLAGS) 165append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SANITIZER_COMMON_CFLAGS) 166if(NOT COMPILER_RT_DEBUG) 167 append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS) 168endif() 169append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS) 170append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS) 171append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS) 172append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS) 173append_list_if(COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG -fvisibility-inlines-hidden SANITIZER_COMMON_CFLAGS) 174append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections SANITIZER_COMMON_CFLAGS) 175append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS) 176 177if(MSVC) 178 # Replace the /M[DT][d] flags with /MT, and strip any definitions of _DEBUG, 179 # which cause definition mismatches at link time. 180 # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214. 181 if(COMPILER_RT_HAS_MT_FLAG) 182 foreach(flag_var 183 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE 184 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO 185 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 186 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 187 string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}") 188 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 189 string(REGEX REPLACE "/D_DEBUG" "" ${flag_var} "${${flag_var}}") 190 endforeach() 191 endif() 192 append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS) 193 append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS) 194 # VS 2015 (version 1900) added support for thread safe static initialization. 195 # However, ASan interceptors run before CRT initialization, which causes the 196 # new thread safe code to crash. Disable this feature for now. 197 if (MSVC_VERSION GREATER 1899) 198 list(APPEND SANITIZER_COMMON_CFLAGS /Zc:threadSafeInit-) 199 endif() 200endif() 201 202append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS) 203 204# Build with optimization, unless we're in debug mode. If we're using MSVC, 205# always respect the optimization flags set by CMAKE_BUILD_TYPE instead. 206if(NOT COMPILER_RT_DEBUG AND NOT MSVC) 207 list(APPEND SANITIZER_COMMON_CFLAGS -O3) 208endif() 209 210# Determine if we should restrict stack frame sizes. 211# Stack frames on PowerPC and Mips and in debug biuld can be much larger than 212# anticipated. 213# FIXME: Fix all sanitizers and add -Wframe-larger-than to 214# SANITIZER_COMMON_FLAGS 215if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG AND NOT COMPILER_RT_DEBUG 216 AND NOT ${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "powerpc|mips") 217 set(SANITIZER_LIMIT_FRAME_SIZE TRUE) 218else() 219 set(SANITIZER_LIMIT_FRAME_SIZE FALSE) 220endif() 221 222# Build sanitizer runtimes with debug info. 223if(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG) 224 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) 225elseif(COMPILER_RT_HAS_G_FLAG) 226 list(APPEND SANITIZER_COMMON_CFLAGS -g) 227elseif(COMPILER_RT_HAS_Zi_FLAG) 228 list(APPEND SANITIZER_COMMON_CFLAGS /Zi) 229endif() 230 231# Turn off several warnings. 232append_list_if(COMPILER_RT_HAS_WGNU_FLAG -Wno-gnu SANITIZER_COMMON_CFLAGS) 233append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros SANITIZER_COMMON_CFLAGS) 234append_list_if(COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG -Wno-c99-extensions SANITIZER_COMMON_CFLAGS) 235append_list_if(COMPILER_RT_HAS_WNON_VIRTUAL_DTOR_FLAG -Wno-non-virtual-dtor SANITIZER_COMMON_CFLAGS) 236append_list_if(COMPILER_RT_HAS_WD4146_FLAG /wd4146 SANITIZER_COMMON_CFLAGS) 237append_list_if(COMPILER_RT_HAS_WD4291_FLAG /wd4291 SANITIZER_COMMON_CFLAGS) 238append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS) 239append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS) 240append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS) 241 242# Warnings to turn off for all libraries, not just sanitizers. 243append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS) 244 245if(APPLE AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9") 246 # Mac OS X prior to 10.9 had problems with exporting symbols from 247 # libc++/libc++abi. 248 set(SANITIZER_CAN_USE_CXXABI FALSE) 249elseif(MSVC) 250 set(SANITIZER_CAN_USE_CXXABI FALSE) 251else() 252 set(SANITIZER_CAN_USE_CXXABI TRUE) 253endif() 254pythonize_bool(SANITIZER_CAN_USE_CXXABI) 255 256add_subdirectory(include) 257 258set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/projects/libcxx) 259if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/) 260 set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE) 261else() 262 set(COMPILER_RT_HAS_LIBCXX_SOURCES FALSE) 263endif() 264 265set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld) 266if(EXISTS ${COMPILER_RT_LLD_PATH}/) 267 set(COMPILER_RT_HAS_LLD_SOURCES TRUE) 268else() 269 set(COMPILER_RT_HAS_LLD_SOURCES FALSE) 270endif() 271pythonize_bool(COMPILER_RT_HAS_LLD_SOURCES) 272 273add_subdirectory(lib) 274 275if(COMPILER_RT_INCLUDE_TESTS) 276 add_subdirectory(unittests) 277 add_subdirectory(test) 278endif() 279