blob: 0f6260ae5fd2088d86b5417b4d3963fe68039bbc [file] [log] [blame]
Alexey Samsonov02dcc632012-12-19 12:33:39 +00001include(AddLLVM)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +00002include(ExternalProject)
Alexey Samsonov02dcc632012-12-19 12:33:39 +00003include(LLVMParseArguments)
Alexey Samsonov392c50d2013-01-18 16:05:21 +00004include(CompilerRTUtils)
Alexey Samsonov02dcc632012-12-19 12:33:39 +00005
Alexey Samsonov392c50d2013-01-18 16:05:21 +00006# 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>
Alexey Samsonov05fa3802013-09-16 15:50:53 +000010# CFLAGS <compile flags>
11# DEFS <compile definitions>)
Alexey Samsonov392c50d2013-01-18 16:05:21 +000012macro(add_compiler_rt_object_library name arch)
13 if(CAN_TARGET_${arch})
Alexey Samsonov05fa3802013-09-16 15:50:53 +000014 parse_arguments(LIB "SOURCES;CFLAGS;DEFS" "" ${ARGN})
Alexey Samsonov392c50d2013-01-18 16:05:21 +000015 add_library(${name}.${arch} OBJECT ${LIB_SOURCES})
16 set_target_compile_flags(${name}.${arch}
Chandler Carruthe7ad96d2014-05-15 15:21:11 +000017 ${CMAKE_CXX_FLAGS} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonov05fa3802013-09-16 15:50:53 +000018 set_property(TARGET ${name}.${arch} APPEND PROPERTY
19 COMPILE_DEFINITIONS ${LIB_DEFS})
Alexey Samsonov392c50d2013-01-18 16:05:21 +000020 else()
21 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
22 endif()
23endmacro()
24
Alexander Potapenko49496742013-11-07 10:08:19 +000025# 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)
Alexey Samsonov05fa3802013-09-16 15:50:53 +000032 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
Alexander Potapenko49496742013-11-07 10:08:19 +000033 set(libname "${name}.${os}")
Alexey Samsonov51623142013-01-20 14:36:12 +000034 add_library(${libname} OBJECT ${LIB_SOURCES})
Alexander Potapenko49496742013-11-07 10:08:19 +000035 set_target_compile_flags(${libname} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000036 set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCH}")
Alexey Samsonov05fa3802013-09-16 15:50:53 +000037 set_property(TARGET ${libname} APPEND PROPERTY
38 COMPILE_DEFINITIONS ${LIB_DEFS})
Alexey Samsonov51623142013-01-20 14:36:12 +000039endmacro()
40
Alexey Samsonove18b7852014-03-31 13:45:36 +000041# 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>)
47macro(add_compiler_rt_runtime name arch type)
Alexey Samsonove16af952013-01-20 13:58:10 +000048 if(CAN_TARGET_${arch})
Alexey Samsonovd6535ea2014-04-01 13:16:30 +000049 parse_arguments(LIB "SOURCES;CFLAGS;DEFS;OUTPUT_NAME" "" ${ARGN})
Alexey Samsonove18b7852014-03-31 13:45:36 +000050 add_library(${name} ${type} ${LIB_SOURCES})
Alexey Samsonove16af952013-01-20 13:58:10 +000051 # Setup compile flags and definitions.
52 set_target_compile_flags(${name}
53 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonove18b7852014-03-31 13:45:36 +000054 set_target_link_flags(${name}
55 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonove16af952013-01-20 13:58:10 +000056 set_property(TARGET ${name} APPEND PROPERTY
57 COMPILE_DEFINITIONS ${LIB_DEFS})
58 # Setup correct output directory in the build tree.
59 set_target_properties(${name} PROPERTIES
Alexey Samsonove18b7852014-03-31 13:45:36 +000060 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
61 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Alexey Samsonovd6535ea2014-04-01 13:16:30 +000062 if (LIB_OUTPUT_NAME)
63 set_target_properties(${name} PROPERTIES
64 OUTPUT_NAME ${LIB_OUTPUT_NAME})
65 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +000066 # Add installation command.
67 install(TARGETS ${name}
Alexey Samsonove18b7852014-03-31 13:45:36 +000068 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
69 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000070 else()
71 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
72 endif()
73endmacro()
74
Alexey Samsonove18b7852014-03-31 13:45:36 +000075# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000076# for several architectures.
77# add_compiler_rt_osx_static_runtime(<name> ARCH <architectures>
78# SOURCES <source files>
79# CFLAGS <compile flags>
80# DEFS <compile definitions>)
81macro(add_compiler_rt_osx_static_runtime name)
82 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
83 add_library(${name} STATIC ${LIB_SOURCES})
84 set_target_compile_flags(${name} ${LIB_CFLAGS})
85 set_property(TARGET ${name} APPEND PROPERTY
86 COMPILE_DEFINITIONS ${LIB_DEFS})
87 set_target_properties(${name} PROPERTIES
88 OSX_ARCHITECTURES "${LIB_ARCH}"
89 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
90 install(TARGETS ${name}
91 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
92endmacro()
93
Alexander Potapenko49496742013-11-07 10:08:19 +000094# Adds dynamic runtime library on osx/iossim, which supports multiple
95# architectures.
96# add_compiler_rt_darwin_dynamic_runtime(<name> <os>
97# ARCH <architectures>
98# SOURCES <source files>
99# CFLAGS <compile flags>
100# DEFS <compile definitions>
101# LINKFLAGS <link flags>)
102macro(add_compiler_rt_darwin_dynamic_runtime name os)
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000103 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS;LINKFLAGS" "" ${ARGN})
104 add_library(${name} SHARED ${LIB_SOURCES})
Alexander Potapenko49496742013-11-07 10:08:19 +0000105 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
106 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000107 set_property(TARGET ${name} APPEND PROPERTY
108 COMPILE_DEFINITIONS ${LIB_DEFS})
109 set_target_properties(${name} PROPERTIES
110 OSX_ARCHITECTURES "${LIB_ARCH}"
111 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
112 install(TARGETS ${name}
113 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
114endmacro()
115
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000116set(COMPILER_RT_TEST_CFLAGS)
117
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000118# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000119set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000120set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000121set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000122 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000123 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000124 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000125 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000126)
127
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000128if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000129 # clang doesn't support exceptions on Windows yet.
130 list(APPEND COMPILER_RT_TEST_CFLAGS
131 -D_HAS_EXCEPTIONS=0)
132
133 # We should teach clang to understand "#pragma intrinsic", see PR19898.
134 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline)
135
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000136 # Clang doesn't support SEH on Windows yet.
137 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000138
139 # gtest use a lot of stuff marked as deprecated on Windows.
140 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000141endif()
142
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000143# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
144# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000145# test_suite.
146# add_compiler_rt_test(<test_suite> <test_name>
147# OBJECTS <object files>
148# DEPS <deps (e.g. runtime libs)>
149# LINK_FLAGS <link flags>)
150macro(add_compiler_rt_test test_suite test_name)
151 parse_arguments(TEST "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonovf6acafc2013-01-28 07:16:22 +0000152 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000153 # Use host compiler in a standalone build, and just-built Clang otherwise.
154 if(NOT COMPILER_RT_STANDALONE_BUILD)
155 list(APPEND TEST_DEPS clang)
156 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000157 # If we're not on MSVC, include the linker flags from CMAKE but override them
158 # with the provided link flags. This ensures that flags which are required to
159 # link programs at all are included, but the changes needed for the test
160 # trump. With MSVC we can't do that because CMake is set up to run link.exe
161 # when linking, not the compiler. Here, we hack it to use the compiler
162 # because we want to use -fsanitize flags.
163 if(NOT MSVC)
164 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
165 separate_arguments(TEST_LINK_FLAGS)
166 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000167 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000168 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000169 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000170 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000171 DEPENDS ${TEST_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000172 # Make the test suite depend on the binary.
173 add_dependencies(${test_suite} ${test_name})
174endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000175
176macro(add_compiler_rt_resource_file target_name file_name)
177 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000178 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000179 add_custom_command(OUTPUT ${dst_file}
180 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000181 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000182 COMMENT "Copying ${file_name}...")
183 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000184 # Install in Clang resource directory.
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000185 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000186endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000187
188macro(add_compiler_rt_script name)
189 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
190 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
191 add_custom_command(OUTPUT ${dst}
192 DEPENDS ${src}
193 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
194 COMMENT "Copying ${name}...")
195 add_custom_target(${name} DEPENDS ${dst})
196 install(FILES ${dst}
197 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
198 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
199endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000200
201# Builds custom version of libc++ and installs it in <prefix>.
202# Can be used to build sanitized versions of libc++ for running unit tests.
203# add_custom_libcxx(<name> <prefix>
204# DEPS <list of build deps>
205# CFLAGS <list of compile flags>)
206macro(add_custom_libcxx name prefix)
207 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
208 message(FATAL_ERROR "libcxx not found!")
209 endif()
210
211 parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN})
212 foreach(flag ${LIBCXX_CFLAGS})
213 set(flagstr "${flagstr} ${flag}")
214 endforeach()
215 set(LIBCXX_CFLAGS ${flagstr})
216
217 if(NOT COMPILER_RT_STANDALONE_BUILD)
218 list(APPEND LIBCXX_DEPS clang)
219 endif()
220
221 ExternalProject_Add(${name}
222 PREFIX ${prefix}
223 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
224 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
225 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
226 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
227 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
228 -DCMAKE_BUILD_TYPE=Release
229 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000230 LOG_BUILD 1
231 LOG_CONFIGURE 1
232 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000233 )
234
235 ExternalProject_Add_Step(${name} force-reconfigure
236 DEPENDERS configure
237 ALWAYS 1
238 )
239
240 ExternalProject_Add_Step(${name} clobber
241 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
242 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
243 COMMENT "Clobberring ${name} build directory..."
244 DEPENDERS configure
245 DEPENDS ${LIBCXX_DEPS}
246 )
247endmacro()