blob: 1904a4be9a1858977a3a8eeee2dde9c339999eb5 [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>
Evgeniy Stepanov44014732014-09-29 13:18:55 +000046# DEFS <compile definitions>
47# OUTPUT_NAME <output library name>)
Alexey Samsonove18b7852014-03-31 13:45:36 +000048macro(add_compiler_rt_runtime name arch type)
Alexey Samsonove16af952013-01-20 13:58:10 +000049 if(CAN_TARGET_${arch})
Alexey Samsonovd6535ea2014-04-01 13:16:30 +000050 parse_arguments(LIB "SOURCES;CFLAGS;DEFS;OUTPUT_NAME" "" ${ARGN})
Alexey Samsonove18b7852014-03-31 13:45:36 +000051 add_library(${name} ${type} ${LIB_SOURCES})
Alexey Samsonove16af952013-01-20 13:58:10 +000052 # Setup compile flags and definitions.
53 set_target_compile_flags(${name}
54 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Aaron Ballman208eae32014-10-29 19:25:20 +000055 set_target_link_flags(${name}
56 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonove16af952013-01-20 13:58:10 +000057 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
Alexey Samsonove18b7852014-03-31 13:45:36 +000061 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
Hans Wennborgb89adc42014-12-04 21:01:49 +000062 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
63 RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Evgeniy Stepanov44014732014-09-29 13:18:55 +000064 if ("${LIB_OUTPUT_NAME}" STREQUAL "")
65 set_target_properties(${name} PROPERTIES
66 OUTPUT_NAME ${name}${COMPILER_RT_OS_SUFFIX})
67 else()
Alexey Samsonovd6535ea2014-04-01 13:16:30 +000068 set_target_properties(${name} PROPERTIES
69 OUTPUT_NAME ${LIB_OUTPUT_NAME})
70 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +000071 # Add installation command.
72 install(TARGETS ${name}
Alexey Samsonove18b7852014-03-31 13:45:36 +000073 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
Hans Wennborgb89adc42014-12-04 21:01:49 +000074 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
75 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000076 else()
77 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
78 endif()
79endmacro()
80
Alexey Samsonove18b7852014-03-31 13:45:36 +000081# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000082# 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
Alexander Potapenko49496742013-11-07 10:08:19 +0000100# 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)
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000109 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS;LINKFLAGS" "" ${ARGN})
110 add_library(${name} SHARED ${LIB_SOURCES})
Alexander Potapenko49496742013-11-07 10:08:19 +0000111 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
112 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000113 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
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000122set(COMPILER_RT_TEST_CFLAGS)
123
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000124# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000125set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000126set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000127set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000128 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000129 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000130 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000131 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000132)
133
Alexey Samsonov001d0382015-01-06 20:58:40 +0000134append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS)
135
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000136if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000137 # clang doesn't support exceptions on Windows yet.
Alexey Samsonov001d0382015-01-06 20:58:40 +0000138 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000139
140 # We should teach clang to understand "#pragma intrinsic", see PR19898.
141 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline)
142
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000143 # Clang doesn't support SEH on Windows yet.
144 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000145
146 # gtest use a lot of stuff marked as deprecated on Windows.
147 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000148
149 # Visual Studio 2012 only supports up to 8 template parameters in
150 # std::tr1::tuple by default, but gtest requires 10
151 if(MSVC_VERSION EQUAL 1700)
Ehsan Akhgari494410f2014-09-25 20:42:49 +0000152 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000153 endif()
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000154endif()
155
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000156# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
157# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000158# test_suite.
159# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000160# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000161# OBJECTS <object files>
162# DEPS <deps (e.g. runtime libs)>
163# LINK_FLAGS <link flags>)
164macro(add_compiler_rt_test test_suite test_name)
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000165 parse_arguments(TEST "SUBDIR;OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
166 if(TEST_SUBDIR)
167 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SUBDIR}/${test_name}")
168 else()
169 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
170 endif()
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000171 # Use host compiler in a standalone build, and just-built Clang otherwise.
172 if(NOT COMPILER_RT_STANDALONE_BUILD)
173 list(APPEND TEST_DEPS clang)
174 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000175 # If we're not on MSVC, include the linker flags from CMAKE but override them
176 # with the provided link flags. This ensures that flags which are required to
177 # link programs at all are included, but the changes needed for the test
178 # trump. With MSVC we can't do that because CMake is set up to run link.exe
179 # when linking, not the compiler. Here, we hack it to use the compiler
180 # because we want to use -fsanitize flags.
181 if(NOT MSVC)
182 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
183 separate_arguments(TEST_LINK_FLAGS)
184 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000185 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000186 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000187 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000188 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000189 DEPENDS ${TEST_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000190 # Make the test suite depend on the binary.
191 add_dependencies(${test_suite} ${test_name})
192endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000193
194macro(add_compiler_rt_resource_file target_name file_name)
195 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000196 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000197 add_custom_command(OUTPUT ${dst_file}
198 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000199 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000200 COMMENT "Copying ${file_name}...")
201 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000202 # Install in Clang resource directory.
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000203 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000204endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000205
206macro(add_compiler_rt_script name)
207 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
208 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
209 add_custom_command(OUTPUT ${dst}
210 DEPENDS ${src}
211 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
212 COMMENT "Copying ${name}...")
213 add_custom_target(${name} DEPENDS ${dst})
214 install(FILES ${dst}
215 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
216 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
217endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000218
219# Builds custom version of libc++ and installs it in <prefix>.
220# Can be used to build sanitized versions of libc++ for running unit tests.
221# add_custom_libcxx(<name> <prefix>
222# DEPS <list of build deps>
223# CFLAGS <list of compile flags>)
224macro(add_custom_libcxx name prefix)
225 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
226 message(FATAL_ERROR "libcxx not found!")
227 endif()
228
229 parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN})
230 foreach(flag ${LIBCXX_CFLAGS})
231 set(flagstr "${flagstr} ${flag}")
232 endforeach()
233 set(LIBCXX_CFLAGS ${flagstr})
234
235 if(NOT COMPILER_RT_STANDALONE_BUILD)
236 list(APPEND LIBCXX_DEPS clang)
237 endif()
238
239 ExternalProject_Add(${name}
240 PREFIX ${prefix}
241 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
242 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
243 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
244 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
245 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
246 -DCMAKE_BUILD_TYPE=Release
247 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000248 LOG_BUILD 1
249 LOG_CONFIGURE 1
250 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000251 )
252
253 ExternalProject_Add_Step(${name} force-reconfigure
254 DEPENDERS configure
255 ALWAYS 1
256 )
257
258 ExternalProject_Add_Step(${name} clobber
259 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
260 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
261 COMMENT "Clobberring ${name} build directory..."
262 DEPENDERS configure
263 DEPENDS ${LIBCXX_DEPS}
264 )
265endmacro()