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    if(MSVC)
19      list(APPEND LLVM_COMPILE_FLAGS "/EHsc")
20    endif()
21  else()
22    if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
23      list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
24    elseif(MSVC)
25      list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
26      list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
27    endif()
28  endif()
29
30  # LLVM_REQUIRES_RTTI is an internal flag that individual
31  # targets can use to force RTTI
32  set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "")
33  if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI))
34    set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "")
35    list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
36    if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
37      list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
38    elseif (MSVC)
39      list(APPEND LLVM_COMPILE_FLAGS "/GR-")
40    endif ()
41  elseif(MSVC)
42    list(APPEND LLVM_COMPILE_FLAGS "/GR")
43  endif()
44
45  # Assume that;
46  #   - LLVM_COMPILE_FLAGS is list.
47  #   - PROPERTY COMPILE_FLAGS is string.
48  string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}")
49
50  if(update_src_props)
51    foreach(fn ${sources})
52      get_filename_component(suf ${fn} EXT)
53      if("${suf}" STREQUAL ".cpp")
54        set_property(SOURCE ${fn} APPEND_STRING PROPERTY
55          COMPILE_FLAGS "${target_compile_flags}")
56      endif()
57    endforeach()
58  else()
59    # Update target props, since all sources are C++.
60    set_property(TARGET ${name} APPEND_STRING PROPERTY
61      COMPILE_FLAGS "${target_compile_flags}")
62  endif()
63
64  set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
65endfunction()
66
67function(add_llvm_symbol_exports target_name export_file)
68  if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
69    set(native_export_file "${target_name}.exports")
70    add_custom_command(OUTPUT ${native_export_file}
71      COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
72      DEPENDS ${export_file}
73      VERBATIM
74      COMMENT "Creating export file for ${target_name}")
75    set_property(TARGET ${target_name} APPEND_STRING PROPERTY
76                 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
77  elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
78    set_property(TARGET ${target_name} APPEND_STRING PROPERTY
79                 LINK_FLAGS " -Wl,-bE:${export_file}")
80  elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
81    # Gold and BFD ld require a version script rather than a plain list.
82    set(native_export_file "${target_name}.exports")
83    # FIXME: Don't write the "local:" line on OpenBSD.
84    # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M)
85    add_custom_command(OUTPUT ${native_export_file}
86      COMMAND echo "LLVM_${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} {" > ${native_export_file}
87      COMMAND grep -q "[[:alnum:]]" ${export_file} && echo "  global:" >> ${native_export_file} || :
88      COMMAND sed -e "s/$/;/" -e "s/^/    /" < ${export_file} >> ${native_export_file}
89      COMMAND echo "  local: *;" >> ${native_export_file}
90      COMMAND echo "};" >> ${native_export_file}
91      DEPENDS ${export_file}
92      VERBATIM
93      COMMENT "Creating export file for ${target_name}")
94    if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
95      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
96                   LINK_FLAGS "  -Wl,-M,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
97    else()
98      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
99                   LINK_FLAGS "  -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
100    endif()
101  else()
102    set(native_export_file "${target_name}.def")
103
104    add_custom_command(OUTPUT ${native_export_file}
105      COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
106        < ${export_file} > ${native_export_file}
107      DEPENDS ${export_file}
108      VERBATIM
109      COMMENT "Creating export file for ${target_name}")
110    set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
111    if(MSVC)
112      set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"")
113    endif()
114    set_property(TARGET ${target_name} APPEND_STRING PROPERTY
115                 LINK_FLAGS " ${export_file_linker_flag}")
116  endif()
117
118  add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
119  set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
120
121  get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
122  foreach(src ${srcs})
123    get_filename_component(extension ${src} EXT)
124    if(extension STREQUAL ".cpp")
125      set(first_source_file ${src})
126      break()
127    endif()
128  endforeach()
129
130  # Force re-linking when the exports file changes. Actually, it
131  # forces recompilation of the source file. The LINK_DEPENDS target
132  # property only works for makefile-based generators.
133  # FIXME: This is not safe because this will create the same target
134  # ${native_export_file} in several different file:
135  # - One where we emitted ${target_name}_exports
136  # - One where we emitted the build command for the following object.
137  # set_property(SOURCE ${first_source_file} APPEND PROPERTY
138  #   OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
139
140  set_property(DIRECTORY APPEND
141    PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
142
143  add_dependencies(${target_name} ${target_name}_exports)
144
145  # Add dependency to *_exports later -- CMake issue 14747
146  list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports)
147  set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
148endfunction(add_llvm_symbol_exports)
149
150if(NOT WIN32 AND NOT APPLE)
151  execute_process(
152    COMMAND ${CMAKE_C_COMPILER} -Wl,--version
153    OUTPUT_VARIABLE stdout
154    ERROR_QUIET
155    )
156  if("${stdout}" MATCHES "GNU gold")
157    set(LLVM_LINKER_IS_GOLD ON)
158  endif()
159endif()
160
161function(add_link_opts target_name)
162  # Don't use linker optimizations in debug builds since it slows down the
163  # linker in a context where the optimizations are not important.
164  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
165
166    # Pass -O3 to the linker. This enabled different optimizations on different
167    # linkers.
168    if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|SunOS|AIX" OR WIN32))
169      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
170                   LINK_FLAGS " -Wl,-O3")
171    endif()
172
173    if(LLVM_LINKER_IS_GOLD)
174      # With gold gc-sections is always safe.
175      set_property(TARGET ${target_name} APPEND_STRING PROPERTY
176                   LINK_FLAGS " -Wl,--gc-sections")
177      # Note that there is a bug with -Wl,--icf=safe so it is not safe
178      # to enable. See https://sourceware.org/bugzilla/show_bug.cgi?id=17704.
179    endif()
180
181    if(NOT LLVM_NO_DEAD_STRIP)
182      if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
183        # ld64's implementation of -dead_strip breaks tools that use plugins.
184        set_property(TARGET ${target_name} APPEND_STRING PROPERTY
185                     LINK_FLAGS " -Wl,-dead_strip")
186      elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
187        set_property(TARGET ${target_name} APPEND_STRING PROPERTY
188                     LINK_FLAGS " -Wl,-z -Wl,discard-unused=sections")
189      elseif(NOT WIN32 AND NOT LLVM_LINKER_IS_GOLD)
190        # Object files are compiled with -ffunction-data-sections.
191        # Versions of bfd ld < 2.23.1 have a bug in --gc-sections that breaks
192        # tools that use plugins. Always pass --gc-sections once we require
193        # a newer linker.
194        set_property(TARGET ${target_name} APPEND_STRING PROPERTY
195                     LINK_FLAGS " -Wl,--gc-sections")
196      endif()
197    endif()
198  endif()
199endfunction(add_link_opts)
200
201# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
202# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
203# or a certain builder, for eaxample, msbuild.exe, would be confused.
204function(set_output_directory target)
205  cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN})
206
207  # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY.
208  # It affects output of add_library(MODULE).
209  if(WIN32 OR CYGWIN)
210    # DLL platform
211    set(module_dir ${ARG_BINARY_DIR})
212  else()
213    set(module_dir ${ARG_LIBRARY_DIR})
214  endif()
215  if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
216    foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
217      string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
218      if(ARG_BINARY_DIR)
219        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR})
220        set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
221      endif()
222      if(ARG_LIBRARY_DIR)
223        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR})
224        set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
225      endif()
226      if(module_dir)
227        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir})
228        set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
229      endif()
230    endforeach()
231  else()
232    if(ARG_BINARY_DIR)
233      set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR})
234    endif()
235    if(ARG_LIBRARY_DIR)
236      set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR})
237    endif()
238    if(module_dir)
239      set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir})
240    endif()
241  endif()
242endfunction()
243
244# If on Windows and building with MSVC, add the resource script containing the
245# VERSIONINFO data to the project.  This embeds version resource information
246# into the output .exe or .dll.
247# TODO: Enable for MinGW Windows builds too.
248#
249function(add_windows_version_resource_file OUT_VAR)
250  set(sources ${ARGN})
251  if (MSVC)
252    set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
253    if(EXISTS ${resource_file})
254      set(sources ${sources} ${resource_file})
255      source_group("Resource Files" ${resource_file})
256      set(windows_resource_file ${resource_file} PARENT_SCOPE)
257    endif()
258  endif(MSVC)
259
260  set(${OUT_VAR} ${sources} PARENT_SCOPE)
261endfunction(add_windows_version_resource_file)
262
263# set_windows_version_resource_properties(name resource_file...
264#   VERSION_MAJOR int
265#     Optional major version number (defaults to LLVM_VERSION_MAJOR)
266#   VERSION_MINOR int
267#     Optional minor version number (defaults to LLVM_VERSION_MINOR)
268#   VERSION_PATCHLEVEL int
269#     Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
270#   VERSION_STRING
271#     Optional version string (defaults to PACKAGE_VERSION)
272#   PRODUCT_NAME
273#     Optional product name string (defaults to "LLVM")
274#   )
275function(set_windows_version_resource_properties name resource_file)
276  cmake_parse_arguments(ARG
277    ""
278    "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
279    ""
280    ${ARGN})
281
282  if (NOT DEFINED ARG_VERSION_MAJOR)
283    set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
284  endif()
285
286  if (NOT DEFINED ARG_VERSION_MINOR)
287    set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
288  endif()
289
290  if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
291    set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
292  endif()
293
294  if (NOT DEFINED ARG_VERSION_STRING)
295    set(ARG_VERSION_STRING ${PACKAGE_VERSION})
296  endif()
297
298  if (NOT DEFINED ARG_PRODUCT_NAME)
299    set(ARG_PRODUCT_NAME "LLVM")
300  endif()
301
302  set_property(SOURCE ${resource_file}
303               PROPERTY COMPILE_FLAGS /nologo)
304  set_property(SOURCE ${resource_file}
305               PROPERTY COMPILE_DEFINITIONS
306               "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
307               "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
308               "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
309               "RC_VERSION_FIELD_4=0"
310               "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
311               "RC_INTERNAL_NAME=\"${name}\""
312               "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
313               "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
314endfunction(set_windows_version_resource_properties)
315
316# llvm_add_library(name sources...
317#   SHARED;STATIC
318#     STATIC by default w/o BUILD_SHARED_LIBS.
319#     SHARED by default w/  BUILD_SHARED_LIBS.
320#   OBJECT
321#     Also create an OBJECT library target. Default if STATIC && SHARED.
322#   MODULE
323#     Target ${name} might not be created on unsupported platforms.
324#     Check with "if(TARGET ${name})".
325#   DISABLE_LLVM_LINK_LLVM_DYLIB
326#     Do not link this library to libLLVM, even if
327#     LLVM_LINK_LLVM_DYLIB is enabled.
328#   OUTPUT_NAME name
329#     Corresponds to OUTPUT_NAME in target properties.
330#   DEPENDS targets...
331#     Same semantics as add_dependencies().
332#   LINK_COMPONENTS components...
333#     Same as the variable LLVM_LINK_COMPONENTS.
334#   LINK_LIBS lib_targets...
335#     Same semantics as target_link_libraries().
336#   ADDITIONAL_HEADERS
337#     May specify header files for IDE generators.
338#   SONAME
339#     Should set SONAME link flags and create symlinks
340#   PLUGIN_TOOL
341#     The tool (i.e. cmake target) that this plugin will link against
342#   )
343function(llvm_add_library name)
344  cmake_parse_arguments(ARG
345    "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME"
346    "OUTPUT_NAME;PLUGIN_TOOL"
347    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
348    ${ARGN})
349  list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
350  if(ARG_ADDITIONAL_HEADERS)
351    # Pass through ADDITIONAL_HEADERS.
352    set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS})
353  endif()
354  if(ARG_OBJLIBS)
355    set(ALL_FILES ${ARG_OBJLIBS})
356  else()
357    llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
358  endif()
359
360  if(ARG_MODULE)
361    if(ARG_SHARED OR ARG_STATIC)
362      message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
363    endif()
364    # Plugins that link against a tool are allowed even when plugins in general are not
365    if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS))
366      message(STATUS "${name} ignored -- Loadable modules not supported on this platform.")
367      return()
368    endif()
369  else()
370    if(ARG_PLUGIN_TOOL)
371      message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.")
372    endif()
373    if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
374      set(ARG_SHARED TRUE)
375    endif()
376    if(NOT ARG_SHARED)
377      set(ARG_STATIC TRUE)
378    endif()
379  endif()
380
381  # Generate objlib
382  if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
383    # Generate an obj library for both targets.
384    set(obj_name "obj.${name}")
385    add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
386      ${ALL_FILES}
387      )
388    llvm_update_compile_flags(${obj_name})
389    set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
390
391    # Do add_dependencies(obj) later due to CMake issue 14747.
392    list(APPEND objlibs ${obj_name})
393
394    set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
395  endif()
396
397  if(ARG_SHARED AND ARG_STATIC)
398    # static
399    set(name_static "${name}_static")
400    if(ARG_OUTPUT_NAME)
401      set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}")
402    endif()
403    # DEPENDS has been appended to LLVM_COMMON_LIBS.
404    llvm_add_library(${name_static} STATIC
405      ${output_name}
406      OBJLIBS ${ALL_FILES} # objlib
407      LINK_LIBS ${ARG_LINK_LIBS}
408      LINK_COMPONENTS ${ARG_LINK_COMPONENTS}
409      )
410    # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
411    set(ARG_STATIC)
412  endif()
413
414  if(ARG_MODULE)
415    add_library(${name} MODULE ${ALL_FILES})
416    llvm_setup_rpath(${name})
417  elseif(ARG_SHARED)
418    add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
419    add_library(${name} SHARED ${ALL_FILES})
420
421    llvm_setup_rpath(${name})
422
423  else()
424    add_library(${name} STATIC ${ALL_FILES})
425  endif()
426
427  setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
428
429  if(DEFINED windows_resource_file)
430    set_windows_version_resource_properties(${name} ${windows_resource_file})
431    set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
432  endif()
433
434  set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
435  # $<TARGET_OBJECTS> doesn't require compile flags.
436  if(NOT obj_name)
437    llvm_update_compile_flags(${name})
438  endif()
439  add_link_opts( ${name} )
440  if(ARG_OUTPUT_NAME)
441    set_target_properties(${name}
442      PROPERTIES
443      OUTPUT_NAME ${ARG_OUTPUT_NAME}
444      )
445  endif()
446
447  if(ARG_MODULE)
448    set_target_properties(${name} PROPERTIES
449      PREFIX ""
450      SUFFIX ${LLVM_PLUGIN_EXT}
451      )
452  endif()
453
454  if(ARG_SHARED)
455    if(WIN32)
456      set_target_properties(${name} PROPERTIES
457        PREFIX ""
458        )
459    endif()
460
461    # Set SOVERSION on shared libraries that lack explicit SONAME
462    # specifier, on *nix systems that are not Darwin.
463    if(UNIX AND NOT APPLE AND NOT ARG_SONAME)
464      set_target_properties(${name}
465        PROPERTIES
466        # Since 4.0.0, the ABI version is indicated by the major version
467        SOVERSION ${LLVM_VERSION_MAJOR}
468        VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
469    endif()
470  endif()
471
472  if(ARG_MODULE OR ARG_SHARED)
473    # Do not add -Dname_EXPORTS to the command-line when building files in this
474    # target. Doing so is actively harmful for the modules build because it
475    # creates extra module variants, and not useful because we don't use these
476    # macros.
477    set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
478
479    if (LLVM_EXPORTED_SYMBOL_FILE)
480      add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
481    endif()
482  endif()
483
484  if(ARG_SHARED AND UNIX)
485    if(NOT APPLE AND ARG_SONAME)
486      get_target_property(output_name ${name} OUTPUT_NAME)
487      if(${output_name} STREQUAL "output_name-NOTFOUND")
488        set(output_name ${name})
489      endif()
490      set(library_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX})
491      set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX})
492      set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name})
493      llvm_install_library_symlink(${api_name} ${library_name} SHARED
494        COMPONENT ${name}
495        ALWAYS_GENERATE)
496      llvm_install_library_symlink(${output_name} ${library_name} SHARED
497        COMPONENT ${name}
498        ALWAYS_GENERATE)
499    endif()
500  endif()
501
502  if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN))
503    # On DLL platforms symbols are imported from the tool by linking against it.
504    set(llvm_libs ${ARG_PLUGIN_TOOL})
505  elseif (DEFINED LLVM_LINK_COMPONENTS OR DEFINED ARG_LINK_COMPONENTS)
506    if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
507      set(llvm_libs LLVM)
508    else()
509      llvm_map_components_to_libnames(llvm_libs
510       ${ARG_LINK_COMPONENTS}
511       ${LLVM_LINK_COMPONENTS}
512       )
513    endif()
514  else()
515    # Components have not been defined explicitly in CMake, so add the
516    # dependency information for this library as defined by LLVMBuild.
517    #
518    # It would be nice to verify that we have the dependencies for this library
519    # name, but using get_property(... SET) doesn't suffice to determine if a
520    # property has been set to an empty value.
521    get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
522  endif()
523
524  if(ARG_STATIC)
525    set(libtype INTERFACE)
526  else()
527    # We can use PRIVATE since SO knows its dependent libs.
528    set(libtype PRIVATE)
529  endif()
530
531  target_link_libraries(${name} ${libtype}
532      ${ARG_LINK_LIBS}
533      ${lib_deps}
534      ${llvm_libs}
535      )
536
537  if(LLVM_COMMON_DEPENDS)
538    add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
539    # Add dependencies also to objlibs.
540    # CMake issue 14747 --  add_dependencies() might be ignored to objlib's user.
541    foreach(objlib ${objlibs})
542      add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS})
543    endforeach()
544  endif()
545
546  if(ARG_SHARED OR ARG_MODULE)
547    llvm_externalize_debuginfo(${name})
548  endif()
549endfunction()
550
551macro(add_llvm_library name)
552  cmake_parse_arguments(ARG
553    "SHARED;BUILDTREE_ONLY"
554    ""
555    ""
556    ${ARGN})
557  if( BUILD_SHARED_LIBS OR ARG_SHARED )
558    llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
559  else()
560    llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
561  endif()
562
563  # Libraries that are meant to only be exposed via the build tree only are
564  # never installed and are only exported as a target in the special build tree
565  # config file.
566  if (NOT ARG_BUILDTREE_ONLY)
567    set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
568  endif()
569
570  if( EXCLUDE_FROM_ALL )
571    set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
572  elseif(ARG_BUILDTREE_ONLY)
573    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
574  else()
575    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO" OR
576        (LLVM_LINK_LLVM_DYLIB AND ${name} STREQUAL "LLVM"))
577      set(install_dir lib${LLVM_LIBDIR_SUFFIX})
578      if(ARG_SHARED OR BUILD_SHARED_LIBS)
579        if(WIN32 OR CYGWIN OR MINGW)
580          set(install_type RUNTIME)
581          set(install_dir bin)
582        else()
583          set(install_type LIBRARY)
584        endif()
585      else()
586        set(install_type ARCHIVE)
587      endif()
588
589      if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
590          NOT LLVM_DISTRIBUTION_COMPONENTS)
591        set(export_to_llvmexports EXPORT LLVMExports)
592        set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
593      endif()
594
595      install(TARGETS ${name}
596              ${export_to_llvmexports}
597              ${install_type} DESTINATION ${install_dir}
598              COMPONENT ${name})
599
600      if (NOT CMAKE_CONFIGURATION_TYPES)
601        add_custom_target(install-${name}
602                          DEPENDS ${name}
603                          COMMAND "${CMAKE_COMMAND}"
604                                  -DCMAKE_INSTALL_COMPONENT=${name}
605                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
606      endif()
607    endif()
608    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
609  endif()
610  set_target_properties(${name} PROPERTIES FOLDER "Libraries")
611endmacro(add_llvm_library name)
612
613macro(add_llvm_loadable_module name)
614  llvm_add_library(${name} MODULE ${ARGN})
615  if(NOT TARGET ${name})
616    # Add empty "phony" target
617    add_custom_target(${name})
618  else()
619    if( EXCLUDE_FROM_ALL )
620      set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
621    else()
622      if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
623        if(WIN32 OR CYGWIN)
624          # DLL platform
625          set(dlldir "bin")
626        else()
627          set(dlldir "lib${LLVM_LIBDIR_SUFFIX}")
628        endif()
629
630        if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
631            NOT LLVM_DISTRIBUTION_COMPONENTS)
632          set(export_to_llvmexports EXPORT LLVMExports)
633          set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
634        endif()
635
636        install(TARGETS ${name}
637                ${export_to_llvmexports}
638                LIBRARY DESTINATION ${dlldir}
639                ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
640      endif()
641      set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
642    endif()
643  endif()
644
645  set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
646endmacro(add_llvm_loadable_module name)
647
648
649macro(add_llvm_executable name)
650  cmake_parse_arguments(ARG "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH" "" "DEPENDS" ${ARGN})
651  llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} )
652
653  list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS})
654
655  # Generate objlib
656  if(LLVM_ENABLE_OBJLIB)
657    # Generate an obj library for both targets.
658    set(obj_name "obj.${name}")
659    add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL
660      ${ALL_FILES}
661      )
662    llvm_update_compile_flags(${obj_name})
663    set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>")
664
665    set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries")
666  endif()
667
668  add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
669
670  if(XCODE)
671    # Note: the dummy.cpp source file provides no definitions. However,
672    # it forces Xcode to properly link the static library.
673    list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
674  endif()
675
676  if( EXCLUDE_FROM_ALL )
677    add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
678  else()
679    add_executable(${name} ${ALL_FILES})
680  endif()
681
682  setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS})
683
684  if(NOT ARG_NO_INSTALL_RPATH)
685    llvm_setup_rpath(${name})
686  endif()
687
688  if(DEFINED windows_resource_file)
689    set_windows_version_resource_properties(${name} ${windows_resource_file})
690  endif()
691
692  # $<TARGET_OBJECTS> doesn't require compile flags.
693  if(NOT LLVM_ENABLE_OBJLIB)
694    llvm_update_compile_flags(${name})
695  endif()
696  add_link_opts( ${name} )
697
698  # Do not add -Dname_EXPORTS to the command-line when building files in this
699  # target. Doing so is actively harmful for the modules build because it
700  # creates extra module variants, and not useful because we don't use these
701  # macros.
702  set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
703
704  if (LLVM_EXPORTED_SYMBOL_FILE)
705    add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
706  endif(LLVM_EXPORTED_SYMBOL_FILE)
707
708  if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
709    set(USE_SHARED USE_SHARED)
710  endif()
711
712  set(EXCLUDE_FROM_ALL OFF)
713  set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
714  llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
715  if( LLVM_COMMON_DEPENDS )
716    add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
717  endif( LLVM_COMMON_DEPENDS )
718
719  if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
720    llvm_externalize_debuginfo(${name})
721  endif()
722  if (LLVM_PTHREAD_LIB)
723    # libpthreads overrides some standard library symbols, so main
724    # executable must be linked with it in order to provide consistent
725    # API for all shared libaries loaded by this executable.
726    target_link_libraries(${name} ${LLVM_PTHREAD_LIB})
727  endif()
728endmacro(add_llvm_executable name)
729
730function(export_executable_symbols target)
731  if (LLVM_EXPORTED_SYMBOL_FILE)
732    # The symbol file should contain the symbols we want the executable to
733    # export
734    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
735  elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
736    # Extract the symbols to export from the static libraries that the
737    # executable links against.
738    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
739    set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols)
740    # We need to consider not just the direct link dependencies, but also the
741    # transitive link dependencies. Do this by starting with the set of direct
742    # dependencies, then the dependencies of those dependencies, and so on.
743    get_target_property(new_libs ${target} LINK_LIBRARIES)
744    set(link_libs ${new_libs})
745    while(NOT "${new_libs}" STREQUAL "")
746      foreach(lib ${new_libs})
747        if(TARGET ${lib})
748          get_target_property(lib_type ${lib} TYPE)
749          if("${lib_type}" STREQUAL "STATIC_LIBRARY")
750            list(APPEND static_libs ${lib})
751          else()
752            list(APPEND other_libs ${lib})
753          endif()
754          get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
755          foreach(transitive_lib ${transitive_libs})
756            list(FIND link_libs ${transitive_lib} idx)
757            if(TARGET ${transitive_lib} AND idx EQUAL -1)
758              list(APPEND newer_libs ${transitive_lib})
759              list(APPEND link_libs ${transitive_lib})
760            endif()
761          endforeach(transitive_lib)
762        endif()
763      endforeach(lib)
764      set(new_libs ${newer_libs})
765      set(newer_libs "")
766    endwhile()
767    if (MSVC)
768      set(mangling microsoft)
769    else()
770      set(mangling itanium)
771    endif()
772    add_custom_command(OUTPUT ${exported_symbol_file}
773                       COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
774                       WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
775                       DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
776                       VERBATIM
777                       COMMENT "Generating export list for ${target}")
778    add_llvm_symbol_exports( ${target} ${exported_symbol_file} )
779    # If something links against this executable then we want a
780    # transitive link against only the libraries whose symbols
781    # we aren't exporting.
782    set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}")
783    # The default import library suffix that cmake uses for cygwin/mingw is
784    # ".dll.a", but for clang.exe that causes a collision with libclang.dll,
785    # where the import libraries of both get named libclang.dll.a. Use a suffix
786    # of ".exe.a" to avoid this.
787    if(CYGWIN OR MINGW)
788      set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a")
789    endif()
790  elseif(NOT (WIN32 OR CYGWIN))
791    # On Windows auto-exporting everything doesn't work because of the limit on
792    # the size of the exported symbol table, but on other platforms we can do
793    # it without any trouble.
794    set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1)
795    if (APPLE)
796      set_property(TARGET ${target} APPEND_STRING PROPERTY
797        LINK_FLAGS " -rdynamic")
798    endif()
799  endif()
800endfunction()
801
802if(NOT LLVM_TOOLCHAIN_TOOLS)
803  set (LLVM_TOOLCHAIN_TOOLS
804    llvm-ar
805    llvm-ranlib
806    llvm-lib
807    llvm-objdump
808    )
809endif()
810
811macro(add_llvm_tool name)
812  if( NOT LLVM_BUILD_TOOLS )
813    set(EXCLUDE_FROM_ALL ON)
814  endif()
815  add_llvm_executable(${name} ${ARGN})
816
817  if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
818    if( LLVM_BUILD_TOOLS )
819      if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
820          NOT LLVM_DISTRIBUTION_COMPONENTS)
821        set(export_to_llvmexports EXPORT LLVMExports)
822        set_property(GLOBAL PROPERTY LLVM_HAS_EXPORTS True)
823      endif()
824
825      install(TARGETS ${name}
826              ${export_to_llvmexports}
827              RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}
828              COMPONENT ${name})
829
830      if (NOT CMAKE_CONFIGURATION_TYPES)
831        add_custom_target(install-${name}
832                          DEPENDS ${name}
833                          COMMAND "${CMAKE_COMMAND}"
834                                  -DCMAKE_INSTALL_COMPONENT=${name}
835                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
836      endif()
837    endif()
838  endif()
839  if( LLVM_BUILD_TOOLS )
840    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
841  endif()
842  set_target_properties(${name} PROPERTIES FOLDER "Tools")
843endmacro(add_llvm_tool name)
844
845
846macro(add_llvm_example name)
847  if( NOT LLVM_BUILD_EXAMPLES )
848    set(EXCLUDE_FROM_ALL ON)
849  endif()
850  add_llvm_executable(${name} ${ARGN})
851  if( LLVM_BUILD_EXAMPLES )
852    install(TARGETS ${name} RUNTIME DESTINATION examples)
853  endif()
854  set_target_properties(${name} PROPERTIES FOLDER "Examples")
855endmacro(add_llvm_example name)
856
857# This is a macro that is used to create targets for executables that are needed
858# for development, but that are not intended to be installed by default.
859macro(add_llvm_utility name)
860  if ( NOT LLVM_BUILD_UTILS )
861    set(EXCLUDE_FROM_ALL ON)
862  endif()
863
864  add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
865  set_target_properties(${name} PROPERTIES FOLDER "Utils")
866  if( LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS )
867    install (TARGETS ${name}
868      RUNTIME DESTINATION bin
869      COMPONENT ${name})
870    if (NOT CMAKE_CONFIGURATION_TYPES)
871      add_custom_target(install-${name}
872                        DEPENDS ${name}
873                        COMMAND "${CMAKE_COMMAND}"
874                                -DCMAKE_INSTALL_COMPONENT=${name}
875                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
876    endif()
877  endif()
878endmacro(add_llvm_utility name)
879
880
881macro(add_llvm_target target_name)
882  include_directories(BEFORE
883    ${CMAKE_CURRENT_BINARY_DIR}
884    ${CMAKE_CURRENT_SOURCE_DIR})
885  add_llvm_library(LLVM${target_name} ${ARGN})
886  set( CURRENT_LLVM_TARGET LLVM${target_name} )
887endmacro(add_llvm_target)
888
889function(canonicalize_tool_name name output)
890  string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name})
891  string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip})
892  string(TOUPPER ${nameUNDERSCORE} nameUPPER)
893  set(${output} "${nameUPPER}" PARENT_SCOPE)
894endfunction(canonicalize_tool_name)
895
896# Custom add_subdirectory wrapper
897# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
898# path if it differs from the name.
899macro(add_llvm_subdirectory project type name)
900  set(add_llvm_external_dir "${ARGN}")
901  if("${add_llvm_external_dir}" STREQUAL "")
902    set(add_llvm_external_dir ${name})
903  endif()
904  canonicalize_tool_name(${name} nameUPPER)
905  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
906    # Treat it as in-tree subproject.
907    option(${project}_${type}_${nameUPPER}_BUILD
908           "Whether to build ${name} as part of ${project}" On)
909    mark_as_advanced(${project}_${type}_${name}_BUILD)
910    if(${project}_${type}_${nameUPPER}_BUILD)
911      add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
912      # Don't process it in add_llvm_implicit_projects().
913      set(${project}_${type}_${nameUPPER}_BUILD OFF)
914    endif()
915  else()
916    set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
917      "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
918      CACHE PATH "Path to ${name} source directory")
919    set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
920    if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
921      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
922    endif()
923    if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
924      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
925    endif()
926    option(${project}_${type}_${nameUPPER}_BUILD
927      "Whether to build ${name} as part of LLVM"
928      ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
929    if (${project}_${type}_${nameUPPER}_BUILD)
930      if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
931        add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
932      elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
933        message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
934      endif()
935      # FIXME: It'd be redundant.
936      set(${project}_${type}_${nameUPPER}_BUILD Off)
937    endif()
938  endif()
939endmacro()
940
941# Add external project that may want to be built as part of llvm such as Clang,
942# lld, and Polly. This adds two options. One for the source directory of the
943# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
944# enable or disable building it with everything else.
945# Additional parameter can be specified as the name of directory.
946macro(add_llvm_external_project name)
947  add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN})
948endmacro()
949
950macro(add_llvm_tool_subdirectory name)
951  add_llvm_external_project(${name})
952endmacro(add_llvm_tool_subdirectory)
953
954function(get_project_name_from_src_var var output)
955  string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
956         MACHED_TOOL "${var}")
957  if(MACHED_TOOL)
958    set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE)
959  else()
960    set(${output} PARENT_SCOPE)
961  endif()
962endfunction()
963
964function(create_subdirectory_options project type)
965  file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
966  foreach(dir ${sub-dirs})
967    if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
968      canonicalize_tool_name(${dir} name)
969      option(${project}_${type}_${name}_BUILD
970           "Whether to build ${name} as part of ${project}" On)
971      mark_as_advanced(${project}_${type}_${name}_BUILD)
972    endif()
973  endforeach()
974endfunction(create_subdirectory_options)
975
976function(create_llvm_tool_options)
977  create_subdirectory_options(LLVM TOOL)
978endfunction(create_llvm_tool_options)
979
980function(llvm_add_implicit_projects project)
981  set(list_of_implicit_subdirs "")
982  file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
983  foreach(dir ${sub-dirs})
984    if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
985      canonicalize_tool_name(${dir} name)
986      if (${project}_TOOL_${name}_BUILD)
987        get_filename_component(fn "${dir}" NAME)
988        list(APPEND list_of_implicit_subdirs "${fn}")
989      endif()
990    endif()
991  endforeach()
992
993  foreach(external_proj ${list_of_implicit_subdirs})
994    add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN})
995  endforeach()
996endfunction(llvm_add_implicit_projects)
997
998function(add_llvm_implicit_projects)
999  llvm_add_implicit_projects(LLVM)
1000endfunction(add_llvm_implicit_projects)
1001
1002# Generic support for adding a unittest.
1003function(add_unittest test_suite test_name)
1004  if( NOT LLVM_BUILD_TESTS )
1005    set(EXCLUDE_FROM_ALL ON)
1006  endif()
1007
1008  include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
1009  include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include)
1010  if (NOT LLVM_ENABLE_THREADS)
1011    list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
1012  endif ()
1013
1014  if (SUPPORTS_VARIADIC_MACROS_FLAG)
1015    list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
1016  endif ()
1017  # Some parts of gtest rely on this GNU extension, don't warn on it.
1018  if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
1019    list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
1020  endif()
1021
1022  set(LLVM_REQUIRES_RTTI OFF)
1023
1024  list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream
1025  add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN})
1026  set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
1027  set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
1028  # libpthreads overrides some standard library symbols, so main
1029  # executable must be linked with it in order to provide consistent
1030  # API for all shared libaries loaded by this executable.
1031  target_link_libraries(${test_name} gtest_main gtest ${LLVM_PTHREAD_LIB})
1032
1033  add_dependencies(${test_suite} ${test_name})
1034  get_target_property(test_suite_folder ${test_suite} FOLDER)
1035  if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
1036    set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
1037  endif ()
1038endfunction()
1039
1040function(llvm_add_go_executable binary pkgpath)
1041  cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
1042
1043  if(LLVM_BINDINGS MATCHES "go")
1044    # FIXME: This should depend only on the libraries Go needs.
1045    get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
1046    set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
1047    set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1048    set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1049    set(cppflags "")
1050    get_property(include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
1051    foreach(d ${include_dirs})
1052      set(cppflags "${cppflags} -I${d}")
1053    endforeach(d)
1054    set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
1055    add_custom_command(OUTPUT ${binpath}
1056      COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "packages=${LLVM_GO_PACKAGES}"
1057              ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
1058      DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
1059              ${llvmlibs} ${ARG_DEPENDS}
1060      COMMENT "Building Go executable ${binary}"
1061      VERBATIM)
1062    if (ARG_ALL)
1063      add_custom_target(${binary} ALL DEPENDS ${binpath})
1064    else()
1065      add_custom_target(${binary} DEPENDS ${binpath})
1066    endif()
1067  endif()
1068endfunction()
1069
1070# This function canonicalize the CMake variables passed by names
1071# from CMake boolean to 0/1 suitable for passing into Python or C++,
1072# in place.
1073function(llvm_canonicalize_cmake_booleans)
1074  foreach(var ${ARGN})
1075    if(${var})
1076      set(${var} 1 PARENT_SCOPE)
1077    else()
1078      set(${var} 0 PARENT_SCOPE)
1079    endif()
1080  endforeach()
1081endfunction(llvm_canonicalize_cmake_booleans)
1082
1083# This function provides an automatic way to 'configure'-like generate a file
1084# based on a set of common and custom variables, specifically targeting the
1085# variables needed for the 'lit.site.cfg' files. This function bundles the
1086# common variables that any Lit instance is likely to need, and custom
1087# variables can be passed in.
1088function(configure_lit_site_cfg input output)
1089  foreach(c ${LLVM_TARGETS_TO_BUILD})
1090    set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
1091  endforeach(c)
1092  set(TARGETS_TO_BUILD ${TARGETS_BUILT})
1093
1094  set(SHLIBEXT "${LTDL_SHLIB_EXT}")
1095
1096  # Configuration-time: See Unit/lit.site.cfg.in
1097  if (CMAKE_CFG_INTDIR STREQUAL ".")
1098    set(LLVM_BUILD_MODE ".")
1099  else ()
1100    set(LLVM_BUILD_MODE "%(build_mode)s")
1101  endif ()
1102
1103  # They below might not be the build tree but provided binary tree.
1104  set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
1105  set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
1106  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
1107  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
1108
1109  # SHLIBDIR points the build tree.
1110  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
1111
1112  set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
1113  # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
1114  # plugins. We may rename it.
1115  if(LLVM_ENABLE_PLUGINS)
1116    set(ENABLE_SHARED "1")
1117  else()
1118    set(ENABLE_SHARED "0")
1119  endif()
1120
1121  if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
1122    set(ENABLE_ASSERTIONS "1")
1123  else()
1124    set(ENABLE_ASSERTIONS "0")
1125  endif()
1126
1127  set(HOST_OS ${CMAKE_SYSTEM_NAME})
1128  set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
1129
1130  set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
1131  set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
1132  set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
1133
1134  set(LIT_SITE_CFG_IN_HEADER  "## Autogenerated from ${input}\n## Do not edit!")
1135
1136  # Override config_target_triple (and the env)
1137  if(LLVM_TARGET_TRIPLE_ENV)
1138    # This is expanded into the heading.
1139    string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1140      "import os\n"
1141      "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
1142      "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
1143      )
1144
1145    # This is expanded to; config.target_triple = ""+config.target_triple+""
1146    set(TARGET_TRIPLE "\"+config.target_triple+\"")
1147  endif()
1148
1149  configure_file(${input} ${output} @ONLY)
1150endfunction()
1151
1152# A raw function to create a lit target. This is used to implement the testuite
1153# management functions.
1154function(add_lit_target target comment)
1155  cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1156  set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
1157  separate_arguments(LIT_ARGS)
1158  if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
1159    list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
1160  endif ()
1161  if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
1162    # reset cache after erraneous r283029
1163    # TODO: remove this once all buildbots run
1164    if (LIT_COMMAND STREQUAL "${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py")
1165      unset(LIT_COMMAND CACHE)
1166    endif()
1167    set (LIT_COMMAND "${PYTHON_EXECUTABLE};${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py"
1168         CACHE STRING "Command used to spawn llvm-lit")
1169  else()
1170    find_program(LIT_COMMAND NAMES llvm-lit lit.py lit)
1171  endif ()
1172  list(APPEND LIT_COMMAND ${LIT_ARGS})
1173  foreach(param ${ARG_PARAMS})
1174    list(APPEND LIT_COMMAND --param ${param})
1175  endforeach()
1176  if (ARG_UNPARSED_ARGUMENTS)
1177    add_custom_target(${target}
1178      COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS}
1179      COMMENT "${comment}"
1180      USES_TERMINAL
1181      )
1182  else()
1183    add_custom_target(${target}
1184      COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
1185    message(STATUS "${target} does nothing.")
1186  endif()
1187  if (ARG_DEPENDS)
1188    add_dependencies(${target} ${ARG_DEPENDS})
1189  endif()
1190
1191  # Tests should be excluded from "Build Solution".
1192  set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
1193endfunction()
1194
1195# A function to add a set of lit test suites to be driven through 'check-*' targets.
1196function(add_lit_testsuite target comment)
1197  cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1198
1199  # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
1200  if(NOT EXCLUDE_FROM_ALL)
1201    # Register the testsuites, params and depends for the global check rule.
1202    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
1203    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
1204    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
1205    set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
1206  endif()
1207
1208  # Produce a specific suffixed check rule.
1209  add_lit_target(${target} ${comment}
1210    ${ARG_UNPARSED_ARGUMENTS}
1211    PARAMS ${ARG_PARAMS}
1212    DEPENDS ${ARG_DEPENDS}
1213    ARGS ${ARG_ARGS}
1214    )
1215endfunction()
1216
1217function(add_lit_testsuites project directory)
1218  if (NOT CMAKE_CONFIGURATION_TYPES)
1219    cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN})
1220
1221    # Search recursively for test directories by assuming anything not
1222    # in a directory called Inputs contains tests.
1223    file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*)
1224    foreach(lit_suite ${to_process})
1225      if(NOT IS_DIRECTORY ${lit_suite})
1226        continue()
1227      endif()
1228      string(FIND ${lit_suite} Inputs is_inputs)
1229      string(FIND ${lit_suite} Output is_output)
1230      if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1))
1231        continue()
1232      endif()
1233
1234      # Create a check- target for the directory.
1235      string(REPLACE ${directory} "" name_slash ${lit_suite})
1236      if (name_slash)
1237        string(REPLACE "/" "-" name_slash ${name_slash})
1238        string(REPLACE "\\" "-" name_dashes ${name_slash})
1239        string(TOLOWER "${project}${name_dashes}" name_var)
1240        add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}"
1241          ${lit_suite}
1242          PARAMS ${ARG_PARAMS}
1243          DEPENDS ${ARG_DEPENDS}
1244          ARGS ${ARG_ARGS}
1245        )
1246      endif()
1247    endforeach()
1248  endif()
1249endfunction()
1250
1251function(llvm_install_library_symlink name dest type)
1252  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1253  foreach(path ${CMAKE_MODULE_PATH})
1254    if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1255      set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1256      break()
1257    endif()
1258  endforeach()
1259
1260  set(component ${ARG_COMPONENT})
1261  if(NOT component)
1262    set(component ${name})
1263  endif()
1264
1265  set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX})
1266  set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX})
1267
1268  set(output_dir lib${LLVM_LIBDIR_SUFFIX})
1269  if(WIN32 AND "${type}" STREQUAL "SHARED")
1270    set(output_dir bin)
1271  endif()
1272
1273  install(SCRIPT ${INSTALL_SYMLINK}
1274          CODE "install_symlink(${full_name} ${full_dest} ${output_dir})"
1275          COMPONENT ${component})
1276
1277  if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1278    add_custom_target(install-${name}
1279                      DEPENDS ${name} ${dest} install-${dest}
1280                      COMMAND "${CMAKE_COMMAND}"
1281                              -DCMAKE_INSTALL_COMPONENT=${name}
1282                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1283  endif()
1284endfunction()
1285
1286function(llvm_install_symlink name dest)
1287  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN})
1288  foreach(path ${CMAKE_MODULE_PATH})
1289    if(EXISTS ${path}/LLVMInstallSymlink.cmake)
1290      set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake)
1291      break()
1292    endif()
1293  endforeach()
1294
1295  if(ARG_COMPONENT)
1296    set(component ${ARG_COMPONENT})
1297  else()
1298    if(ARG_ALWAYS_GENERATE)
1299      set(component ${dest})
1300    else()
1301      set(component ${name})
1302    endif()
1303  endif()
1304
1305  set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX})
1306  set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX})
1307
1308  install(SCRIPT ${INSTALL_SYMLINK}
1309          CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})"
1310          COMPONENT ${component})
1311
1312  if (NOT CMAKE_CONFIGURATION_TYPES AND NOT ARG_ALWAYS_GENERATE)
1313    add_custom_target(install-${name}
1314                      DEPENDS ${name} ${dest} install-${dest}
1315                      COMMAND "${CMAKE_COMMAND}"
1316                              -DCMAKE_INSTALL_COMPONENT=${name}
1317                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
1318  endif()
1319endfunction()
1320
1321function(add_llvm_tool_symlink link_name target)
1322  cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
1323  set(dest_binary "$<TARGET_FILE:${target}>")
1324
1325  # This got a bit gross... For multi-configuration generators the target
1326  # properties return the resolved value of the string, not the build system
1327  # expression. To reconstruct the platform-agnostic path we have to do some
1328  # magic. First we grab one of the types, and a type-specific path. Then from
1329  # the type-specific path we find the last occurrence of the type in the path,
1330  # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
1331  # agnostic again.
1332  if(NOT ARG_OUTPUT_DIR)
1333    # If you're not overriding the OUTPUT_DIR, we can make the link relative in
1334    # the same directory.
1335    if(UNIX)
1336      set(dest_binary "$<TARGET_FILE_NAME:${target}>")
1337    endif()
1338    if(CMAKE_CONFIGURATION_TYPES)
1339      list(GET CMAKE_CONFIGURATION_TYPES 0 first_type)
1340      string(TOUPPER ${first_type} first_type_upper)
1341      set(first_type_suffix _${first_type_upper})
1342    endif()
1343    get_target_property(target_type ${target} TYPE)
1344    if(${target_type} STREQUAL "STATIC_LIBRARY")
1345      get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix})
1346    elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
1347      get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix})
1348    else()
1349      get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix})
1350    endif()
1351    if(CMAKE_CONFIGURATION_TYPES)
1352      string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE)
1353      string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix)
1354      string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix)
1355      string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/"
1356             path_suffix ${path_suffix})
1357      set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix})
1358    endif()
1359  endif()
1360
1361  if(UNIX)
1362    set(LLVM_LINK_OR_COPY create_symlink)
1363  else()
1364    set(LLVM_LINK_OR_COPY copy)
1365  endif()
1366
1367  set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
1368
1369  set(target_name ${link_name})
1370  if(TARGET ${link_name})
1371    set(target_name ${link_name}-link)
1372  endif()
1373
1374
1375  if(ARG_ALWAYS_GENERATE)
1376    set_property(DIRECTORY APPEND PROPERTY
1377      ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
1378    add_custom_command(TARGET ${target} POST_BUILD
1379      COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
1380  else()
1381    add_custom_command(OUTPUT ${output_path}
1382                     COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
1383                     DEPENDS ${target})
1384    add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
1385    set_target_properties(${target_name} PROPERTIES FOLDER Tools)
1386
1387    # Make sure both the link and target are toolchain tools
1388    if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
1389      set(TOOL_IS_TOOLCHAIN ON)
1390    endif()
1391
1392    if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
1393      llvm_install_symlink(${link_name} ${target})
1394    endif()
1395  endif()
1396endfunction()
1397
1398function(llvm_externalize_debuginfo name)
1399  if(NOT LLVM_EXTERNALIZE_DEBUGINFO)
1400    return()
1401  endif()
1402
1403  if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
1404    if(APPLE)
1405      set(strip_command COMMAND xcrun strip -Sxl $<TARGET_FILE:${name}>)
1406    else()
1407      set(strip_command COMMAND strip -gx $<TARGET_FILE:${name}>)
1408    endif()
1409  endif()
1410
1411  if(APPLE)
1412    if(CMAKE_CXX_FLAGS MATCHES "-flto"
1413      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
1414
1415      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
1416      set_property(TARGET ${name} APPEND_STRING PROPERTY
1417        LINK_FLAGS " -Wl,-object_path_lto,${lto_object}")
1418    endif()
1419    add_custom_command(TARGET ${name} POST_BUILD
1420      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
1421      ${strip_command}
1422      )
1423  else()
1424    add_custom_command(TARGET ${name} POST_BUILD
1425      COMMAND objcopy --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug
1426      ${strip_command} -R .gnu_debuglink
1427      COMMAND objcopy --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}>
1428      )
1429  endif()
1430endfunction()
1431
1432function(llvm_setup_rpath name)
1433  if(CMAKE_INSTALL_RPATH)
1434    return()
1435  endif()
1436
1437  if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
1438    set(extra_libdir ${LLVM_LIBRARY_DIR})
1439  elseif(LLVM_BUILD_LIBRARY_DIR)
1440    set(extra_libdir ${LLVM_LIBRARY_DIR})
1441  endif()
1442
1443  if (APPLE)
1444    set(_install_name_dir INSTALL_NAME_DIR "@rpath")
1445    set(_install_rpath "@loader_path/../lib" ${extra_libdir})
1446  elseif(UNIX)
1447    set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
1448    if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
1449      set_property(TARGET ${name} APPEND_STRING PROPERTY
1450                   LINK_FLAGS " -Wl,-z,origin ")
1451    elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT LLVM_LINKER_IS_GOLD)
1452      # $ORIGIN is not interpreted at link time by ld.bfd
1453      set_property(TARGET ${name} APPEND_STRING PROPERTY
1454                   LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ")
1455    endif()
1456  else()
1457    return()
1458  endif()
1459
1460  set_target_properties(${name} PROPERTIES
1461                        BUILD_WITH_INSTALL_RPATH On
1462                        INSTALL_RPATH "${_install_rpath}"
1463                        ${_install_name_dir})
1464endfunction()
1465
1466function(setup_dependency_debugging name)
1467  if(NOT LLVM_DEPENDENCY_DEBUGGING)
1468    return()
1469  endif()
1470
1471  if("intrinsics_gen" IN_LIST ARGN)
1472    return()
1473  endif()
1474
1475  set(deny_attributes_gen "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.gen\"))")
1476  set(deny_intrinsics_gen "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.gen\"))")
1477
1478  set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_gen} ${deny_intrinsics_gen}'")
1479  set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command})
1480endfunction()
1481