1 include(AddLLVM) 2 include(LLVMParseArguments) 3 include(CompilerRTUtils) 4 5 # Tries to add "object library" target for a given architecture 6 # with name "<name>.<arch>" if architecture can be targeted. 7 # add_compiler_rt_object_library(<name> <arch> 8 # SOURCES <source files> 9 # CFLAGS <compile flags>) 10 macro(add_compiler_rt_object_library name arch) 11 if(CAN_TARGET_${arch}) 12 parse_arguments(LIB "SOURCES;CFLAGS" "" ${ARGN}) 13 add_library(${name}.${arch} OBJECT ${LIB_SOURCES}) 14 set_target_compile_flags(${name}.${arch} 15 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) 16 else() 17 message(FATAL_ERROR "Archtecture ${arch} can't be targeted") 18 endif() 19 endmacro() 20 21 # Same as above, but adds universal osx library with name "<name>.osx" 22 # targeting multiple architectures. 23 # add_compiler_rt_osx_object_library(<name> ARCH <architectures> 24 # SOURCES <source files> 25 # CFLAGS <compile flags>) 26 macro(add_compiler_rt_osx_object_library name) 27 parse_arguments(LIB "ARCH;SOURCES;CFLAGS" "" ${ARGN}) 28 set(libname "${name}.osx") 29 add_library(${libname} OBJECT ${LIB_SOURCES}) 30 set_target_compile_flags(${libname} ${LIB_CFLAGS}) 31 set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCH}") 32 endmacro() 33 34 # Adds static runtime for a given architecture and puts it in the proper 35 # directory in the build and install trees. 36 # add_compiler_rt_static_runtime(<name> <arch> 37 # SOURCES <source files> 38 # CFLAGS <compile flags> 39 # DEFS <compile definitions>) 40 macro(add_compiler_rt_static_runtime name arch) 41 if(CAN_TARGET_${arch}) 42 parse_arguments(LIB "SOURCES;CFLAGS;DEFS" "" ${ARGN}) 43 add_library(${name} STATIC ${LIB_SOURCES}) 44 # Setup compile flags and definitions. 45 set_target_compile_flags(${name} 46 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) 47 set_property(TARGET ${name} APPEND PROPERTY 48 COMPILE_DEFINITIONS ${LIB_DEFS}) 49 # Setup correct output directory in the build tree. 50 set_target_properties(${name} PROPERTIES 51 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) 52 # Add installation command. 53 install(TARGETS ${name} 54 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 55 else() 56 message(FATAL_ERROR "Archtecture ${arch} can't be targeted") 57 endif() 58 endmacro() 59 60 # Same as add_compiler_rt_static_runtime, but creates a universal library 61 # for several architectures. 62 # add_compiler_rt_osx_static_runtime(<name> ARCH <architectures> 63 # SOURCES <source files> 64 # CFLAGS <compile flags> 65 # DEFS <compile definitions>) 66 macro(add_compiler_rt_osx_static_runtime name) 67 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN}) 68 add_library(${name} STATIC ${LIB_SOURCES}) 69 set_target_compile_flags(${name} ${LIB_CFLAGS}) 70 set_property(TARGET ${name} APPEND PROPERTY 71 COMPILE_DEFINITIONS ${LIB_DEFS}) 72 set_target_properties(${name} PROPERTIES 73 OSX_ARCHITECTURES "${LIB_ARCH}" 74 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) 75 install(TARGETS ${name} 76 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 77 endmacro() 78 79 # Adds dynamic runtime library on osx, which supports multiple architectures. 80 # add_compiler_rt_osx_dynamic_runtime(<name> ARCH <architectures> 81 # SOURCES <source files> 82 # CFLAGS <compile flags> 83 # DEFS <compile definitions> 84 # LINKFLAGS <link flags>) 85 macro(add_compiler_rt_osx_dynamic_runtime name) 86 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS;LINKFLAGS" "" ${ARGN}) 87 add_library(${name} SHARED ${LIB_SOURCES}) 88 set_target_compile_flags(${name} ${LIB_CFLAGS}) 89 set_target_link_flags(${name} ${LIB_LINKFLAGS}) 90 set_property(TARGET ${name} APPEND PROPERTY 91 COMPILE_DEFINITIONS ${LIB_DEFS}) 92 set_target_properties(${name} PROPERTIES 93 OSX_ARCHITECTURES "${LIB_ARCH}" 94 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) 95 install(TARGETS ${name} 96 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 97 endmacro() 98 99 # Unittests support. 100 set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest) 101 set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/gtest-all.cc) 102 set(COMPILER_RT_GTEST_INCLUDE_CFLAGS 103 -DGTEST_NO_LLVM_RAW_OSTREAM=1 104 -I${COMPILER_RT_GTEST_PATH}/include 105 ) 106 107 # Use Clang to link objects into a single executable with just-built 108 # Clang, using specific link flags. Make executable a part of provided 109 # test_suite. 110 # add_compiler_rt_test(<test_suite> <test_name> 111 # OBJECTS <object files> 112 # DEPS <deps (e.g. runtime libs)> 113 # LINK_FLAGS <link flags>) 114 macro(add_compiler_rt_test test_suite test_name) 115 parse_arguments(TEST "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN}) 116 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}") 117 add_custom_target(${test_name} 118 COMMAND clang ${TEST_OBJECTS} -o "${output_bin}" 119 ${TEST_LINK_FLAGS} 120 DEPENDS clang ${TEST_DEPS}) 121 # Make the test suite depend on the binary. 122 add_dependencies(${test_suite} ${test_name}) 123 endmacro() 124