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 # Compute the Clang version from the LLVM version.
19 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
20 # in Clang cmake files, instead of copying the rules here.
21 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
23 # Setup the paths where compiler-rt runtimes and headers should be stored.
24 set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
25 string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
26 set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
27 set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
28 set(COMPILER_RT_LIBRARY_INSTALL_DIR
29 ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
31 # Add path for custom modules
34 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
36 include(AddCompilerRT)
38 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
39 # Setup custom SDK sysroots.
40 set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
41 set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
43 # Detect whether the current target platform is 32-bit or 64-bit, and setup
44 # the correct commandline flags needed to attempt to target 32-bit and 64-bit.
45 if(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
46 set(TARGET_64_BIT_CFLAGS "-m64")
47 set(TARGET_32_BIT_CFLAGS "")
49 if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
50 message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
52 set(TARGET_64_BIT_CFLAGS "")
53 set(TARGET_32_BIT_CFLAGS "-m32")
56 # List of architectures we can target.
57 set(COMPILER_RT_SUPPORTED_ARCH)
59 function(get_target_flags_for_arch arch out_var)
60 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
61 if(ARCH_INDEX EQUAL -1)
62 message(FATAL_ERROR "Unsupported architecture: ${arch}")
64 set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
68 # Try to compile a very simple source file to ensure we can target the given
69 # platform. We use the results of these tests to build only the various target
70 # runtime libraries supported by our current compilers cross-compiling
72 set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
73 file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
75 # test_target_arch(<arch> <target flags...>)
76 # Sets the target flags for a given architecture and determines if this
77 # architecture is supported by trying to build a simple file.
78 macro(test_target_arch arch)
79 set(TARGET_${arch}_CFLAGS ${ARGN})
80 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
81 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
82 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
83 if(${CAN_TARGET_${arch}})
84 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
88 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
89 test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
90 test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
91 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
92 # Explicitly set -m flag on powerpc, because on ppc64 defaults for gcc and
93 # clang are different.
94 test_target_arch(powerpc64 "-m64")
95 test_target_arch(powerpc "-m32")
98 # We only support running instrumented tests when we're not cross compiling
99 # and target a unix-like system. On Android we define the rules for building
100 # unit tests, but don't execute them.
101 if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
102 set(COMPILER_RT_CAN_EXECUTE_TESTS TRUE)
104 set(COMPILER_RT_CAN_EXECUTE_TESTS FALSE)
107 # Check if compiler-rt is built with libc++.
108 find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
109 COMPILER_RT_USES_LIBCXX)
111 function(filter_available_targets out_var)
113 foreach(arch ${ARGN})
114 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
115 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
116 list(APPEND archs ${arch})
119 set(${out_var} ${archs} PARENT_SCOPE)
122 # Provide some common commmandline flags for Sanitizer runtimes.
123 set(SANITIZER_COMMON_CFLAGS
132 list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
134 # Build sanitizer runtimes with debug info.
135 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
136 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
137 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
139 list(APPEND SANITIZER_COMMON_CFLAGS -g)
141 # Warnings suppressions.
142 check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
143 if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
144 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
146 check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
147 if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
148 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
150 # Sanitizer may not have libstdc++, so we can have problems with virtual
152 check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
153 if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
154 list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
157 # Setup min Mac OS X version.
159 if(COMPILER_RT_USES_LIBCXX)
160 set(SANITIZER_MIN_OSX_VERSION 10.7)
162 set(SANITIZER_MIN_OSX_VERSION 10.5)
164 list(APPEND SANITIZER_COMMON_CFLAGS
165 -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
168 # Architectures supported by Sanitizer runtimes. Specific sanitizers may
169 # support only subset of these (e.g. TSan works on x86_64 only).
170 filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
171 x86_64 i386 powerpc64 powerpc)
173 file(GLOB_RECURSE COMPILER_RT_HEADERS
174 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include"
177 set(output_dir ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/include)
179 if(MSVC_IDE OR XCODE)
180 set(other_output_dir ${LLVM_BINARY_DIR}/bin/lib/clang/${CLANG_VERSION}/include)
183 # Copy compiler-rt headers to the build tree.
185 foreach( f ${COMPILER_RT_HEADERS} )
186 set( src ${CMAKE_CURRENT_SOURCE_DIR}/include/${f} )
187 set( dst ${output_dir}/${f} )
188 add_custom_command(OUTPUT ${dst}
190 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
191 COMMENT "Copying compiler-rt's ${f}...")
192 list(APPEND out_files ${dst})
195 set(other_dst ${other_output_dir}/${f})
196 add_custom_command(OUTPUT ${other_dst}
198 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${other_dst}
199 COMMENT "Copying compiler-rt's ${f}...")
200 list(APPEND out_files ${other_dst})
204 add_custom_target(compiler-rt-headers ALL DEPENDS ${out_files})
206 # Install compiler-rt headers.
207 install(DIRECTORY include/
208 DESTINATION ${LIBCLANG_INSTALL_PATH}/include
211 PATTERN ".svn" EXCLUDE
214 # Add the public header's directory to the includes for all of compiler-rt.
215 include_directories(include)
217 add_subdirectory(lib)
219 if(LLVM_INCLUDE_TESTS)
220 # Currently the tests have not been ported to CMake, so disable this
223 #add_subdirectory(test)