blob: 6784bae9eacf14318c518c03f62af86bb0f1f1c1 [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
Chris Bieneman607a4f72015-08-18 17:32:18 +000089# Adds runtime library for darwin platforms, which supports multiple
Alexander Potapenko49496742013-11-07 10:08:19 +000090# architectures.
Chris Bieneman607a4f72015-08-18 17:32:18 +000091# add_compiler_rt_darwin_runtime(<name> <os>
92# STATIC|SHARED
93# ARCHS <architectures>
94# SOURCES <source files>
95# CFLAGS <compile flags>
96# DEFS <compile definitions>
97# LINKFLAGS <link flags>)
98function(add_compiler_rt_darwin_runtime name os)
99 cmake_parse_arguments(LIB "SHARED;STATIC" "" "ARCHS;SOURCES;CFLAGS;DEFS;LINKFLAGS" ${ARGN})
Chris Bieneman19c84512015-08-13 20:38:16 +0000100 list_union(filtered_arches DARWIN_${os}_ARCHS LIB_ARCHS)
Chris Bieneman607a4f72015-08-18 17:32:18 +0000101 # if there are no supported architectures, don't create the library
102 if(filtered_arches)
103 if(LIB_SHARED)
104 add_library(${name} SHARED ${LIB_SOURCES})
105 set_target_properties(${name} PROPERTIES
106 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
107 install(TARGETS ${name}
108 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
109 elseif(LIB_STATIC)
110 add_library(${name} STATIC ${LIB_SOURCES})
111 set_target_properties(${name} PROPERTIES
112 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
113 install(TARGETS ${name}
114 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
115 else()
116 message(FATAL_ERROR "Must specified SHARED|STATIC to add_compiler_rt_darwin_runtime")
117 endif()
118
119 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
120 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
121 set_property(TARGET ${name} APPEND PROPERTY
122 COMPILE_DEFINITIONS ${LIB_DEFS})
123
124 set_target_properties(${name} PROPERTIES
125 OSX_ARCHITECTURES "${filtered_arches}")
126 endif()
127endfunction()
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000128
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000129set(COMPILER_RT_TEST_CFLAGS)
130
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000131# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000132set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000133set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000134set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000135 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000136 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000137 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000138 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000139)
140
Alexey Samsonov001d0382015-01-06 20:58:40 +0000141append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS)
142
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000143if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000144 # clang doesn't support exceptions on Windows yet.
Alexey Samsonov001d0382015-01-06 20:58:40 +0000145 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000146
147 # We should teach clang to understand "#pragma intrinsic", see PR19898.
148 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline)
149
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000150 # Clang doesn't support SEH on Windows yet.
151 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000152
153 # gtest use a lot of stuff marked as deprecated on Windows.
154 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000155
156 # Visual Studio 2012 only supports up to 8 template parameters in
157 # std::tr1::tuple by default, but gtest requires 10
158 if(MSVC_VERSION EQUAL 1700)
Ehsan Akhgari494410f2014-09-25 20:42:49 +0000159 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000160 endif()
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000161endif()
162
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000163# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
164# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000165# test_suite.
166# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000167# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000168# OBJECTS <object files>
169# DEPS <deps (e.g. runtime libs)>
170# LINK_FLAGS <link flags>)
171macro(add_compiler_rt_test test_suite test_name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000172 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000173 if(TEST_SUBDIR)
174 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SUBDIR}/${test_name}")
175 else()
176 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
177 endif()
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000178 if(MSVC)
179 set(output_bin "${output_bin}.exe")
180 endif()
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000181 # Use host compiler in a standalone build, and just-built Clang otherwise.
182 if(NOT COMPILER_RT_STANDALONE_BUILD)
183 list(APPEND TEST_DEPS clang)
184 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000185 # If we're not on MSVC, include the linker flags from CMAKE but override them
186 # with the provided link flags. This ensures that flags which are required to
187 # link programs at all are included, but the changes needed for the test
188 # trump. With MSVC we can't do that because CMake is set up to run link.exe
189 # when linking, not the compiler. Here, we hack it to use the compiler
190 # because we want to use -fsanitize flags.
191 if(NOT MSVC)
192 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
193 separate_arguments(TEST_LINK_FLAGS)
194 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000195 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000196 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000197 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000198 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000199 DEPENDS ${TEST_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000200 # Make the test suite depend on the binary.
201 add_dependencies(${test_suite} ${test_name})
202endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000203
204macro(add_compiler_rt_resource_file target_name file_name)
205 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000206 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000207 add_custom_command(OUTPUT ${dst_file}
208 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000209 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000210 COMMENT "Copying ${file_name}...")
211 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000212 # Install in Clang resource directory.
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000213 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000214endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000215
216macro(add_compiler_rt_script name)
217 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
218 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
219 add_custom_command(OUTPUT ${dst}
220 DEPENDS ${src}
221 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
222 COMMENT "Copying ${name}...")
223 add_custom_target(${name} DEPENDS ${dst})
224 install(FILES ${dst}
225 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
226 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
227endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000228
229# Builds custom version of libc++ and installs it in <prefix>.
230# Can be used to build sanitized versions of libc++ for running unit tests.
231# add_custom_libcxx(<name> <prefix>
232# DEPS <list of build deps>
233# CFLAGS <list of compile flags>)
234macro(add_custom_libcxx name prefix)
235 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
236 message(FATAL_ERROR "libcxx not found!")
237 endif()
238
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000239 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000240 foreach(flag ${LIBCXX_CFLAGS})
241 set(flagstr "${flagstr} ${flag}")
242 endforeach()
243 set(LIBCXX_CFLAGS ${flagstr})
244
245 if(NOT COMPILER_RT_STANDALONE_BUILD)
246 list(APPEND LIBCXX_DEPS clang)
247 endif()
248
249 ExternalProject_Add(${name}
250 PREFIX ${prefix}
251 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
252 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
253 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
254 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
255 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
256 -DCMAKE_BUILD_TYPE=Release
257 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000258 LOG_BUILD 1
259 LOG_CONFIGURE 1
260 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000261 )
Alexey Samsonovdd6f8442015-07-16 21:20:05 +0000262 set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000263
264 ExternalProject_Add_Step(${name} force-reconfigure
265 DEPENDERS configure
266 ALWAYS 1
267 )
268
269 ExternalProject_Add_Step(${name} clobber
270 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
271 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
272 COMMENT "Clobberring ${name} build directory..."
273 DEPENDERS configure
274 DEPENDS ${LIBCXX_DEPS}
275 )
276endmacro()