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 |
| 4 | # 'projects/compiler-rt' inside of an LLVM tree, it is not a stand-alone build |
| 5 | # system. |
| 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 | include(LLVMParseArguments) |
| 11 | |
Chandler Carruth | c78ad00 | 2012-06-25 08:40:10 +0000 | [diff] [blame] | 12 | # The CompilerRT build system requires CMake version 2.8.8 or higher in order |
| 13 | # to use its support for building convenience "libraries" as a collection of |
| 14 | # .o files. This is particularly useful in producing larger, more complex |
| 15 | # runtime libraries. |
| 16 | cmake_minimum_required(VERSION 2.8.8) |
| 17 | |
Alexey Samsonov | 20abdf6 | 2013-10-01 12:52:15 +0000 | [diff] [blame] | 18 | # Top level target used to build all compiler-rt libraries. |
| 19 | add_custom_target(compiler-rt) |
| 20 | |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 21 | # Compute the Clang version from the LLVM version. |
| 22 | # FIXME: We should be able to reuse CLANG_VERSION variable calculated |
| 23 | # in Clang cmake files, instead of copying the rules here. |
| 24 | string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION |
| 25 | ${PACKAGE_VERSION}) |
| 26 | # Setup the paths where compiler-rt runtimes and headers should be stored. |
| 27 | set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}) |
| 28 | string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR) |
Alexey Samsonov | 4c17c1b | 2013-03-19 09:17:35 +0000 | [diff] [blame] | 29 | set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}) |
| 30 | set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR}) |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 31 | set(COMPILER_RT_LIBRARY_INSTALL_DIR |
Alexey Samsonov | 4c17c1b | 2013-03-19 09:17:35 +0000 | [diff] [blame] | 32 | ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}) |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 33 | |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 34 | # Add path for custom modules |
| 35 | set(CMAKE_MODULE_PATH |
| 36 | ${CMAKE_MODULE_PATH} |
| 37 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
| 38 | ) |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 39 | include(AddCompilerRT) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 40 | |
| 41 | set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
Alexey Samsonov | 6a65b18 | 2013-06-06 12:35:48 +0000 | [diff] [blame] | 42 | set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
Alexey Samsonov | 4c17c1b | 2013-03-19 09:17:35 +0000 | [diff] [blame] | 43 | # Setup custom SDK sysroots. |
| 44 | set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin) |
| 45 | set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux) |
Alexey Samsonov | acab30e | 2013-08-27 15:08:02 +0000 | [diff] [blame] | 46 | include(SanitizerUtils) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 47 | |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 48 | # Detect whether the current target platform is 32-bit or 64-bit, and setup |
| 49 | # the correct commandline flags needed to attempt to target 32-bit and 64-bit. |
Alexey Samsonov | dd8872b | 2013-06-30 16:21:32 +0000 | [diff] [blame] | 50 | if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND |
| 51 | NOT CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 52 | message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.") |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 53 | endif() |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 54 | if (NOT MSVC) |
| 55 | set(TARGET_64_BIT_CFLAGS "-m64") |
| 56 | set(TARGET_32_BIT_CFLAGS "-m32") |
| 57 | else() |
| 58 | set(TARGET_64_BIT_CFLAGS "") |
| 59 | set(TARGET_32_BIT_CFLAGS "") |
| 60 | endif() |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 61 | |
Alexey Samsonov | b670018 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 62 | # List of architectures we can target. |
| 63 | set(COMPILER_RT_SUPPORTED_ARCH) |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 64 | |
Alexey Samsonov | 85bd73d | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 65 | function(get_target_flags_for_arch arch out_var) |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 66 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 67 | if(ARCH_INDEX EQUAL -1) |
Alexey Samsonov | 85bd73d | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 68 | message(FATAL_ERROR "Unsupported architecture: ${arch}") |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 69 | else() |
| 70 | set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE) |
Alexey Samsonov | 85bd73d | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 71 | endif() |
| 72 | endfunction() |
| 73 | |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 74 | # Try to compile a very simple source file to ensure we can target the given |
| 75 | # platform. We use the results of these tests to build only the various target |
| 76 | # runtime libraries supported by our current compilers cross-compiling |
| 77 | # abilities. |
Alexey Samsonov | b670018 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 78 | set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c) |
| 79 | file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}") |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 80 | |
Alexey Samsonov | b670018 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 81 | # test_target_arch(<arch> <target flags...>) |
| 82 | # Sets the target flags for a given architecture and determines if this |
| 83 | # architecture is supported by trying to build a simple file. |
| 84 | macro(test_target_arch arch) |
| 85 | set(TARGET_${arch}_CFLAGS ${ARGN}) |
| 86 | try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE} |
| 87 | COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}" |
| 88 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}") |
| 89 | if(${CAN_TARGET_${arch}}) |
| 90 | list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch}) |
| 91 | endif() |
| 92 | endmacro() |
| 93 | |
| 94 | if("${LLVM_NATIVE_ARCH}" STREQUAL "X86") |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 95 | if (NOT MSVC) |
| 96 | test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS}) |
| 97 | endif() |
Alexey Samsonov | b670018 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 98 | test_target_arch(i386 ${TARGET_32_BIT_CFLAGS}) |
| 99 | elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC") |
Alexey Samsonov | dd8872b | 2013-06-30 16:21:32 +0000 | [diff] [blame] | 100 | test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS}) |
Renato Golin | 66e2b70 | 2014-01-31 14:25:58 +0000 | [diff] [blame^] | 101 | elseif("${LLVM_NATIVE_ARCH}" STREQUAL "ARM") |
| 102 | test_target_arch(arm "") |
Alexey Samsonov | b670018 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 103 | endif() |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 104 | |
Alexey Samsonov | c20f5d2 | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 105 | # We only support running instrumented tests when we're not cross compiling |
| 106 | # and target a unix-like system. On Android we define the rules for building |
| 107 | # unit tests, but don't execute them. |
| 108 | if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) |
Michael Gottesman | 4ddc215 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 109 | option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON) |
Alexey Samsonov | c20f5d2 | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 110 | else() |
Michael Gottesman | 4ddc215 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 111 | option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF) |
Alexey Samsonov | c20f5d2 | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 112 | endif() |
Michael Gottesman | 4ddc215 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 113 | |
Alexey Samsonov | 5cb7860 | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 114 | # Check if compiler-rt is built with libc++. |
| 115 | find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++" |
| 116 | COMPILER_RT_USES_LIBCXX) |
| 117 | |
Alexey Samsonov | fb844c7 | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 118 | function(filter_available_targets out_var) |
| 119 | set(archs) |
| 120 | foreach(arch ${ARGN}) |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 121 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 122 | if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch}) |
Alexey Samsonov | fb844c7 | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 123 | list(APPEND archs ${arch}) |
| 124 | endif() |
| 125 | endforeach() |
| 126 | set(${out_var} ${archs} PARENT_SCOPE) |
| 127 | endfunction() |
| 128 | |
Peter Collingbourne | cbdea32 | 2013-10-25 23:03:34 +0000 | [diff] [blame] | 129 | option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF) |
| 130 | |
| 131 | # COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in. |
| 132 | pythonize_bool(COMPILER_RT_DEBUG) |
| 133 | |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 134 | # Provide some common commmandline flags for Sanitizer runtimes. |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 135 | if (NOT MSVC) |
| 136 | set(SANITIZER_COMMON_CFLAGS |
| 137 | -fPIC |
| 138 | -fno-builtin |
| 139 | -fno-exceptions |
| 140 | -fomit-frame-pointer |
| 141 | -funwind-tables |
| 142 | -fno-stack-protector |
| 143 | -Wno-gnu # Variadic macros with 0 arguments for ... |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 144 | -fvisibility=hidden |
| 145 | ) |
Peter Collingbourne | cbdea32 | 2013-10-25 23:03:34 +0000 | [diff] [blame] | 146 | if (NOT COMPILER_RT_DEBUG) |
| 147 | list(APPEND SANITIZER_COMMON_CFLAGS -O3) |
| 148 | endif() |
Alexey Samsonov | 75fb677 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 149 | else() |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 150 | set(SANITIZER_COMMON_CFLAGS |
| 151 | /MT |
| 152 | /Zi |
Timur Iskhodzhanov | 00aa75b | 2014-01-30 19:48:13 +0000 | [diff] [blame] | 153 | /FS |
Hans Wennborg | f8e5627 | 2013-08-28 16:14:59 +0000 | [diff] [blame] | 154 | /Oy- |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 155 | /GS- |
Timur Iskhodzhanov | 00aa75b | 2014-01-30 19:48:13 +0000 | [diff] [blame] | 156 | /wd4221 |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 157 | /wd4722 |
| 158 | ) |
| 159 | endif() |
| 160 | # Build sanitizer runtimes with debug info. (MSVC gets /Zi above) |
| 161 | if (NOT MSVC) |
| 162 | check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
Peter Collingbourne | cbdea32 | 2013-10-25 23:03:34 +0000 | [diff] [blame] | 163 | if(SUPPORTS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG) |
Hans Wennborg | 67c6e50 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 164 | list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) |
| 165 | else() |
| 166 | list(APPEND SANITIZER_COMMON_CFLAGS -g) |
| 167 | endif() |
Alexey Samsonov | 75fb677 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 168 | endif() |
| 169 | # Warnings suppressions. |
Alexey Samsonov | 3d53c4e | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 170 | check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 171 | if(SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
| 172 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros) |
| 173 | endif() |
Alexey Samsonov | 3d53c4e | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 174 | check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 175 | if(SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
| 176 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions) |
| 177 | endif() |
Alexey Samsonov | acfb82e | 2013-03-25 10:31:49 +0000 | [diff] [blame] | 178 | # Sanitizer may not have libstdc++, so we can have problems with virtual |
| 179 | # destructors. |
| 180 | check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG) |
| 181 | if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG) |
| 182 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor) |
| 183 | endif() |
Alexey Samsonov | 06379b3 | 2013-08-29 12:08:36 +0000 | [diff] [blame] | 184 | check_cxx_compiler_flag(-Wglobal-constructors SUPPORTS_GLOBAL_CONSTRUCTORS_FLAG) |
| 185 | # Not all sanitizers forbid global constructors. |
Alexey Samsonov | 5cb7860 | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 186 | |
Alexey Samsonov | d83ccd0 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 187 | if(APPLE) |
Alexander Potapenko | 49034e3 | 2013-11-07 10:08:19 +0000 | [diff] [blame] | 188 | # Obtain the iOS Simulator SDK path from xcodebuild. |
| 189 | execute_process( |
| 190 | COMMAND xcodebuild -version -sdk iphonesimulator Path |
| 191 | OUTPUT_VARIABLE IOSSIM_SDK_DIR |
| 192 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 193 | ) |
| 194 | set(SANITIZER_COMMON_SUPPORTED_DARWIN_OS osx) |
| 195 | if (IOSSIM_SDK_DIR) |
| 196 | list(APPEND SANITIZER_COMMON_SUPPORTED_DARWIN_OS iossim) |
| 197 | endif() |
| 198 | |
Alexey Samsonov | 5cb7860 | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 199 | if(COMPILER_RT_USES_LIBCXX) |
| 200 | set(SANITIZER_MIN_OSX_VERSION 10.7) |
| 201 | else() |
Alexey Samsonov | eb79732 | 2013-07-16 11:54:40 +0000 | [diff] [blame] | 202 | set(SANITIZER_MIN_OSX_VERSION 10.6) |
Alexey Samsonov | 5cb7860 | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 203 | endif() |
Alexander Potapenko | 49034e3 | 2013-11-07 10:08:19 +0000 | [diff] [blame] | 204 | set(DARWIN_osx_CFLAGS -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION}) |
| 205 | set(DARWIN_iossim_CFLAGS |
| 206 | -mios-simulator-version-min=7.0 -isysroot ${IOSSIM_SDK_DIR}) |
| 207 | set(DARWIN_osx_LINKFLAGS) |
| 208 | set(DARWIN_iossim_LINKFLAGS |
| 209 | -Wl,-ios_simulator_version_min,7.0.0 |
| 210 | -mios-simulator-version-min=7.0 |
Alexander Potapenko | 2b00289 | 2013-11-19 14:58:42 +0000 | [diff] [blame] | 211 | -isysroot ${IOSSIM_SDK_DIR}) |
Alexey Samsonov | d83ccd0 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 212 | endif() |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 213 | |
Alexey Samsonov | b068483 | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 214 | # Architectures supported by Sanitizer runtimes. Specific sanitizers may |
| 215 | # support only subset of these (e.g. TSan works on x86_64 only). |
| 216 | filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH |
Renato Golin | 66e2b70 | 2014-01-31 14:25:58 +0000 | [diff] [blame^] | 217 | x86_64 i386 powerpc64 arm) |
Alexey Samsonov | b068483 | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 218 | |
Alexey Samsonov | 9f3938e | 2013-04-11 15:49:52 +0000 | [diff] [blame] | 219 | add_subdirectory(include) |
| 220 | |
| 221 | set(SANITIZER_COMMON_LIT_TEST_DEPS |
| 222 | clang clang-headers FileCheck count not llvm-nm llvm-symbolizer |
| 223 | compiler-rt-headers) |
Alexey Samsonov | 5a2f073 | 2013-08-29 10:49:04 +0000 | [diff] [blame] | 224 | # Check code style when running lit tests for sanitizers. |
| 225 | if(UNIX) |
| 226 | list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck) |
| 227 | endif() |
Chandler Carruth | 1aa4fef | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 228 | |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 229 | add_subdirectory(lib) |
| 230 | |
| 231 | if(LLVM_INCLUDE_TESTS) |
Chandler Carruth | 84ba680 | 2012-06-22 20:17:27 +0000 | [diff] [blame] | 232 | # Currently the tests have not been ported to CMake, so disable this |
| 233 | # directory. |
| 234 | # |
| 235 | #add_subdirectory(test) |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 236 | endif() |