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 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to
13 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH)
14 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
15 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}")
16 set(LLVM_REQUIRES_RTTI ON)
19 list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
22 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
23 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
24 if(NOT LLVM_ENABLE_UNWIND_TABLES)
25 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables")
26 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables")
29 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
30 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
31 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
32 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh")
36 # LLVM_REQUIRES_RTTI is an internal flag that individual
37 # targets can use to force RTTI
38 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
39 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
40 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
41 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
42 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
43 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
45 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
46 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
47 list(APPEND LLVM_COMPILE_FLAGS "-qnortti")
50 list(APPEND LLVM_COMPILE_FLAGS "/GR")
54 # - LLVM_COMPILE_FLAGS is list.
55 # - PROPERTY COMPILE_FLAGS is string.
56 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
59 foreach(fn ${sources})
60 get_filename_component(suf ${fn} EXT)
61 if("${suf}" STREQUAL ".cpp" OR "${suf}" STREQUAL ".c")
62 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
63 COMPILE_FLAGS "${target_compile_flags}")
67 # Update target props, since all sources are C++.
68 set_property(TARGET ${name} APPEND_STRING PROPERTY
69 COMPILE_FLAGS "${target_compile_flags}")
72 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
75 function(add_llvm_symbol_exports target_name export_file)
76 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
77 set(native_export_file "${target_name}.exports")
78 add_custom_command(OUTPUT ${native_export_file}
79 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
80 DEPENDS ${export_file}
82 COMMENT "Creating export file for ${target_name}")
83 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
84 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
85 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
86 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
87 LINK_FLAGS " -Wl,-bE:${export_file}")
88 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
89 # Gold and BFD ld require a version script rather than a plain list.
90 set(native_export_file "${target_name}.exports")
91 # FIXME: Don't write the "local:" line on OpenBSD.
92 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
93 add_custom_command(OUTPUT ${native_export_file}
94 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file}
95 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
96 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
97 COMMAND echo " local: *;" >> ${native_export_file}
98 COMMAND echo "};" >> ${native_export_file}
99 DEPENDS ${export_file}
101 COMMENT "Creating export file for ${target_name}")
102 if (${LLVM_LINKER_IS_SOLARISLD})
103 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
104 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
106 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
107 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
110 set(native_export_file "${target_name}.def")
112 add_custom_command(OUTPUT ${native_export_file}
113 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
114 < ${export_file} > ${native_export_file}
115 DEPENDS ${export_file}
117 COMMENT "Creating export file for ${target_name}")
118 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
120 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
122 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
123 LINK_FLAGS " ${export_file_linker_flag}")
126 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
127 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
129 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
131 get_filename_component(extension ${src} EXT)
132 if(extension STREQUAL ".cpp")
133 set(first_source_file ${src})
138 # Force re-linking when the exports file changes. Actually, it
139 # forces recompilation of the source file. The LINK_DEPENDS target
140 # property only works for makefile-based generators.
141 # FIXME: This is not safe because this will create the same target
142 # ${native_export_file} in several different file:
143 # - One where we emitted ${target_name}_exports
144 # - One where we emitted the build command for the following object.
145 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
146 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
148 set_property(DIRECTORY APPEND
149 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
151 add_dependencies(${target_name} ${target_name}_exports)
153 # Add dependency to *_exports later -- CMake issue 14747
154 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
155 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
156 endfunction(add_llvm_symbol_exports)
160 COMMAND "${CMAKE_LINKER}" -v
161 ERROR_VARIABLE stderr
163 set(LLVM_LINKER_DETECTED YES)
164 if("${stderr}" MATCHES "PROJECT:ld64")
165 set(LLVM_LINKER_IS_LD64 YES)
166 message(STATUS "Linker detection: ld64")
168 set(LLVM_LINKER_DETECTED NO)
169 message(STATUS "Linker detection: unknown")
172 # Detect what linker we have here
173 if( LLVM_USE_LINKER )
174 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
176 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
177 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version)
181 OUTPUT_VARIABLE stdout
182 ERROR_VARIABLE stderr
184 set(LLVM_LINKER_DETECTED YES)
185 if("${stdout}" MATCHES "GNU gold")
186 set(LLVM_LINKER_IS_GOLD YES)
187 message(STATUS "Linker detection: GNU Gold")
188 elseif("${stdout}" MATCHES "^LLD")
189 set(LLVM_LINKER_IS_LLD YES)
190 message(STATUS "Linker detection: LLD")
191 elseif("${stdout}" MATCHES "GNU ld")
192 set(LLVM_LINKER_IS_GNULD YES)
193 message(STATUS "Linker detection: GNU ld")
194 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
195 "${stdout}" MATCHES "Solaris Link Editors")
196 set(LLVM_LINKER_IS_SOLARISLD YES)
197 message(STATUS "Linker detection: Solaris ld")
199 set(LLVM_LINKER_DETECTED NO)
200 message(STATUS "Linker detection: unknown")
204 function(add_link_opts target_name)
205 # Don't use linker optimizations in debug builds since it slows down the
206 # linker in a context where the optimizations are not important.
207 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
209 # Pass -O3 to the linker. This enabled different optimizations on different
211 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
212 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
213 LINK_FLAGS " -Wl,-O3")
216 if(LLVM_LINKER_IS_GOLD)
217 # With gold gc-sections is always safe.
218 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
219 LINK_FLAGS " -Wl,--gc-sections")
220 # Note that there is a bug with -Wl,--icf=safe so it is not safe
221 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
224 if(NOT LLVM_NO_DEAD_STRIP)
225 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
226 # ld64's implementation of -dead_strip breaks tools that use plugins.
227 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
228 LINK_FLAGS " -Wl,-dead_strip")
229 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
230 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
231 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
232 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD AND
233 NOT ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD|AIX")
234 # Object files are compiled with -ffunction-data-sections.
235 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
236 # tools that use plugins. Always pass --gc-sections once we require
238 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
239 LINK_FLAGS " -Wl,--gc-sections")
241 else() #LLVM_NO_DEAD_STRIP
242 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
243 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
244 LINK_FLAGS " -Wl,-bnogc")
248 endfunction(add_link_opts)
250 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
251 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
252 # or a certain builder, for eaxample, msbuild.exe, would be confused.
253 function(set_output_directory target)
254 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
256 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
257 # It affects output of add_library(MODULE).
260 set(module_dir ${ARG_BINARY_DIR})
262 set(module_dir ${ARG_LIBRARY_DIR})
264 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
265 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
266 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
268 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
269 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
272 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
273 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
276 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
277 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
282 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
285 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
288 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
293 # If on Windows and building with MSVC, add the resource script containing the
294 # VERSIONINFO data to the project. This embeds version resource information
295 # into the output .exe or .dll.
296 # TODO: Enable for MinGW Windows builds too.
298 function(add_windows_version_resource_file OUT_VAR)
300 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
301 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
302 if(EXISTS ${resource_file})
303 set(sources ${sources} ${resource_file})
304 source_group("Resource Files" ${resource_file})
305 set(windows_resource_file ${resource_file} PARENT_SCOPE)
307 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
309 set(${OUT_VAR} ${sources} PARENT_SCOPE)
310 endfunction(add_windows_version_resource_file)
312 # set_windows_version_resource_properties(name resource_file...
314 # Optional major version number (defaults to LLVM_VERSION_MAJOR)
316 # Optional minor version number (defaults to LLVM_VERSION_MINOR)
317 # VERSION_PATCHLEVEL int
318 # Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
320 # Optional version string (defaults to PACKAGE_VERSION)
322 # Optional product name string (defaults to "LLVM")
324 function(set_windows_version_resource_properties name resource_file)
325 cmake_parse_arguments(ARG
327 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
331 if (NOT DEFINED ARG_VERSION_MAJOR)
332 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
335 if (NOT DEFINED ARG_VERSION_MINOR)
336 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
339 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
340 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
343 if (NOT DEFINED ARG_VERSION_STRING)
344 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
347 if (NOT DEFINED ARG_PRODUCT_NAME)
348 set(ARG_PRODUCT_NAME "LLVM")
351 set_property(SOURCE ${resource_file}
352 PROPERTY COMPILE_FLAGS /nologo)
353 set_property(SOURCE ${resource_file}
354 PROPERTY COMPILE_DEFINITIONS
355 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
356 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
357 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
358 "RC_VERSION_FIELD_4=0"
359 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
360 "RC_INTERNAL_NAME=\"${name}\""
361 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
362 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
363 endfunction(set_windows_version_resource_properties)
365 # llvm_add_library(name sources...
367 # STATIC by default w/o BUILD_SHARED_LIBS.
368 # SHARED by default w/ BUILD_SHARED_LIBS.
370 # Also create an OBJECT library target. Default if STATIC && SHARED.
372 # Target ${name} might not be created on unsupported platforms.
373 # Check with "if(TARGET ${name})".
374 # DISABLE_LLVM_LINK_LLVM_DYLIB
375 # Do not link this library to libLLVM, even if
376 # LLVM_LINK_LLVM_DYLIB is enabled.
378 # Corresponds to OUTPUT_NAME in target properties.
380 # Same semantics as add_dependencies().
381 # LINK_COMPONENTS components...
382 # Same as the variable LLVM_LINK_COMPONENTS.
383 # LINK_LIBS lib_targets...
384 # Same semantics as target_link_libraries().
386 # May specify header files for IDE generators.
388 # Should set SONAME link flags and create symlinks
390 # Suppress default RPATH settings in shared libraries.
392 # The tool (i.e. cmake target) that this plugin will link against
394 function(llvm_add_library name)
395 cmake_parse_arguments(ARG
396 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH"
397 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
398 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
400 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
401 if(ARG_ADDITIONAL_HEADERS)
402 # Pass through ADDITIONAL_HEADERS.
403 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
406 set(ALL_FILES ${ARG_OBJLIBS})
408 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
412 if(ARG_SHARED OR ARG_STATIC)
413 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
415 # Plugins that link against a tool are allowed even when plugins in general are not
416 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
417 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
422 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
424 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
433 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
434 # Generate an obj library for both targets.
435 set(obj_name "obj.${name}")
436 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
439 llvm_update_compile_flags(${obj_name})
440 if(CMAKE_GENERATOR STREQUAL "Xcode")
441 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
442 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
443 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
445 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
447 # Do add_dependencies(obj) later due to CMake issue 14747.
448 list(APPEND objlibs ${obj_name})
450 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
452 add_dependencies(${obj_name} ${ARG_DEPENDS})
456 if(ARG_SHARED AND ARG_STATIC)
458 set(name_static "${name}_static")
460 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
462 # DEPENDS has been appended to LLVM_COMMON_LIBS.
463 llvm_add_library(${name_static} STATIC
465 OBJLIBS ${ALL_FILES} # objlib
466 LINK_LIBS ${ARG_LINK_LIBS}
467 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
469 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
474 add_library(${name} MODULE ${ALL_FILES})
476 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
477 add_library(${name} SHARED ${ALL_FILES})
479 add_library(${name} STATIC ${ALL_FILES})
482 if(NOT ARG_NO_INSTALL_RPATH)
483 if(ARG_MODULE OR ARG_SHARED)
484 llvm_setup_rpath(${name})
488 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
490 if(DEFINED windows_resource_file)
491 set_windows_version_resource_properties(${name} ${windows_resource_file})
492 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
495 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
496 # $<TARGET_OBJECTS> doesn't require compile flags.
498 llvm_update_compile_flags(${name})
500 add_link_opts( ${name} )
502 set_target_properties(${name}
504 OUTPUT_NAME ${ARG_OUTPUT_NAME}
509 set_target_properties(${name} PROPERTIES
511 SUFFIX ${LLVM_PLUGIN_EXT}
517 set_target_properties(${name} PROPERTIES
522 # Set SOVERSION on shared libraries that lack explicit SONAME
523 # specifier, on *nix systems that are not Darwin.
524 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
525 set_target_properties(${name}
527 # Since 4.0.0, the ABI version is indicated by the major version
528 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
529 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
533 if(ARG_MODULE OR ARG_SHARED)
534 # Do not add -Dname_EXPORTS to the command-line when building files in this
535 # target. Doing so is actively harmful for the modules build because it
536 # creates extra module variants, and not useful because we don't use these
538 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
540 if (LLVM_EXPORTED_SYMBOL_FILE)
541 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
545 if(ARG_SHARED AND UNIX)
546 if(NOT APPLE AND ARG_SONAME)
547 get_target_property(output_name ${name} OUTPUT_NAME)
548 if(${output_name} STREQUAL "output_name-NOTFOUND")
549 set(output_name ${name})
551 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
552 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
553 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
554 llvm_install_library_symlink(${api_name} ${library_name} SHARED
557 llvm_install_library_symlink(${output_name} ${library_name} SHARED
563 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
564 # On DLL platforms symbols are imported from the tool by linking against it.
565 set(llvm_libs ${ARG_PLUGIN_TOOL})
566 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
567 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
570 llvm_map_components_to_libnames(llvm_libs
571 ${ARG_LINK_COMPONENTS}
572 ${LLVM_LINK_COMPONENTS}
576 # Components have not been defined explicitly in CMake, so add the
577 # dependency information for this library as defined by LLVMBuild.
579 # It would be nice to verify that we have the dependencies for this library
580 # name, but using get_property(... SET) doesn't suffice to determine if a
581 # property has been set to an empty value.
582 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
586 set(libtype INTERFACE)
588 # We can use PRIVATE since SO knows its dependent libs.
592 target_link_libraries(${name} ${libtype}
598 if(LLVM_COMMON_DEPENDS)
599 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
600 # Add dependencies also to objlibs.
601 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
602 foreach(objlib ${objlibs})
603 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
607 if(ARG_SHARED OR ARG_MODULE)
608 llvm_externalize_debuginfo(${name})
609 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
611 # clang and newer versions of ninja use high-resolutions timestamps,
612 # but older versions of libtool on Darwin don't, so the archive will
613 # often get an older timestamp than the last object that was added
614 # or updated. To fix this, we add a custom command to touch archive
615 # after it's been built so that ninja won't rebuild it unnecessarily
616 # the next time it's run.
617 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
618 add_custom_command(TARGET ${name}
620 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
625 function(add_llvm_install_targets target)
626 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX" "DEPENDS" ${ARGN})
628 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
631 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
634 set(file_dependencies)
635 set(target_dependencies)
636 foreach(dependency ${ARG_DEPENDS})
637 if(TARGET ${dependency})
638 list(APPEND target_dependencies ${dependency})
640 list(APPEND file_dependencies ${dependency})
644 add_custom_target(${target}
645 DEPENDS ${file_dependencies}
646 COMMAND "${CMAKE_COMMAND}"
649 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
651 add_custom_target(${target}-stripped
652 DEPENDS ${file_dependencies}
653 COMMAND "${CMAKE_COMMAND}"
656 -DCMAKE_INSTALL_DO_STRIP=1
657 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
659 if(target_dependencies)
660 add_dependencies(${target} ${target_dependencies})
661 add_dependencies(${target}-stripped ${target_dependencies})
665 macro(add_llvm_library name)
666 cmake_parse_arguments(ARG
667 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
672 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
673 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
674 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
676 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
679 # Libraries that are meant to only be exposed via the build tree only are
680 # never installed and are only exported as a target in the special build tree
682 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
683 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
684 set(in_llvm_libs YES)
687 if (ARG_MODULE AND NOT TARGET ${name})
688 # Add empty "phony" target
689 add_custom_target(${name})
690 elseif( EXCLUDE_FROM_ALL )
691 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
692 elseif(ARG_BUILDTREE_ONLY)
693 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
695 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
697 set(export_to_llvmexports)
698 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
699 (in_llvm_libs AND "llvm-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS) OR
700 NOT LLVM_DISTRIBUTION_COMPONENTS)
701 set(export_to_llvmexports EXPORT LLVMExports)
702 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
705 install(TARGETS ${name}
706 ${export_to_llvmexports}
707 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
708 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
709 RUNTIME DESTINATION bin COMPONENT ${name})
711 if (NOT LLVM_ENABLE_IDE)
712 add_llvm_install_targets(install-${name}
717 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
720 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
722 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
724 endmacro(add_llvm_library name)
726 macro(add_llvm_executable name)
727 cmake_parse_arguments(ARG
728 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH"
729 "ENTITLEMENTS;BUNDLE_PATH"
733 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
735 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
738 if(LLVM_ENABLE_OBJLIB)
739 # Generate an obj library for both targets.
740 set(obj_name "obj.${name}")
741 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
744 llvm_update_compile_flags(${obj_name})
745 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
747 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
750 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
753 # Note: the dummy.cpp source file provides no definitions. However,
754 # it forces Xcode to properly link the static library.
755 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
758 if( EXCLUDE_FROM_ALL )
759 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
761 add_executable(${name} ${ALL_FILES})
764 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
766 if(NOT ARG_NO_INSTALL_RPATH)
767 llvm_setup_rpath(${name})
770 if(DEFINED windows_resource_file)
771 set_windows_version_resource_properties(${name} ${windows_resource_file})
774 # $<TARGET_OBJECTS> doesn't require compile flags.
775 if(NOT LLVM_ENABLE_OBJLIB)
776 llvm_update_compile_flags(${name})
778 add_link_opts( ${name} )
780 # Do not add -Dname_EXPORTS to the command-line when building files in this
781 # target. Doing so is actively harmful for the modules build because it
782 # creates extra module variants, and not useful because we don't use these
784 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
786 if (LLVM_EXPORTED_SYMBOL_FILE)
787 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
788 endif(LLVM_EXPORTED_SYMBOL_FILE)
790 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
791 set(USE_SHARED USE_SHARED)
794 set(EXCLUDE_FROM_ALL OFF)
795 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
796 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
797 if( LLVM_COMMON_DEPENDS )
798 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
799 endif( LLVM_COMMON_DEPENDS )
801 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
802 llvm_externalize_debuginfo(${name})
804 if (LLVM_PTHREAD_LIB)
805 # libpthreads overrides some standard library symbols, so main
806 # executable must be linked with it in order to provide consistent
807 # API for all shared libaries loaded by this executable.
808 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
811 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
812 endmacro(add_llvm_executable name)
814 function(export_executable_symbols target)
815 if (LLVM_EXPORTED_SYMBOL_FILE)
816 # The symbol file should contain the symbols we want the executable to
818 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
819 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
820 # Extract the symbols to export from the static libraries that the
821 # executable links against.
822 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
823 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
824 # We need to consider not just the direct link dependencies, but also the
825 # transitive link dependencies. Do this by starting with the set of direct
826 # dependencies, then the dependencies of those dependencies, and so on.
827 get_target_property(new_libs ${target} LINK_LIBRARIES)
828 set(link_libs ${new_libs})
829 while(NOT "${new_libs}" STREQUAL "")
830 foreach(lib ${new_libs})
832 get_target_property(lib_type ${lib} TYPE)
833 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
834 list(APPEND static_libs ${lib})
836 list(APPEND other_libs ${lib})
838 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
839 foreach(transitive_lib ${transitive_libs})
840 list(FIND link_libs ${transitive_lib} idx)
841 if(TARGET ${transitive_lib} AND idx EQUAL -1)
842 list(APPEND newer_libs ${transitive_lib})
843 list(APPEND link_libs ${transitive_lib})
845 endforeach(transitive_lib)
848 set(new_libs ${newer_libs})
852 set(mangling microsoft)
854 set(mangling itanium)
856 add_custom_command(OUTPUT ${exported_symbol_file}
857 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
858 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
859 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
861 COMMENT "Generating export list for ${target}")
862 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
863 # If something links against this executable then we want a
864 # transitive link against only the libraries whose symbols
865 # we aren't exporting.
866 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
867 # The default import library suffix that cmake uses for cygwin/mingw is
868 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
869 # where the import libraries of both get named libclang.dll.a. Use a suffix
870 # of ".exe.a" to avoid this.
872 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
874 elseif(NOT (WIN32 OR CYGWIN))
875 # On Windows auto-exporting everything doesn't work because of the limit on
876 # the size of the exported symbol table, but on other platforms we can do
877 # it without any trouble.
878 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
880 set_property(TARGET ${target} APPEND_STRING PROPERTY
881 LINK_FLAGS " -rdynamic")
886 if(NOT LLVM_TOOLCHAIN_TOOLS)
887 set (LLVM_TOOLCHAIN_TOOLS
900 macro(add_llvm_tool name)
901 if( NOT LLVM_BUILD_TOOLS )
902 set(EXCLUDE_FROM_ALL ON)
904 add_llvm_executable(${name} ${ARGN})
906 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
907 if( LLVM_BUILD_TOOLS )
908 set(export_to_llvmexports)
909 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
910 NOT LLVM_DISTRIBUTION_COMPONENTS)
911 set(export_to_llvmexports EXPORT LLVMExports)
912 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
915 install(TARGETS ${name}
916 ${export_to_llvmexports}
917 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
920 if (NOT LLVM_ENABLE_IDE)
921 add_llvm_install_targets(install-${name}
927 if( LLVM_BUILD_TOOLS )
928 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
930 set_target_properties(${name} PROPERTIES FOLDER "Tools")
931 endmacro(add_llvm_tool name)
934 macro(add_llvm_example name)
935 if( NOT LLVM_BUILD_EXAMPLES )
936 set(EXCLUDE_FROM_ALL ON)
938 add_llvm_executable(${name} ${ARGN})
939 if( LLVM_BUILD_EXAMPLES )
940 install(TARGETS ${name} RUNTIME DESTINATION examples)
942 set_target_properties(${name} PROPERTIES FOLDER "Examples")
943 endmacro(add_llvm_example name)
945 # This is a macro that is used to create targets for executables that are needed
946 # for development, but that are not intended to be installed by default.
947 macro(add_llvm_utility name)
948 if ( NOT LLVM_BUILD_UTILS )
949 set(EXCLUDE_FROM_ALL ON)
952 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
953 set_target_properties(${name} PROPERTIES FOLDER "Utils")
954 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
955 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
956 set(export_to_llvmexports)
957 if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
958 NOT LLVM_DISTRIBUTION_COMPONENTS)
959 set(export_to_llvmexports EXPORT LLVMExports)
960 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
963 install(TARGETS ${name}
964 ${export_to_llvmexports}
965 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
968 if (NOT LLVM_ENABLE_IDE)
969 add_llvm_install_targets(install-${name}
973 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
974 elseif(LLVM_BUILD_UTILS)
975 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
978 endmacro(add_llvm_utility name)
980 macro(add_llvm_fuzzer name)
981 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
982 if( LLVM_LIB_FUZZING_ENGINE )
983 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
984 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
985 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
986 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
987 elseif( LLVM_USE_SANITIZE_COVERAGE )
988 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
989 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
990 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
991 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
992 elseif( ARG_DUMMY_MAIN )
993 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
994 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
998 macro(add_llvm_target target_name)
999 include_directories(BEFORE
1000 ${CMAKE_CURRENT_BINARY_DIR}
1001 ${CMAKE_CURRENT_SOURCE_DIR})
1002 add_llvm_library(LLVM${target_name} ${ARGN})
1003 set( CURRENT_LLVM_TARGET LLVM${target_name} )
1004 endmacro(add_llvm_target)
1006 function(canonicalize_tool_name name output)
1007 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1008 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1009 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1010 set(${output} "${nameUPPER}" PARENT_SCOPE)
1011 endfunction(canonicalize_tool_name)
1013 # Custom add_subdirectory wrapper
1014 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1015 # path if it differs from the name.
1016 function(add_llvm_subdirectory project type name)
1017 set(add_llvm_external_dir "${ARGN}")
1018 if("${add_llvm_external_dir}" STREQUAL "")
1019 set(add_llvm_external_dir ${name})
1021 canonicalize_tool_name(${name} nameUPPER)
1022 set(canonical_full_name ${project}_${type}_${nameUPPER})
1023 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1024 if(already_processed)
1027 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1029 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1030 # Treat it as in-tree subproject.
1031 option(${canonical_full_name}_BUILD
1032 "Whether to build ${name} as part of ${project}" On)
1033 mark_as_advanced(${project}_${type}_${name}_BUILD)
1034 if(${canonical_full_name}_BUILD)
1035 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
1038 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1039 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1040 CACHE PATH "Path to ${name} source directory")
1041 set(${canonical_full_name}_BUILD_DEFAULT ON)
1042 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1043 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1045 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
1046 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1048 option(${canonical_full_name}_BUILD
1049 "Whether to build ${name} as part of LLVM"
1050 ${${canonical_full_name}_BUILD_DEFAULT})
1051 if (${canonical_full_name}_BUILD)
1052 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1053 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1054 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1055 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1061 # Add external project that may want to be built as part of llvm such as Clang,
1062 # lld, and Polly. This adds two options. One for the source directory of the
1063 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1064 # enable or disable building it with everything else.
1065 # Additional parameter can be specified as the name of directory.
1066 macro(add_llvm_external_project name)
1067 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1070 macro(add_llvm_tool_subdirectory name)
1071 add_llvm_external_project(${name})
1072 endmacro(add_llvm_tool_subdirectory)
1074 function(get_project_name_from_src_var var output)
1075 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1076 MACHED_TOOL "${var}")
1078 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1080 set(${output} PARENT_SCOPE)
1084 function(create_subdirectory_options project type)
1085 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1086 foreach(dir ${sub-dirs})
1087 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1088 canonicalize_tool_name(${dir} name)
1089 option(${project}_${type}_${name}_BUILD
1090 "Whether to build ${name} as part of ${project}" On)
1091 mark_as_advanced(${project}_${type}_${name}_BUILD)
1094 endfunction(create_subdirectory_options)
1096 function(create_llvm_tool_options)
1097 create_subdirectory_options(LLVM TOOL)
1098 endfunction(create_llvm_tool_options)
1100 function(llvm_add_implicit_projects project)
1101 set(list_of_implicit_subdirs "")
1102 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1103 foreach(dir ${sub-dirs})
1104 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1105 canonicalize_tool_name(${dir} name)
1106 if (${project}_TOOL_${name}_BUILD)
1107 get_filename_component(fn "${dir}" NAME)
1108 list(APPEND list_of_implicit_subdirs "${fn}")
1113 foreach(external_proj ${list_of_implicit_subdirs})
1114 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1116 endfunction(llvm_add_implicit_projects)
1118 function(add_llvm_implicit_projects)
1119 llvm_add_implicit_projects(LLVM)
1120 endfunction(add_llvm_implicit_projects)
1122 # Generic support for adding a unittest.
1123 function(add_unittest test_suite test_name)
1124 if( NOT LLVM_BUILD_TESTS )
1125 set(EXCLUDE_FROM_ALL ON)
1128 # Our current version of gtest does not properly recognize C++11 support
1129 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM
1130 # itself requires C++11, we can safely force it on unconditionally so that
1131 # we don't have to fight with the buggy gtest check.
1132 add_definitions(-DGTEST_LANG_CXX11=1)
1133 add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
1135 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1136 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
1137 if (NOT LLVM_ENABLE_THREADS)
1138 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1141 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1142 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1144 # Some parts of gtest rely on this GNU extension, don't warn on it.
1145 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1146 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1149 set(LLVM_REQUIRES_RTTI OFF)
1151 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1152 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1153 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1154 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1155 # libpthreads overrides some standard library symbols, so main
1156 # executable must be linked with it in order to provide consistent
1157 # API for all shared libaries loaded by this executable.
1158 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
1160 add_dependencies(${test_suite} ${test_name})
1161 get_target_property(test_suite_folder ${test_suite} FOLDER)
1162 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1163 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1167 # Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1169 function(add_unittest_with_input_files test_suite test_name)
1170 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1172 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1173 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1175 add_unittest(${test_suite} ${test_name} ${ARGN})
1178 # Generic support for adding a benchmark.
1179 function(add_benchmark benchmark_name)
1180 if( NOT LLVM_BUILD_BENCHMARKS )
1181 set(EXCLUDE_FROM_ALL ON)
1184 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1185 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1186 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1187 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1188 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1191 function(llvm_add_go_executable binary pkgpath)
1192 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1194 if(LLVM_BINDINGS MATCHES "go")
1195 # FIXME: This should depend only on the libraries Go needs.
1196 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1197 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1198 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1199 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1201 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1202 foreach(d ${include_dirs})
1203 set(cppflags "${cppflags} -I${d}")
1205 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1206 add_custom_command(OUTPUT ${binpath}
1207 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1208 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1209 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1210 ${llvmlibs} ${ARG_DEPENDS}
1211 COMMENT "Building Go executable ${binary}"
1214 add_custom_target(${binary} ALL DEPENDS ${binpath})
1216 add_custom_target(${binary} DEPENDS ${binpath})
1221 # This function canonicalize the CMake variables passed by names
1222 # from CMake boolean to 0/1 suitable for passing into Python or C++,
1224 function(llvm_canonicalize_cmake_booleans)
1225 foreach(var ${ARGN})
1227 set(${var} 1 PARENT_SCOPE)
1229 set(${var} 0 PARENT_SCOPE)
1232 endfunction(llvm_canonicalize_cmake_booleans)
1234 macro(set_llvm_build_mode)
1235 # Configuration-time: See Unit/lit.site.cfg.in
1236 if (CMAKE_CFG_INTDIR STREQUAL ".")
1237 set(LLVM_BUILD_MODE ".")
1239 set(LLVM_BUILD_MODE "%(build_mode)s")
1243 # This function provides an automatic way to 'configure'-like generate a file
1244 # based on a set of common and custom variables, specifically targeting the
1245 # variables needed for the 'lit.site.cfg' files. This function bundles the
1246 # common variables that any Lit instance is likely to need, and custom
1247 # variables can be passed in.
1248 function(configure_lit_site_cfg site_in site_out)
1249 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
1251 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1252 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1253 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1255 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1256 set(ARG_OUTPUT_MAPPING "${site_out}")
1259 foreach(c ${LLVM_TARGETS_TO_BUILD})
1260 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1262 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1264 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1266 set_llvm_build_mode()
1268 # They below might not be the build tree but provided binary tree.
1269 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1270 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1271 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1272 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1274 # SHLIBDIR points the build tree.
1275 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1277 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1278 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1279 # plugins. We may rename it.
1280 if(LLVM_ENABLE_PLUGINS)
1281 set(ENABLE_SHARED "1")
1283 set(ENABLE_SHARED "0")
1286 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1287 set(ENABLE_ASSERTIONS "1")
1289 set(ENABLE_ASSERTIONS "0")
1292 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1293 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1295 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1296 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1297 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1299 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
1301 # Override config_target_triple (and the env)
1302 if(LLVM_TARGET_TRIPLE_ENV)
1303 # This is expanded into the heading.
1304 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1306 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1307 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1310 # This is expanded to; config.target_triple = ""+config.target_triple+""
1311 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1314 configure_file(${site_in} ${site_out} @ONLY)
1315 if (EXISTS "${ARG_MAIN_CONFIG}")
1316 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
1317 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)
1318 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}")
1319 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP})
1323 function(dump_all_cmake_variables)
1324 get_cmake_property(_variableNames VARIABLES)
1325 foreach (_variableName ${_variableNames})
1326 message(STATUS "${_variableName}=${${_variableName}}")
1330 function(get_llvm_lit_path base_dir file_name)
1331 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1333 if (ARG_ALLOW_EXTERNAL)
1334 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1335 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1336 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1339 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1340 if (EXISTS ${LLVM_EXTERNAL_LIT})
1341 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1342 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1343 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1344 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1347 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1352 set(lit_file_name "llvm-lit")
1353 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
1354 # llvm-lit needs suffix.py for multiprocess to find a main module.
1355 set(lit_file_name "${lit_file_name}.py")
1357 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1359 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1360 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1361 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1364 # Allow individual projects to provide an override
1365 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1366 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1367 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1368 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1370 set(LLVM_LIT_BASE_DIR "")
1373 # Cache this so we don't have to do it again and have subsequent calls
1374 # potentially disagree on the value.
1375 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1376 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1379 # A raw function to create a lit target. This is used to implement the testuite
1380 # management functions.
1381 function(add_lit_target target comment)
1382 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1383 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1384 separate_arguments(LIT_ARGS)
1385 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1386 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1389 # Get the path to the lit to *run* tests with. This can be overriden by
1390 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1397 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1398 list(APPEND LIT_COMMAND ${LIT_ARGS})
1399 foreach(param ${ARG_PARAMS})
1400 list(APPEND LIT_COMMAND --param ${param})
1402 if (ARG_UNPARSED_ARGUMENTS)
1403 add_custom_target(${target}
1404 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1405 COMMENT "${comment}"
1409 add_custom_target(${target}
1410 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1411 message(STATUS "${target} does nothing.")
1415 add_dependencies(${target} ${ARG_DEPENDS})
1418 # Tests should be excluded from "Build Solution".
1419 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1422 # A function to add a set of lit test suites to be driven through 'check-*' targets.
1423 function(add_lit_testsuite target comment)
1424 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1426 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1427 if(NOT EXCLUDE_FROM_ALL)
1428 # Register the testsuites, params and depends for the global check rule.
1429 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1430 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1431 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1432 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1435 # Produce a specific suffixed check rule.
1436 add_lit_target(${target} ${comment}
1437 ${ARG_UNPARSED_ARGUMENTS}
1438 PARAMS ${ARG_PARAMS}
1439 DEPENDS ${ARG_DEPENDS}
1444 function(add_lit_testsuites project directory)
1445 if (NOT LLVM_ENABLE_IDE)
1446 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1448 # Search recursively for test directories by assuming anything not
1449 # in a directory called Inputs contains tests.
1450 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1451 foreach(lit_suite ${to_process})
1452 if(NOT IS_DIRECTORY ${lit_suite})
1455 string(FIND ${lit_suite} Inputs is_inputs)
1456 string(FIND ${lit_suite} Output is_output)
1457 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1461 # Create a check- target for the directory.
1462 string(REPLACE ${directory} "" name_slash ${lit_suite})
1464 string(REPLACE "/" "-" name_slash ${name_slash})
1465 string(REPLACE "\\" "-" name_dashes ${name_slash})
1466 string(TOLOWER "${project}${name_dashes}" name_var)
1467 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1469 PARAMS ${ARG_PARAMS}
1470 DEPENDS ${ARG_DEPENDS}
1478 function(llvm_install_library_symlink name dest type)
1479 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1480 foreach(path ${CMAKE_MODULE_PATH})
1481 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1482 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1487 set(component ${ARG_COMPONENT})
1489 set(component ${name})
1492 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1493 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1495 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1496 if(WIN32 AND "${type}" STREQUAL "SHARED")
1500 install(SCRIPT ${INSTALL_SYMLINK}
1501 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1502 COMPONENT ${component})
1504 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1505 add_llvm_install_targets(install-${name}
1506 DEPENDS ${name} ${dest} install-${dest}
1511 function(llvm_install_symlink name dest)
1512 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1513 foreach(path ${CMAKE_MODULE_PATH})
1514 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1515 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1521 set(component ${ARG_COMPONENT})
1523 if(ARG_ALWAYS_GENERATE)
1524 set(component ${dest})
1526 set(component ${name})
1530 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1531 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1533 install(SCRIPT ${INSTALL_SYMLINK}
1534 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1535 COMPONENT ${component})
1537 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1538 add_llvm_install_targets(install-${name}
1539 DEPENDS ${name} ${dest} install-${dest}
1544 function(add_llvm_tool_symlink link_name target)
1545 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1546 set(dest_binary "$<TARGET_FILE:${target}>")
1548 # This got a bit gross... For multi-configuration generators the target
1549 # properties return the resolved value of the string, not the build system
1550 # expression. To reconstruct the platform-agnostic path we have to do some
1551 # magic. First we grab one of the types, and a type-specific path. Then from
1552 # the type-specific path we find the last occurrence of the type in the path,
1553 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1555 if(NOT ARG_OUTPUT_DIR)
1556 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1557 # the same directory.
1559 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1561 if(CMAKE_CONFIGURATION_TYPES)
1562 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1563 string(TOUPPER ${first_type} first_type_upper)
1564 set(first_type_suffix _${first_type_upper})
1566 get_target_property(target_type ${target} TYPE)
1567 if(${target_type} STREQUAL "STATIC_LIBRARY")
1568 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1569 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1570 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1572 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1574 if(CMAKE_CONFIGURATION_TYPES)
1575 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1576 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1577 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1578 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1579 path_suffix ${path_suffix})
1580 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1585 set(LLVM_LINK_OR_COPY create_symlink)
1587 set(LLVM_LINK_OR_COPY copy)
1590 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1592 set(target_name ${link_name})
1593 if(TARGET ${link_name})
1594 set(target_name ${link_name}-link)
1598 if(ARG_ALWAYS_GENERATE)
1599 set_property(DIRECTORY APPEND PROPERTY
1600 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1601 add_custom_command(TARGET ${target} POST_BUILD
1602 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1604 add_custom_command(OUTPUT ${output_path}
1605 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1607 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1608 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1610 # Make sure both the link and target are toolchain tools
1611 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1612 set(TOOL_IS_TOOLCHAIN ON)
1615 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1616 llvm_install_symlink(${link_name} ${target})
1621 function(llvm_externalize_debuginfo name)
1622 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1626 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1629 set(CMAKE_STRIP xcrun strip)
1631 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>)
1633 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
1638 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
1639 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
1644 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
1646 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
1647 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
1649 set(output_path "-o=${output_name}")
1652 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1653 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1655 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1656 set_property(TARGET ${name} APPEND_STRING PROPERTY
1657 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1659 if(NOT CMAKE_DSYMUTIL)
1660 set(CMAKE_DSYMUTIL xcrun dsymutil)
1662 add_custom_command(TARGET ${name} POST_BUILD
1663 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
1667 add_custom_command(TARGET ${name} POST_BUILD
1668 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1669 ${strip_command} -R .gnu_debuglink
1670 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1675 # Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
1676 function(llvm_codesign name)
1677 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
1679 if(NOT LLVM_CODESIGNING_IDENTITY)
1683 if(CMAKE_GENERATOR STREQUAL "Xcode")
1684 set_target_properties(${name} PROPERTIES
1685 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
1687 if(DEFINED ARG_ENTITLEMENTS)
1688 set_target_properties(${name} PROPERTIES
1689 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
1692 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
1693 if(NOT CMAKE_CODESIGN)
1694 set(CMAKE_CODESIGN xcrun codesign)
1696 if(NOT CMAKE_CODESIGN_ALLOCATE)
1698 COMMAND xcrun -f codesign_allocate
1699 OUTPUT_STRIP_TRAILING_WHITESPACE
1700 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
1703 if(DEFINED ARG_ENTITLEMENTS)
1704 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
1707 if (NOT ARG_BUNDLE_PATH)
1708 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
1712 set(force_flag "-f")
1716 TARGET ${name} POST_BUILD
1717 COMMAND ${CMAKE_COMMAND} -E
1718 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
1719 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
1720 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
1725 function(llvm_setup_rpath name)
1726 if(CMAKE_INSTALL_RPATH)
1730 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1731 set(extra_libdir ${LLVM_LIBRARY_DIR})
1732 elseif(LLVM_BUILD_LIBRARY_DIR)
1733 set(extra_libdir ${LLVM_LIBRARY_DIR})
1737 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1738 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1740 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1741 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1742 set_property(TARGET ${name} APPEND_STRING PROPERTY
1743 LINK_FLAGS " -Wl,-z,origin ")
1745 if(LLVM_LINKER_IS_GNULD)
1746 # $ORIGIN is not interpreted at link time by ld.bfd
1747 set_property(TARGET ${name} APPEND_STRING PROPERTY
1748 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1754 set_target_properties(${name} PROPERTIES
1755 BUILD_WITH_INSTALL_RPATH On
1756 INSTALL_RPATH "${_install_rpath}"
1757 ${_install_name_dir})
1760 function(setup_dependency_debugging name)
1761 if(NOT LLVM_DEPENDENCY_DEBUGGING)
1765 if("intrinsics_gen" IN_LIST ARGN)
1769 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
1770 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
1772 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
1773 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
1776 function(find_first_existing_vc_file path out_var)
1777 if(NOT EXISTS "${path}")
1780 if(EXISTS "${path}/.svn")
1782 "${path}/.svn/wc.db" # SVN 1.7
1783 "${path}/.svn/entries" # SVN 1.6
1785 foreach(file IN LISTS svn_files)
1786 if(EXISTS "${file}")
1787 set(${out_var} "${file}" PARENT_SCOPE)
1794 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
1795 WORKING_DIRECTORY ${path}
1796 RESULT_VARIABLE git_result
1797 OUTPUT_VARIABLE git_output
1799 if(git_result EQUAL 0)
1800 string(STRIP "${git_output}" git_output)
1801 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
1802 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
1803 if (NOT EXISTS "${git_dir}/logs/HEAD")
1804 file(WRITE "${git_dir}/logs/HEAD" "")
1806 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)