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-")
34 # LLVM_REQUIRES_RTTI is an internal flag that individual
35 # targets can use to force RTTI
36 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
37 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
38 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
39 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
40 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
41 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
43 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
46 list(APPEND LLVM_COMPILE_FLAGS "/GR")
50 # - LLVM_COMPILE_FLAGS is list.
51 # - PROPERTY COMPILE_FLAGS is string.
52 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
55 foreach(fn ${sources})
56 get_filename_component(suf ${fn} EXT)
57 if("${suf}" STREQUAL ".cpp")
58 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
59 COMPILE_FLAGS "${target_compile_flags}")
63 # Update target props, since all sources are C++.
64 set_property(TARGET ${name} APPEND_STRING PROPERTY
65 COMPILE_FLAGS "${target_compile_flags}")
68 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
71 function(add_llvm_symbol_exports target_name export_file)
72 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
73 set(native_export_file "${target_name}.exports")
74 add_custom_command(OUTPUT ${native_export_file}
75 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
76 DEPENDS ${export_file}
78 COMMENT "Creating export file for ${target_name}")
79 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
80 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
81 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
82 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
83 LINK_FLAGS " -Wl,-bE:${export_file}")
84 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
85 # Gold and BFD ld require a version script rather than a plain list.
86 set(native_export_file "${target_name}.exports")
87 # FIXME: Don't write the "local:" line on OpenBSD.
88 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
89 add_custom_command(OUTPUT ${native_export_file}
90 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file}
91 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
92 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
93 COMMAND echo " local: *;" >> ${native_export_file}
94 COMMAND echo "};" >> ${native_export_file}
95 DEPENDS ${export_file}
97 COMMENT "Creating export file for ${target_name}")
98 if (${LLVM_LINKER_IS_SOLARISLD})
99 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
100 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
102 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
103 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
106 set(native_export_file "${target_name}.def")
108 add_custom_command(OUTPUT ${native_export_file}
109 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
110 < ${export_file} > ${native_export_file}
111 DEPENDS ${export_file}
113 COMMENT "Creating export file for ${target_name}")
114 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
116 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
118 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
119 LINK_FLAGS " ${export_file_linker_flag}")
122 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
123 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
125 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
127 get_filename_component(extension ${src} EXT)
128 if(extension STREQUAL ".cpp")
129 set(first_source_file ${src})
134 # Force re-linking when the exports file changes. Actually, it
135 # forces recompilation of the source file. The LINK_DEPENDS target
136 # property only works for makefile-based generators.
137 # FIXME: This is not safe because this will create the same target
138 # ${native_export_file} in several different file:
139 # - One where we emitted ${target_name}_exports
140 # - One where we emitted the build command for the following object.
141 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
142 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
144 set_property(DIRECTORY APPEND
145 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
147 add_dependencies(${target_name} ${target_name}_exports)
149 # Add dependency to *_exports later -- CMake issue 14747
150 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
151 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
152 endfunction(add_llvm_symbol_exports)
156 COMMAND "${CMAKE_LINKER}" -v
157 ERROR_VARIABLE stderr
159 set(LLVM_LINKER_DETECTED YES)
160 if("${stderr}" MATCHES "PROJECT:ld64")
161 set(LLVM_LINKER_IS_LD64 YES)
162 message(STATUS "Linker detection: ld64")
164 set(LLVM_LINKER_DETECTED NO)
165 message(STATUS "Linker detection: unknown")
168 # Detect what linker we have here
169 if( LLVM_USE_LINKER )
170 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
172 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
173 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version)
177 OUTPUT_VARIABLE stdout
178 ERROR_VARIABLE stderr
180 set(LLVM_LINKER_DETECTED YES)
181 if("${stdout}" MATCHES "GNU gold")
182 set(LLVM_LINKER_IS_GOLD YES)
183 message(STATUS "Linker detection: GNU Gold")
184 elseif("${stdout}" MATCHES "^LLD")
185 set(LLVM_LINKER_IS_LLD YES)
186 message(STATUS "Linker detection: LLD")
187 elseif("${stdout}" MATCHES "GNU ld")
188 set(LLVM_LINKER_IS_GNULD YES)
189 message(STATUS "Linker detection: GNU ld")
190 elseif("${stderr}" MATCHES "Solaris Link Editors" OR
191 "${stdout}" MATCHES "Solaris Link Editors")
192 set(LLVM_LINKER_IS_SOLARISLD YES)
193 message(STATUS "Linker detection: Solaris ld")
195 set(LLVM_LINKER_DETECTED NO)
196 message(STATUS "Linker detection: unknown")
200 function(add_link_opts target_name)
201 # Don't use linker optimizations in debug builds since it slows down the
202 # linker in a context where the optimizations are not important.
203 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
205 # Pass -O3 to the linker. This enabled different optimizations on different
207 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
208 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
209 LINK_FLAGS " -Wl,-O3")
212 if(LLVM_LINKER_IS_GOLD)
213 # With gold gc-sections is always safe.
214 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
215 LINK_FLAGS " -Wl,--gc-sections")
216 # Note that there is a bug with -Wl,--icf=safe so it is not safe
217 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
220 if(NOT LLVM_NO_DEAD_STRIP)
221 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
222 # ld64's implementation of -dead_strip breaks tools that use plugins.
223 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
224 LINK_FLAGS " -Wl,-dead_strip")
225 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
226 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
227 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
228 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD AND
229 NOT ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD|AIX")
230 # Object files are compiled with -ffunction-data-sections.
231 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
232 # tools that use plugins. Always pass --gc-sections once we require
234 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
235 LINK_FLAGS " -Wl,--gc-sections")
237 else() #LLVM_NO_DEAD_STRIP
238 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
239 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
240 LINK_FLAGS " -Wl,-bnogc")
244 endfunction(add_link_opts)
246 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
247 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
248 # or a certain builder, for eaxample, msbuild.exe, would be confused.
249 function(set_output_directory target)
250 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
252 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
253 # It affects output of add_library(MODULE).
256 set(module_dir ${ARG_BINARY_DIR})
258 set(module_dir ${ARG_LIBRARY_DIR})
260 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
261 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
262 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
264 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
265 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
268 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
269 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
272 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
273 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
278 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
281 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
284 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
289 # If on Windows and building with MSVC, add the resource script containing the
290 # VERSIONINFO data to the project. This embeds version resource information
291 # into the output .exe or .dll.
292 # TODO: Enable for MinGW Windows builds too.
294 function(add_windows_version_resource_file OUT_VAR)
296 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
297 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
298 if(EXISTS ${resource_file})
299 set(sources ${sources} ${resource_file})
300 source_group("Resource Files" ${resource_file})
301 set(windows_resource_file ${resource_file} PARENT_SCOPE)
303 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
305 set(${OUT_VAR} ${sources} PARENT_SCOPE)
306 endfunction(add_windows_version_resource_file)
308 # set_windows_version_resource_properties(name resource_file...
310 # Optional major version number (defaults to LLVM_VERSION_MAJOR)
312 # Optional minor version number (defaults to LLVM_VERSION_MINOR)
313 # VERSION_PATCHLEVEL int
314 # Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
316 # Optional version string (defaults to PACKAGE_VERSION)
318 # Optional product name string (defaults to "LLVM")
320 function(set_windows_version_resource_properties name resource_file)
321 cmake_parse_arguments(ARG
323 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
327 if (NOT DEFINED ARG_VERSION_MAJOR)
328 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
331 if (NOT DEFINED ARG_VERSION_MINOR)
332 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
335 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
336 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
339 if (NOT DEFINED ARG_VERSION_STRING)
340 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
343 if (NOT DEFINED ARG_PRODUCT_NAME)
344 set(ARG_PRODUCT_NAME "LLVM")
347 set_property(SOURCE ${resource_file}
348 PROPERTY COMPILE_FLAGS /nologo)
349 set_property(SOURCE ${resource_file}
350 PROPERTY COMPILE_DEFINITIONS
351 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
352 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
353 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
354 "RC_VERSION_FIELD_4=0"
355 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
356 "RC_INTERNAL_NAME=\"${name}\""
357 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
358 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
359 endfunction(set_windows_version_resource_properties)
361 # llvm_add_library(name sources...
363 # STATIC by default w/o BUILD_SHARED_LIBS.
364 # SHARED by default w/ BUILD_SHARED_LIBS.
366 # Also create an OBJECT library target. Default if STATIC && SHARED.
368 # Target ${name} might not be created on unsupported platforms.
369 # Check with "if(TARGET ${name})".
370 # DISABLE_LLVM_LINK_LLVM_DYLIB
371 # Do not link this library to libLLVM, even if
372 # LLVM_LINK_LLVM_DYLIB is enabled.
374 # Corresponds to OUTPUT_NAME in target properties.
376 # Same semantics as add_dependencies().
377 # LINK_COMPONENTS components...
378 # Same as the variable LLVM_LINK_COMPONENTS.
379 # LINK_LIBS lib_targets...
380 # Same semantics as target_link_libraries().
382 # May specify header files for IDE generators.
384 # Should set SONAME link flags and create symlinks
386 # Suppress default RPATH settings in shared libraries.
388 # The tool (i.e. cmake target) that this plugin will link against
390 function(llvm_add_library name)
391 cmake_parse_arguments(ARG
392 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH"
393 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
394 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
396 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
397 if(ARG_ADDITIONAL_HEADERS)
398 # Pass through ADDITIONAL_HEADERS.
399 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
402 set(ALL_FILES ${ARG_OBJLIBS})
404 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
408 if(ARG_SHARED OR ARG_STATIC)
409 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
411 # Plugins that link against a tool are allowed even when plugins in general are not
412 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
413 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
418 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
420 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
429 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
430 # Generate an obj library for both targets.
431 set(obj_name "obj.${name}")
432 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
435 llvm_update_compile_flags(${obj_name})
436 if(CMAKE_GENERATOR STREQUAL "Xcode")
437 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c)
438 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n")
439 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit")
441 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE})
443 # Do add_dependencies(obj) later due to CMake issue 14747.
444 list(APPEND objlibs ${obj_name})
446 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
449 if(ARG_SHARED AND ARG_STATIC)
451 set(name_static "${name}_static")
453 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
455 # DEPENDS has been appended to LLVM_COMMON_LIBS.
456 llvm_add_library(${name_static} STATIC
458 OBJLIBS ${ALL_FILES} # objlib
459 LINK_LIBS ${ARG_LINK_LIBS}
460 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
462 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
467 add_library(${name} MODULE ${ALL_FILES})
469 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
470 add_library(${name} SHARED ${ALL_FILES})
472 add_library(${name} STATIC ${ALL_FILES})
475 if(NOT ARG_NO_INSTALL_RPATH)
476 if(ARG_MODULE OR ARG_SHARED)
477 llvm_setup_rpath(${name})
481 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
483 if(DEFINED windows_resource_file)
484 set_windows_version_resource_properties(${name} ${windows_resource_file})
485 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
488 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
489 # $<TARGET_OBJECTS> doesn't require compile flags.
491 llvm_update_compile_flags(${name})
493 add_link_opts( ${name} )
495 set_target_properties(${name}
497 OUTPUT_NAME ${ARG_OUTPUT_NAME}
502 set_target_properties(${name} PROPERTIES
504 SUFFIX ${LLVM_PLUGIN_EXT}
510 set_target_properties(${name} PROPERTIES
515 # Set SOVERSION on shared libraries that lack explicit SONAME
516 # specifier, on *nix systems that are not Darwin.
517 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
518 set_target_properties(${name}
520 # Since 4.0.0, the ABI version is indicated by the major version
521 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
522 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
526 if(ARG_MODULE OR ARG_SHARED)
527 # Do not add -Dname_EXPORTS to the command-line when building files in this
528 # target. Doing so is actively harmful for the modules build because it
529 # creates extra module variants, and not useful because we don't use these
531 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
533 if (LLVM_EXPORTED_SYMBOL_FILE)
534 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
538 if(ARG_SHARED AND UNIX)
539 if(NOT APPLE AND ARG_SONAME)
540 get_target_property(output_name ${name} OUTPUT_NAME)
541 if(${output_name} STREQUAL "output_name-NOTFOUND")
542 set(output_name ${name})
544 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
545 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
546 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
547 llvm_install_library_symlink(${api_name} ${library_name} SHARED
550 llvm_install_library_symlink(${output_name} ${library_name} SHARED
556 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
557 # On DLL platforms symbols are imported from the tool by linking against it.
558 set(llvm_libs ${ARG_PLUGIN_TOOL})
559 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
560 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
563 llvm_map_components_to_libnames(llvm_libs
564 ${ARG_LINK_COMPONENTS}
565 ${LLVM_LINK_COMPONENTS}
569 # Components have not been defined explicitly in CMake, so add the
570 # dependency information for this library as defined by LLVMBuild.
572 # It would be nice to verify that we have the dependencies for this library
573 # name, but using get_property(... SET) doesn't suffice to determine if a
574 # property has been set to an empty value.
575 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
579 set(libtype INTERFACE)
581 # We can use PRIVATE since SO knows its dependent libs.
585 target_link_libraries(${name} ${libtype}
591 if(LLVM_COMMON_DEPENDS)
592 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
593 # Add dependencies also to objlibs.
594 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
595 foreach(objlib ${objlibs})
596 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
600 if(ARG_SHARED OR ARG_MODULE)
601 llvm_externalize_debuginfo(${name})
602 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
604 # clang and newer versions of ninja use high-resolutions timestamps,
605 # but older versions of libtool on Darwin don't, so the archive will
606 # often get an older timestamp than the last object that was added
607 # or updated. To fix this, we add a custom command to touch archive
608 # after it's been built so that ninja won't rebuild it unnecessarily
609 # the next time it's run.
610 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES)
611 add_custom_command(TARGET ${name}
613 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}
618 function(add_llvm_install_targets target)
619 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX" "DEPENDS" ${ARGN})
621 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}")
624 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}")
627 set(file_dependencies)
628 set(target_dependencies)
629 foreach(dependency ${ARG_DEPENDS})
630 if(TARGET ${dependency})
631 list(APPEND target_dependencies ${dependency})
633 list(APPEND file_dependencies ${dependency})
637 add_custom_target(${target}
638 DEPENDS ${file_dependencies}
639 COMMAND "${CMAKE_COMMAND}"
642 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
644 add_custom_target(${target}-stripped
645 DEPENDS ${file_dependencies}
646 COMMAND "${CMAKE_COMMAND}"
649 -DCMAKE_INSTALL_DO_STRIP=1
650 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
652 if(target_dependencies)
653 add_dependencies(${target} ${target_dependencies})
654 add_dependencies(${target}-stripped ${target_dependencies})
658 macro(add_llvm_library name)
659 cmake_parse_arguments(ARG
660 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN"
665 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
666 elseif( BUILD_SHARED_LIBS OR ARG_SHARED )
667 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
669 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
672 # Libraries that are meant to only be exposed via the build tree only are
673 # never installed and are only exported as a target in the special build tree
675 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE)
676 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
677 set(in_llvm_libs YES)
680 if (ARG_MODULE AND NOT TARGET ${name})
681 # Add empty "phony" target
682 add_custom_target(${name})
683 elseif( EXCLUDE_FROM_ALL )
684 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
685 elseif(ARG_BUILDTREE_ONLY)
686 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
688 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
690 set(export_to_llvmexports)
691 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
692 (in_llvm_libs AND "llvm-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS) OR
693 NOT LLVM_DISTRIBUTION_COMPONENTS)
694 set(export_to_llvmexports EXPORT LLVMExports)
695 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
698 install(TARGETS ${name}
699 ${export_to_llvmexports}
700 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
701 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
702 RUNTIME DESTINATION bin COMPONENT ${name})
704 if (NOT LLVM_ENABLE_IDE)
705 add_llvm_install_targets(install-${name}
710 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
713 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
715 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
717 endmacro(add_llvm_library name)
719 macro(add_llvm_executable name)
720 cmake_parse_arguments(ARG
721 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH"
722 "ENTITLEMENTS;BUNDLE_PATH"
726 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
728 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
731 if(LLVM_ENABLE_OBJLIB)
732 # Generate an obj library for both targets.
733 set(obj_name "obj.${name}")
734 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
737 llvm_update_compile_flags(${obj_name})
738 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
740 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
743 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
746 # Note: the dummy.cpp source file provides no definitions. However,
747 # it forces Xcode to properly link the static library.
748 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
751 if( EXCLUDE_FROM_ALL )
752 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
754 add_executable(${name} ${ALL_FILES})
757 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
759 if(NOT ARG_NO_INSTALL_RPATH)
760 llvm_setup_rpath(${name})
763 if(DEFINED windows_resource_file)
764 set_windows_version_resource_properties(${name} ${windows_resource_file})
767 # $<TARGET_OBJECTS> doesn't require compile flags.
768 if(NOT LLVM_ENABLE_OBJLIB)
769 llvm_update_compile_flags(${name})
771 add_link_opts( ${name} )
773 # Do not add -Dname_EXPORTS to the command-line when building files in this
774 # target. Doing so is actively harmful for the modules build because it
775 # creates extra module variants, and not useful because we don't use these
777 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
779 if (LLVM_EXPORTED_SYMBOL_FILE)
780 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
781 endif(LLVM_EXPORTED_SYMBOL_FILE)
783 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
784 set(USE_SHARED USE_SHARED)
787 set(EXCLUDE_FROM_ALL OFF)
788 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
789 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
790 if( LLVM_COMMON_DEPENDS )
791 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
792 endif( LLVM_COMMON_DEPENDS )
794 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
795 llvm_externalize_debuginfo(${name})
797 if (LLVM_PTHREAD_LIB)
798 # libpthreads overrides some standard library symbols, so main
799 # executable must be linked with it in order to provide consistent
800 # API for all shared libaries loaded by this executable.
801 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
804 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
805 endmacro(add_llvm_executable name)
807 function(export_executable_symbols target)
808 if (LLVM_EXPORTED_SYMBOL_FILE)
809 # The symbol file should contain the symbols we want the executable to
811 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
812 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
813 # Extract the symbols to export from the static libraries that the
814 # executable links against.
815 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
816 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
817 # We need to consider not just the direct link dependencies, but also the
818 # transitive link dependencies. Do this by starting with the set of direct
819 # dependencies, then the dependencies of those dependencies, and so on.
820 get_target_property(new_libs ${target} LINK_LIBRARIES)
821 set(link_libs ${new_libs})
822 while(NOT "${new_libs}" STREQUAL "")
823 foreach(lib ${new_libs})
825 get_target_property(lib_type ${lib} TYPE)
826 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
827 list(APPEND static_libs ${lib})
829 list(APPEND other_libs ${lib})
831 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
832 foreach(transitive_lib ${transitive_libs})
833 list(FIND link_libs ${transitive_lib} idx)
834 if(TARGET ${transitive_lib} AND idx EQUAL -1)
835 list(APPEND newer_libs ${transitive_lib})
836 list(APPEND link_libs ${transitive_lib})
838 endforeach(transitive_lib)
841 set(new_libs ${newer_libs})
845 set(mangling microsoft)
847 set(mangling itanium)
849 add_custom_command(OUTPUT ${exported_symbol_file}
850 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
851 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
852 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
854 COMMENT "Generating export list for ${target}")
855 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
856 # If something links against this executable then we want a
857 # transitive link against only the libraries whose symbols
858 # we aren't exporting.
859 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
860 # The default import library suffix that cmake uses for cygwin/mingw is
861 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
862 # where the import libraries of both get named libclang.dll.a. Use a suffix
863 # of ".exe.a" to avoid this.
865 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
867 elseif(NOT (WIN32 OR CYGWIN))
868 # On Windows auto-exporting everything doesn't work because of the limit on
869 # the size of the exported symbol table, but on other platforms we can do
870 # it without any trouble.
871 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
873 set_property(TARGET ${target} APPEND_STRING PROPERTY
874 LINK_FLAGS " -rdynamic")
879 if(NOT LLVM_TOOLCHAIN_TOOLS)
880 set (LLVM_TOOLCHAIN_TOOLS
893 macro(add_llvm_tool name)
894 if( NOT LLVM_BUILD_TOOLS )
895 set(EXCLUDE_FROM_ALL ON)
897 add_llvm_executable(${name} ${ARGN})
899 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
900 if( LLVM_BUILD_TOOLS )
901 set(export_to_llvmexports)
902 if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
903 NOT LLVM_DISTRIBUTION_COMPONENTS)
904 set(export_to_llvmexports EXPORT LLVMExports)
905 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
908 install(TARGETS ${name}
909 ${export_to_llvmexports}
910 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
913 if (NOT LLVM_ENABLE_IDE)
914 add_llvm_install_targets(install-${name}
920 if( LLVM_BUILD_TOOLS )
921 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
923 set_target_properties(${name} PROPERTIES FOLDER "Tools")
924 endmacro(add_llvm_tool name)
927 macro(add_llvm_example name)
928 if( NOT LLVM_BUILD_EXAMPLES )
929 set(EXCLUDE_FROM_ALL ON)
931 add_llvm_executable(${name} ${ARGN})
932 if( LLVM_BUILD_EXAMPLES )
933 install(TARGETS ${name} RUNTIME DESTINATION examples)
935 set_target_properties(${name} PROPERTIES FOLDER "Examples")
936 endmacro(add_llvm_example name)
938 # This is a macro that is used to create targets for executables that are needed
939 # for development, but that are not intended to be installed by default.
940 macro(add_llvm_utility name)
941 if ( NOT LLVM_BUILD_UTILS )
942 set(EXCLUDE_FROM_ALL ON)
945 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
946 set_target_properties(${name} PROPERTIES FOLDER "Utils")
947 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
948 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS)
949 set(export_to_llvmexports)
950 if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
951 NOT LLVM_DISTRIBUTION_COMPONENTS)
952 set(export_to_llvmexports EXPORT LLVMExports)
953 set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
956 install(TARGETS ${name}
957 ${export_to_llvmexports}
958 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR}
961 if (NOT LLVM_ENABLE_IDE)
962 add_llvm_install_targets(install-${name}
966 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
967 elseif(LLVM_BUILD_UTILS)
968 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
971 endmacro(add_llvm_utility name)
973 macro(add_llvm_fuzzer name)
974 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN})
975 if( LLVM_LIB_FUZZING_ENGINE )
976 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
977 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
978 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE})
979 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
980 elseif( LLVM_USE_SANITIZE_COVERAGE )
981 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
982 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN})
983 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
984 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
985 elseif( ARG_DUMMY_MAIN )
986 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS})
987 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers")
991 macro(add_llvm_target target_name)
992 include_directories(BEFORE
993 ${CMAKE_CURRENT_BINARY_DIR}
994 ${CMAKE_CURRENT_SOURCE_DIR})
995 add_llvm_library(LLVM${target_name} ${ARGN})
996 set( CURRENT_LLVM_TARGET LLVM${target_name} )
997 endmacro(add_llvm_target)
999 function(canonicalize_tool_name name output)
1000 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
1001 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
1002 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
1003 set(${output} "${nameUPPER}" PARENT_SCOPE)
1004 endfunction(canonicalize_tool_name)
1006 # Custom add_subdirectory wrapper
1007 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
1008 # path if it differs from the name.
1009 function(add_llvm_subdirectory project type name)
1010 set(add_llvm_external_dir "${ARGN}")
1011 if("${add_llvm_external_dir}" STREQUAL "")
1012 set(add_llvm_external_dir ${name})
1014 canonicalize_tool_name(${name} nameUPPER)
1015 set(canonical_full_name ${project}_${type}_${nameUPPER})
1016 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED)
1017 if(already_processed)
1020 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES)
1022 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
1023 # Treat it as in-tree subproject.
1024 option(${canonical_full_name}_BUILD
1025 "Whether to build ${name} as part of ${project}" On)
1026 mark_as_advanced(${project}_${type}_${name}_BUILD)
1027 if(${canonical_full_name}_BUILD)
1028 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
1031 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
1032 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
1033 CACHE PATH "Path to ${name} source directory")
1034 set(${canonical_full_name}_BUILD_DEFAULT ON)
1035 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1036 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1038 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
1039 set(${canonical_full_name}_BUILD_DEFAULT OFF)
1041 option(${canonical_full_name}_BUILD
1042 "Whether to build ${name} as part of LLVM"
1043 ${${canonical_full_name}_BUILD_DEFAULT})
1044 if (${canonical_full_name}_BUILD)
1045 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
1046 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
1047 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
1048 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
1054 # Add external project that may want to be built as part of llvm such as Clang,
1055 # lld, and Polly. This adds two options. One for the source directory of the
1056 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
1057 # enable or disable building it with everything else.
1058 # Additional parameter can be specified as the name of directory.
1059 macro(add_llvm_external_project name)
1060 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
1063 macro(add_llvm_tool_subdirectory name)
1064 add_llvm_external_project(${name})
1065 endmacro(add_llvm_tool_subdirectory)
1067 function(get_project_name_from_src_var var output)
1068 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
1069 MACHED_TOOL "${var}")
1071 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
1073 set(${output} PARENT_SCOPE)
1077 function(create_subdirectory_options project type)
1078 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1079 foreach(dir ${sub-dirs})
1080 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1081 canonicalize_tool_name(${dir} name)
1082 option(${project}_${type}_${name}_BUILD
1083 "Whether to build ${name} as part of ${project}" On)
1084 mark_as_advanced(${project}_${type}_${name}_BUILD)
1087 endfunction(create_subdirectory_options)
1089 function(create_llvm_tool_options)
1090 create_subdirectory_options(LLVM TOOL)
1091 endfunction(create_llvm_tool_options)
1093 function(llvm_add_implicit_projects project)
1094 set(list_of_implicit_subdirs "")
1095 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
1096 foreach(dir ${sub-dirs})
1097 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
1098 canonicalize_tool_name(${dir} name)
1099 if (${project}_TOOL_${name}_BUILD)
1100 get_filename_component(fn "${dir}" NAME)
1101 list(APPEND list_of_implicit_subdirs "${fn}")
1106 foreach(external_proj ${list_of_implicit_subdirs})
1107 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
1109 endfunction(llvm_add_implicit_projects)
1111 function(add_llvm_implicit_projects)
1112 llvm_add_implicit_projects(LLVM)
1113 endfunction(add_llvm_implicit_projects)
1115 # Generic support for adding a unittest.
1116 function(add_unittest test_suite test_name)
1117 if( NOT LLVM_BUILD_TESTS )
1118 set(EXCLUDE_FROM_ALL ON)
1121 # Our current version of gtest does not properly recognize C++11 support
1122 # with MSVC, so it falls back to tr1 / experimental classes. Since LLVM
1123 # itself requires C++11, we can safely force it on unconditionally so that
1124 # we don't have to fight with the buggy gtest check.
1125 add_definitions(-DGTEST_LANG_CXX11=1)
1126 add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
1128 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1129 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
1130 if (NOT LLVM_ENABLE_THREADS)
1131 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1134 if (SUPPORTS_VARIADIC_MACROS_FLAG)
1135 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1137 # Some parts of gtest rely on this GNU extension, don't warn on it.
1138 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1139 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1142 set(LLVM_REQUIRES_RTTI OFF)
1144 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1145 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1146 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1147 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1148 # libpthreads overrides some standard library symbols, so main
1149 # executable must be linked with it in order to provide consistent
1150 # API for all shared libaries loaded by this executable.
1151 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
1153 add_dependencies(${test_suite} ${test_name})
1154 get_target_property(test_suite_folder ${test_suite} FOLDER)
1155 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1156 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1160 # Use for test binaries that call llvm::getInputFileDirectory(). Use of this
1162 function(add_unittest_with_input_files test_suite test_name)
1163 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1165 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in
1166 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt)
1168 add_unittest(${test_suite} ${test_name} ${ARGN})
1171 # Generic support for adding a benchmark.
1172 function(add_benchmark benchmark_name)
1173 if( NOT LLVM_BUILD_BENCHMARKS )
1174 set(EXCLUDE_FROM_ALL ON)
1177 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1178 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1179 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1180 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils")
1181 target_link_libraries(${benchmark_name} PRIVATE benchmark)
1184 function(llvm_add_go_executable binary pkgpath)
1185 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1187 if(LLVM_BINDINGS MATCHES "go")
1188 # FIXME: This should depend only on the libraries Go needs.
1189 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1190 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1191 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1192 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1194 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1195 foreach(d ${include_dirs})
1196 set(cppflags "${cppflags} -I${d}")
1198 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1199 add_custom_command(OUTPUT ${binpath}
1200 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1201 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1202 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1203 ${llvmlibs} ${ARG_DEPENDS}
1204 COMMENT "Building Go executable ${binary}"
1207 add_custom_target(${binary} ALL DEPENDS ${binpath})
1209 add_custom_target(${binary} DEPENDS ${binpath})
1214 # This function canonicalize the CMake variables passed by names
1215 # from CMake boolean to 0/1 suitable for passing into Python or C++,
1217 function(llvm_canonicalize_cmake_booleans)
1218 foreach(var ${ARGN})
1220 set(${var} 1 PARENT_SCOPE)
1222 set(${var} 0 PARENT_SCOPE)
1225 endfunction(llvm_canonicalize_cmake_booleans)
1227 macro(set_llvm_build_mode)
1228 # Configuration-time: See Unit/lit.site.cfg.in
1229 if (CMAKE_CFG_INTDIR STREQUAL ".")
1230 set(LLVM_BUILD_MODE ".")
1232 set(LLVM_BUILD_MODE "%(build_mode)s")
1236 # This function provides an automatic way to 'configure'-like generate a file
1237 # based on a set of common and custom variables, specifically targeting the
1238 # variables needed for the 'lit.site.cfg' files. This function bundles the
1239 # common variables that any Lit instance is likely to need, and custom
1240 # variables can be passed in.
1241 function(configure_lit_site_cfg site_in site_out)
1242 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
1244 if ("${ARG_MAIN_CONFIG}" STREQUAL "")
1245 get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
1246 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
1248 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
1249 set(ARG_OUTPUT_MAPPING "${site_out}")
1252 foreach(c ${LLVM_TARGETS_TO_BUILD})
1253 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1255 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1257 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1259 set_llvm_build_mode()
1261 # They below might not be the build tree but provided binary tree.
1262 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1263 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1264 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
1265 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
1267 # SHLIBDIR points the build tree.
1268 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1270 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1271 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1272 # plugins. We may rename it.
1273 if(LLVM_ENABLE_PLUGINS)
1274 set(ENABLE_SHARED "1")
1276 set(ENABLE_SHARED "0")
1279 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1280 set(ENABLE_ASSERTIONS "1")
1282 set(ENABLE_ASSERTIONS "0")
1285 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1286 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1288 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1289 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1290 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1292 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
1294 # Override config_target_triple (and the env)
1295 if(LLVM_TARGET_TRIPLE_ENV)
1296 # This is expanded into the heading.
1297 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1299 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1300 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1303 # This is expanded to; config.target_triple = ""+config.target_triple+""
1304 set(TARGET_TRIPLE "\"+config.target_triple+\"")
1307 configure_file(${site_in} ${site_out} @ONLY)
1308 if (EXISTS "${ARG_MAIN_CONFIG}")
1309 set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
1310 get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)
1311 set(LLVM_LIT_CONFIG_MAP "${LLVM_LIT_CONFIG_MAP}\n${PYTHON_STATEMENT}")
1312 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP ${LLVM_LIT_CONFIG_MAP})
1316 function(dump_all_cmake_variables)
1317 get_cmake_property(_variableNames VARIABLES)
1318 foreach (_variableName ${_variableNames})
1319 message(STATUS "${_variableName}=${${_variableName}}")
1323 function(get_llvm_lit_path base_dir file_name)
1324 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN})
1326 if (ARG_ALLOW_EXTERNAL)
1327 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit")
1328 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "")
1329 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}")
1332 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "")
1333 if (EXISTS ${LLVM_EXTERNAL_LIT})
1334 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME)
1335 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY)
1336 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE)
1337 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE)
1340 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.")
1345 set(lit_file_name "llvm-lit")
1346 if (CMAKE_HOST_WIN32 AND NOT CYGWIN)
1347 # llvm-lit needs suffix.py for multiprocess to find a main module.
1348 set(lit_file_name "${lit_file_name}.py")
1350 set(${file_name} ${lit_file_name} PARENT_SCOPE)
1352 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR)
1353 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "")
1354 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1357 # Allow individual projects to provide an override
1358 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "")
1359 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR})
1360 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "")
1361 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
1363 set(LLVM_LIT_BASE_DIR "")
1366 # Cache this so we don't have to do it again and have subsequent calls
1367 # potentially disagree on the value.
1368 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR})
1369 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE)
1372 # A raw function to create a lit target. This is used to implement the testuite
1373 # management functions.
1374 function(add_lit_target target comment)
1375 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1376 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1377 separate_arguments(LIT_ARGS)
1378 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1379 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1382 # Get the path to the lit to *run* tests with. This can be overriden by
1383 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py>
1390 set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1391 list(APPEND LIT_COMMAND ${LIT_ARGS})
1392 foreach(param ${ARG_PARAMS})
1393 list(APPEND LIT_COMMAND --param ${param})
1395 if (ARG_UNPARSED_ARGUMENTS)
1396 add_custom_target(${target}
1397 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1398 COMMENT "${comment}"
1402 add_custom_target(${target}
1403 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1404 message(STATUS "${target} does nothing.")
1408 add_dependencies(${target} ${ARG_DEPENDS})
1411 # Tests should be excluded from "Build Solution".
1412 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1415 # A function to add a set of lit test suites to be driven through 'check-*' targets.
1416 function(add_lit_testsuite target comment)
1417 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1419 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1420 if(NOT EXCLUDE_FROM_ALL)
1421 # Register the testsuites, params and depends for the global check rule.
1422 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1423 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1424 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1425 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1428 # Produce a specific suffixed check rule.
1429 add_lit_target(${target} ${comment}
1430 ${ARG_UNPARSED_ARGUMENTS}
1431 PARAMS ${ARG_PARAMS}
1432 DEPENDS ${ARG_DEPENDS}
1437 function(add_lit_testsuites project directory)
1438 if (NOT LLVM_ENABLE_IDE)
1439 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1441 # Search recursively for test directories by assuming anything not
1442 # in a directory called Inputs contains tests.
1443 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1444 foreach(lit_suite ${to_process})
1445 if(NOT IS_DIRECTORY ${lit_suite})
1448 string(FIND ${lit_suite} Inputs is_inputs)
1449 string(FIND ${lit_suite} Output is_output)
1450 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1454 # Create a check- target for the directory.
1455 string(REPLACE ${directory} "" name_slash ${lit_suite})
1457 string(REPLACE "/" "-" name_slash ${name_slash})
1458 string(REPLACE "\\" "-" name_dashes ${name_slash})
1459 string(TOLOWER "${project}${name_dashes}" name_var)
1460 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1462 PARAMS ${ARG_PARAMS}
1463 DEPENDS ${ARG_DEPENDS}
1471 function(llvm_install_library_symlink name dest type)
1472 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1473 foreach(path ${CMAKE_MODULE_PATH})
1474 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1475 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1480 set(component ${ARG_COMPONENT})
1482 set(component ${name})
1485 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1486 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1488 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1489 if(WIN32 AND "${type}" STREQUAL "SHARED")
1493 install(SCRIPT ${INSTALL_SYMLINK}
1494 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1495 COMPONENT ${component})
1497 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1498 add_llvm_install_targets(install-${name}
1499 DEPENDS ${name} ${dest} install-${dest}
1504 function(llvm_install_symlink name dest)
1505 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1506 foreach(path ${CMAKE_MODULE_PATH})
1507 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1508 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1514 set(component ${ARG_COMPONENT})
1516 if(ARG_ALWAYS_GENERATE)
1517 set(component ${dest})
1519 set(component ${name})
1523 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1524 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1526 install(SCRIPT ${INSTALL_SYMLINK}
1527 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1528 COMPONENT ${component})
1530 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE)
1531 add_llvm_install_targets(install-${name}
1532 DEPENDS ${name} ${dest} install-${dest}
1537 function(add_llvm_tool_symlink link_name target)
1538 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1539 set(dest_binary "$<TARGET_FILE:${target}>")
1541 # This got a bit gross... For multi-configuration generators the target
1542 # properties return the resolved value of the string, not the build system
1543 # expression. To reconstruct the platform-agnostic path we have to do some
1544 # magic. First we grab one of the types, and a type-specific path. Then from
1545 # the type-specific path we find the last occurrence of the type in the path,
1546 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1548 if(NOT ARG_OUTPUT_DIR)
1549 # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1550 # the same directory.
1552 set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1554 if(CMAKE_CONFIGURATION_TYPES)
1555 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1556 string(TOUPPER ${first_type} first_type_upper)
1557 set(first_type_suffix _${first_type_upper})
1559 get_target_property(target_type ${target} TYPE)
1560 if(${target_type} STREQUAL "STATIC_LIBRARY")
1561 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1562 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1563 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1565 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1567 if(CMAKE_CONFIGURATION_TYPES)
1568 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1569 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1570 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1571 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1572 path_suffix ${path_suffix})
1573 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1578 set(LLVM_LINK_OR_COPY create_symlink)
1580 set(LLVM_LINK_OR_COPY copy)
1583 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1585 set(target_name ${link_name})
1586 if(TARGET ${link_name})
1587 set(target_name ${link_name}-link)
1591 if(ARG_ALWAYS_GENERATE)
1592 set_property(DIRECTORY APPEND PROPERTY
1593 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1594 add_custom_command(TARGET ${target} POST_BUILD
1595 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1597 add_custom_command(OUTPUT ${output_path}
1598 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1600 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1601 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1603 # Make sure both the link and target are toolchain tools
1604 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1605 set(TOOL_IS_TOOLCHAIN ON)
1608 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1609 llvm_install_symlink(${link_name} ${target})
1614 function(llvm_externalize_debuginfo name)
1615 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1619 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1622 set(CMAKE_STRIP xcrun strip)
1624 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>)
1626 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>)
1631 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION)
1632 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION})
1637 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}")
1639 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR)
1640 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}")
1642 set(output_path "-o=${output_name}")
1645 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1646 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1648 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1649 set_property(TARGET ${name} APPEND_STRING PROPERTY
1650 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1652 if(NOT CMAKE_DSYMUTIL)
1653 set(CMAKE_DSYMUTIL xcrun dsymutil)
1655 add_custom_command(TARGET ${name} POST_BUILD
1656 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}>
1660 add_custom_command(TARGET ${name} POST_BUILD
1661 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1662 ${strip_command} -R .gnu_debuglink
1663 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1668 # Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path])
1669 function(llvm_codesign name)
1670 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN})
1672 if(NOT LLVM_CODESIGNING_IDENTITY)
1676 if(CMAKE_GENERATOR STREQUAL "Xcode")
1677 set_target_properties(${name} PROPERTIES
1678 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY}
1680 if(DEFINED ARG_ENTITLEMENTS)
1681 set_target_properties(${name} PROPERTIES
1682 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS}
1685 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin)
1686 if(NOT CMAKE_CODESIGN)
1687 set(CMAKE_CODESIGN xcrun codesign)
1689 if(NOT CMAKE_CODESIGN_ALLOCATE)
1691 COMMAND xcrun -f codesign_allocate
1692 OUTPUT_STRIP_TRAILING_WHITESPACE
1693 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
1696 if(DEFINED ARG_ENTITLEMENTS)
1697 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS})
1700 if (NOT ARG_BUNDLE_PATH)
1701 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>)
1705 set(force_flag "-f")
1709 TARGET ${name} POST_BUILD
1710 COMMAND ${CMAKE_COMMAND} -E
1711 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
1712 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY}
1713 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH}
1718 function(llvm_setup_rpath name)
1719 if(CMAKE_INSTALL_RPATH)
1723 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1724 set(extra_libdir ${LLVM_LIBRARY_DIR})
1725 elseif(LLVM_BUILD_LIBRARY_DIR)
1726 set(extra_libdir ${LLVM_LIBRARY_DIR})
1730 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1731 set(_install_rpath "@loader_path/../lib" ${extra_libdir})
1733 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1734 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1735 set_property(TARGET ${name} APPEND_STRING PROPERTY
1736 LINK_FLAGS " -Wl,-z,origin ")
1738 if(LLVM_LINKER_IS_GNULD)
1739 # $ORIGIN is not interpreted at link time by ld.bfd
1740 set_property(TARGET ${name} APPEND_STRING PROPERTY
1741 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1747 set_target_properties(${name} PROPERTIES
1748 BUILD_WITH_INSTALL_RPATH On
1749 INSTALL_RPATH "${_install_rpath}"
1750 ${_install_name_dir})
1753 function(setup_dependency_debugging name)
1754 if(NOT LLVM_DEPENDENCY_DEBUGGING)
1758 if("intrinsics_gen" IN_LIST ARGN)
1762 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))")
1763 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))")
1765 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'")
1766 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
1769 function(find_first_existing_vc_file path out_var)
1770 if(NOT EXISTS "${path}")
1773 if(EXISTS "${path}/.svn")
1775 "${path}/.svn/wc.db" # SVN 1.7
1776 "${path}/.svn/entries" # SVN 1.6
1778 foreach(file IN LISTS svn_files)
1779 if(EXISTS "${file}")
1780 set(${out_var} "${file}" PARENT_SCOPE)
1787 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
1788 WORKING_DIRECTORY ${path}
1789 RESULT_VARIABLE git_result
1790 OUTPUT_VARIABLE git_output
1792 if(git_result EQUAL 0)
1793 string(STRIP "${git_output}" git_output)
1794 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
1795 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD
1796 if (NOT EXISTS "${git_dir}/logs/HEAD")
1797 file(WRITE "${git_dir}/logs/HEAD" "")
1799 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE)