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")
25 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
26 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
30 # LLVM_REQUIRES_RTTI is an internal flag that individual
31 # targets can use to force RTTI
32 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
33 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
34 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
35 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
36 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
37 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
39 list(APPEND LLVM_COMPILE_FLAGS "/GR-")
42 list(APPEND LLVM_COMPILE_FLAGS "/GR")
46 # - LLVM_COMPILE_FLAGS is list.
47 # - PROPERTY COMPILE_FLAGS is string.
48 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
51 foreach(fn ${sources})
52 get_filename_component(suf ${fn} EXT)
53 if("${suf}" STREQUAL ".cpp")
54 set_property(SOURCE ${fn} APPEND_STRING PROPERTY
55 COMPILE_FLAGS "${target_compile_flags}")
59 # Update target props, since all sources are C++.
60 set_property(TARGET ${name} APPEND_STRING PROPERTY
61 COMPILE_FLAGS "${target_compile_flags}")
64 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
67 function(add_llvm_symbol_exports target_name export_file)
68 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
69 set(native_export_file "${target_name}.exports")
70 add_custom_command(OUTPUT ${native_export_file}
71 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
72 DEPENDS ${export_file}
74 COMMENT "Creating export file for ${target_name}")
75 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
76 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
77 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
78 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
79 LINK_FLAGS " -Wl,-bE:${export_file}")
80 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
81 # Gold and BFD ld require a version script rather than a plain list.
82 set(native_export_file "${target_name}.exports")
83 # FIXME: Don't write the "local:" line on OpenBSD.
84 add_custom_command(OUTPUT ${native_export_file}
85 COMMAND echo "{" > ${native_export_file}
86 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
87 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
88 COMMAND echo " local: *;" >> ${native_export_file}
89 COMMAND echo "};" >> ${native_export_file}
90 DEPENDS ${export_file}
92 COMMENT "Creating export file for ${target_name}")
93 if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
94 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
95 LINK_FLAGS " -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
97 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
98 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
101 set(native_export_file "${target_name}.def")
103 add_custom_command(OUTPUT ${native_export_file}
104 COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
105 < ${export_file} > ${native_export_file}
106 DEPENDS ${export_file}
108 COMMENT "Creating export file for ${target_name}")
109 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
111 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
113 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
114 LINK_FLAGS " ${export_file_linker_flag}")
117 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
118 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
120 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
122 get_filename_component(extension ${src} EXT)
123 if(extension STREQUAL ".cpp")
124 set(first_source_file ${src})
129 # Force re-linking when the exports file changes. Actually, it
130 # forces recompilation of the source file. The LINK_DEPENDS target
131 # property only works for makefile-based generators.
132 # FIXME: This is not safe because this will create the same target
133 # ${native_export_file} in several different file:
134 # - One where we emitted ${target_name}_exports
135 # - One where we emitted the build command for the following object.
136 # set_property(SOURCE ${first_source_file} APPEND PROPERTY
137 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
139 set_property(DIRECTORY APPEND
140 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
142 add_dependencies(${target_name} ${target_name}_exports)
144 # Add dependency to *_exports later -- CMake issue 14747
145 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
146 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
147 endfunction(add_llvm_symbol_exports)
149 if(NOT WIN32 AND NOT APPLE)
151 COMMAND ${CMAKE_C_COMPILER} -Wl,--version
152 OUTPUT_VARIABLE stdout
155 if("${stdout}" MATCHES "GNU gold")
156 set(LLVM_LINKER_IS_GOLD ON)
160 function(add_link_opts target_name)
161 # Don't use linker optimizations in debug builds since it slows down the
162 # linker in a context where the optimizations are not important.
163 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
165 # Pass -O3 to the linker. This enabled different optimizations on different
167 if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
168 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
169 LINK_FLAGS " -Wl,-O3")
172 if(LLVM_LINKER_IS_GOLD)
173 # With gold gc-sections is always safe.
174 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
175 LINK_FLAGS " -Wl,--gc-sections")
176 # Note that there is a bug with -Wl,--icf=safe so it is not safe
177 # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
180 if(NOT LLVM_NO_DEAD_STRIP)
181 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
182 # ld64's implementation of -dead_strip breaks tools that use plugins.
183 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
184 LINK_FLAGS " -Wl,-dead_strip")
185 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
186 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
187 LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
188 elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
189 # Object files are compiled with -ffunction-data-sections.
190 # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
191 # tools that use plugins. Always pass --gc-sections once we require
193 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
194 LINK_FLAGS " -Wl,--gc-sections")
198 endfunction(add_link_opts)
200 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
201 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
202 # or a certain builder, for eaxample, msbuild.exe, would be confused.
203 function(set_output_directory target)
204 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
206 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
207 # It affects output of add_library(MODULE).
210 set(module_dir ${ARG_BINARY_DIR})
212 set(module_dir ${ARG_LIBRARY_DIR})
214 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
215 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
216 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
218 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
219 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
222 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
223 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
226 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
227 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
232 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
235 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
238 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
243 # If on Windows and building with MSVC, add the resource script containing the
244 # VERSIONINFO data to the project. This embeds version resource information
245 # into the output .exe or .dll.
246 # TODO: Enable for MinGW Windows builds too.
248 function(add_windows_version_resource_file OUT_VAR)
251 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
252 if(EXISTS ${resource_file})
253 set(sources ${sources} ${resource_file})
254 source_group("Resource Files" ${resource_file})
255 set(windows_resource_file ${resource_file} PARENT_SCOPE)
259 set(${OUT_VAR} ${sources} PARENT_SCOPE)
260 endfunction(add_windows_version_resource_file)
262 # set_windows_version_resource_properties(name resource_file...
264 # Optional major version number (defaults to LLVM_VERSION_MAJOR)
266 # Optional minor version number (defaults to LLVM_VERSION_MINOR)
267 # VERSION_PATCHLEVEL int
268 # Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
270 # Optional version string (defaults to PACKAGE_VERSION)
272 # Optional product name string (defaults to "LLVM")
274 function(set_windows_version_resource_properties name resource_file)
275 cmake_parse_arguments(ARG
277 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
281 if (NOT DEFINED ARG_VERSION_MAJOR)
282 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
285 if (NOT DEFINED ARG_VERSION_MINOR)
286 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
289 if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
290 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
293 if (NOT DEFINED ARG_VERSION_STRING)
294 set(ARG_VERSION_STRING ${PACKAGE_VERSION})
297 if (NOT DEFINED ARG_PRODUCT_NAME)
298 set(ARG_PRODUCT_NAME "LLVM")
301 set_property(SOURCE ${resource_file}
302 PROPERTY COMPILE_FLAGS /nologo)
303 set_property(SOURCE ${resource_file}
304 PROPERTY COMPILE_DEFINITIONS
305 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
306 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
307 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
308 "RC_VERSION_FIELD_4=0"
309 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
310 "RC_INTERNAL_NAME=\"${name}\""
311 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
312 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
313 endfunction(set_windows_version_resource_properties)
315 # llvm_add_library(name sources...
317 # STATIC by default w/o BUILD_SHARED_LIBS.
318 # SHARED by default w/ BUILD_SHARED_LIBS.
320 # Also create an OBJECT library target. Default if STATIC && SHARED.
322 # Target ${name} might not be created on unsupported platforms.
323 # Check with "if(TARGET ${name})".
324 # DISABLE_LLVM_LINK_LLVM_DYLIB
325 # Do not link this library to libLLVM, even if
326 # LLVM_LINK_LLVM_DYLIB is enabled.
328 # Corresponds to OUTPUT_NAME in target properties.
330 # Same semantics as add_dependencies().
331 # LINK_COMPONENTS components...
332 # Same as the variable LLVM_LINK_COMPONENTS.
333 # LINK_LIBS lib_targets...
334 # Same semantics as target_link_libraries().
336 # May specify header files for IDE generators.
338 # Should set SONAME link flags and create symlinks
340 # The tool (i.e. cmake target) that this plugin will link against
342 function(llvm_add_library name)
343 cmake_parse_arguments(ARG
344 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
345 "OUTPUT_NAME;PLUGIN_TOOL"
346 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
348 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
349 if(ARG_ADDITIONAL_HEADERS)
350 # Pass through ADDITIONAL_HEADERS.
351 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
354 set(ALL_FILES ${ARG_OBJLIBS})
356 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
360 if(ARG_SHARED OR ARG_STATIC)
361 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
363 # Plugins that link against a tool are allowed even when plugins in general are not
364 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
365 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
370 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
372 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
381 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
382 # Generate an obj library for both targets.
383 set(obj_name "obj.${name}")
384 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
387 llvm_update_compile_flags(${obj_name})
388 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
390 # Do add_dependencies(obj) later due to CMake issue 14747.
391 list(APPEND objlibs ${obj_name})
393 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
396 if(ARG_SHARED AND ARG_STATIC)
398 set(name_static "${name}_static")
400 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
402 # DEPENDS has been appended to LLVM_COMMON_LIBS.
403 llvm_add_library(${name_static} STATIC
405 OBJLIBS ${ALL_FILES} # objlib
406 LINK_LIBS ${ARG_LINK_LIBS}
407 LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
409 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
414 add_library(${name} MODULE ${ALL_FILES})
416 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
417 add_library(${name} SHARED ${ALL_FILES})
419 llvm_setup_rpath(${name})
422 add_library(${name} STATIC ${ALL_FILES})
425 if(DEFINED windows_resource_file)
426 set_windows_version_resource_properties(${name} ${windows_resource_file})
427 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
430 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
431 # $<TARGET_OBJECTS> doesn't require compile flags.
433 llvm_update_compile_flags(${name})
435 add_link_opts( ${name} )
437 set_target_properties(${name}
439 OUTPUT_NAME ${ARG_OUTPUT_NAME}
444 set_target_properties(${name} PROPERTIES
446 SUFFIX ${LLVM_PLUGIN_EXT}
452 set_target_properties(${name} PROPERTIES
457 # Set SOVERSION on shared libraries that lack explicit SONAME
458 # specifier, on *nix systems that are not Darwin.
459 if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
460 set_target_properties(${name}
462 # Concatenate the version numbers since ldconfig expects exactly
463 # one component indicating the ABI version, while LLVM uses
464 # major+minor for that.
465 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR}
466 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
470 if(ARG_MODULE OR ARG_SHARED)
471 # Do not add -Dname_EXPORTS to the command-line when building files in this
472 # target. Doing so is actively harmful for the modules build because it
473 # creates extra module variants, and not useful because we don't use these
475 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
477 if (LLVM_EXPORTED_SYMBOL_FILE)
478 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
482 if(ARG_SHARED AND UNIX)
483 if(NOT APPLE AND ARG_SONAME)
484 get_target_property(output_name ${name} OUTPUT_NAME)
485 if(${output_name} STREQUAL "output_name-NOTFOUND")
486 set(output_name ${name})
488 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
489 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
490 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
491 llvm_install_library_symlink(${api_name} ${library_name} SHARED
494 llvm_install_library_symlink(${output_name} ${library_name} SHARED
500 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
501 # On DLL platforms symbols are imported from the tool by linking against it.
502 set(llvm_libs ${ARG_PLUGIN_TOOL})
503 elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
504 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
507 llvm_map_components_to_libnames(llvm_libs
508 ${ARG_LINK_COMPONENTS}
509 ${LLVM_LINK_COMPONENTS}
513 # Components have not been defined explicitly in CMake, so add the
514 # dependency information for this library as defined by LLVMBuild.
516 # It would be nice to verify that we have the dependencies for this library
517 # name, but using get_property(... SET) doesn't suffice to determine if a
518 # property has been set to an empty value.
519 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
523 set(libtype INTERFACE)
525 # We can use PRIVATE since SO knows its dependent libs.
529 target_link_libraries(${name} ${libtype}
535 if(LLVM_COMMON_DEPENDS)
536 add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
537 # Add dependencies also to objlibs.
538 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user.
539 foreach(objlib ${objlibs})
540 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
544 if(ARG_SHARED OR ARG_MODULE)
545 llvm_externalize_debuginfo(${name})
549 macro(add_llvm_library name)
550 cmake_parse_arguments(ARG
551 "SHARED;BUILDTREE_ONLY"
555 if( BUILD_SHARED_LIBS OR ARG_SHARED )
556 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
558 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
561 # Libraries that are meant to only be exposed via the build tree only are
562 # never installed and are only exported as a target in the special build tree
564 if (NOT ARG_BUILDTREE_ONLY)
565 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
568 if( EXCLUDE_FROM_ALL )
569 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
570 elseif(ARG_BUILDTREE_ONLY)
571 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
573 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO" OR
574 (LLVM_LINK_LLVM_DYLIB AND ${name} STREQUAL "LLVM"))
575 set(install_dir lib${LLVM_LIBDIR_SUFFIX})
576 if(ARG_SHARED OR BUILD_SHARED_LIBS)
577 if(WIN32 OR CYGWIN OR MINGW)
578 set(install_type RUNTIME)
581 set(install_type LIBRARY)
584 set(install_type ARCHIVE)
587 install(TARGETS ${name}
589 ${install_type} DESTINATION ${install_dir}
592 if (NOT CMAKE_CONFIGURATION_TYPES)
593 add_custom_target(install-${name}
595 COMMAND "${CMAKE_COMMAND}"
596 -DCMAKE_INSTALL_COMPONENT=${name}
597 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
600 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
602 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
603 endmacro(add_llvm_library name)
605 macro(add_llvm_loadable_module name)
606 llvm_add_library(${name} MODULE ${ARGN})
607 if(NOT TARGET ${name})
608 # Add empty "phony" target
609 add_custom_target(${name})
611 if( EXCLUDE_FROM_ALL )
612 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
614 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
619 set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
621 install(TARGETS ${name}
623 LIBRARY DESTINATION ${dlldir}
624 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
626 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
630 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
631 endmacro(add_llvm_loadable_module name)
634 macro(add_llvm_executable name)
635 cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH" "" "" ${ARGN})
636 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
639 if(LLVM_ENABLE_OBJLIB)
640 # Generate an obj library for both targets.
641 set(obj_name "obj.${name}")
642 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
645 llvm_update_compile_flags(${obj_name})
646 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
648 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
651 add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
654 # Note: the dummy.cpp source file provides no definitions. However,
655 # it forces Xcode to properly link the static library.
656 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
659 if( EXCLUDE_FROM_ALL )
660 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
662 add_executable(${name} ${ALL_FILES})
665 if(NOT ARG_NO_INSTALL_RPATH)
666 llvm_setup_rpath(${name})
669 if(DEFINED windows_resource_file)
670 set_windows_version_resource_properties(${name} ${windows_resource_file})
673 # $<TARGET_OBJECTS> doesn't require compile flags.
674 if(NOT LLVM_ENABLE_OBJLIB)
675 llvm_update_compile_flags(${name})
677 add_link_opts( ${name} )
679 # Do not add -Dname_EXPORTS to the command-line when building files in this
680 # target. Doing so is actively harmful for the modules build because it
681 # creates extra module variants, and not useful because we don't use these
683 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
685 if (LLVM_EXPORTED_SYMBOL_FILE)
686 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
687 endif(LLVM_EXPORTED_SYMBOL_FILE)
689 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
690 set(USE_SHARED USE_SHARED)
693 set(EXCLUDE_FROM_ALL OFF)
694 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
695 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
696 if( LLVM_COMMON_DEPENDS )
697 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
698 endif( LLVM_COMMON_DEPENDS )
700 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
701 llvm_externalize_debuginfo(${name})
704 # libpthreads overrides some standard library symbols, so main
705 # executable must be linked with it in order to provide consistent
706 # API for all shared libaries loaded by this executable.
707 target_link_libraries(${name} ${PTHREAD_LIB})
709 endmacro(add_llvm_executable name)
711 function(export_executable_symbols target)
712 if (LLVM_EXPORTED_SYMBOL_FILE)
713 # The symbol file should contain the symbols we want the executable to
715 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
716 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
717 # Extract the symbols to export from the static libraries that the
718 # executable links against.
719 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
720 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
721 # We need to consider not just the direct link dependencies, but also the
722 # transitive link dependencies. Do this by starting with the set of direct
723 # dependencies, then the dependencies of those dependencies, and so on.
724 get_target_property(new_libs ${target} LINK_LIBRARIES)
725 set(link_libs ${new_libs})
726 while(NOT "${new_libs}" STREQUAL "")
727 foreach(lib ${new_libs})
729 get_target_property(lib_type ${lib} TYPE)
730 if("${lib_type}" STREQUAL "STATIC_LIBRARY")
731 list(APPEND static_libs ${lib})
733 list(APPEND other_libs ${lib})
735 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
736 foreach(transitive_lib ${transitive_libs})
737 list(FIND link_libs ${transitive_lib} idx)
738 if(TARGET ${transitive_lib} AND idx EQUAL -1)
739 list(APPEND newer_libs ${transitive_lib})
740 list(APPEND link_libs ${transitive_lib})
742 endforeach(transitive_lib)
745 set(new_libs ${newer_libs})
749 set(mangling microsoft)
751 set(mangling itanium)
753 add_custom_command(OUTPUT ${exported_symbol_file}
754 COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
755 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
756 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
758 COMMENT "Generating export list for ${target}")
759 add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
760 # If something links against this executable then we want a
761 # transitive link against only the libraries whose symbols
762 # we aren't exporting.
763 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
764 # The default import library suffix that cmake uses for cygwin/mingw is
765 # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
766 # where the import libraries of both get named libclang.dll.a. Use a suffix
767 # of ".exe.a" to avoid this.
769 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
771 elseif(NOT (WIN32 OR CYGWIN))
772 # On Windows auto-exporting everything doesn't work because of the limit on
773 # the size of the exported symbol table, but on other platforms we can do
774 # it without any trouble.
775 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
777 set_property(TARGET ${target} APPEND_STRING PROPERTY
778 LINK_FLAGS " -rdynamic")
783 if(NOT LLVM_TOOLCHAIN_TOOLS)
784 set (LLVM_TOOLCHAIN_TOOLS
792 macro(add_llvm_tool name)
793 if( NOT LLVM_BUILD_TOOLS )
794 set(EXCLUDE_FROM_ALL ON)
796 add_llvm_executable(${name} ${ARGN})
798 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
799 if( LLVM_BUILD_TOOLS )
800 install(TARGETS ${name}
802 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
805 if (NOT CMAKE_CONFIGURATION_TYPES)
806 add_custom_target(install-${name}
808 COMMAND "${CMAKE_COMMAND}"
809 -DCMAKE_INSTALL_COMPONENT=${name}
810 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
814 if( LLVM_BUILD_TOOLS )
815 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
817 set_target_properties(${name} PROPERTIES FOLDER "Tools")
818 endmacro(add_llvm_tool name)
821 macro(add_llvm_example name)
822 if( NOT LLVM_BUILD_EXAMPLES )
823 set(EXCLUDE_FROM_ALL ON)
825 add_llvm_executable(${name} ${ARGN})
826 if( LLVM_BUILD_EXAMPLES )
827 install(TARGETS ${name} RUNTIME DESTINATION examples)
829 set_target_properties(${name} PROPERTIES FOLDER "Examples")
830 endmacro(add_llvm_example name)
832 # This is a macro that is used to create targets for executables that are needed
833 # for development, but that are not intended to be installed by default.
834 macro(add_llvm_utility name)
835 if ( NOT LLVM_BUILD_UTILS )
836 set(EXCLUDE_FROM_ALL ON)
839 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
840 set_target_properties(${name} PROPERTIES FOLDER "Utils")
841 if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
842 install (TARGETS ${name}
843 RUNTIME DESTINATION bin
845 if (NOT CMAKE_CONFIGURATION_TYPES)
846 add_custom_target(install-${name}
848 COMMAND "${CMAKE_COMMAND}"
849 -DCMAKE_INSTALL_COMPONENT=${name}
850 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
853 endmacro(add_llvm_utility name)
856 macro(add_llvm_target target_name)
857 include_directories(BEFORE
858 ${CMAKE_CURRENT_BINARY_DIR}
859 ${CMAKE_CURRENT_SOURCE_DIR})
860 add_llvm_library(LLVM${target_name} ${ARGN})
861 set( CURRENT_LLVM_TARGET LLVM${target_name} )
862 endmacro(add_llvm_target)
864 function(canonicalize_tool_name name output)
865 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
866 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
867 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
868 set(${output} "${nameUPPER}" PARENT_SCOPE)
869 endfunction(canonicalize_tool_name)
871 # Custom add_subdirectory wrapper
872 # Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
873 # path if it differs from the name.
874 macro(add_llvm_subdirectory project type name)
875 set(add_llvm_external_dir "${ARGN}")
876 if("${add_llvm_external_dir}" STREQUAL "")
877 set(add_llvm_external_dir ${name})
879 canonicalize_tool_name(${name} nameUPPER)
880 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
881 # Treat it as in-tree subproject.
882 option(${project}_${type}_${nameUPPER}_BUILD
883 "Whether to build ${name} as part of ${project}" On)
884 mark_as_advanced(${project}_${type}_${name}_BUILD)
885 if(${project}_${type}_${nameUPPER}_BUILD)
886 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
887 # Don't process it in add_llvm_implicit_projects().
888 set(${project}_${type}_${nameUPPER}_BUILD OFF)
891 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
892 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
893 CACHE PATH "Path to ${name} source directory")
894 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
895 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
896 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
898 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
899 set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
901 option(${project}_${type}_${nameUPPER}_BUILD
902 "Whether to build ${name} as part of LLVM"
903 ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
904 if (${project}_${type}_${nameUPPER}_BUILD)
905 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
906 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
907 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
908 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
910 # FIXME: It'd be redundant.
911 set(${project}_${type}_${nameUPPER}_BUILD Off)
916 # Add external project that may want to be built as part of llvm such as Clang,
917 # lld, and Polly. This adds two options. One for the source directory of the
918 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
919 # enable or disable building it with everything else.
920 # Additional parameter can be specified as the name of directory.
921 macro(add_llvm_external_project name)
922 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
925 macro(add_llvm_tool_subdirectory name)
926 add_llvm_external_project(${name})
927 endmacro(add_llvm_tool_subdirectory)
929 function(get_project_name_from_src_var var output)
930 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
931 MACHED_TOOL "${var}")
933 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
935 set(${output} PARENT_SCOPE)
939 function(create_subdirectory_options project type)
940 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
941 foreach(dir ${sub-dirs})
942 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
943 canonicalize_tool_name(${dir} name)
944 option(${project}_${type}_${name}_BUILD
945 "Whether to build ${name} as part of ${project}" On)
946 mark_as_advanced(${project}_${type}_${name}_BUILD)
949 endfunction(create_subdirectory_options)
951 function(create_llvm_tool_options)
952 create_subdirectory_options(LLVM TOOL)
953 endfunction(create_llvm_tool_options)
955 function(llvm_add_implicit_projects project)
956 set(list_of_implicit_subdirs "")
957 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
958 foreach(dir ${sub-dirs})
959 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
960 canonicalize_tool_name(${dir} name)
961 if (${project}_TOOL_${name}_BUILD)
962 get_filename_component(fn "${dir}" NAME)
963 list(APPEND list_of_implicit_subdirs "${fn}")
968 foreach(external_proj ${list_of_implicit_subdirs})
969 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
971 endfunction(llvm_add_implicit_projects)
973 function(add_llvm_implicit_projects)
974 llvm_add_implicit_projects(LLVM)
975 endfunction(add_llvm_implicit_projects)
977 # Generic support for adding a unittest.
978 function(add_unittest test_suite test_name)
979 if( NOT LLVM_BUILD_TESTS )
980 set(EXCLUDE_FROM_ALL ON)
983 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
984 if (NOT LLVM_ENABLE_THREADS)
985 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
988 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
989 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
992 set(LLVM_REQUIRES_RTTI OFF)
994 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
995 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
996 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
997 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
998 # libpthreads overrides some standard library symbols, so main
999 # executable must be linked with it in order to provide consistent
1000 # API for all shared libaries loaded by this executable.
1001 target_link_libraries(${test_name} gtest_main gtest ${PTHREAD_LIB})
1003 add_dependencies(${test_suite} ${test_name})
1004 get_target_property(test_suite_folder ${test_suite} FOLDER)
1005 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1006 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1010 function(llvm_add_go_executable binary pkgpath)
1011 cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1013 if(LLVM_BINDINGS MATCHES "go")
1014 # FIXME: This should depend only on the libraries Go needs.
1015 get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1016 set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1017 set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1018 set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1020 get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1021 foreach(d ${include_dirs})
1022 set(cppflags "${cppflags} -I${d}")
1024 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1025 add_custom_command(OUTPUT ${binpath}
1026 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1027 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1028 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1029 ${llvmlibs} ${ARG_DEPENDS}
1030 COMMENT "Building Go executable ${binary}"
1033 add_custom_target(${binary} ALL DEPENDS ${binpath})
1035 add_custom_target(${binary} DEPENDS ${binpath})
1040 # This function provides an automatic way to 'configure'-like generate a file
1041 # based on a set of common and custom variables, specifically targeting the
1042 # variables needed for the 'lit.site.cfg' files. This function bundles the
1043 # common variables that any Lit instance is likely to need, and custom
1044 # variables can be passed in.
1045 function(configure_lit_site_cfg input output)
1046 foreach(c ${LLVM_TARGETS_TO_BUILD})
1047 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1049 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1051 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1053 # Configuration-time: See Unit/lit.site.cfg.in
1054 if (CMAKE_CFG_INTDIR STREQUAL ".")
1055 set(LLVM_BUILD_MODE ".")
1057 set(LLVM_BUILD_MODE "%(build_mode)s")
1060 # They below might not be the build tree but provided binary tree.
1061 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1062 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1063 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
1064 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_DIR})
1066 # SHLIBDIR points the build tree.
1067 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1069 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1070 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1071 # plugins. We may rename it.
1072 if(LLVM_ENABLE_PLUGINS)
1073 set(ENABLE_SHARED "1")
1075 set(ENABLE_SHARED "0")
1078 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1079 set(ENABLE_ASSERTIONS "1")
1081 set(ENABLE_ASSERTIONS "0")
1084 set(HOST_OS ${CMAKE_SYSTEM_NAME})
1085 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1087 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1088 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1089 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1091 set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${input}\n## Do not edit!")
1093 configure_file(${input} ${output} @ONLY)
1096 # A raw function to create a lit target. This is used to implement the testuite
1097 # management functions.
1098 function(add_lit_target target comment)
1099 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1100 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1101 separate_arguments(LIT_ARGS)
1102 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1103 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1105 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
1106 # reset cache after erraneous r283029
1107 # TODO: remove this once all buildbots run
1108 if (LIT_COMMAND STREQUAL "${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py")
1109 unset(LIT_COMMAND CACHE)
1111 set (LIT_COMMAND "${PYTHON_EXECUTABLE};${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py"
1112 CACHE STRING "Command used to spawn llvm-lit")
1114 find_program(LIT_COMMAND NAMES llvm-lit lit.py lit)
1116 list(APPEND LIT_COMMAND ${LIT_ARGS})
1117 foreach(param ${ARG_PARAMS})
1118 list(APPEND LIT_COMMAND --param ${param})
1120 if (ARG_UNPARSED_ARGUMENTS)
1121 add_custom_target(${target}
1122 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1123 COMMENT "${comment}"
1127 add_custom_target(${target}
1128 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1129 message(STATUS "${target} does nothing.")
1132 add_dependencies(${target} ${ARG_DEPENDS})
1135 # Tests should be excluded from "Build Solution".
1136 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1139 # A function to add a set of lit test suites to be driven through 'check-*' targets.
1140 function(add_lit_testsuite target comment)
1141 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1143 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1144 if(NOT EXCLUDE_FROM_ALL)
1145 # Register the testsuites, params and depends for the global check rule.
1146 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1147 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1148 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1149 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1152 # Produce a specific suffixed check rule.
1153 add_lit_target(${target} ${comment}
1154 ${ARG_UNPARSED_ARGUMENTS}
1155 PARAMS ${ARG_PARAMS}
1156 DEPENDS ${ARG_DEPENDS}
1161 function(add_lit_testsuites project directory)
1162 if (NOT CMAKE_CONFIGURATION_TYPES)
1163 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1165 # Search recursively for test directories by assuming anything not
1166 # in a directory called Inputs contains tests.
1167 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1168 foreach(lit_suite ${to_process})
1169 if(NOT IS_DIRECTORY ${lit_suite})
1172 string(FIND ${lit_suite} Inputs is_inputs)
1173 string(FIND ${lit_suite} Output is_output)
1174 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1178 # Create a check- target for the directory.
1179 string(REPLACE ${directory} "" name_slash ${lit_suite})
1181 string(REPLACE "/" "-" name_slash ${name_slash})
1182 string(REPLACE "\\" "-" name_dashes ${name_slash})
1183 string(TOLOWER "${project}${name_dashes}" name_var)
1184 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1186 PARAMS ${ARG_PARAMS}
1187 DEPENDS ${ARG_DEPENDS}
1195 function(llvm_install_library_symlink name dest type)
1196 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1197 foreach(path ${CMAKE_MODULE_PATH})
1198 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1199 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1204 set(component ${ARG_COMPONENT})
1206 set(component ${name})
1209 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1210 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1212 set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1213 if(WIN32 AND "${type}" STREQUAL "SHARED")
1217 install(SCRIPT ${INSTALL_SYMLINK}
1218 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1219 COMPONENT ${component})
1221 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1222 add_custom_target(install-${name}
1223 DEPENDS ${name} ${dest} install-${dest}
1224 COMMAND "${CMAKE_COMMAND}"
1225 -DCMAKE_INSTALL_COMPONENT=${name}
1226 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1230 function(llvm_install_symlink name dest)
1231 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
1232 foreach(path ${CMAKE_MODULE_PATH})
1233 if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1234 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1239 if(ARG_ALWAYS_GENERATE)
1240 set(component ${dest})
1242 set(component ${name})
1245 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1246 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1248 install(SCRIPT ${INSTALL_SYMLINK}
1249 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1250 COMPONENT ${component})
1252 if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1253 add_custom_target(install-${name}
1254 DEPENDS ${name} ${dest} install-${dest}
1255 COMMAND "${CMAKE_COMMAND}"
1256 -DCMAKE_INSTALL_COMPONENT=${name}
1257 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1261 function(add_llvm_tool_symlink name dest)
1262 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
1264 set(LLVM_LINK_OR_COPY create_symlink)
1265 set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
1267 set(LLVM_LINK_OR_COPY copy)
1268 set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
1271 set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
1273 set(target_name ${name})
1275 set(target_name ${name}-link)
1279 if(ARG_ALWAYS_GENERATE)
1280 set_property(DIRECTORY APPEND PROPERTY
1281 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1282 add_custom_command(TARGET ${dest} POST_BUILD
1283 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1285 add_custom_command(OUTPUT ${output_path}
1286 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1288 add_custom_target(${target_name} ALL DEPENDS ${output_path})
1289 set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1291 # Make sure both the link and target are toolchain tools
1292 if (${name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${dest} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1293 set(TOOL_IS_TOOLCHAIN ON)
1296 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1297 llvm_install_symlink(${name} ${dest})
1302 function(llvm_externalize_debuginfo name)
1303 if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1307 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1308 set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1312 if(CMAKE_CXX_FLAGS MATCHES "-flto"
1313 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1315 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1316 set_property(TARGET ${name} APPEND_STRING PROPERTY
1317 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1319 add_custom_command(TARGET ${name} POST_BUILD
1320 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
1324 message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
1328 function(llvm_setup_rpath name)
1329 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1330 set(extra_libdir ${LLVM_LIBRARY_DIR})
1331 elseif(LLVM_BUILD_LIBRARY_DIR)
1332 set(extra_libdir ${LLVM_LIBRARY_DIR})
1336 set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1337 set(_install_rpath "@loader_path/../lib" ${extra_libdir})
1339 if(NOT DEFINED CMAKE_INSTALL_RPATH)
1340 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1341 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1342 set_property(TARGET ${name} APPEND_STRING PROPERTY
1343 LINK_FLAGS " -Wl,-z,origin ")
1344 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT LLVM_LINKER_IS_GOLD)
1345 # $ORIGIN is not interpreted at link time by ld.bfd
1346 set_property(TARGET ${name} APPEND_STRING PROPERTY
1347 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1354 set_target_properties(${name} PROPERTIES
1355 BUILD_WITH_INSTALL_RPATH On
1356 INSTALL_RPATH "${_install_rpath}"
1357 ${_install_name_dir})