blob: d5f669649c7b07abc16c3229449d0f1060dfd800 [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
Chandler Carruthd51e0a02012-04-04 22:12:04 +000018# FIXME: Below we assume that the target build of LLVM/Clang is x86, which is
19# not at all valid. Much of this can be fixed just by switching to use
20# a just-built-clang binary for the compiles.
21
22# Detect whether the current target platform is 32-bit or 64-bit, and setup
23# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
Alexey Samsonovbd170042012-08-07 12:14:29 +000024if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
Chandler Carruthd51e0a02012-04-04 22:12:04 +000025 set(TARGET_X86_64_CFLAGS "-m64")
26 set(TARGET_I386_CFLAGS "")
27else()
28 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
29 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
30 endif()
31 set(TARGET_X86_64_CFLAGS "")
32 set(TARGET_I386_CFLAGS "-m32")
33endif()
34
35# Try to compile a very simple source file to ensure we can target the given
36# platform. We use the results of these tests to build only the various target
37# runtime libraries supported by our current compilers cross-compiling
38# abilities.
39set(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c)
40file(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}")
41try_compile(CAN_TARGET_X86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64}
42 COMPILE_DEFINITIONS "${TARGET_X86_64_CFLAGS}"
43 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_X86_64_CFLAGS}")
44
45set(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c)
46file(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}")
47try_compile(CAN_TARGET_I386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32}
48 COMPILE_DEFINITIONS "${TARGET_I386_CFLAGS}"
49 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_I386_CFLAGS}")
50
Alexey Samsonovfe51abb2012-08-10 14:45:52 +000051function(filter_available_targets out_var)
52 set(archs)
53 foreach(arch ${ARGN})
54 if(${arch} STREQUAL "x86_64" AND CAN_TARGET_X86_64)
55 list(APPEND archs ${arch})
56 elseif (${arch} STREQUAL "i386" AND CAN_TARGET_I386)
57 list(APPEND archs ${arch})
58 endif()
59 endforeach()
60 set(${out_var} ${archs} PARENT_SCOPE)
61endfunction()
62
Chandler Carruth60ab0902012-08-29 00:13:11 +000063# Provide some common commmandline flags for Sanitizer runtimes.
64set(SANITIZER_COMMON_CFLAGS
65 -fPIC
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000066 -fno-builtin
Chandler Carruth60ab0902012-08-29 00:13:11 +000067 -fno-exceptions
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000068 -fomit-frame-pointer
Chandler Carruth60ab0902012-08-29 00:13:11 +000069 -funwind-tables
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000070 -O3
Chandler Carruth60ab0902012-08-29 00:13:11 +000071 )
Alexey Samsonovb46941a2012-09-24 11:43:40 +000072if(NOT WIN32)
73 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
74endif()
Alexey Samsonov721460b2012-09-12 08:06:15 +000075check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +000076if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
77 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
78endif()
Alexey Samsonov721460b2012-09-12 08:06:15 +000079check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
Chandler Carruth60ab0902012-08-29 00:13:11 +000080if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
81 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
82endif()
Alexey Samsonov0f7d4a42012-09-05 09:00:03 +000083if(APPLE)
84 list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5)
85endif()
Chandler Carruth60ab0902012-08-29 00:13:11 +000086
Chandler Carruth984f6cf2012-06-27 09:01:24 +000087# Because compiler-rt spends a lot of time setting up custom compile flags,
88# define a handy helper function for it. The compile flags setting in CMake
89# has serious issues that make its syntax challenging at best.
90function(set_target_compile_flags target)
91 foreach(arg ${ARGN})
92 set(argstring "${argstring} ${arg}")
93 endforeach()
94 set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
95endfunction()
96
Evgeniy Stepanov34fc56c2012-09-11 11:55:45 +000097function(set_target_link_flags target)
98 foreach(arg ${ARGN})
99 set(argstring "${argstring} ${arg}")
100 endforeach()
101 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
102endfunction()
103
Alexey Samsonov70a70952012-09-11 10:26:54 +0000104# Compute the Clang version from the LLVM version.
105# FIXME: We should be able to reuse CLANG_VERSION variable calculated
106# in Clang cmake files, instead of copying the rules here.
107string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
108 ${PACKAGE_VERSION})
109# Setup the paths where compiler-rt runtimes and headers should be stored.
Evgeniy Stepanov7e72e452012-09-11 11:35:18 +0000110set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
Alexey Samsonov70a70952012-09-11 10:26:54 +0000111string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
112
113# Install compiler-rt headers.
114install(DIRECTORY include/
115 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
116 FILES_MATCHING
117 PATTERN "*.h"
118 PATTERN ".svn" EXCLUDE
119 )
120
121# Call add_clang_compiler_rt_libraries to make sure that targets are built
122# and installed in the directories where Clang driver expects to find them.
123macro(add_clang_compiler_rt_libraries)
124 # Setup output directories so that clang in build tree works.
125 set_target_properties(${ARGN} PROPERTIES
126 ARCHIVE_OUTPUT_DIRECTORY
127 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
128 LIBRARY_OUTPUT_DIRECTORY
129 ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
130 )
131 # Add installation command.
132 install(TARGETS ${ARGN}
133 ARCHIVE DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
134 LIBRARY DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
135 )
136endmacro(add_clang_compiler_rt_libraries)
137
Chandler Carruthd865fec2012-08-29 02:27:54 +0000138# Add the public header's directory to the includes for all of compiler-rt.
139include_directories(include)
140
Alexey Samsonovc5fee8e2012-09-04 14:52:21 +0000141# Build utils before building compiler-rt library.
142add_subdirectory(utils)
143
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000144add_subdirectory(lib)
145
146if(LLVM_INCLUDE_TESTS)
Chandler Carruth2e82a8b2012-06-22 20:17:27 +0000147 # Currently the tests have not been ported to CMake, so disable this
148 # directory.
149 #
150 #add_subdirectory(test)
Chandler Carruthd51e0a02012-04-04 22:12:04 +0000151endif()