blob: 70178488d0bf764ba1d39891e23be85ccac91e93 [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 Samsonov8c2cd862013-01-20 13:58:10 +000018# Compute the Clang version from the LLVM version.
19# FIXME: We should be able to reuse CLANG_VERSION variable calculated
20# in Clang cmake files, instead of copying the rules here.
21string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
22 ${PACKAGE_VERSION})
23# Setup the paths where compiler-rt runtimes and headers should be stored.
24set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
25string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000026set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
27set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000028set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000029 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000030
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000031# Add path for custom modules
32set(CMAKE_MODULE_PATH
33 ${CMAKE_MODULE_PATH}
34 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
35 )
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000036include(AddCompilerRT)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000037
38set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonov6a65b182013-06-06 12:35:48 +000039set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov4c17c1b2013-03-19 09:17:35 +000040# Setup custom SDK sysroots.
41set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
42set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
Alexey Samsonovacab30e2013-08-27 15:08:02 +000043include(SanitizerUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000044
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000045# Detect whether the current target platform is 32-bit or 64-bit, and setup
46# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovdd8872b2013-06-30 16:21:32 +000047if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
48 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
49 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000050endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +000051if (NOT MSVC)
52 set(TARGET_64_BIT_CFLAGS "-m64")
53 set(TARGET_32_BIT_CFLAGS "-m32")
54else()
55 set(TARGET_64_BIT_CFLAGS "")
56 set(TARGET_32_BIT_CFLAGS "")
57endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000058
Alexey Samsonovb6700182013-01-21 14:31:45 +000059# List of architectures we can target.
60set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000061
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000062function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000063 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
64 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000065 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000066 else()
67 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000068 endif()
69endfunction()
70
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000071# Try to compile a very simple source file to ensure we can target the given
72# platform. We use the results of these tests to build only the various target
73# runtime libraries supported by our current compilers cross-compiling
74# abilities.
Alexey Samsonovb6700182013-01-21 14:31:45 +000075set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
76file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000077
Alexey Samsonovb6700182013-01-21 14:31:45 +000078# test_target_arch(<arch> <target flags...>)
79# Sets the target flags for a given architecture and determines if this
80# architecture is supported by trying to build a simple file.
81macro(test_target_arch arch)
82 set(TARGET_${arch}_CFLAGS ${ARGN})
83 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
84 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
85 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
86 if(${CAN_TARGET_${arch}})
87 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
88 endif()
89endmacro()
90
91if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
Hans Wennborg67c6e502013-08-27 01:24:01 +000092 if (NOT MSVC)
93 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
94 endif()
Alexey Samsonovb6700182013-01-21 14:31:45 +000095 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
96elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
Alexey Samsonovdd8872b2013-06-30 16:21:32 +000097 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
Alexey Samsonovb6700182013-01-21 14:31:45 +000098endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000099
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000100# We only support running instrumented tests when we're not cross compiling
101# and target a unix-like system. On Android we define the rules for building
102# unit tests, but don't execute them.
103if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000104 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000105else()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000106 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +0000107endif()
Michael Gottesman4ddc2152013-04-01 04:13:03 +0000108
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000109# Check if compiler-rt is built with libc++.
110find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
111 COMPILER_RT_USES_LIBCXX)
112
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000113function(filter_available_targets out_var)
114 set(archs)
115 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000116 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
117 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000118 list(APPEND archs ${arch})
119 endif()
120 endforeach()
121 set(${out_var} ${archs} PARENT_SCOPE)
122endfunction()
123
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000124# Provide some common commmandline flags for Sanitizer runtimes.
Hans Wennborg67c6e502013-08-27 01:24:01 +0000125if (NOT MSVC)
126 set(SANITIZER_COMMON_CFLAGS
127 -fPIC
128 -fno-builtin
129 -fno-exceptions
130 -fomit-frame-pointer
131 -funwind-tables
132 -fno-stack-protector
133 -Wno-gnu # Variadic macros with 0 arguments for ...
134 -O3
135 -fvisibility=hidden
136 )
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000137else()
Hans Wennborg67c6e502013-08-27 01:24:01 +0000138 set(SANITIZER_COMMON_CFLAGS
139 /MT
140 /Zi
141 /GS-
142 /wd4722
143 )
144endif()
145# Build sanitizer runtimes with debug info. (MSVC gets /Zi above)
146if (NOT MSVC)
147 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
148 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
149 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
150 else()
151 list(APPEND SANITIZER_COMMON_CFLAGS -g)
152 endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000153endif()
154# Warnings suppressions.
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000155check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000156if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
157 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
158endif()
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000159check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000160if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
161 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
162endif()
Alexey Samsonovacfb82e2013-03-25 10:31:49 +0000163# Sanitizer may not have libstdc++, so we can have problems with virtual
164# destructors.
165check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
166if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
167 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
168endif()
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000169
170# Setup min Mac OS X version.
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000171if(APPLE)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000172 if(COMPILER_RT_USES_LIBCXX)
173 set(SANITIZER_MIN_OSX_VERSION 10.7)
174 else()
Alexey Samsonoveb797322013-07-16 11:54:40 +0000175 set(SANITIZER_MIN_OSX_VERSION 10.6)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000176 endif()
177 list(APPEND SANITIZER_COMMON_CFLAGS
178 -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000179endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000180
Alexey Samsonovb0684832013-01-18 16:51:07 +0000181# Architectures supported by Sanitizer runtimes. Specific sanitizers may
182# support only subset of these (e.g. TSan works on x86_64 only).
183filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Alexey Samsonov575c5992013-06-07 09:44:43 +0000184 x86_64 i386 powerpc64)
Alexey Samsonovb0684832013-01-18 16:51:07 +0000185
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000186# Add the public header's directory to the includes for all of compiler-rt.
187include_directories(include)
Alexey Samsonov9f3938e2013-04-11 15:49:52 +0000188add_subdirectory(include)
189
190set(SANITIZER_COMMON_LIT_TEST_DEPS
191 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
192 compiler-rt-headers)
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000193
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000194add_subdirectory(lib)
195
196if(LLVM_INCLUDE_TESTS)
Chandler Carruth84ba6802012-06-22 20:17:27 +0000197 # Currently the tests have not been ported to CMake, so disable this
198 # directory.
199 #
200 #add_subdirectory(test)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000201endif()