blob: 52cbe35ddcd43501b8cd91cf635d3f43c203c329 [file] [log] [blame]
Alexey Samsonov02dcc632012-12-19 12:33:39 +00001include(AddLLVM)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +00002include(ExternalProject)
Alexey Samsonov392c50d2013-01-18 16:05:21 +00003include(CompilerRTUtils)
Alexey Samsonov02dcc632012-12-19 12:33:39 +00004
Chris Bieneman5c2fd242015-06-10 23:55:07 +00005# Tries to add an "object library" target for a given list of OSs and/or
6# architectures with name "<name>.<arch>" for non-Darwin platforms if
7# architecture can be targeted, and "<name>.<os>" for Darwin platforms.
8# add_compiler_rt_object_libraries(<name>
Filipe Cabecinhasc309de72015-06-19 03:39:24 +00009# OS <os names>
10# ARCHS <architectures>
Chris Bieneman5c2fd242015-06-10 23:55:07 +000011# SOURCES <source files>
12# CFLAGS <compile flags>
13# DEFS <compile definitions>)
14function(add_compiler_rt_object_libraries name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000015 cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Chris Bieneman5c2fd242015-06-10 23:55:07 +000016 set(libnames)
17 if(APPLE)
18 foreach(os ${LIB_OS})
19 set(libname "${name}.${os}")
20 set(libnames ${libnames} ${libname})
21 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS})
22 endforeach()
Alexey Samsonov392c50d2013-01-18 16:05:21 +000023 else()
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000024 foreach(arch ${LIB_ARCHS})
Chris Bieneman5c2fd242015-06-10 23:55:07 +000025 set(libname "${name}.${arch}")
26 set(libnames ${libnames} ${libname})
27 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
28 if(NOT CAN_TARGET_${arch})
29 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
30 return()
31 endif()
32 endforeach()
Alexey Samsonov392c50d2013-01-18 16:05:21 +000033 endif()
Chris Bieneman5c2fd242015-06-10 23:55:07 +000034
35 foreach(libname ${libnames})
36 add_library(${libname} OBJECT ${LIB_SOURCES})
37 set_target_compile_flags(${libname}
38 ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${LIB_CFLAGS})
39 set_property(TARGET ${libname} APPEND PROPERTY
40 COMPILE_DEFINITIONS ${LIB_DEFS})
41 if(APPLE)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000042 set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCHS}")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000043 endif()
44 endforeach()
45endfunction()
Alexey Samsonov51623142013-01-20 14:36:12 +000046
Alexey Samsonove18b7852014-03-31 13:45:36 +000047# Adds static or shared runtime for a given architecture and puts it in the
48# proper directory in the build and install trees.
49# add_compiler_rt_runtime(<name> <arch> {STATIC,SHARED}
50# SOURCES <source files>
51# CFLAGS <compile flags>
Evgeniy Stepanov44014732014-09-29 13:18:55 +000052# DEFS <compile definitions>
53# OUTPUT_NAME <output library name>)
Alexey Samsonove18b7852014-03-31 13:45:36 +000054macro(add_compiler_rt_runtime name arch type)
Alexey Samsonove16af952013-01-20 13:58:10 +000055 if(CAN_TARGET_${arch})
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000056 cmake_parse_arguments(LIB "" "OUTPUT_NAME" "SOURCES;CFLAGS;LINKFLAGS;DEFS" ${ARGN})
Alexey Samsonove18b7852014-03-31 13:45:36 +000057 add_library(${name} ${type} ${LIB_SOURCES})
Alexey Samsonove16af952013-01-20 13:58:10 +000058 # Setup compile flags and definitions.
59 set_target_compile_flags(${name}
60 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Aaron Ballman208eae32014-10-29 19:25:20 +000061 set_target_link_flags(${name}
Evgeniy Stepanov976974a2015-05-05 20:13:39 +000062 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS})
Alexey Samsonove16af952013-01-20 13:58:10 +000063 set_property(TARGET ${name} APPEND PROPERTY
64 COMPILE_DEFINITIONS ${LIB_DEFS})
65 # Setup correct output directory in the build tree.
66 set_target_properties(${name} PROPERTIES
Alexey Samsonove18b7852014-03-31 13:45:36 +000067 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
Hans Wennborgb89adc42014-12-04 21:01:49 +000068 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
69 RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Evgeniy Stepanov44014732014-09-29 13:18:55 +000070 if ("${LIB_OUTPUT_NAME}" STREQUAL "")
71 set_target_properties(${name} PROPERTIES
72 OUTPUT_NAME ${name}${COMPILER_RT_OS_SUFFIX})
73 else()
Alexey Samsonovd6535ea2014-04-01 13:16:30 +000074 set_target_properties(${name} PROPERTIES
75 OUTPUT_NAME ${LIB_OUTPUT_NAME})
76 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +000077 # Add installation command.
78 install(TARGETS ${name}
Alexey Samsonove18b7852014-03-31 13:45:36 +000079 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
Hans Wennborgb89adc42014-12-04 21:01:49 +000080 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
81 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000082 else()
83 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
84 endif()
85endmacro()
86
Alexey Samsonove18b7852014-03-31 13:45:36 +000087# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000088# for several architectures.
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000089# add_compiler_rt_osx_static_runtime(<name> ARCHS <architectures>
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000090# SOURCES <source files>
91# CFLAGS <compile flags>
92# DEFS <compile definitions>)
93macro(add_compiler_rt_osx_static_runtime name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000094 cmake_parse_arguments(LIB "" "" "ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000095 add_library(${name} STATIC ${LIB_SOURCES})
96 set_target_compile_flags(${name} ${LIB_CFLAGS})
97 set_property(TARGET ${name} APPEND PROPERTY
98 COMPILE_DEFINITIONS ${LIB_DEFS})
99 set_target_properties(${name} PROPERTIES
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000100 OSX_ARCHITECTURES "${LIB_ARCHS}"
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000101 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
102 install(TARGETS ${name}
103 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
104endmacro()
105
Alexander Potapenko49496742013-11-07 10:08:19 +0000106# Adds dynamic runtime library on osx/iossim, which supports multiple
107# architectures.
108# add_compiler_rt_darwin_dynamic_runtime(<name> <os>
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000109# ARCHS <architectures>
Alexander Potapenko49496742013-11-07 10:08:19 +0000110# SOURCES <source files>
111# CFLAGS <compile flags>
112# DEFS <compile definitions>
113# LINKFLAGS <link flags>)
114macro(add_compiler_rt_darwin_dynamic_runtime name os)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000115 cmake_parse_arguments(LIB "" "" "ARCHS;SOURCES;CFLAGS;DEFS;LINKFLAGS" ${ARGN})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000116 add_library(${name} SHARED ${LIB_SOURCES})
Alexander Potapenko49496742013-11-07 10:08:19 +0000117 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
118 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000119 set_property(TARGET ${name} APPEND PROPERTY
120 COMPILE_DEFINITIONS ${LIB_DEFS})
121 set_target_properties(${name} PROPERTIES
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000122 OSX_ARCHITECTURES "${LIB_ARCHS}"
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000123 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
124 install(TARGETS ${name}
125 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
126endmacro()
127
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000128set(COMPILER_RT_TEST_CFLAGS)
129
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000130# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000131set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000132set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000133set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000134 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000135 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000136 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000137 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000138)
139
Alexey Samsonov001d0382015-01-06 20:58:40 +0000140append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS)
141
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000142if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000143 # clang doesn't support exceptions on Windows yet.
Alexey Samsonov001d0382015-01-06 20:58:40 +0000144 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000145
146 # We should teach clang to understand "#pragma intrinsic", see PR19898.
147 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline)
148
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000149 # Clang doesn't support SEH on Windows yet.
150 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000151
152 # gtest use a lot of stuff marked as deprecated on Windows.
153 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000154
155 # Visual Studio 2012 only supports up to 8 template parameters in
156 # std::tr1::tuple by default, but gtest requires 10
157 if(MSVC_VERSION EQUAL 1700)
Ehsan Akhgari494410f2014-09-25 20:42:49 +0000158 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000159 endif()
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000160endif()
161
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000162# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
163# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000164# test_suite.
165# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000166# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000167# OBJECTS <object files>
168# DEPS <deps (e.g. runtime libs)>
169# LINK_FLAGS <link flags>)
170macro(add_compiler_rt_test test_suite test_name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000171 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000172 if(TEST_SUBDIR)
173 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SUBDIR}/${test_name}")
174 else()
175 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
176 endif()
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000177 if(MSVC)
178 set(output_bin "${output_bin}.exe")
179 endif()
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000180 # Use host compiler in a standalone build, and just-built Clang otherwise.
181 if(NOT COMPILER_RT_STANDALONE_BUILD)
182 list(APPEND TEST_DEPS clang)
183 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000184 # If we're not on MSVC, include the linker flags from CMAKE but override them
185 # with the provided link flags. This ensures that flags which are required to
186 # link programs at all are included, but the changes needed for the test
187 # trump. With MSVC we can't do that because CMake is set up to run link.exe
188 # when linking, not the compiler. Here, we hack it to use the compiler
189 # because we want to use -fsanitize flags.
190 if(NOT MSVC)
191 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
192 separate_arguments(TEST_LINK_FLAGS)
193 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000194 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000195 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000196 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000197 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000198 DEPENDS ${TEST_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000199 # Make the test suite depend on the binary.
200 add_dependencies(${test_suite} ${test_name})
201endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000202
203macro(add_compiler_rt_resource_file target_name file_name)
204 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000205 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000206 add_custom_command(OUTPUT ${dst_file}
207 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000208 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000209 COMMENT "Copying ${file_name}...")
210 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000211 # Install in Clang resource directory.
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000212 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000213endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000214
215macro(add_compiler_rt_script name)
216 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
217 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
218 add_custom_command(OUTPUT ${dst}
219 DEPENDS ${src}
220 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
221 COMMENT "Copying ${name}...")
222 add_custom_target(${name} DEPENDS ${dst})
223 install(FILES ${dst}
224 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
225 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
226endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000227
228# Builds custom version of libc++ and installs it in <prefix>.
229# Can be used to build sanitized versions of libc++ for running unit tests.
230# add_custom_libcxx(<name> <prefix>
231# DEPS <list of build deps>
232# CFLAGS <list of compile flags>)
233macro(add_custom_libcxx name prefix)
234 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
235 message(FATAL_ERROR "libcxx not found!")
236 endif()
237
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000238 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000239 foreach(flag ${LIBCXX_CFLAGS})
240 set(flagstr "${flagstr} ${flag}")
241 endforeach()
242 set(LIBCXX_CFLAGS ${flagstr})
243
244 if(NOT COMPILER_RT_STANDALONE_BUILD)
245 list(APPEND LIBCXX_DEPS clang)
246 endif()
247
248 ExternalProject_Add(${name}
249 PREFIX ${prefix}
250 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
251 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
252 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
253 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
254 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
255 -DCMAKE_BUILD_TYPE=Release
256 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000257 LOG_BUILD 1
258 LOG_CONFIGURE 1
259 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000260 )
261
262 ExternalProject_Add_Step(${name} force-reconfigure
263 DEPENDERS configure
264 ALWAYS 1
265 )
266
267 ExternalProject_Add_Step(${name} clobber
268 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
269 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
270 COMMENT "Clobberring ${name} build directory..."
271 DEPENDERS configure
272 DEPENDS ${LIBCXX_DEPS}
273 )
274endmacro()