• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 targets can use to
12  # force EH
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)
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#   OBJECT
312#     Also create an OBJECT library target. Default if STATIC && SHARED.
313#   MODULE
314#     Target ${name} might not be created on unsupported platforms.
315#     Check with "if(TARGET ${name})".
316#   DISABLE_LLVM_LINK_LLVM_DYLIB
317#     Do not link this library to libLLVM, even if
318#     LLVM_LINK_LLVM_DYLIB is enabled.
319#   OUTPUT_NAME name
320#     Corresponds to OUTPUT_NAME in target properties.
321#   DEPENDS targets...
322#     Same semantics as add_dependencies().
323#   LINK_COMPONENTS components...
324#     Same as the variable LLVM_LINK_COMPONENTS.
325#   LINK_LIBS lib_targets...
326#     Same semantics as target_link_libraries().
327#   ADDITIONAL_HEADERS
328#     May specify header files for IDE generators.
329#   SONAME
330#     Should set SONAME link flags and create symlinks
331#   PLUGIN_TOOL
332#     The tool (i.e. cmake target) that this plugin will link against
333#   )
334function(llvm_add_library name)
335  cmake_parse_arguments(ARG
336    "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
337    "OUTPUT_NAME;PLUGIN_TOOL"
338    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
339    ${ARGN})
340  list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
341  if(ARG_ADDITIONAL_HEADERS)
342    # Pass through ADDITIONAL_HEADERS.
343    set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
344  endif()
345  if(ARG_OBJLIBS)
346    set(ALL_FILES ${ARG_OBJLIBS})
347  else()
348    llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
349  endif()
350
351  if(ARG_MODULE)
352    if(ARG_SHARED OR ARG_STATIC)
353      message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
354    endif()
355    # Plugins that link against a tool are allowed even when plugins in general are not
356    if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
357      message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
358      return()
359    endif()
360  else()
361    if(ARG_PLUGIN_TOOL)
362      message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
363    endif()
364    if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
365      set(ARG_SHARED TRUE)
366    endif()
367    if(NOT ARG_SHARED)
368      set(ARG_STATIC TRUE)
369    endif()
370  endif()
371
372  # Generate objlib
373  if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
374    # Generate an obj library for both targets.
375    set(obj_name "obj.${name}")
376    add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
377      ${ALL_FILES}
378      )
379    llvm_update_compile_flags(${obj_name})
380    set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
381
382    # Do add_dependencies(obj) later due to CMake issue 14747.
383    list(APPEND objlibs ${obj_name})
384
385    set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
386  endif()
387
388  if(ARG_SHARED AND ARG_STATIC)
389    # static
390    set(name_static "${name}_static")
391    if(ARG_OUTPUT_NAME)
392      set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
393    endif()
394    # DEPENDS has been appended to LLVM_COMMON_LIBS.
395    llvm_add_library(${name_static} STATIC
396      ${output_name}
397      OBJLIBS ${ALL_FILES} # objlib
398      LINK_LIBS ${ARG_LINK_LIBS}
399      LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
400      )
401    # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
402    set(ARG_STATIC)
403  endif()
404
405  if(ARG_MODULE)
406    add_library(${name} MODULE ${ALL_FILES})
407  elseif(ARG_SHARED)
408    add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
409    add_library(${name} SHARED ${ALL_FILES})
410  else()
411    add_library(${name} STATIC ${ALL_FILES})
412  endif()
413
414  if(DEFINED windows_resource_file)
415    set_windows_version_resource_properties(${name} ${windows_resource_file})
416    set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
417  endif()
418
419  set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
420  # $<TARGET_OBJECTS> doesn't require compile flags.
421  if(NOT obj_name)
422    llvm_update_compile_flags(${name})
423  endif()
424  add_link_opts( ${name} )
425  if(ARG_OUTPUT_NAME)
426    set_target_properties(${name}
427      PROPERTIES
428      OUTPUT_NAME ${ARG_OUTPUT_NAME}
429      )
430  endif()
431
432  if(ARG_MODULE)
433    set_target_properties(${name} PROPERTIES
434      PREFIX ""
435      SUFFIX ${LLVM_PLUGIN_EXT}
436      )
437  endif()
438
439  if(ARG_SHARED)
440    if(WIN32)
441      set_target_properties(${name} PROPERTIES
442        PREFIX ""
443        )
444    endif()
445  endif()
446
447  if(ARG_MODULE OR ARG_SHARED)
448    # Do not add -Dname_EXPORTS to the command-line when building files in this
449    # target. Doing so is actively harmful for the modules build because it
450    # creates extra module variants, and not useful because we don't use these
451    # macros.
452    set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
453
454    if (LLVM_EXPORTED_SYMBOL_FILE)
455      add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
456    endif()
457  endif()
458
459  if(ARG_SHARED AND UNIX)
460    if(NOT APPLE AND ARG_SONAME)
461      get_target_property(output_name ${name} OUTPUT_NAME)
462      if(${output_name} STREQUAL "output_name-NOTFOUND")
463        set(output_name ${name})
464      endif()
465      set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
466      set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
467      set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
468      llvm_install_library_symlink(${api_name} ${library_name} SHARED
469        COMPONENT ${name}
470        ALWAYS_GENERATE)
471      llvm_install_library_symlink(${output_name} ${library_name} SHARED
472        COMPONENT ${name}
473        ALWAYS_GENERATE)
474    endif()
475  endif()
476
477  if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
478    # On DLL platforms symbols are imported from the tool by linking against it.
479    set(llvm_libs ${ARG_PLUGIN_TOOL})
480  elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
481    if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
482      set(llvm_libs LLVM)
483    else()
484      llvm_map_components_to_libnames(llvm_libs
485       ${ARG_LINK_COMPONENTS}
486       ${LLVM_LINK_COMPONENTS}
487       )
488    endif()
489  else()
490    # Components have not been defined explicitly in CMake, so add the
491    # dependency information for this library as defined by LLVMBuild.
492    #
493    # It would be nice to verify that we have the dependencies for this library
494    # name, but using get_property(... SET) doesn't suffice to determine if a
495    # property has been set to an empty value.
496    get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
497  endif()
498
499  if(ARG_STATIC)
500    set(libtype INTERFACE)
501  else()
502    # We can use PRIVATE since SO knows its dependent libs.
503    set(libtype PRIVATE)
504  endif()
505
506  target_link_libraries(${name} ${libtype}
507      ${ARG_LINK_LIBS}
508      ${lib_deps}
509      ${llvm_libs}
510      )
511
512  if(LLVM_COMMON_DEPENDS)
513    add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
514    # Add dependencies also to objlibs.
515    # CMake issue 14747 --  add_dependencies() might be ignored to objlib's user.
516    foreach(objlib ${objlibs})
517      add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
518    endforeach()
519  endif()
520
521  if(ARG_SHARED OR ARG_MODULE)
522    llvm_externalize_debuginfo(${name})
523  endif()
524endfunction()
525
526macro(add_llvm_library name)
527  cmake_parse_arguments(ARG
528    "SHARED"
529    ""
530    ""
531    ${ARGN})
532  if( BUILD_SHARED_LIBS )
533    llvm_add_library(${name} SHARED ${ARGN})
534  else()
535    llvm_add_library(${name} ${ARGN})
536  endif()
537  # The gtest libraries should not be installed or exported as a target
538  if ("${name}" STREQUAL gtest OR "${name}" STREQUAL gtest_main)
539    set(_is_gtest TRUE)
540  else()
541    set(_is_gtest FALSE)
542    set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
543  endif()
544
545  if( EXCLUDE_FROM_ALL )
546    set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
547  elseif(NOT _is_gtest)
548    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
549      set(install_dir lib${LLVM_LIBDIR_SUFFIX})
550      if(ARG_SHARED OR BUILD_SHARED_LIBS)
551        if(WIN32 OR CYGWIN OR MINGW)
552          set(install_type RUNTIME)
553          set(install_dir bin)
554        else()
555          set(install_type LIBRARY)
556        endif()
557      else()
558        set(install_type ARCHIVE)
559      endif()
560
561      install(TARGETS ${name}
562            EXPORT LLVMExports
563            ${install_type} DESTINATION ${install_dir}
564            COMPONENT ${name})
565
566      if (NOT CMAKE_CONFIGURATION_TYPES)
567        add_custom_target(install-${name}
568                          DEPENDS ${name}
569                          COMMAND "${CMAKE_COMMAND}"
570                                  -DCMAKE_INSTALL_COMPONENT=${name}
571                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
572      endif()
573    endif()
574    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
575  endif()
576  set_target_properties(${name} PROPERTIES FOLDER "Libraries")
577endmacro(add_llvm_library name)
578
579macro(add_llvm_loadable_module name)
580  llvm_add_library(${name} MODULE ${ARGN})
581  if(NOT TARGET ${name})
582    # Add empty "phony" target
583    add_custom_target(${name})
584  else()
585    if( EXCLUDE_FROM_ALL )
586      set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
587    else()
588      if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
589        if(WIN32 OR CYGWIN)
590          # DLL platform
591          set(dlldir "bin")
592        else()
593          set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
594        endif()
595        install(TARGETS ${name}
596          EXPORT LLVMExports
597          LIBRARY DESTINATION ${dlldir}
598          ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
599      endif()
600      set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
601    endif()
602  endif()
603
604  set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
605endmacro(add_llvm_loadable_module name)
606
607
608macro(add_llvm_executable name)
609  cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO" "" "" ${ARGN})
610  llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
611
612  # Generate objlib
613  if(LLVM_ENABLE_OBJLIB)
614    # Generate an obj library for both targets.
615    set(obj_name "obj.${name}")
616    add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
617      ${ALL_FILES}
618      )
619    llvm_update_compile_flags(${obj_name})
620    set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
621
622    set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
623  endif()
624
625  add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
626
627  if(XCODE)
628    # Note: the dummy.cpp source file provides no definitions. However,
629    # it forces Xcode to properly link the static library.
630    list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
631  endif()
632
633  if( EXCLUDE_FROM_ALL )
634    add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
635  else()
636    add_executable(${name} ${ALL_FILES})
637  endif()
638
639  if(DEFINED windows_resource_file)
640    set_windows_version_resource_properties(${name} ${windows_resource_file})
641  endif()
642
643  # $<TARGET_OBJECTS> doesn't require compile flags.
644  if(NOT LLVM_ENABLE_OBJLIB)
645    llvm_update_compile_flags(${name})
646  endif()
647  add_link_opts( ${name} )
648
649  # Do not add -Dname_EXPORTS to the command-line when building files in this
650  # target. Doing so is actively harmful for the modules build because it
651  # creates extra module variants, and not useful because we don't use these
652  # macros.
653  set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
654
655  if (LLVM_EXPORTED_SYMBOL_FILE)
656    add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
657  endif(LLVM_EXPORTED_SYMBOL_FILE)
658
659  if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
660    set(USE_SHARED USE_SHARED)
661  endif()
662
663  set(EXCLUDE_FROM_ALL OFF)
664  set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
665  llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
666  if( LLVM_COMMON_DEPENDS )
667    add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
668  endif( LLVM_COMMON_DEPENDS )
669
670  if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
671    llvm_externalize_debuginfo(${name})
672  endif()
673  if (PTHREAD_LIB)
674    # libpthreads overrides some standard library symbols, so main
675    # executable must be linked with it in order to provide consistent
676    # API for all shared libaries loaded by this executable.
677    target_link_libraries(${name} ${PTHREAD_LIB})
678  endif()
679endmacro(add_llvm_executable name)
680
681function(export_executable_symbols target)
682  if (LLVM_EXPORTED_SYMBOL_FILE)
683    # The symbol file should contain the symbols we want the executable to
684    # export
685    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
686  elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
687    # Extract the symbols to export from the static libraries that the
688    # executable links against.
689    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
690    set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
691    # We need to consider not just the direct link dependencies, but also the
692    # transitive link dependencies. Do this by starting with the set of direct
693    # dependencies, then the dependencies of those dependencies, and so on.
694    get_target_property(new_libs ${target} LINK_LIBRARIES)
695    set(link_libs ${new_libs})
696    while(NOT "${new_libs}" STREQUAL "")
697      foreach(lib ${new_libs})
698        if(TARGET ${lib})
699          get_target_property(lib_type ${lib} TYPE)
700          if("${lib_type}" STREQUAL "STATIC_LIBRARY")
701            list(APPEND static_libs ${lib})
702          else()
703            list(APPEND other_libs ${lib})
704          endif()
705          get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
706          foreach(transitive_lib ${transitive_libs})
707            list(FIND link_libs ${transitive_lib} idx)
708            if(TARGET ${transitive_lib} AND idx EQUAL -1)
709              list(APPEND newer_libs ${transitive_lib})
710              list(APPEND link_libs ${transitive_lib})
711            endif()
712          endforeach(transitive_lib)
713        endif()
714      endforeach(lib)
715      set(new_libs ${newer_libs})
716      set(newer_libs "")
717    endwhile()
718    if (MSVC)
719      set(mangling microsoft)
720    else()
721      set(mangling itanium)
722    endif()
723    add_custom_command(OUTPUT ${exported_symbol_file}
724                       COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
725                       WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
726                       DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
727                       VERBATIM
728                       COMMENT "Generating export list for ${target}")
729    add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
730    # If something links against this executable then we want a
731    # transitive link against only the libraries whose symbols
732    # we aren't exporting.
733    set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
734    # The default import library suffix that cmake uses for cygwin/mingw is
735    # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
736    # where the import libraries of both get named libclang.dll.a. Use a suffix
737    # of ".exe.a" to avoid this.
738    if(CYGWIN OR MINGW)
739      set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
740    endif()
741  elseif(NOT (WIN32 OR CYGWIN))
742    # On Windows auto-exporting everything doesn't work because of the limit on
743    # the size of the exported symbol table, but on other platforms we can do
744    # it without any trouble.
745    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
746    if (APPLE)
747      set_property(TARGET ${target} APPEND_STRING PROPERTY
748        LINK_FLAGS " -rdynamic")
749    endif()
750  endif()
751endfunction()
752
753if(NOT LLVM_TOOLCHAIN_TOOLS)
754  set (LLVM_TOOLCHAIN_TOOLS
755    llvm-ar
756    llvm-ranlib
757    llvm-lib
758    llvm-objdump
759    )
760endif()
761
762macro(add_llvm_tool name)
763  if( NOT LLVM_BUILD_TOOLS )
764    set(EXCLUDE_FROM_ALL ON)
765  endif()
766  add_llvm_executable(${name} ${ARGN})
767
768  list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
769  if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
770    if( LLVM_BUILD_TOOLS )
771      install(TARGETS ${name}
772              EXPORT LLVMExports
773              RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
774              COMPONENT ${name})
775
776      if (NOT CMAKE_CONFIGURATION_TYPES)
777        add_custom_target(install-${name}
778                          DEPENDS ${name}
779                          COMMAND "${CMAKE_COMMAND}"
780                                  -DCMAKE_INSTALL_COMPONENT=${name}
781                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
782      endif()
783    endif()
784  endif()
785  if( LLVM_BUILD_TOOLS )
786    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
787  endif()
788  set_target_properties(${name} PROPERTIES FOLDER "Tools")
789endmacro(add_llvm_tool name)
790
791
792macro(add_llvm_example name)
793  if( NOT LLVM_BUILD_EXAMPLES )
794    set(EXCLUDE_FROM_ALL ON)
795  endif()
796  add_llvm_executable(${name} ${ARGN})
797  if( LLVM_BUILD_EXAMPLES )
798    install(TARGETS ${name} RUNTIME DESTINATION examples)
799  endif()
800  set_target_properties(${name} PROPERTIES FOLDER "Examples")
801endmacro(add_llvm_example name)
802
803# This is a macro that is used to create targets for executables that are needed
804# for development, but that are not intended to be installed by default.
805macro(add_llvm_utility name)
806  if ( NOT LLVM_BUILD_UTILS )
807    set(EXCLUDE_FROM_ALL ON)
808  endif()
809
810  add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
811  set_target_properties(${name} PROPERTIES FOLDER "Utils")
812  if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
813    install (TARGETS ${name}
814      RUNTIME DESTINATION bin
815      COMPONENT ${name})
816    if (NOT CMAKE_CONFIGURATION_TYPES)
817      add_custom_target(install-${name}
818                        DEPENDS ${name}
819                        COMMAND "${CMAKE_COMMAND}"
820                                -DCMAKE_INSTALL_COMPONENT=${name}
821                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
822    endif()
823  endif()
824endmacro(add_llvm_utility name)
825
826
827macro(add_llvm_target target_name)
828  include_directories(BEFORE
829    ${CMAKE_CURRENT_BINARY_DIR}
830    ${CMAKE_CURRENT_SOURCE_DIR})
831  add_llvm_library(LLVM${target_name} ${ARGN})
832  set( CURRENT_LLVM_TARGET LLVM${target_name} )
833endmacro(add_llvm_target)
834
835function(canonicalize_tool_name name output)
836  string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
837  string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
838  string(TOUPPER ${nameUNDERSCORE} nameUPPER)
839  set(${output} "${nameUPPER}" PARENT_SCOPE)
840endfunction(canonicalize_tool_name)
841
842# Custom add_subdirectory wrapper
843# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
844# path if it differs from the name.
845macro(add_llvm_subdirectory project type name)
846  set(add_llvm_external_dir "${ARGN}")
847  if("${add_llvm_external_dir}" STREQUAL "")
848    set(add_llvm_external_dir ${name})
849  endif()
850  canonicalize_tool_name(${name} nameUPPER)
851  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
852    # Treat it as in-tree subproject.
853    option(${project}_${type}_${nameUPPER}_BUILD
854           "Whether to build ${name} as part of ${project}" On)
855    mark_as_advanced(${project}_${type}_${name}_BUILD)
856    if(${project}_${type}_${nameUPPER}_BUILD)
857      add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
858      # Don't process it in add_llvm_implicit_projects().
859      set(${project}_${type}_${nameUPPER}_BUILD OFF)
860    endif()
861  else()
862    set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
863      "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
864      CACHE PATH "Path to ${name} source directory")
865    set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
866    if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
867      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
868    endif()
869    if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
870      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
871    endif()
872    option(${project}_${type}_${nameUPPER}_BUILD
873      "Whether to build ${name} as part of LLVM"
874      ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
875    if (${project}_${type}_${nameUPPER}_BUILD)
876      if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
877        add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
878      elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
879        message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
880      endif()
881      # FIXME: It'd be redundant.
882      set(${project}_${type}_${nameUPPER}_BUILD Off)
883    endif()
884  endif()
885endmacro()
886
887# Add external project that may want to be built as part of llvm such as Clang,
888# lld, and Polly. This adds two options. One for the source directory of the
889# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
890# enable or disable building it with everything else.
891# Additional parameter can be specified as the name of directory.
892macro(add_llvm_external_project name)
893  add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
894endmacro()
895
896macro(add_llvm_tool_subdirectory name)
897  add_llvm_external_project(${name})
898endmacro(add_llvm_tool_subdirectory)
899
900function(get_project_name_from_src_var var output)
901  string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
902         MACHED_TOOL "${var}")
903  if(MACHED_TOOL)
904    set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
905  else()
906    set(${output} PARENT_SCOPE)
907  endif()
908endfunction()
909
910function(create_subdirectory_options project type)
911  file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
912  foreach(dir ${sub-dirs})
913    if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
914      canonicalize_tool_name(${dir} name)
915      option(${project}_${type}_${name}_BUILD
916           "Whether to build ${name} as part of ${project}" On)
917      mark_as_advanced(${project}_${type}_${name}_BUILD)
918    endif()
919  endforeach()
920endfunction(create_subdirectory_options)
921
922function(create_llvm_tool_options)
923  create_subdirectory_options(LLVM TOOL)
924endfunction(create_llvm_tool_options)
925
926function(llvm_add_implicit_projects project)
927  set(list_of_implicit_subdirs "")
928  file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
929  foreach(dir ${sub-dirs})
930    if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
931      canonicalize_tool_name(${dir} name)
932      if (${project}_TOOL_${name}_BUILD)
933        get_filename_component(fn "${dir}" NAME)
934        list(APPEND list_of_implicit_subdirs "${fn}")
935      endif()
936    endif()
937  endforeach()
938
939  foreach(external_proj ${list_of_implicit_subdirs})
940    add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
941  endforeach()
942endfunction(llvm_add_implicit_projects)
943
944function(add_llvm_implicit_projects)
945  llvm_add_implicit_projects(LLVM)
946endfunction(add_llvm_implicit_projects)
947
948# Generic support for adding a unittest.
949function(add_unittest test_suite test_name)
950  if( NOT LLVM_BUILD_TESTS )
951    set(EXCLUDE_FROM_ALL ON)
952  endif()
953
954  include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
955  if (NOT LLVM_ENABLE_THREADS)
956    list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
957  endif ()
958
959  if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
960    list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
961  endif ()
962
963  set(LLVM_REQUIRES_RTTI OFF)
964
965  list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
966  add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO ${ARGN})
967  set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
968  set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
969  # libpthreads overrides some standard library symbols, so main
970  # executable must be linked with it in order to provide consistent
971  # API for all shared libaries loaded by this executable.
972  target_link_libraries(${test_name} gtest_main gtest ${PTHREAD_LIB})
973
974  add_dependencies(${test_suite} ${test_name})
975  get_target_property(test_suite_folder ${test_suite} FOLDER)
976  if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
977    set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
978  endif ()
979endfunction()
980
981function(llvm_add_go_executable binary pkgpath)
982  cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
983
984  if(LLVM_BINDINGS MATCHES "go")
985    # FIXME: This should depend only on the libraries Go needs.
986    get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
987    set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
988    set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
989    set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
990    set(cppflags "")
991    get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
992    foreach(d ${include_dirs})
993      set(cppflags "${cppflags} -I${d}")
994    endforeach(d)
995    set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
996    add_custom_command(OUTPUT ${binpath}
997      COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
998              ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
999      DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1000              ${llvmlibs} ${ARG_DEPENDS}
1001      COMMENT "Building Go executable ${binary}"
1002      VERBATIM)
1003    if (ARG_ALL)
1004      add_custom_target(${binary} ALL DEPENDS ${binpath})
1005    else()
1006      add_custom_target(${binary} DEPENDS ${binpath})
1007    endif()
1008  endif()
1009endfunction()
1010
1011# This function provides an automatic way to 'configure'-like generate a file
1012# based on a set of common and custom variables, specifically targeting the
1013# variables needed for the 'lit.site.cfg' files. This function bundles the
1014# common variables that any Lit instance is likely to need, and custom
1015# variables can be passed in.
1016function(configure_lit_site_cfg input output)
1017  foreach(c ${LLVM_TARGETS_TO_BUILD})
1018    set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1019  endforeach(c)
1020  set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1021
1022  set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1023
1024  # Configuration-time: See Unit/lit.site.cfg.in
1025  if (CMAKE_CFG_INTDIR STREQUAL ".")
1026    set(LLVM_BUILD_MODE ".")
1027  else ()
1028    set(LLVM_BUILD_MODE "%(build_mode)s")
1029  endif ()
1030
1031  # They below might not be the build tree but provided binary tree.
1032  set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1033  set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1034  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
1035  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
1036
1037  # SHLIBDIR points the build tree.
1038  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1039
1040  set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1041  # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1042  # plugins. We may rename it.
1043  if(LLVM_ENABLE_PLUGINS)
1044    set(ENABLE_SHARED "1")
1045  else()
1046    set(ENABLE_SHARED "0")
1047  endif()
1048
1049  if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1050    set(ENABLE_ASSERTIONS "1")
1051  else()
1052    set(ENABLE_ASSERTIONS "0")
1053  endif()
1054
1055  set(HOST_OS ${CMAKE_SYSTEM_NAME})
1056  set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1057
1058  set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1059  set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1060  set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1061
1062  set(LIT_SITE_CFG_IN_HEADER  "## Autogenerated from ${input}\n## Do not edit!")
1063
1064  configure_file(${input} ${output} @ONLY)
1065endfunction()
1066
1067# A raw function to create a lit target. This is used to implement the testuite
1068# management functions.
1069function(add_lit_target target comment)
1070  cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1071  set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1072  separate_arguments(LIT_ARGS)
1073  if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1074    list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1075  endif ()
1076  if (LLVM_MAIN_SRC_DIR)
1077    set (LIT_COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
1078  else()
1079    find_program(LIT_COMMAND llvm-lit)
1080  endif ()
1081  list(APPEND LIT_COMMAND ${LIT_ARGS})
1082  foreach(param ${ARG_PARAMS})
1083    list(APPEND LIT_COMMAND --param ${param})
1084  endforeach()
1085  if (ARG_UNPARSED_ARGUMENTS)
1086    add_custom_target(${target}
1087      COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1088      COMMENT "${comment}"
1089      USES_TERMINAL
1090      )
1091  else()
1092    add_custom_target(${target}
1093      COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1094    message(STATUS "${target} does nothing.")
1095  endif()
1096  if (ARG_DEPENDS)
1097    add_dependencies(${target} ${ARG_DEPENDS})
1098  endif()
1099
1100  # Tests should be excluded from "Build Solution".
1101  set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1102endfunction()
1103
1104# A function to add a set of lit test suites to be driven through 'check-*' targets.
1105function(add_lit_testsuite target comment)
1106  cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1107
1108  # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1109  if(NOT EXCLUDE_FROM_ALL)
1110    # Register the testsuites, params and depends for the global check rule.
1111    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1112    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1113    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1114    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1115  endif()
1116
1117  # Produce a specific suffixed check rule.
1118  add_lit_target(${target} ${comment}
1119    ${ARG_UNPARSED_ARGUMENTS}
1120    PARAMS ${ARG_PARAMS}
1121    DEPENDS ${ARG_DEPENDS}
1122    ARGS ${ARG_ARGS}
1123    )
1124endfunction()
1125
1126function(add_lit_testsuites project directory)
1127  if (NOT CMAKE_CONFIGURATION_TYPES)
1128    cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1129
1130    # Search recursively for test directories by assuming anything not
1131    # in a directory called Inputs contains tests.
1132    file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1133    foreach(lit_suite ${to_process})
1134      if(NOT IS_DIRECTORY ${lit_suite})
1135        continue()
1136      endif()
1137      string(FIND ${lit_suite} Inputs is_inputs)
1138      if (NOT is_inputs EQUAL -1)
1139        continue()
1140      endif()
1141
1142      # Create a check- target for the directory.
1143      string(REPLACE ${directory} "" name_slash ${lit_suite})
1144      if (name_slash)
1145        string(REPLACE "/" "-" name_slash ${name_slash})
1146        string(REPLACE "\\" "-" name_dashes ${name_slash})
1147        string(TOLOWER "${project}${name_dashes}" name_var)
1148        add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1149          ${lit_suite}
1150          PARAMS ${ARG_PARAMS}
1151          DEPENDS ${ARG_DEPENDS}
1152          ARGS ${ARG_ARGS}
1153        )
1154      endif()
1155    endforeach()
1156  endif()
1157endfunction()
1158
1159function(llvm_install_library_symlink name dest type)
1160  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1161  foreach(path ${CMAKE_MODULE_PATH})
1162    if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1163      set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1164      break()
1165    endif()
1166  endforeach()
1167
1168  set(component ${ARG_COMPONENT})
1169  if(NOT component)
1170    set(component ${name})
1171  endif()
1172
1173  set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1174  set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1175
1176  set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1177  if(WIN32 AND "${type}" STREQUAL "SHARED")
1178    set(output_dir bin)
1179  endif()
1180
1181  install(SCRIPT ${INSTALL_SYMLINK}
1182          CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1183          COMPONENT ${component})
1184
1185  if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1186    add_custom_target(install-${name}
1187                      DEPENDS ${name} ${dest} install-${dest}
1188                      COMMAND "${CMAKE_COMMAND}"
1189                              -DCMAKE_INSTALL_COMPONENT=${name}
1190                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1191  endif()
1192endfunction()
1193
1194function(llvm_install_symlink name dest)
1195  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
1196  foreach(path ${CMAKE_MODULE_PATH})
1197    if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1198      set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1199      break()
1200    endif()
1201  endforeach()
1202
1203  if(ARG_ALWAYS_GENERATE)
1204    set(component ${dest})
1205  else()
1206    set(component ${name})
1207  endif()
1208
1209  set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1210  set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1211
1212  install(SCRIPT ${INSTALL_SYMLINK}
1213          CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1214          COMPONENT ${component})
1215
1216  if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1217    add_custom_target(install-${name}
1218                      DEPENDS ${name} ${dest} install-${dest}
1219                      COMMAND "${CMAKE_COMMAND}"
1220                              -DCMAKE_INSTALL_COMPONENT=${name}
1221                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1222  endif()
1223endfunction()
1224
1225function(add_llvm_tool_symlink name dest)
1226  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
1227  if(UNIX)
1228    set(LLVM_LINK_OR_COPY create_symlink)
1229    set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
1230  else()
1231    set(LLVM_LINK_OR_COPY copy)
1232    set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
1233  endif()
1234
1235  set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
1236
1237  if(ARG_ALWAYS_GENERATE)
1238    set_property(DIRECTORY APPEND PROPERTY
1239      ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1240    add_custom_command(TARGET ${dest} POST_BUILD
1241      COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1242  else()
1243    add_custom_command(OUTPUT ${output_path}
1244                     COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1245                     DEPENDS ${dest})
1246    add_custom_target(${name} ALL DEPENDS ${output_path})
1247    set_target_properties(${name} PROPERTIES FOLDER Tools)
1248
1249    # Make sure the parent tool is a toolchain tool, otherwise exclude this tool
1250    list(FIND LLVM_TOOLCHAIN_TOOLS ${dest} LLVM_IS_${dest}_TOOLCHAIN_TOOL)
1251    if (NOT LLVM_IS_${dest}_TOOLCHAIN_TOOL GREATER -1)
1252      set(LLVM_IS_${name}_TOOLCHAIN_TOOL ${LLVM_IS_${dest}_TOOLCHAIN_TOOL})
1253    else()
1254      list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
1255    endif()
1256
1257    # LLVM_IS_${name}_TOOLCHAIN_TOOL will only be greater than -1 if both this
1258    # tool and its parent tool are in LLVM_TOOLCHAIN_TOOLS
1259    if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1260      if( LLVM_BUILD_TOOLS )
1261        llvm_install_symlink(${name} ${dest})
1262      endif()
1263    endif()
1264  endif()
1265endfunction()
1266
1267function(llvm_externalize_debuginfo name)
1268  if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1269    return()
1270  endif()
1271
1272  if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1273    set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1274  endif()
1275
1276  if(APPLE)
1277    if(CMAKE_CXX_FLAGS MATCHES "-flto"
1278      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1279
1280      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1281      set_property(TARGET ${name} APPEND_STRING PROPERTY
1282        LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1283    endif()
1284    add_custom_command(TARGET ${name} POST_BUILD
1285      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
1286      ${strip_command}
1287      )
1288  else()
1289    message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
1290  endif()
1291endfunction()
1292