blob: 8574cdc245fe958049e8ffdd4befa4d001992882 [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# FIXME: Below we assume that the target build of LLVM/Clang is x86, which is
27# not at all valid. Much of this can be fixed just by switching to use
28# a just-built-clang binary for the compiles.
29
30# Detect whether the current target platform is 32-bit or 64-bit, and setup
31# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovbd170042012-08-07 12:14:29 +000032if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
Chandler Carruthd51e0a02012-04-04 22:12:04 +000033 set(TARGET_X86_64_CFLAGS "-m64")
34 set(TARGET_I386_CFLAGS "")
35else()
36 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
37 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
38 endif()
39 set(TARGET_X86_64_CFLAGS "")
40 set(TARGET_I386_CFLAGS "-m32")
41endif()
42
Alexey Samsonovbf231862012-12-19 15:17:23 +000043function(get_target_flags_for_arch arch out_var)
44 if(${arch} STREQUAL "x86_64")
45 set(${out_var} ${TARGET_X86_64_CFLAGS} PARENT_SCOPE)
46 elseif(${arch} STREQUAL "i386")
47 set(${out_var} ${TARGET_I386_CFLAGS} PARENT_SCOPE)
48 else()
49 message(FATAL_ERROR "Unsupported architecture: ${arch}")
50 endif()
51endfunction()
52
Chandler Carruthd51e0a02012-04-04 22:12:04 +000053# Try to compile a very simple source file to ensure we can target the given
54# platform. We use the results of these tests to build only the various target
55# runtime libraries supported by our current compilers cross-compiling
56# abilities.
57set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c)
58file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}")
59try_compile(CAN_TARGET_X86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64}
60 COMPILE_DEFINITIONS "${TARGET_X86_64_CFLAGS}"
61 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_X86_64_CFLAGS}")
62
63set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c)
64file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}")
65try_compile(CAN_TARGET_I386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32}
66 COMPILE_DEFINITIONS "${TARGET_I386_CFLAGS}"
67 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_I386_CFLAGS}")
68
Alexey Samsonovfe51abb2012-08-10 14:45:52 +000069function(filter_available_targets out_var)
70 set(archs)
71 foreach(arch ${ARGN})
72 if(${arch} STREQUAL "x86_64" AND CAN_TARGET_X86_64)
73 list(APPEND archs ${arch})
74 elseif (${arch} STREQUAL "i386" AND CAN_TARGET_I386)
75 list(APPEND archs ${arch})
76 endif()
77 endforeach()
78 set(${out_var} ${archs} PARENT_SCOPE)
79endfunction()
80
Chandler Carruth60ab0902012-08-29 00:13:11 +000081# Provide some common commmandline flags for Sanitizer runtimes.
82set(SANITIZER_COMMON_CFLAGS
83 -fPIC
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000084 -fno-builtin
Chandler Carruth60ab0902012-08-29 00:13:11 +000085 -fno-exceptions
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000086 -fomit-frame-pointer
Chandler Carruth60ab0902012-08-29 00:13:11 +000087 -funwind-tables
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000088 -O3
Chandler Carruth60ab0902012-08-29 00:13:11 +000089 )
Alexey Samsonovb46941a2012-09-24 11:43:40 +000090if(NOT WIN32)
91 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
92endif()
Alexey Samsonov275ca012012-11-08 14:49:28 +000093# Build sanitizer runtimes with debug info.
94check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
95if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
96 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
97else()
98 list(APPEND SANITIZER_COMMON_CFLAGS -g)
99endif()
100# Warnings suppressions.
Alexey Samsonov721460b2012-09-12 08:06:15 +0000101check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000102if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
103 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
104endif()
Alexey Samsonov721460b2012-09-12 08:06:15 +0000105check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +0000106if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
107 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
108endif()
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +0000109if(APPLE)
110 list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5)
111endif()
Chandler Carruth60ab0902012-08-29 00:13:11 +0000112
Chandler Carruth984f6cf2012-06-27 09:01:24 +0000113# Because compiler-rt spends a lot of time setting up custom compile flags,
114# define a handy helper function for it. The compile flags setting in CMake
115# has serious issues that make its syntax challenging at best.
116function(set_target_compile_flags target)
117 foreach(arg ${ARGN})
118 set(argstring "${argstring} ${arg}")
119 endforeach()
120 set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
121endfunction()
122
Evgeniy Stepanov34fc56c2012-09-11 11:55:45 +0000123function(set_target_link_flags target)
124 foreach(arg ${ARGN})
125 set(argstring "${argstring} ${arg}")
126 endforeach()
127 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
128endfunction()
129
Alexey Samsonov70a70952012-09-11 10:26:54 +0000130# Compute the Clang version from the LLVM version.
131# FIXME: We should be able to reuse CLANG_VERSION variable calculated
132# in Clang cmake files, instead of copying the rules here.
133string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
134 ${PACKAGE_VERSION})
135# Setup the paths where compiler-rt runtimes and headers should be stored.
Evgeniy Stepanov7e72e452012-09-11 11:35:18 +0000136set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
Alexey Samsonov70a70952012-09-11 10:26:54 +0000137string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
138
139# Install compiler-rt headers.
140install(DIRECTORY include/
141 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
142 FILES_MATCHING
143 PATTERN "*.h"
144 PATTERN ".svn" EXCLUDE
145 )
146
147# Call add_clang_compiler_rt_libraries to make sure that targets are built
148# and installed in the directories where Clang driver expects to find them.
149macro(add_clang_compiler_rt_libraries)
150 # Setup output directories so that clang in build tree works.
151 set_target_properties(${ARGN} PROPERTIES
152 ARCHIVE_OUTPUT_DIRECTORY
153 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
154 LIBRARY_OUTPUT_DIRECTORY
155 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
156 )
157 # Add installation command.
158 install(TARGETS ${ARGN}
159 ARCHIVE DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
160 LIBRARY DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
161 )
162endmacro(add_clang_compiler_rt_libraries)
163
Chandler Carruthd865fec2012-08-29 02:27:54 +0000164# Add the public header's directory to the includes for all of compiler-rt.
165include_directories(include)
166
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000167add_subdirectory(lib)
168
169if(LLVM_INCLUDE_TESTS)
Chandler Carruth2e82a8b2012-06-22 20:17:27 +0000170 # Currently the tests have not been ported to CMake, so disable this
171 # directory.
172 #
173 #add_subdirectory(test)
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000174endif()