blob: 97cd61455697302ed541a62b6dfa198bfe1e88a1 [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.
Hans Wennborga97442f2014-02-11 21:46:19 +000016if (NOT MSVC)
17 cmake_minimum_required(VERSION 2.8.8)
18else()
19 # Version 2.8.12.1 is required to build with Visual Studion 2013.
20 cmake_minimum_required(VERSION 2.8.12.1)
21endif()
22
Chandler Carruthc78ad002012-06-25 08:40:10 +000023
Alexey Samsonov20abdf62013-10-01 12:52:15 +000024# Top level target used to build all compiler-rt libraries.
25add_custom_target(compiler-rt)
26
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000027# Compute the Clang version from the LLVM version.
28# FIXME: We should be able to reuse CLANG_VERSION variable calculated
29# in Clang cmake files, instead of copying the rules here.
30string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
31 ${PACKAGE_VERSION})
32# Setup the paths where compiler-rt runtimes and headers should be stored.
33set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
34string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000035set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
36set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000037set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000038 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000039
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000040# Add path for custom modules
41set(CMAKE_MODULE_PATH
42 ${CMAKE_MODULE_PATH}
43 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
44 )
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000045include(AddCompilerRT)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000046
47set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonov6a65b182013-06-06 12:35:48 +000048set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000049# Setup custom SDK sysroots.
50set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
51set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
Alexey Samsonovacab30e2013-08-27 15:08:02 +000052include(SanitizerUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000053
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000054set(COMPILER_RT_EXTRA_ANDROID_HEADERS ${COMPILER_RT_SOURCE_DIR}/third_party/android/include)
55
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000056# Detect whether the current target platform is 32-bit or 64-bit, and setup
57# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovdd8872b2013-06-30 16:21:32 +000058if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
59 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
60 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000061endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +000062if (NOT MSVC)
63 set(TARGET_64_BIT_CFLAGS "-m64")
64 set(TARGET_32_BIT_CFLAGS "-m32")
65else()
66 set(TARGET_64_BIT_CFLAGS "")
67 set(TARGET_32_BIT_CFLAGS "")
68endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000069
Alexey Samsonovb6700182013-01-21 14:31:45 +000070# List of architectures we can target.
71set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000072
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000073function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000074 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
75 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000076 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000077 else()
78 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000079 endif()
80endfunction()
81
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000082# Try to compile a very simple source file to ensure we can target the given
83# platform. We use the results of these tests to build only the various target
84# runtime libraries supported by our current compilers cross-compiling
85# abilities.
Alexey Samsonovb6700182013-01-21 14:31:45 +000086set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
87file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000088
Alexey Samsonovb6700182013-01-21 14:31:45 +000089# test_target_arch(<arch> <target flags...>)
90# Sets the target flags for a given architecture and determines if this
91# architecture is supported by trying to build a simple file.
92macro(test_target_arch arch)
93 set(TARGET_${arch}_CFLAGS ${ARGN})
94 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
95 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
96 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
97 if(${CAN_TARGET_${arch}})
98 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
99 endif()
100endmacro()
101
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000102if(ANDROID_COMMON_FLAGS)
103 test_target_arch(arm_android "${ANDROID_COMMON_FLAGS}")
104else()
105 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
106 if (NOT MSVC)
107 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
108 endif()
109 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
110 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
111 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
112 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "ARM")
113 test_target_arch(arm "")
Hans Wennborg67c6e502013-08-27 01:24:01 +0000114 endif()
Alexey Samsonovb6700182013-01-21 14:31:45 +0000115endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000116
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000117# We only support running instrumented tests when we're not cross compiling
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000118# and target a unix-like system. We can run tests on Android even when we are
119# cross-compiling.
120if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX) OR ANDROID)
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000121 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000122else()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000123 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000124endif()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000125
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000126# Check if compiler-rt is built with libc++.
127find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
128 COMPILER_RT_USES_LIBCXX)
129
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000130function(filter_available_targets out_var)
131 set(archs)
132 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000133 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
134 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000135 list(APPEND archs ${arch})
136 endif()
137 endforeach()
138 set(${out_var} ${archs} PARENT_SCOPE)
139endfunction()
140
Peter Collingbournecbdea322013-10-25 23:03:34 +0000141option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
142
143# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
144pythonize_bool(COMPILER_RT_DEBUG)
145
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000146# Provide some common commmandline flags for Sanitizer runtimes.
Hans Wennborg67c6e502013-08-27 01:24:01 +0000147if (NOT MSVC)
148 set(SANITIZER_COMMON_CFLAGS
149 -fPIC
150 -fno-builtin
151 -fno-exceptions
152 -fomit-frame-pointer
153 -funwind-tables
154 -fno-stack-protector
155 -Wno-gnu # Variadic macros with 0 arguments for ...
Alexey Samsonovd23c7962014-02-04 08:28:17 +0000156 -fvisibility=hidden)
Peter Collingbournecbdea322013-10-25 23:03:34 +0000157 if (NOT COMPILER_RT_DEBUG)
158 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
159 endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000160else()
Hans Wennborg67c6e502013-08-27 01:24:01 +0000161 set(SANITIZER_COMMON_CFLAGS
162 /MT
163 /Zi
Hans Wennborgf8e56272013-08-28 16:14:59 +0000164 /Oy-
Hans Wennborg67c6e502013-08-27 01:24:01 +0000165 /GS-
166 /wd4722
167 )
168endif()
169# Build sanitizer runtimes with debug info. (MSVC gets /Zi above)
170if (NOT MSVC)
171 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
Peter Collingbournecbdea322013-10-25 23:03:34 +0000172 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
Hans Wennborg67c6e502013-08-27 01:24:01 +0000173 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
174 else()
175 list(APPEND SANITIZER_COMMON_CFLAGS -g)
176 endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000177endif()
Alexey Samsonovd23c7962014-02-04 08:28:17 +0000178# Build sanitizer runtimes with -fno-function-sections.
Alexey Samsonovfafbcb62014-02-04 13:24:13 +0000179check_cxx_compiler_flag("-Werror -fno-function-sections" SUPPORTS_FNO_FUNCTION_SECTIONS_FLAG)
Alexey Samsonovd23c7962014-02-04 08:28:17 +0000180if(SUPPORTS_FNO_FUNCTION_SECTIONS_FLAG)
181 list(APPEND SANITIZER_COMMON_CFLAGS -fno-function-sections)
182endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000183# Warnings suppressions.
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000184check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000185if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
186 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
187endif()
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000188check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000189if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
190 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
191endif()
Alexey Samsonovacfb82e2013-03-25 10:31:49 +0000192# Sanitizer may not have libstdc++, so we can have problems with virtual
193# destructors.
194check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
195if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
196 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
197endif()
Alexey Samsonov06379b32013-08-29 12:08:36 +0000198check_cxx_compiler_flag(-Wglobal-constructors SUPPORTS_GLOBAL_CONSTRUCTORS_FLAG)
199# Not all sanitizers forbid global constructors.
Alexey Samsonov167c15a2014-02-12 07:26:40 +0000200check_cxx_compiler_flag("-Werror -Wframe-larger-than=512"
201 SUPPORTS_FRAME_LARGER_THAN_FLAG)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000202
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000203if(APPLE)
Alexander Potapenko49034e32013-11-07 10:08:19 +0000204 # Obtain the iOS Simulator SDK path from xcodebuild.
205 execute_process(
206 COMMAND xcodebuild -version -sdk iphonesimulator Path
207 OUTPUT_VARIABLE IOSSIM_SDK_DIR
208 OUTPUT_STRIP_TRAILING_WHITESPACE
209 )
210 set(SANITIZER_COMMON_SUPPORTED_DARWIN_OS osx)
211 if (IOSSIM_SDK_DIR)
212 list(APPEND SANITIZER_COMMON_SUPPORTED_DARWIN_OS iossim)
213 endif()
214
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000215 if(COMPILER_RT_USES_LIBCXX)
216 set(SANITIZER_MIN_OSX_VERSION 10.7)
217 else()
Alexey Samsonoveb797322013-07-16 11:54:40 +0000218 set(SANITIZER_MIN_OSX_VERSION 10.6)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000219 endif()
Alexander Potapenko49034e32013-11-07 10:08:19 +0000220 set(DARWIN_osx_CFLAGS -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
221 set(DARWIN_iossim_CFLAGS
222 -mios-simulator-version-min=7.0 -isysroot ${IOSSIM_SDK_DIR})
223 set(DARWIN_osx_LINKFLAGS)
224 set(DARWIN_iossim_LINKFLAGS
225 -Wl,-ios_simulator_version_min,7.0.0
226 -mios-simulator-version-min=7.0
Alexander Potapenko2b002892013-11-19 14:58:42 +0000227 -isysroot ${IOSSIM_SDK_DIR})
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000228endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000229
Alexey Samsonovb0684832013-01-18 16:51:07 +0000230# Architectures supported by Sanitizer runtimes. Specific sanitizers may
231# support only subset of these (e.g. TSan works on x86_64 only).
232filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Renato Golin66e2b702014-01-31 14:25:58 +0000233 x86_64 i386 powerpc64 arm)
Alexey Samsonovb0684832013-01-18 16:51:07 +0000234
Alexey Samsonov9f3938e2013-04-11 15:49:52 +0000235add_subdirectory(include)
236
Evgeniy Stepanovaa9d74c2014-02-14 09:22:10 +0000237# When ANDROID, we build tests with the host compiler (i.e. CMAKE_C_COMPILER),
238# and run tests with tools from the host toolchain.
239if (NOT ANDROID)
240 set(SANITIZER_COMMON_LIT_TEST_DEPS
241 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
242 compiler-rt-headers)
243 if(UNIX)
244 list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck)
245 endif()
Alexey Samsonov5a2f0732013-08-29 10:49:04 +0000246endif()
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000247
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000248add_subdirectory(lib)
249
250if(LLVM_INCLUDE_TESTS)
Alexey Samsonov81a2b462014-02-14 11:00:07 +0000251 add_subdirectory(test)
252 add_subdirectory(unittests)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000253endif()