blob: 4239c4197289270797d4e1d359b881577a3b80a8 [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 Samsonov4b0ee8e2013-01-18 13:10:42 +000053# FIXME: Below we assume that the target build of LLVM/Clang is x86, which is
54# not at all valid. Much of this can be fixed just by switching to use
55# a just-built-clang binary for the compiles.
56
57set(TARGET_x86_64_CFLAGS ${TARGET_64_BIT_CFLAGS})
58set(TARGET_i386_CFLAGS ${TARGET_32_BIT_CFLAGS})
59
60set(COMPILER_RT_SUPPORTED_ARCH
61 x86_64 i386)
62
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000063function(get_target_flags_for_arch arch out_var)
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000064 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
65 if(ARCH_INDEX EQUAL -1)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000066 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +000067 else()
68 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonov85bd73d2012-12-19 15:17:23 +000069 endif()
70endfunction()
71
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000072# Try to compile a very simple source file to ensure we can target the given
73# platform. We use the results of these tests to build only the various target
74# runtime libraries supported by our current compilers cross-compiling
75# abilities.
76set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c)
77file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}")
Alexey Samsonov193b45f2013-01-18 12:45:44 +000078try_compile(CAN_TARGET_x86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64}
79 COMPILE_DEFINITIONS "${TARGET_x86_64_CFLAGS}"
80 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_x86_64_CFLAGS}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000081
82set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c)
83file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}")
Alexey Samsonov193b45f2013-01-18 12:45:44 +000084try_compile(CAN_TARGET_i386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32}
85 COMPILE_DEFINITIONS "${TARGET_i386_CFLAGS}"
86 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_i386_CFLAGS}")
Chandler Carruth1f5d5c02012-04-04 22:12:04 +000087
Alexey Samsonovc20f5d22012-12-27 13:19:23 +000088# We only support running instrumented tests when we're not cross compiling
89# and target a unix-like system. On Android we define the rules for building
90# unit tests, but don't execute them.
91if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
92 set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE)
93else()
94 set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE)
95endif()
96
Alexey Samsonovfb844c72012-08-10 14:45:52 +000097function(filter_available_targets out_var)
98 set(archs)
99 foreach(arch ${ARGN})
Alexey Samsonov4b0ee8e2013-01-18 13:10:42 +0000100 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
101 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfb844c72012-08-10 14:45:52 +0000102 list(APPEND archs ${arch})
103 endif()
104 endforeach()
105 set(${out_var} ${archs} PARENT_SCOPE)
106endfunction()
107
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000108# Provide some common commmandline flags for Sanitizer runtimes.
109set(SANITIZER_COMMON_CFLAGS
110 -fPIC
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000111 -fno-builtin
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000112 -fno-exceptions
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000113 -fomit-frame-pointer
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000114 -funwind-tables
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000115 -O3
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000116 )
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000117if(NOT WIN32)
118 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
119endif()
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000120# Build sanitizer runtimes with debug info.
121check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
122if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
123 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
124else()
125 list(APPEND SANITIZER_COMMON_CFLAGS -g)
126endif()
127# Warnings suppressions.
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000128check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000129if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
130 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
131endif()
Alexey Samsonov3d53c4e2012-09-12 08:06:15 +0000132check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000133if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
134 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
135endif()
Alexey Samsonovd83ccd02012-09-05 09:00:03 +0000136if(APPLE)
137 list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5)
138endif()
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000139
Alexey Samsonovb0684832013-01-18 16:51:07 +0000140# Architectures supported by Sanitizer runtimes. Specific sanitizers may
141# support only subset of these (e.g. TSan works on x86_64 only).
142filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
143 x86_64 i386)
144
Alexey Samsonoveeec3c12012-09-11 10:26:54 +0000145# Install compiler-rt headers.
146install(DIRECTORY include/
147 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
148 FILES_MATCHING
149 PATTERN "*.h"
150 PATTERN ".svn" EXCLUDE
151 )
152
153# Call add_clang_compiler_rt_libraries to make sure that targets are built
154# and installed in the directories where Clang driver expects to find them.
155macro(add_clang_compiler_rt_libraries)
156 # Setup output directories so that clang in build tree works.
157 set_target_properties(${ARGN} PROPERTIES
Alexey Samsonov8c2cd862013-01-20 13:58:10 +0000158 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
159 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Alexey Samsonoveeec3c12012-09-11 10:26:54 +0000160 # Add installation command.
161 install(TARGETS ${ARGN}
Alexey Samsonov8c2cd862013-01-20 13:58:10 +0000162 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
163 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonoveeec3c12012-09-11 10:26:54 +0000164endmacro(add_clang_compiler_rt_libraries)
165
Chandler Carruth1aa4fef2012-08-29 02:27:54 +0000166# Add the public header's directory to the includes for all of compiler-rt.
167include_directories(include)
168
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000169add_subdirectory(lib)
170
171if(LLVM_INCLUDE_TESTS)
Chandler Carruth84ba6802012-06-22 20:17:27 +0000172 # Currently the tests have not been ported to CMake, so disable this
173 # directory.
174 #
175 #add_subdirectory(test)
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000176endif()