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 function(llvm_add_library name)
402 cmake_parse_arguments(ARG
403 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH"
404 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
405 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
407 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
408 if(ARG_ADDITIONAL_HEADERS)
409 # Pass through ADDITIONAL_HEADERS.
410 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
413 set(ALL_FILES ${ARG_OBJLIBS})
415 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
419 if(ARG_SHARED OR ARG_STATIC)
420 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
422 # Plugins that link against a tool are allowed even when plugins in general are not
423 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
424 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
429 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
431 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
440 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
441 # Generate an obj library for both targets.
442 set(obj_name "obj.${name}")
443 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
446 llvm_update_compile_flags(${obj_name})
447 if(CMAKE_GENERATOR STREQUAL "Xcode")
448 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
449 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
450 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
452 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
454 # Do add_dependencies(obj) later due to CMake issue 14747.
455 list(APPEND objlibs ${obj_name})
457 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
459 add_dependencies(${obj_name} ${ARG_DEPENDS})
463 if(ARG_SHARED AND ARG_STATIC)
465 set(name_static "${name}_static")
467 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
469 # DEPENDS has been appended to LLVM_COMMON_LIBS.
470 llvm_add_library(${name_static} STATIC
472 OBJLIBS ${ALL_FILES} # objlib
473 LINK_LIBS ${ARG_LINK_LIBS}
474 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
476 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
481 add_library(${name} MODULE ${ALL_FILES})
483 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
484 add_library(${name} SHARED ${ALL_FILES})
486 add_library(${name} STATIC ${ALL_FILES})
489 if(NOT ARG_NO_INSTALL_RPATH)
490 if(ARG_MODULE OR ARG_SHARED)
491 llvm_setup_rpath(${name})
495 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
497 if(DEFINED windows_resource_file)
498 set_windows_version_resource_properties(${name} ${windows_resource_file})
499 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
502 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
503 # $<TARGET_OBJECTS> doesn't require compile flags.
505 llvm_update_compile_flags(${name})
507 add_link_opts( ${name} )
509 set_target_properties(${name}
511 OUTPUT_NAME ${ARG_OUTPUT_NAME}
516 set_target_properties(${name} PROPERTIES
518 SUFFIX ${LLVM_PLUGIN_EXT}
524 set_target_properties(${name} PROPERTIES
529 # Set SOVERSION on shared libraries that lack explicit SONAME
530 # specifier, on *nix systems that are not Darwin.
531 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
532 set_target_properties(${name}
534 # Since 4.0.0, the ABI version is indicated by the major version
535 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
536 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
540 if(ARG_MODULE OR ARG_SHARED)
541 # Do not add -Dname_EXPORTS to the command-line when building files in this
542 # target. Doing so is actively harmful for the modules build because it
543 # creates extra module variants, and not useful because we don't use these
545 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
547 if (LLVM_EXPORTED_SYMBOL_FILE)
548 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
552 if(ARG_SHARED AND UNIX)
553 if(NOT APPLE AND ARG_SONAME)
554 get_target_property(output_name ${name} OUTPUT_NAME)
555 if(${output_name} STREQUAL "output_name-NOTFOUND")
556 set(output_name ${name})
558 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
559 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
560 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
561 llvm_install_library_symlink(${api_name} ${library_name} SHARED
564 llvm_install_library_symlink(${output_name} ${library_name} SHARED
570 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
571 # On DLL platforms symbols are imported from the tool by linking against it.
572 set(llvm_libs ${ARG_PLUGIN_TOOL})
573 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
574 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
577 llvm_map_components_to_libnames(llvm_libs
578 ${ARG_LINK_COMPONENTS}
579 ${LLVM_LINK_COMPONENTS}
583 # Components have not been defined explicitly in CMake, so add the
584 # dependency information for this library as defined by LLVMBuild.
586 # It would be nice to verify that we have the dependencies for this library
587 # name, but using get_property(... SET) doesn't suffice to determine if a
588 # property has been set to an empty value.
589 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
593 set(libtype INTERFACE)
595 # We can use PRIVATE since SO knows its dependent libs.
599 target_link_libraries(${name} ${libtype}
605 if(LLVM_COMMON_DEPENDS)
606 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
607 # Add dependencies also to objlibs.
608 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
609 foreach(objlib ${objlibs})
610 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
614 if(ARG_SHARED OR ARG_MODULE)
615 llvm_externalize_debuginfo(${name})
616 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
618 # clang and newer versions of ninja use high-resolutions timestamps,
619 # but older versions of libtool on Darwin don't, so the archive will
620 # often get an older timestamp than the last object that was added
621 # or updated. To fix this, we add a custom command to touch archive
622 # after it's been built so that ninja won't rebuild it unnecessarily
623 # the next time it's run.
624 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
625 add_custom_command(TARGET ${name}
627 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
632 function(add_llvm_install_targets target)
633 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX" "DEPENDS" ${ARGN})
635 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
638 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
641 set(file_dependencies)
642 set(target_dependencies)
643 foreach(dependency ${ARG_DEPENDS})
644 if(TARGET ${dependency})
645 list(APPEND target_dependencies ${dependency})
647 list(APPEND file_dependencies ${dependency})
651 add_custom_target(${target}
652 DEPENDS ${file_dependencies}
653 COMMAND "${CMAKE_COMMAND}"
656 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
658 add_custom_target(${target}-stripped
659 DEPENDS ${file_dependencies}
660 COMMAND "${CMAKE_COMMAND}"
663 -DCMAKE_INSTALL_DO_STRIP=1
664 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
666 if(target_dependencies)
667 add_dependencies(${target} ${target_dependencies})
668 add_dependencies(${target}-stripped ${target_dependencies})
672 macro(add_llvm_library name)
673 cmake_parse_arguments(ARG
674 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
679 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
680 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
681 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
683 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
686 # Libraries that are meant to only be exposed via the build tree only are
687 # never installed and are only exported as a target in the special build tree
689 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
690 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
691 set(in_llvm_libs YES)
694 if (ARG_MODULE AND NOT TARGET ${name})
695 # Add empty "phony" target
696 add_custom_target(${name})
697 elseif( EXCLUDE_FROM_ALL )
698 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
699 elseif(ARG_BUILDTREE_ONLY)
700 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
702 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
704 set(export_to_llvmexports)
705 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
706 (in_llvm_libs AND "llvm-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS) OR
707 NOT LLVM_DISTRIBUTION_COMPONENTS)
708 set(export_to_llvmexports EXPORT LLVMExports)
709 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
712 install(TARGETS ${name}
713 ${export_to_llvmexports}
714 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
715 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
716 RUNTIME DESTINATION bin COMPONENT ${name})
718 if (NOT LLVM_ENABLE_IDE)
719 add_llvm_install_targets(install-${name}
724 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
727 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
729 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
731 endmacro(add_llvm_library name)
733 macro(add_llvm_executable name)
734 cmake_parse_arguments(ARG
735 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH"
736 "ENTITLEMENTS;BUNDLE_PATH"
740 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
742 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
745 if(LLVM_ENABLE_OBJLIB)
746 # Generate an obj library for both targets.
747 set(obj_name "obj.${name}")
748 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
751 llvm_update_compile_flags(${obj_name})
752 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
754 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
757 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
760 # Note: the dummy.cpp source file provides no definitions. However,
761 # it forces Xcode to properly link the static library.
762 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
765 if( EXCLUDE_FROM_ALL )
766 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
768 add_executable(${name} ${ALL_FILES})
771 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
773 if(NOT ARG_NO_INSTALL_RPATH)
774 llvm_setup_rpath(${name})
777 if(DEFINED windows_resource_file)
778 set_windows_version_resource_properties(${name} ${windows_resource_file})
781 # $<TARGET_OBJECTS> doesn't require compile flags.
782 if(NOT LLVM_ENABLE_OBJLIB)
783 llvm_update_compile_flags(${name})
785 add_link_opts( ${name} )
787 # Do not add -Dname_EXPORTS to the command-line when building files in this
788 # target. Doing so is actively harmful for the modules build because it
789 # creates extra module variants, and not useful because we don't use these
791 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
793 if (LLVM_EXPORTED_SYMBOL_FILE)
794 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
795 endif(LLVM_EXPORTED_SYMBOL_FILE)
797 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
798 set(USE_SHARED USE_SHARED)
801 set(EXCLUDE_FROM_ALL OFF)
802 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
803 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
804 if( LLVM_COMMON_DEPENDS )
805 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
806 endif( LLVM_COMMON_DEPENDS )
808 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
809 llvm_externalize_debuginfo(${name})
811 if (LLVM_PTHREAD_LIB)
812 # libpthreads overrides some standard library symbols, so main
813 # executable must be linked with it in order to provide consistent
814 # API for all shared libaries loaded by this executable.
815 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
818 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
819 endmacro(add_llvm_executable name)
821 function(export_executable_symbols target)
822 if (LLVM_EXPORTED_SYMBOL_FILE)
823 # The symbol file should contain the symbols we want the executable to
825 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
826 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
827 # Extract the symbols to export from the static libraries that the
828 # executable links against.
829 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
830 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
831 # We need to consider not just the direct link dependencies, but also the
832 # transitive link dependencies. Do this by starting with the set of direct
833 # dependencies, then the dependencies of those dependencies, and so on.
834 get_target_property(new_libs ${target} LINK_LIBRARIES)
835 set(link_libs ${new_libs})
836 while(NOT "${new_libs}" STREQUAL "")
837 foreach(lib ${new_libs})
839 get_target_property(lib_type ${lib} TYPE)
840 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
841 list(APPEND static_libs ${lib})
843 list(APPEND other_libs ${lib})
845 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
846 foreach(transitive_lib ${transitive_libs})
847 list(FIND link_libs ${transitive_lib} idx)
848 if(TARGET ${transitive_lib} AND idx EQUAL -1)
849 list(APPEND newer_libs ${transitive_lib})
850 list(APPEND link_libs ${transitive_lib})
852 endforeach(transitive_lib)
855 set(new_libs ${newer_libs})
859 set(mangling microsoft)
861 set(mangling itanium)
863 add_custom_command(OUTPUT ${exported_symbol_file}
864 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
865 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
866 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
868 COMMENT "Generating export list for ${target}")
869 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
870 # If something links against this executable then we want a
871 # transitive link against only the libraries whose symbols
872 # we aren't exporting.
873 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
874 # The default import library suffix that cmake uses for cygwin/mingw is
875 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
876 # where the import libraries of both get named libclang.dll.a. Use a suffix
877 # of ".exe.a" to avoid this.
879 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
881 elseif(NOT (WIN32 OR CYGWIN))
882 # On Windows auto-exporting everything doesn't work because of the limit on
883 # the size of the exported symbol table, but on other platforms we can do
884 # it without any trouble.
885 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
887 set_property(TARGET ${target} APPEND_STRING PROPERTY
888 LINK_FLAGS " -rdynamic")
893 if(NOT LLVM_TOOLCHAIN_TOOLS)
894 set (LLVM_TOOLCHAIN_TOOLS
907 macro(add_llvm_tool name)
908 if( NOT LLVM_BUILD_TOOLS )
909 set(EXCLUDE_FROM_ALL ON)
911 add_llvm_executable(${name} ${ARGN})
913 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
914 if( LLVM_BUILD_TOOLS )
915 set(export_to_llvmexports)
916 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
917 NOT LLVM_DISTRIBUTION_COMPONENTS)
918 set(export_to_llvmexports EXPORT LLVMExports)
919 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
922 install(TARGETS ${name}
923 ${export_to_llvmexports}
924 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
927 if (NOT LLVM_ENABLE_IDE)
928 add_llvm_install_targets(install-${name}
934 if( LLVM_BUILD_TOOLS )
935 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
937 set_target_properties(${name} PROPERTIES FOLDER "Tools")
938 endmacro(add_llvm_tool name)
941 macro(add_llvm_example name)
942 if( NOT LLVM_BUILD_EXAMPLES )
943 set(EXCLUDE_FROM_ALL ON)
945 add_llvm_executable(${name} ${ARGN})
946 if( LLVM_BUILD_EXAMPLES )
947 install(TARGETS ${name} RUNTIME DESTINATION examples)
949 set_target_properties(${name} PROPERTIES FOLDER "Examples")
950 endmacro(add_llvm_example name)
952 # This is a macro that is used to create targets for executables that are needed
953 # for development, but that are not intended to be installed by default.
954 macro(add_llvm_utility name)
955 if ( NOT LLVM_BUILD_UTILS )
956 set(EXCLUDE_FROM_ALL ON)
959 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
960 set_target_properties(${name} PROPERTIES FOLDER "Utils")
961 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
962 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
963 set(export_to_llvmexports)
964 if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
965 NOT LLVM_DISTRIBUTION_COMPONENTS)
966 set(export_to_llvmexports EXPORT LLVMExports)
967 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
970 install(TARGETS ${name}
971 ${export_to_llvmexports}
972 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
975 if (NOT LLVM_ENABLE_IDE)
976 add_llvm_install_targets(install-${name}
980 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
981 elseif(LLVM_BUILD_UTILS)
982 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
985 endmacro(add_llvm_utility name)
987 macro(add_llvm_fuzzer name)
988 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
989 if( LLVM_LIB_FUZZING_ENGINE )
990 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
991 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
992 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
993 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
994 elseif( LLVM_USE_SANITIZE_COVERAGE )
995 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
996 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
997 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
998 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
999 elseif( ARG_DUMMY_MAIN )
1000 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
1001 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
1005 macro(add_llvm_target target_name)
1006 include_directories(BEFORE
1007 ${CMAKE_CURRENT_BINARY_DIR}
1008 ${CMAKE_CURRENT_SOURCE_DIR})
1009 add_llvm_library(LLVM${target_name} ${ARGN})
1010 set( CURRENT_LLVM_TARGET LLVM${target_name} )
1011 endmacro(add_llvm_target)
1013 function(canonicalize_tool_name name output)
1014 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1015 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1016 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1017 set(${output} "${nameUPPER}" PARENT_SCOPE)
1018 endfunction(canonicalize_tool_name)
1020 # Custom add_subdirectory wrapper
1021 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1022 # path if it differs from the name.
1023 function(add_llvm_subdirectory project type name)
1024 set(add_llvm_external_dir "${ARGN}")
1025 if("${add_llvm_external_dir}" STREQUAL "")
1026 set(add_llvm_external_dir ${name})
1028 canonicalize_tool_name(${name} nameUPPER)
1029 set(canonical_full_name ${project}_${type}_${nameUPPER})
1030 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1031 if(already_processed)
1034 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1036 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1037 # Treat it as in-tree subproject.
1038 option(${canonical_full_name}_BUILD
1039 "Whether to build ${name} as part of ${project}" On)
1040 mark_as_advanced(${project}_${type}_${name}_BUILD)
1041 if(${canonical_full_name}_BUILD)
1042 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
1045 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1046 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1047 CACHE PATH "Path to ${name} source directory")
1048 set(${canonical_full_name}_BUILD_DEFAULT ON)
1049 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1050 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1052 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
1053 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1055 option(${canonical_full_name}_BUILD
1056 "Whether to build ${name} as part of LLVM"
1057 ${${canonical_full_name}_BUILD_DEFAULT})
1058 if (${canonical_full_name}_BUILD)
1059 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1060 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1061 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1062 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1068 # Add external project that may want to be built as part of llvm such as Clang,
1069 # lld, and Polly. This adds two options. One for the source directory of the
1070 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1071 # enable or disable building it with everything else.
1072 # Additional parameter can be specified as the name of directory.
1073 macro(add_llvm_external_project name)
1074 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1077 macro(add_llvm_tool_subdirectory name)
1078 add_llvm_external_project(${name})
1079 endmacro(add_llvm_tool_subdirectory)
1081 function(get_project_name_from_src_var var output)
1082 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1083 MACHED_TOOL "${var}")
1085 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1087 set(${output} PARENT_SCOPE)
1091 function(create_subdirectory_options project type)
1092 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1093 foreach(dir ${sub-dirs})
1094 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1095 canonicalize_tool_name(${dir} name)
1096 option(${project}_${type}_${name}_BUILD
1097 "Whether to build ${name} as part of ${project}" On)
1098 mark_as_advanced(${project}_${type}_${name}_BUILD)
1101 endfunction(create_subdirectory_options)
1103 function(create_llvm_tool_options)
1104 create_subdirectory_options(LLVM TOOL)
1105 endfunction(create_llvm_tool_options)
1107 function(llvm_add_implicit_projects project)
1108 set(list_of_implicit_subdirs "")
1109 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1110 foreach(dir ${sub-dirs})
1111 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1112 canonicalize_tool_name(${dir} name)
1113 if (${project}_TOOL_${name}_BUILD)
1114 get_filename_component(fn "${dir}" NAME)
1115 list(APPEND list_of_implicit_subdirs "${fn}")
1120 foreach(external_proj ${list_of_implicit_subdirs})
1121 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1123 endfunction(llvm_add_implicit_projects)
1125 function(add_llvm_implicit_projects)
1126 llvm_add_implicit_projects(LLVM)
1127 endfunction(add_llvm_implicit_projects)
1129 # Generic support for adding a unittest.
1130 function(add_unittest test_suite test_name)
1131 if( NOT LLVM_BUILD_TESTS )
1132 set(EXCLUDE_FROM_ALL ON)
1135 # Our current version of gtest does not properly recognize C++11 support
1136 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM
1137 # itself requires C++11, we can safely force it on unconditionally so that
1138 # we don't have to fight with the buggy gtest check.
1139 add_definitions(-DGTEST_LANG_CXX11=1)
1140 add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
1142 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1143 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
1144 if (NOT LLVM_ENABLE_THREADS)
1145 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1148 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1149 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1151 # Some parts of gtest rely on this GNU extension, don't warn on it.
1152 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1153 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1156 set(LLVM_REQUIRES_RTTI OFF)
1158 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1159 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1160 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1161 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1162 # libpthreads overrides some standard library symbols, so main
1163 # executable must be linked with it in order to provide consistent
1164 # API for all shared libaries loaded by this executable.
1165 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
1167 add_dependencies(${test_suite} ${test_name})
1168 get_target_property(test_suite_folder ${test_suite} FOLDER)
1169 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1170 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1174 # Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1176 function(add_unittest_with_input_files test_suite test_name)
1177 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1179 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1180 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1182 add_unittest(${test_suite} ${test_name} ${ARGN})
1185 # Generic support for adding a benchmark.
1186 function(add_benchmark benchmark_name)
1187 if( NOT LLVM_BUILD_BENCHMARKS )
1188 set(EXCLUDE_FROM_ALL ON)
1191 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1192 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1193 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1194 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1195 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1198 function(llvm_add_go_executable binary pkgpath)
1199 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1201 if(LLVM_BINDINGS MATCHES "go")
1202 # FIXME: This should depend only on the libraries Go needs.
1203 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1204 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1205 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1206 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1208 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1209 foreach(d ${include_dirs})
1210 set(cppflags "${cppflags} -I${d}")
1212 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1213 add_custom_command(OUTPUT ${binpath}
1214 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1215 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1216 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1217 ${llvmlibs} ${ARG_DEPENDS}
1218 COMMENT "Building Go executable ${binary}"
1221 add_custom_target(${binary} ALL DEPENDS ${binpath})
1223 add_custom_target(${binary} DEPENDS ${binpath})
1228 # This function canonicalize the CMake variables passed by names
1229 # from CMake boolean to 0/1 suitable for passing into Python or C++,
1231 function(llvm_canonicalize_cmake_booleans)
1232 foreach(var ${ARGN})
1234 set(${var} 1 PARENT_SCOPE)
1236 set(${var} 0 PARENT_SCOPE)
1239 endfunction(llvm_canonicalize_cmake_booleans)
1241 macro(set_llvm_build_mode)
1242 # Configuration-time: See Unit/lit.site.cfg.in
1243 if (CMAKE_CFG_INTDIR STREQUAL ".")
1244 set(LLVM_BUILD_MODE ".")
1246 set(LLVM_BUILD_MODE "%(build_mode)s")
1250 # This function provides an automatic way to 'configure'-like generate a file
1251 # based on a set of common and custom variables, specifically targeting the
1252 # variables needed for the 'lit.site.cfg' files. This function bundles the
1253 # common variables that any Lit instance is likely to need, and custom
1254 # variables can be passed in.
1255 function(configure_lit_site_cfg site_in site_out)
1256 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
1258 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1259 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1260 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1262 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1263 set(ARG_OUTPUT_MAPPING "${site_out}")
1266 foreach(c ${LLVM_TARGETS_TO_BUILD})
1267 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1269 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1271 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1273 set_llvm_build_mode()
1275 # They below might not be the build tree but provided binary tree.
1276 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1277 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1278 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1279 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1281 # SHLIBDIR points the build tree.
1282 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1284 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1285 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1286 # plugins. We may rename it.
1287 if(LLVM_ENABLE_PLUGINS)
1288 set(ENABLE_SHARED "1")
1290 set(ENABLE_SHARED "0")
1293 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1294 set(ENABLE_ASSERTIONS "1")
1296 set(ENABLE_ASSERTIONS "0")
1299 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1300 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1302 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1303 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1304 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1306 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
1308 # Override config_target_triple (and the env)
1309 if(LLVM_TARGET_TRIPLE_ENV)
1310 # This is expanded into the heading.
1311 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1313 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1314 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1317 # This is expanded to; config.target_triple = ""+config.target_triple+""
1318 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1321 configure_file(${site_in} ${site_out} @ONLY)
1322 if (EXISTS "${ARG_MAIN_CONFIG}")
1323 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
1324 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)
1325 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}")
1326 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP})
1330 function(dump_all_cmake_variables)
1331 get_cmake_property(_variableNames VARIABLES)
1332 foreach (_variableName ${_variableNames})
1333 message(STATUS "${_variableName}=${${_variableName}}")
1337 function(get_llvm_lit_path base_dir file_name)
1338 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1340 if (ARG_ALLOW_EXTERNAL)
1341 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1342 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1343 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1346 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1347 if (EXISTS ${LLVM_EXTERNAL_LIT})
1348 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1349 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1350 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1351 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1354 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1359 set(lit_file_name "llvm-lit")
1360 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
1361 # llvm-lit needs suffix.py for multiprocess to find a main module.
1362 set(lit_file_name "${lit_file_name}.py")
1364 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1366 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1367 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1368 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1371 # Allow individual projects to provide an override
1372 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1373 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1374 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1375 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1377 set(LLVM_LIT_BASE_DIR "")
1380 # Cache this so we don't have to do it again and have subsequent calls
1381 # potentially disagree on the value.
1382 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1383 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1386 # A raw function to create a lit target. This is used to implement the testuite
1387 # management functions.
1388 function(add_lit_target target comment)
1389 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1390 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1391 separate_arguments(LIT_ARGS)
1392 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1393 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1396 # Get the path to the lit to *run* tests with. This can be overriden by
1397 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1404 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1405 list(APPEND LIT_COMMAND ${LIT_ARGS})
1406 foreach(param ${ARG_PARAMS})
1407 list(APPEND LIT_COMMAND --param ${param})
1409 if (ARG_UNPARSED_ARGUMENTS)
1410 add_custom_target(${target}
1411 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1412 COMMENT "${comment}"
1416 add_custom_target(${target}
1417 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1418 message(STATUS "${target} does nothing.")
1422 add_dependencies(${target} ${ARG_DEPENDS})
1425 # Tests should be excluded from "Build Solution".
1426 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1429 # A function to add a set of lit test suites to be driven through 'check-*' targets.
1430 function(add_lit_testsuite target comment)
1431 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1433 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1434 if(NOT EXCLUDE_FROM_ALL)
1435 # Register the testsuites, params and depends for the global check rule.
1436 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1437 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1438 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1439 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1442 # Produce a specific suffixed check rule.
1443 add_lit_target(${target} ${comment}
1444 ${ARG_UNPARSED_ARGUMENTS}
1445 PARAMS ${ARG_PARAMS}
1446 DEPENDS ${ARG_DEPENDS}
1451 function(add_lit_testsuites project directory)
1452 if (NOT LLVM_ENABLE_IDE)
1453 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1455 # Search recursively for test directories by assuming anything not
1456 # in a directory called Inputs contains tests.
1457 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1458 foreach(lit_suite ${to_process})
1459 if(NOT IS_DIRECTORY ${lit_suite})
1462 string(FIND ${lit_suite} Inputs is_inputs)
1463 string(FIND ${lit_suite} Output is_output)
1464 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1468 # Create a check- target for the directory.
1469 string(REPLACE ${directory} "" name_slash ${lit_suite})
1471 string(REPLACE "/" "-" name_slash ${name_slash})
1472 string(REPLACE "\\" "-" name_dashes ${name_slash})
1473 string(TOLOWER "${project}${name_dashes}" name_var)
1474 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1476 PARAMS ${ARG_PARAMS}
1477 DEPENDS ${ARG_DEPENDS}
1485 function(llvm_install_library_symlink name dest type)
1486 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1487 foreach(path ${CMAKE_MODULE_PATH})
1488 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1489 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1494 set(component ${ARG_COMPONENT})
1496 set(component ${name})
1499 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1500 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1502 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1503 if(WIN32 AND "${type}" STREQUAL "SHARED")
1507 install(SCRIPT ${INSTALL_SYMLINK}
1508 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1509 COMPONENT ${component})
1511 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1512 add_llvm_install_targets(install-${name}
1513 DEPENDS ${name} ${dest} install-${dest}
1518 function(llvm_install_symlink name dest)
1519 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1520 foreach(path ${CMAKE_MODULE_PATH})
1521 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1522 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1528 set(component ${ARG_COMPONENT})
1530 if(ARG_ALWAYS_GENERATE)
1531 set(component ${dest})
1533 set(component ${name})
1537 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1538 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1540 install(SCRIPT ${INSTALL_SYMLINK}
1541 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1542 COMPONENT ${component})
1544 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1545 add_llvm_install_targets(install-${name}
1546 DEPENDS ${name} ${dest} install-${dest}
1551 function(add_llvm_tool_symlink link_name target)
1552 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1553 set(dest_binary "$<TARGET_FILE:${target}>")
1555 # This got a bit gross... For multi-configuration generators the target
1556 # properties return the resolved value of the string, not the build system
1557 # expression. To reconstruct the platform-agnostic path we have to do some
1558 # magic. First we grab one of the types, and a type-specific path. Then from
1559 # the type-specific path we find the last occurrence of the type in the path,
1560 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1562 if(NOT ARG_OUTPUT_DIR)
1563 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1564 # the same directory.
1566 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1568 if(CMAKE_CONFIGURATION_TYPES)
1569 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1570 string(TOUPPER ${first_type} first_type_upper)
1571 set(first_type_suffix _${first_type_upper})
1573 get_target_property(target_type ${target} TYPE)
1574 if(${target_type} STREQUAL "STATIC_LIBRARY")
1575 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1576 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1577 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1579 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1581 if(CMAKE_CONFIGURATION_TYPES)
1582 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1583 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1584 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1585 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1586 path_suffix ${path_suffix})
1587 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1592 set(LLVM_LINK_OR_COPY create_symlink)
1594 set(LLVM_LINK_OR_COPY copy)
1597 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1599 set(target_name ${link_name})
1600 if(TARGET ${link_name})
1601 set(target_name ${link_name}-link)
1605 if(ARG_ALWAYS_GENERATE)
1606 set_property(DIRECTORY APPEND PROPERTY
1607 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1608 add_custom_command(TARGET ${target} POST_BUILD
1609 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1611 add_custom_command(OUTPUT ${output_path}
1612 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1614 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1615 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1617 # Make sure both the link and target are toolchain tools
1618 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1619 set(TOOL_IS_TOOLCHAIN ON)
1622 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1623 llvm_install_symlink(${link_name} ${target})
1628 function(llvm_externalize_debuginfo name)
1629 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1633 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1636 set(CMAKE_STRIP xcrun strip)
1638 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>)
1640 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
1645 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
1646 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
1651 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
1653 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
1654 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
1656 set(output_path "-o=${output_name}")
1659 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1660 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1662 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1663 set_property(TARGET ${name} APPEND_STRING PROPERTY
1664 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1666 if(NOT CMAKE_DSYMUTIL)
1667 set(CMAKE_DSYMUTIL xcrun dsymutil)
1669 add_custom_command(TARGET ${name} POST_BUILD
1670 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
1674 add_custom_command(TARGET ${name} POST_BUILD
1675 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1676 ${strip_command} -R .gnu_debuglink
1677 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1682 # Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
1683 function(llvm_codesign name)
1684 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
1686 if(NOT LLVM_CODESIGNING_IDENTITY)
1690 if(CMAKE_GENERATOR STREQUAL "Xcode")
1691 set_target_properties(${name} PROPERTIES
1692 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
1694 if(DEFINED ARG_ENTITLEMENTS)
1695 set_target_properties(${name} PROPERTIES
1696 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
1699 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
1700 if(NOT CMAKE_CODESIGN)
1701 set(CMAKE_CODESIGN xcrun codesign)
1703 if(NOT CMAKE_CODESIGN_ALLOCATE)
1705 COMMAND xcrun -f codesign_allocate
1706 OUTPUT_STRIP_TRAILING_WHITESPACE
1707 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
1710 if(DEFINED ARG_ENTITLEMENTS)
1711 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
1714 if (NOT ARG_BUNDLE_PATH)
1715 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
1719 set(force_flag "-f")
1723 TARGET ${name} POST_BUILD
1724 COMMAND ${CMAKE_COMMAND} -E
1725 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
1726 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
1727 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
1732 function(llvm_setup_rpath name)
1733 if(CMAKE_INSTALL_RPATH)
1737 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1738 set(extra_libdir ${LLVM_LIBRARY_DIR})
1739 elseif(LLVM_BUILD_LIBRARY_DIR)
1740 set(extra_libdir ${LLVM_LIBRARY_DIR})
1744 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1745 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1747 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1748 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1749 set_property(TARGET ${name} APPEND_STRING PROPERTY
1750 LINK_FLAGS " -Wl,-z,origin ")
1752 if(LLVM_LINKER_IS_GNULD)
1753 # $ORIGIN is not interpreted at link time by ld.bfd
1754 set_property(TARGET ${name} APPEND_STRING PROPERTY
1755 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1761 set_target_properties(${name} PROPERTIES
1762 BUILD_WITH_INSTALL_RPATH On
1763 INSTALL_RPATH "${_install_rpath}"
1764 ${_install_name_dir})
1767 function(setup_dependency_debugging name)
1768 if(NOT LLVM_DEPENDENCY_DEBUGGING)
1772 if("intrinsics_gen" IN_LIST ARGN)
1776 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
1777 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
1779 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
1780 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
1783 function(find_first_existing_vc_file path out_var)
1784 if(NOT EXISTS "${path}")
1787 if(EXISTS "${path}/.svn")
1789 "${path}/.svn/wc.db" # SVN 1.7
1790 "${path}/.svn/entries" # SVN 1.6
1792 foreach(file IN LISTS svn_files)
1793 if(EXISTS "${file}")
1794 set(${out_var} "${file}" PARENT_SCOPE)
1801 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
1802 WORKING_DIRECTORY ${path}
1803 RESULT_VARIABLE git_result
1804 OUTPUT_VARIABLE git_output
1806 if(git_result EQUAL 0)
1807 string(STRIP "${git_output}" git_output)
1808 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
1809 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
1810 if (NOT EXISTS "${git_dir}/logs/HEAD")
1811 file(WRITE "${git_dir}/logs/HEAD" "")
1813 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)