blob: 69c30e364cd5737db22034f89ced86d432bd61e4 [file] [log] [blame]
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00001include(AddLLVM)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +00002include(ExternalProject)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00003include(LLVMParseArguments)
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00004include(CompilerRTUtils)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00005
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00006# Tries to add "object library" target for a given architecture
7# with name "<name>.<arch>" if architecture can be targeted.
8# add_compiler_rt_object_library(<name> <arch>
9# SOURCES <source files>
Alexey Samsonov036a5be2013-09-16 15:50:53 +000010# CFLAGS <compile flags>
11# DEFS <compile definitions>)
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000012macro(add_compiler_rt_object_library name arch)
13 if(CAN_TARGET_${arch})
Alexey Samsonov036a5be2013-09-16 15:50:53 +000014 parse_arguments(LIB "SOURCES;CFLAGS;DEFS" "" ${ARGN})
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000015 add_library(${name}.${arch} OBJECT ${LIB_SOURCES})
16 set_target_compile_flags(${name}.${arch}
Chandler Carruth57e0ec72014-05-15 15:21:11 +000017 ${CMAKE_CXX_FLAGS} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonov036a5be2013-09-16 15:50:53 +000018 set_property(TARGET ${name}.${arch} APPEND PROPERTY
19 COMPILE_DEFINITIONS ${LIB_DEFS})
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000020 else()
21 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
22 endif()
23endmacro()
24
Alexander Potapenko49034e32013-11-07 10:08:19 +000025# Same as above, but adds universal osx library for either OSX or iOS simulator
26# with name "<name>.<os>" targeting multiple architectures.
27# add_compiler_rt_darwin_object_library(<name> <os> ARCH <architectures>
28# SOURCES <source files>
29# CFLAGS <compile flags>
30# DEFS <compile definitions>)
31macro(add_compiler_rt_darwin_object_library name os)
Alexey Samsonov036a5be2013-09-16 15:50:53 +000032 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
Alexander Potapenko49034e32013-11-07 10:08:19 +000033 set(libname "${name}.${os}")
Alexey Samsonov4e503332013-01-20 14:36:12 +000034 add_library(${libname} OBJECT ${LIB_SOURCES})
Alexander Potapenko49034e32013-11-07 10:08:19 +000035 set_target_compile_flags(${libname} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
Alexey Samsonovb3991182013-01-21 08:12:20 +000036 set_target_properties(${libname} PROPERTIES OSX_ARCHITECTURES "${LIB_ARCH}")
Alexey Samsonov036a5be2013-09-16 15:50:53 +000037 set_property(TARGET ${libname} APPEND PROPERTY
38 COMPILE_DEFINITIONS ${LIB_DEFS})
Alexey Samsonov4e503332013-01-20 14:36:12 +000039endmacro()
40
Alexey Samsonov78a84352014-03-31 13:45:36 +000041# Adds static or shared runtime for a given architecture and puts it in the
42# proper directory in the build and install trees.
43# add_compiler_rt_runtime(<name> <arch> {STATIC,SHARED}
44# SOURCES <source files>
45# CFLAGS <compile flags>
46# DEFS <compile definitions>)
47macro(add_compiler_rt_runtime name arch type)
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000048 if(CAN_TARGET_${arch})
Alexey Samsonov56b6ee92014-04-01 13:16:30 +000049 parse_arguments(LIB "SOURCES;CFLAGS;DEFS;OUTPUT_NAME" "" ${ARGN})
Alexey Samsonov78a84352014-03-31 13:45:36 +000050 add_library(${name} ${type} ${LIB_SOURCES})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000051 # Setup compile flags and definitions.
52 set_target_compile_flags(${name}
53 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonov78a84352014-03-31 13:45:36 +000054 set_target_link_flags(${name}
55 ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000056 set_property(TARGET ${name} APPEND PROPERTY
57 COMPILE_DEFINITIONS ${LIB_DEFS})
58 # Setup correct output directory in the build tree.
59 set_target_properties(${name} PROPERTIES
Alexey Samsonov78a84352014-03-31 13:45:36 +000060 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
61 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
Alexey Samsonov56b6ee92014-04-01 13:16:30 +000062 if (LIB_OUTPUT_NAME)
63 set_target_properties(${name} PROPERTIES
64 OUTPUT_NAME ${LIB_OUTPUT_NAME})
65 endif()
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000066 # Add installation command.
67 install(TARGETS ${name}
Alexey Samsonov78a84352014-03-31 13:45:36 +000068 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
69 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Alexey Samsonov8c2cd862013-01-20 13:58:10 +000070 else()
71 message(FATAL_ERROR "Archtecture ${arch} can't be targeted")
72 endif()
73endmacro()
74
Alexey Samsonov78a84352014-03-31 13:45:36 +000075# Same as add_compiler_rt_runtime(... STATIC), but creates a universal library
Alexey Samsonovb3991182013-01-21 08:12:20 +000076# for several architectures.
77# add_compiler_rt_osx_static_runtime(<name> ARCH <architectures>
78# SOURCES <source files>
79# CFLAGS <compile flags>
80# DEFS <compile definitions>)
81macro(add_compiler_rt_osx_static_runtime name)
82 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS" "" ${ARGN})
83 add_library(${name} STATIC ${LIB_SOURCES})
84 set_target_compile_flags(${name} ${LIB_CFLAGS})
85 set_property(TARGET ${name} APPEND PROPERTY
86 COMPILE_DEFINITIONS ${LIB_DEFS})
87 set_target_properties(${name} PROPERTIES
88 OSX_ARCHITECTURES "${LIB_ARCH}"
89 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
90 install(TARGETS ${name}
91 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
92endmacro()
93
Alexander Potapenko49034e32013-11-07 10:08:19 +000094# Adds dynamic runtime library on osx/iossim, which supports multiple
95# architectures.
96# add_compiler_rt_darwin_dynamic_runtime(<name> <os>
97# ARCH <architectures>
98# SOURCES <source files>
99# CFLAGS <compile flags>
100# DEFS <compile definitions>
101# LINKFLAGS <link flags>)
102macro(add_compiler_rt_darwin_dynamic_runtime name os)
Alexey Samsonovb3991182013-01-21 08:12:20 +0000103 parse_arguments(LIB "ARCH;SOURCES;CFLAGS;DEFS;LINKFLAGS" "" ${ARGN})
104 add_library(${name} SHARED ${LIB_SOURCES})
Alexander Potapenko49034e32013-11-07 10:08:19 +0000105 set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
106 set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
Alexey Samsonovb3991182013-01-21 08:12:20 +0000107 set_property(TARGET ${name} APPEND PROPERTY
108 COMPILE_DEFINITIONS ${LIB_DEFS})
109 set_target_properties(${name} PROPERTIES
110 OSX_ARCHITECTURES "${LIB_ARCH}"
111 LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
112 install(TARGETS ${name}
113 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
114endmacro()
115
Alexey Samsonov163ab9d2013-01-18 16:05:21 +0000116# Unittests support.
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000117set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
Chandler Carruth65fd2382013-11-15 10:21:15 +0000118set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
Alexey Samsonovc41ca6d2014-03-24 13:29:20 +0000119set(COMPILER_RT_GTEST_CFLAGS
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000120 -DGTEST_NO_LLVM_RAW_OSTREAM=1
121 -I${COMPILER_RT_GTEST_PATH}/include
Chandler Carruth65fd2382013-11-15 10:21:15 +0000122 -I${COMPILER_RT_GTEST_PATH}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000123)
124
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000125# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
126# using specified link flags. Make executable a part of provided
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000127# test_suite.
128# add_compiler_rt_test(<test_suite> <test_name>
129# OBJECTS <object files>
130# DEPS <deps (e.g. runtime libs)>
131# LINK_FLAGS <link flags>)
132macro(add_compiler_rt_test test_suite test_name)
133 parse_arguments(TEST "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
Alexey Samsonov53117542013-01-28 07:16:22 +0000134 set(output_bin "${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000135 # Use host compiler in a standalone build, and just-built Clang otherwise.
136 if(NOT COMPILER_RT_STANDALONE_BUILD)
137 list(APPEND TEST_DEPS clang)
138 endif()
Chandler Carruth332e1b12014-05-15 16:33:58 +0000139 # If we're not on MSVC, include the linker flags from CMAKE but override them
140 # with the provided link flags. This ensures that flags which are required to
141 # link programs at all are included, but the changes needed for the test
142 # trump. With MSVC we can't do that because CMake is set up to run link.exe
143 # when linking, not the compiler. Here, we hack it to use the compiler
144 # because we want to use -fsanitize flags.
145 if(NOT MSVC)
146 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
147 separate_arguments(TEST_LINK_FLAGS)
148 endif()
Alexey Samsonov7c362fb2013-01-28 09:07:30 +0000149 add_custom_target(${test_name}
Timur Iskhodzhanovb0279d72014-05-12 08:55:20 +0000150 # MSVS CL doesn't allow a space between -Fe and the output file name.
151 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
152 ${COMPILER_RT_TEST_COMPILER_EXE}"${output_bin}"
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000153 ${TEST_LINK_FLAGS}
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000154 DEPENDS ${TEST_DEPS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000155 # Make the test suite depend on the binary.
156 add_dependencies(${test_suite} ${test_name})
157endmacro()
Alexey Samsonove8381352013-05-21 13:48:27 +0000158
159macro(add_compiler_rt_resource_file target_name file_name)
160 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonov1181a102014-02-18 14:28:53 +0000161 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000162 add_custom_command(OUTPUT ${dst_file}
163 DEPENDS ${src_file}
Alexey Samsonove8381352013-05-21 13:48:27 +0000164 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000165 COMMENT "Copying ${file_name}...")
166 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonove8381352013-05-21 13:48:27 +0000167 # Install in Clang resource directory.
Alexey Samsonov1181a102014-02-18 14:28:53 +0000168 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonove8381352013-05-21 13:48:27 +0000169endmacro()
Evgeniy Stepanov322e89c2014-02-27 08:41:40 +0000170
171macro(add_compiler_rt_script name)
172 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
173 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
174 add_custom_command(OUTPUT ${dst}
175 DEPENDS ${src}
176 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
177 COMMENT "Copying ${name}...")
178 add_custom_target(${name} DEPENDS ${dst})
179 install(FILES ${dst}
180 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
181 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
182endmacro(add_compiler_rt_script src name)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000183
184# Builds custom version of libc++ and installs it in <prefix>.
185# Can be used to build sanitized versions of libc++ for running unit tests.
186# add_custom_libcxx(<name> <prefix>
187# DEPS <list of build deps>
188# CFLAGS <list of compile flags>)
189macro(add_custom_libcxx name prefix)
190 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
191 message(FATAL_ERROR "libcxx not found!")
192 endif()
193
194 parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN})
195 foreach(flag ${LIBCXX_CFLAGS})
196 set(flagstr "${flagstr} ${flag}")
197 endforeach()
198 set(LIBCXX_CFLAGS ${flagstr})
199
200 if(NOT COMPILER_RT_STANDALONE_BUILD)
201 list(APPEND LIBCXX_DEPS clang)
202 endif()
203
204 ExternalProject_Add(${name}
205 PREFIX ${prefix}
206 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
207 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
208 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
209 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
210 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
211 -DCMAKE_BUILD_TYPE=Release
212 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov5af5f8f2014-05-12 21:07:28 +0000213 LOG_BUILD 1
214 LOG_CONFIGURE 1
215 LOG_INSTALL 1
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000216 )
217
218 ExternalProject_Add_Step(${name} force-reconfigure
219 DEPENDERS configure
220 ALWAYS 1
221 )
222
223 ExternalProject_Add_Step(${name} clobber
224 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
225 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
226 COMMENT "Clobberring ${name} build directory..."
227 DEPENDERS configure
228 DEPENDS ${LIBCXX_DEPS}
229 )
230endmacro()