blob: 622292edecea0074cc60b4c1e23c8375a68aa446 [file] [log] [blame]
Alexey Samsonoveacb4d82014-05-09 22:11:03 +00001include(ExternalProject)
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00002include(CompilerRTUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00003
Etienne Bergeronab42f4d2016-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 Bieneman6bd006f2015-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +000030# OS <os names>
31# ARCHS <architectures>
Chris Bieneman6bd006f2015-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +000036 cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Chris Bieneman6bd006f2015-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 Sandersde098c92016-01-27 09:28:01 +000043 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman6bd006f2015-06-10 23:55:07 +000044 endforeach()
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000045 else()
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +000046 foreach(arch ${LIB_ARCHS})
Chris Bieneman6bd006f2015-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 Cabecinhas2bbdbcb2015-08-10 18:26:29 +000051 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
Chris Bieneman6bd006f2015-06-10 23:55:07 +000052 return()
53 endif()
54 endforeach()
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000055 endif()
Etienne Bergeronab42f4d2016-07-11 21:51:56 +000056
Chris Bieneman6bd006f2015-06-10 23:55:07 +000057 foreach(libname ${libnames})
58 add_library(${libname} OBJECT ${LIB_SOURCES})
59 set_target_compile_flags(${libname}
60 ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${LIB_CFLAGS})
61 set_property(TARGET ${libname} APPEND PROPERTY
62 COMPILE_DEFINITIONS ${LIB_DEFS})
Etienne Bergeronab42f4d2016-07-11 21:51:56 +000063 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
Chris Bieneman6bd006f2015-06-10 23:55:07 +000064 if(APPLE)
Chris Bieneman361231e2015-08-13 20:38:16 +000065 set_target_properties(${libname} PROPERTIES
66 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
Chris Bieneman6bd006f2015-06-10 23:55:07 +000067 endif()
68 endforeach()
69endfunction()
Alexey Samsonov4e503332013-01-20 14:36:12 +000070
Chris Bieneman7173f072015-08-26 18:33:51 +000071# Takes a list of object library targets, and a suffix and appends the proper
72# TARGET_OBJECTS string to the output variable.
73# format_object_libs(<output> <suffix> ...)
74macro(format_object_libs output suffix)
75 foreach(lib ${ARGN})
76 list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
77 endforeach()
78endmacro()
79
Chris Bieneman21395f92016-08-26 20:52:22 +000080function(add_compiler_rt_component name)
81 add_custom_target(${name})
82 set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
83 if(COMMAND runtime_register_component)
84 runtime_register_component(${name})
85 endif()
86 add_dependencies(compiler-rt ${name})
87endfunction()
88
Evgeniy Stepanovc6daf73c2017-08-29 22:12:31 +000089macro(set_output_name output name arch)
90 if(ANDROID AND ${arch} STREQUAL "i386")
91 set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
92 else()
93 set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
94 endif()
95endmacro()
96
Chris Bienemand1602602015-08-25 19:53:09 +000097# Adds static or shared runtime for a list of architectures and operating
98# systems and puts it in the proper directory in the build and install trees.
99# add_compiler_rt_runtime(<name>
100# {STATIC|SHARED}
101# ARCHS <architectures>
102# OS <os list>
Alexey Samsonov78a84352014-03-31 13:45:36 +0000103# SOURCES <source files>
104# CFLAGS <compile flags>
Francis Ricci17781c72017-01-10 04:33:04 +0000105# LINK_FLAGS <linker flags>
Evgeniy Stepanov9e922e72014-09-29 13:18:55 +0000106# DEFS <compile definitions>
Chris Bienemand1602602015-08-25 19:53:09 +0000107# LINK_LIBS <linked libraries> (only for shared library)
Chris Bieneman7173f072015-08-26 18:33:51 +0000108# OBJECT_LIBS <object libraries to use as sources>
Chris Bienemand1602602015-08-25 19:53:09 +0000109# PARENT_TARGET <convenience parent target>)
110function(add_compiler_rt_runtime name type)
111 if(NOT type MATCHES "^(STATIC|SHARED)$")
112 message(FATAL_ERROR "type argument must be STATIC or SHARED")
113 return()
Alexey Samsonov8c2cd862013-01-20 13:58:10 +0000114 endif()
Chris Bienemand1602602015-08-25 19:53:09 +0000115 cmake_parse_arguments(LIB
116 ""
117 "PARENT_TARGET"
Francis Ricci17781c72017-01-10 04:33:04 +0000118 "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
Chris Bienemand1602602015-08-25 19:53:09 +0000119 ${ARGN})
120 set(libnames)
Bob Haarman1012fe82017-03-22 17:25:49 +0000121 # Until we support this some other way, build compiler-rt runtime without LTO
122 # to allow non-LTO projects to link with it.
123 if(COMPILER_RT_HAS_FNO_LTO_FLAG)
124 set(NO_LTO_FLAGS "-fno-lto")
125 else()
126 set(NO_LTO_FLAGS "")
127 endif()
Vedant Kumar6b1de0e2017-09-01 23:23:59 +0000128
Chris Bienemand1602602015-08-25 19:53:09 +0000129 if(APPLE)
130 foreach(os ${LIB_OS})
Vedant Kumar6b1de0e2017-09-01 23:23:59 +0000131 # Strip out -msse3 if this isn't macOS.
Vedant Kumar6b1de0e2017-09-01 23:23:59 +0000132 list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
133 if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
134 list(REMOVE_ITEM LIB_CFLAGS "-msse3")
135 endif()
Chris Bienemand1602602015-08-25 19:53:09 +0000136 if(type STREQUAL "STATIC")
137 set(libname "${name}_${os}")
138 else()
139 set(libname "${name}_${os}_dynamic")
Francis Ricci17781c72017-01-10 04:33:04 +0000140 set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
Chris Bienemand1602602015-08-25 19:53:09 +0000141 endif()
Daniel Sandersde098c92016-01-27 09:28:01 +0000142 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bienemand1602602015-08-25 19:53:09 +0000143 if(LIB_ARCHS_${libname})
144 list(APPEND libnames ${libname})
Bob Haarman1012fe82017-03-22 17:25:49 +0000145 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
Chris Bienemand1602602015-08-25 19:53:09 +0000146 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
Chris Bieneman7173f072015-08-26 18:33:51 +0000147 set(sources_${libname} ${LIB_SOURCES})
148 format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
Chris Bienemand1602602015-08-25 19:53:09 +0000149 endif()
150 endforeach()
151 else()
152 foreach(arch ${LIB_ARCHS})
153 if(NOT CAN_TARGET_${arch})
154 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
155 return()
156 endif()
157 if(type STREQUAL "STATIC")
158 set(libname "${name}-${arch}")
Evgeniy Stepanovc6daf73c2017-08-29 22:12:31 +0000159 set_output_name(output_name_${libname} ${name} ${arch})
Chris Bienemand1602602015-08-25 19:53:09 +0000160 else()
161 set(libname "${name}-dynamic-${arch}")
Etienne Bergeron7e2fc3b2016-06-21 14:46:32 +0000162 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Francis Ricci17781c72017-01-10 04:33:04 +0000163 set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
Chris Bienemand1602602015-08-25 19:53:09 +0000164 if(WIN32)
Evgeniy Stepanovc6daf73c2017-08-29 22:12:31 +0000165 set_output_name(output_name_${libname} ${name}_dynamic ${arch})
Chris Bienemand1602602015-08-25 19:53:09 +0000166 else()
Evgeniy Stepanovc6daf73c2017-08-29 22:12:31 +0000167 set_output_name(output_name_${libname} ${name} ${arch})
Chris Bienemand1602602015-08-25 19:53:09 +0000168 endif()
169 endif()
Chris Bieneman7173f072015-08-26 18:33:51 +0000170 set(sources_${libname} ${LIB_SOURCES})
171 format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
Chris Bienemand1602602015-08-25 19:53:09 +0000172 set(libnames ${libnames} ${libname})
Bob Haarman1012fe82017-03-22 17:25:49 +0000173 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
Chris Bienemand1602602015-08-25 19:53:09 +0000174 endforeach()
175 endif()
Alexey Samsonov8c2cd862013-01-20 13:58:10 +0000176
Chris Bienemand1602602015-08-25 19:53:09 +0000177 if(NOT libnames)
178 return()
179 endif()
180
181 if(LIB_PARENT_TARGET)
Chris Bieneman497a0ac2016-02-23 21:55:38 +0000182 # If the parent targets aren't created we should create them
183 if(NOT TARGET ${LIB_PARENT_TARGET})
184 add_custom_target(${LIB_PARENT_TARGET})
185 endif()
186 if(NOT TARGET install-${LIB_PARENT_TARGET})
187 # The parent install target specifies the parent component to scrape up
188 # anything not installed by the individual install targets, and to handle
189 # installation when running the multi-configuration generators.
190 add_custom_target(install-${LIB_PARENT_TARGET}
191 DEPENDS ${LIB_PARENT_TARGET}
192 COMMAND "${CMAKE_COMMAND}"
193 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
194 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000195 set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES
196 FOLDER "Compiler-RT Misc")
Chris Bieneman83877ee2016-08-19 22:17:46 +0000197 add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET})
Chris Bieneman497a0ac2016-02-23 21:55:38 +0000198 endif()
Chris Bienemand1602602015-08-25 19:53:09 +0000199 endif()
200
201 foreach(libname ${libnames})
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000202 # If you are using a multi-configuration generator we don't generate
Chris Bieneman497a0ac2016-02-23 21:55:38 +0000203 # per-library install rules, so we fall back to the parent target COMPONENT
204 if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
205 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
206 else()
207 set(COMPONENT_OPTION COMPONENT ${libname})
208 endif()
209
Chris Bieneman7173f072015-08-26 18:33:51 +0000210 add_library(${libname} ${type} ${sources_${libname}})
Chris Bienemand1602602015-08-25 19:53:09 +0000211 set_target_compile_flags(${libname} ${extra_cflags_${libname}})
Francis Ricci17781c72017-01-10 04:33:04 +0000212 set_target_link_flags(${libname} ${extra_link_flags_${libname}})
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000213 set_property(TARGET ${libname} APPEND PROPERTY
Chris Bienemand1602602015-08-25 19:53:09 +0000214 COMPILE_DEFINITIONS ${LIB_DEFS})
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000215 set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Chris Bienemand1602602015-08-25 19:53:09 +0000216 set_target_properties(${libname} PROPERTIES
217 OUTPUT_NAME ${output_name_${libname}})
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000218 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
George Karpenkov10ab2ac2017-08-21 23:25:50 +0000219 if(LIB_LINK_LIBS)
220 target_link_libraries(${libname} ${LIB_LINK_LIBS})
221 endif()
Francis Riccie7729c82016-09-07 20:32:48 +0000222 if(${type} STREQUAL "SHARED")
Francis Riccie7729c82016-09-07 20:32:48 +0000223 if(WIN32 AND NOT CYGWIN AND NOT MINGW)
224 set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
225 set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
226 endif()
Kuba Mracek132c8292017-04-26 18:59:22 +0000227 if(APPLE)
228 # Ad-hoc sign the dylibs
229 add_custom_command(TARGET ${libname}
230 POST_BUILD
231 COMMAND codesign --sign - $<TARGET_FILE:${libname}>
232 WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
233 )
234 endif()
Chris Bieneman201d0652015-08-18 17:32:18 +0000235 endif()
Chris Bienemand1602602015-08-25 19:53:09 +0000236 install(TARGETS ${libname}
237 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
238 ${COMPONENT_OPTION}
239 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
240 ${COMPONENT_OPTION}
241 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
242 ${COMPONENT_OPTION})
Chris Bieneman497a0ac2016-02-23 21:55:38 +0000243
244 # We only want to generate per-library install targets if you aren't using
245 # an IDE because the extra targets get cluttered in IDEs.
246 if(NOT CMAKE_CONFIGURATION_TYPES)
247 add_custom_target(install-${libname}
248 DEPENDS ${libname}
249 COMMAND "${CMAKE_COMMAND}"
250 -DCMAKE_INSTALL_COMPONENT=${libname}
251 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
252 # If you have a parent target specified, we bind the new install target
253 # to the parent install target.
254 if(LIB_PARENT_TARGET)
255 add_dependencies(install-${LIB_PARENT_TARGET} install-${libname})
256 endif()
257 endif()
Chris Bienemand1602602015-08-25 19:53:09 +0000258 if(APPLE)
259 set_target_properties(${libname} PROPERTIES
260 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
261 endif()
Chris Bieneman69a372d2015-12-03 20:08:22 +0000262
263 if(type STREQUAL "SHARED")
264 rt_externalize_debuginfo(${libname})
265 endif()
Chris Bienemand1602602015-08-25 19:53:09 +0000266 endforeach()
267 if(LIB_PARENT_TARGET)
Alexey Samsonov2eff7f72016-02-26 20:59:40 +0000268 add_dependencies(${LIB_PARENT_TARGET} ${libnames})
Chris Bieneman201d0652015-08-18 17:32:18 +0000269 endif()
270endfunction()
Alexey Samsonovb3991182013-01-21 08:12:20 +0000271
Sumanth Gundapanenib76bf102016-01-14 18:18:49 +0000272# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help
273# in compilation and linking of unittests.
274string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
Francis Ricci17781c72017-01-10 04:33:04 +0000275set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
Timur Iskhodzhanov1b42b812014-05-30 12:42:57 +0000276
Alexey Samsonov163ab9d2013-01-18 16:05:21 +0000277# Unittests support.
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000278set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruth65fd2382013-11-15 10:21:15 +0000279set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonovc41ca6d2014-03-24 13:29:20 +0000280set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000281 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000282 -DGTEST_HAS_RTTI=0
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000283 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruth65fd2382013-11-15 10:21:15 +0000284 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000285)
286
Sumanth Gundapanenib76bf102016-01-14 18:18:49 +0000287append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
Chandler Carruthbbb82ca2017-01-05 03:41:41 +0000288append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
Alexey Samsonov6239ebc2015-01-06 20:58:40 +0000289
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000290if(MSVC)
Timur Iskhodzhanov1b42b812014-05-30 12:42:57 +0000291 # clang doesn't support exceptions on Windows yet.
Sumanth Gundapanenib76bf102016-01-14 18:18:49 +0000292 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanov1b42b812014-05-30 12:42:57 +0000293
294 # We should teach clang to understand "#pragma intrinsic", see PR19898.
Sumanth Gundapanenib76bf102016-01-14 18:18:49 +0000295 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline)
Timur Iskhodzhanov1b42b812014-05-30 12:42:57 +0000296
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000297 # Clang doesn't support SEH on Windows yet.
298 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanov1b42b812014-05-30 12:42:57 +0000299
300 # gtest use a lot of stuff marked as deprecated on Windows.
301 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000302endif()
303
George Karpenkov769124d2017-08-15 22:56:10 +0000304# Compile and register compiler-rt tests.
305# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
306# <test architecture>
307# KIND <custom prefix>
308# SUBDIR <subdirectory for testing binary>
309# SOURCES <sources to compile>
310# RUNTIME <tests runtime to link in>
311# CFLAGS <compile-time flags>
312# COMPILE_DEPS <compile-time dependencies>
313# DEPS <dependencies>
314# LINK_FLAGS <flags to use during linking>
315# )
316function(generate_compiler_rt_tests test_objects test_suite testname arch)
317 cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
318 "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
319
320 foreach(source ${TEST_SOURCES})
321 sanitizer_test_compile(
322 "${test_objects}" "${source}" "${arch}"
323 KIND ${TEST_KIND}
324 COMPILE_DEPS ${TEST_COMPILE_DEPS}
325 DEPS ${TEST_DEPS}
326 CFLAGS ${TEST_CFLAGS}
327 )
328 endforeach()
329
330 set(TEST_DEPS ${${test_objects}})
331
332 if(NOT "${TEST_RUNTIME}" STREQUAL "")
333 list(APPEND TEST_DEPS ${TEST_RUNTIME})
334 list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
335 endif()
336
337 add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
338 SUBDIR ${TEST_SUBDIR}
339 OBJECTS ${${test_objects}}
340 DEPS ${TEST_DEPS}
George Karpenkov4c269222017-08-15 23:22:52 +0000341 LINK_FLAGS ${TEST_LINK_FLAGS}
George Karpenkov769124d2017-08-15 22:56:10 +0000342 )
343 set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
344endfunction()
345
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000346# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
347# using specified link flags. Make executable a part of provided
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000348# test_suite.
George Karpenkov769124d2017-08-15 22:56:10 +0000349# add_compiler_rt_test(<test_suite> <test_name> <arch>
Alexey Samsonov969902b2014-12-17 23:14:01 +0000350# SUBDIR <subdirectory for binary>
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000351# OBJECTS <object files>
352# DEPS <deps (e.g. runtime libs)>
353# LINK_FLAGS <link flags>)
George Karpenkov769124d2017-08-15 22:56:10 +0000354function(add_compiler_rt_test test_suite test_name arch)
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +0000355 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
George Karpenkovd46f17d2017-08-15 18:32:28 +0000356 set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov969902b2014-12-17 23:14:01 +0000357 if(TEST_SUBDIR)
George Karpenkovd46f17d2017-08-15 18:32:28 +0000358 set(output_dir "${output_dir}/${TEST_SUBDIR}")
Alexey Samsonov969902b2014-12-17 23:14:01 +0000359 endif()
George Karpenkovd46f17d2017-08-15 18:32:28 +0000360 set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
361 file(MAKE_DIRECTORY "${output_dir}")
362 set(output_bin "${output_dir}/${test_name}")
Timur Iskhodzhanov23cfd6e2015-01-22 14:54:22 +0000363 if(MSVC)
364 set(output_bin "${output_bin}.exe")
365 endif()
Etienne Bergeron3df28792016-05-16 14:58:07 +0000366
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000367 # Use host compiler in a standalone build, and just-built Clang otherwise.
368 if(NOT COMPILER_RT_STANDALONE_BUILD)
369 list(APPEND TEST_DEPS clang)
370 endif()
George Karpenkov769124d2017-08-15 22:56:10 +0000371
372 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
373 list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
374
Chandler Carruth332e1b12014-05-15 16:33:58 +0000375 # If we're not on MSVC, include the linker flags from CMAKE but override them
376 # with the provided link flags. This ensures that flags which are required to
377 # link programs at all are included, but the changes needed for the test
378 # trump. With MSVC we can't do that because CMake is set up to run link.exe
379 # when linking, not the compiler. Here, we hack it to use the compiler
380 # because we want to use -fsanitize flags.
381 if(NOT MSVC)
382 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
383 separate_arguments(TEST_LINK_FLAGS)
384 endif()
George Karpenkov93e9e8a2017-08-21 21:19:13 +0000385 add_custom_command(
386 OUTPUT "${output_bin}"
387 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000388 ${TEST_LINK_FLAGS}
George Karpenkov93e9e8a2017-08-21 21:19:13 +0000389 DEPENDS ${TEST_DEPS}
390 )
391 add_custom_target(T${test_name} DEPENDS "${output_bin}")
392 set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000393
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000394 # Make the test suite depend on the binary.
George Karpenkov93e9e8a2017-08-21 21:19:13 +0000395 add_dependencies(${test_suite} T${test_name})
George Karpenkovd46f17d2017-08-15 18:32:28 +0000396endfunction()
Alexey Samsonove8381352013-05-21 13:48:27 +0000397
Chris Bieneman86792ea2016-02-23 21:50:39 +0000398macro(add_compiler_rt_resource_file target_name file_name component)
Alexey Samsonove8381352013-05-21 13:48:27 +0000399 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonov1181a102014-02-18 14:28:53 +0000400 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000401 add_custom_command(OUTPUT ${dst_file}
402 DEPENDS ${src_file}
Alexey Samsonove8381352013-05-21 13:48:27 +0000403 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000404 COMMENT "Copying ${file_name}...")
405 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonove8381352013-05-21 13:48:27 +0000406 # Install in Clang resource directory.
Chris Bieneman86792ea2016-02-23 21:50:39 +0000407 install(FILES ${file_name}
408 DESTINATION ${COMPILER_RT_INSTALL_PATH}
409 COMPONENT ${component})
410 add_dependencies(${component} ${target_name})
Etienne Bergeronab42f4d2016-07-11 21:51:56 +0000411
412 set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
Alexey Samsonove8381352013-05-21 13:48:27 +0000413endmacro()
Evgeniy Stepanov322e89c2014-02-27 08:41:40 +0000414
415macro(add_compiler_rt_script name)
416 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
417 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
418 add_custom_command(OUTPUT ${dst}
419 DEPENDS ${src}
420 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
421 COMMENT "Copying ${name}...")
422 add_custom_target(${name} DEPENDS ${dst})
423 install(FILES ${dst}
424 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
425 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
426endmacro(add_compiler_rt_script src name)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000427
428# Builds custom version of libc++ and installs it in <prefix>.
429# Can be used to build sanitized versions of libc++ for running unit tests.
430# add_custom_libcxx(<name> <prefix>
431# DEPS <list of build deps>
432# CFLAGS <list of compile flags>)
433macro(add_custom_libcxx name prefix)
434 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
435 message(FATAL_ERROR "libcxx not found!")
436 endif()
437
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +0000438 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000439 foreach(flag ${LIBCXX_CFLAGS})
440 set(flagstr "${flagstr} ${flag}")
441 endforeach()
442 set(LIBCXX_CFLAGS ${flagstr})
443
444 if(NOT COMPILER_RT_STANDALONE_BUILD)
445 list(APPEND LIBCXX_DEPS clang)
446 endif()
447
448 ExternalProject_Add(${name}
449 PREFIX ${prefix}
450 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
Andy Gibbsabd2a0b2015-12-02 13:46:56 +0000451 CMAKE_ARGS -DCMAKE_MAKE_PROGRAM:STRING=${CMAKE_MAKE_PROGRAM}
452 -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
Daniel Sanders440b8612016-02-02 12:55:28 +0000453 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER}
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000454 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
455 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
456 -DCMAKE_BUILD_TYPE=Release
457 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Daniel Sanders440b8612016-02-02 12:55:28 +0000458 -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
Petr Hosek78342682017-01-16 00:33:02 +0000459 -DLIBCXX_STANDALONE_BUILD=On
Alexey Samsonov5af5f8f2014-05-12 21:07:28 +0000460 LOG_BUILD 1
461 LOG_CONFIGURE 1
462 LOG_INSTALL 1
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000463 )
Alexey Samsonov9e694e52015-07-16 21:20:05 +0000464 set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000465
466 ExternalProject_Add_Step(${name} force-reconfigure
467 DEPENDERS configure
468 ALWAYS 1
469 )
470
471 ExternalProject_Add_Step(${name} clobber
472 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
473 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
474 COMMENT "Clobberring ${name} build directory..."
475 DEPENDERS configure
476 DEPENDS ${LIBCXX_DEPS}
477 )
478endmacro()
Chris Bieneman69a372d2015-12-03 20:08:22 +0000479
480function(rt_externalize_debuginfo name)
481 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
482 return()
483 endif()
484
Chris Bieneman17bcc432016-03-31 21:17:19 +0000485 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
486 set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
487 endif()
488
Chris Bieneman69a372d2015-12-03 20:08:22 +0000489 if(APPLE)
490 if(CMAKE_CXX_FLAGS MATCHES "-flto"
491 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
492
493 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bieneman1d7a2cf2015-12-03 22:52:22 +0000494 set_property(TARGET ${name} APPEND_STRING PROPERTY
Chris Bienemanbcd09fe2015-12-03 22:56:21 +0000495 LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
Chris Bieneman69a372d2015-12-03 20:08:22 +0000496 endif()
497 add_custom_command(TARGET ${name} POST_BUILD
498 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman17bcc432016-03-31 21:17:19 +0000499 ${strip_command})
Chris Bieneman69a372d2015-12-03 20:08:22 +0000500 else()
501 message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
502 endif()
503endfunction()