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