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