1 # CMake build for CompilerRT.
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
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.
10 include(LLVMParseArguments)
12 # 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
16 cmake_minimum_required(VERSION 2.8.8)
18 # Top level target used to build all compiler-rt libraries.
19 add_custom_target(compiler-rt)
21 # Compute the Clang version from the LLVM version.
22 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
23 # in Clang cmake files, instead of copying the rules here.
24 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
26 # Setup the paths where compiler-rt runtimes and headers should be stored.
27 set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
28 string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
29 set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
30 set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
31 set(COMPILER_RT_LIBRARY_INSTALL_DIR
32 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
34 # Add path for custom modules
37 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
39 include(AddCompilerRT)
41 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
42 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
43 # Setup custom SDK sysroots.
44 set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
45 set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
46 include(SanitizerUtils)
48 # Detect whether the current target platform is 32-bit or 64-bit, and setup
49 # the correct commandline flags needed to attempt to target 32-bit and 64-bit.
50 if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
51 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
52 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
55 set(TARGET_64_BIT_CFLAGS "-m64")
56 set(TARGET_32_BIT_CFLAGS "-m32")
58 set(TARGET_64_BIT_CFLAGS "")
59 set(TARGET_32_BIT_CFLAGS "")
62 # List of architectures we can target.
63 set(COMPILER_RT_SUPPORTED_ARCH)
65 function(get_target_flags_for_arch arch out_var)
66 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
67 if(ARCH_INDEX EQUAL -1)
68 message(FATAL_ERROR "Unsupported architecture: ${arch}")
70 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
74 # Try to compile a very simple source file to ensure we can target the given
75 # platform. We use the results of these tests to build only the various target
76 # runtime libraries supported by our current compilers cross-compiling
78 set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
79 file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
81 # test_target_arch(<arch> <target flags...>)
82 # Sets the target flags for a given architecture and determines if this
83 # architecture is supported by trying to build a simple file.
84 macro(test_target_arch arch)
85 set(TARGET_${arch}_CFLAGS ${ARGN})
86 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
87 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
88 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
89 if(${CAN_TARGET_${arch}})
90 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
94 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
96 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
98 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
99 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
100 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
103 # We only support running instrumented tests when we're not cross compiling
104 # and target a unix-like system. On Android we define the rules for building
105 # unit tests, but don't execute them.
106 if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
107 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
109 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
112 # Check if compiler-rt is built with libc++.
113 find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
114 COMPILER_RT_USES_LIBCXX)
116 function(filter_available_targets out_var)
118 foreach(arch ${ARGN})
119 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
120 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
121 list(APPEND archs ${arch})
124 set(${out_var} ${archs} PARENT_SCOPE)
127 # Provide some common commmandline flags for Sanitizer runtimes.
129 set(SANITIZER_COMMON_CFLAGS
136 -Wno-gnu # Variadic macros with 0 arguments for ...
141 set(SANITIZER_COMMON_CFLAGS
149 # Build sanitizer runtimes with debug info. (MSVC gets /Zi above)
151 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
152 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
153 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
155 list(APPEND SANITIZER_COMMON_CFLAGS -g)
158 # Warnings suppressions.
159 check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
160 if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
161 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
163 check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
164 if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
165 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
167 # Sanitizer may not have libstdc++, so we can have problems with virtual
169 check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
170 if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
171 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
173 check_cxx_compiler_flag(-Wglobal-constructors SUPPORTS_GLOBAL_CONSTRUCTORS_FLAG)
174 # Not all sanitizers forbid global constructors.
176 # Setup min Mac OS X version.
178 if(COMPILER_RT_USES_LIBCXX)
179 set(SANITIZER_MIN_OSX_VERSION 10.7)
181 set(SANITIZER_MIN_OSX_VERSION 10.6)
183 list(APPEND SANITIZER_COMMON_CFLAGS
184 -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
187 # Architectures supported by Sanitizer runtimes. Specific sanitizers may
188 # support only subset of these (e.g. TSan works on x86_64 only).
189 filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
190 x86_64 i386 powerpc64)
192 # Add the public header's directory to the includes for all of compiler-rt.
193 include_directories(include)
194 add_subdirectory(include)
196 set(SANITIZER_COMMON_LIT_TEST_DEPS
197 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
199 # Check code style when running lit tests for sanitizers.
201 list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck)
204 add_subdirectory(lib)
206 if(LLVM_INCLUDE_TESTS)
207 # Currently the tests have not been ported to CMake, so disable this
210 #add_subdirectory(test)