Home | History | Annotate | Download | only in Modules
      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 #                                SYMS <symbols file>)
     41 macro(add_compiler_rt_static_runtime name arch)
     42   if(CAN_TARGET_${arch})
     43     parse_arguments(LIB "SOURCES;CFLAGS;DEFS;SYMS" "" ${ARGN})
     44     add_library(${name} STATIC ${LIB_SOURCES})
     45     # Setup compile flags and definitions.
     46     set_target_compile_flags(${name}
     47       ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
     48     set_property(TARGET ${name} APPEND PROPERTY
     49       COMPILE_DEFINITIONS ${LIB_DEFS})
     50     # Setup correct output directory in the build tree.
     51     set_target_properties(${name} PROPERTIES
     52       ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
     53     # Add installation command.
     54     install(TARGETS ${name}
     55       ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
     56     # Generate the .syms file if possible.
     57     if(LIB_SYMS)
     58       get_target_property(libfile ${name} LOCATION)
     59       configure_file(${LIB_SYMS} ${libfile}.syms)
     60       install(FILES ${libfile}.syms
     61         DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
     62     endif(LIB_SYMS)
     63   else()
     64     message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
     65   endif()
     66 endmacro()
     67 
     68 # Same as add_compiler_rt_static_runtime, but creates a universal library
     69 # for several architectures.
     70 # add_compiler_rt_osx_static_runtime(<name> ARCH <architectures>
     71 #                                    SOURCES <source files>
     72 #                                    CFLAGS <compile flags>
     73 #                                    DEFS <compile definitions>)
     74 macro(add_compiler_rt_osx_static_runtime name)
     75   parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
     76   add_library(${name} STATIC ${LIB_SOURCES})
     77   set_target_compile_flags(${name} ${LIB_CFLAGS})
     78   set_property(TARGET ${name} APPEND PROPERTY
     79     COMPILE_DEFINITIONS ${LIB_DEFS})
     80   set_target_properties(${name} PROPERTIES
     81     OSX_ARCHITECTURES "${LIB_ARCH}"
     82     ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
     83   install(TARGETS ${name}
     84     ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
     85 endmacro()
     86 
     87 # Adds dynamic runtime library on osx, which supports multiple architectures.
     88 # add_compiler_rt_osx_dynamic_runtime(<name> ARCH <architectures>
     89 #                                     SOURCES <source files>
     90 #                                     CFLAGS <compile flags>
     91 #                                     DEFS <compile definitions>
     92 #                                     LINKFLAGS <link flags>)
     93 macro(add_compiler_rt_osx_dynamic_runtime name)
     94   parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS;LINKFLAGS" "" ${ARGN})
     95   add_library(${name} SHARED ${LIB_SOURCES})
     96   set_target_compile_flags(${name} ${LIB_CFLAGS})
     97   set_target_link_flags(${name} ${LIB_LINKFLAGS})
     98   set_property(TARGET ${name} APPEND PROPERTY
     99     COMPILE_DEFINITIONS ${LIB_DEFS})
    100   set_target_properties(${name} PROPERTIES
    101     OSX_ARCHITECTURES "${LIB_ARCH}"
    102     LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
    103   install(TARGETS ${name}
    104     LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
    105 endmacro()
    106 
    107 # Unittests support.
    108 set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
    109 set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/gtest-all.cc)
    110 set(COMPILER_RT_GTEST_INCLUDE_CFLAGS
    111   -DGTEST_NO_LLVM_RAW_OSTREAM=1
    112   -I${COMPILER_RT_GTEST_PATH}/include
    113 )
    114 
    115 # Use Clang to link objects into a single executable with just-built
    116 # Clang, using specific link flags. Make executable a part of provided
    117 # test_suite.
    118 # add_compiler_rt_test(<test_suite> <test_name>
    119 #                      OBJECTS <object files>
    120 #                      DEPS <deps (e.g. runtime libs)>
    121 #                      LINK_FLAGS <link flags>)
    122 macro(add_compiler_rt_test test_suite test_name)
    123   parse_arguments(TEST "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
    124   set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
    125   add_custom_target(${test_name}
    126     COMMAND clang ${TEST_OBJECTS} -o "${output_bin}"
    127             ${TEST_LINK_FLAGS}
    128     DEPENDS clang ${TEST_DEPS})
    129   # Make the test suite depend on the binary.
    130   add_dependencies(${test_suite} ${test_name})
    131 endmacro()
    132 
    133 macro(add_compiler_rt_resource_file target_name file_name)
    134   set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
    135   set(dst_file "${CLANG_RESOURCE_DIR}/${file_name}")
    136   add_custom_target(${target_name}
    137     COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
    138     DEPENDS ${file_name})
    139   # Install in Clang resource directory.
    140   install(FILES ${file_name} DESTINATION ${LIBCLANG_INSTALL_PATH})
    141 endmacro()
    142