1include(AddLLVM) 2include(ExternalProject) 3include(LLVMParseArguments) 4include(CompilerRTUtils) 5 6# Tries to add "object library" target for a given architecture 7# with name "<name>.<arch>" if architecture can be targeted. 8# add_compiler_rt_object_library(<name> <arch> 9# SOURCES <source files> 10# CFLAGS <compile flags> 11# DEFS <compile definitions>) 12macro(add_compiler_rt_object_library name arch) 13 if(CAN_TARGET_${arch}) 14 parse_arguments(LIB "SOURCES;CFLAGS;DEFS" "" ${ARGN}) 15 add_library(${name}.${arch} OBJECT ${LIB_SOURCES}) 16 set_target_compile_flags(${name}.${arch} 17 ${CMAKE_CXX_FLAGS} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) 18 set_property(TARGET ${name}.${arch} APPEND PROPERTY 19 COMPILE_DEFINITIONS ${LIB_DEFS}) 20 else() 21 message(FATAL_ERROR "Archtecture ${arch} can't be targeted") 22 endif() 23endmacro() 24 25# Same as above, but adds universal osx library for either OSX or iOS simulator 26# with name "<name>.<os>" targeting multiple architectures. 27# add_compiler_rt_darwin_object_library(<name> <os> ARCH <architectures> 28# SOURCES <source files> 29# CFLAGS <compile flags> 30# DEFS <compile definitions>) 31macro(add_compiler_rt_darwin_object_library name os) 32 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN}) 33 set(libname "${name}.${os}") 34 add_library(${libname} OBJECT ${LIB_SOURCES}) 35 set_target_compile_flags(${libname} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS}) 36 set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCH}") 37 set_property(TARGET ${libname} APPEND PROPERTY 38 COMPILE_DEFINITIONS ${LIB_DEFS}) 39endmacro() 40 41# Adds static or shared runtime for a given architecture and puts it in the 42# proper directory in the build and install trees. 43# add_compiler_rt_runtime(<name> <arch> {STATIC,SHARED} 44# SOURCES <source files> 45# CFLAGS <compile flags> 46# DEFS <compile definitions> 47# OUTPUT_NAME <output library name>) 48macro(add_compiler_rt_runtime name arch type) 49 if(CAN_TARGET_${arch}) 50 parse_arguments(LIB "SOURCES;CFLAGS;DEFS;OUTPUT_NAME" "" ${ARGN}) 51 add_library(${name} ${type} ${LIB_SOURCES}) 52 # Setup compile flags and definitions. 53 set_target_compile_flags(${name} 54 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) 55 set_target_link_flags(${name} 56 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) 57 set_property(TARGET ${name} APPEND PROPERTY 58 COMPILE_DEFINITIONS ${LIB_DEFS}) 59 # Setup correct output directory in the build tree. 60 set_target_properties(${name} PROPERTIES 61 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR} 62 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR} 63 RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) 64 if ("${LIB_OUTPUT_NAME}" STREQUAL "") 65 set_target_properties(${name} PROPERTIES 66 OUTPUT_NAME ${name}${COMPILER_RT_OS_SUFFIX}) 67 else() 68 set_target_properties(${name} PROPERTIES 69 OUTPUT_NAME ${LIB_OUTPUT_NAME}) 70 endif() 71 # Add installation command. 72 install(TARGETS ${name} 73 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} 74 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} 75 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 76 else() 77 message(FATAL_ERROR "Archtecture ${arch} can't be targeted") 78 endif() 79endmacro() 80 81# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library 82# for several architectures. 83# add_compiler_rt_osx_static_runtime(<name> ARCH <architectures> 84# SOURCES <source files> 85# CFLAGS <compile flags> 86# DEFS <compile definitions>) 87macro(add_compiler_rt_osx_static_runtime name) 88 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN}) 89 add_library(${name} STATIC ${LIB_SOURCES}) 90 set_target_compile_flags(${name} ${LIB_CFLAGS}) 91 set_property(TARGET ${name} APPEND PROPERTY 92 COMPILE_DEFINITIONS ${LIB_DEFS}) 93 set_target_properties(${name} PROPERTIES 94 OSX_ARCHITECTURES "${LIB_ARCH}" 95 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) 96 install(TARGETS ${name} 97 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 98endmacro() 99 100# Adds dynamic runtime library on osx/iossim, which supports multiple 101# architectures. 102# add_compiler_rt_darwin_dynamic_runtime(<name> <os> 103# ARCH <architectures> 104# SOURCES <source files> 105# CFLAGS <compile flags> 106# DEFS <compile definitions> 107# LINKFLAGS <link flags>) 108macro(add_compiler_rt_darwin_dynamic_runtime name os) 109 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS;LINKFLAGS" "" ${ARGN}) 110 add_library(${name} SHARED ${LIB_SOURCES}) 111 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS}) 112 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS}) 113 set_property(TARGET ${name} APPEND PROPERTY 114 COMPILE_DEFINITIONS ${LIB_DEFS}) 115 set_target_properties(${name} PROPERTIES 116 OSX_ARCHITECTURES "${LIB_ARCH}" 117 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) 118 install(TARGETS ${name} 119 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 120endmacro() 121 122set(COMPILER_RT_TEST_CFLAGS) 123 124# Unittests support. 125set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest) 126set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc) 127set(COMPILER_RT_GTEST_CFLAGS 128 -DGTEST_NO_LLVM_RAW_OSTREAM=1 129 -DGTEST_HAS_RTTI=0 130 -I${COMPILER_RT_GTEST_PATH}/include 131 -I${COMPILER_RT_GTEST_PATH} 132) 133 134append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS) 135 136if(MSVC) 137 # clang doesn't support exceptions on Windows yet. 138 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0) 139 140 # We should teach clang to understand "#pragma intrinsic", see PR19898. 141 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline) 142 143 # Clang doesn't support SEH on Windows yet. 144 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0) 145 146 # gtest use a lot of stuff marked as deprecated on Windows. 147 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations) 148 149 # Visual Studio 2012 only supports up to 8 template parameters in 150 # std::tr1::tuple by default, but gtest requires 10 151 if(MSVC_VERSION EQUAL 1700) 152 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10) 153 endif() 154endif() 155 156# Link objects into a single executable with COMPILER_RT_TEST_COMPILER, 157# using specified link flags. Make executable a part of provided 158# test_suite. 159# add_compiler_rt_test(<test_suite> <test_name> 160# SUBDIR <subdirectory for binary> 161# OBJECTS <object files> 162# DEPS <deps (e.g. runtime libs)> 163# LINK_FLAGS <link flags>) 164macro(add_compiler_rt_test test_suite test_name) 165 parse_arguments(TEST "SUBDIR;OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN}) 166 if(TEST_SUBDIR) 167 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SUBDIR}/${test_name}") 168 else() 169 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}") 170 endif() 171 if(MSVC) 172 set(output_bin "${output_bin}.exe") 173 endif() 174 # Use host compiler in a standalone build, and just-built Clang otherwise. 175 if(NOT COMPILER_RT_STANDALONE_BUILD) 176 list(APPEND TEST_DEPS clang) 177 endif() 178 # If we're not on MSVC, include the linker flags from CMAKE but override them 179 # with the provided link flags. This ensures that flags which are required to 180 # link programs at all are included, but the changes needed for the test 181 # trump. With MSVC we can't do that because CMake is set up to run link.exe 182 # when linking, not the compiler. Here, we hack it to use the compiler 183 # because we want to use -fsanitize flags. 184 if(NOT MSVC) 185 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}") 186 separate_arguments(TEST_LINK_FLAGS) 187 endif() 188 add_custom_target(${test_name} 189 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} 190 -o "${output_bin}" 191 ${TEST_LINK_FLAGS} 192 DEPENDS ${TEST_DEPS}) 193 # Make the test suite depend on the binary. 194 add_dependencies(${test_suite} ${test_name}) 195endmacro() 196 197macro(add_compiler_rt_resource_file target_name file_name) 198 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}") 199 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}") 200 add_custom_command(OUTPUT ${dst_file} 201 DEPENDS ${src_file} 202 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file} 203 COMMENT "Copying ${file_name}...") 204 add_custom_target(${target_name} DEPENDS ${dst_file}) 205 # Install in Clang resource directory. 206 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH}) 207endmacro() 208 209macro(add_compiler_rt_script name) 210 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name}) 211 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name}) 212 add_custom_command(OUTPUT ${dst} 213 DEPENDS ${src} 214 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst} 215 COMMENT "Copying ${name}...") 216 add_custom_target(${name} DEPENDS ${dst}) 217 install(FILES ${dst} 218 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 219 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin) 220endmacro(add_compiler_rt_script src name) 221 222# Builds custom version of libc++ and installs it in <prefix>. 223# Can be used to build sanitized versions of libc++ for running unit tests. 224# add_custom_libcxx(<name> <prefix> 225# DEPS <list of build deps> 226# CFLAGS <list of compile flags>) 227macro(add_custom_libcxx name prefix) 228 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES) 229 message(FATAL_ERROR "libcxx not found!") 230 endif() 231 232 parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN}) 233 foreach(flag ${LIBCXX_CFLAGS}) 234 set(flagstr "${flagstr} ${flag}") 235 endforeach() 236 set(LIBCXX_CFLAGS ${flagstr}) 237 238 if(NOT COMPILER_RT_STANDALONE_BUILD) 239 list(APPEND LIBCXX_DEPS clang) 240 endif() 241 242 ExternalProject_Add(${name} 243 PREFIX ${prefix} 244 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH} 245 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER} 246 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER} 247 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS} 248 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS} 249 -DCMAKE_BUILD_TYPE=Release 250 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> 251 LOG_BUILD 1 252 LOG_CONFIGURE 1 253 LOG_INSTALL 1 254 ) 255 256 ExternalProject_Add_Step(${name} force-reconfigure 257 DEPENDERS configure 258 ALWAYS 1 259 ) 260 261 ExternalProject_Add_Step(${name} clobber 262 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR> 263 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR> 264 COMMENT "Clobberring ${name} build directory..." 265 DEPENDERS configure 266 DEPENDS ${LIBCXX_DEPS} 267 ) 268endmacro() 269