blob: 6ed4f161628b0c814d72309b5ab84d1bc09b64ec [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})
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 Bergerona088b362016-07-11 21:51:56 +000063 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000064 if(APPLE)
Chris Bieneman19c84512015-08-13 20:38:16 +000065 set_target_properties(${libname} PROPERTIES
66 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000067 endif()
68 endforeach()
69endfunction()
Alexey Samsonov51623142013-01-20 14:36:12 +000070
Chris Bieneman97c46102015-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 Bieneman3bb55622016-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
Chris Bieneman0576c362015-08-25 19:53:09 +000089# Adds static or shared runtime for a list of architectures and operating
90# systems and puts it in the proper directory in the build and install trees.
91# add_compiler_rt_runtime(<name>
92# {STATIC|SHARED}
93# ARCHS <architectures>
94# OS <os list>
Alexey Samsonove18b7852014-03-31 13:45:36 +000095# SOURCES <source files>
96# CFLAGS <compile flags>
Chris Bieneman0576c362015-08-25 19:53:09 +000097# LINKFLAGS <linker flags>
Evgeniy Stepanov44014732014-09-29 13:18:55 +000098# DEFS <compile definitions>
Chris Bieneman0576c362015-08-25 19:53:09 +000099# LINK_LIBS <linked libraries> (only for shared library)
Chris Bieneman97c46102015-08-26 18:33:51 +0000100# OBJECT_LIBS <object libraries to use as sources>
Chris Bieneman0576c362015-08-25 19:53:09 +0000101# PARENT_TARGET <convenience parent target>)
102function(add_compiler_rt_runtime name type)
103 if(NOT type MATCHES "^(STATIC|SHARED)$")
104 message(FATAL_ERROR "type argument must be STATIC or SHARED")
105 return()
Alexey Samsonove16af952013-01-20 13:58:10 +0000106 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000107 cmake_parse_arguments(LIB
108 ""
109 "PARENT_TARGET"
Chris Bieneman97c46102015-08-26 18:33:51 +0000110 "OS;ARCHS;SOURCES;CFLAGS;LINKFLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
Chris Bieneman0576c362015-08-25 19:53:09 +0000111 ${ARGN})
112 set(libnames)
113 if(APPLE)
114 foreach(os ${LIB_OS})
115 if(type STREQUAL "STATIC")
116 set(libname "${name}_${os}")
117 else()
118 set(libname "${name}_${os}_dynamic")
119 set(extra_linkflags_${libname} ${DARWIN_${os}_LINKFLAGS} ${LIB_LINKFLAGS})
120 endif()
Daniel Sandersbfcbe762016-01-27 09:28:01 +0000121 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman0576c362015-08-25 19:53:09 +0000122 if(LIB_ARCHS_${libname})
123 list(APPEND libnames ${libname})
124 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${LIB_CFLAGS})
125 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
Chris Bieneman97c46102015-08-26 18:33:51 +0000126 set(sources_${libname} ${LIB_SOURCES})
127 format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000128 endif()
129 endforeach()
130 else()
131 foreach(arch ${LIB_ARCHS})
132 if(NOT CAN_TARGET_${arch})
133 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
134 return()
135 endif()
136 if(type STREQUAL "STATIC")
137 set(libname "${name}-${arch}")
138 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
139 else()
140 set(libname "${name}-dynamic-${arch}")
Etienne Bergerona6c33c42016-06-21 14:46:32 +0000141 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
142 set(extra_linkflags_${libname} ${TARGET_${arch}_LINKFLAGS} ${LIB_LINKFLAGS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000143 if(WIN32)
144 set(output_name_${libname} ${name}_dynamic-${arch}${COMPILER_RT_OS_SUFFIX})
145 else()
146 set(output_name_${libname} ${name}-${arch}${COMPILER_RT_OS_SUFFIX})
147 endif()
148 endif()
Chris Bieneman97c46102015-08-26 18:33:51 +0000149 set(sources_${libname} ${LIB_SOURCES})
150 format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
Chris Bieneman0576c362015-08-25 19:53:09 +0000151 set(libnames ${libnames} ${libname})
152 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
153 endforeach()
154 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +0000155
Chris Bieneman0576c362015-08-25 19:53:09 +0000156 if(NOT libnames)
157 return()
158 endif()
159
160 if(LIB_PARENT_TARGET)
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000161 # If the parent targets aren't created we should create them
162 if(NOT TARGET ${LIB_PARENT_TARGET})
163 add_custom_target(${LIB_PARENT_TARGET})
164 endif()
165 if(NOT TARGET install-${LIB_PARENT_TARGET})
166 # The parent install target specifies the parent component to scrape up
167 # anything not installed by the individual install targets, and to handle
168 # installation when running the multi-configuration generators.
169 add_custom_target(install-${LIB_PARENT_TARGET}
170 DEPENDS ${LIB_PARENT_TARGET}
171 COMMAND "${CMAKE_COMMAND}"
172 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
173 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
Etienne Bergerona088b362016-07-11 21:51:56 +0000174 set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES
175 FOLDER "Compiler-RT Misc")
Chris Bienemana213e3e2016-08-19 22:17:46 +0000176 add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET})
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000177 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000178 endif()
179
180 foreach(libname ${libnames})
Etienne Bergerona088b362016-07-11 21:51:56 +0000181 # If you are using a multi-configuration generator we don't generate
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000182 # per-library install rules, so we fall back to the parent target COMPONENT
183 if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
184 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
185 else()
186 set(COMPONENT_OPTION COMPONENT ${libname})
187 endif()
188
Chris Bieneman97c46102015-08-26 18:33:51 +0000189 add_library(${libname} ${type} ${sources_${libname}})
Chris Bieneman0576c362015-08-25 19:53:09 +0000190 set_target_compile_flags(${libname} ${extra_cflags_${libname}})
191 set_target_link_flags(${libname} ${extra_linkflags_${libname}})
Etienne Bergerona088b362016-07-11 21:51:56 +0000192 set_property(TARGET ${libname} APPEND PROPERTY
Chris Bieneman0576c362015-08-25 19:53:09 +0000193 COMPILE_DEFINITIONS ${LIB_DEFS})
Etienne Bergerona088b362016-07-11 21:51:56 +0000194 set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Chris Bieneman0576c362015-08-25 19:53:09 +0000195 set_target_properties(${libname} PROPERTIES
196 OUTPUT_NAME ${output_name_${libname}})
Etienne Bergerona088b362016-07-11 21:51:56 +0000197 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
Chris Bieneman0576c362015-08-25 19:53:09 +0000198 if(LIB_LINK_LIBS AND ${type} STREQUAL "SHARED")
199 target_link_libraries(${libname} ${LIB_LINK_LIBS})
Chris Bieneman607a4f72015-08-18 17:32:18 +0000200 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000201 install(TARGETS ${libname}
202 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
203 ${COMPONENT_OPTION}
204 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
205 ${COMPONENT_OPTION}
206 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
207 ${COMPONENT_OPTION})
Chris Bienemanf0dfa192016-02-23 21:55:38 +0000208
209 # We only want to generate per-library install targets if you aren't using
210 # an IDE because the extra targets get cluttered in IDEs.
211 if(NOT CMAKE_CONFIGURATION_TYPES)
212 add_custom_target(install-${libname}
213 DEPENDS ${libname}
214 COMMAND "${CMAKE_COMMAND}"
215 -DCMAKE_INSTALL_COMPONENT=${libname}
216 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
217 # If you have a parent target specified, we bind the new install target
218 # to the parent install target.
219 if(LIB_PARENT_TARGET)
220 add_dependencies(install-${LIB_PARENT_TARGET} install-${libname})
221 endif()
222 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000223 if(APPLE)
224 set_target_properties(${libname} PROPERTIES
225 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
226 endif()
Chris Bieneman1d908be2015-12-03 20:08:22 +0000227
228 if(type STREQUAL "SHARED")
229 rt_externalize_debuginfo(${libname})
230 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000231 endforeach()
232 if(LIB_PARENT_TARGET)
Alexey Samsonov124a8a52016-02-26 20:59:40 +0000233 add_dependencies(${LIB_PARENT_TARGET} ${libnames})
Chris Bieneman607a4f72015-08-18 17:32:18 +0000234 endif()
235endfunction()
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000236
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000237# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help
238# in compilation and linking of unittests.
239string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
240set(COMPILER_RT_UNITTEST_LINKFLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000241
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000242# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000243set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000244set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000245set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000246 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000247 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000248 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000249 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000250)
251
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000252append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
Alexey Samsonov001d0382015-01-06 20:58:40 +0000253
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000254if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000255 # clang doesn't support exceptions on Windows yet.
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000256 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000257
258 # We should teach clang to understand "#pragma intrinsic", see PR19898.
Sumanth Gundapanenid52305f2016-01-14 18:18:49 +0000259 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000260
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000261 # Clang doesn't support SEH on Windows yet.
262 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000263
264 # gtest use a lot of stuff marked as deprecated on Windows.
265 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000266
267 # Visual Studio 2012 only supports up to 8 template parameters in
268 # std::tr1::tuple by default, but gtest requires 10
269 if(MSVC_VERSION EQUAL 1700)
Ehsan Akhgari494410f2014-09-25 20:42:49 +0000270 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000271 endif()
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000272endif()
273
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000274# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
275# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000276# test_suite.
277# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000278# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000279# OBJECTS <object files>
280# DEPS <deps (e.g. runtime libs)>
281# LINK_FLAGS <link flags>)
282macro(add_compiler_rt_test test_suite test_name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000283 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Etienne Bergeron4659e362016-05-16 14:58:07 +0000284 set(output_bin ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000285 if(TEST_SUBDIR)
Etienne Bergeron4659e362016-05-16 14:58:07 +0000286 set(output_bin "${output_bin}/${TEST_SUBDIR}")
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000287 endif()
Etienne Bergeron4659e362016-05-16 14:58:07 +0000288 if(CMAKE_CONFIGURATION_TYPES)
289 set(output_bin "${output_bin}/${CMAKE_CFG_INTDIR}")
290 endif()
291 set(output_bin "${output_bin}/${test_name}")
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000292 if(MSVC)
293 set(output_bin "${output_bin}.exe")
294 endif()
Etienne Bergeron4659e362016-05-16 14:58:07 +0000295
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000296 # Use host compiler in a standalone build, and just-built Clang otherwise.
297 if(NOT COMPILER_RT_STANDALONE_BUILD)
298 list(APPEND TEST_DEPS clang)
299 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000300 # If we're not on MSVC, include the linker flags from CMAKE but override them
301 # with the provided link flags. This ensures that flags which are required to
302 # link programs at all are included, but the changes needed for the test
303 # trump. With MSVC we can't do that because CMake is set up to run link.exe
304 # when linking, not the compiler. Here, we hack it to use the compiler
305 # because we want to use -fsanitize flags.
306 if(NOT MSVC)
307 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
308 separate_arguments(TEST_LINK_FLAGS)
309 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000310 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000311 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000312 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000313 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000314 DEPENDS ${TEST_DEPS})
Etienne Bergerona088b362016-07-11 21:51:56 +0000315 set_target_properties(${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
316
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000317 # Make the test suite depend on the binary.
318 add_dependencies(${test_suite} ${test_name})
319endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000320
Chris Bieneman772a58d2016-02-23 21:50:39 +0000321macro(add_compiler_rt_resource_file target_name file_name component)
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000322 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000323 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000324 add_custom_command(OUTPUT ${dst_file}
325 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000326 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000327 COMMENT "Copying ${file_name}...")
328 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000329 # Install in Clang resource directory.
Chris Bieneman772a58d2016-02-23 21:50:39 +0000330 install(FILES ${file_name}
331 DESTINATION ${COMPILER_RT_INSTALL_PATH}
332 COMPONENT ${component})
333 add_dependencies(${component} ${target_name})
Etienne Bergerona088b362016-07-11 21:51:56 +0000334
335 set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000336endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000337
338macro(add_compiler_rt_script name)
339 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
340 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
341 add_custom_command(OUTPUT ${dst}
342 DEPENDS ${src}
343 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
344 COMMENT "Copying ${name}...")
345 add_custom_target(${name} DEPENDS ${dst})
346 install(FILES ${dst}
347 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
348 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
349endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000350
351# Builds custom version of libc++ and installs it in <prefix>.
352# Can be used to build sanitized versions of libc++ for running unit tests.
353# add_custom_libcxx(<name> <prefix>
354# DEPS <list of build deps>
355# CFLAGS <list of compile flags>)
356macro(add_custom_libcxx name prefix)
357 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
358 message(FATAL_ERROR "libcxx not found!")
359 endif()
360
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000361 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000362 foreach(flag ${LIBCXX_CFLAGS})
363 set(flagstr "${flagstr} ${flag}")
364 endforeach()
365 set(LIBCXX_CFLAGS ${flagstr})
366
367 if(NOT COMPILER_RT_STANDALONE_BUILD)
368 list(APPEND LIBCXX_DEPS clang)
369 endif()
370
371 ExternalProject_Add(${name}
372 PREFIX ${prefix}
373 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
Andy Gibbs61adea82015-12-02 13:46:56 +0000374 CMAKE_ARGS -DCMAKE_MAKE_PROGRAM:STRING=${CMAKE_MAKE_PROGRAM}
375 -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
Daniel Sandersa268a2f2016-02-02 12:55:28 +0000376 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER}
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000377 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
378 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
379 -DCMAKE_BUILD_TYPE=Release
380 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Daniel Sandersa268a2f2016-02-02 12:55:28 +0000381 -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000382 LOG_BUILD 1
383 LOG_CONFIGURE 1
384 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000385 )
Alexey Samsonovdd6f8442015-07-16 21:20:05 +0000386 set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000387
388 ExternalProject_Add_Step(${name} force-reconfigure
389 DEPENDERS configure
390 ALWAYS 1
391 )
392
393 ExternalProject_Add_Step(${name} clobber
394 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
395 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
396 COMMENT "Clobberring ${name} build directory..."
397 DEPENDERS configure
398 DEPENDS ${LIBCXX_DEPS}
399 )
400endmacro()
Chris Bieneman1d908be2015-12-03 20:08:22 +0000401
402function(rt_externalize_debuginfo name)
403 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
404 return()
405 endif()
406
Chris Bieneman8a782562016-03-31 21:17:19 +0000407 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
408 set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
409 endif()
410
Chris Bieneman1d908be2015-12-03 20:08:22 +0000411 if(APPLE)
412 if(CMAKE_CXX_FLAGS MATCHES "-flto"
413 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
414
415 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
Chris Bieneman5d5bc5b2015-12-03 22:52:22 +0000416 set_property(TARGET ${name} APPEND_STRING PROPERTY
Chris Bieneman960c4902015-12-03 22:56:21 +0000417 LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
Chris Bieneman1d908be2015-12-03 20:08:22 +0000418 endif()
419 add_custom_command(TARGET ${name} POST_BUILD
420 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
Chris Bieneman8a782562016-03-31 21:17:19 +0000421 ${strip_command})
Chris Bieneman1d908be2015-12-03 20:08:22 +0000422 else()
423 message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
424 endif()
425endfunction()