blob: 04d6e9763bf8cb3e87807646d5e5246ba0a09327 [file] [log] [blame]
Chandler Carruth1f5d5c02012-04-04 22:12:04 +00001# 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
10include(LLVMParseArguments)
11
Chandler Carruthc78ad002012-06-25 08:40:10 +000012# 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.
16cmake_minimum_required(VERSION 2.8.8)
17
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000018# Add path for custom modules
19set(CMAKE_MODULE_PATH
20 ${CMAKE_MODULE_PATH}
21 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
22 )
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000023include(AddCompilerRT)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000024
25set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
26
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000027# Detect whether the current target platform is 32-bit or 64-bit, and setup
28# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonov36796b42012-08-07 12:14:29 +000029if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000030 set(TARGET_64_BIT_CFLAGS "-m64")
31 set(TARGET_32_BIT_CFLAGS "")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000032else()
33 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
34 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
35 endif()
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000036 set(TARGET_64_BIT_CFLAGS "")
37 set(TARGET_32_BIT_CFLAGS "-m32")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000038endif()
39
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000040# FIXME: Below we assume that the target build of LLVM/Clang is x86, which is
41# not at all valid. Much of this can be fixed just by switching to use
42# a just-built-clang binary for the compiles.
43
44set(TARGET_x86_64_CFLAGS ${TARGET_64_BIT_CFLAGS})
45set(TARGET_i386_CFLAGS ${TARGET_32_BIT_CFLAGS})
46
47set(COMPILER_RT_SUPPORTED_ARCH
48 x86_64 i386)
49
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000050function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000051 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
52 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000053 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000054 else()
55 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000056 endif()
57endfunction()
58
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000059# Try to compile a very simple source file to ensure we can target the given
60# platform. We use the results of these tests to build only the various target
61# runtime libraries supported by our current compilers cross-compiling
62# abilities.
63set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c)
64file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}")
Alexey Samsonov193b45f2013-01-18 12:45:44 +000065try_compile(CAN_TARGET_x86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64}
66 COMPILE_DEFINITIONS "${TARGET_x86_64_CFLAGS}"
67 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_x86_64_CFLAGS}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000068
69set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c)
70file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}")
Alexey Samsonov193b45f2013-01-18 12:45:44 +000071try_compile(CAN_TARGET_i386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32}
72 COMPILE_DEFINITIONS "${TARGET_i386_CFLAGS}"
73 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_i386_CFLAGS}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000074
Alexey Samsonovc20f5d22012-12-27 13:19:23 +000075# We only support running instrumented tests when we're not cross compiling
76# and target a unix-like system. On Android we define the rules for building
77# unit tests, but don't execute them.
78if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
79 set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE)
80else()
81 set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE)
82endif()
83
Alexey Samsonovfb844c72012-08-10 14:45:52 +000084function(filter_available_targets out_var)
85 set(archs)
86 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000087 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
88 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +000089 list(APPEND archs ${arch})
90 endif()
91 endforeach()
92 set(${out_var} ${archs} PARENT_SCOPE)
93endfunction()
94
Chandler Carruthc1c9d582012-08-29 00:13:11 +000095# Provide some common commmandline flags for Sanitizer runtimes.
96set(SANITIZER_COMMON_CFLAGS
97 -fPIC
Alexey Samsonovd83ccd02012-09-05 09:00:03 +000098 -fno-builtin
Chandler Carruthc1c9d582012-08-29 00:13:11 +000099 -fno-exceptions
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000100 -fomit-frame-pointer
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000101 -funwind-tables
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000102 -O3
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000103 )
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000104if(NOT WIN32)
105 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
106endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000107# Build sanitizer runtimes with debug info.
108check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
109if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
110 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
111else()
112 list(APPEND SANITIZER_COMMON_CFLAGS -g)
113endif()
114# Warnings suppressions.
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000115check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000116if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
117 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
118endif()
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000119check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000120if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
121 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
122endif()
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000123if(APPLE)
124 list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5)
125endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000126
Alexey Samsonovb0684832013-01-18 16:51:07 +0000127# Architectures supported by Sanitizer runtimes. Specific sanitizers may
128# support only subset of these (e.g. TSan works on x86_64 only).
129filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
130 x86_64 i386)
131
Alexey Samsonoveeec3c12012-09-11 10:26:54 +0000132# Compute the Clang version from the LLVM version.
133# FIXME: We should be able to reuse CLANG_VERSION variable calculated
134# in Clang cmake files, instead of copying the rules here.
135string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
136 ${PACKAGE_VERSION})
137# Setup the paths where compiler-rt runtimes and headers should be stored.
Evgeniy Stepanovaf0f6c82012-09-11 11:35:18 +0000138set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
Alexey Samsonoveeec3c12012-09-11 10:26:54 +0000139string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
140
141# Install compiler-rt headers.
142install(DIRECTORY include/
143 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
144 FILES_MATCHING
145 PATTERN "*.h"
146 PATTERN ".svn" EXCLUDE
147 )
148
149# Call add_clang_compiler_rt_libraries to make sure that targets are built
150# and installed in the directories where Clang driver expects to find them.
151macro(add_clang_compiler_rt_libraries)
152 # Setup output directories so that clang in build tree works.
153 set_target_properties(${ARGN} PROPERTIES
154 ARCHIVE_OUTPUT_DIRECTORY
155 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
156 LIBRARY_OUTPUT_DIRECTORY
157 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
158 )
159 # Add installation command.
160 install(TARGETS ${ARGN}
161 ARCHIVE DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
162 LIBRARY DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
163 )
164endmacro(add_clang_compiler_rt_libraries)
165
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000166# Add the public header's directory to the includes for all of compiler-rt.
167include_directories(include)
168
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000169add_subdirectory(lib)
170
171if(LLVM_INCLUDE_TESTS)
Chandler Carruth84ba6802012-06-22 20:17:27 +0000172 # Currently the tests have not been ported to CMake, so disable this
173 # directory.
174 #
175 #add_subdirectory(test)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000176endif()