blob: 4372673186621b2483cf83e3c4efa50fb445b6fa [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
Kuba Mracek0a728802017-11-29 19:27:25 +000096function(add_asm_sources output)
97 set(${output} ${ARGN} PARENT_SCOPE)
98 # Xcode will try to compile asm files as C ('clang -x c'), and that will fail.
99 if (${CMAKE_GENERATOR} STREQUAL "Xcode")
100 enable_language(ASM)
101 else()
102 # Pass ASM file directly to the C++ compiler.
103 set_source_files_properties(${ARGN} PROPERTIES LANGUAGE C)
104 endif()
105endfunction()
106
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000107macro(set_output_name output name arch)
108 if(ANDROID AND ${arch} STREQUAL "i386")
109 set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
110 else()
111 set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
112 endif()
113endmacro()
114
Chris Bieneman0576c362015-08-25 19:53:09 +0000115# Adds static or shared runtime for a list of architectures and operating
116# systems and puts it in the proper directory in the build and install trees.
117# add_compiler_rt_runtime(<name>
118# {STATIC|SHARED}
119# ARCHS <architectures>
120# OS <os list>
Alexey Samsonove18b7852014-03-31 13:45:36 +0000121# SOURCES <source files>
122# CFLAGS <compile flags>
Francis Ricci9f931132017-01-10 04:33:04 +0000123# LINK_FLAGS <linker flags>
Evgeniy Stepanov44014732014-09-29 13:18:55 +0000124# DEFS <compile definitions>
Chris Bieneman0576c362015-08-25 19:53:09 +0000125# LINK_LIBS <linked libraries> (only for shared library)
Chris Bieneman97c46102015-08-26 18:33:51 +0000126# OBJECT_LIBS <object libraries to use as sources>
Chris Bieneman0576c362015-08-25 19:53:09 +0000127# PARENT_TARGET <convenience parent target>)
128function(add_compiler_rt_runtime name type)
129 if(NOT type MATCHES "^(STATIC|SHARED)$")
130 message(FATAL_ERROR "type argument must be STATIC or SHARED")
131 return()
Alexey Samsonove16af952013-01-20 13:58:10 +0000132 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000133 cmake_parse_arguments(LIB
134 ""
135 "PARENT_TARGET"
Francis Ricci9f931132017-01-10 04:33:04 +0000136 "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
Chris Bieneman0576c362015-08-25 19:53:09 +0000137 ${ARGN})
138 set(libnames)
Bob Haarman955475a2017-03-22 17:25:49 +0000139 # Until we support this some other way, build compiler-rt runtime without LTO
140 # to allow non-LTO projects to link with it.
141 if(COMPILER_RT_HAS_FNO_LTO_FLAG)
142 set(NO_LTO_FLAGS "-fno-lto")
143 else()
144 set(NO_LTO_FLAGS "")
145 endif()
Vedant Kumar160179b2017-09-01 23:23:59 +0000146
Chris Bieneman0576c362015-08-25 19:53:09 +0000147 if(APPLE)
148 foreach(os ${LIB_OS})
Vedant Kumar160179b2017-09-01 23:23:59 +0000149 # Strip out -msse3 if this isn't macOS.
Vedant Kumar160179b2017-09-01 23:23:59 +0000150 list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
151 if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
152 list(REMOVE_ITEM LIB_CFLAGS "-msse3")
153 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000154 if(type STREQUAL "STATIC")
155 set(libname "${name}_${os}")
156 else()
157 set(libname "${name}_${os}_dynamic")
Francis Ricci9f931132017-01-10 04:33:04 +0000158 set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000159 endif()
Daniel Sandersbfcbe762016-01-27 09:28:01 +0000160 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman0576c362015-08-25 19:53:09 +0000161 if(LIB_ARCHS_${libname})
162 list(APPEND libnames ${libname})
Bob Haarman955475a2017-03-22 17:25:49 +0000163 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000164 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
Chris Bieneman97c46102015-08-26 18:33:51 +0000165 set(sources_${libname} ${LIB_SOURCES})
166 format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000167 endif()
168 endforeach()
169 else()
170 foreach(arch ${LIB_ARCHS})
171 if(NOT CAN_TARGET_${arch})
172 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
173 return()
174 endif()
175 if(type STREQUAL "STATIC")
176 set(libname "${name}-${arch}")
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000177 set_output_name(output_name_${libname} ${name} ${arch})
Chris Bieneman0576c362015-08-25 19:53:09 +0000178 else()
179 set(libname "${name}-dynamic-${arch}")
Etienne Bergerona6c33c42016-06-21 14:46:32 +0000180 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Francis Ricci9f931132017-01-10 04:33:04 +0000181 set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000182 if(WIN32)
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000183 set_output_name(output_name_${libname} ${name}_dynamic ${arch})
Chris Bieneman0576c362015-08-25 19:53:09 +0000184 else()
Evgeniy Stepanov20bf9b42017-08-29 22:12:31 +0000185 set_output_name(output_name_${libname} ${name} ${arch})
Chris Bieneman0576c362015-08-25 19:53:09 +0000186 endif()
187 endif()
Chris Bieneman97c46102015-08-26 18:33:51 +0000188 set(sources_${libname} ${LIB_SOURCES})
189 format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000190 set(libnames ${libnames} ${libname})
Bob Haarman955475a2017-03-22 17:25:49 +0000191 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000192 endforeach()
193 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +0000194
Chris Bieneman0576c362015-08-25 19:53:09 +0000195 if(NOT libnames)
196 return()
197 endif()
198
199 if(LIB_PARENT_TARGET)
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000200 # If the parent targets aren't created we should create them
201 if(NOT TARGET ${LIB_PARENT_TARGET})
202 add_custom_target(${LIB_PARENT_TARGET})
203 endif()
204 if(NOT TARGET install-${LIB_PARENT_TARGET})
205 # The parent install target specifies the parent component to scrape up
206 # anything not installed by the individual install targets, and to handle
207 # installation when running the multi-configuration generators.
208 add_custom_target(install-${LIB_PARENT_TARGET}
209 DEPENDS ${LIB_PARENT_TARGET}
210 COMMAND "${CMAKE_COMMAND}"
211 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
212 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Shoaib Meenaidb056522017-12-01 19:06:29 +0000213 add_custom_target(install-${LIB_PARENT_TARGET}-stripped
214 DEPENDS ${LIB_PARENT_TARGET}
215 COMMAND "${CMAKE_COMMAND}"
216 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
217 -DCMAKE_INSTALL_DO_STRIP=1
218 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Etienne Bergerona088b362016-07-11 21:51:56 +0000219 set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES
220 FOLDER "Compiler-RT Misc")
Shoaib Meenaidb056522017-12-01 19:06:29 +0000221 set_target_properties(install-${LIB_PARENT_TARGET}-stripped PROPERTIES
222 FOLDER "Compiler-RT Misc")
Chris Bienemana213e3e2016-08-19 22:17:46 +0000223 add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET})
Shoaib Meenaidb056522017-12-01 19:06:29 +0000224 add_dependencies(install-compiler-rt-stripped install-${LIB_PARENT_TARGET}-stripped)
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000225 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000226 endif()
227
228 foreach(libname ${libnames})
Etienne Bergerona088b362016-07-11 21:51:56 +0000229 # If you are using a multi-configuration generator we don't generate
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000230 # per-library install rules, so we fall back to the parent target COMPONENT
231 if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
232 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
233 else()
234 set(COMPONENT_OPTION COMPONENT ${libname})
235 endif()
236
Chris Bieneman97c46102015-08-26 18:33:51 +0000237 add_library(${libname} ${type} ${sources_${libname}})
Chris Bieneman0576c362015-08-25 19:53:09 +0000238 set_target_compile_flags(${libname} ${extra_cflags_${libname}})
Francis Ricci9f931132017-01-10 04:33:04 +0000239 set_target_link_flags(${libname} ${extra_link_flags_${libname}})
Etienne Bergerona088b362016-07-11 21:51:56 +0000240 set_property(TARGET ${libname} APPEND PROPERTY
Chris Bieneman0576c362015-08-25 19:53:09 +0000241 COMPILE_DEFINITIONS ${LIB_DEFS})
Etienne Bergerona088b362016-07-11 21:51:56 +0000242 set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Chris Bieneman0576c362015-08-25 19:53:09 +0000243 set_target_properties(${libname} PROPERTIES
244 OUTPUT_NAME ${output_name_${libname}})
Etienne Bergerona088b362016-07-11 21:51:56 +0000245 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
George Karpenkov0c8339c2017-08-21 23:25:50 +0000246 if(LIB_LINK_LIBS)
247 target_link_libraries(${libname} ${LIB_LINK_LIBS})
248 endif()
Francis Ricci4a0c5cd2016-09-07 20:32:48 +0000249 if(${type} STREQUAL "SHARED")
Francis Ricci4a0c5cd2016-09-07 20:32:48 +0000250 if(WIN32 AND NOT CYGWIN AND NOT MINGW)
251 set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
252 set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
253 endif()
Kuba Mracekfcf0ff12017-04-26 18:59:22 +0000254 if(APPLE)
255 # Ad-hoc sign the dylibs
256 add_custom_command(TARGET ${libname}
257 POST_BUILD
258 COMMAND codesign --sign - $<TARGET_FILE:${libname}>
259 WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
260 )
261 endif()
Chris Bieneman607a4f72015-08-18 17:32:18 +0000262 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000263 install(TARGETS ${libname}
264 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
265 ${COMPONENT_OPTION}
266 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
267 ${COMPONENT_OPTION}
268 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
269 ${COMPONENT_OPTION})
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000270
271 # We only want to generate per-library install targets if you aren't using
272 # an IDE because the extra targets get cluttered in IDEs.
273 if(NOT CMAKE_CONFIGURATION_TYPES)
274 add_custom_target(install-${libname}
275 DEPENDS ${libname}
276 COMMAND "${CMAKE_COMMAND}"
277 -DCMAKE_INSTALL_COMPONENT=${libname}
278 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Shoaib Meenaidb056522017-12-01 19:06:29 +0000279 add_custom_target(install-${libname}-stripped
280 DEPENDS ${libname}
281 COMMAND "${CMAKE_COMMAND}"
282 -DCMAKE_INSTALL_COMPONENT=${libname}
283 -DCMAKE_INSTALL_DO_STRIP=1
284 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000285 # If you have a parent target specified, we bind the new install target
286 # to the parent install target.
287 if(LIB_PARENT_TARGET)
288 add_dependencies(install-${LIB_PARENT_TARGET} install-${libname})
Shoaib Meenaidb056522017-12-01 19:06:29 +0000289 add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${libname}-stripped)
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000290 endif()
291 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000292 if(APPLE)
293 set_target_properties(${libname} PROPERTIES
294 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
295 endif()
Chris Bieneman1d908be2015-12-03 20:08:22 +0000296
297 if(type STREQUAL "SHARED")
298 rt_externalize_debuginfo(${libname})
299 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000300 endforeach()
301 if(LIB_PARENT_TARGET)
Alexey Samsonov124a8a52016-02-26 20:59:40 +0000302 add_dependencies(${LIB_PARENT_TARGET} ${libnames})
Chris Bieneman607a4f72015-08-18 17:32:18 +0000303 endif()
304endfunction()
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000305
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000306# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help
307# in compilation and linking of unittests.
308string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
Francis Ricci9f931132017-01-10 04:33:04 +0000309set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000310
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000311# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000312set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000313set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000314set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000315 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000316 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000317 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000318 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000319)
320
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000321append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
Chandler Carruthe78a0bf2017-01-05 03:41:41 +0000322append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
Alexey Samsonov001d0382015-01-06 20:58:40 +0000323
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000324if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000325 # clang doesn't support exceptions on Windows yet.
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000326 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000327
328 # We should teach clang to understand "#pragma intrinsic", see PR19898.
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000329 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000330
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000331 # Clang doesn't support SEH on Windows yet.
332 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000333
334 # gtest use a lot of stuff marked as deprecated on Windows.
335 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000336endif()
337
George Karpenkov38e269e2017-08-15 22:56:10 +0000338# Compile and register compiler-rt tests.
339# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
340# <test architecture>
341# KIND <custom prefix>
342# SUBDIR <subdirectory for testing binary>
343# SOURCES <sources to compile>
344# RUNTIME <tests runtime to link in>
345# CFLAGS <compile-time flags>
346# COMPILE_DEPS <compile-time dependencies>
347# DEPS <dependencies>
348# LINK_FLAGS <flags to use during linking>
349# )
350function(generate_compiler_rt_tests test_objects test_suite testname arch)
351 cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
352 "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
353
354 foreach(source ${TEST_SOURCES})
355 sanitizer_test_compile(
356 "${test_objects}" "${source}" "${arch}"
357 KIND ${TEST_KIND}
358 COMPILE_DEPS ${TEST_COMPILE_DEPS}
359 DEPS ${TEST_DEPS}
360 CFLAGS ${TEST_CFLAGS}
361 )
362 endforeach()
363
364 set(TEST_DEPS ${${test_objects}})
365
366 if(NOT "${TEST_RUNTIME}" STREQUAL "")
367 list(APPEND TEST_DEPS ${TEST_RUNTIME})
368 list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
369 endif()
370
371 add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
372 SUBDIR ${TEST_SUBDIR}
373 OBJECTS ${${test_objects}}
374 DEPS ${TEST_DEPS}
George Karpenkovbcb584b2017-08-15 23:22:52 +0000375 LINK_FLAGS ${TEST_LINK_FLAGS}
George Karpenkov38e269e2017-08-15 22:56:10 +0000376 )
377 set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
378endfunction()
379
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000380# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
381# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000382# test_suite.
George Karpenkov38e269e2017-08-15 22:56:10 +0000383# add_compiler_rt_test(<test_suite> <test_name> <arch>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000384# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000385# OBJECTS <object files>
386# DEPS <deps (e.g. runtime libs)>
387# LINK_FLAGS <link flags>)
George Karpenkov38e269e2017-08-15 22:56:10 +0000388function(add_compiler_rt_test test_suite test_name arch)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000389 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000390 set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000391 if(TEST_SUBDIR)
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000392 set(output_dir "${output_dir}/${TEST_SUBDIR}")
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000393 endif()
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000394 set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
395 file(MAKE_DIRECTORY "${output_dir}")
396 set(output_bin "${output_dir}/${test_name}")
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000397 if(MSVC)
398 set(output_bin "${output_bin}.exe")
399 endif()
Etienne Bergeron4659e362016-05-16 14:58:07 +0000400
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000401 # Use host compiler in a standalone build, and just-built Clang otherwise.
402 if(NOT COMPILER_RT_STANDALONE_BUILD)
403 list(APPEND TEST_DEPS clang)
404 endif()
George Karpenkov38e269e2017-08-15 22:56:10 +0000405
406 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
407 list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
408
Chandler Carruth272eef92014-05-15 16:33:58 +0000409 # If we're not on MSVC, include the linker flags from CMAKE but override them
410 # with the provided link flags. This ensures that flags which are required to
411 # link programs at all are included, but the changes needed for the test
412 # trump. With MSVC we can't do that because CMake is set up to run link.exe
413 # when linking, not the compiler. Here, we hack it to use the compiler
414 # because we want to use -fsanitize flags.
415 if(NOT MSVC)
416 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
417 separate_arguments(TEST_LINK_FLAGS)
418 endif()
George Karpenkov916b56d2017-08-21 21:19:13 +0000419 add_custom_command(
420 OUTPUT "${output_bin}"
421 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000422 ${TEST_LINK_FLAGS}
George Karpenkov916b56d2017-08-21 21:19:13 +0000423 DEPENDS ${TEST_DEPS}
424 )
425 add_custom_target(T${test_name} DEPENDS "${output_bin}")
426 set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
Etienne Bergerona088b362016-07-11 21:51:56 +0000427
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000428 # Make the test suite depend on the binary.
George Karpenkov916b56d2017-08-21 21:19:13 +0000429 add_dependencies(${test_suite} T${test_name})
George Karpenkov8ffc41a2017-08-15 18:32:28 +0000430endfunction()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000431
Chris Bieneman772a58d2016-02-23 21:50:39 +0000432macro(add_compiler_rt_resource_file target_name file_name component)
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000433 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Petr Hosekb6c53ea2018-01-14 03:43:14 +0000434 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000435 add_custom_command(OUTPUT ${dst_file}
436 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000437 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000438 COMMENT "Copying ${file_name}...")
439 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000440 # Install in Clang resource directory.
Chris Bieneman772a58d2016-02-23 21:50:39 +0000441 install(FILES ${file_name}
Petr Hosekb6c53ea2018-01-14 03:43:14 +0000442 DESTINATION ${COMPILER_RT_INSTALL_PATH}/share
Chris Bieneman772a58d2016-02-23 21:50:39 +0000443 COMPONENT ${component})
444 add_dependencies(${component} ${target_name})
Etienne Bergerona088b362016-07-11 21:51:56 +0000445
446 set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000447endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000448
449macro(add_compiler_rt_script name)
450 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
451 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
452 add_custom_command(OUTPUT ${dst}
453 DEPENDS ${src}
454 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
455 COMMENT "Copying ${name}...")
456 add_custom_target(${name} DEPENDS ${dst})
457 install(FILES ${dst}
458 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
459 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
460endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000461
462# Builds custom version of libc++ and installs it in <prefix>.
463# Can be used to build sanitized versions of libc++ for running unit tests.
464# add_custom_libcxx(<name> <prefix>
465# DEPS <list of build deps>
Petr Hosek7ccfd602018-01-21 01:01:53 +0000466# CFLAGS <list of compile flags>
467# USE_TOOLCHAIN)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000468macro(add_custom_libcxx name prefix)
Petr Hosekeff01552017-12-12 01:20:52 +0000469 if(NOT COMPILER_RT_LIBCXX_PATH)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000470 message(FATAL_ERROR "libcxx not found!")
471 endif()
472
Petr Hosek7ccfd602018-01-21 01:01:53 +0000473 cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000474 foreach(flag ${LIBCXX_CFLAGS})
475 set(flagstr "${flagstr} ${flag}")
476 endforeach()
477 set(LIBCXX_CFLAGS ${flagstr})
478
Petr Hosek7ccfd602018-01-21 01:01:53 +0000479 if(LIBCXX_USE_TOOLCHAIN)
480 set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
481 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER})
482 if(NOT COMPILER_RT_STANDALONE_BUILD)
483 set(force_deps DEPENDS clang)
484 endif()
Petr Hosek06545882018-01-21 03:22:22 +0000485 else()
486 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
487 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
Petr Hosek7ccfd602018-01-21 01:01:53 +0000488 endif()
489
490 if(CMAKE_SYSROOT)
491 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000492 endif()
493
494 ExternalProject_Add(${name}
Petr Hosek7ccfd602018-01-21 01:01:53 +0000495 DEPENDS ${LIBCXX_DEPS}
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000496 PREFIX ${prefix}
497 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
Petr Hosek7ccfd602018-01-21 01:01:53 +0000498 CMAKE_ARGS -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
499 ${compiler_args}
500 ${sysroot_arg}
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000501 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
502 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
503 -DCMAKE_BUILD_TYPE=Release
Petr Hosek7ccfd602018-01-21 01:01:53 +0000504 -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
Daniel Sandersa268a2f2016-02-02 12:55:28 +0000505 -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
Petr Hosekb0495c32017-01-16 00:33:02 +0000506 -DLIBCXX_STANDALONE_BUILD=On
Petr Hosek9090db42017-12-21 20:04:10 +0000507 ${LIBCXX_CMAKE_ARGS}
Petr Hosek7ccfd602018-01-21 01:01:53 +0000508 STEP_TARGETS configure build install
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000509 LOG_BUILD 1
510 LOG_CONFIGURE 1
511 LOG_INSTALL 1
Petr Hosek7ccfd602018-01-21 01:01:53 +0000512 EXCLUDE_FROM_ALL TRUE
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000513 )
514
515 ExternalProject_Add_Step(${name} force-reconfigure
516 DEPENDERS configure
517 ALWAYS 1
518 )
519
520 ExternalProject_Add_Step(${name} clobber
521 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
522 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
523 COMMENT "Clobberring ${name} build directory..."
524 DEPENDERS configure
Petr Hosek7ccfd602018-01-21 01:01:53 +0000525 ${force_deps}
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000526 )
527endmacro()
Chris Bieneman1d908be2015-12-03 20:08:22 +0000528
529function(rt_externalize_debuginfo name)
530 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
531 return()
532 endif()
533
Chris Bieneman8a782562016-03-31 21:17:19 +0000534 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
535 set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
536 endif()
537
Chris Bieneman1d908be2015-12-03 20:08:22 +0000538 if(APPLE)
539 if(CMAKE_CXX_FLAGS MATCHES "-flto"
540 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
541
542 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bieneman5d5bc5b2015-12-03 22:52:22 +0000543 set_property(TARGET ${name} APPEND_STRING PROPERTY
Chris Bieneman960c4902015-12-03 22:56:21 +0000544 LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
Chris Bieneman1d908be2015-12-03 20:08:22 +0000545 endif()
546 add_custom_command(TARGET ${name} POST_BUILD
547 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman8a782562016-03-31 21:17:19 +0000548 ${strip_command})
Chris Bieneman1d908be2015-12-03 20:08:22 +0000549 else()
550 message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
551 endif()
552endfunction()
Greg Bedwell1ad28c72017-11-13 12:57:54 +0000553
554
555# Configure lit configuration files, including compiler-rt specific variables.
556function(configure_compiler_rt_lit_site_cfg input output)
557 set_llvm_build_mode()
558
559 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
560 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
561
562 configure_lit_site_cfg(${input} ${output})
563endfunction()