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) |
| 26 | set(COMPILER_RT_LIBRARY_OUTPUT_DIR |
| 27 | ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}) |
| 28 | set(COMPILER_RT_LIBRARY_INSTALL_DIR |
| 29 | ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}) |
| 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}) |
| 39 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 40 | # Detect whether the current target platform is 32-bit or 64-bit, and setup |
| 41 | # the correct commandline flags needed to attempt to target 32-bit and 64-bit. |
Alexey Samsonov | bd17004 | 2012-08-07 12:14:29 +0000 | [diff] [blame] | 42 | if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 43 | set(TARGET_64_BIT_CFLAGS "-m64") |
| 44 | set(TARGET_32_BIT_CFLAGS "") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 45 | else() |
| 46 | if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 47 | message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.") |
| 48 | endif() |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 49 | set(TARGET_64_BIT_CFLAGS "") |
| 50 | set(TARGET_32_BIT_CFLAGS "-m32") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 51 | endif() |
| 52 | |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 53 | # List of architectures we can target. |
| 54 | set(COMPILER_RT_SUPPORTED_ARCH) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 55 | |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 56 | function(get_target_flags_for_arch arch out_var) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 57 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 58 | if(ARCH_INDEX EQUAL -1) |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 59 | message(FATAL_ERROR "Unsupported architecture: ${arch}") |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 60 | else() |
| 61 | set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE) |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 62 | endif() |
| 63 | endfunction() |
| 64 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 65 | # Try to compile a very simple source file to ensure we can target the given |
| 66 | # platform. We use the results of these tests to build only the various target |
| 67 | # runtime libraries supported by our current compilers cross-compiling |
| 68 | # abilities. |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 69 | set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c) |
| 70 | file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 71 | |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 72 | # test_target_arch(<arch> <target flags...>) |
| 73 | # Sets the target flags for a given architecture and determines if this |
| 74 | # architecture is supported by trying to build a simple file. |
| 75 | macro(test_target_arch arch) |
| 76 | set(TARGET_${arch}_CFLAGS ${ARGN}) |
| 77 | try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE} |
| 78 | COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}" |
| 79 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}") |
| 80 | if(${CAN_TARGET_${arch}}) |
| 81 | list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch}) |
| 82 | endif() |
| 83 | endmacro() |
| 84 | |
| 85 | if("${LLVM_NATIVE_ARCH}" STREQUAL "X86") |
| 86 | test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS}) |
| 87 | test_target_arch(i386 ${TARGET_32_BIT_CFLAGS}) |
| 88 | elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC") |
| 89 | # Explicitly set -m flag on powerpc, because on ppc64 defaults for gcc and |
| 90 | # clang are different. |
| 91 | test_target_arch(powerpc64 "-m64") |
| 92 | test_target_arch(powerpc "-m32") |
| 93 | endif() |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 94 | |
Alexey Samsonov | d7d7b5f | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 95 | # We only support running instrumented tests when we're not cross compiling |
| 96 | # and target a unix-like system. On Android we define the rules for building |
| 97 | # unit tests, but don't execute them. |
| 98 | if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) |
| 99 | set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE) |
| 100 | else() |
| 101 | set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE) |
| 102 | endif() |
| 103 | |
Alexey Samsonov | fe51abb | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 104 | function(filter_available_targets out_var) |
| 105 | set(archs) |
| 106 | foreach(arch ${ARGN}) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 107 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 108 | if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch}) |
Alexey Samsonov | fe51abb | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 109 | list(APPEND archs ${arch}) |
| 110 | endif() |
| 111 | endforeach() |
| 112 | set(${out_var} ${archs} PARENT_SCOPE) |
| 113 | endfunction() |
| 114 | |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 115 | # Provide some common commmandline flags for Sanitizer runtimes. |
| 116 | set(SANITIZER_COMMON_CFLAGS |
| 117 | -fPIC |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 118 | -fno-builtin |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 119 | -fno-exceptions |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 120 | -fomit-frame-pointer |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 121 | -funwind-tables |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 122 | -O3 |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 123 | ) |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 124 | if(NOT WIN32) |
| 125 | list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden) |
| 126 | endif() |
Alexey Samsonov | 275ca01 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 127 | # Build sanitizer runtimes with debug info. |
| 128 | check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 129 | if(SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 130 | list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) |
| 131 | else() |
| 132 | list(APPEND SANITIZER_COMMON_CFLAGS -g) |
| 133 | endif() |
| 134 | # Warnings suppressions. |
Alexey Samsonov | 721460b | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 135 | check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 136 | if(SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
| 137 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros) |
| 138 | endif() |
Alexey Samsonov | 721460b | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 139 | check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 140 | if(SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
| 141 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions) |
| 142 | endif() |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 143 | if(APPLE) |
| 144 | list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5) |
| 145 | endif() |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 146 | |
Alexey Samsonov | 43b4b9c | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 147 | # Architectures supported by Sanitizer runtimes. Specific sanitizers may |
| 148 | # support only subset of these (e.g. TSan works on x86_64 only). |
| 149 | filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH |
Alexey Samsonov | d0b1d46 | 2013-01-21 14:31:45 +0000 | [diff] [blame] | 150 | x86_64 i386 powerpc64 powerpc) |
Alexey Samsonov | 43b4b9c | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 151 | |
Evgeniy Stepanov | bc2b5ba | 2013-01-29 11:46:06 +0000 | [diff] [blame] | 152 | file(GLOB_RECURSE COMPILER_RT_HEADERS |
| 153 | RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include" |
| 154 | "include/*.h") |
| 155 | |
| 156 | set(output_dir ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/include) |
| 157 | |
| 158 | if(MSVC_IDE OR XCODE) |
| 159 | set(other_output_dir ${LLVM_BINARY_DIR}/bin/lib/clang/${CLANG_VERSION}/include) |
| 160 | endif() |
| 161 | |
| 162 | # Copy compiler-rt headers to the build tree. |
| 163 | set(out_files) |
| 164 | foreach( f ${COMPILER_RT_HEADERS} ) |
| 165 | set( src ${CMAKE_CURRENT_SOURCE_DIR}/include/${f} ) |
| 166 | set( dst ${output_dir}/${f} ) |
| 167 | add_custom_command(OUTPUT ${dst} |
| 168 | DEPENDS ${src} |
| 169 | COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst} |
| 170 | COMMENT "Copying compiler-rt's ${f}...") |
| 171 | list(APPEND out_files ${dst}) |
| 172 | |
| 173 | if(other_output_dir) |
| 174 | set(other_dst ${other_output_dir}/${f}) |
| 175 | add_custom_command(OUTPUT ${other_dst} |
| 176 | DEPENDS ${src} |
| 177 | COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${other_dst} |
| 178 | COMMENT "Copying compiler-rt's ${f}...") |
| 179 | list(APPEND out_files ${other_dst}) |
| 180 | endif() |
| 181 | endforeach( f ) |
| 182 | |
| 183 | add_custom_target(compiler-rt-headers ALL DEPENDS ${out_files}) |
| 184 | |
Alexey Samsonov | 70a7095 | 2012-09-11 10:26:54 +0000 | [diff] [blame] | 185 | # Install compiler-rt headers. |
| 186 | install(DIRECTORY include/ |
| 187 | DESTINATION ${LIBCLANG_INSTALL_PATH}/include |
| 188 | FILES_MATCHING |
| 189 | PATTERN "*.h" |
| 190 | PATTERN ".svn" EXCLUDE |
| 191 | ) |
| 192 | |
Chandler Carruth | d865fec | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 193 | # Add the public header's directory to the includes for all of compiler-rt. |
| 194 | include_directories(include) |
| 195 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 196 | add_subdirectory(lib) |
| 197 | |
| 198 | if(LLVM_INCLUDE_TESTS) |
Chandler Carruth | 2e82a8b | 2012-06-22 20:17:27 +0000 | [diff] [blame] | 199 | # Currently the tests have not been ported to CMake, so disable this |
| 200 | # directory. |
| 201 | # |
| 202 | #add_subdirectory(test) |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 203 | endif() |