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(LLVM_ENABLE_UNWIND_TABLES)
29 list(APPEND LLVM_COMPILE_FLAGS "-funwind-tables")
31 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables")
32 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables")
35 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
36 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
37 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
38 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh")
42 # LLVM_REQUIRES_RTTI is an internal flag that individual
43 # targets can use to force RTTI
44 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
45 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
46 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
47 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
48 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
49 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
51 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
52 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
53 list(APPEND LLVM_COMPILE_FLAGS "-qnortti")
56 list(APPEND LLVM_COMPILE_FLAGS "/GR")
60 # - LLVM_COMPILE_FLAGS is list.
61 # - PROPERTY COMPILE_FLAGS is string.
62 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
63 string(REPLACE ";" " " target_compile_cflags " ${LLVM_COMPILE_CFLAGS}")
66 foreach(fn ${sources})
67 get_filename_component(suf ${fn} EXT)
68 if("${suf}" STREQUAL ".cpp")
69 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
70 COMPILE_FLAGS "${target_compile_flags}")
72 if("${suf}" STREQUAL ".c")
73 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
74 COMPILE_FLAGS "${target_compile_cflags}")
78 # Update target props, since all sources are C++.
79 set_property(TARGET ${name} APPEND_STRING PROPERTY
80 COMPILE_FLAGS "${target_compile_flags}")
83 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
86 function(add_llvm_symbol_exports target_name export_file)
87 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
88 set(native_export_file "${target_name}.exports")
89 add_custom_command(OUTPUT ${native_export_file}
90 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
91 DEPENDS ${export_file}
93 COMMENT "Creating export file for ${target_name}")
94 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
95 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
96 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
97 # FIXME: `-Wl,-bE:` bypasses whatever handling there is in the build
98 # compiler driver to defer to the specified export list.
99 set(native_export_file "${export_file}")
100 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
101 LINK_FLAGS " -Wl,-bE:${export_file}")
102 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
103 # Gold and BFD ld require a version script rather than a plain list.
104 set(native_export_file "${target_name}.exports")
105 # FIXME: Don't write the "local:" line on OpenBSD.
106 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
107 add_custom_command(OUTPUT ${native_export_file}
108 COMMAND "${Python3_EXECUTABLE}" "-c"
110 lines = [' ' + l.rstrip() for l in sys.stdin] + [' local: *;']; \
111 print('LLVM_${LLVM_VERSION_MAJOR} {'); \
112 print(' global:') if len(lines) > 1 else None; \
113 print(';\\n'.join(lines) + '\\n};')"
114 < ${export_file} > ${native_export_file}
115 DEPENDS ${export_file}
117 COMMENT "Creating export file for ${target_name}")
118 if (${LLVM_LINKER_IS_SOLARISLD})
119 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
120 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
122 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
123 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
126 set(native_export_file "${target_name}.def")
128 add_custom_command(OUTPUT ${native_export_file}
129 COMMAND "${Python3_EXECUTABLE}" -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
130 < ${export_file} > ${native_export_file}
131 DEPENDS ${export_file}
133 COMMENT "Creating export file for ${target_name}")
134 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
136 # cl.exe or clang-cl, i.e. MSVC style command line interface
137 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
138 elseif(CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
139 # clang in msvc mode, calling a link.exe/lld-link style linker
140 set(export_file_linker_flag "-Wl,/DEF:\"${export_file_linker_flag}\"")
142 # ${export_file_linker_flag}, which is the plain file name, works as is
143 # when passed to the compiler driver, which then passes it on to the
144 # linker as an input file.
145 set(export_file_linker_flag "\"${export_file_linker_flag}\"")
147 message(FATAL_ERROR "Unsupported Windows toolchain")
149 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
150 LINK_FLAGS " ${export_file_linker_flag}")
153 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
154 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
156 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
158 get_filename_component(extension ${src} EXT)
159 if(extension STREQUAL ".cpp")
160 set(first_source_file ${src})
165 # Force re-linking when the exports file changes. Actually, it
166 # forces recompilation of the source file. The LINK_DEPENDS target
167 # property only works for makefile-based generators.
168 # FIXME: This is not safe because this will create the same target
169 # ${native_export_file} in several different file:
170 # - One where we emitted ${target_name}_exports
171 # - One where we emitted the build command for the following object.
172 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
173 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
175 set_property(DIRECTORY APPEND
176 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
178 add_dependencies(${target_name} ${target_name}_exports)
180 # Add dependency to *_exports later -- CMake issue 14747
181 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
182 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
183 endfunction(add_llvm_symbol_exports)
185 if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
186 # Detect what linker we have here.
188 # Linkers with ld64-compatible flags.
189 set(version_flag "-Wl,-v")
191 # Linkers with BFD ld-compatible flags.
192 set(version_flag "-Wl,--version")
195 if (CMAKE_HOST_WIN32)
198 set(DEVNULL "/dev/null")
202 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} ${version_flag} -o ${DEVNULL})
204 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
205 set(command ${CMAKE_C_COMPILER} ${flags} ${version_flag} -o ${DEVNULL})
209 OUTPUT_VARIABLE stdout
210 ERROR_VARIABLE stderr
214 if("${stderr}" MATCHES "PROJECT:ld64")
215 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
216 set(LLVM_LINKER_IS_LD64 YES CACHE INTERNAL "")
217 message(STATUS "Linker detection: ld64")
218 elseif("${stderr}" MATCHES "^LLD" OR
219 "${stdout}" MATCHES "^LLD")
220 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
221 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "")
222 message(STATUS "Linker detection: lld")
224 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
225 message(STATUS "Linker detection: unknown")
228 if("${stdout}" MATCHES "^mold")
229 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
230 set(LLVM_LINKER_IS_MOLD YES CACHE INTERNAL "")
231 message(STATUS "Linker detection: mold")
232 elseif("${stdout}" MATCHES "GNU gold")
233 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
234 set(LLVM_LINKER_IS_GOLD YES CACHE INTERNAL "")
235 message(STATUS "Linker detection: GNU Gold")
236 elseif("${stdout}" MATCHES "^LLD")
237 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
238 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "")
239 message(STATUS "Linker detection: LLD")
240 elseif("${stdout}" MATCHES "GNU ld")
241 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
242 set(LLVM_LINKER_IS_GNULD YES CACHE INTERNAL "")
243 message(STATUS "Linker detection: GNU ld")
244 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
245 "${stdout}" MATCHES "Solaris Link Editors")
246 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
247 set(LLVM_LINKER_IS_SOLARISLD YES CACHE INTERNAL "")
248 message(STATUS "Linker detection: Solaris ld")
250 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
251 message(STATUS "Linker detection: unknown")
256 function(add_link_opts target_name)
257 get_llvm_distribution(${target_name} in_distribution in_distribution_var)
258 if(NOT in_distribution)
259 # Don't LTO optimize targets that aren't part of any distribution.
261 # We may consider avoiding LTO altogether by using -fembed-bitcode
262 # and teaching the linker to select machine code from .o files, see
263 # https://lists.llvm.org/pipermail/llvm-dev/2021-April/149843.html
264 if((UNIX OR MINGW) AND LINKER_IS_LLD)
265 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
266 LINK_FLAGS " -Wl,--lto-O0")
267 elseif(LINKER_IS_LLD_LINK)
268 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
269 LINK_FLAGS " /opt:lldlto=0")
270 elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
271 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
272 LINK_FLAGS " -Wl,-mllvm,-O0")
277 # Don't use linker optimizations in debug builds since it slows down the
278 # linker in a context where the optimizations are not important.
279 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
280 if(NOT LLVM_NO_DEAD_STRIP)
281 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
282 # ld64's implementation of -dead_strip breaks tools that use plugins.
283 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
284 LINK_FLAGS " -Wl,-dead_strip")
285 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS" AND LLVM_LINKER_IS_SOLARISLD)
286 # Support for ld -z discard-unused=sections was only added in
287 # Solaris 11.4. GNU ld ignores it, but warns every time.
288 include(LLVMCheckLinkerFlag)
289 llvm_check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
290 if (LINKER_SUPPORTS_Z_DISCARD_UNUSED)
291 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
292 LINK_FLAGS " -Wl,-z,discard-unused=sections")
294 elseif(NOT MSVC AND NOT CMAKE_SYSTEM_NAME MATCHES "AIX|OS390")
295 # TODO Revisit this later on z/OS.
296 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
297 LINK_FLAGS " -Wl,--gc-sections")
299 else() #LLVM_NO_DEAD_STRIP
300 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
301 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
302 LINK_FLAGS " -Wl,-bnogc")
307 if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
308 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
309 LINK_FLAGS " -Wl,-brtl")
311 endfunction(add_link_opts)
313 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
314 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
315 # or a certain builder, for eaxample, msbuild.exe, would be confused.
316 function(set_output_directory target)
317 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
319 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
320 # It affects output of add_library(MODULE).
323 set(module_dir ${ARG_BINARY_DIR})
325 set(module_dir ${ARG_LIBRARY_DIR})
327 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
328 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
329 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
331 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
332 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
335 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
336 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
339 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
340 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
345 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
348 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
351 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
356 # If on Windows and building with MSVC, add the resource script containing the
357 # VERSIONINFO data to the project. This embeds version resource information
358 # into the output .exe or .dll.
359 # TODO: Enable for MinGW Windows builds too.
361 function(add_windows_version_resource_file OUT_VAR)
363 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
364 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
365 if(EXISTS ${resource_file})
366 set(sources ${sources} ${resource_file})
367 source_group("Resource Files" ${resource_file})
368 set(windows_resource_file ${resource_file} PARENT_SCOPE)
370 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
372 set(${OUT_VAR} ${sources} PARENT_SCOPE)
373 endfunction(add_windows_version_resource_file)
375 # set_windows_version_resource_properties(name resource_file...
377 # Optional major version number (defaults to LLVM_VERSION_MAJOR)
379 # Optional minor version number (defaults to LLVM_VERSION_MINOR)
380 # VERSION_PATCHLEVEL int
381 # Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
383 # Optional version string (defaults to PACKAGE_VERSION)
385 # Optional product name string (defaults to "LLVM")
387 function(set_windows_version_resource_properties name resource_file)
388 cmake_parse_arguments(ARG
390 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
394 if (NOT DEFINED ARG_VERSION_MAJOR)
395 if (${LLVM_VERSION_MAJOR})
396 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
398 set(ARG_VERSION_MAJOR 0)
402 if (NOT DEFINED ARG_VERSION_MINOR)
403 if (${LLVM_VERSION_MINOR})
404 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
406 set(ARG_VERSION_MINOR 0)
410 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
411 if (${LLVM_VERSION_PATCH})
412 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
414 set(ARG_VERSION_PATCHLEVEL 0)
418 if (NOT DEFINED ARG_VERSION_STRING)
419 if (${PACKAGE_VERSION})
420 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
422 set(ARG_VERSION_STRING 0)
426 if (NOT DEFINED ARG_PRODUCT_NAME)
427 set(ARG_PRODUCT_NAME "LLVM")
430 set_property(SOURCE ${resource_file}
431 PROPERTY COMPILE_FLAGS /nologo)
432 set_property(SOURCE ${resource_file}
433 PROPERTY COMPILE_DEFINITIONS
434 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
435 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
436 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
437 "RC_VERSION_FIELD_4=0"
438 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
439 "RC_INTERNAL_NAME=\"${name}\""
440 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
441 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
442 endfunction(set_windows_version_resource_properties)
444 # llvm_add_library(name sources...
446 # STATIC by default w/o BUILD_SHARED_LIBS.
447 # SHARED by default w/ BUILD_SHARED_LIBS.
449 # Also create an OBJECT library target. Default if STATIC && SHARED.
451 # Target ${name} might not be created on unsupported platforms.
452 # Check with "if(TARGET ${name})".
453 # DISABLE_LLVM_LINK_LLVM_DYLIB
454 # Do not link this library to libLLVM, even if
455 # LLVM_LINK_LLVM_DYLIB is enabled.
457 # Corresponds to OUTPUT_NAME in target properties.
459 # Same semantics as add_dependencies().
460 # LINK_COMPONENTS components...
461 # Same as the variable LLVM_LINK_COMPONENTS.
462 # LINK_LIBS lib_targets...
463 # Same semantics as target_link_libraries().
465 # May specify header files for IDE generators.
467 # Should set SONAME link flags and create symlinks
469 # Suppress default RPATH settings in shared libraries.
471 # The tool (i.e. cmake target) that this plugin will link against
473 # This is used to specify that this is a component library of
474 # LLVM which means that the source resides in llvm/lib/ and it is a
475 # candidate for inclusion into libLLVM.so.
477 function(llvm_add_library name)
478 cmake_parse_arguments(ARG
479 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB"
480 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
481 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
483 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
484 if(ARG_ADDITIONAL_HEADERS)
485 # Pass through ADDITIONAL_HEADERS.
486 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
489 set(ALL_FILES ${ARG_OBJLIBS})
491 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
495 if(ARG_SHARED OR ARG_STATIC)
496 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
498 # Plugins that link against a tool are allowed even when plugins in general are not
499 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
500 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
505 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
507 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
516 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
517 # Generate an obj library for both targets.
518 set(obj_name "obj.${name}")
519 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
522 llvm_update_compile_flags(${obj_name})
523 if(CMAKE_GENERATOR STREQUAL "Xcode")
524 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
525 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
526 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
528 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
530 # Do add_dependencies(obj) later due to CMake issue 14747.
531 list(APPEND objlibs ${obj_name})
533 # Bring in the target include directories from our original target.
534 target_include_directories(${obj_name} PRIVATE $<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>)
536 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
538 add_dependencies(${obj_name} ${ARG_DEPENDS})
540 # Treat link libraries like PUBLIC dependencies. LINK_LIBS might
541 # result in generating header files. Add a dependendency so that
542 # the generated header is created before this object library.
544 cmake_parse_arguments(LINK_LIBS_ARG
549 foreach(link_lib ${LINK_LIBS_ARG_PUBLIC})
551 # Can't specify a dependence on -lpthread
552 if(NOT ${link_lib} STREQUAL ${LLVM_PTHREAD_LIB})
553 add_dependencies(${obj_name} ${link_lib})
556 add_dependencies(${obj_name} ${link_lib})
562 if(ARG_SHARED AND ARG_STATIC)
564 set(name_static "${name}_static")
566 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
568 # DEPENDS has been appended to LLVM_COMMON_LIBS.
569 llvm_add_library(${name_static} STATIC
571 OBJLIBS ${ALL_FILES} # objlib
572 LINK_LIBS ${ARG_LINK_LIBS}
573 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
576 # Bring in the target link info from our original target.
577 target_link_directories(${name_static} PRIVATE $<TARGET_PROPERTY:${name},LINK_DIRECTORIES>)
578 target_link_libraries(${name_static} PRIVATE $<TARGET_PROPERTY:${name},LINK_LIBRARIES>)
580 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
585 add_library(${name} MODULE ${ALL_FILES})
587 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
588 add_library(${name} SHARED ${ALL_FILES})
590 add_library(${name} STATIC ${ALL_FILES})
593 if(ARG_COMPONENT_LIB)
594 set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE)
595 set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name})
598 if(NOT ARG_NO_INSTALL_RPATH)
599 if(ARG_MODULE OR ARG_SHARED)
600 llvm_setup_rpath(${name})
604 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
606 if(DEFINED windows_resource_file)
607 set_windows_version_resource_properties(${name} ${windows_resource_file})
608 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
611 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
612 # $<TARGET_OBJECTS> doesn't require compile flags.
614 llvm_update_compile_flags(${name})
616 add_link_opts( ${name} )
618 set_target_properties(${name}
620 OUTPUT_NAME ${ARG_OUTPUT_NAME}
625 set_target_properties(${name} PROPERTIES
627 SUFFIX ${LLVM_PLUGIN_EXT}
633 set_target_properties(${name} PROPERTIES
638 # Set SOVERSION on shared libraries that lack explicit SONAME
639 # specifier, on *nix systems that are not Darwin.
640 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
641 set_target_properties(${name}
643 # Since 4.0.0, the ABI version is indicated by the major version
644 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
645 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
649 if(ARG_MODULE OR ARG_SHARED)
650 # Do not add -Dname_EXPORTS to the command-line when building files in this
651 # target. Doing so is actively harmful for the modules build because it
652 # creates extra module variants, and not useful because we don't use these
654 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
656 if (LLVM_EXPORTED_SYMBOL_FILE)
657 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
662 if(NOT APPLE AND ARG_SONAME)
663 get_target_property(output_name ${name} OUTPUT_NAME)
664 if(${output_name} STREQUAL "output_name-NOTFOUND")
665 set(output_name ${name})
667 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
668 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
669 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
671 llvm_install_library_symlink(${api_name} ${library_name} SHARED
673 llvm_install_library_symlink(${output_name} ${library_name} SHARED
682 # We can use PRIVATE since SO knows its dependent libs.
686 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
687 # On DLL platforms symbols are imported from the tool by linking against it.
688 set(llvm_libs ${ARG_PLUGIN_TOOL})
689 elseif (NOT ARG_COMPONENT_LIB)
690 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
693 llvm_map_components_to_libnames(llvm_libs
694 ${ARG_LINK_COMPONENTS}
695 ${LLVM_LINK_COMPONENTS}
699 # Components have not been defined explicitly in CMake, so add the
700 # dependency information for this library through their name, and let
701 # LLVMBuildResolveComponentsLink resolve the mapping.
703 # It would be nice to verify that we have the dependencies for this library
704 # name, but using get_property(... SET) doesn't suffice to determine if a
705 # property has been set to an empty value.
706 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS})
708 # This property is an internal property only used to make sure the
709 # link step applied in LLVMBuildResolveComponentsLink uses the same
710 # property as the target_link_libraries call below.
711 set_property(TARGET ${name} PROPERTY LLVM_LIBTYPE ${libtype})
714 target_link_libraries(${name} ${libtype}
720 if(LLVM_COMMON_DEPENDS)
721 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
722 # Add dependencies also to objlibs.
723 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
724 foreach(objlib ${objlibs})
725 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
729 add_custom_linker_flags(${name})
731 if(ARG_SHARED OR ARG_MODULE)
732 llvm_externalize_debuginfo(${name})
733 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
735 # clang and newer versions of ninja use high-resolutions timestamps,
736 # but older versions of libtool on Darwin don't, so the archive will
737 # often get an older timestamp than the last object that was added
738 # or updated. To fix this, we add a custom command to touch archive
739 # after it's been built so that ninja won't rebuild it unnecessarily
740 # the next time it's run.
741 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
742 add_custom_command(TARGET ${name}
744 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
749 function(add_llvm_install_targets target)
750 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN})
752 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
755 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
758 set(file_dependencies)
759 set(target_dependencies)
760 foreach(dependency ${ARG_DEPENDS})
761 if(TARGET ${dependency})
762 list(APPEND target_dependencies ${dependency})
764 list(APPEND file_dependencies ${dependency})
768 add_custom_target(${target}
769 DEPENDS ${file_dependencies}
770 COMMAND "${CMAKE_COMMAND}"
773 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
775 set_target_properties(${target} PROPERTIES FOLDER "Component Install Targets")
776 add_custom_target(${target}-stripped
777 DEPENDS ${file_dependencies}
778 COMMAND "${CMAKE_COMMAND}"
781 -DCMAKE_INSTALL_DO_STRIP=1
782 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
784 set_target_properties(${target}-stripped PROPERTIES FOLDER "Component Install Targets (Stripped)")
785 if(target_dependencies)
786 add_dependencies(${target} ${target_dependencies})
787 add_dependencies(${target}-stripped ${target_dependencies})
791 add_dependencies(${target} install-${ARG_SYMLINK})
792 add_dependencies(${target}-stripped install-${ARG_SYMLINK}-stripped)
796 # Define special targets that behave like a component group. They don't have any
797 # source attached but other components can add themselves to them. If the
798 # component supports is a Target and it supports JIT compilation, HAS_JIT must
799 # be passed. One can use ADD_TO_COMPONENT option from add_llvm_component_library
800 # to link extra component into an existing group.
801 function(add_llvm_component_group name)
802 cmake_parse_arguments(ARG "HAS_JIT" "" "LINK_COMPONENTS" ${ARGN})
803 add_custom_target(${name})
805 set_property(TARGET ${name} PROPERTY COMPONENT_HAS_JIT ON)
807 if(ARG_LINK_COMPONENTS)
808 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
812 # An LLVM component is a cmake target with the following cmake properties
814 # - LLVM_COMPONENT_NAME: the name of the component, which can be the name of
815 # the associated library or the one specified through COMPONENT_NAME
816 # - LLVM_LINK_COMPONENTS: a list of component this component depends on
817 # - COMPONENT_HAS_JIT: (only for group component) whether this target group
818 # supports JIT compilation
819 # Additionnaly, the ADD_TO_COMPONENT <component> option make it possible to add this
820 # component to the LLVM_LINK_COMPONENTS of <component>.
821 function(add_llvm_component_library name)
822 cmake_parse_arguments(ARG
824 "COMPONENT_NAME;ADD_TO_COMPONENT"
827 add_llvm_library(${name} COMPONENT_LIB ${ARG_UNPARSED_ARGUMENTS})
828 string(REGEX REPLACE "^LLVM" "" component_name ${name})
829 set_property(TARGET ${name} PROPERTY LLVM_COMPONENT_NAME ${component_name})
831 if(ARG_COMPONENT_NAME)
832 set_property(GLOBAL PROPERTY LLVM_COMPONENT_NAME_${ARG_COMPONENT_NAME} ${component_name})
835 if(ARG_ADD_TO_COMPONENT)
836 set_property(TARGET ${ARG_ADD_TO_COMPONENT} APPEND PROPERTY LLVM_LINK_COMPONENTS ${component_name})
841 macro(add_llvm_library name)
842 cmake_parse_arguments(ARG
843 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
848 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
849 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
850 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
852 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
855 # Libraries that are meant to only be exposed via the build tree only are
856 # never installed and are only exported as a target in the special build tree
858 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
859 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
860 set(in_llvm_libs YES)
863 if (ARG_MODULE AND NOT TARGET ${name})
864 # Add empty "phony" target
865 add_custom_target(${name})
866 elseif( EXCLUDE_FROM_ALL )
867 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
868 elseif(ARG_BUILDTREE_ONLY)
869 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
871 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
873 set(umbrella UMBRELLA llvm-libraries)
878 get_target_export_arg(${name} LLVM export_to_llvmexports ${umbrella})
879 install(TARGETS ${name}
880 ${export_to_llvmexports}
881 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
882 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
883 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT ${name})
885 if (NOT LLVM_ENABLE_IDE)
886 add_llvm_install_targets(install-${name}
891 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
894 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
896 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
898 endmacro(add_llvm_library name)
900 macro(generate_llvm_objects name)
901 cmake_parse_arguments(ARG "GENERATE_DRIVER" "" "DEPENDS" ${ARGN})
903 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
905 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
908 if(LLVM_ENABLE_OBJLIB OR (ARG_GENERATE_DRIVER AND LLVM_TOOL_LLVM_DRIVER_BUILD))
909 # Generate an obj library for both targets.
910 set(obj_name "obj.${name}")
911 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
914 llvm_update_compile_flags(${obj_name})
915 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
917 add_dependencies(${obj_name} ${ARG_DEPENDS})
920 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
923 if (ARG_GENERATE_DRIVER)
924 string(REPLACE "-" "_" TOOL_NAME ${name})
925 foreach(path ${CMAKE_MODULE_PATH})
926 if(EXISTS ${path}/llvm-driver-template.cpp.in)
928 ${path}/llvm-driver-template.cpp.in
929 ${CMAKE_CURRENT_BINARY_DIR}/${name}-driver.cpp)
934 list(APPEND ALL_FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}-driver.cpp)
936 if (LLVM_TOOL_LLVM_DRIVER_BUILD
937 AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
939 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_COMPONENTS ${LLVM_LINK_COMPONENTS})
940 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_DEPS ${ARG_DEPENDS} ${LLVM_COMMON_DEPENDS})
941 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_OBJLIBS "${obj_name}")
943 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOLS ${name})
944 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_ALIASES_${name} ${name})
945 target_link_libraries(${obj_name} ${LLVM_PTHREAD_LIB})
946 llvm_config(${obj_name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
951 macro(add_llvm_executable name)
952 cmake_parse_arguments(ARG
953 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS"
954 "ENTITLEMENTS;BUNDLE_PATH"
957 generate_llvm_objects(${name} ${ARG_UNPARSED_ARGUMENTS})
958 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
961 # Note: the dummy.cpp source file provides no definitions. However,
962 # it forces Xcode to properly link the static library.
963 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
966 if( EXCLUDE_FROM_ALL )
967 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
969 add_executable(${name} ${ALL_FILES})
972 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
974 if(NOT ARG_NO_INSTALL_RPATH)
975 llvm_setup_rpath(${name})
976 elseif(NOT "${LLVM_LOCAL_RPATH}" STREQUAL "")
977 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set.
978 if("${CMAKE_BUILD_RPATH}" STREQUAL "")
979 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
982 set_property(TARGET ${name} PROPERTY INSTALL_RPATH "${LLVM_LOCAL_RPATH}")
985 if(DEFINED windows_resource_file)
986 set_windows_version_resource_properties(${name} ${windows_resource_file})
989 # $<TARGET_OBJECTS> doesn't require compile flags.
990 if(NOT LLVM_ENABLE_OBJLIB)
991 llvm_update_compile_flags(${name})
994 if (ARG_SUPPORT_PLUGINS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
995 set(LLVM_NO_DEAD_STRIP On)
998 add_link_opts( ${name} )
1000 # Do not add -Dname_EXPORTS to the command-line when building files in this
1001 # target. Doing so is actively harmful for the modules build because it
1002 # creates extra module variants, and not useful because we don't use these
1004 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
1006 if (LLVM_EXPORTED_SYMBOL_FILE)
1007 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
1008 endif(LLVM_EXPORTED_SYMBOL_FILE)
1010 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
1011 set(USE_SHARED USE_SHARED)
1014 set(EXCLUDE_FROM_ALL OFF)
1015 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
1016 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
1017 if( LLVM_COMMON_DEPENDS )
1018 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
1019 foreach(objlib ${obj_name})
1020 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
1022 endif( LLVM_COMMON_DEPENDS )
1024 add_custom_linker_flags(${name})
1026 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
1027 llvm_externalize_debuginfo(${name})
1029 if (LLVM_PTHREAD_LIB)
1030 # libpthreads overrides some standard library symbols, so main
1031 # executable must be linked with it in order to provide consistent
1032 # API for all shared libaries loaded by this executable.
1033 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
1037 target_link_libraries(${name} PRIVATE llvmlibc)
1040 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
1041 endmacro(add_llvm_executable name)
1043 # add_llvm_pass_plugin(name [NO_MODULE] ...)
1044 # Add ${name} as an llvm plugin.
1045 # If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically.
1046 # Otherwise a pluggable shared library is registered.
1048 # If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF,
1049 # only an object library is built, and no module is built. This is specific to the Polly use case.
1051 # The SUBPROJECT argument contains the LLVM project the plugin belongs
1052 # to. If set, the plugin will link statically by default it if the
1053 # project was enabled.
1054 function(add_llvm_pass_plugin name)
1055 cmake_parse_arguments(ARG
1056 "NO_MODULE" "SUBPROJECT" ""
1059 string(TOUPPER ${name} name_upper)
1061 # Enable the plugin by default if it was explicitly enabled by the user.
1062 # Note: If was set to "all", LLVM's CMakeLists.txt replaces it with a
1063 # list of all projects, counting as explicitly enabled.
1064 set(link_into_tools_default OFF)
1065 if (ARG_SUBPROJECT AND LLVM_TOOL_${name_upper}_BUILD)
1066 set(link_into_tools_default ON)
1068 option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" ${link_into_tools_default})
1070 # If we statically link the plugin, don't use llvm dylib because we're going
1072 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
1073 list(APPEND ARG_UNPARSED_ARGUMENTS DISABLE_LLVM_LINK_LLVM_DYLIB)
1076 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
1077 list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY)
1078 # process_llvm_pass_plugins takes care of the actual linking, just create an
1079 # object library as of now
1080 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
1081 target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS)
1082 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS)
1083 if (TARGET intrinsics_gen)
1084 add_dependencies(obj.${name} intrinsics_gen)
1087 add_dependencies(obj.${name} omp_gen)
1090 add_dependencies(obj.${name} acc_gen)
1092 set_property(GLOBAL APPEND PROPERTY LLVM_STATIC_EXTENSIONS ${name})
1093 elseif(NOT ARG_NO_MODULE)
1094 add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
1096 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
1098 message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})")
1100 endfunction(add_llvm_pass_plugin)
1102 # process_llvm_pass_plugins([GEN_CONFIG])
1104 # Correctly set lib dependencies between plugins and tools, based on tools
1105 # registered with the ENABLE_PLUGINS option.
1107 # if GEN_CONFIG option is set, also generate X Macro file for extension
1108 # handling. It provides a HANDLE_EXTENSION(extension_namespace, ExtensionProject)
1109 # call for each extension allowing client code to define
1110 # HANDLE_EXTENSION to have a specific code be run for each extension.
1112 function(process_llvm_pass_plugins)
1113 cmake_parse_arguments(ARG
1118 get_property(LLVM_STATIC_EXTENSIONS GLOBAL PROPERTY LLVM_STATIC_EXTENSIONS)
1120 include(LLVMConfigExtensions)
1123 # Add static plugins to the Extension component
1124 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1125 set_property(TARGET LLVMExtensions APPEND PROPERTY LINK_LIBRARIES ${llvm_extension})
1126 set_property(TARGET LLVMExtensions APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension})
1129 # Eventually generate the extension headers, and store config to a cmake file
1130 # for usage in third-party configuration.
1133 ## Part 1: Extension header to be included whenever we need extension
1135 if(NOT DEFINED LLVM_INSTALL_PACKAGE_DIR)
1136 message(FATAL_ERROR "LLVM_INSTALL_PACKAGE_DIR must be defined and writable. GEN_CONFIG should only be passe when building LLVM proper.")
1138 # LLVM_INSTALL_PACKAGE_DIR might be absolute, so don't reuse below.
1139 string(REPLACE "${CMAKE_CFG_INTDIR}" "." llvm_cmake_builddir "${LLVM_LIBRARY_DIR}")
1140 set(llvm_cmake_builddir "${llvm_cmake_builddir}/cmake/llvm")
1142 "${llvm_cmake_builddir}/LLVMConfigExtensions.cmake"
1143 "set(LLVM_STATIC_EXTENSIONS ${LLVM_STATIC_EXTENSIONS})")
1145 ${llvm_cmake_builddir}/LLVMConfigExtensions.cmake
1146 DESTINATION ${LLVM_INSTALL_PACKAGE_DIR}
1147 COMPONENT cmake-exports)
1149 set(ExtensionDef "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def")
1150 file(WRITE "${ExtensionDef}.tmp" "//extension handlers\n")
1151 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1152 file(APPEND "${ExtensionDef}.tmp" "HANDLE_EXTENSION(${llvm_extension})\n")
1154 file(APPEND "${ExtensionDef}.tmp" "#undef HANDLE_EXTENSION\n")
1156 # only replace if there's an actual change
1157 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
1158 "${ExtensionDef}.tmp"
1160 file(REMOVE "${ExtensionDef}.tmp")
1162 ## Part 2: Extension header that captures each extension dependency, to be
1163 # used by llvm-config.
1164 set(ExtensionDeps "${LLVM_BINARY_DIR}/tools/llvm-config/ExtensionDependencies.inc")
1166 # Max needed to correctly size the required library array.
1167 set(llvm_plugin_max_deps_length 0)
1168 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1169 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
1170 list(LENGTH llvm_plugin_deps llvm_plugin_deps_length)
1171 if(llvm_plugin_deps_length GREATER llvm_plugin_max_deps_length)
1172 set(llvm_plugin_max_deps_length ${llvm_plugin_deps_length})
1176 list(LENGTH LLVM_STATIC_EXTENSIONS llvm_static_extension_count)
1178 "${ExtensionDeps}.tmp"
1179 "#include <array>\n\
1180 struct ExtensionDescriptor {\n\
1181 const char* Name;\n\
1182 const char* RequiredLibraries[1 + 1 + ${llvm_plugin_max_deps_length}];\n\
1184 std::array<ExtensionDescriptor, ${llvm_static_extension_count}> AvailableExtensions{\n")
1186 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
1187 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
1189 file(APPEND "${ExtensionDeps}.tmp" "ExtensionDescriptor{\"${llvm_extension}\", {")
1190 foreach(llvm_plugin_dep ${llvm_plugin_deps})
1191 # Turn library dependency back to component name, if possible.
1192 # That way llvm-config can avoid redundant dependencies.
1193 STRING(REGEX REPLACE "^-l" "" plugin_dep_name ${llvm_plugin_dep})
1194 STRING(REGEX MATCH "^LLVM" is_llvm_library ${plugin_dep_name})
1196 STRING(REGEX REPLACE "^LLVM" "" plugin_dep_name ${plugin_dep_name})
1197 STRING(TOLOWER ${plugin_dep_name} plugin_dep_name)
1199 file(APPEND "${ExtensionDeps}.tmp" "\"${plugin_dep_name}\", ")
1202 # Self + mandatory trailing null, because the number of RequiredLibraries differs between extensions.
1203 file(APPEND "${ExtensionDeps}.tmp" \"${llvm_extension}\", "nullptr}},\n")
1205 file(APPEND "${ExtensionDeps}.tmp" "};\n")
1207 # only replace if there's an actual change
1208 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
1209 "${ExtensionDeps}.tmp"
1211 file(REMOVE "${ExtensionDeps}.tmp")
1215 function(export_executable_symbols target)
1216 if (LLVM_EXPORTED_SYMBOL_FILE)
1217 # The symbol file should contain the symbols we want the executable to
1219 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1220 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
1221 # Extract the symbols to export from the static libraries that the
1222 # executable links against.
1223 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1224 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
1225 # We need to consider not just the direct link dependencies, but also the
1226 # transitive link dependencies. Do this by starting with the set of direct
1227 # dependencies, then the dependencies of those dependencies, and so on.
1228 get_target_property(new_libs ${target} LINK_LIBRARIES)
1229 set(link_libs ${new_libs})
1230 while(NOT "${new_libs}" STREQUAL "")
1231 foreach(lib ${new_libs})
1233 get_target_property(lib_type ${lib} TYPE)
1234 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
1235 list(APPEND static_libs ${lib})
1237 list(APPEND other_libs ${lib})
1239 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
1240 foreach(transitive_lib ${transitive_libs})
1241 if(TARGET ${transitive_lib} AND NOT ${transitive_lib} IN_LIST link_libs)
1242 list(APPEND newer_libs ${transitive_lib})
1243 list(APPEND link_libs ${transitive_lib})
1245 endforeach(transitive_lib)
1248 set(new_libs ${newer_libs})
1251 list(REMOVE_DUPLICATES static_libs)
1253 set(mangling microsoft)
1255 set(mangling itanium)
1257 get_host_tool_path(llvm-nm LLVM_NM llvm_nm_exe llvm_nm_target)
1258 get_host_tool_path(llvm-readobj LLVM_READOBJ llvm_readobj_exe llvm_readobj_target)
1259 add_custom_command(OUTPUT ${exported_symbol_file}
1260 COMMAND "${Python3_EXECUTABLE}"
1261 ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py
1262 --mangling=${mangling} ${static_libs}
1263 -o ${exported_symbol_file}
1265 --readobj=${llvm_readobj_exe}
1266 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
1267 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py
1268 ${static_libs} ${llvm_nm_target} ${llvm_readobj_target}
1270 COMMENT "Generating export list for ${target}")
1271 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
1272 # If something links against this executable then we want a
1273 # transitive link against only the libraries whose symbols
1274 # we aren't exporting.
1275 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
1276 # The default import library suffix that cmake uses for cygwin/mingw is
1277 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
1278 # where the import libraries of both get named libclang.dll.a. Use a suffix
1279 # of ".exe.a" to avoid this.
1281 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
1283 elseif(NOT (WIN32 OR CYGWIN))
1284 # On Windows auto-exporting everything doesn't work because of the limit on
1285 # the size of the exported symbol table, but on other platforms we can do
1286 # it without any trouble.
1287 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
1288 # CMake doesn't set CMAKE_EXE_EXPORTS_${lang}_FLAG on Solaris, so
1289 # ENABLE_EXPORTS has no effect. While Solaris ld defaults to -rdynamic
1290 # behaviour, GNU ld needs it.
1291 if (APPLE OR ${CMAKE_SYSTEM_NAME} STREQUAL "SunOS")
1292 set_property(TARGET ${target} APPEND_STRING PROPERTY
1293 LINK_FLAGS " -rdynamic")
1298 # Export symbols if LLVM plugins are enabled.
1299 function(export_executable_symbols_for_plugins target)
1300 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
1301 export_executable_symbols(${target})
1305 if(NOT LLVM_TOOLCHAIN_TOOLS)
1306 set (LLVM_TOOLCHAIN_TOOLS
1325 # symlink version of some of above tools that are enabled by
1326 # LLVM_INSTALL_BINUTILS_SYMLINKS.
1339 # Build llvm-mt if libxml2 is enabled. Can be used by runtimes.
1340 if (LLVM_ENABLE_LIBXML2)
1341 list(APPEND LLVM_TOOLCHAIN_TOOLS llvm-mt)
1345 macro(llvm_add_tool project name)
1346 cmake_parse_arguments(ARG "DEPENDS;GENERATE_DRIVER" "" "" ${ARGN})
1347 if( NOT LLVM_BUILD_TOOLS )
1348 set(EXCLUDE_FROM_ALL ON)
1350 if(ARG_GENERATE_DRIVER
1351 AND LLVM_TOOL_LLVM_DRIVER_BUILD
1352 AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
1354 generate_llvm_objects(${name} ${ARGN})
1355 add_custom_target(${name} DEPENDS llvm-driver)
1357 add_llvm_executable(${name} ${ARGN})
1359 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1360 if( LLVM_BUILD_TOOLS )
1361 get_target_export_arg(${name} LLVM export_to_llvmexports)
1362 install(TARGETS ${name}
1363 ${export_to_llvmexports}
1364 RUNTIME DESTINATION ${${project}_TOOLS_INSTALL_DIR}
1367 if (NOT LLVM_ENABLE_IDE)
1368 add_llvm_install_targets(install-${name}
1374 if( LLVM_BUILD_TOOLS )
1375 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1377 set_target_properties(${name} PROPERTIES FOLDER "Tools")
1379 endmacro(llvm_add_tool project name)
1381 macro(add_llvm_tool name)
1382 llvm_add_tool(LLVM ${ARGV})
1386 macro(add_llvm_example name)
1387 if( NOT LLVM_BUILD_EXAMPLES )
1388 set(EXCLUDE_FROM_ALL ON)
1390 add_llvm_executable(${name} ${ARGN})
1391 if( LLVM_BUILD_EXAMPLES )
1392 install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
1394 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1395 endmacro(add_llvm_example name)
1397 macro(add_llvm_example_library name)
1398 if( NOT LLVM_BUILD_EXAMPLES )
1399 set(EXCLUDE_FROM_ALL ON)
1400 add_llvm_library(${name} BUILDTREE_ONLY ${ARGN})
1402 add_llvm_library(${name} ${ARGN})
1405 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1406 endmacro(add_llvm_example_library name)
1408 # This is a macro that is used to create targets for executables that are needed
1409 # for development, but that are not intended to be installed by default.
1410 macro(add_llvm_utility name)
1411 if ( NOT LLVM_BUILD_UTILS )
1412 set(EXCLUDE_FROM_ALL ON)
1415 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
1416 set_target_properties(${name} PROPERTIES FOLDER "Utils")
1417 if ( ${name} IN_LIST LLVM_TOOLCHAIN_UTILITIES OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1418 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
1419 get_target_export_arg(${name} LLVM export_to_llvmexports)
1420 install(TARGETS ${name}
1421 ${export_to_llvmexports}
1422 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
1425 if (NOT LLVM_ENABLE_IDE)
1426 add_llvm_install_targets(install-${name}
1430 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1431 elseif(LLVM_BUILD_UTILS)
1432 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
1435 endmacro(add_llvm_utility name)
1437 macro(add_llvm_fuzzer name)
1438 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
1439 if( LLVM_LIB_FUZZING_ENGINE )
1440 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1441 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1442 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
1443 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1444 elseif( LLVM_USE_SANITIZE_COVERAGE )
1445 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
1446 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1447 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1448 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1449 elseif( ARG_DUMMY_MAIN )
1450 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
1451 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1455 macro(add_llvm_target target_name)
1456 include_directories(BEFORE
1457 ${CMAKE_CURRENT_BINARY_DIR}
1458 ${CMAKE_CURRENT_SOURCE_DIR})
1459 add_llvm_component_library(LLVM${target_name} ${ARGN})
1460 set( CURRENT_LLVM_TARGET LLVM${target_name} )
1461 endmacro(add_llvm_target)
1463 function(canonicalize_tool_name name output)
1464 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1465 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1466 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1467 set(${output} "${nameUPPER}" PARENT_SCOPE)
1468 endfunction(canonicalize_tool_name)
1470 # Custom add_subdirectory wrapper
1471 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1472 # path if it differs from the name.
1473 function(add_llvm_subdirectory project type name)
1474 set(add_llvm_external_dir "${ARGN}")
1475 if("${add_llvm_external_dir}" STREQUAL "")
1476 set(add_llvm_external_dir ${name})
1478 canonicalize_tool_name(${name} nameUPPER)
1479 set(canonical_full_name ${project}_${type}_${nameUPPER})
1480 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1481 if(already_processed)
1484 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1486 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1487 # Treat it as in-tree subproject.
1488 option(${canonical_full_name}_BUILD
1489 "Whether to build ${name} as part of ${project}" On)
1490 mark_as_advanced(${project}_${type}_${name}_BUILD)
1491 if(${canonical_full_name}_BUILD)
1492 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
1495 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1496 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1497 CACHE PATH "Path to ${name} source directory")
1498 set(${canonical_full_name}_BUILD_DEFAULT ON)
1499 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1500 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1502 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
1503 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1505 option(${canonical_full_name}_BUILD
1506 "Whether to build ${name} as part of LLVM"
1507 ${${canonical_full_name}_BUILD_DEFAULT})
1508 if (${canonical_full_name}_BUILD)
1509 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1510 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1511 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1512 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1518 # Add external project that may want to be built as part of llvm such as Clang,
1519 # lld, and Polly. This adds two options. One for the source directory of the
1520 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1521 # enable or disable building it with everything else.
1522 # Additional parameter can be specified as the name of directory.
1523 macro(add_llvm_external_project name)
1524 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1527 macro(add_llvm_tool_subdirectory name)
1528 add_llvm_external_project(${name})
1529 endmacro(add_llvm_tool_subdirectory)
1531 macro(add_custom_linker_flags name)
1532 if (LLVM_${name}_LINKER_FLAGS)
1533 message(DEBUG "Applying ${LLVM_${name}_LINKER_FLAGS} to ${name}")
1534 target_link_options(${name} PRIVATE ${LLVM_${name}_LINKER_FLAGS})
1538 function(get_project_name_from_src_var var output)
1539 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1540 MACHED_TOOL "${var}")
1542 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1544 set(${output} PARENT_SCOPE)
1548 function(create_subdirectory_options project type)
1549 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1550 foreach(dir ${sub-dirs})
1551 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1552 canonicalize_tool_name(${dir} name)
1553 option(${project}_${type}_${name}_BUILD
1554 "Whether to build ${name} as part of ${project}" On)
1555 mark_as_advanced(${project}_${type}_${name}_BUILD)
1558 endfunction(create_subdirectory_options)
1560 function(create_llvm_tool_options)
1561 create_subdirectory_options(LLVM TOOL)
1562 endfunction(create_llvm_tool_options)
1564 function(llvm_add_implicit_projects project)
1565 set(list_of_implicit_subdirs "")
1566 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1567 foreach(dir ${sub-dirs})
1568 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1569 canonicalize_tool_name(${dir} name)
1570 # I don't like special casing things by order, but the llvm-driver ends up
1571 # linking the object libraries from all the tools that opt-in, so adding
1572 # it separately at the end is probably the simplest case.
1573 if("${name}" STREQUAL "LLVM_DRIVER")
1576 if (${project}_TOOL_${name}_BUILD)
1577 get_filename_component(fn "${dir}" NAME)
1578 list(APPEND list_of_implicit_subdirs "${fn}")
1583 foreach(external_proj ${list_of_implicit_subdirs})
1584 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1586 endfunction(llvm_add_implicit_projects)
1588 function(add_llvm_implicit_projects)
1589 llvm_add_implicit_projects(LLVM)
1590 endfunction(add_llvm_implicit_projects)
1592 # Generic support for adding a unittest.
1593 function(add_unittest test_suite test_name)
1594 if( NOT LLVM_BUILD_TESTS )
1595 set(EXCLUDE_FROM_ALL ON)
1598 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1599 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1601 # Some parts of gtest rely on this GNU extension, don't warn on it.
1602 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1603 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1606 if (NOT DEFINED LLVM_REQUIRES_RTTI)
1607 set(LLVM_REQUIRES_RTTI OFF)
1610 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1611 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1613 # The runtime benefits of LTO don't outweight the compile time costs for tests.
1615 if((UNIX OR MINGW) AND LINKER_IS_LLD)
1616 set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1617 LINK_FLAGS " -Wl,--lto-O0")
1618 elseif(LINKER_IS_LLD_LINK)
1619 set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1620 LINK_FLAGS " /opt:lldlto=0")
1621 elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
1622 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1623 LINK_FLAGS " -Wl,-mllvm,-O0")
1627 target_link_options(${test_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
1629 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1630 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1631 # libpthreads overrides some standard library symbols, so main
1632 # executable must be linked with it in order to provide consistent
1633 # API for all shared libaries loaded by this executable.
1634 target_link_libraries(${test_name} PRIVATE llvm_gtest_main llvm_gtest ${LLVM_PTHREAD_LIB})
1636 add_dependencies(${test_suite} ${test_name})
1637 get_target_property(test_suite_folder ${test_suite} FOLDER)
1638 if (test_suite_folder)
1639 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1643 # Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1645 function(add_unittest_with_input_files test_suite test_name)
1646 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1648 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1649 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1651 add_unittest(${test_suite} ${test_name} ${ARGN})
1654 # Generic support for adding a benchmark.
1655 function(add_benchmark benchmark_name)
1656 if( NOT LLVM_BUILD_BENCHMARKS )
1657 set(EXCLUDE_FROM_ALL ON)
1660 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1661 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1662 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1663 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1664 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1667 # This function canonicalize the CMake variables passed by names
1668 # from CMake boolean to 0/1 suitable for passing into Python or C++,
1670 function(llvm_canonicalize_cmake_booleans)
1671 foreach(var ${ARGN})
1673 set(${var} 1 PARENT_SCOPE)
1675 set(${var} 0 PARENT_SCOPE)
1678 endfunction(llvm_canonicalize_cmake_booleans)
1680 macro(set_llvm_build_mode)
1681 # Configuration-time: See Unit/lit.site.cfg.in
1682 if (CMAKE_CFG_INTDIR STREQUAL ".")
1683 set(LLVM_BUILD_MODE ".")
1685 set(LLVM_BUILD_MODE "%(build_mode)s")
1689 # Takes a list of path names in pathlist and a base directory, and returns
1690 # a list of paths relative to the base directory in out_pathlist.
1691 # Paths that are on a different drive than the basedir (on Windows) or that
1692 # contain symlinks are returned absolute.
1693 # Use with LLVM_LIT_PATH_FUNCTION below.
1694 function(make_paths_relative out_pathlist basedir pathlist)
1695 # Passing ARG_PATH_VALUES as-is to execute_process() makes cmake strip
1696 # empty list entries. So escape the ;s in the list and do the splitting
1697 # ourselves. cmake has no relpath function, so use Python for that.
1698 string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}")
1699 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "\n
1703 if not p or p == os.path.dirname(p): return False\n
1704 return os.path.islink(p) or haslink(os.path.dirname(p))\n
1706 if not p: return ''\n
1707 if os.path.splitdrive(p)[0] != os.path.splitdrive(base)[0]: return p\n
1708 if haslink(p) or haslink(base): return p\n
1709 return os.path.relpath(p, base)\n
1710 if len(sys.argv) < 3: sys.exit(0)\n
1711 sys.stdout.write(';'.join(relpath(p) for p in sys.argv[2].split(';')))"
1714 OUTPUT_VARIABLE pathlist_relative
1715 ERROR_VARIABLE error
1716 RESULT_VARIABLE result)
1717 if (NOT result EQUAL 0)
1718 message(FATAL_ERROR "make_paths_relative() failed due to error '${result}', with stderr\n${error}")
1720 set(${out_pathlist} "${pathlist_relative}" PARENT_SCOPE)
1723 # Converts a file that's relative to the current python file to an absolute
1724 # path. Since this uses __file__, it has to be emitted into python files that
1725 # use it and can't be in a lit module. Use with make_paths_relative().
1726 string(CONCAT LLVM_LIT_PATH_FUNCTION
1727 "# Allow generated file to be relocatable.\n"
1731 " if not p: return ''\n"
1732 " # Follows lit.util.abs_path_preserve_drive, which cannot be imported here.\n"
1733 " if platform.system() == 'Windows':\n"
1734 " return os.path.abspath(os.path.join(os.path.dirname(__file__), p))\n"
1736 " return os.path.realpath(os.path.join(os.path.dirname(__file__), p))\n"
1739 # This function provides an automatic way to 'configure'-like generate a file
1740 # based on a set of common and custom variables, specifically targeting the
1741 # variables needed for the 'lit.site.cfg' files. This function bundles the
1742 # common variables that any Lit instance is likely to need, and custom
1743 # variables can be passed in.
1744 # The keyword PATHS is followed by a list of cmake variable names that are
1745 # mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that
1746 # list are treated as paths that are relative to the directory the generated
1747 # lit.cfg.py file is in, and the `path()` function converts the relative
1748 # path back to absolute form. This makes it possible to move a build directory
1749 # containing lit.cfg.py files from one machine to another.
1750 function(configure_lit_site_cfg site_in site_out)
1751 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;PATHS" ${ARGN})
1753 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1754 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1755 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1758 foreach(c ${LLVM_TARGETS_TO_BUILD})
1759 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1761 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1763 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1765 set_llvm_build_mode()
1767 # For standalone builds of subprojects, these might not be the build tree but
1768 # a provided binary tree.
1769 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1770 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1771 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1772 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1773 # Like LLVM_{TOOLS,LIBS}_DIR, but pointing at the build tree.
1774 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" CURRENT_TOOLS_DIR "${LLVM_RUNTIME_OUTPUT_INTDIR}")
1775 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" CURRENT_LIBS_DIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
1776 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1778 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1779 # plugins. We may rename it.
1780 if(LLVM_ENABLE_PLUGINS)
1781 set(ENABLE_SHARED "1")
1783 set(ENABLE_SHARED "0")
1786 if(LLVM_ENABLE_ASSERTIONS)
1787 set(ENABLE_ASSERTIONS "1")
1789 set(ENABLE_ASSERTIONS "0")
1792 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1793 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1795 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1796 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1797 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1799 string(CONCAT LIT_SITE_CFG_IN_HEADER
1800 "# Autogenerated from ${site_in}\n# Do not edit!\n\n"
1801 "${LLVM_LIT_PATH_FUNCTION}"
1804 # Override config_target_triple (and the env)
1805 if(LLVM_TARGET_TRIPLE_ENV)
1806 # This is expanded into the heading.
1807 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}"
1809 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1810 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${LLVM_TARGET_TRIPLE}\")\n"
1813 # This is expanded to; config.target_triple = ""+config.target_triple+""
1814 set(LLVM_TARGET_TRIPLE "\"+config.target_triple+\"")
1818 # Walk ARG_PATHS and collect the current value of the variables in there.
1819 # list(APPEND) ignores empty elements exactly if the list is empty,
1820 # so start the list with a dummy element and drop it, to make sure that
1821 # even empty values make it into the values list.
1822 set(ARG_PATH_VALUES "dummy")
1823 foreach(path ${ARG_PATHS})
1824 list(APPEND ARG_PATH_VALUES "${${path}}")
1826 list(REMOVE_AT ARG_PATH_VALUES 0)
1828 get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY)
1829 make_paths_relative(
1830 ARG_PATH_VALUES_RELATIVE "${OUTPUT_DIR}" "${ARG_PATH_VALUES}")
1832 list(LENGTH ARG_PATHS len_paths)
1833 list(LENGTH ARG_PATH_VALUES len_path_values)
1834 list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels)
1835 if ((NOT ${len_paths} EQUAL ${len_path_values}) OR
1836 (NOT ${len_paths} EQUAL ${len_path_value_rels}))
1837 message(SEND_ERROR "PATHS lengths got confused")
1840 # Transform variables mentioned in ARG_PATHS to relative paths for
1841 # the configure_file() call. Variables are copied to subscopeds by cmake,
1842 # so this only modifies the local copy of the variables.
1843 math(EXPR arg_path_limit "${len_paths} - 1")
1844 foreach(i RANGE ${arg_path_limit})
1845 list(GET ARG_PATHS ${i} val1)
1846 list(GET ARG_PATH_VALUES_RELATIVE ${i} val2)
1847 set(${val1} ${val2})
1851 configure_file(${site_in} ${site_out} @ONLY)
1853 if (EXISTS "${ARG_MAIN_CONFIG}")
1854 # Remember main config / generated site config for llvm-lit.in.
1855 get_property(LLVM_LIT_CONFIG_FILES GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES)
1856 list(APPEND LLVM_LIT_CONFIG_FILES "${ARG_MAIN_CONFIG}" "${site_out}")
1857 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES ${LLVM_LIT_CONFIG_FILES})
1861 function(dump_all_cmake_variables)
1862 get_cmake_property(_variableNames VARIABLES)
1863 foreach (_variableName ${_variableNames})
1864 message(STATUS "${_variableName}=${${_variableName}}")
1868 function(get_llvm_lit_path base_dir file_name)
1869 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1871 if (ARG_ALLOW_EXTERNAL)
1872 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1873 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1874 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1877 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1878 if (EXISTS ${LLVM_EXTERNAL_LIT})
1879 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1880 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1881 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1882 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1884 elseif (NOT DEFINED CACHE{LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE})
1885 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1886 set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "")
1891 set(lit_file_name "llvm-lit")
1892 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
1893 # llvm-lit needs suffix.py for multiprocess to find a main module.
1894 set(lit_file_name "${lit_file_name}.py")
1896 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1898 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1899 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1900 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1903 # Allow individual projects to provide an override
1904 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1905 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1906 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1907 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1909 set(LLVM_LIT_BASE_DIR "")
1912 # Cache this so we don't have to do it again and have subsequent calls
1913 # potentially disagree on the value.
1914 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1915 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1918 # A raw function to create a lit target. This is used to implement the testuite
1919 # management functions.
1920 function(add_lit_target target comment)
1921 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1922 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1923 separate_arguments(LIT_ARGS)
1924 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1925 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1928 # Get the path to the lit to *run* tests with. This can be overriden by
1929 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1936 set(LIT_COMMAND "${Python3_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1937 list(APPEND LIT_COMMAND ${LIT_ARGS})
1938 foreach(param ${ARG_PARAMS})
1939 list(APPEND LIT_COMMAND --param ${param})
1941 if (ARG_UNPARSED_ARGUMENTS)
1942 add_custom_target(${target}
1943 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1944 COMMENT "${comment}"
1948 add_custom_target(${target}
1949 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1950 message(STATUS "${target} does nothing.")
1954 add_dependencies(${target} ${ARG_DEPENDS})
1957 # Tests should be excluded from "Build Solution".
1958 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1961 # Convert a target name like check-clang to a variable name like CLANG.
1962 function(umbrella_lit_testsuite_var target outvar)
1963 if (NOT target MATCHES "^check-")
1964 message(FATAL_ERROR "umbrella lit suites must be check-*, not '${target}'")
1966 string(SUBSTRING "${target}" 6 -1 var)
1967 string(REPLACE "-" "_" var ${var})
1968 string(TOUPPER "${var}" var)
1969 set(${outvar} "${var}" PARENT_SCOPE)
1972 # Start recording all lit test suites for a combined 'check-foo' target.
1973 # The recording continues until umbrella_lit_testsuite_end() creates the target.
1974 function(umbrella_lit_testsuite_begin target)
1975 umbrella_lit_testsuite_var(${target} name)
1976 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_UMBRELLAS ${name})
1979 # Create a combined 'check-foo' target for a set of related test suites.
1980 # It runs all suites added since the matching umbrella_lit_testsuite_end() call.
1981 # Tests marked EXCLUDE_FROM_CHECK_ALL are not gathered.
1982 function(umbrella_lit_testsuite_end target)
1983 umbrella_lit_testsuite_var(${target} name)
1985 get_property(testsuites GLOBAL PROPERTY LLVM_${name}_LIT_TESTSUITES)
1986 get_property(params GLOBAL PROPERTY LLVM_${name}_LIT_PARAMS)
1987 get_property(depends GLOBAL PROPERTY LLVM_${name}_LIT_DEPENDS)
1988 get_property(extra_args GLOBAL PROPERTY LLVM_${name}_LIT_EXTRA_ARGS)
1989 # Additional test targets are not gathered, but may be set externally.
1990 get_property(additional_test_targets
1991 GLOBAL PROPERTY LLVM_${name}_ADDITIONAL_TEST_TARGETS)
1993 string(TOLOWER ${name} name)
1994 add_lit_target(${target}
1995 "Running ${name} regression tests"
1998 DEPENDS ${depends} ${additional_test_targets}
2003 # A function to add a set of lit test suites to be driven through 'check-*' targets.
2004 function(add_lit_testsuite target comment)
2005 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
2007 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
2008 if(NOT ARG_EXCLUDE_FROM_CHECK_ALL)
2009 get_property(gather_names GLOBAL PROPERTY LLVM_LIT_UMBRELLAS)
2010 foreach(name ${gather_names})
2011 # Register the testsuites, params and depends for the umbrella check rule.
2012 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
2013 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_PARAMS ${ARG_PARAMS})
2014 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_DEPENDS ${ARG_DEPENDS})
2015 set_property(GLOBAL APPEND PROPERTY LLVM_${name}_LIT_EXTRA_ARGS ${ARG_ARGS})
2019 # Produce a specific suffixed check rule.
2020 add_lit_target(${target} ${comment}
2021 ${ARG_UNPARSED_ARGUMENTS}
2022 PARAMS ${ARG_PARAMS}
2023 DEPENDS ${ARG_DEPENDS}
2028 function(add_lit_testsuites project directory)
2029 if (NOT LLVM_ENABLE_IDE)
2030 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "FOLDER" "PARAMS;DEPENDS;ARGS" ${ARGN})
2033 set(ARG_FOLDER "Test Subdirectories")
2036 # Search recursively for test directories by assuming anything not
2037 # in a directory called Inputs contains tests.
2038 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
2039 foreach(lit_suite ${to_process})
2040 if(NOT IS_DIRECTORY ${lit_suite})
2043 string(FIND ${lit_suite} Inputs is_inputs)
2044 string(FIND ${lit_suite} Output is_output)
2045 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
2049 # Create a check- target for the directory.
2050 string(REPLACE ${directory} "" name_slash ${lit_suite})
2052 string(REPLACE "/" "-" name_slash ${name_slash})
2053 string(REPLACE "\\" "-" name_dashes ${name_slash})
2054 string(TOLOWER "${project}${name_dashes}" name_var)
2055 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
2057 ${EXCLUDE_FROM_CHECK_ALL}
2058 PARAMS ${ARG_PARAMS}
2059 DEPENDS ${ARG_DEPENDS}
2062 set_target_properties(check-${name_var} PROPERTIES FOLDER ${ARG_FOLDER})
2068 function(llvm_install_library_symlink name dest type)
2069 cmake_parse_arguments(ARG "" "COMPONENT" "" ${ARGN})
2070 foreach(path ${CMAKE_MODULE_PATH})
2071 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
2072 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
2077 set(component ${ARG_COMPONENT})
2079 set(component ${name})
2082 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
2083 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
2085 if(LLVM_USE_SYMLINKS)
2086 set(LLVM_LINK_OR_COPY create_symlink)
2088 set(LLVM_LINK_OR_COPY copy)
2091 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
2092 if(WIN32 AND "${type}" STREQUAL "SHARED")
2093 set(output_dir "${CMAKE_INSTALL_BINDIR}")
2096 install(SCRIPT ${INSTALL_SYMLINK}
2097 CODE "install_symlink(\"${full_name}\" \"${full_dest}\" \"${output_dir}\" \"${LLVM_LINK_OR_COPY}\")"
2098 COMPONENT ${component})
2102 function(llvm_install_symlink project name dest)
2103 get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
2104 if(LLVM_TOOL_LLVM_DRIVER_BUILD
2105 AND ${dest} IN_LIST LLVM_DRIVER_TOOLS
2106 AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${dest} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
2110 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
2111 foreach(path ${CMAKE_MODULE_PATH})
2112 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
2113 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
2119 set(component ${ARG_COMPONENT})
2121 if(ARG_ALWAYS_GENERATE)
2122 set(component ${dest})
2124 set(component ${name})
2128 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
2129 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
2130 if (${dest} STREQUAL "llvm-driver")
2131 set(full_dest llvm${CMAKE_EXECUTABLE_SUFFIX})
2134 if(LLVM_USE_SYMLINKS)
2135 set(LLVM_LINK_OR_COPY create_symlink)
2137 set(LLVM_LINK_OR_COPY copy)
2140 set(output_dir "${${project}_TOOLS_INSTALL_DIR}")
2142 install(SCRIPT ${INSTALL_SYMLINK}
2143 CODE "install_symlink(\"${full_name}\" \"${full_dest}\" \"${output_dir}\" \"${LLVM_LINK_OR_COPY}\")"
2144 COMPONENT ${component})
2146 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
2147 add_llvm_install_targets(install-${name}
2148 DEPENDS ${name} ${dest}
2149 COMPONENT ${component}
2154 function(llvm_add_tool_symlink project link_name target)
2155 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
2157 get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
2159 if (${target} IN_LIST LLVM_DRIVER_TOOLS)
2160 set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_ALIASES_${target} ${link_name})
2162 set(dest_binary "$<TARGET_FILE:${target}>")
2164 # This got a bit gross... For multi-configuration generators the target
2165 # properties return the resolved value of the string, not the build system
2166 # expression. To reconstruct the platform-agnostic path we have to do some
2167 # magic. First we grab one of the types, and a type-specific path. Then from
2168 # the type-specific path we find the last occurrence of the type in the path,
2169 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
2171 if(NOT ARG_OUTPUT_DIR)
2172 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
2173 # the same directory.
2174 if(LLVM_USE_SYMLINKS)
2175 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
2177 if(CMAKE_CONFIGURATION_TYPES)
2178 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
2179 string(TOUPPER ${first_type} first_type_upper)
2180 set(first_type_suffix _${first_type_upper})
2182 get_target_property(target_type ${target} TYPE)
2183 if(${target_type} STREQUAL "STATIC_LIBRARY")
2184 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
2185 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
2186 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
2188 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
2190 if(CMAKE_CONFIGURATION_TYPES)
2191 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
2192 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
2193 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
2194 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
2195 path_suffix ${path_suffix})
2196 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
2200 if(LLVM_USE_SYMLINKS)
2201 set(LLVM_LINK_OR_COPY create_symlink)
2203 set(LLVM_LINK_OR_COPY copy)
2206 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
2208 set(target_name ${link_name})
2209 if(TARGET ${link_name})
2210 set(target_name ${link_name}-link)
2214 if(ARG_ALWAYS_GENERATE)
2215 set_property(DIRECTORY APPEND PROPERTY
2216 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
2217 add_custom_command(TARGET ${target} POST_BUILD
2218 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
2220 add_custom_command(OUTPUT ${output_path}
2221 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
2224 # TODO: Make use of generator expressions below once CMake 3.19 or higher is the minimum supported version.
2225 set(should_build_all)
2226 get_target_property(target_excluded_from_all ${target} EXCLUDE_FROM_ALL)
2227 if (NOT target_excluded_from_all)
2228 set(should_build_all ALL)
2230 add_custom_target(${target_name} ${should_build_all} DEPENDS ${target} ${output_path})
2231 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
2233 # Make sure both the link and target are toolchain tools
2234 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
2235 set(TOOL_IS_TOOLCHAIN ON)
2238 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
2239 llvm_install_symlink("${project}" ${link_name} ${target})
2244 function(add_llvm_tool_symlink link_name target)
2245 llvm_add_tool_symlink(LLVM ${ARGV})
2248 function(llvm_externalize_debuginfo name)
2249 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
2253 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
2256 set(CMAKE_STRIP xcrun strip)
2258 set(strip_command COMMAND ${CMAKE_STRIP} -S -x $<TARGET_FILE:${name}>)
2260 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
2265 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
2266 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
2271 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
2273 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
2274 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
2276 set(output_path "-o=${output_name}")
2279 if(CMAKE_CXX_FLAGS MATCHES "-flto"
2280 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
2282 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
2283 set_property(TARGET ${name} APPEND_STRING PROPERTY
2284 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
2286 if(NOT CMAKE_DSYMUTIL)
2287 set(CMAKE_DSYMUTIL xcrun dsymutil)
2289 add_custom_command(TARGET ${name} POST_BUILD
2290 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
2294 add_custom_command(TARGET ${name} POST_BUILD
2295 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
2296 ${strip_command} -R .gnu_debuglink
2297 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
2302 # Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
2303 function(llvm_codesign name)
2304 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
2306 if(NOT LLVM_CODESIGNING_IDENTITY)
2310 if(CMAKE_GENERATOR STREQUAL "Xcode")
2311 set_target_properties(${name} PROPERTIES
2312 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
2314 if(DEFINED ARG_ENTITLEMENTS)
2315 set_target_properties(${name} PROPERTIES
2316 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
2319 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
2320 if(NOT CMAKE_CODESIGN)
2321 set(CMAKE_CODESIGN xcrun codesign)
2323 if(NOT CMAKE_CODESIGN_ALLOCATE)
2325 COMMAND xcrun -f codesign_allocate
2326 OUTPUT_STRIP_TRAILING_WHITESPACE
2327 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
2330 if(DEFINED ARG_ENTITLEMENTS)
2331 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
2334 if (NOT ARG_BUNDLE_PATH)
2335 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
2338 # ld64 now always codesigns the binaries it creates. Apply the force arg
2339 # unconditionally so that we can - for example - add entitlements to the
2340 # targets that need it.
2341 set(force_flag "-f")
2344 TARGET ${name} POST_BUILD
2345 COMMAND ${CMAKE_COMMAND} -E
2346 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
2347 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
2348 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
2353 function(llvm_setup_rpath name)
2354 if(CMAKE_INSTALL_RPATH)
2358 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
2359 set(extra_libdir ${LLVM_LIBRARY_DIR})
2360 elseif(LLVM_BUILD_LIBRARY_DIR)
2361 set(extra_libdir ${LLVM_LIBRARY_DIR})
2365 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
2366 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2367 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND BUILD_SHARED_LIBS)
2368 # $ORIGIN is not interpreted at link time by aix ld.
2369 # Since BUILD_SHARED_LIBS is only recommended for use by developers,
2370 # hardcode the rpath to build/install lib dir first in this mode.
2371 # FIXME: update this when there is better solution.
2372 set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_PREFIX}/lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2374 set(_build_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
2375 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}")
2376 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
2377 set_property(TARGET ${name} APPEND_STRING PROPERTY
2378 LINK_FLAGS " -Wl,-z,origin ")
2380 if(LLVM_LINKER_IS_GNULD AND NOT ${LLVM_LIBRARY_OUTPUT_INTDIR} STREQUAL "")
2381 # $ORIGIN is not interpreted at link time by ld.bfd
2382 set_property(TARGET ${name} APPEND_STRING PROPERTY
2383 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
2389 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set and not
2390 # building for macOS or AIX, as those platforms seemingly require it.
2391 # On AIX, the tool chain doesn't support modifying rpaths/libpaths for XCOFF
2392 # on install at the moment, so BUILD_WITH_INSTALL_RPATH is required.
2393 if("${CMAKE_BUILD_RPATH}" STREQUAL "")
2394 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin|AIX")
2395 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
2397 set_property(TARGET ${name} APPEND PROPERTY BUILD_RPATH "${_build_rpath}")
2401 set_target_properties(${name} PROPERTIES
2402 INSTALL_RPATH "${_install_rpath}"
2403 ${_install_name_dir})
2406 function(setup_dependency_debugging name)
2407 if(NOT LLVM_DEPENDENCY_DEBUGGING)
2411 if("intrinsics_gen" IN_LIST ARGN)
2415 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
2416 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
2418 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
2419 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
2422 # If the sources at the given `path` are under version control, set `out_var`
2423 # to the the path of a file which will be modified when the VCS revision
2424 # changes, attempting to create that file if it does not exist; if no such
2425 # file exists and one cannot be created, instead set `out_var` to the
2428 # If the sources are not under version control, do not define `out_var`.
2429 function(find_first_existing_vc_file path out_var)
2430 if(NOT EXISTS "${path}")
2435 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
2436 WORKING_DIRECTORY ${path}
2437 RESULT_VARIABLE git_result
2438 OUTPUT_VARIABLE git_output
2440 if(git_result EQUAL 0)
2441 string(STRIP "${git_output}" git_output)
2442 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
2443 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
2444 if (NOT EXISTS "${git_dir}/logs/HEAD")
2445 execute_process(COMMAND ${CMAKE_COMMAND} -E touch HEAD
2446 WORKING_DIRECTORY "${git_dir}/logs"
2447 RESULT_VARIABLE touch_head_result
2449 if (NOT touch_head_result EQUAL 0)
2450 set(${out_var} "" PARENT_SCOPE)
2454 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)
2459 function(get_host_tool_path tool_name setting_name exe_var_name target_var_name)
2460 set(${setting_name}_DEFAULT "")
2462 if(LLVM_NATIVE_TOOL_DIR)
2463 if(EXISTS "${LLVM_NATIVE_TOOL_DIR}/${tool_name}${LLVM_HOST_EXECUTABLE_SUFFIX}")
2464 set(${setting_name}_DEFAULT "${LLVM_NATIVE_TOOL_DIR}/${tool_name}${LLVM_HOST_EXECUTABLE_SUFFIX}")
2468 set(${setting_name} "${${setting_name}_DEFAULT}" CACHE
2469 STRING "Host ${tool_name} executable. Saves building if cross-compiling.")
2472 set(exe_name ${${setting_name}})
2474 elseif(LLVM_USE_HOST_TOOLS)
2475 get_native_tool_path(${tool_name} exe_name)
2476 set(target_name ${exe_name})
2478 set(exe_name $<TARGET_FILE:${tool_name}>)
2479 set(target_name ${tool_name})
2481 set(${exe_var_name} "${exe_name}" CACHE STRING "")
2482 set(${target_var_name} "${target_name}" CACHE STRING "")
2485 function(setup_host_tool tool_name setting_name exe_var_name target_var_name)
2486 get_host_tool_path(${tool_name} ${setting_name} ${exe_var_name} ${target_var_name})
2487 # Set up a native tool build if necessary
2488 if(LLVM_USE_HOST_TOOLS AND NOT ${setting_name})
2489 build_native_tool(${tool_name} exe_name DEPENDS ${tool_name})
2490 add_custom_target(${target_var_name} DEPENDS ${exe_name})
2494 # Adds the unittests folder if gtest is available.
2495 function(llvm_add_unittests tests_added)
2496 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
2497 add_subdirectory(unittests)
2498 set(${tests_added} ON PARENT_SCOPE)
2500 message(WARNING "gtest not found, unittests will not be available")