1include(LLVMProcessSources)
2include(LLVM-Config)
3include(DetermineGCCCompatible)
4
5function(llvm_update_compile_flags name)
6  get_property(sources TARGET ${name} PROPERTY SOURCES)
7  if("${sources}" MATCHES "\\.c(;|$)")
8    set(update_src_props ON)
9  endif()
10
11  # LLVM_REQUIRES_EH is an internal flag that individual
12  # targets can use to force EH
13  if((LLVM_REQUIRES_EH OR LLVM_ENABLE_EH) AND NOT CLANG_CL)
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)
17    endif()
18  else()
19    if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
20      list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
21    elseif(MSVC)
22      list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
23      list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
24    endif()
25  endif()
26
27  # LLVM_REQUIRES_RTTI is an internal flag that individual
28  # targets can use to force RTTI
29  set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
30  if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
31    set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
32    list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
33    if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
34      list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
35    elseif (MSVC)
36      list(APPEND LLVM_COMPILE_FLAGS "/GR-")
37    endif ()
38  endif()
39
40  # Assume that;
41  #   - LLVM_COMPILE_FLAGS is list.
42  #   - PROPERTY COMPILE_FLAGS is string.
43  string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
44
45  if(update_src_props)
46    foreach(fn ${sources})
47      get_filename_component(suf ${fn} EXT)
48      if("${suf}" STREQUAL ".cpp")
49        set_property(SOURCE ${fn} APPEND_STRING PROPERTY
50          COMPILE_FLAGS "${target_compile_flags}")
51      endif()
52    endforeach()
53  else()
54    # Update target props, since all sources are C++.
55    set_property(TARGET ${name} APPEND_STRING PROPERTY
56      COMPILE_FLAGS "${target_compile_flags}")
57  endif()
58
59  set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
60endfunction()
61
62function(add_llvm_symbol_exports target_name export_file)
63  if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
64    set(native_export_file "${target_name}.exports")
65    add_custom_command(OUTPUT ${native_export_file}
66      COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
67      DEPENDS ${export_file}
68      VERBATIM
69      COMMENT "Creating export file for ${target_name}")
70    set_property(TARGET ${target_name} APPEND_STRING PROPERTY
71                 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
72  elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
73    # Gold and BFD ld require a version script rather than a plain list.
74    set(native_export_file "${target_name}.exports")
75    # FIXME: Don't write the "local:" line on OpenBSD.
76    add_custom_command(OUTPUT ${native_export_file}
77      COMMAND echo "{" > ${native_export_file}
78      COMMAND grep -q "[[:alnum:]]" ${export_file} && echo "  global:" >> ${native_export_file} || :
79      COMMAND sed -e "s/$/;/" -e "s/^/    /" < ${export_file} >> ${native_export_file}
80      COMMAND echo "  local: *;" >> ${native_export_file}
81      COMMAND echo "};" >> ${native_export_file}
82      DEPENDS ${export_file}
83      VERBATIM
84      COMMENT "Creating export file for ${target_name}")
85    if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
86      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
87                   LINK_FLAGS "  -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
88    else()
89      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
90                   LINK_FLAGS "  -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
91    endif()
92  else()
93    set(native_export_file "${target_name}.def")
94
95    add_custom_command(OUTPUT ${native_export_file}
96      COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
97        < ${export_file} > ${native_export_file}
98      DEPENDS ${export_file}
99      VERBATIM
100      COMMENT "Creating export file for ${target_name}")
101    set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
102    if(MSVC)
103      set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
104    endif()
105    set_property(TARGET ${target_name} APPEND_STRING PROPERTY
106                 LINK_FLAGS " ${export_file_linker_flag}")
107  endif()
108
109  add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
110  set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
111
112  get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
113  foreach(src ${srcs})
114    get_filename_component(extension ${src} EXT)
115    if(extension STREQUAL ".cpp")
116      set(first_source_file ${src})
117      break()
118    endif()
119  endforeach()
120
121  # Force re-linking when the exports file changes. Actually, it
122  # forces recompilation of the source file. The LINK_DEPENDS target
123  # property only works for makefile-based generators.
124  # FIXME: This is not safe because this will create the same target
125  # ${native_export_file} in several different file:
126  # - One where we emitted ${target_name}_exports
127  # - One where we emitted the build command for the following object.
128  # set_property(SOURCE ${first_source_file} APPEND PROPERTY
129  #   OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
130
131  set_property(DIRECTORY APPEND
132    PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
133
134  add_dependencies(${target_name} ${target_name}_exports)
135
136  # Add dependency to *_exports later -- CMake issue 14747
137  list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
138  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
139endfunction(add_llvm_symbol_exports)
140
141if(NOT WIN32 AND NOT APPLE)
142  execute_process(
143    COMMAND ${CMAKE_C_COMPILER} -Wl,--version
144    OUTPUT_VARIABLE stdout
145    ERROR_QUIET
146    )
147  if("${stdout}" MATCHES "GNU gold")
148    set(LLVM_LINKER_IS_GOLD ON)
149  endif()
150endif()
151
152function(add_link_opts target_name)
153  # Don't use linker optimizations in debug builds since it slows down the
154  # linker in a context where the optimizations are not important.
155  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
156
157    # Pass -O3 to the linker. This enabled different optimizations on different
158    # linkers.
159    if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS" OR WIN32))
160      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
161                   LINK_FLAGS " -Wl,-O3")
162    endif()
163
164    if(LLVM_LINKER_IS_GOLD)
165      # With gold gc-sections is always safe.
166      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
167                   LINK_FLAGS " -Wl,--gc-sections")
168      # Note that there is a bug with -Wl,--icf=safe so it is not safe
169      # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
170    endif()
171
172    if(NOT LLVM_NO_DEAD_STRIP)
173      if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
174        # ld64's implementation of -dead_strip breaks tools that use plugins.
175        set_property(TARGET ${target_name} APPEND_STRING PROPERTY
176                     LINK_FLAGS " -Wl,-dead_strip")
177      elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
178        set_property(TARGET ${target_name} APPEND_STRING PROPERTY
179                     LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
180      elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
181        # Object files are compiled with -ffunction-data-sections.
182        # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
183        # tools that use plugins. Always pass --gc-sections once we require
184        # a newer linker.
185        set_property(TARGET ${target_name} APPEND_STRING PROPERTY
186                     LINK_FLAGS " -Wl,--gc-sections")
187      endif()
188    endif()
189  endif()
190endfunction(add_link_opts)
191
192# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
193# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
194# or a certain builder, for eaxample, msbuild.exe, would be confused.
195function(set_output_directory target)
196  cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
197
198  # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
199  # It affects output of add_library(MODULE).
200  if(WIN32 OR CYGWIN)
201    # DLL platform
202    set(module_dir ${ARG_BINARY_DIR})
203  else()
204    set(module_dir ${ARG_LIBRARY_DIR})
205  endif()
206  if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
207    foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
208      string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
209      if(ARG_BINARY_DIR)
210        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
211        set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
212      endif()
213      if(ARG_LIBRARY_DIR)
214        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
215        set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
216      endif()
217      if(module_dir)
218        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
219        set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
220      endif()
221    endforeach()
222  else()
223    if(ARG_BINARY_DIR)
224      set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
225    endif()
226    if(ARG_LIBRARY_DIR)
227      set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
228    endif()
229    if(module_dir)
230      set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
231    endif()
232  endif()
233endfunction()
234
235# If on Windows and building with MSVC, add the resource script containing the
236# VERSIONINFO data to the project.  This embeds version resource information
237# into the output .exe or .dll.
238# TODO: Enable for MinGW Windows builds too.
239#
240function(add_windows_version_resource_file OUT_VAR)
241  set(sources ${ARGN})
242  if (MSVC)
243    set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
244    if(EXISTS ${resource_file})
245      set(sources ${sources} ${resource_file})
246      source_group("Resource Files" ${resource_file})
247      set(windows_resource_file ${resource_file} PARENT_SCOPE)
248    endif()
249  endif(MSVC)
250
251  set(${OUT_VAR} ${sources} PARENT_SCOPE)
252endfunction(add_windows_version_resource_file)
253
254# set_windows_version_resource_properties(name resource_file...
255#   VERSION_MAJOR int
256#     Optional major version number (defaults to LLVM_VERSION_MAJOR)
257#   VERSION_MINOR int
258#     Optional minor version number (defaults to LLVM_VERSION_MINOR)
259#   VERSION_PATCHLEVEL int
260#     Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
261#   VERSION_STRING
262#     Optional version string (defaults to PACKAGE_VERSION)
263#   PRODUCT_NAME
264#     Optional product name string (defaults to "LLVM")
265#   )
266function(set_windows_version_resource_properties name resource_file)
267  cmake_parse_arguments(ARG
268    ""
269    "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
270    ""
271    ${ARGN})
272
273  if (NOT DEFINED ARG_VERSION_MAJOR)
274    set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
275  endif()
276
277  if (NOT DEFINED ARG_VERSION_MINOR)
278    set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
279  endif()
280
281  if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
282    set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
283  endif()
284
285  if (NOT DEFINED ARG_VERSION_STRING)
286    set(ARG_VERSION_STRING ${PACKAGE_VERSION})
287  endif()
288
289  if (NOT DEFINED ARG_PRODUCT_NAME)
290    set(ARG_PRODUCT_NAME "LLVM")
291  endif()
292
293  set_property(SOURCE ${resource_file}
294               PROPERTY COMPILE_FLAGS /nologo)
295  set_property(SOURCE ${resource_file}
296               PROPERTY COMPILE_DEFINITIONS
297               "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
298               "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
299               "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
300               "RC_VERSION_FIELD_4=0"
301               "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
302               "RC_INTERNAL_NAME=\"${name}\""
303               "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
304               "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
305endfunction(set_windows_version_resource_properties)
306
307# llvm_add_library(name sources...
308#   SHARED;STATIC
309#     STATIC by default w/o BUILD_SHARED_LIBS.
310#     SHARED by default w/  BUILD_SHARED_LIBS.
311#   MODULE
312#     Target ${name} might not be created on unsupported platforms.
313#     Check with "if(TARGET ${name})".
314#   DISABLE_LLVM_LINK_LLVM_DYLIB
315#     Do not link this library to libLLVM, even if
316#     LLVM_LINK_LLVM_DYLIB is enabled.
317#   OUTPUT_NAME name
318#     Corresponds to OUTPUT_NAME in target properties.
319#   DEPENDS targets...
320#     Same semantics as add_dependencies().
321#   LINK_COMPONENTS components...
322#     Same as the variable LLVM_LINK_COMPONENTS.
323#   LINK_LIBS lib_targets...
324#     Same semantics as target_link_libraries().
325#   ADDITIONAL_HEADERS
326#     May specify header files for IDE generators.
327#   SONAME
328#     Should set SONAME link flags and create symlinks
329#   )
330function(llvm_add_library name)
331  cmake_parse_arguments(ARG
332    "MODULE;SHARED;STATIC;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
333    "OUTPUT_NAME"
334    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
335    ${ARGN})
336  list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
337  if(ARG_ADDITIONAL_HEADERS)
338    # Pass through ADDITIONAL_HEADERS.
339    set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
340  endif()
341  if(ARG_OBJLIBS)
342    set(ALL_FILES ${ARG_OBJLIBS})
343  else()
344    llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
345  endif()
346
347  if(ARG_MODULE)
348    if(ARG_SHARED OR ARG_STATIC)
349      message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
350    endif()
351    if(NOT LLVM_ENABLE_PLUGINS)
352      message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
353      return()
354    endif()
355  else()
356    if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
357      set(ARG_SHARED TRUE)
358    endif()
359    if(NOT ARG_SHARED)
360      set(ARG_STATIC TRUE)
361    endif()
362  endif()
363
364  # Generate objlib
365  if(ARG_SHARED AND ARG_STATIC)
366    # Generate an obj library for both targets.
367    set(obj_name "obj.${name}")
368    add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
369      ${ALL_FILES}
370      )
371    llvm_update_compile_flags(${obj_name})
372    set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
373
374    # Do add_dependencies(obj) later due to CMake issue 14747.
375    list(APPEND objlibs ${obj_name})
376
377    set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
378  endif()
379
380  if(ARG_SHARED AND ARG_STATIC)
381    # static
382    set(name_static "${name}_static")
383    if(ARG_OUTPUT_NAME)
384      set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
385    endif()
386    # DEPENDS has been appended to LLVM_COMMON_LIBS.
387    llvm_add_library(${name_static} STATIC
388      ${output_name}
389      OBJLIBS ${ALL_FILES} # objlib
390      LINK_LIBS ${ARG_LINK_LIBS}
391      LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
392      )
393    # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
394    set(ARG_STATIC)
395  endif()
396
397  if(ARG_MODULE)
398    add_library(${name} MODULE ${ALL_FILES})
399  elseif(ARG_SHARED)
400    add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
401    add_library(${name} SHARED ${ALL_FILES})
402  else()
403    add_library(${name} STATIC ${ALL_FILES})
404  endif()
405
406  if(DEFINED windows_resource_file)
407    set_windows_version_resource_properties(${name} ${windows_resource_file})
408    set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
409  endif()
410
411  set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
412  # $<TARGET_OBJECTS> doesn't require compile flags.
413  if(NOT obj_name)
414    llvm_update_compile_flags(${name})
415  endif()
416  add_link_opts( ${name} )
417  if(ARG_OUTPUT_NAME)
418    set_target_properties(${name}
419      PROPERTIES
420      OUTPUT_NAME ${ARG_OUTPUT_NAME}
421      )
422  endif()
423
424  if(ARG_MODULE)
425    set_target_properties(${name} PROPERTIES
426      PREFIX ""
427      SUFFIX ${LLVM_PLUGIN_EXT}
428      )
429  endif()
430
431  if(ARG_SHARED)
432    if(WIN32)
433      set_target_properties(${name} PROPERTIES
434        PREFIX ""
435        )
436    endif()
437  endif()
438
439  if(ARG_MODULE OR ARG_SHARED)
440    # Do not add -Dname_EXPORTS to the command-line when building files in this
441    # target. Doing so is actively harmful for the modules build because it
442    # creates extra module variants, and not useful because we don't use these
443    # macros.
444    set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
445
446    if (LLVM_EXPORTED_SYMBOL_FILE)
447      add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
448    endif()
449  endif()
450
451  if(ARG_SHARED AND UNIX)
452    if(NOT APPLE AND ARG_SONAME)
453      get_target_property(output_name ${name} OUTPUT_NAME)
454      if(${output_name} STREQUAL "output_name-NOTFOUND")
455        set(output_name ${name})
456      endif()
457      set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
458      set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
459      set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
460      llvm_install_library_symlink(${api_name} ${library_name} SHARED
461        COMPONENT ${name}
462        ALWAYS_GENERATE)
463      llvm_install_library_symlink(${output_name} ${library_name} SHARED
464        COMPONENT ${name}
465        ALWAYS_GENERATE)
466    endif()
467  endif()
468
469  # Add the explicit dependency information for this library.
470  #
471  # It would be nice to verify that we have the dependencies for this library
472  # name, but using get_property(... SET) doesn't suffice to determine if a
473  # property has been set to an empty value.
474  get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
475
476  if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_STATIC AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
477    set(llvm_libs LLVM)
478  else()
479    llvm_map_components_to_libnames(llvm_libs
480      ${ARG_LINK_COMPONENTS}
481      ${LLVM_LINK_COMPONENTS}
482      )
483  endif()
484
485  if(CMAKE_VERSION VERSION_LESS 2.8.12)
486    # Link libs w/o keywords, assuming PUBLIC.
487    target_link_libraries(${name}
488      ${ARG_LINK_LIBS}
489      ${lib_deps}
490      ${llvm_libs}
491      )
492  elseif(ARG_STATIC)
493    target_link_libraries(${name} INTERFACE
494      ${ARG_LINK_LIBS}
495      ${lib_deps}
496      ${llvm_libs}
497      )
498  else()
499    # We can use PRIVATE since SO knows its dependent libs.
500    target_link_libraries(${name} PRIVATE
501      ${ARG_LINK_LIBS}
502      ${lib_deps}
503      ${llvm_libs}
504      )
505  endif()
506
507  if(LLVM_COMMON_DEPENDS)
508    add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
509    # Add dependencies also to objlibs.
510    # CMake issue 14747 --  add_dependencies() might be ignored to objlib's user.
511    foreach(objlib ${objlibs})
512      add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
513    endforeach()
514  endif()
515
516  if(ARG_SHARED OR ARG_MODULE)
517    llvm_externalize_debuginfo(${name})
518  endif()
519endfunction()
520
521macro(add_llvm_library name)
522  cmake_parse_arguments(ARG
523    "SHARED"
524    ""
525    ""
526    ${ARGN})
527  if( BUILD_SHARED_LIBS )
528    llvm_add_library(${name} SHARED ${ARGN})
529  else()
530    llvm_add_library(${name} ${ARGN})
531  endif()
532  # The gtest libraries should not be installed or exported as a target
533  if ("${name}" STREQUAL gtest OR "${name}" STREQUAL gtest_main)
534    set(_is_gtest TRUE)
535  else()
536    set(_is_gtest FALSE)
537    set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
538  endif()
539
540  if( EXCLUDE_FROM_ALL )
541    set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
542  elseif(NOT _is_gtest)
543    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
544      set(install_dir lib${LLVM_LIBDIR_SUFFIX})
545      if(ARG_SHARED OR BUILD_SHARED_LIBS)
546        if(WIN32 OR CYGWIN OR MINGW)
547          set(install_type RUNTIME)
548          set(install_dir bin)
549        else()
550          set(install_type LIBRARY)
551        endif()
552      else()
553        set(install_type ARCHIVE)
554      endif()
555
556      install(TARGETS ${name}
557            EXPORT LLVMExports
558            ${install_type} DESTINATION ${install_dir}
559            COMPONENT ${name})
560
561      if (NOT CMAKE_CONFIGURATION_TYPES)
562        add_custom_target(install-${name}
563                          DEPENDS ${name}
564                          COMMAND "${CMAKE_COMMAND}"
565                                  -DCMAKE_INSTALL_COMPONENT=${name}
566                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
567      endif()
568    endif()
569    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
570  endif()
571  set_target_properties(${name} PROPERTIES FOLDER "Libraries")
572endmacro(add_llvm_library name)
573
574macro(add_llvm_loadable_module name)
575  llvm_add_library(${name} MODULE ${ARGN})
576  if(NOT TARGET ${name})
577    # Add empty "phony" target
578    add_custom_target(${name})
579  else()
580    if( EXCLUDE_FROM_ALL )
581      set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
582    else()
583      if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
584        if(WIN32 OR CYGWIN)
585          # DLL platform
586          set(dlldir "bin")
587        else()
588          set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
589        endif()
590        install(TARGETS ${name}
591          EXPORT LLVMExports
592          LIBRARY DESTINATION ${dlldir}
593          ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
594      endif()
595      set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
596    endif()
597  endif()
598
599  set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
600endmacro(add_llvm_loadable_module name)
601
602
603macro(add_llvm_executable name)
604  cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO" "" "" ${ARGN})
605  llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
606
607  # Generate objlib
608  if(LLVM_ENABLE_OBJLIB)
609    # Generate an obj library for both targets.
610    set(obj_name "obj.${name}")
611    add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
612      ${ALL_FILES}
613      )
614    llvm_update_compile_flags(${obj_name})
615    set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
616
617    set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
618  endif()
619
620  add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
621
622  if(XCODE)
623    # Note: the dummy.cpp source file provides no definitions. However,
624    # it forces Xcode to properly link the static library.
625    list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
626  endif()
627
628  if( EXCLUDE_FROM_ALL )
629    add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
630  else()
631    add_executable(${name} ${ALL_FILES})
632  endif()
633
634  if(DEFINED windows_resource_file)
635    set_windows_version_resource_properties(${name} ${windows_resource_file})
636  endif()
637
638  # $<TARGET_OBJECTS> doesn't require compile flags.
639  if(NOT LLVM_ENABLE_OBJLIB)
640    llvm_update_compile_flags(${name})
641  endif()
642  add_link_opts( ${name} )
643
644  # Do not add -Dname_EXPORTS to the command-line when building files in this
645  # target. Doing so is actively harmful for the modules build because it
646  # creates extra module variants, and not useful because we don't use these
647  # macros.
648  set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
649
650  if (LLVM_EXPORTED_SYMBOL_FILE)
651    add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
652  endif(LLVM_EXPORTED_SYMBOL_FILE)
653
654  if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
655    set(USE_SHARED USE_SHARED)
656  endif()
657
658  set(EXCLUDE_FROM_ALL OFF)
659  set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
660  llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
661  if( LLVM_COMMON_DEPENDS )
662    add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
663  endif( LLVM_COMMON_DEPENDS )
664
665  if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
666    llvm_externalize_debuginfo(${name})
667  endif()
668endmacro(add_llvm_executable name)
669
670function(export_executable_symbols target)
671  if (NOT MSVC) # MSVC's linker doesn't support exporting all symbols.
672    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
673    if (APPLE)
674      set_property(TARGET ${target} APPEND_STRING PROPERTY
675        LINK_FLAGS " -rdynamic")
676    endif()
677  endif()
678endfunction()
679
680if(NOT LLVM_TOOLCHAIN_TOOLS)
681  set (LLVM_TOOLCHAIN_TOOLS
682    llvm-ar
683    llvm-ranlib
684    llvm-lib
685    llvm-objdump
686    )
687endif()
688
689macro(add_llvm_tool name)
690  if( NOT LLVM_BUILD_TOOLS )
691    set(EXCLUDE_FROM_ALL ON)
692  endif()
693  add_llvm_executable(${name} ${ARGN})
694
695  list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
696  if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
697    if( LLVM_BUILD_TOOLS )
698      install(TARGETS ${name}
699              EXPORT LLVMExports
700              RUNTIME DESTINATION bin
701              COMPONENT ${name})
702
703      if (NOT CMAKE_CONFIGURATION_TYPES)
704        add_custom_target(install-${name}
705                          DEPENDS ${name}
706                          COMMAND "${CMAKE_COMMAND}"
707                                  -DCMAKE_INSTALL_COMPONENT=${name}
708                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
709      endif()
710    endif()
711  endif()
712  if( LLVM_BUILD_TOOLS )
713    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
714  endif()
715  set_target_properties(${name} PROPERTIES FOLDER "Tools")
716endmacro(add_llvm_tool name)
717
718
719macro(add_llvm_example name)
720  if( NOT LLVM_BUILD_EXAMPLES )
721    set(EXCLUDE_FROM_ALL ON)
722  endif()
723  add_llvm_executable(${name} ${ARGN})
724  if( LLVM_BUILD_EXAMPLES )
725    install(TARGETS ${name} RUNTIME DESTINATION examples)
726  endif()
727  set_target_properties(${name} PROPERTIES FOLDER "Examples")
728endmacro(add_llvm_example name)
729
730
731macro(add_llvm_utility name)
732  add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
733  set_target_properties(${name} PROPERTIES FOLDER "Utils")
734  if( LLVM_INSTALL_UTILS )
735    install (TARGETS ${name}
736      RUNTIME DESTINATION bin
737      COMPONENT ${name})
738    if (NOT CMAKE_CONFIGURATION_TYPES)
739      add_custom_target(install-${name}
740                        DEPENDS ${name}
741                        COMMAND "${CMAKE_COMMAND}"
742                                -DCMAKE_INSTALL_COMPONENT=${name}
743                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
744    endif()
745  endif()
746endmacro(add_llvm_utility name)
747
748
749macro(add_llvm_target target_name)
750  include_directories(BEFORE
751    ${CMAKE_CURRENT_BINARY_DIR}
752    ${CMAKE_CURRENT_SOURCE_DIR})
753  add_llvm_library(LLVM${target_name} ${ARGN})
754  set( CURRENT_LLVM_TARGET LLVM${target_name} )
755endmacro(add_llvm_target)
756
757function(canonicalize_tool_name name output)
758  string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
759  string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
760  string(TOUPPER ${nameUNDERSCORE} nameUPPER)
761  set(${output} "${nameUPPER}" PARENT_SCOPE)
762endfunction(canonicalize_tool_name)
763
764# Custom add_subdirectory wrapper
765# Takes in a project name (i.e. LLVM), the the subdirectory name, and an
766# and an optional path if it differs from the name.
767macro(add_llvm_subdirectory project type name)
768  set(add_llvm_external_dir "${ARGN}")
769  if("${add_llvm_external_dir}" STREQUAL "")
770    set(add_llvm_external_dir ${name})
771  endif()
772  canonicalize_tool_name(${name} nameUPPER)
773  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
774    # Treat it as in-tree subproject.
775    option(${project}_${type}_${nameUPPER}_BUILD
776           "Whether to build ${name} as part of ${project}" On)
777    mark_as_advanced(${project}_${type}_${name}_BUILD)
778    if(${project}_${type}_${nameUPPER}_BUILD)
779      add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
780      # Don't process it in add_llvm_implicit_projects().
781      set(${project}_${type}_${nameUPPER}_BUILD OFF)
782    endif()
783  else()
784    set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
785      "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
786      CACHE PATH "Path to ${name} source directory")
787    set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
788    if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
789      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
790    endif()
791    if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
792      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
793    endif()
794    option(${project}_${type}_${nameUPPER}_BUILD
795      "Whether to build ${name} as part of LLVM"
796      ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
797    if (${project}_${type}_${nameUPPER}_BUILD)
798      if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
799        add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
800      elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
801        message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
802      endif()
803      # FIXME: It'd be redundant.
804      set(${project}_${type}_${nameUPPER}_BUILD Off)
805    endif()
806  endif()
807endmacro()
808
809# Add external project that may want to be built as part of llvm such as Clang,
810# lld, and Polly. This adds two options. One for the source directory of the
811# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
812# enable or disable building it with everything else.
813# Additional parameter can be specified as the name of directory.
814macro(add_llvm_external_project name)
815  add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
816endmacro()
817
818macro(add_llvm_tool_subdirectory name)
819  add_llvm_external_project(${name})
820endmacro(add_llvm_tool_subdirectory)
821
822function(get_project_name_from_src_var var output)
823  string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
824         MACHED_TOOL "${var}")
825  if(MACHED_TOOL)
826    set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
827  else()
828    set(${output} PARENT_SCOPE)
829  endif()
830endfunction()
831
832function(create_subdirectory_options project type)
833  file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
834  foreach(dir ${sub-dirs})
835    if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
836      canonicalize_tool_name(${dir} name)
837      option(${project}_${type}_${name}_BUILD
838           "Whether to build ${name} as part of ${project}" On)
839      mark_as_advanced(${project}_${type}_${name}_BUILD)
840    endif()
841  endforeach()
842endfunction(create_subdirectory_options)
843
844function(create_llvm_tool_options)
845  create_subdirectory_options(LLVM TOOL)
846endfunction(create_llvm_tool_options)
847
848function(add_llvm_implicit_projects)
849  set(list_of_implicit_subdirs "")
850  file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
851  foreach(dir ${sub-dirs})
852    if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
853      canonicalize_tool_name(${dir} name)
854      if (LLVM_TOOL_${name}_BUILD)
855        get_filename_component(fn "${dir}" NAME)
856        list(APPEND list_of_implicit_subdirs "${fn}")
857      endif()
858    endif()
859  endforeach()
860
861  foreach(external_proj ${list_of_implicit_subdirs})
862    add_llvm_external_project("${external_proj}")
863  endforeach()
864endfunction(add_llvm_implicit_projects)
865
866# Generic support for adding a unittest.
867function(add_unittest test_suite test_name)
868  if( NOT LLVM_BUILD_TESTS )
869    set(EXCLUDE_FROM_ALL ON)
870  endif()
871
872  include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
873  if (NOT LLVM_ENABLE_THREADS)
874    list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
875  endif ()
876
877  if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
878    list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
879  endif ()
880
881  set(LLVM_REQUIRES_RTTI OFF)
882
883  add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO ${ARGN})
884  set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
885  set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
886  target_link_libraries(${test_name}
887    gtest
888    gtest_main
889    LLVMSupport # gtest needs it for raw_ostream.
890    )
891
892  add_dependencies(${test_suite} ${test_name})
893  get_target_property(test_suite_folder ${test_suite} FOLDER)
894  if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
895    set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
896  endif ()
897endfunction()
898
899function(llvm_add_go_executable binary pkgpath)
900  cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
901
902  if(LLVM_BINDINGS MATCHES "go")
903    # FIXME: This should depend only on the libraries Go needs.
904    get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
905    set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
906    set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
907    set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
908    set(cppflags "")
909    get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
910    foreach(d ${include_dirs})
911      set(cppflags "${cppflags} -I${d}")
912    endforeach(d)
913    set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
914    if (LLVM_LINK_LLVM_DYLIB)
915      set(linkmode "dylib")
916    else()
917      set(linkmode "component-libs")
918    endif()
919    add_custom_command(OUTPUT ${binpath}
920      COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "linkmode=${linkmode}"
921              ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
922      DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
923              ${llvmlibs} ${ARG_DEPENDS}
924      COMMENT "Building Go executable ${binary}"
925      VERBATIM)
926    if (ARG_ALL)
927      add_custom_target(${binary} ALL DEPENDS ${binpath})
928    else()
929      add_custom_target(${binary} DEPENDS ${binpath})
930    endif()
931  endif()
932endfunction()
933
934# This function provides an automatic way to 'configure'-like generate a file
935# based on a set of common and custom variables, specifically targeting the
936# variables needed for the 'lit.site.cfg' files. This function bundles the
937# common variables that any Lit instance is likely to need, and custom
938# variables can be passed in.
939function(configure_lit_site_cfg input output)
940  foreach(c ${LLVM_TARGETS_TO_BUILD})
941    set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
942  endforeach(c)
943  set(TARGETS_TO_BUILD ${TARGETS_BUILT})
944
945  set(SHLIBEXT "${LTDL_SHLIB_EXT}")
946
947  # Configuration-time: See Unit/lit.site.cfg.in
948  if (CMAKE_CFG_INTDIR STREQUAL ".")
949    set(LLVM_BUILD_MODE ".")
950  else ()
951    set(LLVM_BUILD_MODE "%(build_mode)s")
952  endif ()
953
954  # They below might not be the build tree but provided binary tree.
955  set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
956  set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
957  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
958  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
959
960  # SHLIBDIR points the build tree.
961  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
962
963  set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
964  # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
965  # plugins. We may rename it.
966  if(LLVM_ENABLE_PLUGINS)
967    set(ENABLE_SHARED "1")
968  else()
969    set(ENABLE_SHARED "0")
970  endif()
971
972  if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
973    set(ENABLE_ASSERTIONS "1")
974  else()
975    set(ENABLE_ASSERTIONS "0")
976  endif()
977
978  set(HOST_OS ${CMAKE_SYSTEM_NAME})
979  set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
980
981  set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
982  set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
983  set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
984
985  configure_file(${input} ${output} @ONLY)
986endfunction()
987
988# A raw function to create a lit target. This is used to implement the testuite
989# management functions.
990function(add_lit_target target comment)
991  cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
992  set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
993  separate_arguments(LIT_ARGS)
994  if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
995    list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
996  endif ()
997  if (LLVM_MAIN_SRC_DIR)
998    set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
999  else()
1000    find_program(LIT_COMMAND llvm-lit)
1001  endif ()
1002  list(APPEND LIT_COMMAND ${LIT_ARGS})
1003  foreach(param ${ARG_PARAMS})
1004    list(APPEND LIT_COMMAND --param ${param})
1005  endforeach()
1006  if (ARG_UNPARSED_ARGUMENTS)
1007    add_custom_target(${target}
1008      COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1009      COMMENT "${comment}"
1010      ${cmake_3_2_USES_TERMINAL}
1011      )
1012  else()
1013    add_custom_target(${target}
1014      COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1015    message(STATUS "${target} does nothing.")
1016  endif()
1017  if (ARG_DEPENDS)
1018    add_dependencies(${target} ${ARG_DEPENDS})
1019  endif()
1020
1021  # Tests should be excluded from "Build Solution".
1022  set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1023endfunction()
1024
1025# A function to add a set of lit test suites to be driven through 'check-*' targets.
1026function(add_lit_testsuite target comment)
1027  cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1028
1029  # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1030  if(NOT EXCLUDE_FROM_ALL)
1031    # Register the testsuites, params and depends for the global check rule.
1032    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1033    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1034    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1035    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1036  endif()
1037
1038  # Produce a specific suffixed check rule.
1039  add_lit_target(${target} ${comment}
1040    ${ARG_UNPARSED_ARGUMENTS}
1041    PARAMS ${ARG_PARAMS}
1042    DEPENDS ${ARG_DEPENDS}
1043    ARGS ${ARG_ARGS}
1044    )
1045endfunction()
1046
1047function(add_lit_testsuites project directory)
1048  if (NOT CMAKE_CONFIGURATION_TYPES)
1049    cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1050    file(GLOB_RECURSE litCfg ${directory}/lit*.cfg)
1051    set(lit_suites)
1052    foreach(f ${litCfg})
1053      get_filename_component(dir ${f} DIRECTORY)
1054      set(lit_suites ${lit_suites} ${dir})
1055    endforeach()
1056    list(REMOVE_DUPLICATES lit_suites)
1057    foreach(dir ${lit_suites})
1058      string(REPLACE ${directory} "" name_slash ${dir})
1059      if (name_slash)
1060        string(REPLACE "/" "-" name_slash ${name_slash})
1061        string(REPLACE "\\" "-" name_dashes ${name_slash})
1062        string(TOLOWER "${project}${name_dashes}" name_var)
1063        add_lit_target("check-${name_var}" "Running lit suite ${dir}"
1064          ${dir}
1065          PARAMS ${ARG_PARAMS}
1066          DEPENDS ${ARG_DEPENDS}
1067          ARGS ${ARG_ARGS}
1068        )
1069      endif()
1070    endforeach()
1071  endif()
1072endfunction()
1073
1074function(llvm_install_library_symlink name dest type)
1075  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1076  foreach(path ${CMAKE_MODULE_PATH})
1077    if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1078      set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1079      break()
1080    endif()
1081  endforeach()
1082
1083  set(component ${ARG_COMPONENT})
1084  if(NOT component)
1085    set(component ${name})
1086  endif()
1087
1088  set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1089  set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1090
1091  set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1092  if(WIN32 AND "${type}" STREQUAL "SHARED")
1093    set(output_dir bin)
1094  endif()
1095
1096  install(SCRIPT ${INSTALL_SYMLINK}
1097          CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1098          COMPONENT ${component})
1099
1100  if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1101    add_custom_target(install-${name}
1102                      DEPENDS ${name} ${dest} install-${dest}
1103                      COMMAND "${CMAKE_COMMAND}"
1104                              -DCMAKE_INSTALL_COMPONENT=${name}
1105                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1106  endif()
1107endfunction()
1108
1109function(llvm_install_symlink name dest)
1110  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
1111  foreach(path ${CMAKE_MODULE_PATH})
1112    if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1113      set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1114      break()
1115    endif()
1116  endforeach()
1117
1118  if(ARG_ALWAYS_GENERATE)
1119    set(component ${dest})
1120  else()
1121    set(component ${name})
1122  endif()
1123
1124  set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1125  set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1126
1127  install(SCRIPT ${INSTALL_SYMLINK}
1128          CODE "install_symlink(${full_name} ${full_dest} bin)"
1129          COMPONENT ${component})
1130
1131  if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1132    add_custom_target(install-${name}
1133                      DEPENDS ${name} ${dest} install-${dest}
1134                      COMMAND "${CMAKE_COMMAND}"
1135                              -DCMAKE_INSTALL_COMPONENT=${name}
1136                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1137  endif()
1138endfunction()
1139
1140function(add_llvm_tool_symlink name dest)
1141  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
1142  if(UNIX)
1143    set(LLVM_LINK_OR_COPY create_symlink)
1144    set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
1145  else()
1146    set(LLVM_LINK_OR_COPY copy)
1147    set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
1148  endif()
1149
1150  set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
1151
1152  if(ARG_ALWAYS_GENERATE)
1153    set_property(DIRECTORY APPEND PROPERTY
1154      ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1155    add_custom_command(TARGET ${dest} POST_BUILD
1156      COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1157  else()
1158    add_custom_command(OUTPUT ${output_path}
1159                     COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1160                     DEPENDS ${dest})
1161    add_custom_target(${name} ALL DEPENDS ${output_path})
1162    set_target_properties(${name} PROPERTIES FOLDER Tools)
1163
1164    # Make sure the parent tool is a toolchain tool, otherwise exclude this tool
1165    list(FIND LLVM_TOOLCHAIN_TOOLS ${dest} LLVM_IS_${dest}_TOOLCHAIN_TOOL)
1166    if (NOT LLVM_IS_${dest}_TOOLCHAIN_TOOL GREATER -1)
1167      set(LLVM_IS_${name}_TOOLCHAIN_TOOL ${LLVM_IS_${dest}_TOOLCHAIN_TOOL})
1168    else()
1169      list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
1170    endif()
1171
1172    # LLVM_IS_${name}_TOOLCHAIN_TOOL will only be greater than -1 if both this
1173    # tool and its parent tool are in LLVM_TOOLCHAIN_TOOLS
1174    if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1175      if( LLVM_BUILD_TOOLS )
1176        llvm_install_symlink(${name} ${dest})
1177      endif()
1178    endif()
1179  endif()
1180endfunction()
1181
1182function(llvm_externalize_debuginfo name)
1183  if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1184    return()
1185  endif()
1186
1187  if(APPLE)
1188    if(CMAKE_CXX_FLAGS MATCHES "-flto"
1189      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1190
1191      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1192      set_property(TARGET ${name} APPEND_STRING PROPERTY
1193        LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1194    endif()
1195    add_custom_command(TARGET ${name} POST_BUILD
1196      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
1197      COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
1198  else()
1199    message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
1200  endif()
1201endfunction()
1202