blob: 99679324467006f926337856f70040a1c55a2ca7 [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
Chris Bieneman0576c362015-08-25 19:53:09 +000049# Adds static or shared runtime for a list of architectures and operating
50# systems and puts it in the proper directory in the build and install trees.
51# add_compiler_rt_runtime(<name>
52# {STATIC|SHARED}
53# ARCHS <architectures>
54# OS <os list>
Alexey Samsonove18b7852014-03-31 13:45:36 +000055# SOURCES <source files>
56# CFLAGS <compile flags>
Chris Bieneman0576c362015-08-25 19:53:09 +000057# LINKFLAGS <linker flags>
Evgeniy Stepanov44014732014-09-29 13:18:55 +000058# DEFS <compile definitions>
Chris Bieneman0576c362015-08-25 19:53:09 +000059# LINK_LIBS <linked libraries> (only for shared library)
60# PARENT_TARGET <convenience parent target>)
61function(add_compiler_rt_runtime name type)
62 if(NOT type MATCHES "^(STATIC|SHARED)$")
63 message(FATAL_ERROR "type argument must be STATIC or SHARED")
64 return()
Alexey Samsonove16af952013-01-20 13:58:10 +000065 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +000066 cmake_parse_arguments(LIB
67 ""
68 "PARENT_TARGET"
69 "OS;ARCHS;SOURCES;CFLAGS;LINKFLAGS;DEFS;LINK_LIBS"
70 ${ARGN})
71 set(libnames)
72 if(APPLE)
73 foreach(os ${LIB_OS})
74 if(type STREQUAL "STATIC")
75 set(libname "${name}_${os}")
76 else()
77 set(libname "${name}_${os}_dynamic")
78 set(extra_linkflags_${libname} ${DARWIN_${os}_LINKFLAGS} ${LIB_LINKFLAGS})
79 endif()
80 list_union(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
81 if(LIB_ARCHS_${libname})
82 list(APPEND libnames ${libname})
83 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${LIB_CFLAGS})
84 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
85 endif()
86 endforeach()
87 else()
88 foreach(arch ${LIB_ARCHS})
89 if(NOT CAN_TARGET_${arch})
90 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
91 return()
92 endif()
93 if(type STREQUAL "STATIC")
94 set(libname "${name}-${arch}")
95 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
96 else()
97 set(libname "${name}-dynamic-${arch}")
98 set(extra_linkflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS})
99 if(WIN32)
100 set(output_name_${libname} ${name}_dynamic-${arch}${COMPILER_RT_OS_SUFFIX})
101 else()
102 set(output_name_${libname} ${name}-${arch}${COMPILER_RT_OS_SUFFIX})
103 endif()
104 endif()
105 set(libnames ${libnames} ${libname})
106 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
107 endforeach()
108 endif()
Alexey Samsonove16af952013-01-20 13:58:10 +0000109
Chris Bieneman0576c362015-08-25 19:53:09 +0000110 if(NOT libnames)
111 return()
112 endif()
113
114 if(LIB_PARENT_TARGET)
115 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
116 endif()
117
118 foreach(libname ${libnames})
119 add_library(${libname} ${type} ${LIB_SOURCES})
120 set_target_compile_flags(${libname} ${extra_cflags_${libname}})
121 set_target_link_flags(${libname} ${extra_linkflags_${libname}})
122 set_property(TARGET ${libname} APPEND PROPERTY
123 COMPILE_DEFINITIONS ${LIB_DEFS})
124 set_target_properties(${libname} PROPERTIES
125 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
126 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
127 RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
128 set_target_properties(${libname} PROPERTIES
129 OUTPUT_NAME ${output_name_${libname}})
130 if(LIB_LINK_LIBS AND ${type} STREQUAL "SHARED")
131 target_link_libraries(${libname} ${LIB_LINK_LIBS})
Chris Bieneman607a4f72015-08-18 17:32:18 +0000132 endif()
Chris Bieneman0576c362015-08-25 19:53:09 +0000133 install(TARGETS ${libname}
134 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
135 ${COMPONENT_OPTION}
136 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
137 ${COMPONENT_OPTION}
138 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
139 ${COMPONENT_OPTION})
140 if(APPLE)
141 set_target_properties(${libname} PROPERTIES
142 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
143 endif()
144 endforeach()
145 if(LIB_PARENT_TARGET)
146 add_dependencies(${LIB_PARENT_TARGET} ${libnames})
Chris Bieneman607a4f72015-08-18 17:32:18 +0000147 endif()
148endfunction()
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000149
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000150set(COMPILER_RT_TEST_CFLAGS)
151
Alexey Samsonov392c50d2013-01-18 16:05:21 +0000152# Unittests support.
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000153set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruthace53502013-11-15 10:21:15 +0000154set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonov1e5d8be2014-03-24 13:29:20 +0000155set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000156 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000157 -DGTEST_HAS_RTTI=0
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000158 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruthace53502013-11-15 10:21:15 +0000159 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000160)
161
Alexey Samsonov001d0382015-01-06 20:58:40 +0000162append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS)
163
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000164if(MSVC)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000165 # clang doesn't support exceptions on Windows yet.
Alexey Samsonov001d0382015-01-06 20:58:40 +0000166 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000167
168 # We should teach clang to understand "#pragma intrinsic", see PR19898.
169 list(APPEND COMPILER_RT_TEST_CFLAGS -Wno-undefined-inline)
170
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000171 # Clang doesn't support SEH on Windows yet.
172 list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
Timur Iskhodzhanova73e9db2014-05-30 12:42:57 +0000173
174 # gtest use a lot of stuff marked as deprecated on Windows.
175 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000176
177 # Visual Studio 2012 only supports up to 8 template parameters in
178 # std::tr1::tuple by default, but gtest requires 10
179 if(MSVC_VERSION EQUAL 1700)
Ehsan Akhgari494410f2014-09-25 20:42:49 +0000180 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari1c4db802014-09-15 11:33:50 +0000181 endif()
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000182endif()
183
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000184# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
185# using specified link flags. Make executable a part of provided
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000186# test_suite.
187# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000188# SUBDIR <subdirectory for binary>
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000189# OBJECTS <object files>
190# DEPS <deps (e.g. runtime libs)>
191# LINK_FLAGS <link flags>)
192macro(add_compiler_rt_test test_suite test_name)
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000193 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonov431f94d2014-12-17 23:14:01 +0000194 if(TEST_SUBDIR)
195 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SUBDIR}/${test_name}")
196 else()
197 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
198 endif()
Timur Iskhodzhanov00b915c2015-01-22 14:54:22 +0000199 if(MSVC)
200 set(output_bin "${output_bin}.exe")
201 endif()
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000202 # Use host compiler in a standalone build, and just-built Clang otherwise.
203 if(NOT COMPILER_RT_STANDALONE_BUILD)
204 list(APPEND TEST_DEPS clang)
205 endif()
Chandler Carruth272eef92014-05-15 16:33:58 +0000206 # If we're not on MSVC, include the linker flags from CMAKE but override them
207 # with the provided link flags. This ensures that flags which are required to
208 # link programs at all are included, but the changes needed for the test
209 # trump. With MSVC we can't do that because CMake is set up to run link.exe
210 # when linking, not the compiler. Here, we hack it to use the compiler
211 # because we want to use -fsanitize flags.
212 if(NOT MSVC)
213 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
214 separate_arguments(TEST_LINK_FLAGS)
215 endif()
Alexey Samsonova74424e2013-01-28 09:07:30 +0000216 add_custom_target(${test_name}
Timur Iskhodzhanov7c707342014-05-12 08:55:20 +0000217 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov402bcb52014-05-28 08:38:13 +0000218 -o "${output_bin}"
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000219 ${TEST_LINK_FLAGS}
Alexey Samsonov17cf7c52014-02-19 13:01:03 +0000220 DEPENDS ${TEST_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +0000221 # Make the test suite depend on the binary.
222 add_dependencies(${test_suite} ${test_name})
223endmacro()
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000224
225macro(add_compiler_rt_resource_file target_name file_name)
226 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000227 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000228 add_custom_command(OUTPUT ${dst_file}
229 DEPENDS ${src_file}
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000230 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov41bf0bc2014-02-27 07:22:59 +0000231 COMMENT "Copying ${file_name}...")
232 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000233 # Install in Clang resource directory.
Alexey Samsonova52e2dc2014-02-18 14:28:53 +0000234 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000235endmacro()
Evgeniy Stepanov9514ed12014-02-27 08:41:40 +0000236
237macro(add_compiler_rt_script name)
238 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
239 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
240 add_custom_command(OUTPUT ${dst}
241 DEPENDS ${src}
242 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
243 COMMENT "Copying ${name}...")
244 add_custom_target(${name} DEPENDS ${dst})
245 install(FILES ${dst}
246 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
247 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
248endmacro(add_compiler_rt_script src name)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000249
250# Builds custom version of libc++ and installs it in <prefix>.
251# Can be used to build sanitized versions of libc++ for running unit tests.
252# add_custom_libcxx(<name> <prefix>
253# DEPS <list of build deps>
254# CFLAGS <list of compile flags>)
255macro(add_custom_libcxx name prefix)
256 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
257 message(FATAL_ERROR "libcxx not found!")
258 endif()
259
Filipe Cabecinhasc309de72015-06-19 03:39:24 +0000260 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000261 foreach(flag ${LIBCXX_CFLAGS})
262 set(flagstr "${flagstr} ${flag}")
263 endforeach()
264 set(LIBCXX_CFLAGS ${flagstr})
265
266 if(NOT COMPILER_RT_STANDALONE_BUILD)
267 list(APPEND LIBCXX_DEPS clang)
268 endif()
269
270 ExternalProject_Add(${name}
271 PREFIX ${prefix}
272 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
273 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
274 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
275 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
276 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
277 -DCMAKE_BUILD_TYPE=Release
278 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov19de1ba2014-05-12 21:07:28 +0000279 LOG_BUILD 1
280 LOG_CONFIGURE 1
281 LOG_INSTALL 1
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000282 )
Alexey Samsonovdd6f8442015-07-16 21:20:05 +0000283 set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Alexey Samsonov2e8e4412014-05-09 22:11:03 +0000284
285 ExternalProject_Add_Step(${name} force-reconfigure
286 DEPENDERS configure
287 ALWAYS 1
288 )
289
290 ExternalProject_Add_Step(${name} clobber
291 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
292 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
293 COMMENT "Clobberring ${name} build directory..."
294 DEPENDERS configure
295 DEPENDS ${LIBCXX_DEPS}
296 )
297endmacro()