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