blob: ab155bf45ae3113c2a70c3f1f3d099a13b113548 [file] [log] [blame]
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00001include(AddLLVM)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +00002include(ExternalProject)
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00003include(CompilerRTUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00004
Chris Bieneman6bd006f2015-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +00009# OS <os names>
10# ARCHS <architectures>
Chris Bieneman6bd006f2015-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +000015 cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Chris Bieneman6bd006f2015-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 Samsonov163ab9d2013-01-18 16:05:21 +000023 else()
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +000024 foreach(arch ${LIB_ARCHS})
Chris Bieneman6bd006f2015-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 Samsonov163ab9d2013-01-18 16:05:21 +000033 endif()
Chris Bieneman6bd006f2015-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +000042 set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCHS}")
Chris Bieneman6bd006f2015-06-10 23:55:07 +000043 endif()
44 endforeach()
45endfunction()
Alexey Samsonov4e503332013-01-20 14:36:12 +000046
Alexey Samsonov78a84352014-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 Stepanov9e922e72014-09-29 13:18:55 +000052# DEFS <compile definitions>
53# OUTPUT_NAME <output library name>)
Alexey Samsonov78a84352014-03-31 13:45:36 +000054macro(add_compiler_rt_runtime name arch type)
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000055 if(CAN_TARGET_${arch})
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +000056 cmake_parse_arguments(LIB "" "OUTPUT_NAME" "SOURCES;CFLAGS;LINKFLAGS;DEFS" ${ARGN})
Alexey Samsonov78a84352014-03-31 13:45:36 +000057 add_library(${name} ${type} ${LIB_SOURCES})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000058 # Setup compile flags and definitions.
59 set_target_compile_flags(${name}
60 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Aaron Ballmand98e1972014-10-29 19:25:20 +000061 set_target_link_flags(${name}
Evgeniy Stepanov91960902015-05-05 20:13:39 +000062 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS})
Alexey Samsonov8c2cd862013-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 Samsonov78a84352014-03-31 13:45:36 +000067 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
Hans Wennborg217cf222014-12-04 21:01:49 +000068 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
69 RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Evgeniy Stepanov9e922e72014-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 Samsonov56b6ee92014-04-01 13:16:30 +000074 set_target_properties(${name} PROPERTIES
75 OUTPUT_NAME ${LIB_OUTPUT_NAME})
76 endif()
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000077 # Add installation command.
78 install(TARGETS ${name}
Alexey Samsonov78a84352014-03-31 13:45:36 +000079 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
Hans Wennborg217cf222014-12-04 21:01:49 +000080 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
81 RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000082 else()
83 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
84 endif()
85endmacro()
86
Alexey Samsonov78a84352014-03-31 13:45:36 +000087# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library
Alexey Samsonovb3991182013-01-21 08:12:20 +000088# for several architectures.
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +000089# add_compiler_rt_osx_static_runtime(<name> ARCHS <architectures>
Alexey Samsonovb3991182013-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +000094 cmake_parse_arguments(LIB "" "" "ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
Alexey Samsonovb3991182013-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +0000100 OSX_ARCHITECTURES "${LIB_ARCHS}"
Alexey Samsonovb3991182013-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 Potapenko49034e32013-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +0000109# ARCHS <architectures>
Alexander Potapenko49034e32013-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +0000115 cmake_parse_arguments(LIB "" "" "ARCHS;SOURCES;CFLAGS;DEFS;LINKFLAGS" ${ARGN})
Alexey Samsonovb3991182013-01-21 08:12:20 +0000116 add_library(${name} SHARED ${LIB_SOURCES})
Alexander Potapenko49034e32013-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 Samsonovb3991182013-01-21 08:12:20 +0000119 set_property(TARGET ${name} APPEND PROPERTY
120 COMPILE_DEFINITIONS ${LIB_DEFS})
121 set_target_properties(${name} PROPERTIES
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +0000122 OSX_ARCHITECTURES "${LIB_ARCHS}"
Alexey Samsonovb3991182013-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 Iskhodzhanov1b42b812014-05-30 12:42:57 +0000128set(COMPILER_RT_TEST_CFLAGS)
129
Alexey Samsonov163ab9d2013-01-18 16:05:21 +0000130# Unittests support.
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000131set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruth65fd2382013-11-15 10:21:15 +0000132set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonovc41ca6d2014-03-24 13:29:20 +0000133set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000134 -DGTEST_NO_LLVM_RAW_OSTREAM=1
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000135 -DGTEST_HAS_RTTI=0
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000136 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruth65fd2382013-11-15 10:21:15 +0000137 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000138)
139
Alexey Samsonov6239ebc2015-01-06 20:58:40 +0000140append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_TEST_CFLAGS)
141
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000142if(MSVC)
Timur Iskhodzhanov1b42b812014-05-30 12:42:57 +0000143 # clang doesn't support exceptions on Windows yet.
Alexey Samsonov6239ebc2015-01-06 20:58:40 +0000144 list(APPEND COMPILER_RT_TEST_CFLAGS -D_HAS_EXCEPTIONS=0)
Timur Iskhodzhanov1b42b812014-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 Iskhodzhanov82ee0432014-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 Iskhodzhanov1b42b812014-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 Akhgari47d26092014-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 Akhgari045423f2014-09-25 20:42:49 +0000158 list(APPEND COMPILER_RT_GTEST_CFLAGS -D_VARIADIC_MAX=10)
Ehsan Akhgari47d26092014-09-15 11:33:50 +0000159 endif()
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000160endif()
161
Alexey Samsonov150d62a2014-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 Samsonovca7fcf22012-12-19 12:33:39 +0000164# test_suite.
165# add_compiler_rt_test(<test_suite> <test_name>
Alexey Samsonov969902b2014-12-17 23:14:01 +0000166# SUBDIR <subdirectory for binary>
Alexey Samsonovca7fcf22012-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +0000171 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonov969902b2014-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 Iskhodzhanov23cfd6e2015-01-22 14:54:22 +0000177 if(MSVC)
178 set(output_bin "${output_bin}.exe")
179 endif()
Alexey Samsonov150d62a2014-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 Carruth332e1b12014-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 Samsonov7c362fb2013-01-28 09:07:30 +0000194 add_custom_target(${test_name}
Timur Iskhodzhanovb0279d72014-05-12 08:55:20 +0000195 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +0000196 -o "${output_bin}"
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000197 ${TEST_LINK_FLAGS}
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000198 DEPENDS ${TEST_DEPS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000199 # Make the test suite depend on the binary.
200 add_dependencies(${test_suite} ${test_name})
201endmacro()
Alexey Samsonove8381352013-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 Samsonov1181a102014-02-18 14:28:53 +0000205 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000206 add_custom_command(OUTPUT ${dst_file}
207 DEPENDS ${src_file}
Alexey Samsonove8381352013-05-21 13:48:27 +0000208 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000209 COMMENT "Copying ${file_name}...")
210 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonove8381352013-05-21 13:48:27 +0000211 # Install in Clang resource directory.
Alexey Samsonov1181a102014-02-18 14:28:53 +0000212 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonove8381352013-05-21 13:48:27 +0000213endmacro()
Evgeniy Stepanov322e89c2014-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 Samsonoveacb4d82014-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 Cabecinhas7af0a1c2015-06-19 03:39:24 +0000238 cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
Alexey Samsonoveacb4d82014-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
Alexey Samsonov70231d62015-07-16 17:53:01 +0000248 set(EXCLUDE_FROM_ALL TRUE)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000249 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 Samsonov5af5f8f2014-05-12 21:07:28 +0000258 LOG_BUILD 1
259 LOG_CONFIGURE 1
260 LOG_INSTALL 1
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000261 )
262
263 ExternalProject_Add_Step(${name} force-reconfigure
264 DEPENDERS configure
265 ALWAYS 1
266 )
267
268 ExternalProject_Add_Step(${name} clobber
269 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
270 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
271 COMMENT "Clobberring ${name} build directory..."
272 DEPENDERS configure
273 DEPENDS ${LIBCXX_DEPS}
274 )
Alexey Samsonov70231d62015-07-16 17:53:01 +0000275 set(EXCLUDE_FROM_ALL FALSE)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000276endmacro()