blob: 9ab408916362c3877fcb753e727190c4e495d8e1 [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 Samsonov02dcc632012-12-19 12:33:39 +000018# Add path for custom modules
19set(CMAKE_MODULE_PATH
20 ${CMAKE_MODULE_PATH}
21 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
22 )
23
24set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
25
Chandler Carruthd51e0a02012-04-04 22:12:04 +000026# Detect whether the current target platform is 32-bit or 64-bit, and setup
27# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovbd170042012-08-07 12:14:29 +000028if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
Alexey Samsonov34421182013-01-18 13:10:42 +000029 set(TARGET_64_BIT_CFLAGS "-m64")
30 set(TARGET_32_BIT_CFLAGS "")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000031else()
32 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
33 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
34 endif()
Alexey Samsonov34421182013-01-18 13:10:42 +000035 set(TARGET_64_BIT_CFLAGS "")
36 set(TARGET_32_BIT_CFLAGS "-m32")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000037endif()
38
Alexey Samsonov34421182013-01-18 13:10:42 +000039# FIXME: Below we assume that the target build of LLVM/Clang is x86, which is
40# not at all valid. Much of this can be fixed just by switching to use
41# a just-built-clang binary for the compiles.
42
43set(TARGET_x86_64_CFLAGS ${TARGET_64_BIT_CFLAGS})
44set(TARGET_i386_CFLAGS ${TARGET_32_BIT_CFLAGS})
45
46set(COMPILER_RT_SUPPORTED_ARCH
47 x86_64 i386)
48
Alexey Samsonovbf231862012-12-19 15:17:23 +000049function(get_target_flags_for_arch arch out_var)
Alexey Samsonov34421182013-01-18 13:10:42 +000050 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
51 if(ARCH_INDEX EQUAL -1)
Alexey Samsonovbf231862012-12-19 15:17:23 +000052 message(FATAL_ERROR "Unsupported architecture: ${arch}")
Alexey Samsonov34421182013-01-18 13:10:42 +000053 else()
54 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
Alexey Samsonovbf231862012-12-19 15:17:23 +000055 endif()
56endfunction()
57
Chandler Carruthd51e0a02012-04-04 22:12:04 +000058# Try to compile a very simple source file to ensure we can target the given
59# platform. We use the results of these tests to build only the various target
60# runtime libraries supported by our current compilers cross-compiling
61# abilities.
62set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c)
63file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}")
Alexey Samsonov2f3aef02013-01-18 12:45:44 +000064try_compile(CAN_TARGET_x86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64}
65 COMPILE_DEFINITIONS "${TARGET_x86_64_CFLAGS}"
66 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_x86_64_CFLAGS}")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000067
68set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c)
69file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}")
Alexey Samsonov2f3aef02013-01-18 12:45:44 +000070try_compile(CAN_TARGET_i386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32}
71 COMPILE_DEFINITIONS "${TARGET_i386_CFLAGS}"
72 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_i386_CFLAGS}")
Chandler Carruthd51e0a02012-04-04 22:12:04 +000073
Alexey Samsonovd7d7b5f2012-12-27 13:19:23 +000074# We only support running instrumented tests when we're not cross compiling
75# and target a unix-like system. On Android we define the rules for building
76# unit tests, but don't execute them.
77if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
78 set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE)
79else()
80 set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE)
81endif()
82
Alexey Samsonovfe51abb2012-08-10 14:45:52 +000083function(filter_available_targets out_var)
84 set(archs)
85 foreach(arch ${ARGN})
Alexey Samsonov34421182013-01-18 13:10:42 +000086 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
87 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
Alexey Samsonovfe51abb2012-08-10 14:45:52 +000088 list(APPEND archs ${arch})
89 endif()
90 endforeach()
91 set(${out_var} ${archs} PARENT_SCOPE)
92endfunction()
93
Chandler Carruth60ab0902012-08-29 00:13:11 +000094# Provide some common commmandline flags for Sanitizer runtimes.
95set(SANITIZER_COMMON_CFLAGS
96 -fPIC
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000097 -fno-builtin
Chandler Carruth60ab0902012-08-29 00:13:11 +000098 -fno-exceptions
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000099 -fomit-frame-pointer
Chandler Carruth60ab0902012-08-29 00:13:11 +0000100 -funwind-tables
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000101 -O3
Chandler Carruth60ab0902012-08-29 00:13:11 +0000102 )
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000103if(NOT WIN32)
104 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
105endif()
Alexey Samsonov275ca012012-11-08 14:49:28 +0000106# Build sanitizer runtimes with debug info.
107check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
108if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
109 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
110else()
111 list(APPEND SANITIZER_COMMON_CFLAGS -g)
112endif()
113# Warnings suppressions.
Alexey Samsonov721460b2012-09-12 08:06:15 +0000114check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000115if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
116 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
117endif()
Alexey Samsonov721460b2012-09-12 08:06:15 +0000118check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000119if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
120 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
121endif()
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000122if(APPLE)
123 list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5)
124endif()
Chandler Carruth60ab0902012-08-29 00:13:11 +0000125
Chandler Carruth984f6cf2012-06-27 09:01:24 +0000126# Because compiler-rt spends a lot of time setting up custom compile flags,
127# define a handy helper function for it. The compile flags setting in CMake
128# has serious issues that make its syntax challenging at best.
129function(set_target_compile_flags target)
130 foreach(arg ${ARGN})
131 set(argstring "${argstring} ${arg}")
132 endforeach()
133 set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
134endfunction()
135
Evgeniy Stepanov34fc56c2012-09-11 11:55:45 +0000136function(set_target_link_flags target)
137 foreach(arg ${ARGN})
138 set(argstring "${argstring} ${arg}")
139 endforeach()
140 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
141endfunction()
142
Alexey Samsonov70a70952012-09-11 10:26:54 +0000143# Compute the Clang version from the LLVM version.
144# FIXME: We should be able to reuse CLANG_VERSION variable calculated
145# in Clang cmake files, instead of copying the rules here.
146string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
147 ${PACKAGE_VERSION})
148# Setup the paths where compiler-rt runtimes and headers should be stored.
Evgeniy Stepanov7e72e452012-09-11 11:35:18 +0000149set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
Alexey Samsonov70a70952012-09-11 10:26:54 +0000150string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
151
152# Install compiler-rt headers.
153install(DIRECTORY include/
154 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
155 FILES_MATCHING
156 PATTERN "*.h"
157 PATTERN ".svn" EXCLUDE
158 )
159
160# Call add_clang_compiler_rt_libraries to make sure that targets are built
161# and installed in the directories where Clang driver expects to find them.
162macro(add_clang_compiler_rt_libraries)
163 # Setup output directories so that clang in build tree works.
164 set_target_properties(${ARGN} PROPERTIES
165 ARCHIVE_OUTPUT_DIRECTORY
166 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
167 LIBRARY_OUTPUT_DIRECTORY
168 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
169 )
170 # Add installation command.
171 install(TARGETS ${ARGN}
172 ARCHIVE DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
173 LIBRARY DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
174 )
175endmacro(add_clang_compiler_rt_libraries)
176
Chandler Carruthd865fec2012-08-29 02:27:54 +0000177# Add the public header's directory to the includes for all of compiler-rt.
178include_directories(include)
179
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000180add_subdirectory(lib)
181
182if(LLVM_INCLUDE_TESTS)
Chandler Carruth2e82a8b2012-06-22 20:17:27 +0000183 # Currently the tests have not been ported to CMake, so disable this
184 # directory.
185 #
186 #add_subdirectory(test)
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000187endif()