Alexey Samsonov | eacb4d8 | 2014-05-09 22:11:03 +0000 | [diff] [blame] | 1 | include(ExternalProject) |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 2 | include(CompilerRTUtils) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 3 | |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 4 | function(set_target_output_directories target output_dir) |
| 5 | # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators |
| 6 | # append a per-configuration subdirectory to the specified directory. |
| 7 | # To avoid the appended folder, the configuration specific variable must be |
| 8 | # set 'RUNTIME_OUTPUT_DIRECTORY_${CONF}': |
| 9 | # RUNTIME_OUTPUT_DIRECTORY_DEBUG, RUNTIME_OUTPUT_DIRECTORY_RELEASE, ... |
| 10 | if(CMAKE_CONFIGURATION_TYPES) |
| 11 | foreach(build_mode ${CMAKE_CONFIGURATION_TYPES}) |
| 12 | string(TOUPPER "${build_mode}" CONFIG_SUFFIX) |
| 13 | set_target_properties("${target}" PROPERTIES |
| 14 | "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir} |
| 15 | "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir} |
| 16 | "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}) |
| 17 | endforeach() |
| 18 | else() |
| 19 | set_target_properties("${target}" PROPERTIES |
| 20 | ARCHIVE_OUTPUT_DIRECTORY ${output_dir} |
| 21 | LIBRARY_OUTPUT_DIRECTORY ${output_dir} |
| 22 | RUNTIME_OUTPUT_DIRECTORY ${output_dir}) |
| 23 | endif() |
| 24 | endfunction() |
| 25 | |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 26 | # Tries to add an "object library" target for a given list of OSs and/or |
| 27 | # architectures with name "<name>.<arch>" for non-Darwin platforms if |
| 28 | # architecture can be targeted, and "<name>.<os>" for Darwin platforms. |
| 29 | # add_compiler_rt_object_libraries(<name> |
Filipe Cabecinhas | 7af0a1c | 2015-06-19 03:39:24 +0000 | [diff] [blame] | 30 | # OS <os names> |
| 31 | # ARCHS <architectures> |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 32 | # SOURCES <source files> |
| 33 | # CFLAGS <compile flags> |
| 34 | # DEFS <compile definitions>) |
| 35 | function(add_compiler_rt_object_libraries name) |
Filipe Cabecinhas | 7af0a1c | 2015-06-19 03:39:24 +0000 | [diff] [blame] | 36 | cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN}) |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 37 | set(libnames) |
| 38 | if(APPLE) |
| 39 | foreach(os ${LIB_OS}) |
| 40 | set(libname "${name}.${os}") |
| 41 | set(libnames ${libnames} ${libname}) |
| 42 | set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS}) |
Daniel Sanders | de098c9 | 2016-01-27 09:28:01 +0000 | [diff] [blame] | 43 | list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS) |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 44 | endforeach() |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 45 | else() |
Filipe Cabecinhas | 7af0a1c | 2015-06-19 03:39:24 +0000 | [diff] [blame] | 46 | foreach(arch ${LIB_ARCHS}) |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 47 | set(libname "${name}.${arch}") |
| 48 | set(libnames ${libnames} ${libname}) |
| 49 | set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS}) |
| 50 | if(NOT CAN_TARGET_${arch}) |
Filipe Cabecinhas | 2bbdbcb | 2015-08-10 18:26:29 +0000 | [diff] [blame] | 51 | message(FATAL_ERROR "Architecture ${arch} can't be targeted") |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 52 | return() |
| 53 | endif() |
| 54 | endforeach() |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 55 | endif() |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 56 | |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 57 | foreach(libname ${libnames}) |
| 58 | add_library(${libname} OBJECT ${LIB_SOURCES}) |
| 59 | set_target_compile_flags(${libname} |
| 60 | ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${LIB_CFLAGS}) |
| 61 | set_property(TARGET ${libname} APPEND PROPERTY |
| 62 | COMPILE_DEFINITIONS ${LIB_DEFS}) |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 63 | set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries") |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 64 | if(APPLE) |
Chris Bieneman | 361231e | 2015-08-13 20:38:16 +0000 | [diff] [blame] | 65 | set_target_properties(${libname} PROPERTIES |
| 66 | OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}") |
Chris Bieneman | 6bd006f | 2015-06-10 23:55:07 +0000 | [diff] [blame] | 67 | endif() |
| 68 | endforeach() |
| 69 | endfunction() |
Alexey Samsonov | 4e50333 | 2013-01-20 14:36:12 +0000 | [diff] [blame] | 70 | |
Chris Bieneman | 7173f07 | 2015-08-26 18:33:51 +0000 | [diff] [blame] | 71 | # Takes a list of object library targets, and a suffix and appends the proper |
| 72 | # TARGET_OBJECTS string to the output variable. |
| 73 | # format_object_libs(<output> <suffix> ...) |
| 74 | macro(format_object_libs output suffix) |
| 75 | foreach(lib ${ARGN}) |
| 76 | list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>) |
| 77 | endforeach() |
| 78 | endmacro() |
| 79 | |
Chris Bieneman | 21395f9 | 2016-08-26 20:52:22 +0000 | [diff] [blame] | 80 | function(add_compiler_rt_component name) |
| 81 | add_custom_target(${name}) |
| 82 | set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc") |
| 83 | if(COMMAND runtime_register_component) |
| 84 | runtime_register_component(${name}) |
| 85 | endif() |
| 86 | add_dependencies(compiler-rt ${name}) |
| 87 | endfunction() |
| 88 | |
Evgeniy Stepanov | c6daf73c | 2017-08-29 22:12:31 +0000 | [diff] [blame] | 89 | macro(set_output_name output name arch) |
| 90 | if(ANDROID AND ${arch} STREQUAL "i386") |
| 91 | set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}") |
| 92 | else() |
| 93 | set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}") |
| 94 | endif() |
| 95 | endmacro() |
| 96 | |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 97 | # Adds static or shared runtime for a list of architectures and operating |
| 98 | # systems and puts it in the proper directory in the build and install trees. |
| 99 | # add_compiler_rt_runtime(<name> |
| 100 | # {STATIC|SHARED} |
| 101 | # ARCHS <architectures> |
| 102 | # OS <os list> |
Alexey Samsonov | 78a8435 | 2014-03-31 13:45:36 +0000 | [diff] [blame] | 103 | # SOURCES <source files> |
| 104 | # CFLAGS <compile flags> |
Francis Ricci | 17781c7 | 2017-01-10 04:33:04 +0000 | [diff] [blame] | 105 | # LINK_FLAGS <linker flags> |
Evgeniy Stepanov | 9e922e7 | 2014-09-29 13:18:55 +0000 | [diff] [blame] | 106 | # DEFS <compile definitions> |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 107 | # LINK_LIBS <linked libraries> (only for shared library) |
Chris Bieneman | 7173f07 | 2015-08-26 18:33:51 +0000 | [diff] [blame] | 108 | # OBJECT_LIBS <object libraries to use as sources> |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 109 | # PARENT_TARGET <convenience parent target>) |
| 110 | function(add_compiler_rt_runtime name type) |
| 111 | if(NOT type MATCHES "^(STATIC|SHARED)$") |
| 112 | message(FATAL_ERROR "type argument must be STATIC or SHARED") |
| 113 | return() |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 114 | endif() |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 115 | cmake_parse_arguments(LIB |
| 116 | "" |
| 117 | "PARENT_TARGET" |
Francis Ricci | 17781c7 | 2017-01-10 04:33:04 +0000 | [diff] [blame] | 118 | "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS" |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 119 | ${ARGN}) |
| 120 | set(libnames) |
Bob Haarman | 1012fe8 | 2017-03-22 17:25:49 +0000 | [diff] [blame] | 121 | # Until we support this some other way, build compiler-rt runtime without LTO |
| 122 | # to allow non-LTO projects to link with it. |
| 123 | if(COMPILER_RT_HAS_FNO_LTO_FLAG) |
| 124 | set(NO_LTO_FLAGS "-fno-lto") |
| 125 | else() |
| 126 | set(NO_LTO_FLAGS "") |
| 127 | endif() |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 128 | if(APPLE) |
| 129 | foreach(os ${LIB_OS}) |
| 130 | if(type STREQUAL "STATIC") |
| 131 | set(libname "${name}_${os}") |
| 132 | else() |
| 133 | set(libname "${name}_${os}_dynamic") |
Francis Ricci | 17781c7 | 2017-01-10 04:33:04 +0000 | [diff] [blame] | 134 | set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 135 | endif() |
Daniel Sanders | de098c9 | 2016-01-27 09:28:01 +0000 | [diff] [blame] | 136 | list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 137 | if(LIB_ARCHS_${libname}) |
| 138 | list(APPEND libnames ${libname}) |
Bob Haarman | 1012fe8 | 2017-03-22 17:25:49 +0000 | [diff] [blame] | 139 | set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 140 | set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) |
Chris Bieneman | 7173f07 | 2015-08-26 18:33:51 +0000 | [diff] [blame] | 141 | set(sources_${libname} ${LIB_SOURCES}) |
| 142 | format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 143 | endif() |
| 144 | endforeach() |
| 145 | else() |
| 146 | foreach(arch ${LIB_ARCHS}) |
| 147 | if(NOT CAN_TARGET_${arch}) |
| 148 | message(FATAL_ERROR "Architecture ${arch} can't be targeted") |
| 149 | return() |
| 150 | endif() |
| 151 | if(type STREQUAL "STATIC") |
| 152 | set(libname "${name}-${arch}") |
Evgeniy Stepanov | c6daf73c | 2017-08-29 22:12:31 +0000 | [diff] [blame] | 153 | set_output_name(output_name_${libname} ${name} ${arch}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 154 | else() |
| 155 | set(libname "${name}-dynamic-${arch}") |
Etienne Bergeron | 7e2fc3b | 2016-06-21 14:46:32 +0000 | [diff] [blame] | 156 | set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) |
Francis Ricci | 17781c7 | 2017-01-10 04:33:04 +0000 | [diff] [blame] | 157 | set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 158 | if(WIN32) |
Evgeniy Stepanov | c6daf73c | 2017-08-29 22:12:31 +0000 | [diff] [blame] | 159 | set_output_name(output_name_${libname} ${name}_dynamic ${arch}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 160 | else() |
Evgeniy Stepanov | c6daf73c | 2017-08-29 22:12:31 +0000 | [diff] [blame] | 161 | set_output_name(output_name_${libname} ${name} ${arch}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 162 | endif() |
| 163 | endif() |
Chris Bieneman | 7173f07 | 2015-08-26 18:33:51 +0000 | [diff] [blame] | 164 | set(sources_${libname} ${LIB_SOURCES}) |
| 165 | format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 166 | set(libnames ${libnames} ${libname}) |
Bob Haarman | 1012fe8 | 2017-03-22 17:25:49 +0000 | [diff] [blame] | 167 | set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 168 | endforeach() |
| 169 | endif() |
Alexey Samsonov | 8c2cd86 | 2013-01-20 13:58:10 +0000 | [diff] [blame] | 170 | |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 171 | if(NOT libnames) |
| 172 | return() |
| 173 | endif() |
| 174 | |
| 175 | if(LIB_PARENT_TARGET) |
Chris Bieneman | 497a0ac | 2016-02-23 21:55:38 +0000 | [diff] [blame] | 176 | # If the parent targets aren't created we should create them |
| 177 | if(NOT TARGET ${LIB_PARENT_TARGET}) |
| 178 | add_custom_target(${LIB_PARENT_TARGET}) |
| 179 | endif() |
| 180 | if(NOT TARGET install-${LIB_PARENT_TARGET}) |
| 181 | # The parent install target specifies the parent component to scrape up |
| 182 | # anything not installed by the individual install targets, and to handle |
| 183 | # installation when running the multi-configuration generators. |
| 184 | add_custom_target(install-${LIB_PARENT_TARGET} |
| 185 | DEPENDS ${LIB_PARENT_TARGET} |
| 186 | COMMAND "${CMAKE_COMMAND}" |
| 187 | -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET} |
| 188 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 189 | set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES |
| 190 | FOLDER "Compiler-RT Misc") |
Chris Bieneman | 83877ee | 2016-08-19 22:17:46 +0000 | [diff] [blame] | 191 | add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET}) |
Chris Bieneman | 497a0ac | 2016-02-23 21:55:38 +0000 | [diff] [blame] | 192 | endif() |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 193 | endif() |
| 194 | |
| 195 | foreach(libname ${libnames}) |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 196 | # If you are using a multi-configuration generator we don't generate |
Chris Bieneman | 497a0ac | 2016-02-23 21:55:38 +0000 | [diff] [blame] | 197 | # per-library install rules, so we fall back to the parent target COMPONENT |
| 198 | if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET) |
| 199 | set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET}) |
| 200 | else() |
| 201 | set(COMPONENT_OPTION COMPONENT ${libname}) |
| 202 | endif() |
| 203 | |
Chris Bieneman | 7173f07 | 2015-08-26 18:33:51 +0000 | [diff] [blame] | 204 | add_library(${libname} ${type} ${sources_${libname}}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 205 | set_target_compile_flags(${libname} ${extra_cflags_${libname}}) |
Francis Ricci | 17781c7 | 2017-01-10 04:33:04 +0000 | [diff] [blame] | 206 | set_target_link_flags(${libname} ${extra_link_flags_${libname}}) |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 207 | set_property(TARGET ${libname} APPEND PROPERTY |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 208 | COMPILE_DEFINITIONS ${LIB_DEFS}) |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 209 | set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 210 | set_target_properties(${libname} PROPERTIES |
| 211 | OUTPUT_NAME ${output_name_${libname}}) |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 212 | set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime") |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 213 | if(LIB_LINK_LIBS) |
| 214 | target_link_libraries(${libname} ${LIB_LINK_LIBS}) |
| 215 | endif() |
Francis Ricci | e7729c8 | 2016-09-07 20:32:48 +0000 | [diff] [blame] | 216 | if(${type} STREQUAL "SHARED") |
Francis Ricci | e7729c8 | 2016-09-07 20:32:48 +0000 | [diff] [blame] | 217 | if(WIN32 AND NOT CYGWIN AND NOT MINGW) |
| 218 | set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "") |
| 219 | set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib") |
| 220 | endif() |
Kuba Mracek | 132c829 | 2017-04-26 18:59:22 +0000 | [diff] [blame] | 221 | if(APPLE) |
| 222 | # Ad-hoc sign the dylibs |
| 223 | add_custom_command(TARGET ${libname} |
| 224 | POST_BUILD |
| 225 | COMMAND codesign --sign - $<TARGET_FILE:${libname}> |
| 226 | WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR} |
| 227 | ) |
| 228 | endif() |
Chris Bieneman | 201d065 | 2015-08-18 17:32:18 +0000 | [diff] [blame] | 229 | endif() |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 230 | install(TARGETS ${libname} |
| 231 | ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} |
| 232 | ${COMPONENT_OPTION} |
| 233 | LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} |
| 234 | ${COMPONENT_OPTION} |
| 235 | RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} |
| 236 | ${COMPONENT_OPTION}) |
Chris Bieneman | 497a0ac | 2016-02-23 21:55:38 +0000 | [diff] [blame] | 237 | |
| 238 | # We only want to generate per-library install targets if you aren't using |
| 239 | # an IDE because the extra targets get cluttered in IDEs. |
| 240 | if(NOT CMAKE_CONFIGURATION_TYPES) |
| 241 | add_custom_target(install-${libname} |
| 242 | DEPENDS ${libname} |
| 243 | COMMAND "${CMAKE_COMMAND}" |
| 244 | -DCMAKE_INSTALL_COMPONENT=${libname} |
| 245 | -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") |
| 246 | # If you have a parent target specified, we bind the new install target |
| 247 | # to the parent install target. |
| 248 | if(LIB_PARENT_TARGET) |
| 249 | add_dependencies(install-${LIB_PARENT_TARGET} install-${libname}) |
| 250 | endif() |
| 251 | endif() |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 252 | if(APPLE) |
| 253 | set_target_properties(${libname} PROPERTIES |
| 254 | OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}") |
| 255 | endif() |
Chris Bieneman | 69a372d | 2015-12-03 20:08:22 +0000 | [diff] [blame] | 256 | |
| 257 | if(type STREQUAL "SHARED") |
| 258 | rt_externalize_debuginfo(${libname}) |
| 259 | endif() |
Chris Bieneman | d160260 | 2015-08-25 19:53:09 +0000 | [diff] [blame] | 260 | endforeach() |
| 261 | if(LIB_PARENT_TARGET) |
Alexey Samsonov | 2eff7f7 | 2016-02-26 20:59:40 +0000 | [diff] [blame] | 262 | add_dependencies(${LIB_PARENT_TARGET} ${libnames}) |
Chris Bieneman | 201d065 | 2015-08-18 17:32:18 +0000 | [diff] [blame] | 263 | endif() |
| 264 | endfunction() |
Alexey Samsonov | b399118 | 2013-01-21 08:12:20 +0000 | [diff] [blame] | 265 | |
Sumanth Gundapaneni | b76bf10 | 2016-01-14 18:18:49 +0000 | [diff] [blame] | 266 | # when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help |
| 267 | # in compilation and linking of unittests. |
| 268 | string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}") |
Francis Ricci | 17781c7 | 2017-01-10 04:33:04 +0000 | [diff] [blame] | 269 | set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS}) |
Timur Iskhodzhanov | 1b42b81 | 2014-05-30 12:42:57 +0000 | [diff] [blame] | 270 | |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 271 | # Unittests support. |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 272 | set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest) |
Chandler Carruth | 65fd238 | 2013-11-15 10:21:15 +0000 | [diff] [blame] | 273 | set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc) |
Alexey Samsonov | c41ca6d | 2014-03-24 13:29:20 +0000 | [diff] [blame] | 274 | set(COMPILER_RT_GTEST_CFLAGS |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 275 | -DGTEST_NO_LLVM_RAW_OSTREAM=1 |
Timur Iskhodzhanov | 82ee043 | 2014-05-28 08:38:13 +0000 | [diff] [blame] | 276 | -DGTEST_HAS_RTTI=0 |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 277 | -I${COMPILER_RT_GTEST_PATH}/include |
Chandler Carruth | 65fd238 | 2013-11-15 10:21:15 +0000 | [diff] [blame] | 278 | -I${COMPILER_RT_GTEST_PATH} |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 279 | ) |
| 280 | |
Sumanth Gundapaneni | b76bf10 | 2016-01-14 18:18:49 +0000 | [diff] [blame] | 281 | append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS) |
Chandler Carruth | bbb82ca | 2017-01-05 03:41:41 +0000 | [diff] [blame] | 282 | append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS) |
Alexey Samsonov | 6239ebc | 2015-01-06 20:58:40 +0000 | [diff] [blame] | 283 | |
Timur Iskhodzhanov | 82ee043 | 2014-05-28 08:38:13 +0000 | [diff] [blame] | 284 | if(MSVC) |
Timur Iskhodzhanov | 1b42b81 | 2014-05-30 12:42:57 +0000 | [diff] [blame] | 285 | # clang doesn't support exceptions on Windows yet. |
Sumanth Gundapaneni | b76bf10 | 2016-01-14 18:18:49 +0000 | [diff] [blame] | 286 | list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0) |
Timur Iskhodzhanov | 1b42b81 | 2014-05-30 12:42:57 +0000 | [diff] [blame] | 287 | |
| 288 | # We should teach clang to understand "#pragma intrinsic", see PR19898. |
Sumanth Gundapaneni | b76bf10 | 2016-01-14 18:18:49 +0000 | [diff] [blame] | 289 | list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline) |
Timur Iskhodzhanov | 1b42b81 | 2014-05-30 12:42:57 +0000 | [diff] [blame] | 290 | |
Timur Iskhodzhanov | 82ee043 | 2014-05-28 08:38:13 +0000 | [diff] [blame] | 291 | # Clang doesn't support SEH on Windows yet. |
| 292 | list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0) |
Timur Iskhodzhanov | 1b42b81 | 2014-05-30 12:42:57 +0000 | [diff] [blame] | 293 | |
| 294 | # gtest use a lot of stuff marked as deprecated on Windows. |
| 295 | list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations) |
Timur Iskhodzhanov | 82ee043 | 2014-05-28 08:38:13 +0000 | [diff] [blame] | 296 | endif() |
| 297 | |
George Karpenkov | 769124d | 2017-08-15 22:56:10 +0000 | [diff] [blame] | 298 | # Compile and register compiler-rt tests. |
| 299 | # generate_compiler_rt_tests(<output object files> <test_suite> <test_name> |
| 300 | # <test architecture> |
| 301 | # KIND <custom prefix> |
| 302 | # SUBDIR <subdirectory for testing binary> |
| 303 | # SOURCES <sources to compile> |
| 304 | # RUNTIME <tests runtime to link in> |
| 305 | # CFLAGS <compile-time flags> |
| 306 | # COMPILE_DEPS <compile-time dependencies> |
| 307 | # DEPS <dependencies> |
| 308 | # LINK_FLAGS <flags to use during linking> |
| 309 | # ) |
| 310 | function(generate_compiler_rt_tests test_objects test_suite testname arch) |
| 311 | cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR" |
| 312 | "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN}) |
| 313 | |
| 314 | foreach(source ${TEST_SOURCES}) |
| 315 | sanitizer_test_compile( |
| 316 | "${test_objects}" "${source}" "${arch}" |
| 317 | KIND ${TEST_KIND} |
| 318 | COMPILE_DEPS ${TEST_COMPILE_DEPS} |
| 319 | DEPS ${TEST_DEPS} |
| 320 | CFLAGS ${TEST_CFLAGS} |
| 321 | ) |
| 322 | endforeach() |
| 323 | |
| 324 | set(TEST_DEPS ${${test_objects}}) |
| 325 | |
| 326 | if(NOT "${TEST_RUNTIME}" STREQUAL "") |
| 327 | list(APPEND TEST_DEPS ${TEST_RUNTIME}) |
| 328 | list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>) |
| 329 | endif() |
| 330 | |
| 331 | add_compiler_rt_test(${test_suite} "${testname}" "${arch}" |
| 332 | SUBDIR ${TEST_SUBDIR} |
| 333 | OBJECTS ${${test_objects}} |
| 334 | DEPS ${TEST_DEPS} |
George Karpenkov | 4c26922 | 2017-08-15 23:22:52 +0000 | [diff] [blame] | 335 | LINK_FLAGS ${TEST_LINK_FLAGS} |
George Karpenkov | 769124d | 2017-08-15 22:56:10 +0000 | [diff] [blame] | 336 | ) |
| 337 | set("${test_objects}" "${${test_objects}}" PARENT_SCOPE) |
| 338 | endfunction() |
| 339 | |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 340 | # Link objects into a single executable with COMPILER_RT_TEST_COMPILER, |
| 341 | # using specified link flags. Make executable a part of provided |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 342 | # test_suite. |
George Karpenkov | 769124d | 2017-08-15 22:56:10 +0000 | [diff] [blame] | 343 | # add_compiler_rt_test(<test_suite> <test_name> <arch> |
Alexey Samsonov | 969902b | 2014-12-17 23:14:01 +0000 | [diff] [blame] | 344 | # SUBDIR <subdirectory for binary> |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 345 | # OBJECTS <object files> |
| 346 | # DEPS <deps (e.g. runtime libs)> |
| 347 | # LINK_FLAGS <link flags>) |
George Karpenkov | 769124d | 2017-08-15 22:56:10 +0000 | [diff] [blame] | 348 | function(add_compiler_rt_test test_suite test_name arch) |
Filipe Cabecinhas | 7af0a1c | 2015-06-19 03:39:24 +0000 | [diff] [blame] | 349 | cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN}) |
George Karpenkov | d46f17d | 2017-08-15 18:32:28 +0000 | [diff] [blame] | 350 | set(output_dir ${CMAKE_CURRENT_BINARY_DIR}) |
Alexey Samsonov | 969902b | 2014-12-17 23:14:01 +0000 | [diff] [blame] | 351 | if(TEST_SUBDIR) |
George Karpenkov | d46f17d | 2017-08-15 18:32:28 +0000 | [diff] [blame] | 352 | set(output_dir "${output_dir}/${TEST_SUBDIR}") |
Alexey Samsonov | 969902b | 2014-12-17 23:14:01 +0000 | [diff] [blame] | 353 | endif() |
George Karpenkov | d46f17d | 2017-08-15 18:32:28 +0000 | [diff] [blame] | 354 | set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}") |
| 355 | file(MAKE_DIRECTORY "${output_dir}") |
| 356 | set(output_bin "${output_dir}/${test_name}") |
Timur Iskhodzhanov | 23cfd6e | 2015-01-22 14:54:22 +0000 | [diff] [blame] | 357 | if(MSVC) |
| 358 | set(output_bin "${output_bin}.exe") |
| 359 | endif() |
Etienne Bergeron | 3df2879 | 2016-05-16 14:58:07 +0000 | [diff] [blame] | 360 | |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 361 | # Use host compiler in a standalone build, and just-built Clang otherwise. |
| 362 | if(NOT COMPILER_RT_STANDALONE_BUILD) |
| 363 | list(APPEND TEST_DEPS clang) |
| 364 | endif() |
George Karpenkov | 769124d | 2017-08-15 22:56:10 +0000 | [diff] [blame] | 365 | |
| 366 | get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS) |
| 367 | list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS}) |
| 368 | |
Chandler Carruth | 332e1b1 | 2014-05-15 16:33:58 +0000 | [diff] [blame] | 369 | # If we're not on MSVC, include the linker flags from CMAKE but override them |
| 370 | # with the provided link flags. This ensures that flags which are required to |
| 371 | # link programs at all are included, but the changes needed for the test |
| 372 | # trump. With MSVC we can't do that because CMake is set up to run link.exe |
| 373 | # when linking, not the compiler. Here, we hack it to use the compiler |
| 374 | # because we want to use -fsanitize flags. |
| 375 | if(NOT MSVC) |
| 376 | set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}") |
| 377 | separate_arguments(TEST_LINK_FLAGS) |
| 378 | endif() |
George Karpenkov | 93e9e8a | 2017-08-21 21:19:13 +0000 | [diff] [blame] | 379 | add_custom_command( |
| 380 | OUTPUT "${output_bin}" |
| 381 | COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}" |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 382 | ${TEST_LINK_FLAGS} |
George Karpenkov | 93e9e8a | 2017-08-21 21:19:13 +0000 | [diff] [blame] | 383 | DEPENDS ${TEST_DEPS} |
| 384 | ) |
| 385 | add_custom_target(T${test_name} DEPENDS "${output_bin}") |
| 386 | set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests") |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 387 | |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 388 | # Make the test suite depend on the binary. |
George Karpenkov | 93e9e8a | 2017-08-21 21:19:13 +0000 | [diff] [blame] | 389 | add_dependencies(${test_suite} T${test_name}) |
George Karpenkov | d46f17d | 2017-08-15 18:32:28 +0000 | [diff] [blame] | 390 | endfunction() |
Alexey Samsonov | e838135 | 2013-05-21 13:48:27 +0000 | [diff] [blame] | 391 | |
Chris Bieneman | 86792ea | 2016-02-23 21:50:39 +0000 | [diff] [blame] | 392 | macro(add_compiler_rt_resource_file target_name file_name component) |
Alexey Samsonov | e838135 | 2013-05-21 13:48:27 +0000 | [diff] [blame] | 393 | set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}") |
Alexey Samsonov | 1181a10 | 2014-02-18 14:28:53 +0000 | [diff] [blame] | 394 | set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}") |
Alexey Samsonov | 4cced3e | 2014-02-27 07:22:59 +0000 | [diff] [blame] | 395 | add_custom_command(OUTPUT ${dst_file} |
| 396 | DEPENDS ${src_file} |
Alexey Samsonov | e838135 | 2013-05-21 13:48:27 +0000 | [diff] [blame] | 397 | COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file} |
Alexey Samsonov | 4cced3e | 2014-02-27 07:22:59 +0000 | [diff] [blame] | 398 | COMMENT "Copying ${file_name}...") |
| 399 | add_custom_target(${target_name} DEPENDS ${dst_file}) |
Alexey Samsonov | e838135 | 2013-05-21 13:48:27 +0000 | [diff] [blame] | 400 | # Install in Clang resource directory. |
Chris Bieneman | 86792ea | 2016-02-23 21:50:39 +0000 | [diff] [blame] | 401 | install(FILES ${file_name} |
| 402 | DESTINATION ${COMPILER_RT_INSTALL_PATH} |
| 403 | COMPONENT ${component}) |
| 404 | add_dependencies(${component} ${target_name}) |
Etienne Bergeron | ab42f4d | 2016-07-11 21:51:56 +0000 | [diff] [blame] | 405 | |
| 406 | set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc") |
Alexey Samsonov | e838135 | 2013-05-21 13:48:27 +0000 | [diff] [blame] | 407 | endmacro() |
Evgeniy Stepanov | 322e89c | 2014-02-27 08:41:40 +0000 | [diff] [blame] | 408 | |
| 409 | macro(add_compiler_rt_script name) |
| 410 | set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name}) |
| 411 | set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name}) |
| 412 | add_custom_command(OUTPUT ${dst} |
| 413 | DEPENDS ${src} |
| 414 | COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst} |
| 415 | COMMENT "Copying ${name}...") |
| 416 | add_custom_target(${name} DEPENDS ${dst}) |
| 417 | install(FILES ${dst} |
| 418 | PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE |
| 419 | DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin) |
| 420 | endmacro(add_compiler_rt_script src name) |
Alexey Samsonov | eacb4d8 | 2014-05-09 22:11:03 +0000 | [diff] [blame] | 421 | |
| 422 | # Builds custom version of libc++ and installs it in <prefix>. |
| 423 | # Can be used to build sanitized versions of libc++ for running unit tests. |
| 424 | # add_custom_libcxx(<name> <prefix> |
| 425 | # DEPS <list of build deps> |
| 426 | # CFLAGS <list of compile flags>) |
| 427 | macro(add_custom_libcxx name prefix) |
| 428 | if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES) |
| 429 | message(FATAL_ERROR "libcxx not found!") |
| 430 | endif() |
| 431 | |
Filipe Cabecinhas | 7af0a1c | 2015-06-19 03:39:24 +0000 | [diff] [blame] | 432 | cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN}) |
Alexey Samsonov | eacb4d8 | 2014-05-09 22:11:03 +0000 | [diff] [blame] | 433 | foreach(flag ${LIBCXX_CFLAGS}) |
| 434 | set(flagstr "${flagstr} ${flag}") |
| 435 | endforeach() |
| 436 | set(LIBCXX_CFLAGS ${flagstr}) |
| 437 | |
| 438 | if(NOT COMPILER_RT_STANDALONE_BUILD) |
| 439 | list(APPEND LIBCXX_DEPS clang) |
| 440 | endif() |
| 441 | |
| 442 | ExternalProject_Add(${name} |
| 443 | PREFIX ${prefix} |
| 444 | SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH} |
Andy Gibbs | abd2a0b | 2015-12-02 13:46:56 +0000 | [diff] [blame] | 445 | CMAKE_ARGS -DCMAKE_MAKE_PROGRAM:STRING=${CMAKE_MAKE_PROGRAM} |
| 446 | -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER} |
Daniel Sanders | 440b861 | 2016-02-02 12:55:28 +0000 | [diff] [blame] | 447 | -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER} |
Alexey Samsonov | eacb4d8 | 2014-05-09 22:11:03 +0000 | [diff] [blame] | 448 | -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS} |
| 449 | -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS} |
| 450 | -DCMAKE_BUILD_TYPE=Release |
| 451 | -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> |
Daniel Sanders | 440b861 | 2016-02-02 12:55:28 +0000 | [diff] [blame] | 452 | -DLLVM_PATH=${LLVM_MAIN_SRC_DIR} |
Petr Hosek | 7834268 | 2017-01-16 00:33:02 +0000 | [diff] [blame] | 453 | -DLIBCXX_STANDALONE_BUILD=On |
Alexey Samsonov | 5af5f8f | 2014-05-12 21:07:28 +0000 | [diff] [blame] | 454 | LOG_BUILD 1 |
| 455 | LOG_CONFIGURE 1 |
| 456 | LOG_INSTALL 1 |
Alexey Samsonov | eacb4d8 | 2014-05-09 22:11:03 +0000 | [diff] [blame] | 457 | ) |
Alexey Samsonov | 9e694e5 | 2015-07-16 21:20:05 +0000 | [diff] [blame] | 458 | set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE) |
Alexey Samsonov | eacb4d8 | 2014-05-09 22:11:03 +0000 | [diff] [blame] | 459 | |
| 460 | ExternalProject_Add_Step(${name} force-reconfigure |
| 461 | DEPENDERS configure |
| 462 | ALWAYS 1 |
| 463 | ) |
| 464 | |
| 465 | ExternalProject_Add_Step(${name} clobber |
| 466 | COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR> |
| 467 | COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR> |
| 468 | COMMENT "Clobberring ${name} build directory..." |
| 469 | DEPENDERS configure |
| 470 | DEPENDS ${LIBCXX_DEPS} |
| 471 | ) |
| 472 | endmacro() |
Chris Bieneman | 69a372d | 2015-12-03 20:08:22 +0000 | [diff] [blame] | 473 | |
| 474 | function(rt_externalize_debuginfo name) |
| 475 | if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO) |
| 476 | return() |
| 477 | endif() |
| 478 | |
Chris Bieneman | 17bcc43 | 2016-03-31 21:17:19 +0000 | [diff] [blame] | 479 | if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP) |
| 480 | set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>) |
| 481 | endif() |
| 482 | |
Chris Bieneman | 69a372d | 2015-12-03 20:08:22 +0000 | [diff] [blame] | 483 | if(APPLE) |
| 484 | if(CMAKE_CXX_FLAGS MATCHES "-flto" |
| 485 | OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto") |
| 486 | |
| 487 | set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o) |
Chris Bieneman | 1d7a2cf | 2015-12-03 22:52:22 +0000 | [diff] [blame] | 488 | set_property(TARGET ${name} APPEND_STRING PROPERTY |
Chris Bieneman | bcd09fe | 2015-12-03 22:56:21 +0000 | [diff] [blame] | 489 | LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}") |
Chris Bieneman | 69a372d | 2015-12-03 20:08:22 +0000 | [diff] [blame] | 490 | endif() |
| 491 | add_custom_command(TARGET ${name} POST_BUILD |
| 492 | COMMAND xcrun dsymutil $<TARGET_FILE:${name}> |
Chris Bieneman | 17bcc43 | 2016-03-31 21:17:19 +0000 | [diff] [blame] | 493 | ${strip_command}) |
Chris Bieneman | 69a372d | 2015-12-03 20:08:22 +0000 | [diff] [blame] | 494 | else() |
| 495 | message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!") |
| 496 | endif() |
| 497 | endfunction() |