blob: 549df4d9e011cd847f0177383f43decbb8c7b04e [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 Carruth57e0ec72014-05-15 15:21:11 +0000139 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
140 separate_arguments(TEST_LINK_FLAGS)
Alexey Samsonov7c362fb2013-01-28 09:07:30 +0000141 add_custom_target(${test_name}
Timur Iskhodzhanovb0279d72014-05-12 08:55:20 +0000142 # MSVS CL doesn't allow a space between -Fe and the output file name.
143 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS}
144 ${COMPILER_RT_TEST_COMPILER_EXE}"${output_bin}"
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000145 ${TEST_LINK_FLAGS}
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000146 DEPENDS ${TEST_DEPS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000147 # Make the test suite depend on the binary.
148 add_dependencies(${test_suite} ${test_name})
149endmacro()
Alexey Samsonove8381352013-05-21 13:48:27 +0000150
151macro(add_compiler_rt_resource_file target_name file_name)
152 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
Alexey Samsonov1181a102014-02-18 14:28:53 +0000153 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000154 add_custom_command(OUTPUT ${dst_file}
155 DEPENDS ${src_file}
Alexey Samsonove8381352013-05-21 13:48:27 +0000156 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
Alexey Samsonov4cced3e2014-02-27 07:22:59 +0000157 COMMENT "Copying ${file_name}...")
158 add_custom_target(${target_name} DEPENDS ${dst_file})
Alexey Samsonove8381352013-05-21 13:48:27 +0000159 # Install in Clang resource directory.
Alexey Samsonov1181a102014-02-18 14:28:53 +0000160 install(FILES ${file_name} DESTINATION ${COMPILER_RT_INSTALL_PATH})
Alexey Samsonove8381352013-05-21 13:48:27 +0000161endmacro()
Evgeniy Stepanov322e89c2014-02-27 08:41:40 +0000162
163macro(add_compiler_rt_script name)
164 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
165 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
166 add_custom_command(OUTPUT ${dst}
167 DEPENDS ${src}
168 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
169 COMMENT "Copying ${name}...")
170 add_custom_target(${name} DEPENDS ${dst})
171 install(FILES ${dst}
172 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
173 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
174endmacro(add_compiler_rt_script src name)
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000175
176# Builds custom version of libc++ and installs it in <prefix>.
177# Can be used to build sanitized versions of libc++ for running unit tests.
178# add_custom_libcxx(<name> <prefix>
179# DEPS <list of build deps>
180# CFLAGS <list of compile flags>)
181macro(add_custom_libcxx name prefix)
182 if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
183 message(FATAL_ERROR "libcxx not found!")
184 endif()
185
186 parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN})
187 foreach(flag ${LIBCXX_CFLAGS})
188 set(flagstr "${flagstr} ${flag}")
189 endforeach()
190 set(LIBCXX_CFLAGS ${flagstr})
191
192 if(NOT COMPILER_RT_STANDALONE_BUILD)
193 list(APPEND LIBCXX_DEPS clang)
194 endif()
195
196 ExternalProject_Add(${name}
197 PREFIX ${prefix}
198 SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
199 CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
200 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
201 -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
202 -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
203 -DCMAKE_BUILD_TYPE=Release
204 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
Alexey Samsonov5af5f8f2014-05-12 21:07:28 +0000205 LOG_BUILD 1
206 LOG_CONFIGURE 1
207 LOG_INSTALL 1
Alexey Samsonoveacb4d82014-05-09 22:11:03 +0000208 )
209
210 ExternalProject_Add_Step(${name} force-reconfigure
211 DEPENDERS configure
212 ALWAYS 1
213 )
214
215 ExternalProject_Add_Step(${name} clobber
216 COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
217 COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
218 COMMENT "Clobberring ${name} build directory..."
219 DEPENDERS configure
220 DEPENDS ${LIBCXX_DEPS}
221 )
222endmacro()