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
17 cmake_minimum_required(VERSION 2.8.8)
19 # Version 2.8.12.1 is required to build with Visual Studion 2013.
20 cmake_minimum_required(VERSION 2.8.12.1)
24 # Top level target used to build all compiler-rt libraries.
25 add_custom_target(compiler-rt)
27 # Compute the Clang version from the LLVM version.
28 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
29 # in Clang cmake files, instead of copying the rules here.
30 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
32 # Setup the paths where compiler-rt runtimes and headers should be stored.
33 set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
34 string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
35 set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
36 set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
37 set(COMPILER_RT_LIBRARY_INSTALL_DIR
38 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
40 # Add path for custom modules
43 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
45 include(AddCompilerRT)
47 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
48 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
49 # Setup custom SDK sysroots.
50 set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
51 set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
52 include(SanitizerUtils)
54 set(COMPILER_RT_EXTRA_ANDROID_HEADERS ${COMPILER_RT_SOURCE_DIR}/third_party/android/include)
56 # Detect whether the current target platform is 32-bit or 64-bit, and setup
57 # the correct commandline flags needed to attempt to target 32-bit and 64-bit.
58 if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
59 NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
60 message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
63 set(TARGET_64_BIT_CFLAGS "-m64")
64 set(TARGET_32_BIT_CFLAGS "-m32")
66 set(TARGET_64_BIT_CFLAGS "")
67 set(TARGET_32_BIT_CFLAGS "")
70 # List of architectures we can target.
71 set(COMPILER_RT_SUPPORTED_ARCH)
73 function(get_target_flags_for_arch arch out_var)
74 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
75 if(ARCH_INDEX EQUAL -1)
76 message(FATAL_ERROR "Unsupported architecture: ${arch}")
78 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
82 # Try to compile a very simple source file to ensure we can target the given
83 # platform. We use the results of these tests to build only the various target
84 # runtime libraries supported by our current compilers cross-compiling
86 set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
87 file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
89 # test_target_arch(<arch> <target flags...>)
90 # Sets the target flags for a given architecture and determines if this
91 # architecture is supported by trying to build a simple file.
92 macro(test_target_arch arch)
93 set(TARGET_${arch}_CFLAGS ${ARGN})
94 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
95 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
96 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
97 if(${CAN_TARGET_${arch}})
98 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
102 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
104 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
106 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
107 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
108 test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
109 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "ARM")
110 test_target_arch(arm "")
113 # We only support running instrumented tests when we're not cross compiling
114 # and target a unix-like system. On Android we define the rules for building
115 # unit tests, but don't execute them.
116 if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
117 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
119 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
122 # Check if compiler-rt is built with libc++.
123 find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
124 COMPILER_RT_USES_LIBCXX)
126 function(filter_available_targets out_var)
128 foreach(arch ${ARGN})
129 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
130 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
131 list(APPEND archs ${arch})
134 set(${out_var} ${archs} PARENT_SCOPE)
137 option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
139 # COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
140 pythonize_bool(COMPILER_RT_DEBUG)
142 # Provide some common commmandline flags for Sanitizer runtimes.
144 set(SANITIZER_COMMON_CFLAGS
151 -Wno-gnu # Variadic macros with 0 arguments for ...
153 if (NOT COMPILER_RT_DEBUG)
154 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
157 set(SANITIZER_COMMON_CFLAGS
165 # Build sanitizer runtimes with debug info. (MSVC gets /Zi above)
167 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
168 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
169 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
171 list(APPEND SANITIZER_COMMON_CFLAGS -g)
174 # Build sanitizer runtimes with -fno-function-sections.
175 check_cxx_compiler_flag("-Werror -fno-function-sections" SUPPORTS_FNO_FUNCTION_SECTIONS_FLAG)
176 if(SUPPORTS_FNO_FUNCTION_SECTIONS_FLAG)
177 list(APPEND SANITIZER_COMMON_CFLAGS -fno-function-sections)
179 # Warnings suppressions.
180 check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
181 if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
182 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
184 check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
185 if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
186 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
188 # Sanitizer may not have libstdc++, so we can have problems with virtual
190 check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
191 if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
192 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
194 check_cxx_compiler_flag(-Wglobal-constructors SUPPORTS_GLOBAL_CONSTRUCTORS_FLAG)
195 # Not all sanitizers forbid global constructors.
198 # Obtain the iOS Simulator SDK path from xcodebuild.
200 COMMAND xcodebuild -version -sdk iphonesimulator Path
201 OUTPUT_VARIABLE IOSSIM_SDK_DIR
202 OUTPUT_STRIP_TRAILING_WHITESPACE
204 set(SANITIZER_COMMON_SUPPORTED_DARWIN_OS osx)
206 list(APPEND SANITIZER_COMMON_SUPPORTED_DARWIN_OS iossim)
209 if(COMPILER_RT_USES_LIBCXX)
210 set(SANITIZER_MIN_OSX_VERSION 10.7)
212 set(SANITIZER_MIN_OSX_VERSION 10.6)
214 set(DARWIN_osx_CFLAGS -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
215 set(DARWIN_iossim_CFLAGS
216 -mios-simulator-version-min=7.0 -isysroot ${IOSSIM_SDK_DIR})
217 set(DARWIN_osx_LINKFLAGS)
218 set(DARWIN_iossim_LINKFLAGS
219 -Wl,-ios_simulator_version_min,7.0.0
220 -mios-simulator-version-min=7.0
221 -isysroot ${IOSSIM_SDK_DIR})
224 # Architectures supported by Sanitizer runtimes. Specific sanitizers may
225 # support only subset of these (e.g. TSan works on x86_64 only).
226 filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
227 x86_64 i386 powerpc64 arm)
229 add_subdirectory(include)
231 set(SANITIZER_COMMON_LIT_TEST_DEPS
232 clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
234 # Check code style when running lit tests for sanitizers.
236 list(APPEND SANITIZER_COMMON_LIT_TEST_DEPS SanitizerLintCheck)
239 add_subdirectory(lib)
241 if(LLVM_INCLUDE_TESTS)
242 # Currently the tests have not been ported to CMake, so disable this
245 #add_subdirectory(test)