blob: 123b60ea524e9732b1c4e5fa57d80f2e394917e4 [file] [log] [blame]
Chandler Carruthd51e0a02012-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 Carrutha765ffc2012-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 Samsonovc3494662013-10-01 12:52:15 +000018# Top level target used to build all compiler-rt libraries.
19add_custom_target(compiler-rt)
20
Alexey Samsonove16af952013-01-20 13:58:10 +000021# Compute the Clang version from the LLVM version.
22# FIXME: We should be able to reuse CLANG_VERSION variable calculated
23# in Clang cmake files, instead of copying the rules here.
24string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
25 ${PACKAGE_VERSION})
26# Setup the paths where compiler-rt runtimes and headers should be stored.
27set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
28string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
Alexey Samsonovc9de4512013-03-19 09:17:35 +000029set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
30set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000031set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonovc9de4512013-03-19 09:17:35 +000032 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000033
Alexey Samsonov02dcc632012-12-19 12:33:39 +000034# Add path for custom modules
35set(CMAKE_MODULE_PATH
36 ${CMAKE_MODULE_PATH}
37 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
38 )
Alexey Samsonov392c50d2013-01-18 16:05:21 +000039include(AddCompilerRT)
Alexey Samsonov02dcc632012-12-19 12:33:39 +000040
41set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonovdd6605e2013-06-06 12:35:48 +000042set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonovc9de4512013-03-19 09:17:35 +000043# Setup custom SDK sysroots.
44set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
45set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
Alexey Samsonove5fa2432013-08-27 15:08:02 +000046include(SanitizerUtils)
Alexey Samsonov02dcc632012-12-19 12:33:39 +000047
Chandler Carruthd51e0a02012-04-04 22:12:04 +000048# Detect whether the current target platform is 32-bit or 64-bit, and setup
49# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonov33b21352013-06-30 16:21:32 +000050if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
51 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
52 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000053endif()
Hans Wennborgc1f1af72013-08-27 01:24:01 +000054if (NOT MSVC)
55 set(TARGET_64_BIT_CFLAGS "-m64")
56 set(TARGET_32_BIT_CFLAGS "-m32")
57else()
58 set(TARGET_64_BIT_CFLAGS "")
59 set(TARGET_32_BIT_CFLAGS "")
60endif()
Chandler Carruthd51e0a02012-04-04 22:12:04 +000061
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000062# List of architectures we can target.
63set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov34421182013-01-18 13:10:42 +000064
Alexey Samsonovbf231862012-12-19 15:17:23 +000065function(get_target_flags_for_arch arch out_var)
Alexey Samsonov34421182013-01-18 13:10:42 +000066 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
67 if(ARCH_INDEX EQUAL -1)
Alexey Samsonovbf231862012-12-19 15:17:23 +000068 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov34421182013-01-18 13:10:42 +000069 else()
70 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonovbf231862012-12-19 15:17:23 +000071 endif()
72endfunction()
73
Chandler Carruthd51e0a02012-04-04 22:12:04 +000074# Try to compile a very simple source file to ensure we can target the given
75# platform. We use the results of these tests to build only the various target
76# runtime libraries supported by our current compilers cross-compiling
77# abilities.
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000078set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
79file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000080
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000081# test_target_arch(<arch> <target flags...>)
82# Sets the target flags for a given architecture and determines if this
83# architecture is supported by trying to build a simple file.
84macro(test_target_arch arch)
85 set(TARGET_${arch}_CFLAGS ${ARGN})
86 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
87 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
88 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
89 if(${CAN_TARGET_${arch}})
90 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
91 endif()
92endmacro()
93
94if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
Hans Wennborgc1f1af72013-08-27 01:24:01 +000095 if (NOT MSVC)
96 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
97 endif()
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000098 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
99elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
Alexey Samsonov33b21352013-06-30 16:21:32 +0000100 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
Alexey Samsonovd0b1d462013-01-21 14:31:45 +0000101endif()
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000102
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +0000103# We only support running instrumented tests when we're not cross compiling
104# and target a unix-like system. On Android we define the rules for building
105# unit tests, but don't execute them.
106if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
Michael Gottesmanfd0a7892013-04-01 04:13:03 +0000107 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +0000108else()
Michael Gottesmanfd0a7892013-04-01 04:13:03 +0000109 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +0000110endif()
Michael Gottesmanfd0a7892013-04-01 04:13:03 +0000111
Alexey Samsonov304026d2013-02-08 07:39:25 +0000112# Check if compiler-rt is built with libc++.
113find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
114 COMPILER_RT_USES_LIBCXX)
115
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000116function(filter_available_targets out_var)
117 set(archs)
118 foreach(arch ${ARGN})
Alexey Samsonov34421182013-01-18 13:10:42 +0000119 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
120 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000121 list(APPEND archs ${arch})
122 endif()
123 endforeach()
124 set(${out_var} ${archs} PARENT_SCOPE)
125endfunction()
126
Chandler Carruth60ab0902012-08-29 00:13:11 +0000127# Provide some common commmandline flags for Sanitizer runtimes.
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000128if (NOT MSVC)
129 set(SANITIZER_COMMON_CFLAGS
130 -fPIC
131 -fno-builtin
132 -fno-exceptions
133 -fomit-frame-pointer
134 -funwind-tables
135 -fno-stack-protector
136 -Wno-gnu # Variadic macros with 0 arguments for ...
137 -O3
138 -fvisibility=hidden
139 )
Alexey Samsonov275ca012012-11-08 14:49:28 +0000140else()
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000141 set(SANITIZER_COMMON_CFLAGS
142 /MT
143 /Zi
Hans Wennborg34724132013-08-28 16:14:59 +0000144 /Oy-
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000145 /GS-
146 /wd4722
147 )
148endif()
149# Build sanitizer runtimes with debug info. (MSVC gets /Zi above)
150if (NOT MSVC)
151 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
152 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
153 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
154 else()
155 list(APPEND SANITIZER_COMMON_CFLAGS -g)
156 endif()
Alexey Samsonov275ca012012-11-08 14:49:28 +0000157endif()
158# Warnings suppressions.
Alexey Samsonov721460b2012-09-12 08:06:15 +0000159check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000160if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
161 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
162endif()
Alexey Samsonov721460b2012-09-12 08:06:15 +0000163check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000164if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
165 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
166endif()
Alexey Samsonove374e4e2013-03-25 10:31:49 +0000167# Sanitizer may not have libstdc++, so we can have problems with virtual
168# destructors.
169check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
170if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
171 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
172endif()
Alexey Samsonov0bc4a0b2013-08-29 12:08:36 +0000173check_cxx_compiler_flag(-Wglobal-constructors SUPPORTS_GLOBAL_CONSTRUCTORS_FLAG)
174# Not all sanitizers forbid global constructors.
Alexey Samsonov304026d2013-02-08 07:39:25 +0000175
176# Setup min Mac OS X version.
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000177if(APPLE)
Alexey Samsonov304026d2013-02-08 07:39:25 +0000178 if(COMPILER_RT_USES_LIBCXX)
179 set(SANITIZER_MIN_OSX_VERSION 10.7)
180 else()
Alexey Samsonovb6bde4d2013-07-16 11:54:40 +0000181 set(SANITIZER_MIN_OSX_VERSION 10.6)
Alexey Samsonov304026d2013-02-08 07:39:25 +0000182 endif()
183 list(APPEND SANITIZER_COMMON_CFLAGS
184 -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000185endif()
Chandler Carruth60ab0902012-08-29 00:13:11 +0000186
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000187# Architectures supported by Sanitizer runtimes. Specific sanitizers may
188# support only subset of these (e.g. TSan works on x86_64 only).
189filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Alexey Samsonovb957d9f2013-06-07 09:44:43 +0000190 x86_64 i386 powerpc64)
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000191
Chandler Carruthd865fec2012-08-29 02:27:54 +0000192# Add the public header's directory to the includes for all of compiler-rt.
193include_directories(include)
Alexey Samsonov2a529ad2013-04-11 15:49:52 +0000194add_subdirectory(include)
195
196set(SANITIZER_COMMON_LIT_TEST_DEPS
197 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
198 compiler-rt-headers)
Alexey Samsonov38a61aa2013-08-29 10:49:04 +0000199# Check code style when running lit tests for sanitizers.
200if(UNIX)
201 list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck)
202endif()
Chandler Carruthd865fec2012-08-29 02:27:54 +0000203
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000204add_subdirectory(lib)
205
206if(LLVM_INCLUDE_TESTS)
Chandler Carruth2e82a8b2012-06-22 20:17:27 +0000207 # Currently the tests have not been ported to CMake, so disable this
208 # directory.
209 #
210 #add_subdirectory(test)
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000211endif()