1include(LLVMProcessSources) 2include(LLVM-Config) 3include(DetermineGCCCompatible) 4 5function(llvm_update_compile_flags name) 6 get_property(sources TARGET ${name} PROPERTY SOURCES) 7 if("${sources}" MATCHES "\\.c(;|$)") 8 set(update_src_props ON) 9 endif() 10 11 list(APPEND LLVM_COMPILE_CFLAGS " ${LLVM_COMPILE_FLAGS}") 12 13 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to 14 # force EH 15 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH) 16 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) 17 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}") 18 set(LLVM_REQUIRES_RTTI ON) 19 endif() 20 if(MSVC) 21 list(APPEND LLVM_COMPILE_FLAGS "/EHsc") 22 endif() 23 else() 24 if(LLVM_COMPILER_IS_GCC_COMPATIBLE) 25 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions") 26 if(NOT LLVM_ENABLE_UNWIND_TABLES) 27 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables") 28 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables") 29 endif() 30 elseif(MSVC) 31 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0) 32 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-") 33 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") 34 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh") 35 endif() 36 endif() 37 38 # LLVM_REQUIRES_RTTI is an internal flag that individual 39 # targets can use to force RTTI 40 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "") 41 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) 42 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "") 43 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0) 44 if (LLVM_COMPILER_IS_GCC_COMPATIBLE) 45 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti") 46 elseif (MSVC) 47 list(APPEND LLVM_COMPILE_FLAGS "/GR-") 48 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") 49 list(APPEND LLVM_COMPILE_FLAGS "-qnortti") 50 endif () 51 elseif(MSVC) 52 list(APPEND LLVM_COMPILE_FLAGS "/GR") 53 endif() 54 55 # Assume that; 56 # - LLVM_COMPILE_FLAGS is list. 57 # - PROPERTY COMPILE_FLAGS is string. 58 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}") 59 string(REPLACE ";" " " target_compile_cflags " ${LLVM_COMPILE_CFLAGS}") 60 61 if(update_src_props) 62 foreach(fn ${sources}) 63 get_filename_component(suf ${fn} EXT) 64 if("${suf}" STREQUAL ".cpp") 65 set_property(SOURCE ${fn} APPEND_STRING PROPERTY 66 COMPILE_FLAGS "${target_compile_flags}") 67 endif() 68 if("${suf}" STREQUAL ".c") 69 set_property(SOURCE ${fn} APPEND_STRING PROPERTY 70 COMPILE_FLAGS "${target_compile_cflags}") 71 endif() 72 endforeach() 73 else() 74 # Update target props, since all sources are C++. 75 set_property(TARGET ${name} APPEND_STRING PROPERTY 76 COMPILE_FLAGS "${target_compile_flags}") 77 endif() 78 79 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) 80endfunction() 81 82function(add_llvm_symbol_exports target_name export_file) 83 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 84 set(native_export_file "${target_name}.exports") 85 add_custom_command(OUTPUT ${native_export_file} 86 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file} 87 DEPENDS ${export_file} 88 VERBATIM 89 COMMENT "Creating export file for ${target_name}") 90 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 91 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 92 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX") 93 # FIXME: `-Wl,-bE:` bypasses whatever handling there is in the build 94 # compiler driver to defer to the specified export list. 95 set(native_export_file "${export_file}") 96 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 97 LINK_FLAGS " -Wl,-bE:${export_file}") 98 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT) 99 # Gold and BFD ld require a version script rather than a plain list. 100 set(native_export_file "${target_name}.exports") 101 # FIXME: Don't write the "local:" line on OpenBSD. 102 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M) 103 add_custom_command(OUTPUT ${native_export_file} 104 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file} 105 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || : 106 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file} 107 COMMAND echo " local: *;" >> ${native_export_file} 108 COMMAND echo "};" >> ${native_export_file} 109 DEPENDS ${export_file} 110 VERBATIM 111 COMMENT "Creating export file for ${target_name}") 112 if (${LLVM_LINKER_IS_SOLARISLD}) 113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 114 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 115 else() 116 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 117 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 118 endif() 119 else() 120 set(native_export_file "${target_name}.def") 121 122 add_custom_command(OUTPUT ${native_export_file} 123 COMMAND "${Python3_EXECUTABLE}" -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))" 124 < ${export_file} > ${native_export_file} 125 DEPENDS ${export_file} 126 VERBATIM 127 COMMENT "Creating export file for ${target_name}") 128 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}") 129 if(MSVC) 130 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"") 131 endif() 132 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 133 LINK_FLAGS " ${export_file_linker_flag}") 134 endif() 135 136 add_custom_target(${target_name}_exports DEPENDS ${native_export_file}) 137 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc") 138 139 get_property(srcs TARGET ${target_name} PROPERTY SOURCES) 140 foreach(src ${srcs}) 141 get_filename_component(extension ${src} EXT) 142 if(extension STREQUAL ".cpp") 143 set(first_source_file ${src}) 144 break() 145 endif() 146 endforeach() 147 148 # Force re-linking when the exports file changes. Actually, it 149 # forces recompilation of the source file. The LINK_DEPENDS target 150 # property only works for makefile-based generators. 151 # FIXME: This is not safe because this will create the same target 152 # ${native_export_file} in several different file: 153 # - One where we emitted ${target_name}_exports 154 # - One where we emitted the build command for the following object. 155 # set_property(SOURCE ${first_source_file} APPEND PROPERTY 156 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}) 157 158 set_property(DIRECTORY APPEND 159 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file}) 160 161 add_dependencies(${target_name} ${target_name}_exports) 162 163 # Add dependency to *_exports later -- CMake issue 14747 164 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports) 165 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE) 166endfunction(add_llvm_symbol_exports) 167 168if (NOT DEFINED LLVM_LINKER_DETECTED) 169 if(APPLE) 170 execute_process( 171 COMMAND "${CMAKE_LINKER}" -v 172 ERROR_VARIABLE stderr 173 ) 174 if("${stderr}" MATCHES "PROJECT:ld64") 175 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 176 set(LLVM_LINKER_IS_LD64 YES CACHE INTERNAL "") 177 message(STATUS "Linker detection: ld64") 178 else() 179 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "") 180 message(STATUS "Linker detection: unknown") 181 endif() 182 elseif(NOT WIN32) 183 # Detect what linker we have here 184 if( LLVM_USE_LINKER ) 185 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version) 186 else() 187 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}") 188 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version) 189 endif() 190 execute_process( 191 COMMAND ${command} 192 OUTPUT_VARIABLE stdout 193 ERROR_VARIABLE stderr 194 ) 195 if("${stdout}" MATCHES "GNU gold") 196 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 197 set(LLVM_LINKER_IS_GOLD YES CACHE INTERNAL "") 198 message(STATUS "Linker detection: GNU Gold") 199 elseif("${stdout}" MATCHES "^LLD") 200 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 201 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "") 202 message(STATUS "Linker detection: LLD") 203 elseif("${stdout}" MATCHES "GNU ld") 204 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 205 set(LLVM_LINKER_IS_GNULD YES CACHE INTERNAL "") 206 message(STATUS "Linker detection: GNU ld") 207 elseif("${stderr}" MATCHES "Solaris Link Editors" OR 208 "${stdout}" MATCHES "Solaris Link Editors") 209 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 210 set(LLVM_LINKER_IS_SOLARISLD YES CACHE INTERNAL "") 211 message(STATUS "Linker detection: Solaris ld") 212 else() 213 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "") 214 message(STATUS "Linker detection: unknown") 215 endif() 216 endif() 217endif() 218 219function(add_link_opts target_name) 220 # Don't use linker optimizations in debug builds since it slows down the 221 # linker in a context where the optimizations are not important. 222 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 223 224 # Pass -O3 to the linker. This enabled different optimizations on different 225 # linkers. 226 if(NOT (CMAKE_SYSTEM_NAME MATCHES "Darwin|SunOS|AIX|OS390" OR WIN32)) 227 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 228 LINK_FLAGS " -Wl,-O3") 229 endif() 230 231 if(NOT LLVM_NO_DEAD_STRIP) 232 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 233 # ld64's implementation of -dead_strip breaks tools that use plugins. 234 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 235 LINK_FLAGS " -Wl,-dead_strip") 236 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") 237 # Support for ld -z discard-unused=sections was only added in 238 # Solaris 11.4. 239 include(CheckLinkerFlag) 240 check_linker_flag("-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED) 241 if (LINKER_SUPPORTS_Z_DISCARD_UNUSED) 242 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 243 LINK_FLAGS " -Wl,-z,discard-unused=sections") 244 endif() 245 elseif(NOT WIN32 AND NOT CMAKE_SYSTEM_NAME MATCHES "OpenBSD|AIX|OS390") 246 # TODO Revisit this later on z/OS. 247 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 248 LINK_FLAGS " -Wl,--gc-sections") 249 endif() 250 else() #LLVM_NO_DEAD_STRIP 251 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX") 252 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 253 LINK_FLAGS " -Wl,-bnogc") 254 endif() 255 endif() 256 endif() 257 258 if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX") 259 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 260 LINK_FLAGS " -Wl,-brtl") 261 endif() 262endfunction(add_link_opts) 263 264# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}. 265# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more, 266# or a certain builder, for eaxample, msbuild.exe, would be confused. 267function(set_output_directory target) 268 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN}) 269 270 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY. 271 # It affects output of add_library(MODULE). 272 if(WIN32 OR CYGWIN) 273 # DLL platform 274 set(module_dir ${ARG_BINARY_DIR}) 275 else() 276 set(module_dir ${ARG_LIBRARY_DIR}) 277 endif() 278 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".") 279 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES}) 280 string(TOUPPER "${build_mode}" CONFIG_SUFFIX) 281 if(ARG_BINARY_DIR) 282 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR}) 283 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi}) 284 endif() 285 if(ARG_LIBRARY_DIR) 286 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR}) 287 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li}) 288 endif() 289 if(module_dir) 290 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir}) 291 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi}) 292 endif() 293 endforeach() 294 else() 295 if(ARG_BINARY_DIR) 296 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR}) 297 endif() 298 if(ARG_LIBRARY_DIR) 299 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR}) 300 endif() 301 if(module_dir) 302 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir}) 303 endif() 304 endif() 305endfunction() 306 307# If on Windows and building with MSVC, add the resource script containing the 308# VERSIONINFO data to the project. This embeds version resource information 309# into the output .exe or .dll. 310# TODO: Enable for MinGW Windows builds too. 311# 312function(add_windows_version_resource_file OUT_VAR) 313 set(sources ${ARGN}) 314 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") 315 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc) 316 if(EXISTS ${resource_file}) 317 set(sources ${sources} ${resource_file}) 318 source_group("Resource Files" ${resource_file}) 319 set(windows_resource_file ${resource_file} PARENT_SCOPE) 320 endif() 321 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") 322 323 set(${OUT_VAR} ${sources} PARENT_SCOPE) 324endfunction(add_windows_version_resource_file) 325 326# set_windows_version_resource_properties(name resource_file... 327# VERSION_MAJOR int 328# Optional major version number (defaults to LLVM_VERSION_MAJOR) 329# VERSION_MINOR int 330# Optional minor version number (defaults to LLVM_VERSION_MINOR) 331# VERSION_PATCHLEVEL int 332# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH) 333# VERSION_STRING 334# Optional version string (defaults to PACKAGE_VERSION) 335# PRODUCT_NAME 336# Optional product name string (defaults to "LLVM") 337# ) 338function(set_windows_version_resource_properties name resource_file) 339 cmake_parse_arguments(ARG 340 "" 341 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME" 342 "" 343 ${ARGN}) 344 345 if (NOT DEFINED ARG_VERSION_MAJOR) 346 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR}) 347 endif() 348 349 if (NOT DEFINED ARG_VERSION_MINOR) 350 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR}) 351 endif() 352 353 if (NOT DEFINED ARG_VERSION_PATCHLEVEL) 354 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH}) 355 endif() 356 357 if (NOT DEFINED ARG_VERSION_STRING) 358 set(ARG_VERSION_STRING ${PACKAGE_VERSION}) 359 endif() 360 361 if (NOT DEFINED ARG_PRODUCT_NAME) 362 set(ARG_PRODUCT_NAME "LLVM") 363 endif() 364 365 set_property(SOURCE ${resource_file} 366 PROPERTY COMPILE_FLAGS /nologo) 367 set_property(SOURCE ${resource_file} 368 PROPERTY COMPILE_DEFINITIONS 369 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}" 370 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}" 371 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}" 372 "RC_VERSION_FIELD_4=0" 373 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\"" 374 "RC_INTERNAL_NAME=\"${name}\"" 375 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\"" 376 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"") 377endfunction(set_windows_version_resource_properties) 378 379# llvm_add_library(name sources... 380# SHARED;STATIC 381# STATIC by default w/o BUILD_SHARED_LIBS. 382# SHARED by default w/ BUILD_SHARED_LIBS. 383# OBJECT 384# Also create an OBJECT library target. Default if STATIC && SHARED. 385# MODULE 386# Target ${name} might not be created on unsupported platforms. 387# Check with "if(TARGET ${name})". 388# DISABLE_LLVM_LINK_LLVM_DYLIB 389# Do not link this library to libLLVM, even if 390# LLVM_LINK_LLVM_DYLIB is enabled. 391# OUTPUT_NAME name 392# Corresponds to OUTPUT_NAME in target properties. 393# DEPENDS targets... 394# Same semantics as add_dependencies(). 395# LINK_COMPONENTS components... 396# Same as the variable LLVM_LINK_COMPONENTS. 397# LINK_LIBS lib_targets... 398# Same semantics as target_link_libraries(). 399# ADDITIONAL_HEADERS 400# May specify header files for IDE generators. 401# SONAME 402# Should set SONAME link flags and create symlinks 403# NO_INSTALL_RPATH 404# Suppress default RPATH settings in shared libraries. 405# PLUGIN_TOOL 406# The tool (i.e. cmake target) that this plugin will link against 407# COMPONENT_LIB 408# This is used to specify that this is a component library of 409# LLVM which means that the source resides in llvm/lib/ and it is a 410# candidate for inclusion into libLLVM.so. 411# ) 412function(llvm_add_library name) 413 cmake_parse_arguments(ARG 414 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB" 415 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH" 416 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS" 417 ${ARGN}) 418 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) 419 if(ARG_ADDITIONAL_HEADERS) 420 # Pass through ADDITIONAL_HEADERS. 421 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS}) 422 endif() 423 if(ARG_OBJLIBS) 424 set(ALL_FILES ${ARG_OBJLIBS}) 425 else() 426 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS}) 427 endif() 428 429 if(ARG_MODULE) 430 if(ARG_SHARED OR ARG_STATIC) 431 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.") 432 endif() 433 # Plugins that link against a tool are allowed even when plugins in general are not 434 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)) 435 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.") 436 return() 437 endif() 438 else() 439 if(ARG_PLUGIN_TOOL) 440 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.") 441 endif() 442 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC) 443 set(ARG_SHARED TRUE) 444 endif() 445 if(NOT ARG_SHARED) 446 set(ARG_STATIC TRUE) 447 endif() 448 endif() 449 450 # Generate objlib 451 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT) 452 # Generate an obj library for both targets. 453 set(obj_name "obj.${name}") 454 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL 455 ${ALL_FILES} 456 ) 457 llvm_update_compile_flags(${obj_name}) 458 if(CMAKE_GENERATOR STREQUAL "Xcode") 459 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c) 460 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n") 461 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit") 462 endif() 463 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE}) 464 465 # Do add_dependencies(obj) later due to CMake issue 14747. 466 list(APPEND objlibs ${obj_name}) 467 468 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries") 469 if(ARG_DEPENDS) 470 add_dependencies(${obj_name} ${ARG_DEPENDS}) 471 endif() 472 # Treat link libraries like PUBLIC dependencies. LINK_LIBS might 473 # result in generating header files. Add a dependendency so that 474 # the generated header is created before this object library. 475 if(ARG_LINK_LIBS) 476 cmake_parse_arguments(LINK_LIBS_ARG 477 "" 478 "" 479 "PUBLIC;PRIVATE" 480 ${ARG_LINK_LIBS}) 481 foreach(link_lib ${LINK_LIBS_ARG_PUBLIC}) 482 if(LLVM_PTHREAD_LIB) 483 # Can't specify a dependence on -lpthread 484 if(NOT ${link_lib} STREQUAL ${LLVM_PTHREAD_LIB}) 485 add_dependencies(${obj_name} ${link_lib}) 486 endif() 487 else() 488 add_dependencies(${obj_name} ${link_lib}) 489 endif() 490 endforeach() 491 endif() 492 endif() 493 494 if(ARG_SHARED AND ARG_STATIC) 495 # static 496 set(name_static "${name}_static") 497 if(ARG_OUTPUT_NAME) 498 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}") 499 endif() 500 # DEPENDS has been appended to LLVM_COMMON_LIBS. 501 llvm_add_library(${name_static} STATIC 502 ${output_name} 503 OBJLIBS ${ALL_FILES} # objlib 504 LINK_LIBS ${ARG_LINK_LIBS} 505 LINK_COMPONENTS ${ARG_LINK_COMPONENTS} 506 ) 507 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY. 508 set(ARG_STATIC) 509 endif() 510 511 if(ARG_MODULE) 512 add_library(${name} MODULE ${ALL_FILES}) 513 elseif(ARG_SHARED) 514 add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) 515 add_library(${name} SHARED ${ALL_FILES}) 516 else() 517 add_library(${name} STATIC ${ALL_FILES}) 518 endif() 519 520 if(ARG_COMPONENT_LIB) 521 set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE) 522 set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name}) 523 endif() 524 525 if(NOT ARG_NO_INSTALL_RPATH) 526 if(ARG_MODULE OR ARG_SHARED) 527 llvm_setup_rpath(${name}) 528 endif() 529 endif() 530 531 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS}) 532 533 if(DEFINED windows_resource_file) 534 set_windows_version_resource_properties(${name} ${windows_resource_file}) 535 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE) 536 endif() 537 538 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 539 # $<TARGET_OBJECTS> doesn't require compile flags. 540 if(NOT obj_name) 541 llvm_update_compile_flags(${name}) 542 endif() 543 add_link_opts( ${name} ) 544 if(ARG_OUTPUT_NAME) 545 set_target_properties(${name} 546 PROPERTIES 547 OUTPUT_NAME ${ARG_OUTPUT_NAME} 548 ) 549 endif() 550 551 if(ARG_MODULE) 552 set_target_properties(${name} PROPERTIES 553 PREFIX "" 554 SUFFIX ${LLVM_PLUGIN_EXT} 555 ) 556 endif() 557 558 if(ARG_SHARED) 559 if(MSVC) 560 set_target_properties(${name} PROPERTIES 561 PREFIX "" 562 ) 563 endif() 564 565 # Set SOVERSION on shared libraries that lack explicit SONAME 566 # specifier, on *nix systems that are not Darwin. 567 if(UNIX AND NOT APPLE AND NOT ARG_SONAME) 568 set_target_properties(${name} 569 PROPERTIES 570 # Since 4.0.0, the ABI version is indicated by the major version 571 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX} 572 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}) 573 endif() 574 endif() 575 576 if(ARG_MODULE OR ARG_SHARED) 577 # Do not add -Dname_EXPORTS to the command-line when building files in this 578 # target. Doing so is actively harmful for the modules build because it 579 # creates extra module variants, and not useful because we don't use these 580 # macros. 581 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" ) 582 583 if (LLVM_EXPORTED_SYMBOL_FILE) 584 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} ) 585 endif() 586 endif() 587 588 if(ARG_SHARED AND UNIX) 589 if(NOT APPLE AND ARG_SONAME) 590 get_target_property(output_name ${name} OUTPUT_NAME) 591 if(${output_name} STREQUAL "output_name-NOTFOUND") 592 set(output_name ${name}) 593 endif() 594 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}) 595 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX}) 596 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name}) 597 llvm_install_library_symlink(${api_name} ${library_name} SHARED 598 COMPONENT ${name} 599 ALWAYS_GENERATE) 600 llvm_install_library_symlink(${output_name} ${library_name} SHARED 601 COMPONENT ${name} 602 ALWAYS_GENERATE) 603 endif() 604 endif() 605 606 if(ARG_STATIC) 607 set(libtype PUBLIC) 608 else() 609 # We can use PRIVATE since SO knows its dependent libs. 610 set(libtype PRIVATE) 611 endif() 612 613 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN)) 614 # On DLL platforms symbols are imported from the tool by linking against it. 615 set(llvm_libs ${ARG_PLUGIN_TOOL}) 616 elseif (NOT ARG_COMPONENT_LIB) 617 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB) 618 set(llvm_libs LLVM) 619 else() 620 llvm_map_components_to_libnames(llvm_libs 621 ${ARG_LINK_COMPONENTS} 622 ${LLVM_LINK_COMPONENTS} 623 ) 624 endif() 625 else() 626 # Components have not been defined explicitly in CMake, so add the 627 # dependency information for this library through their name, and let 628 # LLVMBuildResolveComponentsLink resolve the mapping. 629 # 630 # It would be nice to verify that we have the dependencies for this library 631 # name, but using get_property(... SET) doesn't suffice to determine if a 632 # property has been set to an empty value. 633 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS}) 634 635 # These two properties are internal properties only used to make sure the 636 # link step applied in LLVMBuildResolveComponentsLink uses the same 637 # properties as the target_link_libraries call below. 638 set_property(TARGET ${name} PROPERTY LLVM_LINK_LIBS ${ARG_LINK_LIBS}) 639 set_property(TARGET ${name} PROPERTY LLVM_LIBTYPE ${libtype}) 640 endif() 641 642 target_link_libraries(${name} ${libtype} 643 ${ARG_LINK_LIBS} 644 ${lib_deps} 645 ${llvm_libs} 646 ) 647 648 if(LLVM_COMMON_DEPENDS) 649 add_dependencies(${name} ${LLVM_COMMON_DEPENDS}) 650 # Add dependencies also to objlibs. 651 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user. 652 foreach(objlib ${objlibs}) 653 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS}) 654 endforeach() 655 endif() 656 657 if(ARG_SHARED OR ARG_MODULE) 658 llvm_externalize_debuginfo(${name}) 659 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH}) 660 endif() 661 # clang and newer versions of ninja use high-resolutions timestamps, 662 # but older versions of libtool on Darwin don't, so the archive will 663 # often get an older timestamp than the last object that was added 664 # or updated. To fix this, we add a custom command to touch archive 665 # after it's been built so that ninja won't rebuild it unnecessarily 666 # the next time it's run. 667 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES) 668 add_custom_command(TARGET ${name} 669 POST_BUILD 670 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX} 671 ) 672 endif() 673endfunction() 674 675function(add_llvm_install_targets target) 676 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN}) 677 if(ARG_COMPONENT) 678 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}") 679 endif() 680 if(ARG_PREFIX) 681 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}") 682 endif() 683 684 set(file_dependencies) 685 set(target_dependencies) 686 foreach(dependency ${ARG_DEPENDS}) 687 if(TARGET ${dependency}) 688 list(APPEND target_dependencies ${dependency}) 689 else() 690 list(APPEND file_dependencies ${dependency}) 691 endif() 692 endforeach() 693 694 add_custom_target(${target} 695 DEPENDS ${file_dependencies} 696 COMMAND "${CMAKE_COMMAND}" 697 ${component_option} 698 ${prefix_option} 699 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" 700 USES_TERMINAL) 701 add_custom_target(${target}-stripped 702 DEPENDS ${file_dependencies} 703 COMMAND "${CMAKE_COMMAND}" 704 ${component_option} 705 ${prefix_option} 706 -DCMAKE_INSTALL_DO_STRIP=1 707 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" 708 USES_TERMINAL) 709 if(target_dependencies) 710 add_dependencies(${target} ${target_dependencies}) 711 add_dependencies(${target}-stripped ${target_dependencies}) 712 endif() 713 714 if(ARG_SYMLINK) 715 add_dependencies(${target} install-${ARG_SYMLINK}) 716 add_dependencies(${target}-stripped install-${ARG_SYMLINK}-stripped) 717 endif() 718endfunction() 719 720# Define special targets that behave like a component group. They don't have any 721# source attached but other components can add themselves to them. If the 722# component supports is a Target and it supports JIT compilation, HAS_JIT must 723# be passed. One can use ADD_TO_COMPONENT option from add_llvm_component_library 724# to link extra component into an existing group. 725function(add_llvm_component_group name) 726 cmake_parse_arguments(ARG "HAS_JIT" "" "LINK_COMPONENTS" ${ARGN}) 727 add_custom_target(${name}) 728 if(ARG_HAS_JIT) 729 set_property(TARGET ${name} PROPERTY COMPONENT_HAS_JIT ON) 730 endif() 731 if(ARG_LINK_COMPONENTS) 732 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS}) 733 endif() 734endfunction() 735 736# An LLVM component is a cmake target with the following cmake properties 737# eventually set: 738# - LLVM_COMPONENT_NAME: the name of the component, which can be the name of 739# the associated library or the one specified through COMPONENT_NAME 740# - LLVM_LINK_COMPONENTS: a list of component this component depends on 741# - COMPONENT_HAS_JIT: (only for group component) whether this target group 742# supports JIT compilation 743# Additionnaly, the ADD_TO_COMPONENT <component> option make it possible to add this 744# component to the LLVM_LINK_COMPONENTS of <component>. 745function(add_llvm_component_library name) 746 cmake_parse_arguments(ARG 747 "" 748 "COMPONENT_NAME;ADD_TO_COMPONENT" 749 "" 750 ${ARGN}) 751 add_llvm_library(${name} COMPONENT_LIB ${ARG_UNPARSED_ARGUMENTS}) 752 string(REGEX REPLACE "^LLVM" "" component_name ${name}) 753 set_property(TARGET ${name} PROPERTY LLVM_COMPONENT_NAME ${component_name}) 754 755 if(ARG_COMPONENT_NAME) 756 set_property(GLOBAL PROPERTY LLVM_COMPONENT_NAME_${ARG_COMPONENT_NAME} ${component_name}) 757 endif() 758 759 if(ARG_ADD_TO_COMPONENT) 760 set_property(TARGET ${ARG_ADD_TO_COMPONENT} APPEND PROPERTY LLVM_LINK_COMPONENTS ${component_name}) 761 endif() 762 763endfunction() 764 765macro(add_llvm_library name) 766 cmake_parse_arguments(ARG 767 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN" 768 "" 769 "" 770 ${ARGN}) 771 if(ARG_MODULE) 772 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS}) 773 elseif( BUILD_SHARED_LIBS OR ARG_SHARED ) 774 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS}) 775 else() 776 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS}) 777 endif() 778 779 # Libraries that are meant to only be exposed via the build tree only are 780 # never installed and are only exported as a target in the special build tree 781 # config file. 782 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE) 783 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} ) 784 set(in_llvm_libs YES) 785 endif() 786 787 if (ARG_MODULE AND NOT TARGET ${name}) 788 # Add empty "phony" target 789 add_custom_target(${name}) 790 elseif( EXCLUDE_FROM_ALL ) 791 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) 792 elseif(ARG_BUILDTREE_ONLY) 793 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name}) 794 else() 795 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN) 796 797 set(export_to_llvmexports) 798 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 799 (in_llvm_libs AND "llvm-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS) OR 800 NOT LLVM_DISTRIBUTION_COMPONENTS) 801 set(export_to_llvmexports EXPORT LLVMExports) 802 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True) 803 endif() 804 805 install(TARGETS ${name} 806 ${export_to_llvmexports} 807 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name} 808 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name} 809 RUNTIME DESTINATION bin COMPONENT ${name}) 810 811 if (NOT LLVM_ENABLE_IDE) 812 add_llvm_install_targets(install-${name} 813 DEPENDS ${name} 814 COMPONENT ${name}) 815 endif() 816 endif() 817 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 818 endif() 819 if (ARG_MODULE) 820 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules") 821 else() 822 set_target_properties(${name} PROPERTIES FOLDER "Libraries") 823 endif() 824endmacro(add_llvm_library name) 825 826macro(add_llvm_executable name) 827 cmake_parse_arguments(ARG 828 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS" 829 "ENTITLEMENTS;BUNDLE_PATH" 830 "DEPENDS" 831 ${ARGN}) 832 833 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ) 834 835 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) 836 837 # Generate objlib 838 if(LLVM_ENABLE_OBJLIB) 839 # Generate an obj library for both targets. 840 set(obj_name "obj.${name}") 841 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL 842 ${ALL_FILES} 843 ) 844 llvm_update_compile_flags(${obj_name}) 845 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>") 846 847 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries") 848 endif() 849 850 add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) 851 852 if(XCODE) 853 # Note: the dummy.cpp source file provides no definitions. However, 854 # it forces Xcode to properly link the static library. 855 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp") 856 endif() 857 858 if( EXCLUDE_FROM_ALL ) 859 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES}) 860 else() 861 add_executable(${name} ${ALL_FILES}) 862 endif() 863 864 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS}) 865 866 if(NOT ARG_NO_INSTALL_RPATH) 867 llvm_setup_rpath(${name}) 868 elseif (LLVM_LOCAL_RPATH) 869 set_target_properties(${name} PROPERTIES 870 BUILD_WITH_INSTALL_RPATH On 871 INSTALL_RPATH "${LLVM_LOCAL_RPATH}") 872 endif() 873 874 if(DEFINED windows_resource_file) 875 set_windows_version_resource_properties(${name} ${windows_resource_file}) 876 endif() 877 878 # $<TARGET_OBJECTS> doesn't require compile flags. 879 if(NOT LLVM_ENABLE_OBJLIB) 880 llvm_update_compile_flags(${name}) 881 endif() 882 883 if (ARG_SUPPORT_PLUGINS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX") 884 set(LLVM_NO_DEAD_STRIP On) 885 endif() 886 887 add_link_opts( ${name} ) 888 889 # Do not add -Dname_EXPORTS to the command-line when building files in this 890 # target. Doing so is actively harmful for the modules build because it 891 # creates extra module variants, and not useful because we don't use these 892 # macros. 893 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" ) 894 895 if (LLVM_EXPORTED_SYMBOL_FILE) 896 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} ) 897 endif(LLVM_EXPORTED_SYMBOL_FILE) 898 899 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB) 900 set(USE_SHARED USE_SHARED) 901 endif() 902 903 set(EXCLUDE_FROM_ALL OFF) 904 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 905 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} ) 906 if( LLVM_COMMON_DEPENDS ) 907 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) 908 endif( LLVM_COMMON_DEPENDS ) 909 910 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO) 911 llvm_externalize_debuginfo(${name}) 912 endif() 913 if (LLVM_PTHREAD_LIB) 914 # libpthreads overrides some standard library symbols, so main 915 # executable must be linked with it in order to provide consistent 916 # API for all shared libaries loaded by this executable. 917 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB}) 918 endif() 919 920 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH}) 921endmacro(add_llvm_executable name) 922 923# add_llvm_pass_plugin(name [NO_MODULE] ...) 924# Add ${name} as an llvm plugin. 925# If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically. 926# Otherwise a pluggable shared library is registered. 927# 928# If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF, 929# only an object library is built, and no module is built. This is specific to the Polly use case. 930# 931# The SUBPROJECT argument contains the LLVM project the plugin belongs 932# to. If set, the plugin will link statically by default it if the 933# project was enabled. 934function(add_llvm_pass_plugin name) 935 cmake_parse_arguments(ARG 936 "NO_MODULE" "SUBPROJECT" "" 937 ${ARGN}) 938 939 string(TOUPPER ${name} name_upper) 940 941 # Enable the plugin by default if it was explicitly enabled by the user. 942 # Note: If was set to "all", LLVM's CMakeLists.txt replaces it with a 943 # list of all projects, counting as explicitly enabled. 944 set(link_into_tools_default OFF) 945 if (ARG_SUBPROJECT AND LLVM_TOOL_${name_upper}_BUILD) 946 set(link_into_tools_default ON) 947 endif() 948 option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" ${link_into_tools_default}) 949 950 # If we statically link the plugin, don't use llvm dylib because we're going 951 # to be part of it. 952 if(LLVM_${name_upper}_LINK_INTO_TOOLS) 953 list(APPEND ARG_UNPARSED_ARGUMENTS DISABLE_LLVM_LINK_LLVM_DYLIB) 954 endif() 955 956 if(LLVM_${name_upper}_LINK_INTO_TOOLS) 957 list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY) 958 # process_llvm_pass_plugins takes care of the actual linking, just create an 959 # object library as of now 960 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS}) 961 target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS) 962 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS) 963 if (TARGET intrinsics_gen) 964 add_dependencies(obj.${name} intrinsics_gen) 965 endif() 966 if (TARGET omp_gen) 967 add_dependencies(obj.${name} omp_gen) 968 endif() 969 if (TARGET acc_gen) 970 add_dependencies(obj.${name} acc_gen) 971 endif() 972 set_property(GLOBAL APPEND PROPERTY LLVM_STATIC_EXTENSIONS ${name}) 973 elseif(NOT ARG_NO_MODULE) 974 add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS}) 975 else() 976 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS}) 977 endif() 978 message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})") 979 980endfunction(add_llvm_pass_plugin) 981 982# process_llvm_pass_plugins([GEN_CONFIG]) 983# 984# Correctly set lib dependencies between plugins and tools, based on tools 985# registered with the ENABLE_PLUGINS option. 986# 987# if GEN_CONFIG option is set, also generate X Macro file for extension 988# handling. It provides a HANDLE_EXTENSION(extension_namespace, ExtensionProject) 989# call for each extension allowing client code to define 990# HANDLE_EXTENSION to have a specific code be run for each extension. 991# 992function(process_llvm_pass_plugins) 993 cmake_parse_arguments(ARG 994 "GEN_CONFIG" "" "" 995 ${ARGN}) 996 997 if(ARG_GEN_CONFIG) 998 get_property(LLVM_STATIC_EXTENSIONS GLOBAL PROPERTY LLVM_STATIC_EXTENSIONS) 999 else() 1000 include(LLVMConfigExtensions) 1001 endif() 1002 1003 # Add static plugins to the Extension component 1004 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1005 set_property(TARGET LLVMExtensions APPEND PROPERTY LINK_LIBRARIES ${llvm_extension}) 1006 set_property(TARGET LLVMExtensions APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension}) 1007 endforeach() 1008 1009 # Eventually generate the extension headers, and store config to a cmake file 1010 # for usage in third-party configuration. 1011 if(ARG_GEN_CONFIG) 1012 1013 ## Part 1: Extension header to be included whenever we need extension 1014 # processing. 1015 set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) 1016 set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") 1017 file(WRITE 1018 "${llvm_cmake_builddir}/LLVMConfigExtensions.cmake" 1019 "set(LLVM_STATIC_EXTENSIONS ${LLVM_STATIC_EXTENSIONS})") 1020 install(FILES 1021 ${llvm_cmake_builddir}/LLVMConfigExtensions.cmake 1022 DESTINATION ${LLVM_INSTALL_PACKAGE_DIR} 1023 COMPONENT cmake-exports) 1024 1025 set(ExtensionDef "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def") 1026 file(WRITE "${ExtensionDef}.tmp" "//extension handlers\n") 1027 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1028 file(APPEND "${ExtensionDef}.tmp" "HANDLE_EXTENSION(${llvm_extension})\n") 1029 endforeach() 1030 file(APPEND "${ExtensionDef}.tmp" "#undef HANDLE_EXTENSION\n") 1031 1032 # only replace if there's an actual change 1033 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different 1034 "${ExtensionDef}.tmp" 1035 "${ExtensionDef}") 1036 file(REMOVE "${ExtensionDef}.tmp") 1037 1038 ## Part 2: Extension header that captures each extension dependency, to be 1039 # used by llvm-config. 1040 set(ExtensionDeps "${LLVM_BINARY_DIR}/tools/llvm-config/ExtensionDependencies.inc") 1041 1042 # Max needed to correctly size the required library array. 1043 set(llvm_plugin_max_deps_length 0) 1044 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1045 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES) 1046 list(LENGTH llvm_plugin_deps llvm_plugin_deps_length) 1047 if(llvm_plugin_deps_length GREATER llvm_plugin_max_deps_length) 1048 set(llvm_plugin_max_deps_length ${llvm_plugin_deps_length}) 1049 endif() 1050 endforeach() 1051 1052 list(LENGTH LLVM_STATIC_EXTENSIONS llvm_static_extension_count) 1053 file(WRITE 1054 "${ExtensionDeps}.tmp" 1055 "#include <array>\n\ 1056 struct ExtensionDescriptor {\n\ 1057 const char* Name;\n\ 1058 const char* RequiredLibraries[1 + 1 + ${llvm_plugin_max_deps_length}];\n\ 1059 };\n\ 1060 std::array<ExtensionDescriptor, ${llvm_static_extension_count}> AvailableExtensions{\n") 1061 1062 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1063 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES) 1064 1065 file(APPEND "${ExtensionDeps}.tmp" "ExtensionDescriptor{\"${llvm_extension}\", {") 1066 foreach(llvm_plugin_dep ${llvm_plugin_deps}) 1067 # Turn library dependency back to component name, if possible. 1068 # That way llvm-config can avoid redundant dependencies. 1069 STRING(REGEX REPLACE "^-l" "" plugin_dep_name ${llvm_plugin_dep}) 1070 STRING(REGEX MATCH "^LLVM" is_llvm_library ${plugin_dep_name}) 1071 if(is_llvm_library) 1072 STRING(REGEX REPLACE "^LLVM" "" plugin_dep_name ${plugin_dep_name}) 1073 STRING(TOLOWER ${plugin_dep_name} plugin_dep_name) 1074 endif() 1075 file(APPEND "${ExtensionDeps}.tmp" "\"${plugin_dep_name}\", ") 1076 endforeach() 1077 1078 # Self + mandatory trailing null, because the number of RequiredLibraries differs between extensions. 1079 file(APPEND "${ExtensionDeps}.tmp" \"${llvm_extension}\", "nullptr}},\n") 1080 endforeach() 1081 file(APPEND "${ExtensionDeps}.tmp" "};\n") 1082 1083 # only replace if there's an actual change 1084 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different 1085 "${ExtensionDeps}.tmp" 1086 "${ExtensionDeps}") 1087 file(REMOVE "${ExtensionDeps}.tmp") 1088 endif() 1089endfunction() 1090 1091function(export_executable_symbols target) 1092 if (LLVM_EXPORTED_SYMBOL_FILE) 1093 # The symbol file should contain the symbols we want the executable to 1094 # export 1095 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 1096 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 1097 # Extract the symbols to export from the static libraries that the 1098 # executable links against. 1099 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 1100 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols) 1101 # We need to consider not just the direct link dependencies, but also the 1102 # transitive link dependencies. Do this by starting with the set of direct 1103 # dependencies, then the dependencies of those dependencies, and so on. 1104 get_target_property(new_libs ${target} LINK_LIBRARIES) 1105 set(link_libs ${new_libs}) 1106 while(NOT "${new_libs}" STREQUAL "") 1107 foreach(lib ${new_libs}) 1108 if(TARGET ${lib}) 1109 get_target_property(lib_type ${lib} TYPE) 1110 if("${lib_type}" STREQUAL "STATIC_LIBRARY") 1111 list(APPEND static_libs ${lib}) 1112 else() 1113 list(APPEND other_libs ${lib}) 1114 endif() 1115 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES) 1116 foreach(transitive_lib ${transitive_libs}) 1117 list(FIND link_libs ${transitive_lib} idx) 1118 if(TARGET ${transitive_lib} AND idx EQUAL -1) 1119 list(APPEND newer_libs ${transitive_lib}) 1120 list(APPEND link_libs ${transitive_lib}) 1121 endif() 1122 endforeach(transitive_lib) 1123 endif() 1124 endforeach(lib) 1125 set(new_libs ${newer_libs}) 1126 set(newer_libs "") 1127 endwhile() 1128 list(REMOVE_DUPLICATES static_libs) 1129 if (MSVC) 1130 set(mangling microsoft) 1131 else() 1132 set(mangling itanium) 1133 endif() 1134 add_custom_command(OUTPUT ${exported_symbol_file} 1135 COMMAND "${Python3_EXECUTABLE}" ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file} 1136 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR} 1137 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs} 1138 VERBATIM 1139 COMMENT "Generating export list for ${target}") 1140 add_llvm_symbol_exports( ${target} ${exported_symbol_file} ) 1141 # If something links against this executable then we want a 1142 # transitive link against only the libraries whose symbols 1143 # we aren't exporting. 1144 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}") 1145 # The default import library suffix that cmake uses for cygwin/mingw is 1146 # ".dll.a", but for clang.exe that causes a collision with libclang.dll, 1147 # where the import libraries of both get named libclang.dll.a. Use a suffix 1148 # of ".exe.a" to avoid this. 1149 if(CYGWIN OR MINGW) 1150 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a") 1151 endif() 1152 elseif(NOT (WIN32 OR CYGWIN)) 1153 # On Windows auto-exporting everything doesn't work because of the limit on 1154 # the size of the exported symbol table, but on other platforms we can do 1155 # it without any trouble. 1156 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 1157 if (APPLE) 1158 set_property(TARGET ${target} APPEND_STRING PROPERTY 1159 LINK_FLAGS " -rdynamic") 1160 endif() 1161 endif() 1162endfunction() 1163 1164# Export symbols if LLVM plugins are enabled. 1165function(export_executable_symbols_for_plugins target) 1166 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 1167 export_executable_symbols(${target}) 1168 endif() 1169endfunction() 1170 1171if(NOT LLVM_TOOLCHAIN_TOOLS) 1172 set (LLVM_TOOLCHAIN_TOOLS 1173 llvm-ar 1174 llvm-cov 1175 llvm-cxxfilt 1176 llvm-ranlib 1177 llvm-lib 1178 llvm-nm 1179 llvm-objcopy 1180 llvm-objdump 1181 llvm-rc 1182 llvm-size 1183 llvm-strings 1184 llvm-strip 1185 llvm-profdata 1186 llvm-symbolizer 1187 # symlink version of some of above tools that are enabled by 1188 # LLVM_INSTALL_BINUTILS_SYMLINKS. 1189 addr2line 1190 ar 1191 c++filt 1192 ranlib 1193 nm 1194 objcopy 1195 objdump 1196 size 1197 strings 1198 strip 1199 ) 1200endif() 1201 1202macro(add_llvm_tool name) 1203 if( NOT LLVM_BUILD_TOOLS ) 1204 set(EXCLUDE_FROM_ALL ON) 1205 endif() 1206 add_llvm_executable(${name} ${ARGN}) 1207 1208 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 1209 if( LLVM_BUILD_TOOLS ) 1210 set(export_to_llvmexports) 1211 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 1212 NOT LLVM_DISTRIBUTION_COMPONENTS) 1213 set(export_to_llvmexports EXPORT LLVMExports) 1214 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True) 1215 endif() 1216 1217 install(TARGETS ${name} 1218 ${export_to_llvmexports} 1219 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR} 1220 COMPONENT ${name}) 1221 1222 if (NOT LLVM_ENABLE_IDE) 1223 add_llvm_install_targets(install-${name} 1224 DEPENDS ${name} 1225 COMPONENT ${name}) 1226 endif() 1227 endif() 1228 endif() 1229 if( LLVM_BUILD_TOOLS ) 1230 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 1231 endif() 1232 set_target_properties(${name} PROPERTIES FOLDER "Tools") 1233endmacro(add_llvm_tool name) 1234 1235 1236macro(add_llvm_example name) 1237 if( NOT LLVM_BUILD_EXAMPLES ) 1238 set(EXCLUDE_FROM_ALL ON) 1239 endif() 1240 add_llvm_executable(${name} ${ARGN}) 1241 if( LLVM_BUILD_EXAMPLES ) 1242 install(TARGETS ${name} RUNTIME DESTINATION examples) 1243 endif() 1244 set_target_properties(${name} PROPERTIES FOLDER "Examples") 1245endmacro(add_llvm_example name) 1246 1247macro(add_llvm_example_library name) 1248 if( NOT LLVM_BUILD_EXAMPLES ) 1249 set(EXCLUDE_FROM_ALL ON) 1250 add_llvm_library(${name} BUILDTREE_ONLY ${ARGN}) 1251 else() 1252 add_llvm_library(${name} ${ARGN}) 1253 endif() 1254 1255 set_target_properties(${name} PROPERTIES FOLDER "Examples") 1256endmacro(add_llvm_example_library name) 1257 1258# This is a macro that is used to create targets for executables that are needed 1259# for development, but that are not intended to be installed by default. 1260macro(add_llvm_utility name) 1261 if ( NOT LLVM_BUILD_UTILS ) 1262 set(EXCLUDE_FROM_ALL ON) 1263 endif() 1264 1265 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) 1266 set_target_properties(${name} PROPERTIES FOLDER "Utils") 1267 if ( ${name} IN_LIST LLVM_TOOLCHAIN_UTILITIES OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 1268 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS) 1269 set(export_to_llvmexports) 1270 if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 1271 NOT LLVM_DISTRIBUTION_COMPONENTS) 1272 set(export_to_llvmexports EXPORT LLVMExports) 1273 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True) 1274 endif() 1275 1276 install(TARGETS ${name} 1277 ${export_to_llvmexports} 1278 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR} 1279 COMPONENT ${name}) 1280 1281 if (NOT LLVM_ENABLE_IDE) 1282 add_llvm_install_targets(install-${name} 1283 DEPENDS ${name} 1284 COMPONENT ${name}) 1285 endif() 1286 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 1287 elseif(LLVM_BUILD_UTILS) 1288 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name}) 1289 endif() 1290 endif() 1291endmacro(add_llvm_utility name) 1292 1293macro(add_llvm_fuzzer name) 1294 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN}) 1295 if( LLVM_LIB_FUZZING_ENGINE ) 1296 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN}) 1297 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) 1298 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE}) 1299 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 1300 elseif( LLVM_USE_SANITIZE_COVERAGE ) 1301 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer") 1302 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN}) 1303 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) 1304 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 1305 elseif( ARG_DUMMY_MAIN ) 1306 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS}) 1307 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 1308 endif() 1309endmacro() 1310 1311macro(add_llvm_target target_name) 1312 include_directories(BEFORE 1313 ${CMAKE_CURRENT_BINARY_DIR} 1314 ${CMAKE_CURRENT_SOURCE_DIR}) 1315 add_llvm_component_library(LLVM${target_name} ${ARGN}) 1316 set( CURRENT_LLVM_TARGET LLVM${target_name} ) 1317endmacro(add_llvm_target) 1318 1319function(canonicalize_tool_name name output) 1320 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name}) 1321 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip}) 1322 string(TOUPPER ${nameUNDERSCORE} nameUPPER) 1323 set(${output} "${nameUPPER}" PARENT_SCOPE) 1324endfunction(canonicalize_tool_name) 1325 1326# Custom add_subdirectory wrapper 1327# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional 1328# path if it differs from the name. 1329function(add_llvm_subdirectory project type name) 1330 set(add_llvm_external_dir "${ARGN}") 1331 if("${add_llvm_external_dir}" STREQUAL "") 1332 set(add_llvm_external_dir ${name}) 1333 endif() 1334 canonicalize_tool_name(${name} nameUPPER) 1335 set(canonical_full_name ${project}_${type}_${nameUPPER}) 1336 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED) 1337 if(already_processed) 1338 return() 1339 endif() 1340 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES) 1341 1342 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt) 1343 # Treat it as in-tree subproject. 1344 option(${canonical_full_name}_BUILD 1345 "Whether to build ${name} as part of ${project}" On) 1346 mark_as_advanced(${project}_${type}_${name}_BUILD) 1347 if(${canonical_full_name}_BUILD) 1348 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir}) 1349 endif() 1350 else() 1351 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR 1352 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" 1353 CACHE PATH "Path to ${name} source directory") 1354 set(${canonical_full_name}_BUILD_DEFAULT ON) 1355 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}) 1356 set(${canonical_full_name}_BUILD_DEFAULT OFF) 1357 endif() 1358 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF") 1359 set(${canonical_full_name}_BUILD_DEFAULT OFF) 1360 endif() 1361 option(${canonical_full_name}_BUILD 1362 "Whether to build ${name} as part of LLVM" 1363 ${${canonical_full_name}_BUILD_DEFAULT}) 1364 if (${canonical_full_name}_BUILD) 1365 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}) 1366 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir}) 1367 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "") 1368 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}") 1369 endif() 1370 endif() 1371 endif() 1372endfunction() 1373 1374# Add external project that may want to be built as part of llvm such as Clang, 1375# lld, and Polly. This adds two options. One for the source directory of the 1376# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to 1377# enable or disable building it with everything else. 1378# Additional parameter can be specified as the name of directory. 1379macro(add_llvm_external_project name) 1380 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN}) 1381endmacro() 1382 1383macro(add_llvm_tool_subdirectory name) 1384 add_llvm_external_project(${name}) 1385endmacro(add_llvm_tool_subdirectory) 1386 1387function(get_project_name_from_src_var var output) 1388 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR" 1389 MACHED_TOOL "${var}") 1390 if(MACHED_TOOL) 1391 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE) 1392 else() 1393 set(${output} PARENT_SCOPE) 1394 endif() 1395endfunction() 1396 1397function(create_subdirectory_options project type) 1398 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") 1399 foreach(dir ${sub-dirs}) 1400 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt") 1401 canonicalize_tool_name(${dir} name) 1402 option(${project}_${type}_${name}_BUILD 1403 "Whether to build ${name} as part of ${project}" On) 1404 mark_as_advanced(${project}_${type}_${name}_BUILD) 1405 endif() 1406 endforeach() 1407endfunction(create_subdirectory_options) 1408 1409function(create_llvm_tool_options) 1410 create_subdirectory_options(LLVM TOOL) 1411endfunction(create_llvm_tool_options) 1412 1413function(llvm_add_implicit_projects project) 1414 set(list_of_implicit_subdirs "") 1415 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") 1416 foreach(dir ${sub-dirs}) 1417 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt") 1418 canonicalize_tool_name(${dir} name) 1419 if (${project}_TOOL_${name}_BUILD) 1420 get_filename_component(fn "${dir}" NAME) 1421 list(APPEND list_of_implicit_subdirs "${fn}") 1422 endif() 1423 endif() 1424 endforeach() 1425 1426 foreach(external_proj ${list_of_implicit_subdirs}) 1427 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN}) 1428 endforeach() 1429endfunction(llvm_add_implicit_projects) 1430 1431function(add_llvm_implicit_projects) 1432 llvm_add_implicit_projects(LLVM) 1433endfunction(add_llvm_implicit_projects) 1434 1435# Generic support for adding a unittest. 1436function(add_unittest test_suite test_name) 1437 if( NOT LLVM_BUILD_TESTS ) 1438 set(EXCLUDE_FROM_ALL ON) 1439 endif() 1440 1441 if (SUPPORTS_VARIADIC_MACROS_FLAG) 1442 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros") 1443 endif () 1444 # Some parts of gtest rely on this GNU extension, don't warn on it. 1445 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG) 1446 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments") 1447 endif() 1448 1449 set(LLVM_REQUIRES_RTTI OFF) 1450 1451 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream 1452 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) 1453 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) 1454 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) 1455 # libpthreads overrides some standard library symbols, so main 1456 # executable must be linked with it in order to provide consistent 1457 # API for all shared libaries loaded by this executable. 1458 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB}) 1459 1460 add_dependencies(${test_suite} ${test_name}) 1461 get_target_property(test_suite_folder ${test_suite} FOLDER) 1462 if (test_suite_folder) 1463 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}") 1464 endif () 1465endfunction() 1466 1467# Use for test binaries that call llvm::getInputFileDirectory(). Use of this 1468# is discouraged. 1469function(add_unittest_with_input_files test_suite test_name) 1470 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 1471 configure_file( 1472 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in 1473 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt) 1474 1475 add_unittest(${test_suite} ${test_name} ${ARGN}) 1476endfunction() 1477 1478# Generic support for adding a benchmark. 1479function(add_benchmark benchmark_name) 1480 if( NOT LLVM_BUILD_BENCHMARKS ) 1481 set(EXCLUDE_FROM_ALL ON) 1482 endif() 1483 1484 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) 1485 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) 1486 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) 1487 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils") 1488 target_link_libraries(${benchmark_name} PRIVATE benchmark) 1489endfunction() 1490 1491# This function canonicalize the CMake variables passed by names 1492# from CMake boolean to 0/1 suitable for passing into Python or C++, 1493# in place. 1494function(llvm_canonicalize_cmake_booleans) 1495 foreach(var ${ARGN}) 1496 if(${var}) 1497 set(${var} 1 PARENT_SCOPE) 1498 else() 1499 set(${var} 0 PARENT_SCOPE) 1500 endif() 1501 endforeach() 1502endfunction(llvm_canonicalize_cmake_booleans) 1503 1504macro(set_llvm_build_mode) 1505 # Configuration-time: See Unit/lit.site.cfg.in 1506 if (CMAKE_CFG_INTDIR STREQUAL ".") 1507 set(LLVM_BUILD_MODE ".") 1508 else () 1509 set(LLVM_BUILD_MODE "%(build_mode)s") 1510 endif () 1511endmacro() 1512 1513# Takes a list of path names in pathlist and a base directory, and returns 1514# a list of paths relative to the base directory in out_pathlist. 1515# Paths that are on a different drive than the basedir (on Windows) or that 1516# contain symlinks are returned absolute. 1517# Use with LLVM_LIT_PATH_FUNCTION below. 1518function(make_paths_relative out_pathlist basedir pathlist) 1519 # Passing ARG_PATH_VALUES as-is to execute_process() makes cmake strip 1520 # empty list entries. So escape the ;s in the list and do the splitting 1521 # ourselves. cmake has no relpath function, so use Python for that. 1522 string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}") 1523 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "\n 1524import os, sys\n 1525base = sys.argv[1] 1526def haslink(p):\n 1527 if not p or p == os.path.dirname(p): return False\n 1528 return os.path.islink(p) or haslink(os.path.dirname(p))\n 1529def relpath(p):\n 1530 if not p: return ''\n 1531 if os.path.splitdrive(p)[0] != os.path.splitdrive(base)[0]: return p\n 1532 if haslink(p) or haslink(base): return p\n 1533 return os.path.relpath(p, base)\n 1534if len(sys.argv) < 3: sys.exit(0)\n 1535sys.stdout.write(';'.join(relpath(p) for p in sys.argv[2].split(';')))" 1536 ${basedir} 1537 ${pathlist_escaped} 1538 OUTPUT_VARIABLE pathlist_relative 1539 ERROR_VARIABLE error 1540 RESULT_VARIABLE result) 1541 if (NOT result EQUAL 0) 1542 message(FATAL_ERROR "make_paths_relative() failed due to error '${result}', with stderr\n${error}") 1543 endif() 1544 set(${out_pathlist} "${pathlist_relative}" PARENT_SCOPE) 1545endfunction() 1546 1547# Converts a file that's relative to the current python file to an absolute 1548# path. Since this uses __file__, it has to be emitted into python files that 1549# use it and can't be in a lit module. Use with make_paths_relative(). 1550string(CONCAT LLVM_LIT_PATH_FUNCTION 1551 "# Allow generated file to be relocatable.\n" 1552 "def path(p):\n" 1553 " if not p: return ''\n" 1554 " return os.path.join(os.path.dirname(os.path.abspath(__file__)), p)\n" 1555 ) 1556 1557# This function provides an automatic way to 'configure'-like generate a file 1558# based on a set of common and custom variables, specifically targeting the 1559# variables needed for the 'lit.site.cfg' files. This function bundles the 1560# common variables that any Lit instance is likely to need, and custom 1561# variables can be passed in. 1562# The keyword PATHS is followed by a list of cmake variable names that are 1563# mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that 1564# list are treated as paths that are relative to the directory the generated 1565# lit.cfg.py file is in, and the `path()` function converts the relative 1566# path back to absolute form. This makes it possible to move a build directory 1567# containing lit.cfg.py files from one machine to another. 1568function(configure_lit_site_cfg site_in site_out) 1569 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING;PATHS" ${ARGN}) 1570 1571 if ("${ARG_MAIN_CONFIG}" STREQUAL "") 1572 get_filename_component(INPUT_DIR ${site_in} DIRECTORY) 1573 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg") 1574 endif() 1575 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "") 1576 set(ARG_OUTPUT_MAPPING "${site_out}") 1577 endif() 1578 1579 foreach(c ${LLVM_TARGETS_TO_BUILD}) 1580 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}") 1581 endforeach(c) 1582 set(TARGETS_TO_BUILD ${TARGETS_BUILT}) 1583 1584 set(SHLIBEXT "${LTDL_SHLIB_EXT}") 1585 1586 set_llvm_build_mode() 1587 1588 # They below might not be the build tree but provided binary tree. 1589 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR}) 1590 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR}) 1591 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}") 1592 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}") 1593 1594 # SHLIBDIR points the build tree. 1595 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}") 1596 1597 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for 1598 # plugins. We may rename it. 1599 if(LLVM_ENABLE_PLUGINS) 1600 set(ENABLE_SHARED "1") 1601 else() 1602 set(ENABLE_SHARED "0") 1603 endif() 1604 1605 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE) 1606 set(ENABLE_ASSERTIONS "1") 1607 else() 1608 set(ENABLE_ASSERTIONS "0") 1609 endif() 1610 1611 set(HOST_OS ${CMAKE_SYSTEM_NAME}) 1612 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR}) 1613 1614 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}") 1615 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}") 1616 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}") 1617 1618 string(CONCAT LIT_SITE_CFG_IN_HEADER 1619 "# Autogenerated from ${site_in}\n# Do not edit!\n\n" 1620 "${LLVM_LIT_PATH_FUNCTION}" 1621 ) 1622 1623 # Override config_target_triple (and the env) 1624 if(LLVM_TARGET_TRIPLE_ENV) 1625 # This is expanded into the heading. 1626 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}" 1627 "import os\n" 1628 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n" 1629 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n" 1630 ) 1631 1632 # This is expanded to; config.target_triple = ""+config.target_triple+"" 1633 set(TARGET_TRIPLE "\"+config.target_triple+\"") 1634 endif() 1635 1636 if (ARG_PATHS) 1637 # Walk ARG_PATHS and collect the current value of the variables in there. 1638 # list(APPEND) ignores empty elements exactly if the list is empty, 1639 # so start the list with a dummy element and drop it, to make sure that 1640 # even empty values make it into the values list. 1641 set(ARG_PATH_VALUES "dummy") 1642 foreach(path ${ARG_PATHS}) 1643 list(APPEND ARG_PATH_VALUES "${${path}}") 1644 endforeach() 1645 list(REMOVE_AT ARG_PATH_VALUES 0) 1646 1647 get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY) 1648 make_paths_relative( 1649 ARG_PATH_VALUES_RELATIVE "${OUTPUT_DIR}" "${ARG_PATH_VALUES}") 1650 1651 list(LENGTH ARG_PATHS len_paths) 1652 list(LENGTH ARG_PATH_VALUES len_path_values) 1653 list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels) 1654 if ((NOT ${len_paths} EQUAL ${len_path_values}) OR 1655 (NOT ${len_paths} EQUAL ${len_path_value_rels})) 1656 message(SEND_ERROR "PATHS lengths got confused") 1657 endif() 1658 1659 # Transform variables mentioned in ARG_PATHS to relative paths for 1660 # the configure_file() call. Variables are copied to subscopeds by cmake, 1661 # so this only modifies the local copy of the variables. 1662 math(EXPR arg_path_limit "${len_paths} - 1") 1663 foreach(i RANGE ${arg_path_limit}) 1664 list(GET ARG_PATHS ${i} val1) 1665 list(GET ARG_PATH_VALUES_RELATIVE ${i} val2) 1666 set(${val1} ${val2}) 1667 endforeach() 1668 endif() 1669 1670 configure_file(${site_in} ${site_out} @ONLY) 1671 1672 if (EXISTS "${ARG_MAIN_CONFIG}") 1673 # Remember main config / generated site config for llvm-lit.in. 1674 get_property(LLVM_LIT_CONFIG_FILES GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES) 1675 list(APPEND LLVM_LIT_CONFIG_FILES "${ARG_MAIN_CONFIG}" "${site_out}") 1676 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES ${LLVM_LIT_CONFIG_FILES}) 1677 endif() 1678endfunction() 1679 1680function(dump_all_cmake_variables) 1681 get_cmake_property(_variableNames VARIABLES) 1682 foreach (_variableName ${_variableNames}) 1683 message(STATUS "${_variableName}=${${_variableName}}") 1684 endforeach() 1685endfunction() 1686 1687function(get_llvm_lit_path base_dir file_name) 1688 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN}) 1689 1690 if (ARG_ALLOW_EXTERNAL) 1691 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit") 1692 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "") 1693 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}") 1694 endif() 1695 1696 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "") 1697 if (EXISTS ${LLVM_EXTERNAL_LIT}) 1698 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME) 1699 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY) 1700 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE) 1701 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE) 1702 return() 1703 elseif (NOT DEFINED CACHE{LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE}) 1704 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.") 1705 set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "") 1706 endif() 1707 endif() 1708 endif() 1709 1710 set(lit_file_name "llvm-lit") 1711 if (CMAKE_HOST_WIN32 AND NOT CYGWIN) 1712 # llvm-lit needs suffix.py for multiprocess to find a main module. 1713 set(lit_file_name "${lit_file_name}.py") 1714 endif () 1715 set(${file_name} ${lit_file_name} PARENT_SCOPE) 1716 1717 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR) 1718 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "") 1719 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE) 1720 endif() 1721 1722 # Allow individual projects to provide an override 1723 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "") 1724 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR}) 1725 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "") 1726 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR}) 1727 else() 1728 set(LLVM_LIT_BASE_DIR "") 1729 endif() 1730 1731 # Cache this so we don't have to do it again and have subsequent calls 1732 # potentially disagree on the value. 1733 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR}) 1734 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE) 1735endfunction() 1736 1737# A raw function to create a lit target. This is used to implement the testuite 1738# management functions. 1739function(add_lit_target target comment) 1740 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1741 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}") 1742 separate_arguments(LIT_ARGS) 1743 if (NOT CMAKE_CFG_INTDIR STREQUAL ".") 1744 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR}) 1745 endif () 1746 1747 # Get the path to the lit to *run* tests with. This can be overriden by 1748 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py> 1749 get_llvm_lit_path( 1750 lit_base_dir 1751 lit_file_name 1752 ALLOW_EXTERNAL 1753 ) 1754 1755 set(LIT_COMMAND "${Python3_EXECUTABLE};${lit_base_dir}/${lit_file_name}") 1756 list(APPEND LIT_COMMAND ${LIT_ARGS}) 1757 foreach(param ${ARG_PARAMS}) 1758 list(APPEND LIT_COMMAND --param ${param}) 1759 endforeach() 1760 if (ARG_UNPARSED_ARGUMENTS) 1761 add_custom_target(${target} 1762 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS} 1763 COMMENT "${comment}" 1764 USES_TERMINAL 1765 ) 1766 else() 1767 add_custom_target(${target} 1768 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.") 1769 message(STATUS "${target} does nothing.") 1770 endif() 1771 1772 if (ARG_DEPENDS) 1773 add_dependencies(${target} ${ARG_DEPENDS}) 1774 endif() 1775 1776 # Tests should be excluded from "Build Solution". 1777 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON) 1778endfunction() 1779 1780# A function to add a set of lit test suites to be driven through 'check-*' targets. 1781function(add_lit_testsuite target comment) 1782 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1783 1784 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all. 1785 if(NOT ARG_EXCLUDE_FROM_CHECK_ALL) 1786 # Register the testsuites, params and depends for the global check rule. 1787 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS}) 1788 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS}) 1789 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS}) 1790 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS}) 1791 endif() 1792 1793 # Produce a specific suffixed check rule. 1794 add_lit_target(${target} ${comment} 1795 ${ARG_UNPARSED_ARGUMENTS} 1796 PARAMS ${ARG_PARAMS} 1797 DEPENDS ${ARG_DEPENDS} 1798 ARGS ${ARG_ARGS} 1799 ) 1800endfunction() 1801 1802function(add_lit_testsuites project directory) 1803 if (NOT LLVM_ENABLE_IDE) 1804 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1805 1806 # Search recursively for test directories by assuming anything not 1807 # in a directory called Inputs contains tests. 1808 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*) 1809 foreach(lit_suite ${to_process}) 1810 if(NOT IS_DIRECTORY ${lit_suite}) 1811 continue() 1812 endif() 1813 string(FIND ${lit_suite} Inputs is_inputs) 1814 string(FIND ${lit_suite} Output is_output) 1815 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1)) 1816 continue() 1817 endif() 1818 1819 # Create a check- target for the directory. 1820 string(REPLACE ${directory} "" name_slash ${lit_suite}) 1821 if (name_slash) 1822 string(REPLACE "/" "-" name_slash ${name_slash}) 1823 string(REPLACE "\\" "-" name_dashes ${name_slash}) 1824 string(TOLOWER "${project}${name_dashes}" name_var) 1825 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}" 1826 ${lit_suite} 1827 ${EXCLUDE_FROM_CHECK_ALL} 1828 PARAMS ${ARG_PARAMS} 1829 DEPENDS ${ARG_DEPENDS} 1830 ARGS ${ARG_ARGS} 1831 ) 1832 endif() 1833 endforeach() 1834 endif() 1835endfunction() 1836 1837function(llvm_install_library_symlink name dest type) 1838 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN}) 1839 foreach(path ${CMAKE_MODULE_PATH}) 1840 if(EXISTS ${path}/LLVMInstallSymlink.cmake) 1841 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake) 1842 break() 1843 endif() 1844 endforeach() 1845 1846 set(component ${ARG_COMPONENT}) 1847 if(NOT component) 1848 set(component ${name}) 1849 endif() 1850 1851 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX}) 1852 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX}) 1853 1854 set(output_dir lib${LLVM_LIBDIR_SUFFIX}) 1855 if(WIN32 AND "${type}" STREQUAL "SHARED") 1856 set(output_dir bin) 1857 endif() 1858 1859 install(SCRIPT ${INSTALL_SYMLINK} 1860 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})" 1861 COMPONENT ${component}) 1862 1863 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE) 1864 add_llvm_install_targets(install-${name} 1865 DEPENDS ${name} ${dest} 1866 COMPONENT ${name} 1867 SYMLINK ${dest}) 1868 endif() 1869endfunction() 1870 1871function(llvm_install_symlink name dest) 1872 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN}) 1873 foreach(path ${CMAKE_MODULE_PATH}) 1874 if(EXISTS ${path}/LLVMInstallSymlink.cmake) 1875 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake) 1876 break() 1877 endif() 1878 endforeach() 1879 1880 if(ARG_COMPONENT) 1881 set(component ${ARG_COMPONENT}) 1882 else() 1883 if(ARG_ALWAYS_GENERATE) 1884 set(component ${dest}) 1885 else() 1886 set(component ${name}) 1887 endif() 1888 endif() 1889 1890 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX}) 1891 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX}) 1892 1893 install(SCRIPT ${INSTALL_SYMLINK} 1894 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})" 1895 COMPONENT ${component}) 1896 1897 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE) 1898 add_llvm_install_targets(install-${name} 1899 DEPENDS ${name} ${dest} 1900 COMPONENT ${name} 1901 SYMLINK ${dest}) 1902 endif() 1903endfunction() 1904 1905function(add_llvm_tool_symlink link_name target) 1906 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN}) 1907 set(dest_binary "$<TARGET_FILE:${target}>") 1908 1909 # This got a bit gross... For multi-configuration generators the target 1910 # properties return the resolved value of the string, not the build system 1911 # expression. To reconstruct the platform-agnostic path we have to do some 1912 # magic. First we grab one of the types, and a type-specific path. Then from 1913 # the type-specific path we find the last occurrence of the type in the path, 1914 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type 1915 # agnostic again. 1916 if(NOT ARG_OUTPUT_DIR) 1917 # If you're not overriding the OUTPUT_DIR, we can make the link relative in 1918 # the same directory. 1919 if(CMAKE_HOST_UNIX) 1920 set(dest_binary "$<TARGET_FILE_NAME:${target}>") 1921 endif() 1922 if(CMAKE_CONFIGURATION_TYPES) 1923 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type) 1924 string(TOUPPER ${first_type} first_type_upper) 1925 set(first_type_suffix _${first_type_upper}) 1926 endif() 1927 get_target_property(target_type ${target} TYPE) 1928 if(${target_type} STREQUAL "STATIC_LIBRARY") 1929 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix}) 1930 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY") 1931 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix}) 1932 else() 1933 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix}) 1934 endif() 1935 if(CMAKE_CONFIGURATION_TYPES) 1936 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE) 1937 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix) 1938 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix) 1939 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/" 1940 path_suffix ${path_suffix}) 1941 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix}) 1942 endif() 1943 endif() 1944 1945 if(CMAKE_HOST_UNIX) 1946 set(LLVM_LINK_OR_COPY create_symlink) 1947 else() 1948 set(LLVM_LINK_OR_COPY copy) 1949 endif() 1950 1951 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}") 1952 1953 set(target_name ${link_name}) 1954 if(TARGET ${link_name}) 1955 set(target_name ${link_name}-link) 1956 endif() 1957 1958 1959 if(ARG_ALWAYS_GENERATE) 1960 set_property(DIRECTORY APPEND PROPERTY 1961 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary}) 1962 add_custom_command(TARGET ${target} POST_BUILD 1963 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}") 1964 else() 1965 add_custom_command(OUTPUT ${output_path} 1966 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}" 1967 DEPENDS ${target}) 1968 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path}) 1969 set_target_properties(${target_name} PROPERTIES FOLDER Tools) 1970 1971 # Make sure both the link and target are toolchain tools 1972 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS) 1973 set(TOOL_IS_TOOLCHAIN ON) 1974 endif() 1975 1976 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS) 1977 llvm_install_symlink(${link_name} ${target}) 1978 endif() 1979 endif() 1980endfunction() 1981 1982function(llvm_externalize_debuginfo name) 1983 if(NOT LLVM_EXTERNALIZE_DEBUGINFO) 1984 return() 1985 endif() 1986 1987 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP) 1988 if(APPLE) 1989 if(NOT CMAKE_STRIP) 1990 set(CMAKE_STRIP xcrun strip) 1991 endif() 1992 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>) 1993 else() 1994 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>) 1995 endif() 1996 endif() 1997 1998 if(APPLE) 1999 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION) 2000 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION}) 2001 else() 2002 set(file_ext dSYM) 2003 endif() 2004 2005 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}") 2006 2007 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR) 2008 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}") 2009 else() 2010 set(output_path "-o=${output_name}") 2011 endif() 2012 2013 if(CMAKE_CXX_FLAGS MATCHES "-flto" 2014 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto") 2015 2016 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o) 2017 set_property(TARGET ${name} APPEND_STRING PROPERTY 2018 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}") 2019 endif() 2020 if(NOT CMAKE_DSYMUTIL) 2021 set(CMAKE_DSYMUTIL xcrun dsymutil) 2022 endif() 2023 add_custom_command(TARGET ${name} POST_BUILD 2024 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}> 2025 ${strip_command} 2026 ) 2027 else() 2028 add_custom_command(TARGET ${name} POST_BUILD 2029 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug 2030 ${strip_command} -R .gnu_debuglink 2031 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}> 2032 ) 2033 endif() 2034endfunction() 2035 2036# Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path]) 2037function(llvm_codesign name) 2038 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN}) 2039 2040 if(NOT LLVM_CODESIGNING_IDENTITY) 2041 return() 2042 endif() 2043 2044 if(CMAKE_GENERATOR STREQUAL "Xcode") 2045 set_target_properties(${name} PROPERTIES 2046 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY} 2047 ) 2048 if(DEFINED ARG_ENTITLEMENTS) 2049 set_target_properties(${name} PROPERTIES 2050 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS} 2051 ) 2052 endif() 2053 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin) 2054 if(NOT CMAKE_CODESIGN) 2055 set(CMAKE_CODESIGN xcrun codesign) 2056 endif() 2057 if(NOT CMAKE_CODESIGN_ALLOCATE) 2058 execute_process( 2059 COMMAND xcrun -f codesign_allocate 2060 OUTPUT_STRIP_TRAILING_WHITESPACE 2061 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE 2062 ) 2063 endif() 2064 if(DEFINED ARG_ENTITLEMENTS) 2065 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS}) 2066 endif() 2067 2068 if (NOT ARG_BUNDLE_PATH) 2069 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>) 2070 endif() 2071 2072 # ld64 now always codesigns the binaries it creates. Apply the force arg 2073 # unconditionally so that we can - for example - add entitlements to the 2074 # targets that need it. 2075 set(force_flag "-f") 2076 2077 add_custom_command( 2078 TARGET ${name} POST_BUILD 2079 COMMAND ${CMAKE_COMMAND} -E 2080 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE} 2081 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY} 2082 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH} 2083 ) 2084 endif() 2085endfunction() 2086 2087function(llvm_setup_rpath name) 2088 if(CMAKE_INSTALL_RPATH) 2089 return() 2090 endif() 2091 2092 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX)) 2093 set(extra_libdir ${LLVM_LIBRARY_DIR}) 2094 elseif(LLVM_BUILD_LIBRARY_DIR) 2095 set(extra_libdir ${LLVM_LIBRARY_DIR}) 2096 endif() 2097 2098 if (APPLE) 2099 set(_install_name_dir INSTALL_NAME_DIR "@rpath") 2100 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) 2101 elseif(UNIX) 2102 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) 2103 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)") 2104 set_property(TARGET ${name} APPEND_STRING PROPERTY 2105 LINK_FLAGS " -Wl,-z,origin ") 2106 endif() 2107 if(LLVM_LINKER_IS_GNULD) 2108 # $ORIGIN is not interpreted at link time by ld.bfd 2109 set_property(TARGET ${name} APPEND_STRING PROPERTY 2110 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ") 2111 endif() 2112 else() 2113 return() 2114 endif() 2115 2116 set_target_properties(${name} PROPERTIES 2117 BUILD_WITH_INSTALL_RPATH On 2118 INSTALL_RPATH "${_install_rpath}" 2119 ${_install_name_dir}) 2120endfunction() 2121 2122function(setup_dependency_debugging name) 2123 if(NOT LLVM_DEPENDENCY_DEBUGGING) 2124 return() 2125 endif() 2126 2127 if("intrinsics_gen" IN_LIST ARGN) 2128 return() 2129 endif() 2130 2131 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))") 2132 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))") 2133 2134 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'") 2135 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command}) 2136endfunction() 2137 2138function(find_first_existing_vc_file path out_var) 2139 if(NOT EXISTS "${path}") 2140 return() 2141 endif() 2142 find_package(Git) 2143 if(GIT_FOUND) 2144 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir 2145 WORKING_DIRECTORY ${path} 2146 RESULT_VARIABLE git_result 2147 OUTPUT_VARIABLE git_output 2148 ERROR_QUIET) 2149 if(git_result EQUAL 0) 2150 string(STRIP "${git_output}" git_output) 2151 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path}) 2152 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD 2153 if (NOT EXISTS "${git_dir}/logs/HEAD") 2154 execute_process(COMMAND ${CMAKE_COMMAND} -E touch HEAD 2155 WORKING_DIRECTORY "${git_dir}/logs" 2156 RESULT_VARIABLE touch_head_result 2157 ERROR_QUIET) 2158 if (NOT touch_head_result EQUAL 0) 2159 return() 2160 endif() 2161 endif() 2162 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE) 2163 endif() 2164 endif() 2165endfunction() 2166