blob: f94af32cebfbe72a8b8267f349e4e306e96bf143 [file] [log] [blame]
Chandler Carruthd51e0a02012-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 Carrutha765ffc2012-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 Samsonove16af952013-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 Samsonovc9de4512013-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 Samsonove16af952013-01-20 13:58:10 +000028set(COMPILER_RT_LIBRARY_INSTALL_DIR
Alexey Samsonovc9de4512013-03-19 09:17:35 +000029 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000030
Alexey Samsonov02dcc632012-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 Samsonov392c50d2013-01-18 16:05:21 +000036include(AddCompilerRT)
Alexey Samsonov02dcc632012-12-19 12:33:39 +000037
38set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonovc9de4512013-03-19 09:17:35 +000039# Setup custom SDK sysroots.
40set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
41set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
Alexey Samsonov02dcc632012-12-19 12:33:39 +000042
Chandler Carruthd51e0a02012-04-04 22:12:04 +000043# Detect whether the current target platform is 32-bit or 64-bit, and setup
44# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovbd170042012-08-07 12:14:29 +000045if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
Alexey Samsonov34421182013-01-18 13:10:42 +000046 set(TARGET_64_BIT_CFLAGS "-m64")
47 set(TARGET_32_BIT_CFLAGS "")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000048else()
49 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
50 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
51 endif()
Alexey Samsonov34421182013-01-18 13:10:42 +000052 set(TARGET_64_BIT_CFLAGS "")
53 set(TARGET_32_BIT_CFLAGS "-m32")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000054endif()
55
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000056# List of architectures we can target.
57set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov34421182013-01-18 13:10:42 +000058
Alexey Samsonovbf231862012-12-19 15:17:23 +000059function(get_target_flags_for_arch arch out_var)
Alexey Samsonov34421182013-01-18 13:10:42 +000060 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
61 if(ARCH_INDEX EQUAL -1)
Alexey Samsonovbf231862012-12-19 15:17:23 +000062 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov34421182013-01-18 13:10:42 +000063 else()
64 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonovbf231862012-12-19 15:17:23 +000065 endif()
66endfunction()
67
Chandler Carruthd51e0a02012-04-04 22:12:04 +000068# Try to compile a very simple source file to ensure we can target the given
69# platform. We use the results of these tests to build only the various target
70# runtime libraries supported by our current compilers cross-compiling
71# abilities.
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000072set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
73file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000074
Alexey Samsonovd0b1d462013-01-21 14:31:45 +000075# test_target_arch(<arch> <target flags...>)
76# Sets the target flags for a given architecture and determines if this
77# architecture is supported by trying to build a simple file.
78macro(test_target_arch arch)
79 set(TARGET_${arch}_CFLAGS ${ARGN})
80 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
81 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
82 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
83 if(${CAN_TARGET_${arch}})
84 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
85 endif()
86endmacro()
87
88if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
89 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
90 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
91elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
92 # Explicitly set -m flag on powerpc, because on ppc64 defaults for gcc and
93 # clang are different.
94 test_target_arch(powerpc64 "-m64")
95 test_target_arch(powerpc "-m32")
96endif()
Chandler Carruthd51e0a02012-04-04 22:12:04 +000097
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +000098# We only support running instrumented tests when we're not cross compiling
99# and target a unix-like system. On Android we define the rules for building
100# unit tests, but don't execute them.
101if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
Michael Gottesmanfd0a7892013-04-01 04:13:03 +0000102 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +0000103else()
Michael Gottesmanfd0a7892013-04-01 04:13:03 +0000104 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +0000105endif()
Michael Gottesmanfd0a7892013-04-01 04:13:03 +0000106
Alexey Samsonov304026d2013-02-08 07:39:25 +0000107# Check if compiler-rt is built with libc++.
108find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
109 COMPILER_RT_USES_LIBCXX)
110
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000111function(filter_available_targets out_var)
112 set(archs)
113 foreach(arch ${ARGN})
Alexey Samsonov34421182013-01-18 13:10:42 +0000114 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
115 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000116 list(APPEND archs ${arch})
117 endif()
118 endforeach()
119 set(${out_var} ${archs} PARENT_SCOPE)
120endfunction()
121
Chandler Carruth60ab0902012-08-29 00:13:11 +0000122# Provide some common commmandline flags for Sanitizer runtimes.
123set(SANITIZER_COMMON_CFLAGS
124 -fPIC
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000125 -fno-builtin
Chandler Carruth60ab0902012-08-29 00:13:11 +0000126 -fno-exceptions
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000127 -fomit-frame-pointer
Chandler Carruth60ab0902012-08-29 00:13:11 +0000128 -funwind-tables
Peter Collingbourne8496afd2013-05-08 10:39:05 +0000129 -fno-stack-protector
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000130 -O3
Chandler Carruth60ab0902012-08-29 00:13:11 +0000131 )
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000132if(NOT WIN32)
133 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
134endif()
Alexey Samsonov275ca012012-11-08 14:49:28 +0000135# Build sanitizer runtimes with debug info.
136check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
137if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
138 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
139else()
140 list(APPEND SANITIZER_COMMON_CFLAGS -g)
141endif()
142# Warnings suppressions.
Alexey Samsonov721460b2012-09-12 08:06:15 +0000143check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000144if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
145 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
146endif()
Alexey Samsonov721460b2012-09-12 08:06:15 +0000147check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000148if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
149 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
150endif()
Alexey Samsonove374e4e2013-03-25 10:31:49 +0000151# Sanitizer may not have libstdc++, so we can have problems with virtual
152# destructors.
153check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
154if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
155 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
156endif()
Alexey Samsonov304026d2013-02-08 07:39:25 +0000157
158# Setup min Mac OS X version.
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000159if(APPLE)
Alexey Samsonov304026d2013-02-08 07:39:25 +0000160 if(COMPILER_RT_USES_LIBCXX)
161 set(SANITIZER_MIN_OSX_VERSION 10.7)
162 else()
163 set(SANITIZER_MIN_OSX_VERSION 10.5)
164 endif()
165 list(APPEND SANITIZER_COMMON_CFLAGS
166 -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000167endif()
Chandler Carruth60ab0902012-08-29 00:13:11 +0000168
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000169# Architectures supported by Sanitizer runtimes. Specific sanitizers may
170# support only subset of these (e.g. TSan works on x86_64 only).
171filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Alexey Samsonovd0b1d462013-01-21 14:31:45 +0000172 x86_64 i386 powerpc64 powerpc)
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000173
Chandler Carruthd865fec2012-08-29 02:27:54 +0000174# Add the public header's directory to the includes for all of compiler-rt.
175include_directories(include)
Alexey Samsonov2a529ad2013-04-11 15:49:52 +0000176add_subdirectory(include)
177
178set(SANITIZER_COMMON_LIT_TEST_DEPS
179 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
180 compiler-rt-headers)
Chandler Carruthd865fec2012-08-29 02:27:54 +0000181
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000182add_subdirectory(lib)
183
184if(LLVM_INCLUDE_TESTS)
Chandler Carruth2e82a8b2012-06-22 20:17:27 +0000185 # Currently the tests have not been ported to CMake, so disable this
186 # directory.
187 #
188 #add_subdirectory(test)
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000189endif()