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