1include(AddLLVM)
2include(LLVMParseArguments)
3include(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#                                DEFS <compile definitions>)
11macro(add_compiler_rt_object_library name arch)
12  if(CAN_TARGET_${arch})
13    parse_arguments(LIB "SOURCES;CFLAGS;DEFS" "" ${ARGN})
14    add_library(${name}.${arch} OBJECT ${LIB_SOURCES})
15    set_target_compile_flags(${name}.${arch}
16      ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
17    set_property(TARGET ${name}.${arch} APPEND PROPERTY
18      COMPILE_DEFINITIONS ${LIB_DEFS})
19  else()
20    message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
21  endif()
22endmacro()
23
24# Same as above, but adds universal osx library for either OSX or iOS simulator
25# with name "<name>.<os>" targeting multiple architectures.
26# add_compiler_rt_darwin_object_library(<name> <os> ARCH <architectures>
27#                                                   SOURCES <source files>
28#                                                   CFLAGS <compile flags>
29#                                                   DEFS <compile definitions>)
30macro(add_compiler_rt_darwin_object_library name os)
31  parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
32  set(libname "${name}.${os}")
33  add_library(${libname} OBJECT ${LIB_SOURCES})
34  set_target_compile_flags(${libname} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
35  set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCH}")
36  set_property(TARGET ${libname} APPEND PROPERTY
37    COMPILE_DEFINITIONS ${LIB_DEFS})
38endmacro()
39
40# Adds static runtime for a given architecture and puts it in the proper
41# directory in the build and install trees.
42# add_compiler_rt_static_runtime(<name> <arch>
43#                                SOURCES <source files>
44#                                CFLAGS <compile flags>
45#                                DEFS <compile definitions>)
46macro(add_compiler_rt_static_runtime name arch)
47  if(CAN_TARGET_${arch})
48    parse_arguments(LIB "SOURCES;CFLAGS;DEFS" "" ${ARGN})
49    add_library(${name} STATIC ${LIB_SOURCES})
50    # Setup compile flags and definitions.
51    set_target_compile_flags(${name}
52      ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
53    set_property(TARGET ${name} APPEND PROPERTY
54      COMPILE_DEFINITIONS ${LIB_DEFS})
55    # Setup correct output directory in the build tree.
56    set_target_properties(${name} PROPERTIES
57      ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
58    # Add installation command.
59    install(TARGETS ${name}
60      ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
61  else()
62    message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
63  endif()
64endmacro()
65
66# Same as add_compiler_rt_static_runtime, but creates a universal library
67# for several architectures.
68# add_compiler_rt_osx_static_runtime(<name> ARCH <architectures>
69#                                    SOURCES <source files>
70#                                    CFLAGS <compile flags>
71#                                    DEFS <compile definitions>)
72macro(add_compiler_rt_osx_static_runtime name)
73  parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
74  add_library(${name} STATIC ${LIB_SOURCES})
75  set_target_compile_flags(${name} ${LIB_CFLAGS})
76  set_property(TARGET ${name} APPEND PROPERTY
77    COMPILE_DEFINITIONS ${LIB_DEFS})
78  set_target_properties(${name} PROPERTIES
79    OSX_ARCHITECTURES "${LIB_ARCH}"
80    ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
81  install(TARGETS ${name}
82    ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
83endmacro()
84
85# Adds dynamic runtime library on osx/iossim, which supports multiple
86# architectures.
87# add_compiler_rt_darwin_dynamic_runtime(<name> <os>
88#                                        ARCH <architectures>
89#                                        SOURCES <source files>
90#                                        CFLAGS <compile flags>
91#                                        DEFS <compile definitions>
92#                                        LINKFLAGS <link flags>)
93macro(add_compiler_rt_darwin_dynamic_runtime name os)
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} ${DARWIN_${os}_CFLAGS})
97  set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_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})
105endmacro()
106
107# Unittests support.
108set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
109set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
110set(COMPILER_RT_GTEST_CFLAGS
111  -DGTEST_NO_LLVM_RAW_OSTREAM=1
112  -I${COMPILER_RT_GTEST_PATH}/include
113  -I${COMPILER_RT_GTEST_PATH}
114)
115
116# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
117# using specified link flags. Make executable a part of provided
118# test_suite.
119# add_compiler_rt_test(<test_suite> <test_name>
120#                      OBJECTS <object files>
121#                      DEPS <deps (e.g. runtime libs)>
122#                      LINK_FLAGS <link flags>)
123macro(add_compiler_rt_test test_suite test_name)
124  parse_arguments(TEST "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
125  set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
126  # Use host compiler in a standalone build, and just-built Clang otherwise.
127  if(NOT COMPILER_RT_STANDALONE_BUILD)
128    list(APPEND TEST_DEPS clang)
129  endif()
130  add_custom_target(${test_name}
131    COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
132            ${TEST_LINK_FLAGS}
133    DEPENDS ${TEST_DEPS})
134  # Make the test suite depend on the binary.
135  add_dependencies(${test_suite} ${test_name})
136endmacro()
137
138macro(add_compiler_rt_resource_file target_name file_name)
139  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
140  set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
141  add_custom_command(OUTPUT ${dst_file}
142    DEPENDS ${src_file}
143    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
144    COMMENT "Copying ${file_name}...")
145  add_custom_target(${target_name} DEPENDS ${dst_file})
146  # Install in Clang resource directory.
147  install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
148endmacro()
149
150macro(add_compiler_rt_script name)
151  set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
152  set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
153  add_custom_command(OUTPUT ${dst}
154    DEPENDS ${src}
155    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
156    COMMENT "Copying ${name}...")
157  add_custom_target(${name} DEPENDS ${dst})
158  install(FILES ${dst}
159    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
160    DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
161endmacro(add_compiler_rt_script src name)
162