blob: 4a0a475775e3e98a554e986143af2b58b1f6a082 [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})
Chris Bieneman19c84512015-08-13 20:38:16 +000022 list_union(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman5c2fd242015-06-10 23:55:07 +000023 endforeach()
Alexey Samsonov392c50d2013-01-18 16:05:21 +000024 else()
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000025 foreach(arch ${LIB_ARCHS})
Chris Bieneman5c2fd242015-06-10 23:55:07 +000026 set(libname "${name}.${arch}")
27 set(libnames ${libnames} ${libname})
28 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
29 if(NOT CAN_TARGET_${arch})
Filipe Cabecinhas030b5902015-08-10 18:26:29 +000030 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000031 return()
32 endif()
33 endforeach()
Alexey Samsonov392c50d2013-01-18 16:05:21 +000034 endif()
Chris Bieneman5c2fd242015-06-10 23:55:07 +000035
36 foreach(libname ${libnames})
37 add_library(${libname} OBJECT ${LIB_SOURCES})
38 set_target_compile_flags(${libname}
39 ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${LIB_CFLAGS})
40 set_property(TARGET ${libname} APPEND PROPERTY
41 COMPILE_DEFINITIONS ${LIB_DEFS})
42 if(APPLE)
Chris Bieneman19c84512015-08-13 20:38:16 +000043 set_target_properties(${libname} PROPERTIES
44 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
Chris Bieneman5c2fd242015-06-10 23:55:07 +000045 endif()
46 endforeach()
47endfunction()
Alexey Samsonov51623142013-01-20 14:36:12 +000048
Alexey Samsonove18b7852014-03-31 13:45:36 +000049# Adds static or shared runtime for a given architecture and puts it in the
50# proper directory in the build and install trees.
51# add_compiler_rt_runtime(<name> <arch> {STATIC,SHARED}
52# SOURCES <source files>
53# CFLAGS <compile flags>
Evgeniy Stepanov44014732014-09-29 13:18:55 +000054# DEFS <compile definitions>
55# OUTPUT_NAME <output library name>)
Alexey Samsonove18b7852014-03-31 13:45:36 +000056macro(add_compiler_rt_runtime name arch type)
Alexey Samsonove16af952013-01-20 13:58:10 +000057 if(CAN_TARGET_${arch})
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000058 cmake_parse_arguments(LIB "" "OUTPUT_NAME" "SOURCES;CFLAGS;LINKFLAGS;DEFS" ${ARGN})
Alexey Samsonove18b7852014-03-31 13:45:36 +000059 add_library(${name} ${type} ${LIB_SOURCES})
Alexey Samsonove16af952013-01-20 13:58:10 +000060 # Setup compile flags and definitions.
61 set_target_compile_flags(${name}
62 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Aaron Ballman208eae32014-10-29 19:25:20 +000063 set_target_link_flags(${name}
Evgeniy Stepanov976974a2015-05-05 20:13:39 +000064 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS})
Alexey Samsonove16af952013-01-20 13:58:10 +000065 set_property(TARGET ${name} APPEND PROPERTY
66 COMPILE_DEFINITIONS ${LIB_DEFS})
67 # Setup correct output directory in the build tree.
68 set_target_properties(${name} PROPERTIES
Alexey Samsonove18b7852014-03-31 13:45:36 +000069 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
Hans Wennborgb89adc42014-12-04 21:01:49 +000070 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
71 RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Evgeniy Stepanov44014732014-09-29 13:18:55 +000072 if ("${LIB_OUTPUT_NAME}" STREQUAL "")
73 set_target_properties(${name} PROPERTIES
74 OUTPUT_NAME ${name}${COMPILER_RT_OS_SUFFIX})
75 else()
Alexey Samsonovd6535ea2014-04-01 13:16:30 +000076 set_target_properties(${name} PROPERTIES
77 OUTPUT_NAME ${LIB_OUTPUT_NAME})
78 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +000079 # Add installation command.
80 install(TARGETS ${name}
Alexey Samsonove18b7852014-03-31 13:45:36 +000081 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
Hans Wennborgb89adc42014-12-04 21:01:49 +000082 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
83 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonove16af952013-01-20 13:58:10 +000084 else()
Filipe Cabecinhas030b5902015-08-10 18:26:29 +000085 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
Alexey Samsonove16af952013-01-20 13:58:10 +000086 endif()
87endmacro()
88
Alexey Samsonove18b7852014-03-31 13:45:36 +000089# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000090# for several architectures.
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000091# add_compiler_rt_osx_static_runtime(<name> ARCHS <architectures>
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000092# SOURCES <source files>
93# CFLAGS <compile flags>
94# DEFS <compile definitions>)
95macro(add_compiler_rt_osx_static_runtime name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +000096 cmake_parse_arguments(LIB "" "" "ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +000097 add_library(${name} STATIC ${LIB_SOURCES})
98 set_target_compile_flags(${name} ${LIB_CFLAGS})
99 set_property(TARGET ${name} APPEND PROPERTY
100 COMPILE_DEFINITIONS ${LIB_DEFS})
101 set_target_properties(${name} PROPERTIES
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000102 OSX_ARCHITECTURES "${LIB_ARCHS}"
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000103 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
104 install(TARGETS ${name}
105 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
106endmacro()
107
Alexander Potapenko49496742013-11-07 10:08:19 +0000108# Adds dynamic runtime library on osx/iossim, which supports multiple
109# architectures.
110# add_compiler_rt_darwin_dynamic_runtime(<name> <os>
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000111# ARCHS <architectures>
Alexander Potapenko49496742013-11-07 10:08:19 +0000112# SOURCES <source files>
113# CFLAGS <compile flags>
114# DEFS <compile definitions>
115# LINKFLAGS <link flags>)
116macro(add_compiler_rt_darwin_dynamic_runtime name os)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000117 cmake_parse_arguments(LIB "" "" "ARCHS;SOURCES;CFLAGS;DEFS;LINKFLAGS" ${ARGN})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000118 add_library(${name} SHARED ${LIB_SOURCES})
Alexander Potapenko49496742013-11-07 10:08:19 +0000119 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
120 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000121 set_property(TARGET ${name} APPEND PROPERTY
122 COMPILE_DEFINITIONS ${LIB_DEFS})
Chris Bieneman19c84512015-08-13 20:38:16 +0000123 list_union(filtered_arches DARWIN_${os}_ARCHS LIB_ARCHS)
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000124 set_target_properties(${name} PROPERTIES
Chris Bieneman19c84512015-08-13 20:38:16 +0000125 OSX_ARCHITECTURES "${filtered_arches}"
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000126 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
127 install(TARGETS ${name}
128 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
129endmacro()
130
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000131set(COMPILER_RT_TEST_CFLAGS)
132
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000133# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000134set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000135set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000136set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000137 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000138 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000139 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000140 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000141)
142
Alexey Samsonov001d0382015-01-06 20:58:40 +0000143append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS)
144
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000145if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000146 # clang doesn't support exceptions on Windows yet.
Alexey Samsonov001d0382015-01-06 20:58:40 +0000147 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000148
149 # We should teach clang to understand "#pragma intrinsic", see PR19898.
150 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline)
151
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000152 # Clang doesn't support SEH on Windows yet.
153 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000154
155 # gtest use a lot of stuff marked as deprecated on Windows.
156 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000157
158 # Visual Studio 2012 only supports up to 8 template parameters in
159 # std::tr1::tuple by default, but gtest requires 10
160 if(MSVC_VERSION EQUAL 1700)
Ehsan Akhgari494410f2014-09-25 20:42:49 +0000161 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000162 endif()
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000163endif()
164
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000165# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
166# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000167# test_suite.
168# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000169# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000170# OBJECTS <object files>
171# DEPS <deps (e.g. runtime libs)>
172# LINK_FLAGS <link flags>)
173macro(add_compiler_rt_test test_suite test_name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000174 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000175 if(TEST_SUBDIR)
176 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SUBDIR}/${test_name}")
177 else()
178 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
179 endif()
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000180 if(MSVC)
181 set(output_bin "${output_bin}.exe")
182 endif()
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000183 # Use host compiler in a standalone build, and just-built Clang otherwise.
184 if(NOT COMPILER_RT_STANDALONE_BUILD)
185 list(APPEND TEST_DEPS clang)
186 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000187 # If we're not on MSVC, include the linker flags from CMAKE but override them
188 # with the provided link flags. This ensures that flags which are required to
189 # link programs at all are included, but the changes needed for the test
190 # trump. With MSVC we can't do that because CMake is set up to run link.exe
191 # when linking, not the compiler. Here, we hack it to use the compiler
192 # because we want to use -fsanitize flags.
193 if(NOT MSVC)
194 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
195 separate_arguments(TEST_LINK_FLAGS)
196 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000197 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000198 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000199 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000200 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000201 DEPENDS ${TEST_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000202 # Make the test suite depend on the binary.
203 add_dependencies(${test_suite} ${test_name})
204endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000205
206macro(add_compiler_rt_resource_file target_name file_name)
207 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000208 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000209 add_custom_command(OUTPUT ${dst_file}
210 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000211 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000212 COMMENT "Copying ${file_name}...")
213 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000214 # Install in Clang resource directory.
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000215 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000216endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000217
218macro(add_compiler_rt_script name)
219 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
220 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
221 add_custom_command(OUTPUT ${dst}
222 DEPENDS ${src}
223 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
224 COMMENT "Copying ${name}...")
225 add_custom_target(${name} DEPENDS ${dst})
226 install(FILES ${dst}
227 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
228 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
229endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000230
231# Builds custom version of libc++ and installs it in <prefix>.
232# Can be used to build sanitized versions of libc++ for running unit tests.
233# add_custom_libcxx(<name> <prefix>
234# DEPS <list of build deps>
235# CFLAGS <list of compile flags>)
236macro(add_custom_libcxx name prefix)
237 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
238 message(FATAL_ERROR "libcxx not found!")
239 endif()
240
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000241 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000242 foreach(flag ${LIBCXX_CFLAGS})
243 set(flagstr "${flagstr} ${flag}")
244 endforeach()
245 set(LIBCXX_CFLAGS ${flagstr})
246
247 if(NOT COMPILER_RT_STANDALONE_BUILD)
248 list(APPEND LIBCXX_DEPS clang)
249 endif()
250
251 ExternalProject_Add(${name}
252 PREFIX ${prefix}
253 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
254 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
255 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
256 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
257 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
258 -DCMAKE_BUILD_TYPE=Release
259 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000260 LOG_BUILD 1
261 LOG_CONFIGURE 1
262 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000263 )
Alexey Samsonovdd6f8442015-07-16 21:20:05 +0000264 set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000265
266 ExternalProject_Add_Step(${name} force-reconfigure
267 DEPENDERS configure
268 ALWAYS 1
269 )
270
271 ExternalProject_Add_Step(${name} clobber
272 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
273 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
274 COMMENT "Clobberring ${name} build directory..."
275 DEPENDERS configure
276 DEPENDS ${LIBCXX_DEPS}
277 )
278endmacro()