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 | 02dcc63 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 18 | # Add path for custom modules |
| 19 | set(CMAKE_MODULE_PATH |
| 20 | ${CMAKE_MODULE_PATH} |
| 21 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
| 22 | ) |
| 23 | |
| 24 | set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 25 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 26 | # Detect whether the current target platform is 32-bit or 64-bit, and setup |
| 27 | # 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] | 28 | if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame^] | 29 | set(TARGET_64_BIT_CFLAGS "-m64") |
| 30 | set(TARGET_32_BIT_CFLAGS "") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 31 | else() |
| 32 | if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 33 | message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.") |
| 34 | endif() |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame^] | 35 | set(TARGET_64_BIT_CFLAGS "") |
| 36 | set(TARGET_32_BIT_CFLAGS "-m32") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 37 | endif() |
| 38 | |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame^] | 39 | # FIXME: Below we assume that the target build of LLVM/Clang is x86, which is |
| 40 | # not at all valid. Much of this can be fixed just by switching to use |
| 41 | # a just-built-clang binary for the compiles. |
| 42 | |
| 43 | set(TARGET_x86_64_CFLAGS ${TARGET_64_BIT_CFLAGS}) |
| 44 | set(TARGET_i386_CFLAGS ${TARGET_32_BIT_CFLAGS}) |
| 45 | |
| 46 | set(COMPILER_RT_SUPPORTED_ARCH |
| 47 | x86_64 i386) |
| 48 | |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 49 | function(get_target_flags_for_arch arch out_var) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame^] | 50 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 51 | if(ARCH_INDEX EQUAL -1) |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 52 | message(FATAL_ERROR "Unsupported architecture: ${arch}") |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame^] | 53 | else() |
| 54 | set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE) |
Alexey Samsonov | bf23186 | 2012-12-19 15:17:23 +0000 | [diff] [blame] | 55 | endif() |
| 56 | endfunction() |
| 57 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 58 | # Try to compile a very simple source file to ensure we can target the given |
| 59 | # platform. We use the results of these tests to build only the various target |
| 60 | # runtime libraries supported by our current compilers cross-compiling |
| 61 | # abilities. |
| 62 | set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c) |
| 63 | file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}") |
Alexey Samsonov | 2f3aef0 | 2013-01-18 12:45:44 +0000 | [diff] [blame] | 64 | try_compile(CAN_TARGET_x86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64} |
| 65 | COMPILE_DEFINITIONS "${TARGET_x86_64_CFLAGS}" |
| 66 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_x86_64_CFLAGS}") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 67 | |
| 68 | set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c) |
| 69 | file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}") |
Alexey Samsonov | 2f3aef0 | 2013-01-18 12:45:44 +0000 | [diff] [blame] | 70 | try_compile(CAN_TARGET_i386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32} |
| 71 | COMPILE_DEFINITIONS "${TARGET_i386_CFLAGS}" |
| 72 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_i386_CFLAGS}") |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 73 | |
Alexey Samsonov | d7d7b5f | 2012-12-27 13:19:23 +0000 | [diff] [blame] | 74 | # We only support running instrumented tests when we're not cross compiling |
| 75 | # and target a unix-like system. On Android we define the rules for building |
| 76 | # unit tests, but don't execute them. |
| 77 | if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) |
| 78 | set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE) |
| 79 | else() |
| 80 | set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE) |
| 81 | endif() |
| 82 | |
Alexey Samsonov | fe51abb | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 83 | function(filter_available_targets out_var) |
| 84 | set(archs) |
| 85 | foreach(arch ${ARGN}) |
Alexey Samsonov | 3442118 | 2013-01-18 13:10:42 +0000 | [diff] [blame^] | 86 | list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) |
| 87 | if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch}) |
Alexey Samsonov | fe51abb | 2012-08-10 14:45:52 +0000 | [diff] [blame] | 88 | list(APPEND archs ${arch}) |
| 89 | endif() |
| 90 | endforeach() |
| 91 | set(${out_var} ${archs} PARENT_SCOPE) |
| 92 | endfunction() |
| 93 | |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 94 | # Provide some common commmandline flags for Sanitizer runtimes. |
| 95 | set(SANITIZER_COMMON_CFLAGS |
| 96 | -fPIC |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 97 | -fno-builtin |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 98 | -fno-exceptions |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 99 | -fomit-frame-pointer |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 100 | -funwind-tables |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 101 | -O3 |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 102 | ) |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 103 | if(NOT WIN32) |
| 104 | list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden) |
| 105 | endif() |
Alexey Samsonov | 275ca01 | 2012-11-08 14:49:28 +0000 | [diff] [blame] | 106 | # Build sanitizer runtimes with debug info. |
| 107 | check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 108 | if(SUPPORTS_GLINE_TABLES_ONLY_FLAG) |
| 109 | list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only) |
| 110 | else() |
| 111 | list(APPEND SANITIZER_COMMON_CFLAGS -g) |
| 112 | endif() |
| 113 | # Warnings suppressions. |
Alexey Samsonov | 721460b | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 114 | check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 115 | if(SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
| 116 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros) |
| 117 | endif() |
Alexey Samsonov | 721460b | 2012-09-12 08:06:15 +0000 | [diff] [blame] | 118 | check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 119 | if(SUPPORTS_NO_C99_EXTENSIONS_FLAG) |
| 120 | list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions) |
| 121 | endif() |
Alexey Samsonov | 0f7d4a4 | 2012-09-05 09:00:03 +0000 | [diff] [blame] | 122 | if(APPLE) |
| 123 | list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5) |
| 124 | endif() |
Chandler Carruth | 60ab090 | 2012-08-29 00:13:11 +0000 | [diff] [blame] | 125 | |
Chandler Carruth | 984f6cf | 2012-06-27 09:01:24 +0000 | [diff] [blame] | 126 | # Because compiler-rt spends a lot of time setting up custom compile flags, |
| 127 | # define a handy helper function for it. The compile flags setting in CMake |
| 128 | # has serious issues that make its syntax challenging at best. |
| 129 | function(set_target_compile_flags target) |
| 130 | foreach(arg ${ARGN}) |
| 131 | set(argstring "${argstring} ${arg}") |
| 132 | endforeach() |
| 133 | set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}") |
| 134 | endfunction() |
| 135 | |
Evgeniy Stepanov | 34fc56c | 2012-09-11 11:55:45 +0000 | [diff] [blame] | 136 | function(set_target_link_flags target) |
| 137 | foreach(arg ${ARGN}) |
| 138 | set(argstring "${argstring} ${arg}") |
| 139 | endforeach() |
| 140 | set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}") |
| 141 | endfunction() |
| 142 | |
Alexey Samsonov | 70a7095 | 2012-09-11 10:26:54 +0000 | [diff] [blame] | 143 | # Compute the Clang version from the LLVM version. |
| 144 | # FIXME: We should be able to reuse CLANG_VERSION variable calculated |
| 145 | # in Clang cmake files, instead of copying the rules here. |
| 146 | string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION |
| 147 | ${PACKAGE_VERSION}) |
| 148 | # Setup the paths where compiler-rt runtimes and headers should be stored. |
Evgeniy Stepanov | 7e72e45 | 2012-09-11 11:35:18 +0000 | [diff] [blame] | 149 | set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}) |
Alexey Samsonov | 70a7095 | 2012-09-11 10:26:54 +0000 | [diff] [blame] | 150 | string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR) |
| 151 | |
| 152 | # Install compiler-rt headers. |
| 153 | install(DIRECTORY include/ |
| 154 | DESTINATION ${LIBCLANG_INSTALL_PATH}/include |
| 155 | FILES_MATCHING |
| 156 | PATTERN "*.h" |
| 157 | PATTERN ".svn" EXCLUDE |
| 158 | ) |
| 159 | |
| 160 | # Call add_clang_compiler_rt_libraries to make sure that targets are built |
| 161 | # and installed in the directories where Clang driver expects to find them. |
| 162 | macro(add_clang_compiler_rt_libraries) |
| 163 | # Setup output directories so that clang in build tree works. |
| 164 | set_target_properties(${ARGN} PROPERTIES |
| 165 | ARCHIVE_OUTPUT_DIRECTORY |
| 166 | ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR} |
| 167 | LIBRARY_OUTPUT_DIRECTORY |
| 168 | ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR} |
| 169 | ) |
| 170 | # Add installation command. |
| 171 | install(TARGETS ${ARGN} |
| 172 | ARCHIVE DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR} |
| 173 | LIBRARY DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR} |
| 174 | ) |
| 175 | endmacro(add_clang_compiler_rt_libraries) |
| 176 | |
Chandler Carruth | d865fec | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 177 | # Add the public header's directory to the includes for all of compiler-rt. |
| 178 | include_directories(include) |
| 179 | |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 180 | add_subdirectory(lib) |
| 181 | |
| 182 | if(LLVM_INCLUDE_TESTS) |
Chandler Carruth | 2e82a8b | 2012-06-22 20:17:27 +0000 | [diff] [blame] | 183 | # Currently the tests have not been ported to CMake, so disable this |
| 184 | # directory. |
| 185 | # |
| 186 | #add_subdirectory(test) |
Chandler Carruth | d51e0a0 | 2012-04-04 22:12:04 +0000 | [diff] [blame] | 187 | endif() |