Chandler Carruth | d51e0a0 | 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 | a765ffc | 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 | e16af95 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 18 | # Compute the Clang version from the LLVM version. |
| 19 | # FIXME: We should be able to reuse CLANG_VERSION variable calculated |
| 20 | # in Clang cmake files, instead of copying the rules here. |
| 21 | string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION |
| 22 | ${PACKAGE_VERSION}) |
| 23 | # Setup the paths where compiler-rt runtimes and headers should be stored. |
| 24 | set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}) |
| 25 | string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR) |
Alexey Samsonov | c9de451 | 2013-03-19 09:17:35 +0000 | [diff] [blame] | 26 | set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}) |
| 27 | set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR}) |
Alexey Samsonov | e16af95 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 28 | set(COMPILER_RT_LIBRARY_INSTALL_DIR |
Alexey Samsonov | c9de451 | 2013-03-19 09:17:35 +0000 | [diff] [blame] | 29 | ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}) |
Alexey Samsonov | e16af95 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 30 | |
Alexey Samsonov | 02dcc63 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 31 | # Add path for custom modules |
| 32 | set(CMAKE_MODULE_PATH |
| 33 | ${CMAKE_MODULE_PATH} |
| 34 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
| 35 | ) |
Alexey Samsonov | 392c50d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 36 | include(AddCompilerRT) |
Alexey Samsonov | 02dcc63 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 37 | |
| 38 | set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
Alexey Samsonov | dd6605e | 2013-06-06 12:35:48 +0000 | [diff] [blame] | 39 | set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
Alexey Samsonov | c9de451 | 2013-03-19 09:17:35 +0000 | [diff] [blame] | 40 | # Setup custom SDK sysroots. |
| 41 | set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin) |
| 42 | set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux) |
Alexey Samsonov | e5fa243 | 2013-08-27 15:08:02 +0000 | [diff] [blame] | 43 | include(SanitizerUtils) |
Alexey Samsonov | 02dcc63 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 44 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 45 | # Detect whether the current target platform is 32-bit or 64-bit, and setup |
| 46 | # the correct commandline flags needed to attempt to target 32-bit and 64-bit. |
Alexey Samsonov | 33b2135 | 2013-06-30 16:21:32 +0000 | [diff] [blame] | 47 | if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND |
| 48 | NOT CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 49 | message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 50 | endif() |
Hans Wennborg | c1f1af7 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 51 | if (NOT MSVC) |
| 52 | set(TARGET_64_BIT_CFLAGS "-m64") |
| 53 | set(TARGET_32_BIT_CFLAGS "-m32") |
| 54 | else() |
| 55 | set(TARGET_64_BIT_CFLAGS "") |
| 56 | set(TARGET_32_BIT_CFLAGS "") |
| 57 | endif() |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 58 | |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 59 | # List of architectures we can target. |
| 60 | set(COMPILER_RT_SUPPORTED_ARCH) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 61 | |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 62 | function(get_target_flags_for_arch arch out_var) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 63 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 64 | if(ARCH_INDEX EQUAL -1) |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 65 | message(FATAL_ERROR "Unsupported architecture: ${arch}") |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 66 | else() |
| 67 | set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE) |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 68 | endif() |
| 69 | endfunction() |
| 70 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 71 | # Try to compile a very simple source file to ensure we can target the given |
| 72 | # platform. We use the results of these tests to build only the various target |
| 73 | # runtime libraries supported by our current compilers cross-compiling |
| 74 | # abilities. |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 75 | set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c) |
| 76 | file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 77 | |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 78 | # test_target_arch(<arch> <target flags...>) |
| 79 | # Sets the target flags for a given architecture and determines if this |
| 80 | # architecture is supported by trying to build a simple file. |
| 81 | macro(test_target_arch arch) |
| 82 | set(TARGET_${arch}_CFLAGS ${ARGN}) |
| 83 | try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE} |
| 84 | COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}" |
| 85 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}") |
| 86 | if(${CAN_TARGET_${arch}}) |
| 87 | list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch}) |
| 88 | endif() |
| 89 | endmacro() |
| 90 | |
| 91 | if("${LLVM_NATIVE_ARCH}" STREQUAL "X86") |
Hans Wennborg | c1f1af7 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 92 | if (NOT MSVC) |
| 93 | test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS}) |
| 94 | endif() |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 95 | test_target_arch(i386 ${TARGET_32_BIT_CFLAGS}) |
| 96 | elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC") |
Alexey Samsonov | 33b2135 | 2013-06-30 16:21:32 +0000 | [diff] [blame] | 97 | test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS}) |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 98 | endif() |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 99 | |
Alexey Samsonov | d7d7b5f | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 100 | # We only support running instrumented tests when we're not cross compiling |
| 101 | # and target a unix-like system. On Android we define the rules for building |
| 102 | # unit tests, but don't execute them. |
| 103 | if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) |
Michael Gottesman | fd0a789 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 104 | option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON) |
Alexey Samsonov | d7d7b5f | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 105 | else() |
Michael Gottesman | fd0a789 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 106 | option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF) |
Alexey Samsonov | d7d7b5f | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 107 | endif() |
Michael Gottesman | fd0a789 | 2013-04-01 04:13:03 +0000 | [diff] [blame] | 108 | |
Alexey Samsonov | 304026d | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 109 | # Check if compiler-rt is built with libc++. |
| 110 | find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++" |
| 111 | COMPILER_RT_USES_LIBCXX) |
| 112 | |
Alexey Samsonov | fe51abb | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 113 | function(filter_available_targets out_var) |
| 114 | set(archs) |
| 115 | foreach(arch ${ARGN}) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 116 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 117 | if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch}) |
Alexey Samsonov | fe51abb | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 118 | list(APPEND archs ${arch}) |
| 119 | endif() |
| 120 | endforeach() |
| 121 | set(${out_var} ${archs} PARENT_SCOPE) |
| 122 | endfunction() |
| 123 | |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 124 | # Provide some common commmandline flags for Sanitizer runtimes. |
Hans Wennborg | c1f1af7 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 125 | if (NOT MSVC) |
| 126 | set(SANITIZER_COMMON_CFLAGS |
| 127 | -fPIC |
| 128 | -fno-builtin |
| 129 | -fno-exceptions |
| 130 | -fomit-frame-pointer |
| 131 | -funwind-tables |
| 132 | -fno-stack-protector |
| 133 | -Wno-gnu # Variadic macros with 0 arguments for ... |
| 134 | -O3 |
| 135 | -fvisibility=hidden |
| 136 | ) |
Alexey Samsonov | 275ca01 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 137 | else() |
Hans Wennborg | c1f1af7 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 138 | set(SANITIZER_COMMON_CFLAGS |
| 139 | /MT |
| 140 | /Zi |
Hans Wennborg | 3472413 | 2013-08-28 16:14:59 +0000 | [diff] [blame] | 141 | /Oy- |
Hans Wennborg | c1f1af7 | 2013-08-27 01:24:01 +0000 | [diff] [blame] | 142 | /GS- |
| 143 | /wd4722 |
| 144 | ) |
| 145 | endif() |
| 146 | # Build sanitizer runtimes with debug info. (MSVC gets /Zi above) |
| 147 | if (NOT MSVC) |
| 148 | check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 149 | if(SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 150 | list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) |
| 151 | else() |
| 152 | list(APPEND SANITIZER_COMMON_CFLAGS -g) |
| 153 | endif() |
Alexey Samsonov | 275ca01 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 154 | endif() |
| 155 | # Warnings suppressions. |
Alexey Samsonov | 721460b | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 156 | check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 157 | if(SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
| 158 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros) |
| 159 | endif() |
Alexey Samsonov | 721460b | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 160 | check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 161 | if(SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
| 162 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions) |
| 163 | endif() |
Alexey Samsonov | e374e4e | 2013-03-25 10:31:49 +0000 | [diff] [blame] | 164 | # Sanitizer may not have libstdc++, so we can have problems with virtual |
| 165 | # destructors. |
| 166 | check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG) |
| 167 | if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG) |
| 168 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor) |
| 169 | endif() |
Alexey Samsonov | 304026d | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 170 | |
| 171 | # Setup min Mac OS X version. |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 172 | if(APPLE) |
Alexey Samsonov | 304026d | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 173 | if(COMPILER_RT_USES_LIBCXX) |
| 174 | set(SANITIZER_MIN_OSX_VERSION 10.7) |
| 175 | else() |
Alexey Samsonov | b6bde4d | 2013-07-16 11:54:40 +0000 | [diff] [blame] | 176 | set(SANITIZER_MIN_OSX_VERSION 10.6) |
Alexey Samsonov | 304026d | 2013-02-08 07:39:25 +0000 | [diff] [blame] | 177 | endif() |
| 178 | list(APPEND SANITIZER_COMMON_CFLAGS |
| 179 | -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION}) |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 180 | endif() |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 181 | |
Alexey Samsonov | 43b4b9c | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 182 | # Architectures supported by Sanitizer runtimes. Specific sanitizers may |
| 183 | # support only subset of these (e.g. TSan works on x86_64 only). |
| 184 | filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH |
Alexey Samsonov | b957d9f | 2013-06-07 09:44:43 +0000 | [diff] [blame] | 185 | x86_64 i386 powerpc64) |
Alexey Samsonov | 43b4b9c | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 186 | |
Chandler Carruth | d865fec | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 187 | # Add the public header's directory to the includes for all of compiler-rt. |
| 188 | include_directories(include) |
Alexey Samsonov | 2a529ad | 2013-04-11 15:49:52 +0000 | [diff] [blame] | 189 | add_subdirectory(include) |
| 190 | |
| 191 | set(SANITIZER_COMMON_LIT_TEST_DEPS |
| 192 | clang clang-headers FileCheck count not llvm-nm llvm-symbolizer |
| 193 | compiler-rt-headers) |
Alexey Samsonov | 38a61aa | 2013-08-29 10:49:04 +0000 | [diff] [blame^] | 194 | # Check code style when running lit tests for sanitizers. |
| 195 | if(UNIX) |
| 196 | list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck) |
| 197 | endif() |
Chandler Carruth | d865fec | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 198 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 199 | add_subdirectory(lib) |
| 200 | |
| 201 | if(LLVM_INCLUDE_TESTS) |
Chandler Carruth | 2e82a8b | 2012-06-22 20:17:27 +0000 | [diff] [blame] | 202 | # Currently the tests have not been ported to CMake, so disable this |
| 203 | # directory. |
| 204 | # |
| 205 | #add_subdirectory(test) |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 206 | endif() |