blob: 07186b30f22fc39c98b7fdacdbce1ed658bc2726 [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
Chandler Carruthc78ad002012-06-25 08:40:10 +000010# The CompilerRT build system requires CMake version 2.8.8 or higher in order
11# to use its support for building convenience "libraries" as a collection of
12# .o files. This is particularly useful in producing larger, more complex
13# runtime libraries.
Hans Wennborga97442f2014-02-11 21:46:19 +000014if (NOT MSVC)
15 cmake_minimum_required(VERSION 2.8.8)
16else()
17 # Version 2.8.12.1 is required to build with Visual Studion 2013.
18 cmake_minimum_required(VERSION 2.8.12.1)
19endif()
20
Alexey Samsonov20abdf62013-10-01 12:52:15 +000021# Top level target used to build all compiler-rt libraries.
22add_custom_target(compiler-rt)
23
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000024# Compute the Clang version from the LLVM version.
25# FIXME: We should be able to reuse CLANG_VERSION variable calculated
26# in Clang cmake files, instead of copying the rules here.
27string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
28 ${PACKAGE_VERSION})
29# Setup the paths where compiler-rt runtimes and headers should be stored.
30set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
31string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000032set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
33set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000034set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000035 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000036
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000037# Add path for custom modules
38set(CMAKE_MODULE_PATH
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +000039 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000040 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +000041 ${CMAKE_MODULE_PATH}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000042 )
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +000043include(CompilerRTUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000044
45set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonov6a65b182013-06-06 12:35:48 +000046set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000047# Setup custom SDK sysroots.
48set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
49set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000050
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000051set(COMPILER_RT_EXTRA_ANDROID_HEADERS ${COMPILER_RT_SOURCE_DIR}/third_party/android/include)
52
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000053# Detect whether the current target platform is 32-bit or 64-bit, and setup
54# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovdd8872b2013-06-30 16:21:32 +000055if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
56 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
57 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000058endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +000059if (NOT MSVC)
60 set(TARGET_64_BIT_CFLAGS "-m64")
61 set(TARGET_32_BIT_CFLAGS "-m32")
62else()
63 set(TARGET_64_BIT_CFLAGS "")
64 set(TARGET_32_BIT_CFLAGS "")
65endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000066
Alexey Samsonovb6700182013-01-21 14:31:45 +000067# List of architectures we can target.
68set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000069
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000070function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000071 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
72 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000073 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000074 else()
75 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000076 endif()
77endfunction()
78
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000079# Try to compile a very simple source file to ensure we can target the given
80# platform. We use the results of these tests to build only the various target
81# runtime libraries supported by our current compilers cross-compiling
82# abilities.
Alexey Samsonovb6700182013-01-21 14:31:45 +000083set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
84file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000085
Alexey Samsonovb6700182013-01-21 14:31:45 +000086# test_target_arch(<arch> <target flags...>)
87# Sets the target flags for a given architecture and determines if this
88# architecture is supported by trying to build a simple file.
89macro(test_target_arch arch)
90 set(TARGET_${arch}_CFLAGS ${ARGN})
91 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
92 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
93 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
94 if(${CAN_TARGET_${arch}})
95 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
96 endif()
97endmacro()
98
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +000099if(ANDROID_COMMON_FLAGS)
100 test_target_arch(arm_android "${ANDROID_COMMON_FLAGS}")
101else()
102 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
103 if (NOT MSVC)
104 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
105 endif()
106 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
107 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
108 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
109 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "ARM")
110 test_target_arch(arm "")
Hans Wennborg67c6e502013-08-27 01:24:01 +0000111 endif()
Alexey Samsonovb6700182013-01-21 14:31:45 +0000112endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000113
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000114# We only support running instrumented tests when we're not cross compiling
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000115# and target a unix-like system. We can run tests on Android even when we are
116# cross-compiling.
117if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX) OR ANDROID)
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000118 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000119else()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000120 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000121endif()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000122
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000123# Check if compiler-rt is built with libc++.
124find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
125 COMPILER_RT_USES_LIBCXX)
126
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000127function(filter_available_targets out_var)
128 set(archs)
129 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000130 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
131 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000132 list(APPEND archs ${arch})
133 endif()
134 endforeach()
135 set(${out_var} ${archs} PARENT_SCOPE)
136endfunction()
137
Peter Collingbournecbdea322013-10-25 23:03:34 +0000138option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
Peter Collingbournecbdea322013-10-25 23:03:34 +0000139# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
140pythonize_bool(COMPILER_RT_DEBUG)
141
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000142#================================
143# Setup Compiler Flags
144#================================
145include(config-ix)
146
147macro(append_if list condition var)
148 if (${condition})
149 list(APPEND ${list} ${var})
150 endif()
151endmacro()
152
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000153# Provide some common commmandline flags for Sanitizer runtimes.
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000154append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FPIC_FLAG -fPIC)
155append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin)
156append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions)
157append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer)
158append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables)
159append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector)
160append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden)
161append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections)
162
163append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_MT_FLAG /MT)
164append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_Oy_FLAG /Oy-)
165append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_GS_FLAG /GS-)
166
167# Build with optimization, unless we're in debug mode.
168if(NOT COMPILER_RT_DEBUG)
169 if(MSVC)
170 list(APPEND SANITIZER_COMMON_CFLAGS /O2)
171 else()
Peter Collingbournecbdea322013-10-25 23:03:34 +0000172 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
173 endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +0000174endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000175
176# Build sanitizer runtimes with debug info.
177if(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG)
178 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
179elseif(COMPILER_RT_HAS_G_FLAG)
180 list(APPEND SANITIZER_COMMON_CFLAGS -g)
181elseif(COMPILER_RT_HAS_Zi_FLAG)
182 list(APPEND SANITIZER_COMMON_CFLAGS /Zi)
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000183endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000184
185# Turn off several warnings.
186append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_GNU_FLAG -Wno-gnu)
187append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_VARIADIC_MACROS_FLAG -Wno-variadic-macros)
188append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_C99_EXTENSIONS_FLAG -Wno-c99-extensions)
189append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_NON_VIRTUAL_DTOR_FLAG -Wno-non-virtual-dtor)
190append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WD4722_FLAG /wd4722)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000191
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000192if(APPLE)
Alexander Potapenko49034e32013-11-07 10:08:19 +0000193 # Obtain the iOS Simulator SDK path from xcodebuild.
194 execute_process(
195 COMMAND xcodebuild -version -sdk iphonesimulator Path
196 OUTPUT_VARIABLE IOSSIM_SDK_DIR
197 OUTPUT_STRIP_TRAILING_WHITESPACE
198 )
199 set(SANITIZER_COMMON_SUPPORTED_DARWIN_OS osx)
200 if (IOSSIM_SDK_DIR)
201 list(APPEND SANITIZER_COMMON_SUPPORTED_DARWIN_OS iossim)
202 endif()
203
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000204 if(COMPILER_RT_USES_LIBCXX)
205 set(SANITIZER_MIN_OSX_VERSION 10.7)
206 else()
Alexey Samsonoveb797322013-07-16 11:54:40 +0000207 set(SANITIZER_MIN_OSX_VERSION 10.6)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000208 endif()
Alexander Potapenko49034e32013-11-07 10:08:19 +0000209 set(DARWIN_osx_CFLAGS -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
210 set(DARWIN_iossim_CFLAGS
211 -mios-simulator-version-min=7.0 -isysroot ${IOSSIM_SDK_DIR})
212 set(DARWIN_osx_LINKFLAGS)
213 set(DARWIN_iossim_LINKFLAGS
214 -Wl,-ios_simulator_version_min,7.0.0
215 -mios-simulator-version-min=7.0
Alexander Potapenko2b002892013-11-19 14:58:42 +0000216 -isysroot ${IOSSIM_SDK_DIR})
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000217endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000218
Alexey Samsonovb0684832013-01-18 16:51:07 +0000219# Architectures supported by Sanitizer runtimes. Specific sanitizers may
220# support only subset of these (e.g. TSan works on x86_64 only).
221filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Renato Golin66e2b702014-01-31 14:25:58 +0000222 x86_64 i386 powerpc64 arm)
Alexey Samsonov9f20d672014-02-14 14:06:10 +0000223filter_available_targets(ASAN_SUPPORTED_ARCH x86_64 i386 powerpc64)
Alexey Samsonovf6cf6ab2014-02-14 12:05:41 +0000224filter_available_targets(DFSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonov7eeef852014-02-14 12:26:05 +0000225filter_available_targets(LSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonov8434e602014-02-14 13:02:58 +0000226filter_available_targets(MSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonove6a61832014-02-14 14:35:48 +0000227filter_available_targets(TSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonovf6cf6ab2014-02-14 12:05:41 +0000228filter_available_targets(UBSAN_SUPPORTED_ARCH x86_64 i386)
Alexey Samsonovb0684832013-01-18 16:51:07 +0000229
Alexey Samsonov9f3938e2013-04-11 15:49:52 +0000230add_subdirectory(include)
231
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000232# When ANDROID, we build tests with the host compiler (i.e. CMAKE_C_COMPILER),
233# and run tests with tools from the host toolchain.
234if (NOT ANDROID)
235 set(SANITIZER_COMMON_LIT_TEST_DEPS
236 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
237 compiler-rt-headers)
238 if(UNIX)
239 list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck)
240 endif()
Alexey Samsonov5a2f0732013-08-29 10:49:04 +0000241endif()
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000242
Alexey Samsonov8434e602014-02-14 13:02:58 +0000243set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/projects/libcxx)
244if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/)
245 set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE)
246else()
247 set(COMPILER_RT_HAS_LIBCXX_SOURCES FALSE)
248endif()
249
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000250add_subdirectory(lib)
251
252if(LLVM_INCLUDE_TESTS)
Alexey Samsonov81a2b462014-02-14 11:00:07 +0000253 add_subdirectory(unittests)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000254endif()
Alexey Samsonov9f20d672014-02-14 14:06:10 +0000255add_subdirectory(test)