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 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to 12 # force EH 13 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH) 14 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) 15 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}") 16 set(LLVM_REQUIRES_RTTI ON) 17 endif() 18 if(MSVC) 19 list(APPEND LLVM_COMPILE_FLAGS "/EHsc") 20 endif() 21 else() 22 if(LLVM_COMPILER_IS_GCC_COMPATIBLE) 23 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions") 24 elseif(MSVC) 25 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0) 26 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-") 27 endif() 28 endif() 29 30 # LLVM_REQUIRES_RTTI is an internal flag that individual 31 # targets can use to force RTTI 32 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "") 33 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) 34 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "") 35 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0) 36 if (LLVM_COMPILER_IS_GCC_COMPATIBLE) 37 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti") 38 elseif (MSVC) 39 list(APPEND LLVM_COMPILE_FLAGS "/GR-") 40 endif () 41 elseif(MSVC) 42 list(APPEND LLVM_COMPILE_FLAGS "/GR") 43 endif() 44 45 # Assume that; 46 # - LLVM_COMPILE_FLAGS is list. 47 # - PROPERTY COMPILE_FLAGS is string. 48 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}") 49 50 if(update_src_props) 51 foreach(fn ${sources}) 52 get_filename_component(suf ${fn} EXT) 53 if("${suf}" STREQUAL ".cpp") 54 set_property(SOURCE ${fn} APPEND_STRING PROPERTY 55 COMPILE_FLAGS "${target_compile_flags}") 56 endif() 57 endforeach() 58 else() 59 # Update target props, since all sources are C++. 60 set_property(TARGET ${name} APPEND_STRING PROPERTY 61 COMPILE_FLAGS "${target_compile_flags}") 62 endif() 63 64 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) 65endfunction() 66 67function(add_llvm_symbol_exports target_name export_file) 68 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 69 set(native_export_file "${target_name}.exports") 70 add_custom_command(OUTPUT ${native_export_file} 71 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file} 72 DEPENDS ${export_file} 73 VERBATIM 74 COMMENT "Creating export file for ${target_name}") 75 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 76 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 77 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX") 78 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 79 LINK_FLAGS " -Wl,-bE:${export_file}") 80 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT) 81 # Gold and BFD ld require a version script rather than a plain list. 82 set(native_export_file "${target_name}.exports") 83 # FIXME: Don't write the "local:" line on OpenBSD. 84 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M) 85 add_custom_command(OUTPUT ${native_export_file} 86 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file} 87 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || : 88 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file} 89 COMMAND echo " local: *;" >> ${native_export_file} 90 COMMAND echo "};" >> ${native_export_file} 91 DEPENDS ${export_file} 92 VERBATIM 93 COMMENT "Creating export file for ${target_name}") 94 if (${LLVM_LINKER_IS_SOLARISLD}) 95 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 96 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 97 else() 98 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 99 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 100 endif() 101 else() 102 set(native_export_file "${target_name}.def") 103 104 add_custom_command(OUTPUT ${native_export_file} 105 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))" 106 < ${export_file} > ${native_export_file} 107 DEPENDS ${export_file} 108 VERBATIM 109 COMMENT "Creating export file for ${target_name}") 110 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}") 111 if(MSVC) 112 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"") 113 endif() 114 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 115 LINK_FLAGS " ${export_file_linker_flag}") 116 endif() 117 118 add_custom_target(${target_name}_exports DEPENDS ${native_export_file}) 119 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc") 120 121 get_property(srcs TARGET ${target_name} PROPERTY SOURCES) 122 foreach(src ${srcs}) 123 get_filename_component(extension ${src} EXT) 124 if(extension STREQUAL ".cpp") 125 set(first_source_file ${src}) 126 break() 127 endif() 128 endforeach() 129 130 # Force re-linking when the exports file changes. Actually, it 131 # forces recompilation of the source file. The LINK_DEPENDS target 132 # property only works for makefile-based generators. 133 # FIXME: This is not safe because this will create the same target 134 # ${native_export_file} in several different file: 135 # - One where we emitted ${target_name}_exports 136 # - One where we emitted the build command for the following object. 137 # set_property(SOURCE ${first_source_file} APPEND PROPERTY 138 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}) 139 140 set_property(DIRECTORY APPEND 141 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file}) 142 143 add_dependencies(${target_name} ${target_name}_exports) 144 145 # Add dependency to *_exports later -- CMake issue 14747 146 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports) 147 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE) 148endfunction(add_llvm_symbol_exports) 149 150if(APPLE) 151 execute_process( 152 COMMAND "${CMAKE_LINKER}" -v 153 ERROR_VARIABLE stderr 154 ) 155 set(LLVM_LINKER_DETECTED YES) 156 if("${stderr}" MATCHES "PROJECT:ld64") 157 set(LLVM_LINKER_IS_LD64 YES) 158 message(STATUS "Linker detection: ld64") 159 else() 160 set(LLVM_LINKER_DETECTED NO) 161 message(STATUS "Linker detection: unknown") 162 endif() 163elseif(NOT WIN32) 164 # Detect what linker we have here 165 if( LLVM_USE_LINKER ) 166 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version) 167 else() 168 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}") 169 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version) 170 endif() 171 execute_process( 172 COMMAND ${command} 173 OUTPUT_VARIABLE stdout 174 ERROR_VARIABLE stderr 175 ) 176 set(LLVM_LINKER_DETECTED YES) 177 if("${stdout}" MATCHES "GNU gold") 178 set(LLVM_LINKER_IS_GOLD YES) 179 message(STATUS "Linker detection: GNU Gold") 180 elseif("${stdout}" MATCHES "^LLD") 181 set(LLVM_LINKER_IS_LLD YES) 182 message(STATUS "Linker detection: LLD") 183 elseif("${stdout}" MATCHES "GNU ld") 184 set(LLVM_LINKER_IS_GNULD YES) 185 message(STATUS "Linker detection: GNU ld") 186 elseif("${stderr}" MATCHES "Solaris Link Editors" OR 187 "${stdout}" MATCHES "Solaris Link Editors") 188 set(LLVM_LINKER_IS_SOLARISLD YES) 189 message(STATUS "Linker detection: Solaris ld") 190 else() 191 set(LLVM_LINKER_DETECTED NO) 192 message(STATUS "Linker detection: unknown") 193 endif() 194endif() 195 196function(add_link_opts target_name) 197 # Don't use linker optimizations in debug builds since it slows down the 198 # linker in a context where the optimizations are not important. 199 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 200 201 # Pass -O3 to the linker. This enabled different optimizations on different 202 # linkers. 203 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32)) 204 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 205 LINK_FLAGS " -Wl,-O3") 206 endif() 207 208 if(LLVM_LINKER_IS_GOLD) 209 # With gold gc-sections is always safe. 210 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 211 LINK_FLAGS " -Wl,--gc-sections") 212 # Note that there is a bug with -Wl,--icf=safe so it is not safe 213 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704. 214 endif() 215 216 if(NOT LLVM_NO_DEAD_STRIP) 217 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 218 # ld64's implementation of -dead_strip breaks tools that use plugins. 219 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 220 LINK_FLAGS " -Wl,-dead_strip") 221 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") 222 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 223 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections") 224 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD") 225 # Object files are compiled with -ffunction-data-sections. 226 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks 227 # tools that use plugins. Always pass --gc-sections once we require 228 # a newer linker. 229 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 230 LINK_FLAGS " -Wl,--gc-sections") 231 endif() 232 endif() 233 endif() 234endfunction(add_link_opts) 235 236# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}. 237# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more, 238# or a certain builder, for eaxample, msbuild.exe, would be confused. 239function(set_output_directory target) 240 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN}) 241 242 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY. 243 # It affects output of add_library(MODULE). 244 if(WIN32 OR CYGWIN) 245 # DLL platform 246 set(module_dir ${ARG_BINARY_DIR}) 247 else() 248 set(module_dir ${ARG_LIBRARY_DIR}) 249 endif() 250 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".") 251 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES}) 252 string(TOUPPER "${build_mode}" CONFIG_SUFFIX) 253 if(ARG_BINARY_DIR) 254 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR}) 255 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi}) 256 endif() 257 if(ARG_LIBRARY_DIR) 258 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR}) 259 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li}) 260 endif() 261 if(module_dir) 262 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir}) 263 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi}) 264 endif() 265 endforeach() 266 else() 267 if(ARG_BINARY_DIR) 268 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR}) 269 endif() 270 if(ARG_LIBRARY_DIR) 271 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR}) 272 endif() 273 if(module_dir) 274 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir}) 275 endif() 276 endif() 277endfunction() 278 279# If on Windows and building with MSVC, add the resource script containing the 280# VERSIONINFO data to the project. This embeds version resource information 281# into the output .exe or .dll. 282# TODO: Enable for MinGW Windows builds too. 283# 284function(add_windows_version_resource_file OUT_VAR) 285 set(sources ${ARGN}) 286 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") 287 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc) 288 if(EXISTS ${resource_file}) 289 set(sources ${sources} ${resource_file}) 290 source_group("Resource Files" ${resource_file}) 291 set(windows_resource_file ${resource_file} PARENT_SCOPE) 292 endif() 293 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") 294 295 set(${OUT_VAR} ${sources} PARENT_SCOPE) 296endfunction(add_windows_version_resource_file) 297 298# set_windows_version_resource_properties(name resource_file... 299# VERSION_MAJOR int 300# Optional major version number (defaults to LLVM_VERSION_MAJOR) 301# VERSION_MINOR int 302# Optional minor version number (defaults to LLVM_VERSION_MINOR) 303# VERSION_PATCHLEVEL int 304# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH) 305# VERSION_STRING 306# Optional version string (defaults to PACKAGE_VERSION) 307# PRODUCT_NAME 308# Optional product name string (defaults to "LLVM") 309# ) 310function(set_windows_version_resource_properties name resource_file) 311 cmake_parse_arguments(ARG 312 "" 313 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME" 314 "" 315 ${ARGN}) 316 317 if (NOT DEFINED ARG_VERSION_MAJOR) 318 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR}) 319 endif() 320 321 if (NOT DEFINED ARG_VERSION_MINOR) 322 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR}) 323 endif() 324 325 if (NOT DEFINED ARG_VERSION_PATCHLEVEL) 326 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH}) 327 endif() 328 329 if (NOT DEFINED ARG_VERSION_STRING) 330 set(ARG_VERSION_STRING ${PACKAGE_VERSION}) 331 endif() 332 333 if (NOT DEFINED ARG_PRODUCT_NAME) 334 set(ARG_PRODUCT_NAME "LLVM") 335 endif() 336 337 set_property(SOURCE ${resource_file} 338 PROPERTY COMPILE_FLAGS /nologo) 339 set_property(SOURCE ${resource_file} 340 PROPERTY COMPILE_DEFINITIONS 341 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}" 342 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}" 343 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}" 344 "RC_VERSION_FIELD_4=0" 345 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\"" 346 "RC_INTERNAL_NAME=\"${name}\"" 347 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\"" 348 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"") 349endfunction(set_windows_version_resource_properties) 350 351# llvm_add_library(name sources... 352# SHARED;STATIC 353# STATIC by default w/o BUILD_SHARED_LIBS. 354# SHARED by default w/ BUILD_SHARED_LIBS. 355# OBJECT 356# Also create an OBJECT library target. Default if STATIC && SHARED. 357# MODULE 358# Target ${name} might not be created on unsupported platforms. 359# Check with "if(TARGET ${name})". 360# DISABLE_LLVM_LINK_LLVM_DYLIB 361# Do not link this library to libLLVM, even if 362# LLVM_LINK_LLVM_DYLIB is enabled. 363# OUTPUT_NAME name 364# Corresponds to OUTPUT_NAME in target properties. 365# DEPENDS targets... 366# Same semantics as add_dependencies(). 367# LINK_COMPONENTS components... 368# Same as the variable LLVM_LINK_COMPONENTS. 369# LINK_LIBS lib_targets... 370# Same semantics as target_link_libraries(). 371# ADDITIONAL_HEADERS 372# May specify header files for IDE generators. 373# SONAME 374# Should set SONAME link flags and create symlinks 375# PLUGIN_TOOL 376# The tool (i.e. cmake target) that this plugin will link against 377# ) 378function(llvm_add_library name) 379 cmake_parse_arguments(ARG 380 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME" 381 "OUTPUT_NAME;PLUGIN_TOOL" 382 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS" 383 ${ARGN}) 384 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) 385 if(ARG_ADDITIONAL_HEADERS) 386 # Pass through ADDITIONAL_HEADERS. 387 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS}) 388 endif() 389 if(ARG_OBJLIBS) 390 set(ALL_FILES ${ARG_OBJLIBS}) 391 else() 392 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS}) 393 endif() 394 395 if(ARG_MODULE) 396 if(ARG_SHARED OR ARG_STATIC) 397 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.") 398 endif() 399 # Plugins that link against a tool are allowed even when plugins in general are not 400 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)) 401 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.") 402 return() 403 endif() 404 else() 405 if(ARG_PLUGIN_TOOL) 406 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.") 407 endif() 408 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC) 409 set(ARG_SHARED TRUE) 410 endif() 411 if(NOT ARG_SHARED) 412 set(ARG_STATIC TRUE) 413 endif() 414 endif() 415 416 # Generate objlib 417 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT) 418 # Generate an obj library for both targets. 419 set(obj_name "obj.${name}") 420 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL 421 ${ALL_FILES} 422 ) 423 llvm_update_compile_flags(${obj_name}) 424 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>") 425 426 # Do add_dependencies(obj) later due to CMake issue 14747. 427 list(APPEND objlibs ${obj_name}) 428 429 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries") 430 endif() 431 432 if(ARG_SHARED AND ARG_STATIC) 433 # static 434 set(name_static "${name}_static") 435 if(ARG_OUTPUT_NAME) 436 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}") 437 endif() 438 # DEPENDS has been appended to LLVM_COMMON_LIBS. 439 llvm_add_library(${name_static} STATIC 440 ${output_name} 441 OBJLIBS ${ALL_FILES} # objlib 442 LINK_LIBS ${ARG_LINK_LIBS} 443 LINK_COMPONENTS ${ARG_LINK_COMPONENTS} 444 ) 445 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY. 446 set(ARG_STATIC) 447 endif() 448 449 if(ARG_MODULE) 450 add_library(${name} MODULE ${ALL_FILES}) 451 llvm_setup_rpath(${name}) 452 elseif(ARG_SHARED) 453 add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) 454 add_library(${name} SHARED ${ALL_FILES}) 455 456 llvm_setup_rpath(${name}) 457 458 else() 459 add_library(${name} STATIC ${ALL_FILES}) 460 endif() 461 462 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS}) 463 464 if(DEFINED windows_resource_file) 465 set_windows_version_resource_properties(${name} ${windows_resource_file}) 466 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE) 467 endif() 468 469 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 470 # $<TARGET_OBJECTS> doesn't require compile flags. 471 if(NOT obj_name) 472 llvm_update_compile_flags(${name}) 473 endif() 474 add_link_opts( ${name} ) 475 if(ARG_OUTPUT_NAME) 476 set_target_properties(${name} 477 PROPERTIES 478 OUTPUT_NAME ${ARG_OUTPUT_NAME} 479 ) 480 endif() 481 482 if(ARG_MODULE) 483 set_target_properties(${name} PROPERTIES 484 PREFIX "" 485 SUFFIX ${LLVM_PLUGIN_EXT} 486 ) 487 endif() 488 489 if(ARG_SHARED) 490 if(WIN32) 491 set_target_properties(${name} PROPERTIES 492 PREFIX "" 493 ) 494 endif() 495 496 # Set SOVERSION on shared libraries that lack explicit SONAME 497 # specifier, on *nix systems that are not Darwin. 498 if(UNIX AND NOT APPLE AND NOT ARG_SONAME) 499 set_target_properties(${name} 500 PROPERTIES 501 # Since 4.0.0, the ABI version is indicated by the major version 502 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX} 503 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}) 504 endif() 505 endif() 506 507 if(ARG_MODULE OR ARG_SHARED) 508 # Do not add -Dname_EXPORTS to the command-line when building files in this 509 # target. Doing so is actively harmful for the modules build because it 510 # creates extra module variants, and not useful because we don't use these 511 # macros. 512 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" ) 513 514 if (LLVM_EXPORTED_SYMBOL_FILE) 515 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} ) 516 endif() 517 endif() 518 519 if(ARG_SHARED AND UNIX) 520 if(NOT APPLE AND ARG_SONAME) 521 get_target_property(output_name ${name} OUTPUT_NAME) 522 if(${output_name} STREQUAL "output_name-NOTFOUND") 523 set(output_name ${name}) 524 endif() 525 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}) 526 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX}) 527 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name}) 528 llvm_install_library_symlink(${api_name} ${library_name} SHARED 529 COMPONENT ${name} 530 ALWAYS_GENERATE) 531 llvm_install_library_symlink(${output_name} ${library_name} SHARED 532 COMPONENT ${name} 533 ALWAYS_GENERATE) 534 endif() 535 endif() 536 537 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN)) 538 # On DLL platforms symbols are imported from the tool by linking against it. 539 set(llvm_libs ${ARG_PLUGIN_TOOL}) 540 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS) 541 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB) 542 set(llvm_libs LLVM) 543 else() 544 llvm_map_components_to_libnames(llvm_libs 545 ${ARG_LINK_COMPONENTS} 546 ${LLVM_LINK_COMPONENTS} 547 ) 548 endif() 549 else() 550 # Components have not been defined explicitly in CMake, so add the 551 # dependency information for this library as defined by LLVMBuild. 552 # 553 # It would be nice to verify that we have the dependencies for this library 554 # name, but using get_property(... SET) doesn't suffice to determine if a 555 # property has been set to an empty value. 556 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name}) 557 endif() 558 559 if(ARG_STATIC) 560 set(libtype INTERFACE) 561 else() 562 # We can use PRIVATE since SO knows its dependent libs. 563 set(libtype PRIVATE) 564 endif() 565 566 target_link_libraries(${name} ${libtype} 567 ${ARG_LINK_LIBS} 568 ${lib_deps} 569 ${llvm_libs} 570 ) 571 572 if(LLVM_COMMON_DEPENDS) 573 add_dependencies(${name} ${LLVM_COMMON_DEPENDS}) 574 # Add dependencies also to objlibs. 575 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user. 576 foreach(objlib ${objlibs}) 577 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS}) 578 endforeach() 579 endif() 580 581 if(ARG_SHARED OR ARG_MODULE) 582 llvm_externalize_debuginfo(${name}) 583 llvm_codesign(TARGET ${name}) 584 endif() 585endfunction() 586 587function(add_llvm_install_targets target) 588 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX" "DEPENDS" ${ARGN}) 589 if(ARG_COMPONENT) 590 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}") 591 endif() 592 if(ARG_PREFIX) 593 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}") 594 endif() 595 596 add_custom_target(${target} 597 DEPENDS ${ARG_DEPENDS} 598 COMMAND "${CMAKE_COMMAND}" 599 ${component_option} 600 ${prefix_option} 601 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" 602 USES_TERMINAL) 603 add_custom_target(${target}-stripped 604 DEPENDS ${ARG_DEPENDS} 605 COMMAND "${CMAKE_COMMAND}" 606 ${component_option} 607 ${prefix_option} 608 -DCMAKE_INSTALL_DO_STRIP=1 609 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" 610 USES_TERMINAL) 611endfunction() 612 613macro(add_llvm_library name) 614 cmake_parse_arguments(ARG 615 "SHARED;BUILDTREE_ONLY" 616 "" 617 "" 618 ${ARGN}) 619 if( BUILD_SHARED_LIBS OR ARG_SHARED ) 620 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS}) 621 else() 622 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS}) 623 endif() 624 625 # Libraries that are meant to only be exposed via the build tree only are 626 # never installed and are only exported as a target in the special build tree 627 # config file. 628 if (NOT ARG_BUILDTREE_ONLY) 629 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} ) 630 endif() 631 632 if( EXCLUDE_FROM_ALL ) 633 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) 634 elseif(ARG_BUILDTREE_ONLY) 635 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name}) 636 else() 637 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO" OR 638 ${name} STREQUAL "OptRemarks" OR 639 (LLVM_LINK_LLVM_DYLIB AND ${name} STREQUAL "LLVM")) 640 set(install_dir lib${LLVM_LIBDIR_SUFFIX}) 641 if(ARG_SHARED OR BUILD_SHARED_LIBS) 642 if(WIN32 OR CYGWIN OR MINGW) 643 set(install_type RUNTIME) 644 set(install_dir bin) 645 else() 646 set(install_type LIBRARY) 647 endif() 648 else() 649 set(install_type ARCHIVE) 650 endif() 651 652 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 653 NOT LLVM_DISTRIBUTION_COMPONENTS) 654 set(export_to_llvmexports EXPORT LLVMExports) 655 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True) 656 endif() 657 658 install(TARGETS ${name} 659 ${export_to_llvmexports} 660 ${install_type} DESTINATION ${install_dir} 661 COMPONENT ${name}) 662 663 if (NOT LLVM_ENABLE_IDE) 664 add_llvm_install_targets(install-${name} 665 DEPENDS ${name} 666 COMPONENT ${name}) 667 endif() 668 endif() 669 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 670 endif() 671 set_target_properties(${name} PROPERTIES FOLDER "Libraries") 672endmacro(add_llvm_library name) 673 674macro(add_llvm_loadable_module name) 675 llvm_add_library(${name} MODULE ${ARGN}) 676 if(NOT TARGET ${name}) 677 # Add empty "phony" target 678 add_custom_target(${name}) 679 else() 680 if( EXCLUDE_FROM_ALL ) 681 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) 682 else() 683 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 684 if(WIN32 OR CYGWIN) 685 # DLL platform 686 set(dlldir "bin") 687 else() 688 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}") 689 endif() 690 691 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 692 NOT LLVM_DISTRIBUTION_COMPONENTS) 693 set(export_to_llvmexports EXPORT LLVMExports) 694 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True) 695 endif() 696 697 install(TARGETS ${name} 698 ${export_to_llvmexports} 699 LIBRARY DESTINATION ${dlldir} 700 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) 701 endif() 702 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 703 endif() 704 endif() 705 706 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules") 707endmacro(add_llvm_loadable_module name) 708 709 710macro(add_llvm_executable name) 711 cmake_parse_arguments(ARG 712 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH" 713 "ENTITLEMENTS" 714 "DEPENDS" 715 ${ARGN}) 716 717 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ) 718 719 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) 720 721 # Generate objlib 722 if(LLVM_ENABLE_OBJLIB) 723 # Generate an obj library for both targets. 724 set(obj_name "obj.${name}") 725 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL 726 ${ALL_FILES} 727 ) 728 llvm_update_compile_flags(${obj_name}) 729 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>") 730 731 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries") 732 endif() 733 734 add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) 735 736 if(XCODE) 737 # Note: the dummy.cpp source file provides no definitions. However, 738 # it forces Xcode to properly link the static library. 739 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp") 740 endif() 741 742 if( EXCLUDE_FROM_ALL ) 743 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES}) 744 else() 745 add_executable(${name} ${ALL_FILES}) 746 endif() 747 748 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS}) 749 750 if(NOT ARG_NO_INSTALL_RPATH) 751 llvm_setup_rpath(${name}) 752 endif() 753 754 if(DEFINED windows_resource_file) 755 set_windows_version_resource_properties(${name} ${windows_resource_file}) 756 endif() 757 758 # $<TARGET_OBJECTS> doesn't require compile flags. 759 if(NOT LLVM_ENABLE_OBJLIB) 760 llvm_update_compile_flags(${name}) 761 endif() 762 add_link_opts( ${name} ) 763 764 # Do not add -Dname_EXPORTS to the command-line when building files in this 765 # target. Doing so is actively harmful for the modules build because it 766 # creates extra module variants, and not useful because we don't use these 767 # macros. 768 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" ) 769 770 if (LLVM_EXPORTED_SYMBOL_FILE) 771 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} ) 772 endif(LLVM_EXPORTED_SYMBOL_FILE) 773 774 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB) 775 set(USE_SHARED USE_SHARED) 776 endif() 777 778 set(EXCLUDE_FROM_ALL OFF) 779 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 780 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} ) 781 if( LLVM_COMMON_DEPENDS ) 782 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) 783 endif( LLVM_COMMON_DEPENDS ) 784 785 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO) 786 llvm_externalize_debuginfo(${name}) 787 endif() 788 if (LLVM_PTHREAD_LIB) 789 # libpthreads overrides some standard library symbols, so main 790 # executable must be linked with it in order to provide consistent 791 # API for all shared libaries loaded by this executable. 792 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB}) 793 endif() 794 795 llvm_codesign(TARGET ${name} ENTITLEMENTS ${ARG_ENTITLEMENTS}) 796endmacro(add_llvm_executable name) 797 798function(export_executable_symbols target) 799 if (LLVM_EXPORTED_SYMBOL_FILE) 800 # The symbol file should contain the symbols we want the executable to 801 # export 802 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 803 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 804 # Extract the symbols to export from the static libraries that the 805 # executable links against. 806 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 807 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols) 808 # We need to consider not just the direct link dependencies, but also the 809 # transitive link dependencies. Do this by starting with the set of direct 810 # dependencies, then the dependencies of those dependencies, and so on. 811 get_target_property(new_libs ${target} LINK_LIBRARIES) 812 set(link_libs ${new_libs}) 813 while(NOT "${new_libs}" STREQUAL "") 814 foreach(lib ${new_libs}) 815 if(TARGET ${lib}) 816 get_target_property(lib_type ${lib} TYPE) 817 if("${lib_type}" STREQUAL "STATIC_LIBRARY") 818 list(APPEND static_libs ${lib}) 819 else() 820 list(APPEND other_libs ${lib}) 821 endif() 822 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES) 823 foreach(transitive_lib ${transitive_libs}) 824 list(FIND link_libs ${transitive_lib} idx) 825 if(TARGET ${transitive_lib} AND idx EQUAL -1) 826 list(APPEND newer_libs ${transitive_lib}) 827 list(APPEND link_libs ${transitive_lib}) 828 endif() 829 endforeach(transitive_lib) 830 endif() 831 endforeach(lib) 832 set(new_libs ${newer_libs}) 833 set(newer_libs "") 834 endwhile() 835 if (MSVC) 836 set(mangling microsoft) 837 else() 838 set(mangling itanium) 839 endif() 840 add_custom_command(OUTPUT ${exported_symbol_file} 841 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file} 842 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR} 843 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs} 844 VERBATIM 845 COMMENT "Generating export list for ${target}") 846 add_llvm_symbol_exports( ${target} ${exported_symbol_file} ) 847 # If something links against this executable then we want a 848 # transitive link against only the libraries whose symbols 849 # we aren't exporting. 850 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}") 851 # The default import library suffix that cmake uses for cygwin/mingw is 852 # ".dll.a", but for clang.exe that causes a collision with libclang.dll, 853 # where the import libraries of both get named libclang.dll.a. Use a suffix 854 # of ".exe.a" to avoid this. 855 if(CYGWIN OR MINGW) 856 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a") 857 endif() 858 elseif(NOT (WIN32 OR CYGWIN)) 859 # On Windows auto-exporting everything doesn't work because of the limit on 860 # the size of the exported symbol table, but on other platforms we can do 861 # it without any trouble. 862 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 863 if (APPLE) 864 set_property(TARGET ${target} APPEND_STRING PROPERTY 865 LINK_FLAGS " -rdynamic") 866 endif() 867 endif() 868endfunction() 869 870if(NOT LLVM_TOOLCHAIN_TOOLS) 871 set (LLVM_TOOLCHAIN_TOOLS 872 llvm-ar 873 llvm-ranlib 874 llvm-lib 875 llvm-objdump 876 llvm-rc 877 ) 878endif() 879 880macro(add_llvm_tool name) 881 if( NOT LLVM_BUILD_TOOLS ) 882 set(EXCLUDE_FROM_ALL ON) 883 endif() 884 add_llvm_executable(${name} ${ARGN}) 885 886 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 887 if( LLVM_BUILD_TOOLS ) 888 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR 889 NOT LLVM_DISTRIBUTION_COMPONENTS) 890 set(export_to_llvmexports EXPORT LLVMExports) 891 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True) 892 endif() 893 894 install(TARGETS ${name} 895 ${export_to_llvmexports} 896 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR} 897 COMPONENT ${name}) 898 899 if (NOT LLVM_ENABLE_IDE) 900 add_llvm_install_targets(install-${name} 901 DEPENDS ${name} 902 COMPONENT ${name}) 903 endif() 904 endif() 905 endif() 906 if( LLVM_BUILD_TOOLS ) 907 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 908 endif() 909 set_target_properties(${name} PROPERTIES FOLDER "Tools") 910endmacro(add_llvm_tool name) 911 912 913macro(add_llvm_example name) 914 if( NOT LLVM_BUILD_EXAMPLES ) 915 set(EXCLUDE_FROM_ALL ON) 916 endif() 917 add_llvm_executable(${name} ${ARGN}) 918 if( LLVM_BUILD_EXAMPLES ) 919 install(TARGETS ${name} RUNTIME DESTINATION examples) 920 endif() 921 set_target_properties(${name} PROPERTIES FOLDER "Examples") 922endmacro(add_llvm_example name) 923 924# This is a macro that is used to create targets for executables that are needed 925# for development, but that are not intended to be installed by default. 926macro(add_llvm_utility name) 927 if ( NOT LLVM_BUILD_UTILS ) 928 set(EXCLUDE_FROM_ALL ON) 929 endif() 930 931 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) 932 set_target_properties(${name} PROPERTIES FOLDER "Utils") 933 if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS ) 934 install (TARGETS ${name} 935 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR} 936 COMPONENT ${name}) 937 if (NOT LLVM_ENABLE_IDE) 938 add_llvm_install_targets(install-${name} 939 DEPENDS ${name} 940 COMPONENT ${name}) 941 endif() 942 endif() 943endmacro(add_llvm_utility name) 944 945macro(add_llvm_fuzzer name) 946 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN}) 947 if( LLVM_LIB_FUZZING_ENGINE ) 948 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN}) 949 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) 950 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE}) 951 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 952 elseif( LLVM_USE_SANITIZE_COVERAGE ) 953 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer") 954 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN}) 955 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) 956 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 957 elseif( ARG_DUMMY_MAIN ) 958 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS}) 959 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 960 endif() 961endmacro() 962 963macro(add_llvm_target target_name) 964 include_directories(BEFORE 965 ${CMAKE_CURRENT_BINARY_DIR} 966 ${CMAKE_CURRENT_SOURCE_DIR}) 967 add_llvm_library(LLVM${target_name} ${ARGN}) 968 set( CURRENT_LLVM_TARGET LLVM${target_name} ) 969endmacro(add_llvm_target) 970 971function(canonicalize_tool_name name output) 972 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name}) 973 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip}) 974 string(TOUPPER ${nameUNDERSCORE} nameUPPER) 975 set(${output} "${nameUPPER}" PARENT_SCOPE) 976endfunction(canonicalize_tool_name) 977 978# Custom add_subdirectory wrapper 979# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional 980# path if it differs from the name. 981function(add_llvm_subdirectory project type name) 982 set(add_llvm_external_dir "${ARGN}") 983 if("${add_llvm_external_dir}" STREQUAL "") 984 set(add_llvm_external_dir ${name}) 985 endif() 986 canonicalize_tool_name(${name} nameUPPER) 987 set(canonical_full_name ${project}_${type}_${nameUPPER}) 988 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED) 989 if(already_processed) 990 return() 991 endif() 992 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES) 993 994 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt) 995 # Treat it as in-tree subproject. 996 option(${canonical_full_name}_BUILD 997 "Whether to build ${name} as part of ${project}" On) 998 mark_as_advanced(${project}_${type}_${name}_BUILD) 999 if(${canonical_full_name}_BUILD) 1000 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir}) 1001 endif() 1002 else() 1003 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR 1004 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" 1005 CACHE PATH "Path to ${name} source directory") 1006 set(${canonical_full_name}_BUILD_DEFAULT ON) 1007 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}) 1008 set(${canonical_full_name}_BUILD_DEFAULT OFF) 1009 endif() 1010 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF") 1011 set(${canonical_full_name}_BUILD_DEFAULT OFF) 1012 endif() 1013 option(${canonical_full_name}_BUILD 1014 "Whether to build ${name} as part of LLVM" 1015 ${${canonical_full_name}_BUILD_DEFAULT}) 1016 if (${canonical_full_name}_BUILD) 1017 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}) 1018 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir}) 1019 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "") 1020 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}") 1021 endif() 1022 endif() 1023 endif() 1024endfunction() 1025 1026# Add external project that may want to be built as part of llvm such as Clang, 1027# lld, and Polly. This adds two options. One for the source directory of the 1028# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to 1029# enable or disable building it with everything else. 1030# Additional parameter can be specified as the name of directory. 1031macro(add_llvm_external_project name) 1032 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN}) 1033endmacro() 1034 1035macro(add_llvm_tool_subdirectory name) 1036 add_llvm_external_project(${name}) 1037endmacro(add_llvm_tool_subdirectory) 1038 1039function(get_project_name_from_src_var var output) 1040 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR" 1041 MACHED_TOOL "${var}") 1042 if(MACHED_TOOL) 1043 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE) 1044 else() 1045 set(${output} PARENT_SCOPE) 1046 endif() 1047endfunction() 1048 1049function(create_subdirectory_options project type) 1050 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") 1051 foreach(dir ${sub-dirs}) 1052 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt") 1053 canonicalize_tool_name(${dir} name) 1054 option(${project}_${type}_${name}_BUILD 1055 "Whether to build ${name} as part of ${project}" On) 1056 mark_as_advanced(${project}_${type}_${name}_BUILD) 1057 endif() 1058 endforeach() 1059endfunction(create_subdirectory_options) 1060 1061function(create_llvm_tool_options) 1062 create_subdirectory_options(LLVM TOOL) 1063endfunction(create_llvm_tool_options) 1064 1065function(llvm_add_implicit_projects project) 1066 set(list_of_implicit_subdirs "") 1067 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") 1068 foreach(dir ${sub-dirs}) 1069 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt") 1070 canonicalize_tool_name(${dir} name) 1071 if (${project}_TOOL_${name}_BUILD) 1072 get_filename_component(fn "${dir}" NAME) 1073 list(APPEND list_of_implicit_subdirs "${fn}") 1074 endif() 1075 endif() 1076 endforeach() 1077 1078 foreach(external_proj ${list_of_implicit_subdirs}) 1079 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN}) 1080 endforeach() 1081endfunction(llvm_add_implicit_projects) 1082 1083function(add_llvm_implicit_projects) 1084 llvm_add_implicit_projects(LLVM) 1085endfunction(add_llvm_implicit_projects) 1086 1087# Generic support for adding a unittest. 1088function(add_unittest test_suite test_name) 1089 if( NOT LLVM_BUILD_TESTS ) 1090 set(EXCLUDE_FROM_ALL ON) 1091 endif() 1092 1093 # Our current version of gtest does not properly recognize C++11 support 1094 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM 1095 # itself requires C++11, we can safely force it on unconditionally so that 1096 # we don't have to fight with the buggy gtest check. 1097 add_definitions(-DGTEST_LANG_CXX11=1) 1098 add_definitions(-DGTEST_HAS_TR1_TUPLE=0) 1099 1100 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include) 1101 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include) 1102 if (NOT LLVM_ENABLE_THREADS) 1103 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0) 1104 endif () 1105 1106 if (SUPPORTS_VARIADIC_MACROS_FLAG) 1107 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros") 1108 endif () 1109 # Some parts of gtest rely on this GNU extension, don't warn on it. 1110 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG) 1111 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments") 1112 endif() 1113 1114 set(LLVM_REQUIRES_RTTI OFF) 1115 1116 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream 1117 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) 1118 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) 1119 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) 1120 # libpthreads overrides some standard library symbols, so main 1121 # executable must be linked with it in order to provide consistent 1122 # API for all shared libaries loaded by this executable. 1123 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB}) 1124 1125 add_dependencies(${test_suite} ${test_name}) 1126 get_target_property(test_suite_folder ${test_suite} FOLDER) 1127 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND") 1128 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}") 1129 endif () 1130endfunction() 1131 1132# Use for test binaries that call llvm::getInputFileDirectory(). Use of this 1133# is discouraged. 1134function(add_unittest_with_input_files test_suite test_name) 1135 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 1136 configure_file( 1137 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in 1138 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt) 1139 1140 add_unittest(${test_suite} ${test_name} ${ARGN}) 1141endfunction() 1142 1143# Generic support for adding a benchmark. 1144function(add_benchmark benchmark_name) 1145 if( NOT LLVM_BUILD_BENCHMARKS ) 1146 set(EXCLUDE_FROM_ALL ON) 1147 endif() 1148 1149 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) 1150 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) 1151 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) 1152 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils") 1153 target_link_libraries(${benchmark_name} PRIVATE benchmark) 1154endfunction() 1155 1156function(llvm_add_go_executable binary pkgpath) 1157 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN}) 1158 1159 if(LLVM_BINDINGS MATCHES "go") 1160 # FIXME: This should depend only on the libraries Go needs. 1161 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS) 1162 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX}) 1163 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}") 1164 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}") 1165 set(cppflags "") 1166 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES) 1167 foreach(d ${include_dirs}) 1168 set(cppflags "${cppflags} -I${d}") 1169 endforeach(d) 1170 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}") 1171 add_custom_command(OUTPUT ${binpath} 1172 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}" 1173 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath} 1174 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX} 1175 ${llvmlibs} ${ARG_DEPENDS} 1176 COMMENT "Building Go executable ${binary}" 1177 VERBATIM) 1178 if (ARG_ALL) 1179 add_custom_target(${binary} ALL DEPENDS ${binpath}) 1180 else() 1181 add_custom_target(${binary} DEPENDS ${binpath}) 1182 endif() 1183 endif() 1184endfunction() 1185 1186# This function canonicalize the CMake variables passed by names 1187# from CMake boolean to 0/1 suitable for passing into Python or C++, 1188# in place. 1189function(llvm_canonicalize_cmake_booleans) 1190 foreach(var ${ARGN}) 1191 if(${var}) 1192 set(${var} 1 PARENT_SCOPE) 1193 else() 1194 set(${var} 0 PARENT_SCOPE) 1195 endif() 1196 endforeach() 1197endfunction(llvm_canonicalize_cmake_booleans) 1198 1199macro(set_llvm_build_mode) 1200 # Configuration-time: See Unit/lit.site.cfg.in 1201 if (CMAKE_CFG_INTDIR STREQUAL ".") 1202 set(LLVM_BUILD_MODE ".") 1203 else () 1204 set(LLVM_BUILD_MODE "%(build_mode)s") 1205 endif () 1206endmacro() 1207 1208# This function provides an automatic way to 'configure'-like generate a file 1209# based on a set of common and custom variables, specifically targeting the 1210# variables needed for the 'lit.site.cfg' files. This function bundles the 1211# common variables that any Lit instance is likely to need, and custom 1212# variables can be passed in. 1213function(configure_lit_site_cfg site_in site_out) 1214 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN}) 1215 1216 if ("${ARG_MAIN_CONFIG}" STREQUAL "") 1217 get_filename_component(INPUT_DIR ${site_in} DIRECTORY) 1218 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg") 1219 endif() 1220 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "") 1221 set(ARG_OUTPUT_MAPPING "${site_out}") 1222 endif() 1223 1224 foreach(c ${LLVM_TARGETS_TO_BUILD}) 1225 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}") 1226 endforeach(c) 1227 set(TARGETS_TO_BUILD ${TARGETS_BUILT}) 1228 1229 set(SHLIBEXT "${LTDL_SHLIB_EXT}") 1230 1231 set_llvm_build_mode() 1232 1233 # They below might not be the build tree but provided binary tree. 1234 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR}) 1235 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR}) 1236 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}") 1237 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}") 1238 1239 # SHLIBDIR points the build tree. 1240 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}") 1241 1242 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}) 1243 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for 1244 # plugins. We may rename it. 1245 if(LLVM_ENABLE_PLUGINS) 1246 set(ENABLE_SHARED "1") 1247 else() 1248 set(ENABLE_SHARED "0") 1249 endif() 1250 1251 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE) 1252 set(ENABLE_ASSERTIONS "1") 1253 else() 1254 set(ENABLE_ASSERTIONS "0") 1255 endif() 1256 1257 set(HOST_OS ${CMAKE_SYSTEM_NAME}) 1258 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR}) 1259 1260 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}") 1261 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}") 1262 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}") 1263 1264 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!") 1265 1266 # Override config_target_triple (and the env) 1267 if(LLVM_TARGET_TRIPLE_ENV) 1268 # This is expanded into the heading. 1269 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n" 1270 "import os\n" 1271 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n" 1272 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n" 1273 ) 1274 1275 # This is expanded to; config.target_triple = ""+config.target_triple+"" 1276 set(TARGET_TRIPLE "\"+config.target_triple+\"") 1277 endif() 1278 1279 configure_file(${site_in} ${site_out} @ONLY) 1280 if (EXISTS "${ARG_MAIN_CONFIG}") 1281 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')") 1282 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP) 1283 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}") 1284 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP}) 1285 endif() 1286endfunction() 1287 1288function(dump_all_cmake_variables) 1289 get_cmake_property(_variableNames VARIABLES) 1290 foreach (_variableName ${_variableNames}) 1291 message(STATUS "${_variableName}=${${_variableName}}") 1292 endforeach() 1293endfunction() 1294 1295function(get_llvm_lit_path base_dir file_name) 1296 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN}) 1297 1298 if (ARG_ALLOW_EXTERNAL) 1299 set(LLVM_DEFAULT_EXTERNAL_LIT "${LLVM_EXTERNAL_LIT}") 1300 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit") 1301 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "") 1302 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}") 1303 endif() 1304 1305 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "") 1306 if (EXISTS ${LLVM_EXTERNAL_LIT}) 1307 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME) 1308 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY) 1309 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE) 1310 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE) 1311 return() 1312 else() 1313 message(WARN "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.") 1314 endif() 1315 endif() 1316 endif() 1317 1318 set(lit_file_name "llvm-lit") 1319 if (CMAKE_HOST_WIN32 AND NOT CYGWIN) 1320 # llvm-lit needs suffix.py for multiprocess to find a main module. 1321 set(lit_file_name "${lit_file_name}.py") 1322 endif () 1323 set(${file_name} ${lit_file_name} PARENT_SCOPE) 1324 1325 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR) 1326 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "") 1327 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE) 1328 endif() 1329 1330 # Allow individual projects to provide an override 1331 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "") 1332 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR}) 1333 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "") 1334 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR}) 1335 else() 1336 set(LLVM_LIT_BASE_DIR "") 1337 endif() 1338 1339 # Cache this so we don't have to do it again and have subsequent calls 1340 # potentially disagree on the value. 1341 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR}) 1342 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE) 1343endfunction() 1344 1345# A raw function to create a lit target. This is used to implement the testuite 1346# management functions. 1347function(add_lit_target target comment) 1348 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1349 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}") 1350 separate_arguments(LIT_ARGS) 1351 if (NOT CMAKE_CFG_INTDIR STREQUAL ".") 1352 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR}) 1353 endif () 1354 1355 # Get the path to the lit to *run* tests with. This can be overriden by 1356 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py> 1357 get_llvm_lit_path( 1358 lit_base_dir 1359 lit_file_name 1360 ALLOW_EXTERNAL 1361 ) 1362 1363 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}") 1364 list(APPEND LIT_COMMAND ${LIT_ARGS}) 1365 foreach(param ${ARG_PARAMS}) 1366 list(APPEND LIT_COMMAND --param ${param}) 1367 endforeach() 1368 if (ARG_UNPARSED_ARGUMENTS) 1369 add_custom_target(${target} 1370 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS} 1371 COMMENT "${comment}" 1372 USES_TERMINAL 1373 ) 1374 else() 1375 add_custom_target(${target} 1376 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.") 1377 message(STATUS "${target} does nothing.") 1378 endif() 1379 1380 if (ARG_DEPENDS) 1381 add_dependencies(${target} ${ARG_DEPENDS}) 1382 endif() 1383 1384 # Tests should be excluded from "Build Solution". 1385 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON) 1386endfunction() 1387 1388# A function to add a set of lit test suites to be driven through 'check-*' targets. 1389function(add_lit_testsuite target comment) 1390 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1391 1392 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all. 1393 if(NOT EXCLUDE_FROM_ALL) 1394 # Register the testsuites, params and depends for the global check rule. 1395 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS}) 1396 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS}) 1397 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS}) 1398 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS}) 1399 endif() 1400 1401 # Produce a specific suffixed check rule. 1402 add_lit_target(${target} ${comment} 1403 ${ARG_UNPARSED_ARGUMENTS} 1404 PARAMS ${ARG_PARAMS} 1405 DEPENDS ${ARG_DEPENDS} 1406 ARGS ${ARG_ARGS} 1407 ) 1408endfunction() 1409 1410function(add_lit_testsuites project directory) 1411 if (NOT LLVM_ENABLE_IDE) 1412 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1413 1414 # Search recursively for test directories by assuming anything not 1415 # in a directory called Inputs contains tests. 1416 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*) 1417 foreach(lit_suite ${to_process}) 1418 if(NOT IS_DIRECTORY ${lit_suite}) 1419 continue() 1420 endif() 1421 string(FIND ${lit_suite} Inputs is_inputs) 1422 string(FIND ${lit_suite} Output is_output) 1423 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1)) 1424 continue() 1425 endif() 1426 1427 # Create a check- target for the directory. 1428 string(REPLACE ${directory} "" name_slash ${lit_suite}) 1429 if (name_slash) 1430 string(REPLACE "/" "-" name_slash ${name_slash}) 1431 string(REPLACE "\\" "-" name_dashes ${name_slash}) 1432 string(TOLOWER "${project}${name_dashes}" name_var) 1433 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}" 1434 ${lit_suite} 1435 PARAMS ${ARG_PARAMS} 1436 DEPENDS ${ARG_DEPENDS} 1437 ARGS ${ARG_ARGS} 1438 ) 1439 endif() 1440 endforeach() 1441 endif() 1442endfunction() 1443 1444function(llvm_install_library_symlink name dest type) 1445 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN}) 1446 foreach(path ${CMAKE_MODULE_PATH}) 1447 if(EXISTS ${path}/LLVMInstallSymlink.cmake) 1448 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake) 1449 break() 1450 endif() 1451 endforeach() 1452 1453 set(component ${ARG_COMPONENT}) 1454 if(NOT component) 1455 set(component ${name}) 1456 endif() 1457 1458 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX}) 1459 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX}) 1460 1461 set(output_dir lib${LLVM_LIBDIR_SUFFIX}) 1462 if(WIN32 AND "${type}" STREQUAL "SHARED") 1463 set(output_dir bin) 1464 endif() 1465 1466 install(SCRIPT ${INSTALL_SYMLINK} 1467 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})" 1468 COMPONENT ${component}) 1469 1470 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE) 1471 add_llvm_install_targets(install-${name} 1472 DEPENDS ${name} ${dest} install-${dest} 1473 COMPONENT ${name}) 1474 endif() 1475endfunction() 1476 1477function(llvm_install_symlink name dest) 1478 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN}) 1479 foreach(path ${CMAKE_MODULE_PATH}) 1480 if(EXISTS ${path}/LLVMInstallSymlink.cmake) 1481 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake) 1482 break() 1483 endif() 1484 endforeach() 1485 1486 if(ARG_COMPONENT) 1487 set(component ${ARG_COMPONENT}) 1488 else() 1489 if(ARG_ALWAYS_GENERATE) 1490 set(component ${dest}) 1491 else() 1492 set(component ${name}) 1493 endif() 1494 endif() 1495 1496 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX}) 1497 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX}) 1498 1499 install(SCRIPT ${INSTALL_SYMLINK} 1500 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})" 1501 COMPONENT ${component}) 1502 1503 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE) 1504 add_llvm_install_targets(install-${name} 1505 DEPENDS ${name} ${dest} install-${dest} 1506 COMPONENT ${name}) 1507 endif() 1508endfunction() 1509 1510function(add_llvm_tool_symlink link_name target) 1511 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN}) 1512 set(dest_binary "$<TARGET_FILE:${target}>") 1513 1514 # This got a bit gross... For multi-configuration generators the target 1515 # properties return the resolved value of the string, not the build system 1516 # expression. To reconstruct the platform-agnostic path we have to do some 1517 # magic. First we grab one of the types, and a type-specific path. Then from 1518 # the type-specific path we find the last occurrence of the type in the path, 1519 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type 1520 # agnostic again. 1521 if(NOT ARG_OUTPUT_DIR) 1522 # If you're not overriding the OUTPUT_DIR, we can make the link relative in 1523 # the same directory. 1524 if(CMAKE_HOST_UNIX) 1525 set(dest_binary "$<TARGET_FILE_NAME:${target}>") 1526 endif() 1527 if(CMAKE_CONFIGURATION_TYPES) 1528 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type) 1529 string(TOUPPER ${first_type} first_type_upper) 1530 set(first_type_suffix _${first_type_upper}) 1531 endif() 1532 get_target_property(target_type ${target} TYPE) 1533 if(${target_type} STREQUAL "STATIC_LIBRARY") 1534 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix}) 1535 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY") 1536 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix}) 1537 else() 1538 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix}) 1539 endif() 1540 if(CMAKE_CONFIGURATION_TYPES) 1541 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE) 1542 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix) 1543 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix) 1544 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/" 1545 path_suffix ${path_suffix}) 1546 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix}) 1547 endif() 1548 endif() 1549 1550 if(CMAKE_HOST_UNIX) 1551 set(LLVM_LINK_OR_COPY create_symlink) 1552 else() 1553 set(LLVM_LINK_OR_COPY copy) 1554 endif() 1555 1556 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}") 1557 1558 set(target_name ${link_name}) 1559 if(TARGET ${link_name}) 1560 set(target_name ${link_name}-link) 1561 endif() 1562 1563 1564 if(ARG_ALWAYS_GENERATE) 1565 set_property(DIRECTORY APPEND PROPERTY 1566 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary}) 1567 add_custom_command(TARGET ${target} POST_BUILD 1568 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}") 1569 else() 1570 add_custom_command(OUTPUT ${output_path} 1571 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}" 1572 DEPENDS ${target}) 1573 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path}) 1574 set_target_properties(${target_name} PROPERTIES FOLDER Tools) 1575 1576 # Make sure both the link and target are toolchain tools 1577 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS) 1578 set(TOOL_IS_TOOLCHAIN ON) 1579 endif() 1580 1581 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS) 1582 llvm_install_symlink(${link_name} ${target}) 1583 endif() 1584 endif() 1585endfunction() 1586 1587function(llvm_externalize_debuginfo name) 1588 if(NOT LLVM_EXTERNALIZE_DEBUGINFO) 1589 return() 1590 endif() 1591 1592 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP) 1593 if(APPLE) 1594 if(NOT CMAKE_STRIP) 1595 set(CMAKE_STRIP xcrun strip) 1596 endif() 1597 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>) 1598 else() 1599 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>) 1600 endif() 1601 endif() 1602 1603 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR) 1604 if(APPLE) 1605 set(output_name "$<TARGET_FILE_NAME:${name}>.dSYM") 1606 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}") 1607 endif() 1608 endif() 1609 1610 if(APPLE) 1611 if(CMAKE_CXX_FLAGS MATCHES "-flto" 1612 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto") 1613 1614 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o) 1615 set_property(TARGET ${name} APPEND_STRING PROPERTY 1616 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}") 1617 endif() 1618 if(NOT CMAKE_DSYMUTIL) 1619 set(CMAKE_DSYMUTIL xcrun dsymutil) 1620 endif() 1621 add_custom_command(TARGET ${name} POST_BUILD 1622 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}> 1623 ${strip_command} 1624 ) 1625 else() 1626 add_custom_command(TARGET ${name} POST_BUILD 1627 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug 1628 ${strip_command} -R .gnu_debuglink 1629 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}> 1630 ) 1631 endif() 1632endfunction() 1633 1634# Usage: llvm_codesign(TARGET name [ENTITLEMENTS file]) 1635# 1636# Code-sign the given TARGET with the global LLVM_CODESIGNING_IDENTITY or skip 1637# if undefined. Customize capabilities by passing a file path to ENTITLEMENTS. 1638# 1639function(llvm_codesign) 1640 cmake_parse_arguments(ARG "" "TARGET;ENTITLEMENTS" "" ${ARGN}) 1641 1642 if(NOT LLVM_CODESIGNING_IDENTITY) 1643 return() 1644 endif() 1645 1646 if(APPLE) 1647 if(NOT CMAKE_CODESIGN) 1648 set(CMAKE_CODESIGN xcrun codesign) 1649 endif() 1650 if(NOT CMAKE_CODESIGN_ALLOCATE) 1651 execute_process( 1652 COMMAND xcrun -f codesign_allocate 1653 OUTPUT_STRIP_TRAILING_WHITESPACE 1654 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE 1655 ) 1656 endif() 1657 if(DEFINED ARG_ENTITLEMENTS) 1658 set(PASS_ENTITLEMENTS --entitlements ${ARG_ENTITLEMENTS}) 1659 endif() 1660 1661 add_custom_command( 1662 TARGET ${ARG_TARGET} POST_BUILD 1663 COMMAND ${CMAKE_COMMAND} -E 1664 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE} 1665 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY} 1666 ${PASS_ENTITLEMENTS} $<TARGET_FILE:${ARG_TARGET}> 1667 ) 1668 endif() 1669endfunction() 1670 1671function(llvm_setup_rpath name) 1672 if(CMAKE_INSTALL_RPATH) 1673 return() 1674 endif() 1675 1676 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX)) 1677 set(extra_libdir ${LLVM_LIBRARY_DIR}) 1678 elseif(LLVM_BUILD_LIBRARY_DIR) 1679 set(extra_libdir ${LLVM_LIBRARY_DIR}) 1680 endif() 1681 1682 if (APPLE) 1683 set(_install_name_dir INSTALL_NAME_DIR "@rpath") 1684 set(_install_rpath "@loader_path/../lib" ${extra_libdir}) 1685 elseif(UNIX) 1686 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) 1687 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)") 1688 set_property(TARGET ${name} APPEND_STRING PROPERTY 1689 LINK_FLAGS " -Wl,-z,origin ") 1690 endif() 1691 if(LLVM_LINKER_IS_GNULD) 1692 # $ORIGIN is not interpreted at link time by ld.bfd 1693 set_property(TARGET ${name} APPEND_STRING PROPERTY 1694 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ") 1695 endif() 1696 else() 1697 return() 1698 endif() 1699 1700 set_target_properties(${name} PROPERTIES 1701 BUILD_WITH_INSTALL_RPATH On 1702 INSTALL_RPATH "${_install_rpath}" 1703 ${_install_name_dir}) 1704endfunction() 1705 1706function(setup_dependency_debugging name) 1707 if(NOT LLVM_DEPENDENCY_DEBUGGING) 1708 return() 1709 endif() 1710 1711 if("intrinsics_gen" IN_LIST ARGN) 1712 return() 1713 endif() 1714 1715 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))") 1716 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))") 1717 1718 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'") 1719 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command}) 1720endfunction() 1721 1722# Figure out if we can track VC revisions. 1723function(find_first_existing_file out_var) 1724 foreach(file ${ARGN}) 1725 if(EXISTS "${file}") 1726 set(${out_var} "${file}" PARENT_SCOPE) 1727 return() 1728 endif() 1729 endforeach() 1730endfunction() 1731 1732macro(find_first_existing_vc_file out_var path) 1733 find_program(git_executable NAMES git git.exe git.cmd) 1734 # Run from a subdirectory to force git to print an absolute path. 1735 execute_process(COMMAND ${git_executable} rev-parse --git-dir 1736 WORKING_DIRECTORY ${path}/cmake 1737 RESULT_VARIABLE git_result 1738 OUTPUT_VARIABLE git_dir 1739 ERROR_QUIET) 1740 if(git_result EQUAL 0) 1741 string(STRIP "${git_dir}" git_dir) 1742 set(${out_var} "${git_dir}/logs/HEAD") 1743 # some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD 1744 if (NOT EXISTS "${git_dir}/logs/HEAD") 1745 file(WRITE "${git_dir}/logs/HEAD" "") 1746 endif() 1747 else() 1748 find_first_existing_file(${out_var} 1749 "${path}/.svn/wc.db" # SVN 1.7 1750 "${path}/.svn/entries" # SVN 1.6 1751 ) 1752 endif() 1753endmacro() 1754