blob: 90062cd172f058d67c620a56c6c0dcc7cd744227 [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)
26set(COMPILER_RT_LIBRARY_OUTPUT_DIR
27 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR})
28set(COMPILER_RT_LIBRARY_INSTALL_DIR
29 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
30
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})
39
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000040# Detect whether the current target platform is 32-bit or 64-bit, and setup
41# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonov36796b42012-08-07 12:14:29 +000042if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000043 set(TARGET_64_BIT_CFLAGS "-m64")
44 set(TARGET_32_BIT_CFLAGS "")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000045else()
46 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
47 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
48 endif()
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000049 set(TARGET_64_BIT_CFLAGS "")
50 set(TARGET_32_BIT_CFLAGS "-m32")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000051endif()
52
Alexey Samsonovb6700182013-01-21 14:31:45 +000053# List of architectures we can target.
54set(COMPILER_RT_SUPPORTED_ARCH)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000055
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000056function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000057 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
58 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000059 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000060 else()
61 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000062 endif()
63endfunction()
64
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000065# Try to compile a very simple source file to ensure we can target the given
66# platform. We use the results of these tests to build only the various target
67# runtime libraries supported by our current compilers cross-compiling
68# abilities.
Alexey Samsonovb6700182013-01-21 14:31:45 +000069set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
70file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000071
Alexey Samsonovb6700182013-01-21 14:31:45 +000072# test_target_arch(<arch> <target flags...>)
73# Sets the target flags for a given architecture and determines if this
74# architecture is supported by trying to build a simple file.
75macro(test_target_arch arch)
76 set(TARGET_${arch}_CFLAGS ${ARGN})
77 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
78 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
79 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
80 if(${CAN_TARGET_${arch}})
81 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
82 endif()
83endmacro()
84
85if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
86 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
87 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
88elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
89 # Explicitly set -m flag on powerpc, because on ppc64 defaults for gcc and
90 # clang are different.
91 test_target_arch(powerpc64 "-m64")
92 test_target_arch(powerpc "-m32")
93endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000094
Alexey Samsonovc20f5d22012-12-27 13:19:23 +000095# We only support running instrumented tests when we're not cross compiling
96# and target a unix-like system. On Android we define the rules for building
97# unit tests, but don't execute them.
98if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
99 set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE)
100else()
101 set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE)
102endif()
103
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000104# Check if compiler-rt is built with libc++.
105find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
106 COMPILER_RT_USES_LIBCXX)
107
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000108function(filter_available_targets out_var)
109 set(archs)
110 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000111 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
112 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000113 list(APPEND archs ${arch})
114 endif()
115 endforeach()
116 set(${out_var} ${archs} PARENT_SCOPE)
117endfunction()
118
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000119# Provide some common commmandline flags for Sanitizer runtimes.
120set(SANITIZER_COMMON_CFLAGS
121 -fPIC
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000122 -fno-builtin
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000123 -fno-exceptions
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000124 -fomit-frame-pointer
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000125 -funwind-tables
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000126 -O3
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000127 )
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000128if(NOT WIN32)
129 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
130endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000131# Build sanitizer runtimes with debug info.
132check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
133if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
134 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
135else()
136 list(APPEND SANITIZER_COMMON_CFLAGS -g)
137endif()
138# Warnings suppressions.
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000139check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000140if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
141 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
142endif()
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000143check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000144if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
145 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
146endif()
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000147
148# Setup min Mac OS X version.
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000149if(APPLE)
Alexey Samsonov5cb78602013-02-08 07:39:25 +0000150 if(COMPILER_RT_USES_LIBCXX)
151 set(SANITIZER_MIN_OSX_VERSION 10.7)
152 else()
153 set(SANITIZER_MIN_OSX_VERSION 10.5)
154 endif()
155 list(APPEND SANITIZER_COMMON_CFLAGS
156 -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000157endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000158
Alexey Samsonovb0684832013-01-18 16:51:07 +0000159# Architectures supported by Sanitizer runtimes. Specific sanitizers may
160# support only subset of these (e.g. TSan works on x86_64 only).
161filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
Alexey Samsonovb6700182013-01-21 14:31:45 +0000162 x86_64 i386 powerpc64 powerpc)
Alexey Samsonovb0684832013-01-18 16:51:07 +0000163
Evgeniy Stepanovc9db3b82013-01-29 11:46:06 +0000164file(GLOB_RECURSE COMPILER_RT_HEADERS
165 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include"
166 "include/*.h")
167
168set(output_dir ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/include)
169
170if(MSVC_IDE OR XCODE)
171 set(other_output_dir ${LLVM_BINARY_DIR}/bin/lib/clang/${CLANG_VERSION}/include)
172endif()
173
174# Copy compiler-rt headers to the build tree.
175set(out_files)
176foreach( f ${COMPILER_RT_HEADERS} )
177 set( src ${CMAKE_CURRENT_SOURCE_DIR}/include/${f} )
178 set( dst ${output_dir}/${f} )
179 add_custom_command(OUTPUT ${dst}
180 DEPENDS ${src}
181 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
182 COMMENT "Copying compiler-rt's ${f}...")
183 list(APPEND out_files ${dst})
184
185 if(other_output_dir)
186 set(other_dst ${other_output_dir}/${f})
187 add_custom_command(OUTPUT ${other_dst}
188 DEPENDS ${src}
189 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${other_dst}
190 COMMENT "Copying compiler-rt's ${f}...")
191 list(APPEND out_files ${other_dst})
192 endif()
193endforeach( f )
194
195add_custom_target(compiler-rt-headers ALL DEPENDS ${out_files})
196
Alexey Samsonoveeec3c12012-09-11 10:26:54 +0000197# Install compiler-rt headers.
198install(DIRECTORY include/
199 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
200 FILES_MATCHING
201 PATTERN "*.h"
202 PATTERN ".svn" EXCLUDE
203 )
204
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000205# Add the public header's directory to the includes for all of compiler-rt.
206include_directories(include)
207
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000208add_subdirectory(lib)
209
210if(LLVM_INCLUDE_TESTS)
Chandler Carruth84ba6802012-06-22 20:17:27 +0000211 # Currently the tests have not been ported to CMake, so disable this
212 # directory.
213 #
214 #add_subdirectory(test)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000215endif()