1 include(GNUInstallDirs)
2 include(LLVMDistributionSupport)
3 include(LLVMProcessSources)
5 include(DetermineGCCCompatible)
7 function(llvm_update_compile_flags name)
8 get_property(sources TARGET ${name} PROPERTY SOURCES)
9 if("${sources}" MATCHES "\\.c(;|$)")
10 set(update_src_props ON)
13 list(APPEND LLVM_COMPILE_CFLAGS " ${LLVM_COMPILE_FLAGS}")
15 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to
17 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH)
18 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
19 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
20 set(LLVM_REQUIRES_RTTI ON)
23 list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
26 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
27 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
28 if(NOT LLVM_ENABLE_UNWIND_TABLES)
29 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables")
30 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables")
33 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
34 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
35 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
36 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh")
40 # LLVM_REQUIRES_RTTI is an internal flag that individual
41 # targets can use to force RTTI
42 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
43 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
44 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
45 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
46 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
47 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
49 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
50 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
51 list(APPEND LLVM_COMPILE_FLAGS "-qnortti")
54 list(APPEND LLVM_COMPILE_FLAGS "/GR")
58 # - LLVM_COMPILE_FLAGS is list.
59 # - PROPERTY COMPILE_FLAGS is string.
60 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
61 string(REPLACE ";" " " target_compile_cflags " ${LLVM_COMPILE_CFLAGS}")
64 foreach(fn ${sources})
65 get_filename_component(suf ${fn} EXT)
66 if("${suf}" STREQUAL ".cpp")
67 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
68 COMPILE_FLAGS "${target_compile_flags}")
70 if("${suf}" STREQUAL ".c")
71 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
72 COMPILE_FLAGS "${target_compile_cflags}")
76 # Update target props, since all sources are C++.
77 set_property(TARGET ${name} APPEND_STRING PROPERTY
78 COMPILE_FLAGS "${target_compile_flags}")
81 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
84 function(add_llvm_symbol_exports target_name export_file)
85 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
86 set(native_export_file "${target_name}.exports")
87 add_custom_command(OUTPUT ${native_export_file}
88 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
89 DEPENDS ${export_file}
91 COMMENT "Creating export file for ${target_name}")
92 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
93 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
94 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
95 # FIXME: `-Wl,-bE:` bypasses whatever handling there is in the build
96 # compiler driver to defer to the specified export list.
97 set(native_export_file "${export_file}")
98 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
99 LINK_FLAGS " -Wl,-bE:${export_file}")
100 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
101 # Gold and BFD ld require a version script rather than a plain list.
102 set(native_export_file "${target_name}.exports")
103 # FIXME: Don't write the "local:" line on OpenBSD.
104 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
105 add_custom_command(OUTPUT ${native_export_file}
106 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file}
107 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
108 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
109 COMMAND echo " local: *;" >> ${native_export_file}
110 COMMAND echo "};" >> ${native_export_file}
111 DEPENDS ${export_file}
113 COMMENT "Creating export file for ${target_name}")
114 if (${LLVM_LINKER_IS_SOLARISLD})
115 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
116 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
118 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
119 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
122 set(native_export_file "${target_name}.def")
124 add_custom_command(OUTPUT ${native_export_file}
125 COMMAND "${Python3_EXECUTABLE}" -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
126 < ${export_file} > ${native_export_file}
127 DEPENDS ${export_file}
129 COMMENT "Creating export file for ${target_name}")
130 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
132 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
134 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
135 LINK_FLAGS " ${export_file_linker_flag}")
138 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
139 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
141 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
143 get_filename_component(extension ${src} EXT)
144 if(extension STREQUAL ".cpp")
145 set(first_source_file ${src})
150 # Force re-linking when the exports file changes. Actually, it
151 # forces recompilation of the source file. The LINK_DEPENDS target
152 # property only works for makefile-based generators.
153 # FIXME: This is not safe because this will create the same target
154 # ${native_export_file} in several different file:
155 # - One where we emitted ${target_name}_exports
156 # - One where we emitted the build command for the following object.
157 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
158 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
160 set_property(DIRECTORY APPEND
161 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
163 add_dependencies(${target_name} ${target_name}_exports)
165 # Add dependency to *_exports later -- CMake issue 14747
166 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
167 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
168 endfunction(add_llvm_symbol_exports)
170 if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
171 # Detect what linker we have here.
173 # Linkers with ld64-compatible flags.
174 set(version_flag "-Wl,-v")
176 # Linkers with BFD ld-compatible flags.
177 set(version_flag "-Wl,--version")
180 if (CMAKE_HOST_WIN32)
183 set(DEVNULL "/dev/null")
187 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} ${version_flag} -o ${DEVNULL})
189 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
190 set(command ${CMAKE_C_COMPILER} ${flags} ${version_flag} -o ${DEVNULL})
194 OUTPUT_VARIABLE stdout
195 ERROR_VARIABLE stderr
199 if("${stderr}" MATCHES "PROJECT:ld64")
200 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
201 set(LLVM_LINKER_IS_LD64 YES CACHE INTERNAL "")
202 message(STATUS "Linker detection: ld64")
203 elseif("${stderr}" MATCHES "^LLD" OR
204 "${stdout}" MATCHES "^LLD")
205 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
206 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "")
207 message(STATUS "Linker detection: lld")
209 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
210 message(STATUS "Linker detection: unknown")
213 if("${stdout}" MATCHES "^mold")
214 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
215 message(STATUS "Linker detection: mold")
216 elseif("${stdout}" MATCHES "GNU gold")
217 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
218 set(LLVM_LINKER_IS_GOLD YES CACHE INTERNAL "")
219 message(STATUS "Linker detection: GNU Gold")
220 elseif("${stdout}" MATCHES "^LLD")
221 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
222 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "")
223 message(STATUS "Linker detection: LLD")
224 elseif("${stdout}" MATCHES "GNU ld")
225 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
226 set(LLVM_LINKER_IS_GNULD YES CACHE INTERNAL "")
227 message(STATUS "Linker detection: GNU ld")
228 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
229 "${stdout}" MATCHES "Solaris Link Editors")
230 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
231 set(LLVM_LINKER_IS_SOLARISLD YES CACHE INTERNAL "")
232 message(STATUS "Linker detection: Solaris ld")
234 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
235 message(STATUS "Linker detection: unknown")
240 function(add_link_opts target_name)
241 get_llvm_distribution(${target_name} in_distribution in_distribution_var)
242 if(NOT in_distribution)
243 # Don't LTO optimize targets that aren't part of any distribution.
245 # We may consider avoiding LTO altogether by using -fembed-bitcode
246 # and teaching the linker to select machine code from .o files, see
247 # https://lists.llvm.org/pipermail/llvm-dev/2021-April/149843.html
248 if((UNIX OR MINGW) AND LINKER_IS_LLD)
249 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
250 LINK_FLAGS " -Wl,--lto-O0")
251 elseif(LINKER_IS_LLD_LINK)
252 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
253 LINK_FLAGS " /opt:lldlto=0")
254 elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
255 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
256 LINK_FLAGS " -Wl,-mllvm,-O0")
261 # Don't use linker optimizations in debug builds since it slows down the
262 # linker in a context where the optimizations are not important.
263 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
264 if(NOT LLVM_NO_DEAD_STRIP)
265 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
266 # ld64's implementation of -dead_strip breaks tools that use plugins.
267 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
268 LINK_FLAGS " -Wl,-dead_strip")
269 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
270 # Support for ld -z discard-unused=sections was only added in
272 include(LLVMCheckLinkerFlag)
273 llvm_check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
274 if (LINKER_SUPPORTS_Z_DISCARD_UNUSED)
275 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
276 LINK_FLAGS " -Wl,-z,discard-unused=sections")
278 elseif(NOT MSVC AND NOT CMAKE_SYSTEM_NAME MATCHES "AIX|OS390")
279 # TODO Revisit this later on z/OS.
280 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
281 LINK_FLAGS " -Wl,--gc-sections")
283 else() #LLVM_NO_DEAD_STRIP
284 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
285 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
286 LINK_FLAGS " -Wl,-bnogc")
291 if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
292 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
293 LINK_FLAGS " -Wl,-brtl")
295 endfunction(add_link_opts)
297 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
298 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
299 # or a certain builder, for eaxample, msbuild.exe, would be confused.
300 function(set_output_directory target)
301 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
303 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
304 # It affects output of add_library(MODULE).
307 set(module_dir ${ARG_BINARY_DIR})
309 set(module_dir ${ARG_LIBRARY_DIR})
311 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
312 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
313 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
315 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
316 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
319 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
320 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
323 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
324 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
329 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
332 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
335 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
340 # If on Windows and building with MSVC, add the resource script containing the
341 # VERSIONINFO data to the project. This embeds version resource information
342 # into the output .exe or .dll.
343 # TODO: Enable for MinGW Windows builds too.
345 function(add_windows_version_resource_file OUT_VAR)
347 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
348 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
349 if(EXISTS ${resource_file})
350 set(sources ${sources} ${resource_file})
351 source_group("Resource Files" ${resource_file})
352 set(windows_resource_file ${resource_file} PARENT_SCOPE)
354 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
356 set(${OUT_VAR} ${sources} PARENT_SCOPE)
357 endfunction(add_windows_version_resource_file)
359 # set_windows_version_resource_properties(name resource_file...
361 # Optional major version number (defaults to LLVM_VERSION_MAJOR)
363 # Optional minor version number (defaults to LLVM_VERSION_MINOR)
364 # VERSION_PATCHLEVEL int
365 # Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
367 # Optional version string (defaults to PACKAGE_VERSION)
369 # Optional product name string (defaults to "LLVM")
371 function(set_windows_version_resource_properties name resource_file)
372 cmake_parse_arguments(ARG
374 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
378 if (NOT DEFINED ARG_VERSION_MAJOR)
379 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
382 if (NOT DEFINED ARG_VERSION_MINOR)
383 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
386 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
387 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
390 if (NOT DEFINED ARG_VERSION_STRING)
391 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
394 if (NOT DEFINED ARG_PRODUCT_NAME)
395 set(ARG_PRODUCT_NAME "LLVM")
398 set_property(SOURCE ${resource_file}
399 PROPERTY COMPILE_FLAGS /nologo)
400 set_property(SOURCE ${resource_file}
401 PROPERTY COMPILE_DEFINITIONS
402 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
403 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
404 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
405 "RC_VERSION_FIELD_4=0"
406 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
407 "RC_INTERNAL_NAME=\"${name}\""
408 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
409 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
410 endfunction(set_windows_version_resource_properties)
412 # llvm_add_library(name sources...
414 # STATIC by default w/o BUILD_SHARED_LIBS.
415 # SHARED by default w/ BUILD_SHARED_LIBS.
417 # Also create an OBJECT library target. Default if STATIC && SHARED.
419 # Target ${name} might not be created on unsupported platforms.
420 # Check with "if(TARGET ${name})".
421 # DISABLE_LLVM_LINK_LLVM_DYLIB
422 # Do not link this library to libLLVM, even if
423 # LLVM_LINK_LLVM_DYLIB is enabled.
425 # Corresponds to OUTPUT_NAME in target properties.
427 # Same semantics as add_dependencies().
428 # LINK_COMPONENTS components...
429 # Same as the variable LLVM_LINK_COMPONENTS.
430 # LINK_LIBS lib_targets...
431 # Same semantics as target_link_libraries().
433 # May specify header files for IDE generators.
435 # Should set SONAME link flags and create symlinks
437 # Suppress default RPATH settings in shared libraries.
439 # The tool (i.e. cmake target) that this plugin will link against
441 # This is used to specify that this is a component library of
442 # LLVM which means that the source resides in llvm/lib/ and it is a
443 # candidate for inclusion into libLLVM.so.
445 function(llvm_add_library name)
446 cmake_parse_arguments(ARG
447 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB"
448 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
449 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
451 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
452 if(ARG_ADDITIONAL_HEADERS)
453 # Pass through ADDITIONAL_HEADERS.
454 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
457 set(ALL_FILES ${ARG_OBJLIBS})
459 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
463 if(ARG_SHARED OR ARG_STATIC)
464 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
466 # Plugins that link against a tool are allowed even when plugins in general are not
467 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
468 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
473 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
475 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
484 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
485 # Generate an obj library for both targets.
486 set(obj_name "obj.${name}")
487 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
490 llvm_update_compile_flags(${obj_name})
491 if(CMAKE_GENERATOR STREQUAL "Xcode")
492 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
493 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
494 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
496 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
498 # Do add_dependencies(obj) later due to CMake issue 14747.
499 list(APPEND objlibs ${obj_name})
501 # Bring in the target include directories from our original target.
502 target_include_directories(${obj_name} PRIVATE $<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>)
504 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
506 add_dependencies(${obj_name} ${ARG_DEPENDS})
508 # Treat link libraries like PUBLIC dependencies. LINK_LIBS might
509 # result in generating header files. Add a dependendency so that
510 # the generated header is created before this object library.
512 cmake_parse_arguments(LINK_LIBS_ARG
517 foreach(link_lib ${LINK_LIBS_ARG_PUBLIC})
519 # Can't specify a dependence on -lpthread
520 if(NOT ${link_lib} STREQUAL ${LLVM_PTHREAD_LIB})
521 add_dependencies(${obj_name} ${link_lib})
524 add_dependencies(${obj_name} ${link_lib})
530 if(ARG_SHARED AND ARG_STATIC)
532 set(name_static "${name}_static")
534 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
536 # DEPENDS has been appended to LLVM_COMMON_LIBS.
537 llvm_add_library(${name_static} STATIC
539 OBJLIBS ${ALL_FILES} # objlib
540 LINK_LIBS ${ARG_LINK_LIBS}
541 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
544 # Bring in the target link info from our original target.
545 target_link_directories(${name_static} PRIVATE $<TARGET_PROPERTY:${name},LINK_DIRECTORIES>)
546 target_link_libraries(${name_static} PRIVATE $<TARGET_PROPERTY:${name},LINK_LIBRARIES>)
548 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
553 add_library(${name} MODULE ${ALL_FILES})
555 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
556 add_library(${name} SHARED ${ALL_FILES})
558 add_library(${name} STATIC ${ALL_FILES})
561 if(ARG_COMPONENT_LIB)
562 set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE)
563 set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name})
566 if(NOT ARG_NO_INSTALL_RPATH)
567 if(ARG_MODULE OR ARG_SHARED)
568 llvm_setup_rpath(${name})
572 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
574 if(DEFINED windows_resource_file)
575 set_windows_version_resource_properties(${name} ${windows_resource_file})
576 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
579 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
580 # $<TARGET_OBJECTS> doesn't require compile flags.
582 llvm_update_compile_flags(${name})
584 add_link_opts( ${name} )
586 set_target_properties(${name}
588 OUTPUT_NAME ${ARG_OUTPUT_NAME}
593 set_target_properties(${name} PROPERTIES
595 SUFFIX ${LLVM_PLUGIN_EXT}
601 set_target_properties(${name} PROPERTIES
606 # Set SOVERSION on shared libraries that lack explicit SONAME
607 # specifier, on *nix systems that are not Darwin.
608 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
609 set_target_properties(${name}
611 # Since 4.0.0, the ABI version is indicated by the major version
612 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
613 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
617 if(ARG_MODULE OR ARG_SHARED)
618 # Do not add -Dname_EXPORTS to the command-line when building files in this
619 # target. Doing so is actively harmful for the modules build because it
620 # creates extra module variants, and not useful because we don't use these
622 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
624 if (LLVM_EXPORTED_SYMBOL_FILE)
625 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
630 if(NOT APPLE AND ARG_SONAME)
631 get_target_property(output_name ${name} OUTPUT_NAME)
632 if(${output_name} STREQUAL "output_name-NOTFOUND")
633 set(output_name ${name})
635 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
636 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
637 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
639 llvm_install_library_symlink(${api_name} ${library_name} SHARED
641 llvm_install_library_symlink(${output_name} ${library_name} SHARED
650 # We can use PRIVATE since SO knows its dependent libs.
654 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
655 # On DLL platforms symbols are imported from the tool by linking against it.
656 set(llvm_libs ${ARG_PLUGIN_TOOL})
657 elseif (NOT ARG_COMPONENT_LIB)
658 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
661 llvm_map_components_to_libnames(llvm_libs
662 ${ARG_LINK_COMPONENTS}
663 ${LLVM_LINK_COMPONENTS}
667 # Components have not been defined explicitly in CMake, so add the
668 # dependency information for this library through their name, and let
669 # LLVMBuildResolveComponentsLink resolve the mapping.
671 # It would be nice to verify that we have the dependencies for this library
672 # name, but using get_property(... SET) doesn't suffice to determine if a
673 # property has been set to an empty value.
674 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS})
676 # This property is an internal property only used to make sure the
677 # link step applied in LLVMBuildResolveComponentsLink uses the same
678 # property as the target_link_libraries call below.
679 set_property(TARGET ${name} PROPERTY LLVM_LIBTYPE ${libtype})
682 target_link_libraries(${name} ${libtype}
688 if(LLVM_COMMON_DEPENDS)
689 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
690 # Add dependencies also to objlibs.
691 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
692 foreach(objlib ${objlibs})
693 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
697 if(ARG_SHARED OR ARG_MODULE)
698 llvm_externalize_debuginfo(${name})
699 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
701 # clang and newer versions of ninja use high-resolutions timestamps,
702 # but older versions of libtool on Darwin don't, so the archive will
703 # often get an older timestamp than the last object that was added
704 # or updated. To fix this, we add a custom command to touch archive
705 # after it's been built so that ninja won't rebuild it unnecessarily
706 # the next time it's run.
707 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
708 add_custom_command(TARGET ${name}
710 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
715 function(add_llvm_install_targets target)
716 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN})
718 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
721 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
724 set(file_dependencies)
725 set(target_dependencies)
726 foreach(dependency ${ARG_DEPENDS})
727 if(TARGET ${dependency})
728 list(APPEND target_dependencies ${dependency})
730 list(APPEND file_dependencies ${dependency})
734 add_custom_target(${target}
735 DEPENDS ${file_dependencies}
736 COMMAND "${CMAKE_COMMAND}"
739 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
741 set_target_properties(${target} PROPERTIES FOLDER "Component Install Targets")
742 add_custom_target(${target}-stripped
743 DEPENDS ${file_dependencies}
744 COMMAND "${CMAKE_COMMAND}"
747 -DCMAKE_INSTALL_DO_STRIP=1
748 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
750 set_target_properties(${target}-stripped PROPERTIES FOLDER "Component Install Targets (Stripped)")
751 if(target_dependencies)
752 add_dependencies(${target} ${target_dependencies})
753 add_dependencies(${target}-stripped ${target_dependencies})
757 add_dependencies(${target} install-${ARG_SYMLINK})
758 add_dependencies(${target}-stripped install-${ARG_SYMLINK}-stripped)
762 # Define special targets that behave like a component group. They don't have any
763 # source attached but other components can add themselves to them. If the
764 # component supports is a Target and it supports JIT compilation, HAS_JIT must
765 # be passed. One can use ADD_TO_COMPONENT option from add_llvm_component_library
766 # to link extra component into an existing group.
767 function(add_llvm_component_group name)
768 cmake_parse_arguments(ARG "HAS_JIT" "" "LINK_COMPONENTS" ${ARGN})
769 add_custom_target(${name})
771 set_property(TARGET ${name} PROPERTY COMPONENT_HAS_JIT ON)
773 if(ARG_LINK_COMPONENTS)
774 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
778 # An LLVM component is a cmake target with the following cmake properties
780 # - LLVM_COMPONENT_NAME: the name of the component, which can be the name of
781 # the associated library or the one specified through COMPONENT_NAME
782 # - LLVM_LINK_COMPONENTS: a list of component this component depends on
783 # - COMPONENT_HAS_JIT: (only for group component) whether this target group
784 # supports JIT compilation
785 # Additionnaly, the ADD_TO_COMPONENT <component> option make it possible to add this
786 # component to the LLVM_LINK_COMPONENTS of <component>.
787 function(add_llvm_component_library name)
788 cmake_parse_arguments(ARG
790 "COMPONENT_NAME;ADD_TO_COMPONENT"
793 add_llvm_library(${name} COMPONENT_LIB ${ARG_UNPARSED_ARGUMENTS})
794 string(REGEX REPLACE "^LLVM" "" component_name ${name})
795 set_property(TARGET ${name} PROPERTY LLVM_COMPONENT_NAME ${component_name})
797 if(ARG_COMPONENT_NAME)
798 set_property(GLOBAL PROPERTY LLVM_COMPONENT_NAME_${ARG_COMPONENT_NAME} ${component_name})
801 if(ARG_ADD_TO_COMPONENT)
802 set_property(TARGET ${ARG_ADD_TO_COMPONENT} APPEND PROPERTY LLVM_LINK_COMPONENTS ${component_name})
807 macro(add_llvm_library name)
808 cmake_parse_arguments(ARG
809 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
814 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
815 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
816 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
818 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
821 # Libraries that are meant to only be exposed via the build tree only are
822 # never installed and are only exported as a target in the special build tree
824 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
825 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
826 set(in_llvm_libs YES)
829 if (ARG_MODULE AND NOT TARGET ${name})
830 # Add empty "phony" target
831 add_custom_target(${name})
832 elseif( EXCLUDE_FROM_ALL )
833 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
834 elseif(ARG_BUILDTREE_ONLY)
835 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
837 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
839 set(umbrella UMBRELLA llvm-libraries)
844 get_target_export_arg(${name} LLVM export_to_llvmexports ${umbrella})
845 install(TARGETS ${name}
846 ${export_to_llvmexports}
847 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
848 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
849 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT ${name})
851 if (NOT LLVM_ENABLE_IDE)
852 add_llvm_install_targets(install-${name}
857 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
860 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
862 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
864 endmacro(add_llvm_library name)
866 macro(generate_llvm_objects name)
867 cmake_parse_arguments(ARG "GENERATE_DRIVER" "" "DEPENDS" ${ARGN})
869 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
871 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
874 if(LLVM_ENABLE_OBJLIB OR (ARG_GENERATE_DRIVER AND LLVM_TOOL_LLVM_DRIVER_BUILD))
875 # Generate an obj library for both targets.
876 set(obj_name "obj.${name}")
877 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
880 llvm_update_compile_flags(${obj_name})
881 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
883 add_dependencies(${obj_name} ${ARG_DEPENDS})
886 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
889 if (ARG_GENERATE_DRIVER)
890 string(REPLACE "-" "_" TOOL_NAME ${name})
891 foreach(path ${CMAKE_MODULE_PATH})
892 if(EXISTS ${path}/llvm-driver-template.cpp.in)
894 ${path}/llvm-driver-template.cpp.in
895 ${CMAKE_CURRENT_BINARY_DIR}/${name}-driver.cpp)
900 list(APPEND ALL_FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}-driver.cpp)
902 if (LLVM_TOOL_LLVM_DRIVER_BUILD)
903 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_COMPONENTS ${LLVM_LINK_COMPONENTS})
904 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_DEPS ${ARG_DEPENDS} ${LLVM_COMMON_DEPENDS})
905 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_OBJLIBS "${obj_name}")
907 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOLS ${name})
908 target_link_libraries(${obj_name} ${LLVM_PTHREAD_LIB})
909 llvm_config(${obj_name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
914 macro(add_llvm_executable name)
915 cmake_parse_arguments(ARG
916 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS"
917 "ENTITLEMENTS;BUNDLE_PATH"
920 generate_llvm_objects(${name} ${ARG_UNPARSED_ARGUMENTS})
921 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
924 # Note: the dummy.cpp source file provides no definitions. However,
925 # it forces Xcode to properly link the static library.
926 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
929 if( EXCLUDE_FROM_ALL )
930 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
932 add_executable(${name} ${ALL_FILES})
935 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
937 if(NOT ARG_NO_INSTALL_RPATH)
938 llvm_setup_rpath(${name})
939 elseif(NOT "${LLVM_LOCAL_RPATH}" STREQUAL "")
940 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set.
941 if("${CMAKE_BUILD_RPATH}" STREQUAL "")
942 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
945 set_property(TARGET ${name} PROPERTY INSTALL_RPATH "${LLVM_LOCAL_RPATH}")
948 if(DEFINED windows_resource_file)
949 set_windows_version_resource_properties(${name} ${windows_resource_file})
952 # $<TARGET_OBJECTS> doesn't require compile flags.
953 if(NOT LLVM_ENABLE_OBJLIB)
954 llvm_update_compile_flags(${name})
957 if (ARG_SUPPORT_PLUGINS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
958 set(LLVM_NO_DEAD_STRIP On)
961 add_link_opts( ${name} )
963 # Do not add -Dname_EXPORTS to the command-line when building files in this
964 # target. Doing so is actively harmful for the modules build because it
965 # creates extra module variants, and not useful because we don't use these
967 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
969 if (LLVM_EXPORTED_SYMBOL_FILE)
970 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
971 endif(LLVM_EXPORTED_SYMBOL_FILE)
973 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
974 set(USE_SHARED USE_SHARED)
977 set(EXCLUDE_FROM_ALL OFF)
978 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
979 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
980 if( LLVM_COMMON_DEPENDS )
981 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
982 foreach(objlib ${obj_name})
983 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
985 endif( LLVM_COMMON_DEPENDS )
987 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
988 llvm_externalize_debuginfo(${name})
990 if (LLVM_PTHREAD_LIB)
991 # libpthreads overrides some standard library symbols, so main
992 # executable must be linked with it in order to provide consistent
993 # API for all shared libaries loaded by this executable.
994 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
997 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
998 endmacro(add_llvm_executable name)
1000 # add_llvm_pass_plugin(name [NO_MODULE] ...)
1001 # Add ${name} as an llvm plugin.
1002 # If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically.
1003 # Otherwise a pluggable shared library is registered.
1005 # If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF,
1006 # only an object library is built, and no module is built. This is specific to the Polly use case.
1008 # The SUBPROJECT argument contains the LLVM project the plugin belongs
1009 # to. If set, the plugin will link statically by default it if the
1010 # project was enabled.
1011 function(add_llvm_pass_plugin name)
1012 cmake_parse_arguments(ARG
1013 "NO_MODULE" "SUBPROJECT" ""
1016 string(TOUPPER ${name} name_upper)
1018 # Enable the plugin by default if it was explicitly enabled by the user.
1019 # Note: If was set to "all", LLVM's CMakeLists.txt replaces it with a
1020 # list of all projects, counting as explicitly enabled.
1021 set(link_into_tools_default OFF)
1022 if (ARG_SUBPROJECT AND LLVM_TOOL_${name_upper}_BUILD)
1023 set(link_into_tools_default ON)
1025 option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" ${link_into_tools_default})
1027 # If we statically link the plugin, don't use llvm dylib because we're going
1029 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
1030 list(APPEND ARG_UNPARSED_ARGUMENTS DISABLE_LLVM_LINK_LLVM_DYLIB)
1033 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
1034 list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY)
1035 # process_llvm_pass_plugins takes care of the actual linking, just create an
1036 # object library as of now
1037 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
1038 target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS)
1039 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS)
1040 if (TARGET intrinsics_gen)
1041 add_dependencies(obj.${name} intrinsics_gen)
1044 add_dependencies(obj.${name} omp_gen)
1047 add_dependencies(obj.${name} acc_gen)
1049 set_property(GLOBAL APPEND PROPERTY LLVM_STATIC_EXTENSIONS ${name})
1050 elseif(NOT ARG_NO_MODULE)
1051 add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
1053 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
1055 message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})")
1057 endfunction(add_llvm_pass_plugin)
1059 # process_llvm_pass_plugins([GEN_CONFIG])
1061 # Correctly set lib dependencies between plugins and tools, based on tools
1062 # registered with the ENABLE_PLUGINS option.
1064 # if GEN_CONFIG option is set, also generate X Macro file for extension
1065 # handling. It provides a HANDLE_EXTENSION(extension_namespace, ExtensionProject)
1066 # call for each extension allowing client code to define
1067 # HANDLE_EXTENSION to have a specific code be run for each extension.
1069 function(process_llvm_pass_plugins)
1070 cmake_parse_arguments(ARG
1075 get_property(LLVM_STATIC_EXTENSIONS GLOBAL PROPERTY LLVM_STATIC_EXTENSIONS)
1077 include(LLVMConfigExtensions)
1080 # Add static plugins to the Extension component
1081 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1082 set_property(TARGET LLVMExtensions APPEND PROPERTY LINK_LIBRARIES ${llvm_extension})
1083 set_property(TARGET LLVMExtensions APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension})
1086 # Eventually generate the extension headers, and store config to a cmake file
1087 # for usage in third-party configuration.
1090 ## Part 1: Extension header to be included whenever we need extension
1092 if(NOT DEFINED LLVM_INSTALL_PACKAGE_DIR)
1093 message(FATAL_ERROR "LLVM_INSTALL_PACKAGE_DIR must be defined and writable. GEN_CONFIG should only be passe when building LLVM proper.")
1095 # LLVM_INSTALL_PACKAGE_DIR might be absolute, so don't reuse below.
1096 set(llvm_cmake_builddir "${LLVM_LIBRARY_DIR}/cmake/llvm")
1098 "${llvm_cmake_builddir}/LLVMConfigExtensions.cmake"
1099 "set(LLVM_STATIC_EXTENSIONS ${LLVM_STATIC_EXTENSIONS})")
1101 ${llvm_cmake_builddir}/LLVMConfigExtensions.cmake
1102 DESTINATION ${LLVM_INSTALL_PACKAGE_DIR}
1103 COMPONENT cmake-exports)
1105 set(ExtensionDef "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def")
1106 file(WRITE "${ExtensionDef}.tmp" "//extension handlers\n")
1107 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1108 file(APPEND "${ExtensionDef}.tmp" "HANDLE_EXTENSION(${llvm_extension})\n")
1110 file(APPEND "${ExtensionDef}.tmp" "#undef HANDLE_EXTENSION\n")
1112 # only replace if there's an actual change
1113 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
1114 "${ExtensionDef}.tmp"
1116 file(REMOVE "${ExtensionDef}.tmp")
1118 ## Part 2: Extension header that captures each extension dependency, to be
1119 # used by llvm-config.
1120 set(ExtensionDeps "${LLVM_BINARY_DIR}/tools/llvm-config/ExtensionDependencies.inc")
1122 # Max needed to correctly size the required library array.
1123 set(llvm_plugin_max_deps_length 0)
1124 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1125 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
1126 list(LENGTH llvm_plugin_deps llvm_plugin_deps_length)
1127 if(llvm_plugin_deps_length GREATER llvm_plugin_max_deps_length)
1128 set(llvm_plugin_max_deps_length ${llvm_plugin_deps_length})
1132 list(LENGTH LLVM_STATIC_EXTENSIONS llvm_static_extension_count)
1134 "${ExtensionDeps}.tmp"
1135 "#include <array>\n\
1136 struct ExtensionDescriptor {\n\
1137 const char* Name;\n\
1138 const char* RequiredLibraries[1 + 1 + ${llvm_plugin_max_deps_length}];\n\
1140 std::array<ExtensionDescriptor, ${llvm_static_extension_count}> AvailableExtensions{\n")
1142 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1143 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
1145 file(APPEND "${ExtensionDeps}.tmp" "ExtensionDescriptor{\"${llvm_extension}\", {")
1146 foreach(llvm_plugin_dep ${llvm_plugin_deps})
1147 # Turn library dependency back to component name, if possible.
1148 # That way llvm-config can avoid redundant dependencies.
1149 STRING(REGEX REPLACE "^-l" "" plugin_dep_name ${llvm_plugin_dep})
1150 STRING(REGEX MATCH "^LLVM" is_llvm_library ${plugin_dep_name})
1152 STRING(REGEX REPLACE "^LLVM" "" plugin_dep_name ${plugin_dep_name})
1153 STRING(TOLOWER ${plugin_dep_name} plugin_dep_name)
1155 file(APPEND "${ExtensionDeps}.tmp" "\"${plugin_dep_name}\", ")
1158 # Self + mandatory trailing null, because the number of RequiredLibraries differs between extensions.
1159 file(APPEND "${ExtensionDeps}.tmp" \"${llvm_extension}\", "nullptr}},\n")
1161 file(APPEND "${ExtensionDeps}.tmp" "};\n")
1163 # only replace if there's an actual change
1164 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
1165 "${ExtensionDeps}.tmp"
1167 file(REMOVE "${ExtensionDeps}.tmp")
1171 function(export_executable_symbols target)
1172 if (LLVM_EXPORTED_SYMBOL_FILE)
1173 # The symbol file should contain the symbols we want the executable to
1175 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1176 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
1177 # Extract the symbols to export from the static libraries that the
1178 # executable links against.
1179 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1180 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
1181 # We need to consider not just the direct link dependencies, but also the
1182 # transitive link dependencies. Do this by starting with the set of direct
1183 # dependencies, then the dependencies of those dependencies, and so on.
1184 get_target_property(new_libs ${target} LINK_LIBRARIES)
1185 set(link_libs ${new_libs})
1186 while(NOT "${new_libs}" STREQUAL "")
1187 foreach(lib ${new_libs})
1189 get_target_property(lib_type ${lib} TYPE)
1190 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
1191 list(APPEND static_libs ${lib})
1193 list(APPEND other_libs ${lib})
1195 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
1196 foreach(transitive_lib ${transitive_libs})
1197 list(FIND link_libs ${transitive_lib} idx)
1198 if(TARGET ${transitive_lib} AND idx EQUAL -1)
1199 list(APPEND newer_libs ${transitive_lib})
1200 list(APPEND link_libs ${transitive_lib})
1202 endforeach(transitive_lib)
1205 set(new_libs ${newer_libs})
1208 list(REMOVE_DUPLICATES static_libs)
1210 set(mangling microsoft)
1212 set(mangling itanium)
1214 add_custom_command(OUTPUT ${exported_symbol_file}
1215 COMMAND "${Python3_EXECUTABLE}" ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${LLVM_EXTRACT_SYMBOLS_FLAGS} --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
1216 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
1217 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
1219 COMMENT "Generating export list for ${target}")
1220 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
1221 # If something links against this executable then we want a
1222 # transitive link against only the libraries whose symbols
1223 # we aren't exporting.
1224 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
1225 # The default import library suffix that cmake uses for cygwin/mingw is
1226 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
1227 # where the import libraries of both get named libclang.dll.a. Use a suffix
1228 # of ".exe.a" to avoid this.
1230 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
1232 elseif(NOT (WIN32 OR CYGWIN))
1233 # On Windows auto-exporting everything doesn't work because of the limit on
1234 # the size of the exported symbol table, but on other platforms we can do
1235 # it without any trouble.
1236 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1238 set_property(TARGET ${target} APPEND_STRING PROPERTY
1239 LINK_FLAGS " -rdynamic")
1244 # Export symbols if LLVM plugins are enabled.
1245 function(export_executable_symbols_for_plugins target)
1246 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
1247 export_executable_symbols(${target})
1251 if(NOT LLVM_TOOLCHAIN_TOOLS)
1252 set (LLVM_TOOLCHAIN_TOOLS
1271 # symlink version of some of above tools that are enabled by
1272 # LLVM_INSTALL_BINUTILS_SYMLINKS.
1287 macro(llvm_add_tool project name)
1288 cmake_parse_arguments(ARG "DEPENDS;GENERATE_DRIVER" "" "" ${ARGN})
1289 if( NOT LLVM_BUILD_TOOLS )
1290 set(EXCLUDE_FROM_ALL ON)
1292 if(ARG_GENERATE_DRIVER AND LLVM_TOOL_LLVM_DRIVER_BUILD)
1293 generate_llvm_objects(${name} ${ARGN})
1294 add_custom_target(${name} DEPENDS llvm-driver)
1296 add_llvm_executable(${name} ${ARGN})
1298 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1299 if( LLVM_BUILD_TOOLS )
1300 get_target_export_arg(${name} LLVM export_to_llvmexports)
1301 install(TARGETS ${name}
1302 ${export_to_llvmexports}
1303 RUNTIME DESTINATION ${${project}_TOOLS_INSTALL_DIR}
1306 if (NOT LLVM_ENABLE_IDE)
1307 add_llvm_install_targets(install-${name}
1313 if( LLVM_BUILD_TOOLS )
1314 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1316 set_target_properties(${name} PROPERTIES FOLDER "Tools")
1318 endmacro(llvm_add_tool project name)
1320 macro(add_llvm_tool name)
1321 llvm_add_tool(LLVM ${ARGV})
1325 macro(add_llvm_example name)
1326 if( NOT LLVM_BUILD_EXAMPLES )
1327 set(EXCLUDE_FROM_ALL ON)
1329 add_llvm_executable(${name} ${ARGN})
1330 if( LLVM_BUILD_EXAMPLES )
1331 install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
1333 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1334 endmacro(add_llvm_example name)
1336 macro(add_llvm_example_library name)
1337 if( NOT LLVM_BUILD_EXAMPLES )
1338 set(EXCLUDE_FROM_ALL ON)
1339 add_llvm_library(${name} BUILDTREE_ONLY ${ARGN})
1341 add_llvm_library(${name} ${ARGN})
1344 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1345 endmacro(add_llvm_example_library name)
1347 # This is a macro that is used to create targets for executables that are needed
1348 # for development, but that are not intended to be installed by default.
1349 macro(add_llvm_utility name)
1350 if ( NOT LLVM_BUILD_UTILS )
1351 set(EXCLUDE_FROM_ALL ON)
1354 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
1355 set_target_properties(${name} PROPERTIES FOLDER "Utils")
1356 if ( ${name} IN_LIST LLVM_TOOLCHAIN_UTILITIES OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1357 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
1358 get_target_export_arg(${name} LLVM export_to_llvmexports)
1359 install(TARGETS ${name}
1360 ${export_to_llvmexports}
1361 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
1364 if (NOT LLVM_ENABLE_IDE)
1365 add_llvm_install_targets(install-${name}
1369 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1370 elseif(LLVM_BUILD_UTILS)
1371 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
1374 endmacro(add_llvm_utility name)
1376 macro(add_llvm_fuzzer name)
1377 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
1378 if( LLVM_LIB_FUZZING_ENGINE )
1379 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1380 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1381 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
1382 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1383 elseif( LLVM_USE_SANITIZE_COVERAGE )
1384 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
1385 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1386 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1387 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1388 elseif( ARG_DUMMY_MAIN )
1389 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
1390 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1394 macro(add_llvm_target target_name)
1395 include_directories(BEFORE
1396 ${CMAKE_CURRENT_BINARY_DIR}
1397 ${CMAKE_CURRENT_SOURCE_DIR})
1398 add_llvm_component_library(LLVM${target_name} ${ARGN})
1399 set( CURRENT_LLVM_TARGET LLVM${target_name} )
1400 endmacro(add_llvm_target)
1402 function(canonicalize_tool_name name output)
1403 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1404 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1405 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1406 set(${output} "${nameUPPER}" PARENT_SCOPE)
1407 endfunction(canonicalize_tool_name)
1409 # Custom add_subdirectory wrapper
1410 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1411 # path if it differs from the name.
1412 function(add_llvm_subdirectory project type name)
1413 set(add_llvm_external_dir "${ARGN}")
1414 if("${add_llvm_external_dir}" STREQUAL "")
1415 set(add_llvm_external_dir ${name})
1417 canonicalize_tool_name(${name} nameUPPER)
1418 set(canonical_full_name ${project}_${type}_${nameUPPER})
1419 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1420 if(already_processed)
1423 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1425 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1426 # Treat it as in-tree subproject.
1427 option(${canonical_full_name}_BUILD
1428 "Whether to build ${name} as part of ${project}" On)
1429 mark_as_advanced(${project}_${type}_${name}_BUILD)
1430 if(${canonical_full_name}_BUILD)
1431 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
1434 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1435 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1436 CACHE PATH "Path to ${name} source directory")
1437 set(${canonical_full_name}_BUILD_DEFAULT ON)
1438 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1439 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1441 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
1442 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1444 option(${canonical_full_name}_BUILD
1445 "Whether to build ${name} as part of LLVM"
1446 ${${canonical_full_name}_BUILD_DEFAULT})
1447 if (${canonical_full_name}_BUILD)
1448 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1449 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1450 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1451 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1457 # Add external project that may want to be built as part of llvm such as Clang,
1458 # lld, and Polly. This adds two options. One for the source directory of the
1459 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1460 # enable or disable building it with everything else.
1461 # Additional parameter can be specified as the name of directory.
1462 macro(add_llvm_external_project name)
1463 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1466 macro(add_llvm_tool_subdirectory name)
1467 add_llvm_external_project(${name})
1468 endmacro(add_llvm_tool_subdirectory)
1470 function(get_project_name_from_src_var var output)
1471 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1472 MACHED_TOOL "${var}")
1474 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1476 set(${output} PARENT_SCOPE)
1480 function(create_subdirectory_options project type)
1481 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1482 foreach(dir ${sub-dirs})
1483 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1484 canonicalize_tool_name(${dir} name)
1485 option(${project}_${type}_${name}_BUILD
1486 "Whether to build ${name} as part of ${project}" On)
1487 mark_as_advanced(${project}_${type}_${name}_BUILD)
1490 endfunction(create_subdirectory_options)
1492 function(create_llvm_tool_options)
1493 create_subdirectory_options(LLVM TOOL)
1494 endfunction(create_llvm_tool_options)
1496 function(llvm_add_implicit_projects project)
1497 set(list_of_implicit_subdirs "")
1498 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1499 foreach(dir ${sub-dirs})
1500 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1501 canonicalize_tool_name(${dir} name)
1502 # I don't like special casing things by order, but the llvm-driver ends up
1503 # linking the object libraries from all the tools that opt-in, so adding
1504 # it separately at the end is probably the simplest case.
1505 if("${name}" STREQUAL "LLVM_DRIVER")
1508 if (${project}_TOOL_${name}_BUILD)
1509 get_filename_component(fn "${dir}" NAME)
1510 list(APPEND list_of_implicit_subdirs "${fn}")
1515 foreach(external_proj ${list_of_implicit_subdirs})
1516 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1518 endfunction(llvm_add_implicit_projects)
1520 function(add_llvm_implicit_projects)
1521 llvm_add_implicit_projects(LLVM)
1522 endfunction(add_llvm_implicit_projects)
1524 # Generic support for adding a unittest.
1525 function(add_unittest test_suite test_name)
1526 if( NOT LLVM_BUILD_TESTS )
1527 set(EXCLUDE_FROM_ALL ON)
1530 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1531 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1533 # Some parts of gtest rely on this GNU extension, don't warn on it.
1534 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1535 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1538 if (NOT DEFINED LLVM_REQUIRES_RTTI)
1539 set(LLVM_REQUIRES_RTTI OFF)
1542 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1543 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1545 # The runtime benefits of LTO don't outweight the compile time costs for tests.
1547 if((UNIX OR MINGW) AND LINKER_IS_LLD)
1548 set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1549 LINK_FLAGS " -Wl,--lto-O0")
1550 elseif(LINKER_IS_LLD_LINK)
1551 set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1552 LINK_FLAGS " /opt:lldlto=0")
1553 elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
1554 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1555 LINK_FLAGS " -Wl,-mllvm,-O0")
1559 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1560 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1561 # libpthreads overrides some standard library symbols, so main
1562 # executable must be linked with it in order to provide consistent
1563 # API for all shared libaries loaded by this executable.
1564 target_link_libraries(${test_name} PRIVATE llvm_gtest_main llvm_gtest ${LLVM_PTHREAD_LIB})
1566 add_dependencies(${test_suite} ${test_name})
1567 get_target_property(test_suite_folder ${test_suite} FOLDER)
1568 if (test_suite_folder)
1569 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1573 # Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1575 function(add_unittest_with_input_files test_suite test_name)
1576 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1578 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1579 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1581 add_unittest(${test_suite} ${test_name} ${ARGN})
1584 # Generic support for adding a benchmark.
1585 function(add_benchmark benchmark_name)
1586 if( NOT LLVM_BUILD_BENCHMARKS )
1587 set(EXCLUDE_FROM_ALL ON)
1590 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1591 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1592 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1593 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1594 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1597 # This function canonicalize the CMake variables passed by names
1598 # from CMake boolean to 0/1 suitable for passing into Python or C++,
1600 function(llvm_canonicalize_cmake_booleans)
1601 foreach(var ${ARGN})
1603 set(${var} 1 PARENT_SCOPE)
1605 set(${var} 0 PARENT_SCOPE)
1608 endfunction(llvm_canonicalize_cmake_booleans)
1610 macro(set_llvm_build_mode)
1611 # Configuration-time: See Unit/lit.site.cfg.in
1612 if (CMAKE_CFG_INTDIR STREQUAL ".")
1613 set(LLVM_BUILD_MODE ".")
1615 set(LLVM_BUILD_MODE "%(build_mode)s")
1619 # Takes a list of path names in pathlist and a base directory, and returns
1620 # a list of paths relative to the base directory in out_pathlist.
1621 # Paths that are on a different drive than the basedir (on Windows) or that
1622 # contain symlinks are returned absolute.
1623 # Use with LLVM_LIT_PATH_FUNCTION below.
1624 function(make_paths_relative out_pathlist basedir pathlist)
1625 # Passing ARG_PATH_VALUES as-is to execute_process() makes cmake strip
1626 # empty list entries. So escape the ;s in the list and do the splitting
1627 # ourselves. cmake has no relpath function, so use Python for that.
1628 string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}")
1629 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "\n
1633 if not p or p == os.path.dirname(p): return False\n
1634 return os.path.islink(p) or haslink(os.path.dirname(p))\n
1636 if not p: return ''\n
1637 if os.path.splitdrive(p)[0] != os.path.splitdrive(base)[0]: return p\n
1638 if haslink(p) or haslink(base): return p\n
1639 return os.path.relpath(p, base)\n
1640 if len(sys.argv) < 3: sys.exit(0)\n
1641 sys.stdout.write(';'.join(relpath(p) for p in sys.argv[2].split(';')))"
1644 OUTPUT_VARIABLE pathlist_relative
1645 ERROR_VARIABLE error
1646 RESULT_VARIABLE result)
1647 if (NOT result EQUAL 0)
1648 message(FATAL_ERROR "make_paths_relative() failed due to error '${result}', with stderr\n${error}")
1650 set(${out_pathlist} "${pathlist_relative}" PARENT_SCOPE)
1653 # Converts a file that's relative to the current python file to an absolute
1654 # path. Since this uses __file__, it has to be emitted into python files that
1655 # use it and can't be in a lit module. Use with make_paths_relative().
1656 string(CONCAT LLVM_LIT_PATH_FUNCTION
1657 "# Allow generated file to be relocatable.\n"
1658 "from pathlib import Path\n"
1660 " if not p: return ''\n"
1661 " return str((Path(__file__).parent / p).resolve())\n"
1664 # This function provides an automatic way to 'configure'-like generate a file
1665 # based on a set of common and custom variables, specifically targeting the
1666 # variables needed for the 'lit.site.cfg' files. This function bundles the
1667 # common variables that any Lit instance is likely to need, and custom
1668 # variables can be passed in.
1669 # The keyword PATHS is followed by a list of cmake variable names that are
1670 # mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that
1671 # list are treated as paths that are relative to the directory the generated
1672 # lit.cfg.py file is in, and the `path()` function converts the relative
1673 # path back to absolute form. This makes it possible to move a build directory
1674 # containing lit.cfg.py files from one machine to another.
1675 function(configure_lit_site_cfg site_in site_out)
1676 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;PATHS" ${ARGN})
1678 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1679 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1680 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1683 foreach(c ${LLVM_TARGETS_TO_BUILD})
1684 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1686 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1688 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1690 set_llvm_build_mode()
1692 # For standalone builds of subprojects, these might not be the build tree but
1693 # a provided binary tree.
1694 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1695 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1696 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1697 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1698 # Like LLVM_{TOOLS,LIBS}_DIR, but pointing at the build tree.
1699 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" CURRENT_TOOLS_DIR "${LLVM_RUNTIME_OUTPUT_INTDIR}")
1700 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" CURRENT_LIBS_DIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
1701 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1703 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1704 # plugins. We may rename it.
1705 if(LLVM_ENABLE_PLUGINS)
1706 set(ENABLE_SHARED "1")
1708 set(ENABLE_SHARED "0")
1711 if(LLVM_ENABLE_ASSERTIONS)
1712 set(ENABLE_ASSERTIONS "1")
1714 set(ENABLE_ASSERTIONS "0")
1717 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1718 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1720 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1721 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1722 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1724 string(CONCAT LIT_SITE_CFG_IN_HEADER
1725 "# Autogenerated from ${site_in}\n# Do not edit!\n\n"
1726 "${LLVM_LIT_PATH_FUNCTION}"
1729 # Override config_target_triple (and the env)
1730 if(LLVM_TARGET_TRIPLE_ENV)
1731 # This is expanded into the heading.
1732 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}"
1734 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1735 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${LLVM_TARGET_TRIPLE}\")\n"
1738 # This is expanded to; config.target_triple = ""+config.target_triple+""
1739 set(LLVM_TARGET_TRIPLE "\"+config.target_triple+\"")
1743 # Walk ARG_PATHS and collect the current value of the variables in there.
1744 # list(APPEND) ignores empty elements exactly if the list is empty,
1745 # so start the list with a dummy element and drop it, to make sure that
1746 # even empty values make it into the values list.
1747 set(ARG_PATH_VALUES "dummy")
1748 foreach(path ${ARG_PATHS})
1749 list(APPEND ARG_PATH_VALUES "${${path}}")
1751 list(REMOVE_AT ARG_PATH_VALUES 0)
1753 get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY)
1754 make_paths_relative(
1755 ARG_PATH_VALUES_RELATIVE "${OUTPUT_DIR}" "${ARG_PATH_VALUES}")
1757 list(LENGTH ARG_PATHS len_paths)
1758 list(LENGTH ARG_PATH_VALUES len_path_values)
1759 list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels)
1760 if ((NOT ${len_paths} EQUAL ${len_path_values}) OR
1761 (NOT ${len_paths} EQUAL ${len_path_value_rels}))
1762 message(SEND_ERROR "PATHS lengths got confused")
1765 # Transform variables mentioned in ARG_PATHS to relative paths for
1766 # the configure_file() call. Variables are copied to subscopeds by cmake,
1767 # so this only modifies the local copy of the variables.
1768 math(EXPR arg_path_limit "${len_paths} - 1")
1769 foreach(i RANGE ${arg_path_limit})
1770 list(GET ARG_PATHS ${i} val1)
1771 list(GET ARG_PATH_VALUES_RELATIVE ${i} val2)
1772 set(${val1} ${val2})
1776 configure_file(${site_in} ${site_out} @ONLY)
1778 if (EXISTS "${ARG_MAIN_CONFIG}")
1779 # Remember main config / generated site config for llvm-lit.in.
1780 get_property(LLVM_LIT_CONFIG_FILES GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES)
1781 list(APPEND LLVM_LIT_CONFIG_FILES "${ARG_MAIN_CONFIG}" "${site_out}")
1782 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES ${LLVM_LIT_CONFIG_FILES})
1786 function(dump_all_cmake_variables)
1787 get_cmake_property(_variableNames VARIABLES)
1788 foreach (_variableName ${_variableNames})
1789 message(STATUS "${_variableName}=${${_variableName}}")
1793 function(get_llvm_lit_path base_dir file_name)
1794 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1796 if (ARG_ALLOW_EXTERNAL)
1797 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1798 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1799 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1802 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1803 if (EXISTS ${LLVM_EXTERNAL_LIT})
1804 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1805 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1806 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1807 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1809 elseif (NOT DEFINED CACHE{LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE})
1810 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1811 set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "")
1816 set(lit_file_name "llvm-lit")
1817 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
1818 # llvm-lit needs suffix.py for multiprocess to find a main module.
1819 set(lit_file_name "${lit_file_name}.py")
1821 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1823 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1824 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1825 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1828 # Allow individual projects to provide an override
1829 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1830 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1831 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1832 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1834 set(LLVM_LIT_BASE_DIR "")
1837 # Cache this so we don't have to do it again and have subsequent calls
1838 # potentially disagree on the value.
1839 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1840 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1843 # A raw function to create a lit target. This is used to implement the testuite
1844 # management functions.
1845 function(add_lit_target target comment)
1846 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1847 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1848 separate_arguments(LIT_ARGS)
1849 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1850 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1853 # Get the path to the lit to *run* tests with. This can be overriden by
1854 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1861 set(LIT_COMMAND "${Python3_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1862 list(APPEND LIT_COMMAND ${LIT_ARGS})
1863 foreach(param ${ARG_PARAMS})
1864 list(APPEND LIT_COMMAND --param ${param})
1866 if (ARG_UNPARSED_ARGUMENTS)
1867 add_custom_target(${target}
1868 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1869 COMMENT "${comment}"
1873 add_custom_target(${target}
1874 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1875 message(STATUS "${target} does nothing.")
1879 add_dependencies(${target} ${ARG_DEPENDS})
1882 # Tests should be excluded from "Build Solution".
1883 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1886 # Convert a target name like check-clang to a variable name like CLANG.
1887 function(umbrella_lit_testsuite_var target outvar)
1888 if (NOT target MATCHES "^check-")
1889 message(FATAL_ERROR "umbrella lit suites must be check-*, not '${target}'")
1891 string(SUBSTRING "${target}" 6 -1 var)
1892 string(REPLACE "-" "_" var ${var})
1893 string(TOUPPER "${var}" var)
1894 set(${outvar} "${var}" PARENT_SCOPE)
1897 # Start recording all lit test suites for a combined 'check-foo' target.
1898 # The recording continues until umbrella_lit_testsuite_end() creates the target.
1899 function(umbrella_lit_testsuite_begin target)
1900 umbrella_lit_testsuite_var(${target} name)
1901 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_UMBRELLAS ${name})
1904 # Create a combined 'check-foo' target for a set of related test suites.
1905 # It runs all suites added since the matching umbrella_lit_testsuite_end() call.
1906 # Tests marked EXCLUDE_FROM_CHECK_ALL are not gathered.
1907 function(umbrella_lit_testsuite_end target)
1908 umbrella_lit_testsuite_var(${target} name)
1910 get_property(testsuites GLOBAL PROPERTY LLVM_${name}_LIT_TESTSUITES)
1911 get_property(params GLOBAL PROPERTY LLVM_${name}_LIT_PARAMS)
1912 get_property(depends GLOBAL PROPERTY LLVM_${name}_LIT_DEPENDS)
1913 get_property(extra_args GLOBAL PROPERTY LLVM_${name}_LIT_EXTRA_ARGS)
1914 # Additional test targets are not gathered, but may be set externally.
1915 get_property(additional_test_targets
1916 GLOBAL PROPERTY LLVM_${name}_ADDITIONAL_TEST_TARGETS)
1918 string(TOLOWER ${name} name)
1919 add_lit_target(${target}
1920 "Running ${name} regression tests"
1923 DEPENDS ${depends} ${additional_test_targets}
1928 # A function to add a set of lit test suites to be driven through 'check-*' targets.
1929 function(add_lit_testsuite target comment)
1930 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1932 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1933 if(NOT ARG_EXCLUDE_FROM_CHECK_ALL)
1934 get_property(gather_names GLOBAL PROPERTY LLVM_LIT_UMBRELLAS)
1935 foreach(name ${gather_names})
1936 # Register the testsuites, params and depends for the umbrella check rule.
1937 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1938 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_PARAMS ${ARG_PARAMS})
1939 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_DEPENDS ${ARG_DEPENDS})
1940 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_EXTRA_ARGS ${ARG_ARGS})
1944 # Produce a specific suffixed check rule.
1945 add_lit_target(${target} ${comment}
1946 ${ARG_UNPARSED_ARGUMENTS}
1947 PARAMS ${ARG_PARAMS}
1948 DEPENDS ${ARG_DEPENDS}
1953 function(add_lit_testsuites project directory)
1954 if (NOT LLVM_ENABLE_IDE)
1955 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "FOLDER" "PARAMS;DEPENDS;ARGS" ${ARGN})
1958 set(ARG_FOLDER "Test Subdirectories")
1961 # Search recursively for test directories by assuming anything not
1962 # in a directory called Inputs contains tests.
1963 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1964 foreach(lit_suite ${to_process})
1965 if(NOT IS_DIRECTORY ${lit_suite})
1968 string(FIND ${lit_suite} Inputs is_inputs)
1969 string(FIND ${lit_suite} Output is_output)
1970 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1974 # Create a check- target for the directory.
1975 string(REPLACE ${directory} "" name_slash ${lit_suite})
1977 string(REPLACE "/" "-" name_slash ${name_slash})
1978 string(REPLACE "\\" "-" name_dashes ${name_slash})
1979 string(TOLOWER "${project}${name_dashes}" name_var)
1980 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1982 ${EXCLUDE_FROM_CHECK_ALL}
1983 PARAMS ${ARG_PARAMS}
1984 DEPENDS ${ARG_DEPENDS}
1987 set_target_properties(check-${name_var} PROPERTIES FOLDER ${ARG_FOLDER})
1993 function(llvm_install_library_symlink name dest type)
1994 cmake_parse_arguments(ARG "" "COMPONENT" "" ${ARGN})
1995 foreach(path ${CMAKE_MODULE_PATH})
1996 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1997 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
2002 set(component ${ARG_COMPONENT})
2004 set(component ${name})
2007 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
2008 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
2010 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
2011 if(WIN32 AND "${type}" STREQUAL "SHARED")
2012 set(output_dir "${CMAKE_INSTALL_BINDIR}")
2015 install(SCRIPT ${INSTALL_SYMLINK}
2016 CODE "install_symlink(\"${full_name}\" \"${full_dest}\" \"${output_dir}\")"
2017 COMPONENT ${component})
2021 function(llvm_install_symlink project name dest)
2022 get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
2023 if(LLVM_TOOL_LLVM_DRIVER_BUILD AND ${dest} IN_LIST LLVM_DRIVER_TOOLS)
2024 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_SYMLINKS ${name})
2027 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
2028 foreach(path ${CMAKE_MODULE_PATH})
2029 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
2030 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
2036 set(component ${ARG_COMPONENT})
2038 if(ARG_ALWAYS_GENERATE)
2039 set(component ${dest})
2041 set(component ${name})
2045 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
2046 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
2047 if (${dest} STREQUAL "llvm-driver")
2048 set(full_dest llvm${CMAKE_EXECUTABLE_SUFFIX})
2051 set(output_dir "${${project}_TOOLS_INSTALL_DIR}")
2053 install(SCRIPT ${INSTALL_SYMLINK}
2054 CODE "install_symlink(\"${full_name}\" \"${full_dest}\" \"${output_dir}\")"
2055 COMPONENT ${component})
2057 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
2058 add_llvm_install_targets(install-${name}
2059 DEPENDS ${name} ${dest}
2065 function(llvm_add_tool_symlink project link_name target)
2066 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
2068 get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
2070 if (${target} IN_LIST LLVM_DRIVER_TOOLS)
2071 string(REPLACE "-" "_" tool_entry ${target})
2072 string(REPLACE "-" "_" key ${link_name})
2073 string(REPLACE "llvm-" "" tool_name ${link_name})
2074 set_property(GLOBAL APPEND_STRING PROPERTY
2075 LLVM_EXTRA_DRIVER_ENTRIES "LLVM_DRIVER_TOOL(\"${tool_name}\", ${tool_entry})\n")
2077 set(dest_binary "$<TARGET_FILE:${target}>")
2079 # This got a bit gross... For multi-configuration generators the target
2080 # properties return the resolved value of the string, not the build system
2081 # expression. To reconstruct the platform-agnostic path we have to do some
2082 # magic. First we grab one of the types, and a type-specific path. Then from
2083 # the type-specific path we find the last occurrence of the type in the path,
2084 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
2086 if(NOT ARG_OUTPUT_DIR)
2087 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
2088 # the same directory.
2090 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
2092 if(CMAKE_CONFIGURATION_TYPES)
2093 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
2094 string(TOUPPER ${first_type} first_type_upper)
2095 set(first_type_suffix _${first_type_upper})
2097 get_target_property(target_type ${target} TYPE)
2098 if(${target_type} STREQUAL "STATIC_LIBRARY")
2099 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
2100 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
2101 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
2103 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
2105 if(CMAKE_CONFIGURATION_TYPES)
2106 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
2107 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
2108 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
2109 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
2110 path_suffix ${path_suffix})
2111 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
2116 set(LLVM_LINK_OR_COPY create_symlink)
2118 set(LLVM_LINK_OR_COPY copy)
2121 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
2123 set(target_name ${link_name})
2124 if(TARGET ${link_name})
2125 set(target_name ${link_name}-link)
2129 if(ARG_ALWAYS_GENERATE)
2130 set_property(DIRECTORY APPEND PROPERTY
2131 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
2132 add_custom_command(TARGET ${target} POST_BUILD
2133 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
2135 add_custom_command(OUTPUT ${output_path}
2136 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
2139 # TODO: Make use of generator expressions below once CMake 3.19 or higher is the minimum supported version.
2140 set(should_build_all)
2141 get_target_property(target_excluded_from_all ${target} EXCLUDE_FROM_ALL)
2142 if (NOT target_excluded_from_all)
2143 set(should_build_all ALL)
2145 add_custom_target(${target_name} ${should_build_all} DEPENDS ${target} ${output_path})
2146 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
2148 # Make sure both the link and target are toolchain tools
2149 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
2150 set(TOOL_IS_TOOLCHAIN ON)
2153 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
2154 llvm_install_symlink("${project}" ${link_name} ${target})
2159 function(add_llvm_tool_symlink link_name target)
2160 llvm_add_tool_symlink(LLVM ${ARGV})
2163 function(llvm_externalize_debuginfo name)
2164 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
2168 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
2171 set(CMAKE_STRIP xcrun strip)
2173 set(strip_command COMMAND ${CMAKE_STRIP} -S -x $<TARGET_FILE:${name}>)
2175 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
2180 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
2181 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
2186 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
2188 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
2189 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
2191 set(output_path "-o=${output_name}")
2194 if(CMAKE_CXX_FLAGS MATCHES "-flto"
2195 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
2197 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
2198 set_property(TARGET ${name} APPEND_STRING PROPERTY
2199 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
2201 if(NOT CMAKE_DSYMUTIL)
2202 set(CMAKE_DSYMUTIL xcrun dsymutil)
2204 add_custom_command(TARGET ${name} POST_BUILD
2205 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
2209 add_custom_command(TARGET ${name} POST_BUILD
2210 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
2211 ${strip_command} -R .gnu_debuglink
2212 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
2217 # Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
2218 function(llvm_codesign name)
2219 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
2221 if(NOT LLVM_CODESIGNING_IDENTITY)
2225 if(CMAKE_GENERATOR STREQUAL "Xcode")
2226 set_target_properties(${name} PROPERTIES
2227 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
2229 if(DEFINED ARG_ENTITLEMENTS)
2230 set_target_properties(${name} PROPERTIES
2231 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
2234 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
2235 if(NOT CMAKE_CODESIGN)
2236 set(CMAKE_CODESIGN xcrun codesign)
2238 if(NOT CMAKE_CODESIGN_ALLOCATE)
2240 COMMAND xcrun -f codesign_allocate
2241 OUTPUT_STRIP_TRAILING_WHITESPACE
2242 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
2245 if(DEFINED ARG_ENTITLEMENTS)
2246 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
2249 if (NOT ARG_BUNDLE_PATH)
2250 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
2253 # ld64 now always codesigns the binaries it creates. Apply the force arg
2254 # unconditionally so that we can - for example - add entitlements to the
2255 # targets that need it.
2256 set(force_flag "-f")
2259 TARGET ${name} POST_BUILD
2260 COMMAND ${CMAKE_COMMAND} -E
2261 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
2262 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
2263 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
2268 function(llvm_setup_rpath name)
2269 if(CMAKE_INSTALL_RPATH)
2273 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
2274 set(extra_libdir ${LLVM_LIBRARY_DIR})
2275 elseif(LLVM_BUILD_LIBRARY_DIR)
2276 set(extra_libdir ${LLVM_LIBRARY_DIR})
2280 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
2281 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2282 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND BUILD_SHARED_LIBS)
2283 # $ORIGIN is not interpreted at link time by aix ld.
2284 # Since BUILD_SHARED_LIBS is only recommended for use by developers,
2285 # hardcode the rpath to build/install lib dir first in this mode.
2286 # FIXME: update this when there is better solution.
2287 set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_PREFIX}/lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2289 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2290 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
2291 set_property(TARGET ${name} APPEND_STRING PROPERTY
2292 LINK_FLAGS " -Wl,-z,origin ")
2294 if(LLVM_LINKER_IS_GNULD)
2295 # $ORIGIN is not interpreted at link time by ld.bfd
2296 set_property(TARGET ${name} APPEND_STRING PROPERTY
2297 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
2303 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set.
2304 if("${CMAKE_BUILD_RPATH}" STREQUAL "")
2305 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
2308 set_target_properties(${name} PROPERTIES
2309 INSTALL_RPATH "${_install_rpath}"
2310 ${_install_name_dir})
2313 function(setup_dependency_debugging name)
2314 if(NOT LLVM_DEPENDENCY_DEBUGGING)
2318 if("intrinsics_gen" IN_LIST ARGN)
2322 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
2323 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
2325 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
2326 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
2329 # If the sources at the given `path` are under version control, set `out_var`
2330 # to the the path of a file which will be modified when the VCS revision
2331 # changes, attempting to create that file if it does not exist; if no such
2332 # file exists and one cannot be created, instead set `out_var` to the
2335 # If the sources are not under version control, do not define `out_var`.
2336 function(find_first_existing_vc_file path out_var)
2337 if(NOT EXISTS "${path}")
2342 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
2343 WORKING_DIRECTORY ${path}
2344 RESULT_VARIABLE git_result
2345 OUTPUT_VARIABLE git_output
2347 if(git_result EQUAL 0)
2348 string(STRIP "${git_output}" git_output)
2349 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
2350 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
2351 if (NOT EXISTS "${git_dir}/logs/HEAD")
2352 execute_process(COMMAND ${CMAKE_COMMAND} -E touch HEAD
2353 WORKING_DIRECTORY "${git_dir}/logs"
2354 RESULT_VARIABLE touch_head_result
2356 if (NOT touch_head_result EQUAL 0)
2357 set(${out_var} "" PARENT_SCOPE)
2361 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)