blob: d9588253f19b8beb72ff15b352614371be4b9a28 [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.
Alexey Samsonov1181a102014-02-18 14:28:53 +000030set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
31string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
32set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
33set(COMPILER_RT_LIBRARY_OUTPUT_DIR
34 ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000035set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonov1181a102014-02-18 14:28:53 +000036 ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000037
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000038# Add path for custom modules
39set(CMAKE_MODULE_PATH
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +000040 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000041 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +000042 ${CMAKE_MODULE_PATH}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000043 )
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +000044include(CompilerRTUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000045
46set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonov6a65b182013-06-06 12:35:48 +000047set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000048# Setup custom SDK sysroots.
49set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
50set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000051
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000052set(COMPILER_RT_EXTRA_ANDROID_HEADERS ${COMPILER_RT_SOURCE_DIR}/third_party/android/include)
53
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000054# Detect whether the current target platform is 32-bit or 64-bit, and setup
55# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovdd8872b2013-06-30 16:21:32 +000056if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
57 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
58 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000059endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +000060if (NOT MSVC)
61 set(TARGET_64_BIT_CFLAGS "-m64")
62 set(TARGET_32_BIT_CFLAGS "-m32")
63else()
64 set(TARGET_64_BIT_CFLAGS "")
65 set(TARGET_32_BIT_CFLAGS "")
66endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000067
Alexey Samsonovb6700182013-01-21 14:31:45 +000068# List of architectures we can target.
69set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000070
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000071function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000072 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
73 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000074 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000075 else()
76 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000077 endif()
78endfunction()
79
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000080# Try to compile a very simple source file to ensure we can target the given
81# platform. We use the results of these tests to build only the various target
82# runtime libraries supported by our current compilers cross-compiling
83# abilities.
Alexey Samsonovb6700182013-01-21 14:31:45 +000084set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
85file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000086
Alexey Samsonovb6700182013-01-21 14:31:45 +000087# test_target_arch(<arch> <target flags...>)
88# Sets the target flags for a given architecture and determines if this
89# architecture is supported by trying to build a simple file.
90macro(test_target_arch arch)
91 set(TARGET_${arch}_CFLAGS ${ARGN})
92 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
93 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
94 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
95 if(${CAN_TARGET_${arch}})
96 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
97 endif()
98endmacro()
99
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000100if(ANDROID_COMMON_FLAGS)
101 test_target_arch(arm_android "${ANDROID_COMMON_FLAGS}")
102else()
103 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
104 if (NOT MSVC)
105 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
106 endif()
107 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
108 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
109 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
110 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "ARM")
111 test_target_arch(arm "")
Hans Wennborg67c6e502013-08-27 01:24:01 +0000112 endif()
Alexey Samsonovb6700182013-01-21 14:31:45 +0000113endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000114
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000115# We only support running instrumented tests when we're not cross compiling
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000116# and target a unix-like system. We can run tests on Android even when we are
117# cross-compiling.
118if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX) OR ANDROID)
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000119 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000120else()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000121 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000122endif()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000123
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000124# Check if compiler-rt is built with libc++.
125find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
126 COMPILER_RT_USES_LIBCXX)
127
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000128function(filter_available_targets out_var)
129 set(archs)
130 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000131 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
132 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000133 list(APPEND archs ${arch})
134 endif()
135 endforeach()
136 set(${out_var} ${archs} PARENT_SCOPE)
137endfunction()
138
Peter Collingbournecbdea322013-10-25 23:03:34 +0000139option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
Peter Collingbournecbdea322013-10-25 23:03:34 +0000140# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
141pythonize_bool(COMPILER_RT_DEBUG)
142
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000143#================================
144# Setup Compiler Flags
145#================================
146include(config-ix)
147
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000148# Provide some common commmandline flags for Sanitizer runtimes.
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000149append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FPIC_FLAG -fPIC)
150append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin)
151append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions)
152append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer)
153append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables)
154append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector)
155append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden)
156append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections)
157
158append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_MT_FLAG /MT)
159append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_Oy_FLAG /Oy-)
160append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_GS_FLAG /GS-)
161
162# Build with optimization, unless we're in debug mode.
163if(NOT COMPILER_RT_DEBUG)
164 if(MSVC)
165 list(APPEND SANITIZER_COMMON_CFLAGS /O2)
166 else()
Peter Collingbournecbdea322013-10-25 23:03:34 +0000167 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
168 endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +0000169endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000170
171# Build sanitizer runtimes with debug info.
172if(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG)
173 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
174elseif(COMPILER_RT_HAS_G_FLAG)
175 list(APPEND SANITIZER_COMMON_CFLAGS -g)
176elseif(COMPILER_RT_HAS_Zi_FLAG)
177 list(APPEND SANITIZER_COMMON_CFLAGS /Zi)
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000178endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000179
180# Turn off several warnings.
181append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_GNU_FLAG -Wno-gnu)
182append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_VARIADIC_MACROS_FLAG -Wno-variadic-macros)
183append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_C99_EXTENSIONS_FLAG -Wno-c99-extensions)
184append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WNO_NON_VIRTUAL_DTOR_FLAG -Wno-non-virtual-dtor)
185append_if(SANITIZER_COMMON_CFLAGS COMPILER_RT_HAS_WD4722_FLAG /wd4722)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000186
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000187if(APPLE)
Alexander Potapenko49034e32013-11-07 10:08:19 +0000188 # Obtain the iOS Simulator SDK path from xcodebuild.
189 execute_process(
190 COMMAND xcodebuild -version -sdk iphonesimulator Path
191 OUTPUT_VARIABLE IOSSIM_SDK_DIR
192 OUTPUT_STRIP_TRAILING_WHITESPACE
193 )
Alexey Samsonov1dbe6e02014-02-18 12:01:24 +0000194 string(REGEX MATCH "-mmacosx-version-min="
195 MACOSX_VERSION_MIN_FLAG "${CMAKE_CXX_FLAGS}")
Alexander Potapenko49034e32013-11-07 10:08:19 +0000196 set(SANITIZER_COMMON_SUPPORTED_DARWIN_OS osx)
Alexey Samsonov1dbe6e02014-02-18 12:01:24 +0000197 if (IOSSIM_SDK_DIR AND NOT MACOSX_VERSION_MIN_FLAG)
Alexander Potapenko49034e32013-11-07 10:08:19 +0000198 list(APPEND SANITIZER_COMMON_SUPPORTED_DARWIN_OS iossim)
199 endif()
200
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000201 if(COMPILER_RT_USES_LIBCXX)
202 set(SANITIZER_MIN_OSX_VERSION 10.7)
203 else()
Alexey Samsonoveb797322013-07-16 11:54:40 +0000204 set(SANITIZER_MIN_OSX_VERSION 10.6)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000205 endif()
Alexander Potapenko49034e32013-11-07 10:08:19 +0000206 set(DARWIN_osx_CFLAGS -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
207 set(DARWIN_iossim_CFLAGS
208 -mios-simulator-version-min=7.0 -isysroot ${IOSSIM_SDK_DIR})
209 set(DARWIN_osx_LINKFLAGS)
210 set(DARWIN_iossim_LINKFLAGS
211 -Wl,-ios_simulator_version_min,7.0.0
212 -mios-simulator-version-min=7.0
Alexander Potapenko2b002892013-11-19 14:58:42 +0000213 -isysroot ${IOSSIM_SDK_DIR})
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000214endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000215
Alexey Samsonovb0684832013-01-18 16:51:07 +0000216# Architectures supported by Sanitizer runtimes. Specific sanitizers may
217# support only subset of these (e.g. TSan works on x86_64 only).
218filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Renato Golin66e2b702014-01-31 14:25:58 +0000219 x86_64 i386 powerpc64 arm)
Alexey Samsonov9f20d672014-02-14 14:06:10 +0000220filter_available_targets(ASAN_SUPPORTED_ARCH x86_64 i386 powerpc64)
Alexey Samsonovf6cf6ab2014-02-14 12:05:41 +0000221filter_available_targets(DFSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonov7eeef852014-02-14 12:26:05 +0000222filter_available_targets(LSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonov8434e602014-02-14 13:02:58 +0000223filter_available_targets(MSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonove6a61832014-02-14 14:35:48 +0000224filter_available_targets(TSAN_SUPPORTED_ARCH x86_64)
Alexey Samsonovf6cf6ab2014-02-14 12:05:41 +0000225filter_available_targets(UBSAN_SUPPORTED_ARCH x86_64 i386)
Alexey Samsonovb0684832013-01-18 16:51:07 +0000226
Alexey Samsonov9f3938e2013-04-11 15:49:52 +0000227add_subdirectory(include)
228
Alexey Samsonov8434e602014-02-14 13:02:58 +0000229set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/projects/libcxx)
230if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/)
231 set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE)
232else()
233 set(COMPILER_RT_HAS_LIBCXX_SOURCES FALSE)
234endif()
235
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000236add_subdirectory(lib)
237
238if(LLVM_INCLUDE_TESTS)
Alexey Samsonov81a2b462014-02-14 11:00:07 +0000239 add_subdirectory(unittests)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000240endif()
Alexey Samsonov9f20d672014-02-14 14:06:10 +0000241add_subdirectory(test)