blob: 700c31f16f027b8de55752e20114e957bed4bd31 [file] [log] [blame]
Alexey Samsonov2e8e4412014-05-09 22:11:03 +00001include(ExternalProject)
Alexey Samsonov392c50d2013-01-18 16:05:21 +00002include(CompilerRTUtils)
Alexey Samsonov02dcc632012-12-19 12:33:39 +00003
Etienne Bergerona088b362016-07-11 21:51:56 +00004function(set_target_output_directories target output_dir)
5 # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators
6 # append a per-configuration subdirectory to the specified directory.
7 # To avoid the appended folder, the configuration specific variable must be
8 # set 'RUNTIME_OUTPUT_DIRECTORY_${CONF}':
9 # RUNTIME_OUTPUT_DIRECTORY_DEBUG, RUNTIME_OUTPUT_DIRECTORY_RELEASE, ...
10 if(CMAKE_CONFIGURATION_TYPES)
11 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
12 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
13 set_target_properties("${target}" PROPERTIES
14 "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
15 "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
16 "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir})
17 endforeach()
18 else()
19 set_target_properties("${target}" PROPERTIES
20 ARCHIVE_OUTPUT_DIRECTORY ${output_dir}
21 LIBRARY_OUTPUT_DIRECTORY ${output_dir}
22 RUNTIME_OUTPUT_DIRECTORY ${output_dir})
23 endif()
24endfunction()
25
Chris Bieneman5c2fd242015-06-10 23:55:07 +000026# Tries to add an "object library" target for a given list of OSs and/or
27# architectures with name "<name>.<arch>" for non-Darwin platforms if
28# architecture can be targeted, and "<name>.<os>" for Darwin platforms.
29# add_compiler_rt_object_libraries(<name>
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000030# OS <os names>
31# ARCHS <architectures>
Chris Bieneman5c2fd242015-06-10 23:55:07 +000032# SOURCES <source files>
33# CFLAGS <compile flags>
34# DEFS <compile definitions>)
35function(add_compiler_rt_object_libraries name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000036 cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Chris Bieneman5c2fd242015-06-10 23:55:07 +000037 set(libnames)
38 if(APPLE)
39 foreach(os ${LIB_OS})
40 set(libname "${name}.${os}")
41 set(libnames ${libnames} ${libname})
42 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS})
Daniel Sandersbfcbe762016-01-27 09:28:01 +000043 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman5c2fd242015-06-10 23:55:07 +000044 endforeach()
Alexey Samsonov392c50d2013-01-18 16:05:21 +000045 else()
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000046 foreach(arch ${LIB_ARCHS})
Chris Bieneman5c2fd242015-06-10 23:55:07 +000047 set(libname "${name}.${arch}")
48 set(libnames ${libnames} ${libname})
49 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
50 if(NOT CAN_TARGET_${arch})
Filipe Cabecinhas030b5902015-08-10 18:26:29 +000051 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000052 return()
53 endif()
54 endforeach()
Alexey Samsonov392c50d2013-01-18 16:05:21 +000055 endif()
Etienne Bergerona088b362016-07-11 21:51:56 +000056
Chris Bieneman5c2fd242015-06-10 23:55:07 +000057 foreach(libname ${libnames})
58 add_library(${libname} OBJECT ${LIB_SOURCES})
Vedant Kumar9c418492017-09-07 01:36:47 +000059
60 # Strip out -msse3 if this isn't macOS.
61 set(target_flags ${LIB_CFLAGS})
62 if(APPLE AND NOT "${libname}" MATCHES ".*\.osx.*")
63 list(REMOVE_ITEM target_flags "-msse3")
64 endif()
65
Chris Bieneman5c2fd242015-06-10 23:55:07 +000066 set_target_compile_flags(${libname}
Vedant Kumar9c418492017-09-07 01:36:47 +000067 ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${target_flags})
Chris Bieneman5c2fd242015-06-10 23:55:07 +000068 set_property(TARGET ${libname} APPEND PROPERTY
69 COMPILE_DEFINITIONS ${LIB_DEFS})
Etienne Bergerona088b362016-07-11 21:51:56 +000070 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000071 if(APPLE)
Chris Bieneman19c84512015-08-13 20:38:16 +000072 set_target_properties(${libname} PROPERTIES
73 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000074 endif()
75 endforeach()
76endfunction()
Alexey Samsonov51623142013-01-20 14:36:12 +000077
Chris Bieneman97c46102015-08-26 18:33:51 +000078# Takes a list of object library targets, and a suffix and appends the proper
79# TARGET_OBJECTS string to the output variable.
80# format_object_libs(<output> <suffix> ...)
81macro(format_object_libs output suffix)
82 foreach(lib ${ARGN})
83 list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
84 endforeach()
85endmacro()
86
Chris Bieneman3bb55622016-08-26 20:52:22 +000087function(add_compiler_rt_component name)
88 add_custom_target(${name})
89 set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
90 if(COMMAND runtime_register_component)
91 runtime_register_component(${name})
92 endif()
93 add_dependencies(compiler-rt ${name})
94endfunction()
95
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +000096macro(set_output_name output name arch)
97 if(ANDROID AND ${arch} STREQUAL "i386")
98 set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
99 else()
100 set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
101 endif()
102endmacro()
103
Chris Bieneman0576c362015-08-25 19:53:09 +0000104# Adds static or shared runtime for a list of architectures and operating
105# systems and puts it in the proper directory in the build and install trees.
106# add_compiler_rt_runtime(<name>
107# {STATIC|SHARED}
108# ARCHS <architectures>
109# OS <os list>
Alexey Samsonove18b7852014-03-31 13:45:36 +0000110# SOURCES <source files>
111# CFLAGS <compile flags>
Francis Ricci9f931132017-01-10 04:33:04 +0000112# LINK_FLAGS <linker flags>
Evgeniy Stepanov44014732014-09-29 13:18:55 +0000113# DEFS <compile definitions>
Chris Bieneman0576c362015-08-25 19:53:09 +0000114# LINK_LIBS <linked libraries> (only for shared library)
Chris Bieneman97c46102015-08-26 18:33:51 +0000115# OBJECT_LIBS <object libraries to use as sources>
Chris Bieneman0576c362015-08-25 19:53:09 +0000116# PARENT_TARGET <convenience parent target>)
117function(add_compiler_rt_runtime name type)
118 if(NOT type MATCHES "^(STATIC|SHARED)$")
119 message(FATAL_ERROR "type argument must be STATIC or SHARED")
120 return()
Alexey Samsonove16af952013-01-20 13:58:10 +0000121 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000122 cmake_parse_arguments(LIB
123 ""
124 "PARENT_TARGET"
Francis Ricci9f931132017-01-10 04:33:04 +0000125 "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
Chris Bieneman0576c362015-08-25 19:53:09 +0000126 ${ARGN})
127 set(libnames)
Bob Haarman955475a2017-03-22 17:25:49 +0000128 # Until we support this some other way, build compiler-rt runtime without LTO
129 # to allow non-LTO projects to link with it.
130 if(COMPILER_RT_HAS_FNO_LTO_FLAG)
131 set(NO_LTO_FLAGS "-fno-lto")
132 else()
133 set(NO_LTO_FLAGS "")
134 endif()
Vedant Kumar160179b2017-09-01 23:23:59 +0000135
Chris Bieneman0576c362015-08-25 19:53:09 +0000136 if(APPLE)
137 foreach(os ${LIB_OS})
Vedant Kumar160179b2017-09-01 23:23:59 +0000138 # Strip out -msse3 if this isn't macOS.
Vedant Kumar160179b2017-09-01 23:23:59 +0000139 list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
140 if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
141 list(REMOVE_ITEM LIB_CFLAGS "-msse3")
142 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000143 if(type STREQUAL "STATIC")
144 set(libname "${name}_${os}")
145 else()
146 set(libname "${name}_${os}_dynamic")
Francis Ricci9f931132017-01-10 04:33:04 +0000147 set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000148 endif()
Daniel Sandersbfcbe762016-01-27 09:28:01 +0000149 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman0576c362015-08-25 19:53:09 +0000150 if(LIB_ARCHS_${libname})
151 list(APPEND libnames ${libname})
Bob Haarman955475a2017-03-22 17:25:49 +0000152 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000153 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
Chris Bieneman97c46102015-08-26 18:33:51 +0000154 set(sources_${libname} ${LIB_SOURCES})
155 format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000156 endif()
157 endforeach()
158 else()
159 foreach(arch ${LIB_ARCHS})
160 if(NOT CAN_TARGET_${arch})
161 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
162 return()
163 endif()
164 if(type STREQUAL "STATIC")
165 set(libname "${name}-${arch}")
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000166 set_output_name(output_name_${libname} ${name} ${arch})
Chris Bieneman0576c362015-08-25 19:53:09 +0000167 else()
168 set(libname "${name}-dynamic-${arch}")
Etienne Bergerona6c33c42016-06-21 14:46:32 +0000169 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Francis Ricci9f931132017-01-10 04:33:04 +0000170 set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000171 if(WIN32)
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000172 set_output_name(output_name_${libname} ${name}_dynamic ${arch})
Chris Bieneman0576c362015-08-25 19:53:09 +0000173 else()
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000174 set_output_name(output_name_${libname} ${name} ${arch})
Chris Bieneman0576c362015-08-25 19:53:09 +0000175 endif()
176 endif()
Chris Bieneman97c46102015-08-26 18:33:51 +0000177 set(sources_${libname} ${LIB_SOURCES})
178 format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000179 set(libnames ${libnames} ${libname})
Bob Haarman955475a2017-03-22 17:25:49 +0000180 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000181 endforeach()
182 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +0000183
Chris Bieneman0576c362015-08-25 19:53:09 +0000184 if(NOT libnames)
185 return()
186 endif()
187
188 if(LIB_PARENT_TARGET)
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000189 # If the parent targets aren't created we should create them
190 if(NOT TARGET ${LIB_PARENT_TARGET})
191 add_custom_target(${LIB_PARENT_TARGET})
192 endif()
193 if(NOT TARGET install-${LIB_PARENT_TARGET})
194 # The parent install target specifies the parent component to scrape up
195 # anything not installed by the individual install targets, and to handle
196 # installation when running the multi-configuration generators.
197 add_custom_target(install-${LIB_PARENT_TARGET}
198 DEPENDS ${LIB_PARENT_TARGET}
199 COMMAND "${CMAKE_COMMAND}"
200 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
201 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Etienne Bergerona088b362016-07-11 21:51:56 +0000202 set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES
203 FOLDER "Compiler-RT Misc")
Chris Bienemana213e3e2016-08-19 22:17:46 +0000204 add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET})
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000205 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000206 endif()
207
208 foreach(libname ${libnames})
Etienne Bergerona088b362016-07-11 21:51:56 +0000209 # If you are using a multi-configuration generator we don't generate
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000210 # per-library install rules, so we fall back to the parent target COMPONENT
211 if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
212 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
213 else()
214 set(COMPONENT_OPTION COMPONENT ${libname})
215 endif()
216
Chris Bieneman97c46102015-08-26 18:33:51 +0000217 add_library(${libname} ${type} ${sources_${libname}})
Chris Bieneman0576c362015-08-25 19:53:09 +0000218 set_target_compile_flags(${libname} ${extra_cflags_${libname}})
Francis Ricci9f931132017-01-10 04:33:04 +0000219 set_target_link_flags(${libname} ${extra_link_flags_${libname}})
Etienne Bergerona088b362016-07-11 21:51:56 +0000220 set_property(TARGET ${libname} APPEND PROPERTY
Chris Bieneman0576c362015-08-25 19:53:09 +0000221 COMPILE_DEFINITIONS ${LIB_DEFS})
Etienne Bergerona088b362016-07-11 21:51:56 +0000222 set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Chris Bieneman0576c362015-08-25 19:53:09 +0000223 set_target_properties(${libname} PROPERTIES
224 OUTPUT_NAME ${output_name_${libname}})
Etienne Bergerona088b362016-07-11 21:51:56 +0000225 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
George Karpenkov0c8339c2017-08-21 23:25:50 +0000226 if(LIB_LINK_LIBS)
227 target_link_libraries(${libname} ${LIB_LINK_LIBS})
228 endif()
Francis Ricci4a0c5cd2016-09-07 20:32:48 +0000229 if(${type} STREQUAL "SHARED")
Francis Ricci4a0c5cd2016-09-07 20:32:48 +0000230 if(WIN32 AND NOT CYGWIN AND NOT MINGW)
231 set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
232 set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
233 endif()
Kuba Mracekfcf0ff12017-04-26 18:59:22 +0000234 if(APPLE)
235 # Ad-hoc sign the dylibs
236 add_custom_command(TARGET ${libname}
237 POST_BUILD
238 COMMAND codesign --sign - $<TARGET_FILE:${libname}>
239 WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
240 )
241 endif()
Chris Bieneman607a4f72015-08-18 17:32:18 +0000242 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000243 install(TARGETS ${libname}
244 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
245 ${COMPONENT_OPTION}
246 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
247 ${COMPONENT_OPTION}
248 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
249 ${COMPONENT_OPTION})
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000250
251 # We only want to generate per-library install targets if you aren't using
252 # an IDE because the extra targets get cluttered in IDEs.
253 if(NOT CMAKE_CONFIGURATION_TYPES)
254 add_custom_target(install-${libname}
255 DEPENDS ${libname}
256 COMMAND "${CMAKE_COMMAND}"
257 -DCMAKE_INSTALL_COMPONENT=${libname}
258 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
259 # If you have a parent target specified, we bind the new install target
260 # to the parent install target.
261 if(LIB_PARENT_TARGET)
262 add_dependencies(install-${LIB_PARENT_TARGET} install-${libname})
263 endif()
264 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000265 if(APPLE)
266 set_target_properties(${libname} PROPERTIES
267 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
268 endif()
Chris Bieneman1d908be2015-12-03 20:08:22 +0000269
270 if(type STREQUAL "SHARED")
271 rt_externalize_debuginfo(${libname})
272 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000273 endforeach()
274 if(LIB_PARENT_TARGET)
Alexey Samsonov124a8a52016-02-26 20:59:40 +0000275 add_dependencies(${LIB_PARENT_TARGET} ${libnames})
Chris Bieneman607a4f72015-08-18 17:32:18 +0000276 endif()
277endfunction()
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000278
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000279# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help
280# in compilation and linking of unittests.
281string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
Francis Ricci9f931132017-01-10 04:33:04 +0000282set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000283
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000284# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000285set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000286set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000287set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000288 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000289 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000290 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000291 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000292)
293
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000294append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
Chandler Carruthe78a0bf2017-01-05 03:41:41 +0000295append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
Alexey Samsonov001d0382015-01-06 20:58:40 +0000296
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000297if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000298 # clang doesn't support exceptions on Windows yet.
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000299 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000300
301 # We should teach clang to understand "#pragma intrinsic", see PR19898.
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000302 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000303
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000304 # Clang doesn't support SEH on Windows yet.
305 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000306
307 # gtest use a lot of stuff marked as deprecated on Windows.
308 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000309endif()
310
George Karpenkov38e269e2017-08-15 22:56:10 +0000311# Compile and register compiler-rt tests.
312# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
313# <test architecture>
314# KIND <custom prefix>
315# SUBDIR <subdirectory for testing binary>
316# SOURCES <sources to compile>
317# RUNTIME <tests runtime to link in>
318# CFLAGS <compile-time flags>
319# COMPILE_DEPS <compile-time dependencies>
320# DEPS <dependencies>
321# LINK_FLAGS <flags to use during linking>
322# )
323function(generate_compiler_rt_tests test_objects test_suite testname arch)
324 cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
325 "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
326
327 foreach(source ${TEST_SOURCES})
328 sanitizer_test_compile(
329 "${test_objects}" "${source}" "${arch}"
330 KIND ${TEST_KIND}
331 COMPILE_DEPS ${TEST_COMPILE_DEPS}
332 DEPS ${TEST_DEPS}
333 CFLAGS ${TEST_CFLAGS}
334 )
335 endforeach()
336
337 set(TEST_DEPS ${${test_objects}})
338
339 if(NOT "${TEST_RUNTIME}" STREQUAL "")
340 list(APPEND TEST_DEPS ${TEST_RUNTIME})
341 list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
342 endif()
343
344 add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
345 SUBDIR ${TEST_SUBDIR}
346 OBJECTS ${${test_objects}}
347 DEPS ${TEST_DEPS}
George Karpenkovbcb584b2017-08-15 23:22:52 +0000348 LINK_FLAGS ${TEST_LINK_FLAGS}
George Karpenkov38e269e2017-08-15 22:56:10 +0000349 )
350 set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
351endfunction()
352
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000353# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
354# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000355# test_suite.
George Karpenkov38e269e2017-08-15 22:56:10 +0000356# add_compiler_rt_test(<test_suite> <test_name> <arch>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000357# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000358# OBJECTS <object files>
359# DEPS <deps (e.g. runtime libs)>
360# LINK_FLAGS <link flags>)
George Karpenkov38e269e2017-08-15 22:56:10 +0000361function(add_compiler_rt_test test_suite test_name arch)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000362 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000363 set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000364 if(TEST_SUBDIR)
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000365 set(output_dir "${output_dir}/${TEST_SUBDIR}")
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000366 endif()
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000367 set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
368 file(MAKE_DIRECTORY "${output_dir}")
369 set(output_bin "${output_dir}/${test_name}")
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000370 if(MSVC)
371 set(output_bin "${output_bin}.exe")
372 endif()
Etienne Bergeron4659e362016-05-16 14:58:07 +0000373
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000374 # Use host compiler in a standalone build, and just-built Clang otherwise.
375 if(NOT COMPILER_RT_STANDALONE_BUILD)
376 list(APPEND TEST_DEPS clang)
377 endif()
George Karpenkov38e269e2017-08-15 22:56:10 +0000378
379 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
380 list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
381
Chandler Carruth272eef92014-05-15 16:33:58 +0000382 # If we're not on MSVC, include the linker flags from CMAKE but override them
383 # with the provided link flags. This ensures that flags which are required to
384 # link programs at all are included, but the changes needed for the test
385 # trump. With MSVC we can't do that because CMake is set up to run link.exe
386 # when linking, not the compiler. Here, we hack it to use the compiler
387 # because we want to use -fsanitize flags.
388 if(NOT MSVC)
389 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
390 separate_arguments(TEST_LINK_FLAGS)
391 endif()
George Karpenkov916b56d2017-08-21 21:19:13 +0000392 add_custom_command(
393 OUTPUT "${output_bin}"
394 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000395 ${TEST_LINK_FLAGS}
George Karpenkov916b56d2017-08-21 21:19:13 +0000396 DEPENDS ${TEST_DEPS}
397 )
398 add_custom_target(T${test_name} DEPENDS "${output_bin}")
399 set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
Etienne Bergerona088b362016-07-11 21:51:56 +0000400
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000401 # Make the test suite depend on the binary.
George Karpenkov916b56d2017-08-21 21:19:13 +0000402 add_dependencies(${test_suite} T${test_name})
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000403endfunction()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000404
Chris Bieneman772a58d2016-02-23 21:50:39 +0000405macro(add_compiler_rt_resource_file target_name file_name component)
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000406 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000407 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000408 add_custom_command(OUTPUT ${dst_file}
409 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000410 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000411 COMMENT "Copying ${file_name}...")
412 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000413 # Install in Clang resource directory.
Chris Bieneman772a58d2016-02-23 21:50:39 +0000414 install(FILES ${file_name}
415 DESTINATION ${COMPILER_RT_INSTALL_PATH}
416 COMPONENT ${component})
417 add_dependencies(${component} ${target_name})
Etienne Bergerona088b362016-07-11 21:51:56 +0000418
419 set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000420endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000421
422macro(add_compiler_rt_script name)
423 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
424 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
425 add_custom_command(OUTPUT ${dst}
426 DEPENDS ${src}
427 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
428 COMMENT "Copying ${name}...")
429 add_custom_target(${name} DEPENDS ${dst})
430 install(FILES ${dst}
431 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
432 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
433endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000434
435# Builds custom version of libc++ and installs it in <prefix>.
436# Can be used to build sanitized versions of libc++ for running unit tests.
437# add_custom_libcxx(<name> <prefix>
438# DEPS <list of build deps>
439# CFLAGS <list of compile flags>)
440macro(add_custom_libcxx name prefix)
441 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
442 message(FATAL_ERROR "libcxx not found!")
443 endif()
444
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000445 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000446 foreach(flag ${LIBCXX_CFLAGS})
447 set(flagstr "${flagstr} ${flag}")
448 endforeach()
449 set(LIBCXX_CFLAGS ${flagstr})
450
451 if(NOT COMPILER_RT_STANDALONE_BUILD)
452 list(APPEND LIBCXX_DEPS clang)
453 endif()
454
455 ExternalProject_Add(${name}
456 PREFIX ${prefix}
457 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
Andy Gibbs61adea82015-12-02 13:46:56 +0000458 CMAKE_ARGS -DCMAKE_MAKE_PROGRAM:STRING=${CMAKE_MAKE_PROGRAM}
459 -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
Daniel Sandersa268a2f2016-02-02 12:55:28 +0000460 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER}
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000461 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
462 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
463 -DCMAKE_BUILD_TYPE=Release
464 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Daniel Sandersa268a2f2016-02-02 12:55:28 +0000465 -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
Petr Hosekb0495c32017-01-16 00:33:02 +0000466 -DLIBCXX_STANDALONE_BUILD=On
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000467 LOG_BUILD 1
468 LOG_CONFIGURE 1
469 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000470 )
Alexey Samsonovdd6f8442015-07-16 21:20:05 +0000471 set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000472
473 ExternalProject_Add_Step(${name} force-reconfigure
474 DEPENDERS configure
475 ALWAYS 1
476 )
477
478 ExternalProject_Add_Step(${name} clobber
479 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
480 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
481 COMMENT "Clobberring ${name} build directory..."
482 DEPENDERS configure
483 DEPENDS ${LIBCXX_DEPS}
484 )
485endmacro()
Chris Bieneman1d908be2015-12-03 20:08:22 +0000486
487function(rt_externalize_debuginfo name)
488 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
489 return()
490 endif()
491
Chris Bieneman8a782562016-03-31 21:17:19 +0000492 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
493 set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
494 endif()
495
Chris Bieneman1d908be2015-12-03 20:08:22 +0000496 if(APPLE)
497 if(CMAKE_CXX_FLAGS MATCHES "-flto"
498 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
499
500 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bieneman5d5bc5b2015-12-03 22:52:22 +0000501 set_property(TARGET ${name} APPEND_STRING PROPERTY
Chris Bieneman960c4902015-12-03 22:56:21 +0000502 LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
Chris Bieneman1d908be2015-12-03 20:08:22 +0000503 endif()
504 add_custom_command(TARGET ${name} POST_BUILD
505 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman8a782562016-03-31 21:17:19 +0000506 ${strip_command})
Chris Bieneman1d908be2015-12-03 20:08:22 +0000507 else()
508 message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
509 endif()
510endfunction()