blob: 455f33c7a82c6f27804c6acd415dadc5bf5882ab [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 Samsonov20abdf62013-10-01 12:52:15 +000018# Top level target used to build all compiler-rt libraries.
19add_custom_target(compiler-rt)
20
Alexey Samsonov8c2cd862013-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 Samsonov4c17c1b2013-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 Samsonov8c2cd862013-01-20 13:58:10 +000031set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000032 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000033
Alexey Samsonovca7fcf22012-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 Samsonov163ab9d2013-01-18 16:05:21 +000039include(AddCompilerRT)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000040
41set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonov6a65b182013-06-06 12:35:48 +000042set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov4c17c1b2013-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 Samsonovacab30e2013-08-27 15:08:02 +000046include(SanitizerUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000047
Chandler Carruth1f5d5c02012-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 Samsonovdd8872b2013-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 Carruth1f5d5c02012-04-04 22:12:04 +000053endif()
Hans Wennborg67c6e502013-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 Carruth1f5d5c02012-04-04 22:12:04 +000061
Alexey Samsonovb6700182013-01-21 14:31:45 +000062# List of architectures we can target.
63set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000064
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000065function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000066 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
67 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000068 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000069 else()
70 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000071 endif()
72endfunction()
73
Chandler Carruth1f5d5c02012-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 Samsonovb6700182013-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 Carruth1f5d5c02012-04-04 22:12:04 +000080
Alexey Samsonovb6700182013-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 Wennborg67c6e502013-08-27 01:24:01 +000095 if (NOT MSVC)
96 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
97 endif()
Alexey Samsonovb6700182013-01-21 14:31:45 +000098 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
99elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
Alexey Samsonovdd8872b2013-06-30 16:21:32 +0000100 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
Renato Golin66e2b702014-01-31 14:25:58 +0000101elseif("${LLVM_NATIVE_ARCH}" STREQUAL "ARM")
102 test_target_arch(arm "")
Alexey Samsonovb6700182013-01-21 14:31:45 +0000103endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000104
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000105# We only support running instrumented tests when we're not cross compiling
106# and target a unix-like system. On Android we define the rules for building
107# unit tests, but don't execute them.
108if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000109 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000110else()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000111 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000112endif()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000113
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000114# Check if compiler-rt is built with libc++.
115find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
116 COMPILER_RT_USES_LIBCXX)
117
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000118function(filter_available_targets out_var)
119 set(archs)
120 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000121 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
122 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000123 list(APPEND archs ${arch})
124 endif()
125 endforeach()
126 set(${out_var} ${archs} PARENT_SCOPE)
127endfunction()
128
Peter Collingbournecbdea322013-10-25 23:03:34 +0000129option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
130
131# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
132pythonize_bool(COMPILER_RT_DEBUG)
133
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000134# Provide some common commmandline flags for Sanitizer runtimes.
Hans Wennborg67c6e502013-08-27 01:24:01 +0000135if (NOT MSVC)
136 set(SANITIZER_COMMON_CFLAGS
137 -fPIC
138 -fno-builtin
139 -fno-exceptions
140 -fomit-frame-pointer
141 -funwind-tables
142 -fno-stack-protector
143 -Wno-gnu # Variadic macros with 0 arguments for ...
Hans Wennborg67c6e502013-08-27 01:24:01 +0000144 -fvisibility=hidden
145 )
Peter Collingbournecbdea322013-10-25 23:03:34 +0000146 if (NOT COMPILER_RT_DEBUG)
147 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
148 endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000149else()
Hans Wennborg67c6e502013-08-27 01:24:01 +0000150 set(SANITIZER_COMMON_CFLAGS
151 /MT
152 /Zi
Timur Iskhodzhanov00aa75b2014-01-30 19:48:13 +0000153 /FS
Hans Wennborgf8e56272013-08-28 16:14:59 +0000154 /Oy-
Hans Wennborg67c6e502013-08-27 01:24:01 +0000155 /GS-
Timur Iskhodzhanov00aa75b2014-01-30 19:48:13 +0000156 /wd4221
Hans Wennborg67c6e502013-08-27 01:24:01 +0000157 /wd4722
158 )
159endif()
160# Build sanitizer runtimes with debug info. (MSVC gets /Zi above)
161if (NOT MSVC)
162 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
Peter Collingbournecbdea322013-10-25 23:03:34 +0000163 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
Hans Wennborg67c6e502013-08-27 01:24:01 +0000164 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
165 else()
166 list(APPEND SANITIZER_COMMON_CFLAGS -g)
167 endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000168endif()
169# Warnings suppressions.
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000170check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000171if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
172 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
173endif()
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000174check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000175if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
176 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
177endif()
Alexey Samsonovacfb82e2013-03-25 10:31:49 +0000178# Sanitizer may not have libstdc++, so we can have problems with virtual
179# destructors.
180check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
181if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
182 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
183endif()
Alexey Samsonov06379b32013-08-29 12:08:36 +0000184check_cxx_compiler_flag(-Wglobal-constructors SUPPORTS_GLOBAL_CONSTRUCTORS_FLAG)
185# Not all sanitizers forbid global constructors.
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 )
194 set(SANITIZER_COMMON_SUPPORTED_DARWIN_OS osx)
195 if (IOSSIM_SDK_DIR)
196 list(APPEND SANITIZER_COMMON_SUPPORTED_DARWIN_OS iossim)
197 endif()
198
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000199 if(COMPILER_RT_USES_LIBCXX)
200 set(SANITIZER_MIN_OSX_VERSION 10.7)
201 else()
Alexey Samsonoveb797322013-07-16 11:54:40 +0000202 set(SANITIZER_MIN_OSX_VERSION 10.6)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000203 endif()
Alexander Potapenko49034e32013-11-07 10:08:19 +0000204 set(DARWIN_osx_CFLAGS -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
205 set(DARWIN_iossim_CFLAGS
206 -mios-simulator-version-min=7.0 -isysroot ${IOSSIM_SDK_DIR})
207 set(DARWIN_osx_LINKFLAGS)
208 set(DARWIN_iossim_LINKFLAGS
209 -Wl,-ios_simulator_version_min,7.0.0
210 -mios-simulator-version-min=7.0
Alexander Potapenko2b002892013-11-19 14:58:42 +0000211 -isysroot ${IOSSIM_SDK_DIR})
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000212endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000213
Alexey Samsonovb0684832013-01-18 16:51:07 +0000214# Architectures supported by Sanitizer runtimes. Specific sanitizers may
215# support only subset of these (e.g. TSan works on x86_64 only).
216filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Renato Golin66e2b702014-01-31 14:25:58 +0000217 x86_64 i386 powerpc64 arm)
Alexey Samsonovb0684832013-01-18 16:51:07 +0000218
Alexey Samsonov9f3938e2013-04-11 15:49:52 +0000219add_subdirectory(include)
220
221set(SANITIZER_COMMON_LIT_TEST_DEPS
222 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
223 compiler-rt-headers)
Alexey Samsonov5a2f0732013-08-29 10:49:04 +0000224# Check code style when running lit tests for sanitizers.
225if(UNIX)
226 list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck)
227endif()
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000228
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000229add_subdirectory(lib)
230
231if(LLVM_INCLUDE_TESTS)
Chandler Carruth84ba6802012-06-22 20:17:27 +0000232 # Currently the tests have not been ported to CMake, so disable this
233 # directory.
234 #
235 #add_subdirectory(test)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000236endif()