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 | 8c2cd86 | 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 | ca7fcf2 | 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 | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 36 | include(AddCompilerRT) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 37 | |
| 38 | set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 39 | |
Chandler Carruth | 1f5d5c0 | 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 | 36796b4 | 2012-08-07 12:14:29 +0000 | [diff] [blame] | 42 | if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS) |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 43 | set(TARGET_64_BIT_CFLAGS "-m64") |
| 44 | set(TARGET_32_BIT_CFLAGS "") |
Chandler Carruth | 1f5d5c0 | 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 | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 49 | set(TARGET_64_BIT_CFLAGS "") |
| 50 | set(TARGET_32_BIT_CFLAGS "-m32") |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 51 | endif() |
| 52 | |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 53 | # FIXME: Below we assume that the target build of LLVM/Clang is x86, which is |
| 54 | # not at all valid. Much of this can be fixed just by switching to use |
| 55 | # a just-built-clang binary for the compiles. |
| 56 | |
| 57 | set(TARGET_x86_64_CFLAGS ${TARGET_64_BIT_CFLAGS}) |
| 58 | set(TARGET_i386_CFLAGS ${TARGET_32_BIT_CFLAGS}) |
| 59 | |
| 60 | set(COMPILER_RT_SUPPORTED_ARCH |
| 61 | x86_64 i386) |
| 62 | |
Alexey Samsonov | 85bd73d | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 63 | function(get_target_flags_for_arch arch out_var) |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 64 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 65 | if(ARCH_INDEX EQUAL -1) |
Alexey Samsonov | 85bd73d | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 66 | message(FATAL_ERROR "Unsupported architecture: ${arch}") |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 67 | else() |
| 68 | set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE) |
Alexey Samsonov | 85bd73d | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 69 | endif() |
| 70 | endfunction() |
| 71 | |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 72 | # Try to compile a very simple source file to ensure we can target the given |
| 73 | # platform. We use the results of these tests to build only the various target |
| 74 | # runtime libraries supported by our current compilers cross-compiling |
| 75 | # abilities. |
| 76 | set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c) |
| 77 | file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}") |
Alexey Samsonov | 193b45f | 2013-01-18 12:45:44 +0000 | [diff] [blame] | 78 | try_compile(CAN_TARGET_x86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64} |
| 79 | COMPILE_DEFINITIONS "${TARGET_x86_64_CFLAGS}" |
| 80 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_x86_64_CFLAGS}") |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 81 | |
| 82 | set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c) |
| 83 | file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}") |
Alexey Samsonov | 193b45f | 2013-01-18 12:45:44 +0000 | [diff] [blame] | 84 | try_compile(CAN_TARGET_i386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32} |
| 85 | COMPILE_DEFINITIONS "${TARGET_i386_CFLAGS}" |
| 86 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_i386_CFLAGS}") |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 87 | |
Alexey Samsonov | c20f5d2 | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 88 | # We only support running instrumented tests when we're not cross compiling |
| 89 | # and target a unix-like system. On Android we define the rules for building |
| 90 | # unit tests, but don't execute them. |
| 91 | if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) |
| 92 | set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE) |
| 93 | else() |
| 94 | set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE) |
| 95 | endif() |
| 96 | |
Alexey Samsonov | fb844c7 | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 97 | function(filter_available_targets out_var) |
| 98 | set(archs) |
| 99 | foreach(arch ${ARGN}) |
Alexey Samsonov | 4b0ee8e | 2013-01-18 13:10:42 +0000 | [diff] [blame] | 100 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 101 | if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch}) |
Alexey Samsonov | fb844c7 | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 102 | list(APPEND archs ${arch}) |
| 103 | endif() |
| 104 | endforeach() |
| 105 | set(${out_var} ${archs} PARENT_SCOPE) |
| 106 | endfunction() |
| 107 | |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 108 | # Provide some common commmandline flags for Sanitizer runtimes. |
| 109 | set(SANITIZER_COMMON_CFLAGS |
| 110 | -fPIC |
Alexey Samsonov | d83ccd0 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 111 | -fno-builtin |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 112 | -fno-exceptions |
Alexey Samsonov | d83ccd0 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 113 | -fomit-frame-pointer |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 114 | -funwind-tables |
Alexey Samsonov | d83ccd0 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 115 | -O3 |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 116 | ) |
Alexey Samsonov | a555b3f | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 117 | if(NOT WIN32) |
| 118 | list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden) |
| 119 | endif() |
Alexey Samsonov | 75fb677 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 120 | # Build sanitizer runtimes with debug info. |
| 121 | check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 122 | if(SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 123 | list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) |
| 124 | else() |
| 125 | list(APPEND SANITIZER_COMMON_CFLAGS -g) |
| 126 | endif() |
| 127 | # Warnings suppressions. |
Alexey Samsonov | 3d53c4e | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 128 | check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 129 | if(SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
| 130 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros) |
| 131 | endif() |
Alexey Samsonov | 3d53c4e | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 132 | check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 133 | if(SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
| 134 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions) |
| 135 | endif() |
Alexey Samsonov | d83ccd0 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 136 | if(APPLE) |
| 137 | list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5) |
| 138 | endif() |
Chandler Carruth | c1c9d58 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 139 | |
Alexey Samsonov | b068483 | 2013-01-18 16:51:07 +0000 | [diff] [blame] | 140 | # Architectures supported by Sanitizer runtimes. Specific sanitizers may |
| 141 | # support only subset of these (e.g. TSan works on x86_64 only). |
| 142 | filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH |
| 143 | x86_64 i386) |
| 144 | |
Alexey Samsonov | eeec3c1 | 2012-09-11 10:26:54 +0000 | [diff] [blame] | 145 | # Install compiler-rt headers. |
| 146 | install(DIRECTORY include/ |
| 147 | DESTINATION ${LIBCLANG_INSTALL_PATH}/include |
| 148 | FILES_MATCHING |
| 149 | PATTERN "*.h" |
| 150 | PATTERN ".svn" EXCLUDE |
| 151 | ) |
| 152 | |
| 153 | # Call add_clang_compiler_rt_libraries to make sure that targets are built |
| 154 | # and installed in the directories where Clang driver expects to find them. |
| 155 | macro(add_clang_compiler_rt_libraries) |
| 156 | # Setup output directories so that clang in build tree works. |
| 157 | set_target_properties(${ARGN} PROPERTIES |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame^] | 158 | ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR} |
| 159 | LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) |
Alexey Samsonov | eeec3c1 | 2012-09-11 10:26:54 +0000 | [diff] [blame] | 160 | # Add installation command. |
| 161 | install(TARGETS ${ARGN} |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame^] | 162 | ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} |
| 163 | LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) |
Alexey Samsonov | eeec3c1 | 2012-09-11 10:26:54 +0000 | [diff] [blame] | 164 | endmacro(add_clang_compiler_rt_libraries) |
| 165 | |
Chandler Carruth | 1aa4fef | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 166 | # Add the public header's directory to the includes for all of compiler-rt. |
| 167 | include_directories(include) |
| 168 | |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 169 | add_subdirectory(lib) |
| 170 | |
| 171 | if(LLVM_INCLUDE_TESTS) |
Chandler Carruth | 84ba680 | 2012-06-22 20:17:27 +0000 | [diff] [blame] | 172 | # Currently the tests have not been ported to CMake, so disable this |
| 173 | # directory. |
| 174 | # |
| 175 | #add_subdirectory(test) |
Chandler Carruth | 1f5d5c0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 176 | endif() |