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 134if(MSVC) 135 # clang doesn't support exceptions on Windows yet. 136 list(APPEND COMPILER_RT_TEST_CFLAGS 137 -D_HAS_EXCEPTIONS=0) 138 139 # We should teach clang to understand "#pragma intrinsic", see PR19898. 140 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline) 141 142 # Clang doesn't support SEH on Windows yet. 143 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0) 144 145 # gtest use a lot of stuff marked as deprecated on Windows. 146 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations) 147 148 # Visual Studio 2012 only supports up to 8 template parameters in 149 # std::tr1::tuple by default, but gtest requires 10 150 if(MSVC_VERSION EQUAL 1700) 151 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10) 152 endif() 153endif() 154 155# Link objects into a single executable with COMPILER_RT_TEST_COMPILER, 156# using specified link flags. Make executable a part of provided 157# test_suite. 158# add_compiler_rt_test(<test_suite> <test_name> 159# OBJECTS <object files> 160# DEPS <deps (e.g. runtime libs)> 161# LINK_FLAGS <link flags>) 162macro(add_compiler_rt_test test_suite test_name) 163 parse_arguments(TEST "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN}) 164 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}") 165 # Use host compiler in a standalone build, and just-built Clang otherwise. 166 if(NOT COMPILER_RT_STANDALONE_BUILD) 167 list(APPEND TEST_DEPS clang) 168 endif() 169 # If we're not on MSVC, include the linker flags from CMAKE but override them 170 # with the provided link flags. This ensures that flags which are required to 171 # link programs at all are included, but the changes needed for the test 172 # trump. With MSVC we can't do that because CMake is set up to run link.exe 173 # when linking, not the compiler. Here, we hack it to use the compiler 174 # because we want to use -fsanitize flags. 175 if(NOT MSVC) 176 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}") 177 separate_arguments(TEST_LINK_FLAGS) 178 endif() 179 add_custom_target(${test_name} 180 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} 181 -o "${output_bin}" 182 ${TEST_LINK_FLAGS} 183 DEPENDS ${TEST_DEPS}) 184 # Make the test suite depend on the binary. 185 add_dependencies(${test_suite} ${test_name}) 186endmacro() 187 188macro(add_compiler_rt_resource_file target_name file_name) 189 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}") 190 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}") 191 add_custom_command(OUTPUT ${dst_file} 192 DEPENDS ${src_file} 193 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file} 194 COMMENT "Copying ${file_name}...") 195 add_custom_target(${target_name} DEPENDS ${dst_file}) 196 # Install in Clang resource directory. 197 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH}) 198endmacro() 199 200macro(add_compiler_rt_script name) 201 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name}) 202 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name}) 203 add_custom_command(OUTPUT ${dst} 204 DEPENDS ${src} 205 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst} 206 COMMENT "Copying ${name}...") 207 add_custom_target(${name} DEPENDS ${dst}) 208 install(FILES ${dst} 209 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 210 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin) 211endmacro(add_compiler_rt_script src name) 212 213# Builds custom version of libc++ and installs it in <prefix>. 214# Can be used to build sanitized versions of libc++ for running unit tests. 215# add_custom_libcxx(<name> <prefix> 216# DEPS <list of build deps> 217# CFLAGS <list of compile flags>) 218macro(add_custom_libcxx name prefix) 219 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES) 220 message(FATAL_ERROR "libcxx not found!") 221 endif() 222 223 parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN}) 224 foreach(flag ${LIBCXX_CFLAGS}) 225 set(flagstr "${flagstr} ${flag}") 226 endforeach() 227 set(LIBCXX_CFLAGS ${flagstr}) 228 229 if(NOT COMPILER_RT_STANDALONE_BUILD) 230 list(APPEND LIBCXX_DEPS clang) 231 endif() 232 233 ExternalProject_Add(${name} 234 PREFIX ${prefix} 235 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH} 236 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER} 237 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER} 238 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS} 239 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS} 240 -DCMAKE_BUILD_TYPE=Release 241 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> 242 LOG_BUILD 1 243 LOG_CONFIGURE 1 244 LOG_INSTALL 1 245 ) 246 247 ExternalProject_Add_Step(${name} force-reconfigure 248 DEPENDERS configure 249 ALWAYS 1 250 ) 251 252 ExternalProject_Add_Step(${name} clobber 253 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR> 254 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR> 255 COMMENT "Clobberring ${name} build directory..." 256 DEPENDERS configure 257 DEPENDS ${LIBCXX_DEPS} 258 ) 259endmacro() 260