1 include(LLVMProcessSources)
3 include(DetermineGCCCompatible)
5 function(llvm_update_compile_flags name)
6 get_property(sources TARGET ${name} PROPERTY SOURCES)
7 if("${sources}" MATCHES "\\.c(;|$)")
8 set(update_src_props ON)
11 list(APPEND LLVM_COMPILE_CFLAGS " ${LLVM_COMPILE_FLAGS}")
13 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to
15 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH)
16 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
17 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
18 set(LLVM_REQUIRES_RTTI ON)
21 list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
24 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
25 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
26 if(NOT LLVM_ENABLE_UNWIND_TABLES)
27 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables")
28 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables")
31 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
32 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
33 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
34 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh")
38 # LLVM_REQUIRES_RTTI is an internal flag that individual
39 # targets can use to force RTTI
40 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
41 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
42 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
43 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
44 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
45 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
47 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
48 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
49 list(APPEND LLVM_COMPILE_FLAGS "-qnortti")
52 list(APPEND LLVM_COMPILE_FLAGS "/GR")
56 # - LLVM_COMPILE_FLAGS is list.
57 # - PROPERTY COMPILE_FLAGS is string.
58 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
59 string(REPLACE ";" " " target_compile_cflags " ${LLVM_COMPILE_CFLAGS}")
62 foreach(fn ${sources})
63 get_filename_component(suf ${fn} EXT)
64 if("${suf}" STREQUAL ".cpp")
65 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
66 COMPILE_FLAGS "${target_compile_flags}")
68 if("${suf}" STREQUAL ".c")
69 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
70 COMPILE_FLAGS "${target_compile_cflags}")
74 # Update target props, since all sources are C++.
75 set_property(TARGET ${name} APPEND_STRING PROPERTY
76 COMPILE_FLAGS "${target_compile_flags}")
79 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
82 function(add_llvm_symbol_exports target_name export_file)
83 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
84 set(native_export_file "${target_name}.exports")
85 add_custom_command(OUTPUT ${native_export_file}
86 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
87 DEPENDS ${export_file}
89 COMMENT "Creating export file for ${target_name}")
90 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
91 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
92 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
93 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
94 LINK_FLAGS " -Wl,-bE:${export_file}")
95 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
96 # Gold and BFD ld require a version script rather than a plain list.
97 set(native_export_file "${target_name}.exports")
98 # FIXME: Don't write the "local:" line on OpenBSD.
99 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
100 add_custom_command(OUTPUT ${native_export_file}
101 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file}
102 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
103 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
104 COMMAND echo " local: *;" >> ${native_export_file}
105 COMMAND echo "};" >> ${native_export_file}
106 DEPENDS ${export_file}
108 COMMENT "Creating export file for ${target_name}")
109 if (${LLVM_LINKER_IS_SOLARISLD})
110 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
111 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
114 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
117 set(native_export_file "${target_name}.def")
119 add_custom_command(OUTPUT ${native_export_file}
120 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
121 < ${export_file} > ${native_export_file}
122 DEPENDS ${export_file}
124 COMMENT "Creating export file for ${target_name}")
125 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
127 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
129 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
130 LINK_FLAGS " ${export_file_linker_flag}")
133 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
134 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
136 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
138 get_filename_component(extension ${src} EXT)
139 if(extension STREQUAL ".cpp")
140 set(first_source_file ${src})
145 # Force re-linking when the exports file changes. Actually, it
146 # forces recompilation of the source file. The LINK_DEPENDS target
147 # property only works for makefile-based generators.
148 # FIXME: This is not safe because this will create the same target
149 # ${native_export_file} in several different file:
150 # - One where we emitted ${target_name}_exports
151 # - One where we emitted the build command for the following object.
152 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
153 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
155 set_property(DIRECTORY APPEND
156 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
158 add_dependencies(${target_name} ${target_name}_exports)
160 # Add dependency to *_exports later -- CMake issue 14747
161 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
162 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
163 endfunction(add_llvm_symbol_exports)
167 COMMAND "${CMAKE_LINKER}" -v
168 ERROR_VARIABLE stderr
170 set(LLVM_LINKER_DETECTED YES)
171 if("${stderr}" MATCHES "PROJECT:ld64")
172 set(LLVM_LINKER_IS_LD64 YES)
173 message(STATUS "Linker detection: ld64")
175 set(LLVM_LINKER_DETECTED NO)
176 message(STATUS "Linker detection: unknown")
179 # Detect what linker we have here
180 if( LLVM_USE_LINKER )
181 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
183 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
184 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version)
188 OUTPUT_VARIABLE stdout
189 ERROR_VARIABLE stderr
191 set(LLVM_LINKER_DETECTED YES)
192 if("${stdout}" MATCHES "GNU gold")
193 set(LLVM_LINKER_IS_GOLD YES)
194 message(STATUS "Linker detection: GNU Gold")
195 elseif("${stdout}" MATCHES "^LLD")
196 set(LLVM_LINKER_IS_LLD YES)
197 message(STATUS "Linker detection: LLD")
198 elseif("${stdout}" MATCHES "GNU ld")
199 set(LLVM_LINKER_IS_GNULD YES)
200 message(STATUS "Linker detection: GNU ld")
201 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
202 "${stdout}" MATCHES "Solaris Link Editors")
203 set(LLVM_LINKER_IS_SOLARISLD YES)
204 message(STATUS "Linker detection: Solaris ld")
206 set(LLVM_LINKER_DETECTED NO)
207 message(STATUS "Linker detection: unknown")
211 function(add_link_opts target_name)
212 # Don't use linker optimizations in debug builds since it slows down the
213 # linker in a context where the optimizations are not important.
214 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
216 # Pass -O3 to the linker. This enabled different optimizations on different
218 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
219 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
220 LINK_FLAGS " -Wl,-O3")
223 if(LLVM_LINKER_IS_GOLD)
224 # With gold gc-sections is always safe.
225 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
226 LINK_FLAGS " -Wl,--gc-sections")
227 # Note that there is a bug with -Wl,--icf=safe so it is not safe
228 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
231 if(NOT LLVM_NO_DEAD_STRIP)
232 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
233 # ld64's implementation of -dead_strip breaks tools that use plugins.
234 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
235 LINK_FLAGS " -Wl,-dead_strip")
236 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
237 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
238 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
239 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD AND
240 NOT ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD|AIX")
241 # Object files are compiled with -ffunction-data-sections.
242 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
243 # tools that use plugins. Always pass --gc-sections once we require
245 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
246 LINK_FLAGS " -Wl,--gc-sections")
248 else() #LLVM_NO_DEAD_STRIP
249 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
250 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
251 LINK_FLAGS " -Wl,-bnogc")
255 endfunction(add_link_opts)
257 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
258 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
259 # or a certain builder, for eaxample, msbuild.exe, would be confused.
260 function(set_output_directory target)
261 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
263 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
264 # It affects output of add_library(MODULE).
267 set(module_dir ${ARG_BINARY_DIR})
269 set(module_dir ${ARG_LIBRARY_DIR})
271 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
272 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
273 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
275 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
276 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
279 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
280 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
283 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
284 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
289 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
292 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
295 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
300 # If on Windows and building with MSVC, add the resource script containing the
301 # VERSIONINFO data to the project. This embeds version resource information
302 # into the output .exe or .dll.
303 # TODO: Enable for MinGW Windows builds too.
305 function(add_windows_version_resource_file OUT_VAR)
307 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
308 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
309 if(EXISTS ${resource_file})
310 set(sources ${sources} ${resource_file})
311 source_group("Resource Files" ${resource_file})
312 set(windows_resource_file ${resource_file} PARENT_SCOPE)
314 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
316 set(${OUT_VAR} ${sources} PARENT_SCOPE)
317 endfunction(add_windows_version_resource_file)
319 # set_windows_version_resource_properties(name resource_file...
321 # Optional major version number (defaults to LLVM_VERSION_MAJOR)
323 # Optional minor version number (defaults to LLVM_VERSION_MINOR)
324 # VERSION_PATCHLEVEL int
325 # Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
327 # Optional version string (defaults to PACKAGE_VERSION)
329 # Optional product name string (defaults to "LLVM")
331 function(set_windows_version_resource_properties name resource_file)
332 cmake_parse_arguments(ARG
334 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
338 if (NOT DEFINED ARG_VERSION_MAJOR)
339 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
342 if (NOT DEFINED ARG_VERSION_MINOR)
343 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
346 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
347 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
350 if (NOT DEFINED ARG_VERSION_STRING)
351 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
354 if (NOT DEFINED ARG_PRODUCT_NAME)
355 set(ARG_PRODUCT_NAME "LLVM")
358 set_property(SOURCE ${resource_file}
359 PROPERTY COMPILE_FLAGS /nologo)
360 set_property(SOURCE ${resource_file}
361 PROPERTY COMPILE_DEFINITIONS
362 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
363 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
364 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
365 "RC_VERSION_FIELD_4=0"
366 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
367 "RC_INTERNAL_NAME=\"${name}\""
368 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
369 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
370 endfunction(set_windows_version_resource_properties)
372 # llvm_add_library(name sources...
374 # STATIC by default w/o BUILD_SHARED_LIBS.
375 # SHARED by default w/ BUILD_SHARED_LIBS.
377 # Also create an OBJECT library target. Default if STATIC && SHARED.
379 # Target ${name} might not be created on unsupported platforms.
380 # Check with "if(TARGET ${name})".
381 # DISABLE_LLVM_LINK_LLVM_DYLIB
382 # Do not link this library to libLLVM, even if
383 # LLVM_LINK_LLVM_DYLIB is enabled.
385 # Corresponds to OUTPUT_NAME in target properties.
387 # Same semantics as add_dependencies().
388 # LINK_COMPONENTS components...
389 # Same as the variable LLVM_LINK_COMPONENTS.
390 # LINK_LIBS lib_targets...
391 # Same semantics as target_link_libraries().
393 # May specify header files for IDE generators.
395 # Should set SONAME link flags and create symlinks
397 # Suppress default RPATH settings in shared libraries.
399 # The tool (i.e. cmake target) that this plugin will link against
401 # This is used to specify that this is a component library of
402 # LLVM which means that the source resides in llvm/lib/ and it is a
403 # candidate for inclusion into libLLVM.so.
405 function(llvm_add_library name)
406 cmake_parse_arguments(ARG
407 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB;ENABLE_PLUGINS"
408 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
409 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
411 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
412 if(ARG_ADDITIONAL_HEADERS)
413 # Pass through ADDITIONAL_HEADERS.
414 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
417 set(ALL_FILES ${ARG_OBJLIBS})
419 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
421 if(ARG_ENABLE_PLUGINS)
422 set_property(GLOBAL APPEND PROPERTY LLVM_PLUGIN_TARGETS ${name})
426 if(ARG_SHARED OR ARG_STATIC)
427 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
429 # Plugins that link against a tool are allowed even when plugins in general are not
430 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
431 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
436 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
438 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
447 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
448 # Generate an obj library for both targets.
449 set(obj_name "obj.${name}")
450 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
453 llvm_update_compile_flags(${obj_name})
454 if(CMAKE_GENERATOR STREQUAL "Xcode")
455 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
456 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
457 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
459 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
461 # Do add_dependencies(obj) later due to CMake issue 14747.
462 list(APPEND objlibs ${obj_name})
464 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
466 add_dependencies(${obj_name} ${ARG_DEPENDS})
470 if(ARG_SHARED AND ARG_STATIC)
472 set(name_static "${name}_static")
474 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
476 # DEPENDS has been appended to LLVM_COMMON_LIBS.
477 llvm_add_library(${name_static} STATIC
479 OBJLIBS ${ALL_FILES} # objlib
480 LINK_LIBS ${ARG_LINK_LIBS}
481 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
483 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
488 add_library(${name} MODULE ${ALL_FILES})
490 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
491 add_library(${name} SHARED ${ALL_FILES})
493 add_library(${name} STATIC ${ALL_FILES})
496 if(ARG_COMPONENT_LIB)
497 set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE)
498 set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name})
501 if(NOT ARG_NO_INSTALL_RPATH)
502 if(ARG_MODULE OR ARG_SHARED)
503 llvm_setup_rpath(${name})
507 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
509 if(DEFINED windows_resource_file)
510 set_windows_version_resource_properties(${name} ${windows_resource_file})
511 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
514 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
515 # $<TARGET_OBJECTS> doesn't require compile flags.
517 llvm_update_compile_flags(${name})
519 add_link_opts( ${name} )
521 set_target_properties(${name}
523 OUTPUT_NAME ${ARG_OUTPUT_NAME}
528 set_target_properties(${name} PROPERTIES
530 SUFFIX ${LLVM_PLUGIN_EXT}
536 set_target_properties(${name} PROPERTIES
541 # Set SOVERSION on shared libraries that lack explicit SONAME
542 # specifier, on *nix systems that are not Darwin.
543 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
544 set_target_properties(${name}
546 # Since 4.0.0, the ABI version is indicated by the major version
547 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
548 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
552 if(ARG_MODULE OR ARG_SHARED)
553 # Do not add -Dname_EXPORTS to the command-line when building files in this
554 # target. Doing so is actively harmful for the modules build because it
555 # creates extra module variants, and not useful because we don't use these
557 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
559 if (LLVM_EXPORTED_SYMBOL_FILE)
560 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
564 if(ARG_SHARED AND UNIX)
565 if(NOT APPLE AND ARG_SONAME)
566 get_target_property(output_name ${name} OUTPUT_NAME)
567 if(${output_name} STREQUAL "output_name-NOTFOUND")
568 set(output_name ${name})
570 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
571 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
572 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
573 llvm_install_library_symlink(${api_name} ${library_name} SHARED
576 llvm_install_library_symlink(${output_name} ${library_name} SHARED
582 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
583 # On DLL platforms symbols are imported from the tool by linking against it.
584 set(llvm_libs ${ARG_PLUGIN_TOOL})
585 elseif (NOT ARG_COMPONENT_LIB)
586 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
589 llvm_map_components_to_libnames(llvm_libs
590 ${ARG_LINK_COMPONENTS}
591 ${LLVM_LINK_COMPONENTS}
595 # Components have not been defined explicitly in CMake, so add the
596 # dependency information for this library as defined by LLVMBuild.
598 # It would be nice to verify that we have the dependencies for this library
599 # name, but using get_property(... SET) doesn't suffice to determine if a
600 # property has been set to an empty value.
601 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
605 set(libtype INTERFACE)
607 # We can use PRIVATE since SO knows its dependent libs.
611 target_link_libraries(${name} ${libtype}
617 if(LLVM_COMMON_DEPENDS)
618 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
619 # Add dependencies also to objlibs.
620 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
621 foreach(objlib ${objlibs})
622 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
626 if(ARG_SHARED OR ARG_MODULE)
627 llvm_externalize_debuginfo(${name})
628 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
630 # clang and newer versions of ninja use high-resolutions timestamps,
631 # but older versions of libtool on Darwin don't, so the archive will
632 # often get an older timestamp than the last object that was added
633 # or updated. To fix this, we add a custom command to touch archive
634 # after it's been built so that ninja won't rebuild it unnecessarily
635 # the next time it's run.
636 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
637 add_custom_command(TARGET ${name}
639 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
644 function(add_llvm_install_targets target)
645 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN})
647 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
650 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
653 set(file_dependencies)
654 set(target_dependencies)
655 foreach(dependency ${ARG_DEPENDS})
656 if(TARGET ${dependency})
657 list(APPEND target_dependencies ${dependency})
659 list(APPEND file_dependencies ${dependency})
663 add_custom_target(${target}
664 DEPENDS ${file_dependencies}
665 COMMAND "${CMAKE_COMMAND}"
668 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
670 add_custom_target(${target}-stripped
671 DEPENDS ${file_dependencies}
672 COMMAND "${CMAKE_COMMAND}"
675 -DCMAKE_INSTALL_DO_STRIP=1
676 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
678 if(target_dependencies)
679 add_dependencies(${target} ${target_dependencies})
680 add_dependencies(${target}-stripped ${target_dependencies})
684 add_dependencies(${target} install-${ARG_SYMLINK})
685 add_dependencies(${target}-stripped install-${ARG_SYMLINK}-stripped)
689 function(add_llvm_component_library name)
690 add_llvm_library(${name} COMPONENT_LIB ${ARGN})
693 macro(add_llvm_library name)
694 cmake_parse_arguments(ARG
695 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
700 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
701 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
702 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
704 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
707 # Libraries that are meant to only be exposed via the build tree only are
708 # never installed and are only exported as a target in the special build tree
710 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
711 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
712 set(in_llvm_libs YES)
715 if (ARG_MODULE AND NOT TARGET ${name})
716 # Add empty "phony" target
717 add_custom_target(${name})
718 elseif( EXCLUDE_FROM_ALL )
719 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
720 elseif(ARG_BUILDTREE_ONLY)
721 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
723 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
725 set(export_to_llvmexports)
726 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
727 (in_llvm_libs AND "llvm-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS) OR
728 NOT LLVM_DISTRIBUTION_COMPONENTS)
729 set(export_to_llvmexports EXPORT LLVMExports)
730 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
733 install(TARGETS ${name}
734 ${export_to_llvmexports}
735 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
736 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
737 RUNTIME DESTINATION bin COMPONENT ${name})
739 if (NOT LLVM_ENABLE_IDE)
740 add_llvm_install_targets(install-${name}
745 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
748 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
750 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
752 endmacro(add_llvm_library name)
754 macro(add_llvm_executable name)
755 cmake_parse_arguments(ARG
756 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS;ENABLE_PLUGINS"
757 "ENTITLEMENTS;BUNDLE_PATH"
761 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
763 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
766 if(LLVM_ENABLE_OBJLIB)
767 # Generate an obj library for both targets.
768 set(obj_name "obj.${name}")
769 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
772 llvm_update_compile_flags(${obj_name})
773 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
775 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
778 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
781 # Note: the dummy.cpp source file provides no definitions. However,
782 # it forces Xcode to properly link the static library.
783 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
786 if( EXCLUDE_FROM_ALL )
787 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
789 add_executable(${name} ${ALL_FILES})
792 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
794 if(NOT ARG_NO_INSTALL_RPATH)
795 llvm_setup_rpath(${name})
798 if(DEFINED windows_resource_file)
799 set_windows_version_resource_properties(${name} ${windows_resource_file})
802 # $<TARGET_OBJECTS> doesn't require compile flags.
803 if(NOT LLVM_ENABLE_OBJLIB)
804 llvm_update_compile_flags(${name})
807 if (ARG_SUPPORT_PLUGINS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
808 set(LLVM_NO_DEAD_STRIP On)
811 add_link_opts( ${name} )
813 # Do not add -Dname_EXPORTS to the command-line when building files in this
814 # target. Doing so is actively harmful for the modules build because it
815 # creates extra module variants, and not useful because we don't use these
817 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
819 if (LLVM_EXPORTED_SYMBOL_FILE)
820 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
821 endif(LLVM_EXPORTED_SYMBOL_FILE)
823 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
824 set(USE_SHARED USE_SHARED)
827 set(EXCLUDE_FROM_ALL OFF)
828 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
829 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
830 if( LLVM_COMMON_DEPENDS )
831 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
832 endif( LLVM_COMMON_DEPENDS )
834 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
835 llvm_externalize_debuginfo(${name})
837 if (LLVM_PTHREAD_LIB)
838 # libpthreads overrides some standard library symbols, so main
839 # executable must be linked with it in order to provide consistent
840 # API for all shared libaries loaded by this executable.
841 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
843 if(ARG_ENABLE_PLUGINS)
844 set_property(GLOBAL APPEND PROPERTY LLVM_PLUGIN_TARGETS ${name})
847 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
848 endmacro(add_llvm_executable name)
850 # add_llvm_pass_plugin(name [NO_MODULE] ...)
851 # Add ${name} as an llvm plugin.
852 # If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically.
853 # Otherwise a pluggable shared library is registered.
855 # If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF,
856 # only an object library is built, and no module is built. This is specific to the Polly use case.
857 function(add_llvm_pass_plugin name)
858 cmake_parse_arguments(ARG
862 string(TOUPPER ${name} name_upper)
864 option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" OFF)
866 if(LLVM_${name_upper}_LINK_INTO_TOOLS)
867 list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY)
868 # process_llvm_pass_plugins takes care of the actual linking, just create an
869 # object library as of now
870 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
871 target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS)
872 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS)
873 if (TARGET intrinsics_gen)
874 add_dependencies(obj.${name} intrinsics_gen)
876 message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})")
877 set_property(GLOBAL APPEND PROPERTY LLVM_COMPILE_EXTENSIONS ${name})
878 elseif(NOT ARG_NO_MODULE)
879 add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
881 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
884 endfunction(add_llvm_pass_plugin)
886 # Generate X Macro file for extension handling. It provides a
887 # HANDLE_EXTENSION(extension_namespace, ExtensionProject) call for each extension
888 # allowing client code to define HANDLE_EXTENSION to have a specific code be run for
891 # Also correctly set lib dependencies between plugins and tools.
892 function(process_llvm_pass_plugins)
893 get_property(LLVM_EXTENSIONS GLOBAL PROPERTY LLVM_COMPILE_EXTENSIONS)
894 file(WRITE "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "//extension handlers\n")
895 foreach(llvm_extension ${LLVM_EXTENSIONS})
896 string(TOLOWER ${llvm_extension} llvm_extension_lower)
898 string(TOUPPER ${llvm_extension} llvm_extension_upper)
899 string(SUBSTRING ${llvm_extension_upper} 0 1 llvm_extension_upper_first)
900 string(SUBSTRING ${llvm_extension_lower} 1 -1 llvm_extension_lower_tail)
901 string(CONCAT llvm_extension_project ${llvm_extension_upper_first} ${llvm_extension_lower_tail})
903 if(LLVM_${llvm_extension_upper}_LINK_INTO_TOOLS)
904 file(APPEND "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "HANDLE_EXTENSION(${llvm_extension_project})\n")
906 get_property(llvm_plugin_targets GLOBAL PROPERTY LLVM_PLUGIN_TARGETS)
907 foreach(llvm_plugin_target ${llvm_plugin_targets})
908 set_property(TARGET ${llvm_plugin_target} APPEND PROPERTY LINK_LIBRARIES ${llvm_extension})
909 set_property(TARGET ${llvm_plugin_target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension})
912 add_llvm_library(${llvm_extension_lower} MODULE obj.${llvm_extension_lower})
916 file(APPEND "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "#undef HANDLE_EXTENSION\n")
918 # only replace if there's an actual change
919 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
920 "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def.tmp"
921 "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def")
922 file(REMOVE "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def.tmp")
925 function(export_executable_symbols target)
926 if (LLVM_EXPORTED_SYMBOL_FILE)
927 # The symbol file should contain the symbols we want the executable to
929 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
930 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
931 # Extract the symbols to export from the static libraries that the
932 # executable links against.
933 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
934 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
935 # We need to consider not just the direct link dependencies, but also the
936 # transitive link dependencies. Do this by starting with the set of direct
937 # dependencies, then the dependencies of those dependencies, and so on.
938 get_target_property(new_libs ${target} LINK_LIBRARIES)
939 set(link_libs ${new_libs})
940 while(NOT "${new_libs}" STREQUAL "")
941 foreach(lib ${new_libs})
943 get_target_property(lib_type ${lib} TYPE)
944 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
945 list(APPEND static_libs ${lib})
947 list(APPEND other_libs ${lib})
949 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
950 foreach(transitive_lib ${transitive_libs})
951 list(FIND link_libs ${transitive_lib} idx)
952 if(TARGET ${transitive_lib} AND idx EQUAL -1)
953 list(APPEND newer_libs ${transitive_lib})
954 list(APPEND link_libs ${transitive_lib})
956 endforeach(transitive_lib)
959 set(new_libs ${newer_libs})
962 list(REMOVE_DUPLICATES static_libs)
964 set(mangling microsoft)
966 set(mangling itanium)
968 add_custom_command(OUTPUT ${exported_symbol_file}
969 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
970 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
971 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
973 COMMENT "Generating export list for ${target}")
974 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
975 # If something links against this executable then we want a
976 # transitive link against only the libraries whose symbols
977 # we aren't exporting.
978 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
979 # The default import library suffix that cmake uses for cygwin/mingw is
980 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
981 # where the import libraries of both get named libclang.dll.a. Use a suffix
982 # of ".exe.a" to avoid this.
984 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
986 elseif(NOT (WIN32 OR CYGWIN))
987 # On Windows auto-exporting everything doesn't work because of the limit on
988 # the size of the exported symbol table, but on other platforms we can do
989 # it without any trouble.
990 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
992 set_property(TARGET ${target} APPEND_STRING PROPERTY
993 LINK_FLAGS " -rdynamic")
998 if(NOT LLVM_TOOLCHAIN_TOOLS)
999 set (LLVM_TOOLCHAIN_TOOLS
1013 # symlink version of some of above tools that are enabled by
1014 # LLVM_INSTALL_BINUTILS_SYMLINKS.
1028 macro(add_llvm_tool name)
1029 if( NOT LLVM_BUILD_TOOLS )
1030 set(EXCLUDE_FROM_ALL ON)
1032 add_llvm_executable(${name} ${ARGN})
1034 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1035 if( LLVM_BUILD_TOOLS )
1036 set(export_to_llvmexports)
1037 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
1038 NOT LLVM_DISTRIBUTION_COMPONENTS)
1039 set(export_to_llvmexports EXPORT LLVMExports)
1040 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
1043 install(TARGETS ${name}
1044 ${export_to_llvmexports}
1045 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
1048 if (NOT LLVM_ENABLE_IDE)
1049 add_llvm_install_targets(install-${name}
1055 if( LLVM_BUILD_TOOLS )
1056 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1058 set_target_properties(${name} PROPERTIES FOLDER "Tools")
1059 endmacro(add_llvm_tool name)
1062 macro(add_llvm_example name)
1063 if( NOT LLVM_BUILD_EXAMPLES )
1064 set(EXCLUDE_FROM_ALL ON)
1066 add_llvm_executable(${name} ${ARGN})
1067 if( LLVM_BUILD_EXAMPLES )
1068 install(TARGETS ${name} RUNTIME DESTINATION examples)
1070 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1071 endmacro(add_llvm_example name)
1073 macro(add_llvm_example_library name)
1074 if( NOT LLVM_BUILD_EXAMPLES )
1075 set(EXCLUDE_FROM_ALL ON)
1076 add_llvm_library(${name} BUILDTREE_ONLY ${ARGN})
1078 add_llvm_library(${name} ${ARGN})
1081 set_target_properties(${name} PROPERTIES FOLDER "Examples")
1082 endmacro(add_llvm_example_library name)
1084 # This is a macro that is used to create targets for executables that are needed
1085 # for development, but that are not intended to be installed by default.
1086 macro(add_llvm_utility name)
1087 if ( NOT LLVM_BUILD_UTILS )
1088 set(EXCLUDE_FROM_ALL ON)
1091 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
1092 set_target_properties(${name} PROPERTIES FOLDER "Utils")
1093 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1094 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
1095 set(export_to_llvmexports)
1096 if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
1097 NOT LLVM_DISTRIBUTION_COMPONENTS)
1098 set(export_to_llvmexports EXPORT LLVMExports)
1099 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
1102 install(TARGETS ${name}
1103 ${export_to_llvmexports}
1104 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
1107 if (NOT LLVM_ENABLE_IDE)
1108 add_llvm_install_targets(install-${name}
1112 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
1113 elseif(LLVM_BUILD_UTILS)
1114 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
1117 endmacro(add_llvm_utility name)
1119 macro(add_llvm_fuzzer name)
1120 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
1121 if( LLVM_LIB_FUZZING_ENGINE )
1122 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1123 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1124 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
1125 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1126 elseif( LLVM_USE_SANITIZE_COVERAGE )
1127 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
1128 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
1129 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
1130 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1131 elseif( ARG_DUMMY_MAIN )
1132 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
1133 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1137 macro(add_llvm_target target_name)
1138 include_directories(BEFORE
1139 ${CMAKE_CURRENT_BINARY_DIR}
1140 ${CMAKE_CURRENT_SOURCE_DIR})
1141 add_llvm_component_library(LLVM${target_name} ${ARGN})
1142 set( CURRENT_LLVM_TARGET LLVM${target_name} )
1143 endmacro(add_llvm_target)
1145 function(canonicalize_tool_name name output)
1146 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1147 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1148 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1149 set(${output} "${nameUPPER}" PARENT_SCOPE)
1150 endfunction(canonicalize_tool_name)
1152 # Custom add_subdirectory wrapper
1153 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1154 # path if it differs from the name.
1155 function(add_llvm_subdirectory project type name)
1156 set(add_llvm_external_dir "${ARGN}")
1157 if("${add_llvm_external_dir}" STREQUAL "")
1158 set(add_llvm_external_dir ${name})
1160 canonicalize_tool_name(${name} nameUPPER)
1161 set(canonical_full_name ${project}_${type}_${nameUPPER})
1162 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1163 if(already_processed)
1166 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1168 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1169 # Treat it as in-tree subproject.
1170 option(${canonical_full_name}_BUILD
1171 "Whether to build ${name} as part of ${project}" On)
1172 mark_as_advanced(${project}_${type}_${name}_BUILD)
1173 if(${canonical_full_name}_BUILD)
1174 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
1177 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1178 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1179 CACHE PATH "Path to ${name} source directory")
1180 set(${canonical_full_name}_BUILD_DEFAULT ON)
1181 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1182 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1184 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
1185 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1187 option(${canonical_full_name}_BUILD
1188 "Whether to build ${name} as part of LLVM"
1189 ${${canonical_full_name}_BUILD_DEFAULT})
1190 if (${canonical_full_name}_BUILD)
1191 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1192 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1193 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1194 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1200 # Add external project that may want to be built as part of llvm such as Clang,
1201 # lld, and Polly. This adds two options. One for the source directory of the
1202 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1203 # enable or disable building it with everything else.
1204 # Additional parameter can be specified as the name of directory.
1205 macro(add_llvm_external_project name)
1206 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1209 macro(add_llvm_tool_subdirectory name)
1210 add_llvm_external_project(${name})
1211 endmacro(add_llvm_tool_subdirectory)
1213 function(get_project_name_from_src_var var output)
1214 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1215 MACHED_TOOL "${var}")
1217 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1219 set(${output} PARENT_SCOPE)
1223 function(create_subdirectory_options project type)
1224 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1225 foreach(dir ${sub-dirs})
1226 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1227 canonicalize_tool_name(${dir} name)
1228 option(${project}_${type}_${name}_BUILD
1229 "Whether to build ${name} as part of ${project}" On)
1230 mark_as_advanced(${project}_${type}_${name}_BUILD)
1233 endfunction(create_subdirectory_options)
1235 function(create_llvm_tool_options)
1236 create_subdirectory_options(LLVM TOOL)
1237 endfunction(create_llvm_tool_options)
1239 function(llvm_add_implicit_projects project)
1240 set(list_of_implicit_subdirs "")
1241 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1242 foreach(dir ${sub-dirs})
1243 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1244 canonicalize_tool_name(${dir} name)
1245 if (${project}_TOOL_${name}_BUILD)
1246 get_filename_component(fn "${dir}" NAME)
1247 list(APPEND list_of_implicit_subdirs "${fn}")
1252 foreach(external_proj ${list_of_implicit_subdirs})
1253 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1255 endfunction(llvm_add_implicit_projects)
1257 function(add_llvm_implicit_projects)
1258 llvm_add_implicit_projects(LLVM)
1259 endfunction(add_llvm_implicit_projects)
1261 # Generic support for adding a unittest.
1262 function(add_unittest test_suite test_name)
1263 if( NOT LLVM_BUILD_TESTS )
1264 set(EXCLUDE_FROM_ALL ON)
1267 # Our current version of gtest does not properly recognize C++11 support
1268 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM
1269 # itself requires C++11, we can safely force it on unconditionally so that
1270 # we don't have to fight with the buggy gtest check.
1271 add_definitions(-DGTEST_LANG_CXX11=1)
1272 add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
1274 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1275 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
1276 if (NOT LLVM_ENABLE_THREADS)
1277 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1280 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1281 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1283 # Some parts of gtest rely on this GNU extension, don't warn on it.
1284 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1285 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1288 set(LLVM_REQUIRES_RTTI OFF)
1290 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1291 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1292 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1293 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1294 # libpthreads overrides some standard library symbols, so main
1295 # executable must be linked with it in order to provide consistent
1296 # API for all shared libaries loaded by this executable.
1297 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
1299 add_dependencies(${test_suite} ${test_name})
1300 get_target_property(test_suite_folder ${test_suite} FOLDER)
1301 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1302 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1306 # Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1308 function(add_unittest_with_input_files test_suite test_name)
1309 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1311 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1312 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1314 add_unittest(${test_suite} ${test_name} ${ARGN})
1317 # Generic support for adding a benchmark.
1318 function(add_benchmark benchmark_name)
1319 if( NOT LLVM_BUILD_BENCHMARKS )
1320 set(EXCLUDE_FROM_ALL ON)
1323 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1324 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1325 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1326 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1327 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1330 function(llvm_add_go_executable binary pkgpath)
1331 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1333 if(LLVM_BINDINGS MATCHES "go")
1334 # FIXME: This should depend only on the libraries Go needs.
1335 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1336 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1337 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1338 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1340 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1341 foreach(d ${include_dirs})
1342 set(cppflags "${cppflags} -I${d}")
1344 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1345 add_custom_command(OUTPUT ${binpath}
1346 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1347 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1348 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1349 ${llvmlibs} ${ARG_DEPENDS}
1350 COMMENT "Building Go executable ${binary}"
1353 add_custom_target(${binary} ALL DEPENDS ${binpath})
1355 add_custom_target(${binary} DEPENDS ${binpath})
1360 # This function canonicalize the CMake variables passed by names
1361 # from CMake boolean to 0/1 suitable for passing into Python or C++,
1363 function(llvm_canonicalize_cmake_booleans)
1364 foreach(var ${ARGN})
1366 set(${var} 1 PARENT_SCOPE)
1368 set(${var} 0 PARENT_SCOPE)
1371 endfunction(llvm_canonicalize_cmake_booleans)
1373 macro(set_llvm_build_mode)
1374 # Configuration-time: See Unit/lit.site.cfg.in
1375 if (CMAKE_CFG_INTDIR STREQUAL ".")
1376 set(LLVM_BUILD_MODE ".")
1378 set(LLVM_BUILD_MODE "%(build_mode)s")
1382 # This function provides an automatic way to 'configure'-like generate a file
1383 # based on a set of common and custom variables, specifically targeting the
1384 # variables needed for the 'lit.site.cfg' files. This function bundles the
1385 # common variables that any Lit instance is likely to need, and custom
1386 # variables can be passed in.
1387 function(configure_lit_site_cfg site_in site_out)
1388 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
1390 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1391 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1392 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1394 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1395 set(ARG_OUTPUT_MAPPING "${site_out}")
1398 foreach(c ${LLVM_TARGETS_TO_BUILD})
1399 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1401 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1403 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1405 set_llvm_build_mode()
1407 # They below might not be the build tree but provided binary tree.
1408 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1409 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1410 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1411 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1413 # SHLIBDIR points the build tree.
1414 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1416 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1417 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1418 # plugins. We may rename it.
1419 if(LLVM_ENABLE_PLUGINS)
1420 set(ENABLE_SHARED "1")
1422 set(ENABLE_SHARED "0")
1425 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1426 set(ENABLE_ASSERTIONS "1")
1428 set(ENABLE_ASSERTIONS "0")
1431 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1432 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1434 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1435 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1436 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1438 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
1440 # Override config_target_triple (and the env)
1441 if(LLVM_TARGET_TRIPLE_ENV)
1442 # This is expanded into the heading.
1443 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1445 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1446 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1449 # This is expanded to; config.target_triple = ""+config.target_triple+""
1450 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1453 configure_file(${site_in} ${site_out} @ONLY)
1454 if (EXISTS "${ARG_MAIN_CONFIG}")
1455 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
1456 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)
1457 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}")
1458 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP})
1462 function(dump_all_cmake_variables)
1463 get_cmake_property(_variableNames VARIABLES)
1464 foreach (_variableName ${_variableNames})
1465 message(STATUS "${_variableName}=${${_variableName}}")
1469 function(get_llvm_lit_path base_dir file_name)
1470 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1472 if (ARG_ALLOW_EXTERNAL)
1473 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1474 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1475 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1478 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1479 if (EXISTS ${LLVM_EXTERNAL_LIT})
1480 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1481 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1482 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1483 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1486 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1491 set(lit_file_name "llvm-lit")
1492 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
1493 # llvm-lit needs suffix.py for multiprocess to find a main module.
1494 set(lit_file_name "${lit_file_name}.py")
1496 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1498 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1499 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1500 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1503 # Allow individual projects to provide an override
1504 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1505 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1506 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1507 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1509 set(LLVM_LIT_BASE_DIR "")
1512 # Cache this so we don't have to do it again and have subsequent calls
1513 # potentially disagree on the value.
1514 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1515 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1518 # A raw function to create a lit target. This is used to implement the testuite
1519 # management functions.
1520 function(add_lit_target target comment)
1521 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1522 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1523 separate_arguments(LIT_ARGS)
1524 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1525 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1528 # Get the path to the lit to *run* tests with. This can be overriden by
1529 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1536 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1537 list(APPEND LIT_COMMAND ${LIT_ARGS})
1538 foreach(param ${ARG_PARAMS})
1539 list(APPEND LIT_COMMAND --param ${param})
1541 if (ARG_UNPARSED_ARGUMENTS)
1542 add_custom_target(${target}
1543 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1544 COMMENT "${comment}"
1548 add_custom_target(${target}
1549 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1550 message(STATUS "${target} does nothing.")
1554 add_dependencies(${target} ${ARG_DEPENDS})
1557 # Tests should be excluded from "Build Solution".
1558 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1561 # A function to add a set of lit test suites to be driven through 'check-*' targets.
1562 function(add_lit_testsuite target comment)
1563 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1565 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1566 if(NOT EXCLUDE_FROM_ALL)
1567 # Register the testsuites, params and depends for the global check rule.
1568 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1569 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1570 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1571 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1574 # Produce a specific suffixed check rule.
1575 add_lit_target(${target} ${comment}
1576 ${ARG_UNPARSED_ARGUMENTS}
1577 PARAMS ${ARG_PARAMS}
1578 DEPENDS ${ARG_DEPENDS}
1583 function(add_lit_testsuites project directory)
1584 if (NOT LLVM_ENABLE_IDE)
1585 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1587 # Search recursively for test directories by assuming anything not
1588 # in a directory called Inputs contains tests.
1589 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1590 foreach(lit_suite ${to_process})
1591 if(NOT IS_DIRECTORY ${lit_suite})
1594 string(FIND ${lit_suite} Inputs is_inputs)
1595 string(FIND ${lit_suite} Output is_output)
1596 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1600 # Create a check- target for the directory.
1601 string(REPLACE ${directory} "" name_slash ${lit_suite})
1603 string(REPLACE "/" "-" name_slash ${name_slash})
1604 string(REPLACE "\\" "-" name_dashes ${name_slash})
1605 string(TOLOWER "${project}${name_dashes}" name_var)
1606 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1608 PARAMS ${ARG_PARAMS}
1609 DEPENDS ${ARG_DEPENDS}
1617 function(llvm_install_library_symlink name dest type)
1618 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1619 foreach(path ${CMAKE_MODULE_PATH})
1620 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1621 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1626 set(component ${ARG_COMPONENT})
1628 set(component ${name})
1631 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1632 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1634 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1635 if(WIN32 AND "${type}" STREQUAL "SHARED")
1639 install(SCRIPT ${INSTALL_SYMLINK}
1640 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1641 COMPONENT ${component})
1643 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1644 add_llvm_install_targets(install-${name}
1645 DEPENDS ${name} ${dest}
1651 function(llvm_install_symlink name dest)
1652 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1653 foreach(path ${CMAKE_MODULE_PATH})
1654 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1655 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1661 set(component ${ARG_COMPONENT})
1663 if(ARG_ALWAYS_GENERATE)
1664 set(component ${dest})
1666 set(component ${name})
1670 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1671 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1673 install(SCRIPT ${INSTALL_SYMLINK}
1674 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1675 COMPONENT ${component})
1677 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1678 add_llvm_install_targets(install-${name}
1679 DEPENDS ${name} ${dest}
1685 function(add_llvm_tool_symlink link_name target)
1686 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1687 set(dest_binary "$<TARGET_FILE:${target}>")
1689 # This got a bit gross... For multi-configuration generators the target
1690 # properties return the resolved value of the string, not the build system
1691 # expression. To reconstruct the platform-agnostic path we have to do some
1692 # magic. First we grab one of the types, and a type-specific path. Then from
1693 # the type-specific path we find the last occurrence of the type in the path,
1694 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1696 if(NOT ARG_OUTPUT_DIR)
1697 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1698 # the same directory.
1700 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1702 if(CMAKE_CONFIGURATION_TYPES)
1703 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1704 string(TOUPPER ${first_type} first_type_upper)
1705 set(first_type_suffix _${first_type_upper})
1707 get_target_property(target_type ${target} TYPE)
1708 if(${target_type} STREQUAL "STATIC_LIBRARY")
1709 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1710 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1711 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1713 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1715 if(CMAKE_CONFIGURATION_TYPES)
1716 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1717 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1718 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1719 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1720 path_suffix ${path_suffix})
1721 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1726 set(LLVM_LINK_OR_COPY create_symlink)
1728 set(LLVM_LINK_OR_COPY copy)
1731 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1733 set(target_name ${link_name})
1734 if(TARGET ${link_name})
1735 set(target_name ${link_name}-link)
1739 if(ARG_ALWAYS_GENERATE)
1740 set_property(DIRECTORY APPEND PROPERTY
1741 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1742 add_custom_command(TARGET ${target} POST_BUILD
1743 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1745 add_custom_command(OUTPUT ${output_path}
1746 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1748 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1749 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1751 # Make sure both the link and target are toolchain tools
1752 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1753 set(TOOL_IS_TOOLCHAIN ON)
1756 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1757 llvm_install_symlink(${link_name} ${target})
1762 function(llvm_externalize_debuginfo name)
1763 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1767 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1770 set(CMAKE_STRIP xcrun strip)
1772 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>)
1774 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
1779 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
1780 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
1785 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
1787 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
1788 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
1790 set(output_path "-o=${output_name}")
1793 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1794 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1796 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1797 set_property(TARGET ${name} APPEND_STRING PROPERTY
1798 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1800 if(NOT CMAKE_DSYMUTIL)
1801 set(CMAKE_DSYMUTIL xcrun dsymutil)
1803 add_custom_command(TARGET ${name} POST_BUILD
1804 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
1808 add_custom_command(TARGET ${name} POST_BUILD
1809 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1810 ${strip_command} -R .gnu_debuglink
1811 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1816 # Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
1817 function(llvm_codesign name)
1818 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
1820 if(NOT LLVM_CODESIGNING_IDENTITY)
1824 if(CMAKE_GENERATOR STREQUAL "Xcode")
1825 set_target_properties(${name} PROPERTIES
1826 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
1828 if(DEFINED ARG_ENTITLEMENTS)
1829 set_target_properties(${name} PROPERTIES
1830 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
1833 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
1834 if(NOT CMAKE_CODESIGN)
1835 set(CMAKE_CODESIGN xcrun codesign)
1837 if(NOT CMAKE_CODESIGN_ALLOCATE)
1839 COMMAND xcrun -f codesign_allocate
1840 OUTPUT_STRIP_TRAILING_WHITESPACE
1841 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
1844 if(DEFINED ARG_ENTITLEMENTS)
1845 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
1848 if (NOT ARG_BUNDLE_PATH)
1849 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
1853 set(force_flag "-f")
1857 TARGET ${name} POST_BUILD
1858 COMMAND ${CMAKE_COMMAND} -E
1859 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
1860 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
1861 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
1866 function(llvm_setup_rpath name)
1867 if(CMAKE_INSTALL_RPATH)
1871 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1872 set(extra_libdir ${LLVM_LIBRARY_DIR})
1873 elseif(LLVM_BUILD_LIBRARY_DIR)
1874 set(extra_libdir ${LLVM_LIBRARY_DIR})
1878 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1879 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1881 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1882 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1883 set_property(TARGET ${name} APPEND_STRING PROPERTY
1884 LINK_FLAGS " -Wl,-z,origin ")
1886 if(LLVM_LINKER_IS_GNULD)
1887 # $ORIGIN is not interpreted at link time by ld.bfd
1888 set_property(TARGET ${name} APPEND_STRING PROPERTY
1889 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1895 set_target_properties(${name} PROPERTIES
1896 BUILD_WITH_INSTALL_RPATH On
1897 INSTALL_RPATH "${_install_rpath}"
1898 ${_install_name_dir})
1901 function(setup_dependency_debugging name)
1902 if(NOT LLVM_DEPENDENCY_DEBUGGING)
1906 if("intrinsics_gen" IN_LIST ARGN)
1910 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
1911 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
1913 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
1914 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
1917 function(find_first_existing_vc_file path out_var)
1918 if(NOT EXISTS "${path}")
1921 if(EXISTS "${path}/.svn")
1923 "${path}/.svn/wc.db" # SVN 1.7
1924 "${path}/.svn/entries" # SVN 1.6
1926 foreach(file IN LISTS svn_files)
1927 if(EXISTS "${file}")
1928 set(${out_var} "${file}" PARENT_SCOPE)
1935 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
1936 WORKING_DIRECTORY ${path}
1937 RESULT_VARIABLE git_result
1938 OUTPUT_VARIABLE git_output
1940 if(git_result EQUAL 0)
1941 string(STRIP "${git_output}" git_output)
1942 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
1943 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
1944 if (NOT EXISTS "${git_dir}/logs/HEAD")
1945 file(WRITE "${git_dir}/logs/HEAD" "")
1947 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)