1function(get_system_libs return_var)
2  message(AUTHOR_WARNING "get_system_libs no longer needed")
3  set(${return_var} "" PARENT_SCOPE)
4endfunction()
5
6
7function(link_system_libs target)
8  message(AUTHOR_WARNING "link_system_libs no longer needed")
9endfunction()
10
11# is_llvm_target_library(
12#   library
13#     Name of the LLVM library to check
14#   return_var
15#     Output variable name
16#   ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS
17#     ALL_TARGETS - default looks at the full list of known targets
18#     INCLUDED_TARGETS - looks only at targets being configured
19#     OMITTED_TARGETS - looks only at targets that are not being configured
20# )
21function(is_llvm_target_library library return_var)
22  cmake_parse_arguments(ARG "ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS" "" "" ${ARGN})
23  # Sets variable `return_var' to ON if `library' corresponds to a
24  # LLVM supported target. To OFF if it doesn't.
25  set(${return_var} OFF PARENT_SCOPE)
26  string(TOUPPER "${library}" capitalized_lib)
27  if(ARG_INCLUDED_TARGETS)
28    string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" targets)
29  elseif(ARG_OMITTED_TARGETS)
30    set(omitted_targets ${LLVM_ALL_TARGETS})
31    if (LLVM_TARGETS_TO_BUILD)
32      list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
33    endif()
34    string(TOUPPER "${omitted_targets}" targets)
35  else()
36    string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
37  endif()
38  foreach(t ${targets})
39    if( capitalized_lib STREQUAL t OR
40        capitalized_lib STREQUAL "${t}" OR
41        capitalized_lib STREQUAL "${t}DESC" OR
42        capitalized_lib STREQUAL "${t}CODEGEN" OR
43        capitalized_lib STREQUAL "${t}ASMPARSER" OR
44        capitalized_lib STREQUAL "${t}ASMPRINTER" OR
45        capitalized_lib STREQUAL "${t}DISASSEMBLER" OR
46        capitalized_lib STREQUAL "${t}INFO" OR
47        capitalized_lib STREQUAL "${t}UTILS" )
48      set(${return_var} ON PARENT_SCOPE)
49      break()
50    endif()
51  endforeach()
52endfunction(is_llvm_target_library)
53
54function(is_llvm_target_specifier library return_var)
55  is_llvm_target_library(${library} ${return_var} ${ARGN})
56  string(TOUPPER "${library}" capitalized_lib)
57  if(NOT ${return_var})
58    if( capitalized_lib STREQUAL "ALLTARGETSASMPARSERS" OR
59        capitalized_lib STREQUAL "ALLTARGETSDESCS" OR
60        capitalized_lib STREQUAL "ALLTARGETSDISASSEMBLERS" OR
61        capitalized_lib STREQUAL "ALLTARGETSINFOS" OR
62        capitalized_lib STREQUAL "NATIVE" OR
63        capitalized_lib STREQUAL "NATIVECODEGEN" )
64      set(${return_var} ON PARENT_SCOPE)
65    endif()
66  endif()
67endfunction()
68
69macro(llvm_config executable)
70  cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
71  set(link_components ${ARG_UNPARSED_ARGUMENTS})
72
73  if(ARG_USE_SHARED)
74    # If USE_SHARED is specified, then we link against libLLVM,
75    # but also against the component libraries below. This is
76    # done in case libLLVM does not contain all of the components
77    # the target requires.
78    #
79    # Strip LLVM_DYLIB_COMPONENTS out of link_components.
80    # To do this, we need special handling for "all", since that
81    # may imply linking to libraries that are not included in
82    # libLLVM.
83
84    if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
85      if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
86        set(link_components "")
87      else()
88        list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
89      endif()
90    endif()
91
92    target_link_libraries(${executable} PRIVATE LLVM)
93  endif()
94
95  explicit_llvm_config(${executable} ${link_components})
96endmacro(llvm_config)
97
98
99function(explicit_llvm_config executable)
100  set( link_components ${ARGN} )
101
102  llvm_map_components_to_libnames(LIBRARIES ${link_components})
103  get_target_property(t ${executable} TYPE)
104  if(t STREQUAL "STATIC_LIBRARY")
105    target_link_libraries(${executable} INTERFACE ${LIBRARIES})
106  elseif(t STREQUAL "EXECUTABLE" OR t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
107    target_link_libraries(${executable} PRIVATE ${LIBRARIES})
108  else()
109    # Use plain form for legacy user.
110    target_link_libraries(${executable} ${LIBRARIES})
111  endif()
112endfunction(explicit_llvm_config)
113
114
115# This is Deprecated
116function(llvm_map_components_to_libraries OUT_VAR)
117  message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
118  explicit_map_components_to_libraries(result ${ARGN})
119  set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
120endfunction(llvm_map_components_to_libraries)
121
122# Expand pseudo-components into real components.
123# Does not cover 'native', 'backend', or 'engine' as these require special
124# handling. Also does not cover 'all' as we only have a list of the libnames
125# available and not a list of the components.
126function(llvm_expand_pseudo_components out_components)
127  set( link_components ${ARGN} )
128  foreach(c ${link_components})
129    # add codegen, asmprinter, asmparser, disassembler
130    list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
131    if( NOT idx LESS 0 )
132      if( TARGET LLVM${c}CodeGen )
133        list(APPEND expanded_components "${c}CodeGen")
134      else()
135        if( TARGET LLVM${c} )
136          list(APPEND expanded_components "${c}")
137        else()
138          message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
139        endif()
140      endif()
141      if( TARGET LLVM${c}AsmPrinter )
142        list(APPEND expanded_components "${c}AsmPrinter")
143      endif()
144      if( TARGET LLVM${c}AsmParser )
145        list(APPEND expanded_components "${c}AsmParser")
146      endif()
147      if( TARGET LLVM${c}Desc )
148        list(APPEND expanded_components "${c}Desc")
149      endif()
150      if( TARGET LLVM${c}Disassembler )
151        list(APPEND expanded_components "${c}Disassembler")
152      endif()
153      if( TARGET LLVM${c}Info )
154        list(APPEND expanded_components "${c}Info")
155      endif()
156      if( TARGET LLVM${c}Utils )
157        list(APPEND expanded_components "${c}Utils")
158      endif()
159    elseif( c STREQUAL "nativecodegen" )
160      if( TARGET LLVM${LLVM_NATIVE_ARCH}CodeGen )
161        list(APPEND expanded_components "${LLVM_NATIVE_ARCH}CodeGen")
162      endif()
163      if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
164        list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Desc")
165      endif()
166      if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
167        list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Info")
168      endif()
169    elseif( c STREQUAL "AllTargetsCodeGens" )
170      # Link all the codegens from all the targets
171      foreach(t ${LLVM_TARGETS_TO_BUILD})
172        if( TARGET LLVM${t}CodeGen)
173          list(APPEND expanded_components "${t}CodeGen")
174        endif()
175      endforeach(t)
176    elseif( c STREQUAL "AllTargetsAsmParsers" )
177      # Link all the asm parsers from all the targets
178      foreach(t ${LLVM_TARGETS_TO_BUILD})
179        if( TARGET LLVM${t}AsmParser )
180          list(APPEND expanded_components "${t}AsmParser")
181        endif()
182      endforeach(t)
183    elseif( c STREQUAL "AllTargetsDescs" )
184      # Link all the descs from all the targets
185      foreach(t ${LLVM_TARGETS_TO_BUILD})
186        if( TARGET LLVM${t}Desc )
187          list(APPEND expanded_components "${t}Desc")
188        endif()
189      endforeach(t)
190    elseif( c STREQUAL "AllTargetsDisassemblers" )
191      # Link all the disassemblers from all the targets
192      foreach(t ${LLVM_TARGETS_TO_BUILD})
193        if( TARGET LLVM${t}Disassembler )
194          list(APPEND expanded_components "${t}Disassembler")
195        endif()
196      endforeach(t)
197    elseif( c STREQUAL "AllTargetsInfos" )
198      # Link all the infos from all the targets
199      foreach(t ${LLVM_TARGETS_TO_BUILD})
200        if( TARGET LLVM${t}Info )
201          list(APPEND expanded_components "${t}Info")
202        endif()
203      endforeach(t)
204    elseif( c STREQUAL "AllTargetsMCAs" )
205      # Link all the TargetMCAs from all the targets
206      foreach(t ${LLVM_TARGETS_TO_BUILD})
207        if( TARGET LLVM${t}TargetMCA )
208          list(APPEND expanded_components "${t}TargetMCA")
209        endif()
210      endforeach(t)
211    else()
212      list(APPEND expanded_components "${c}")
213    endif()
214  endforeach()
215  set(${out_components} ${expanded_components} PARENT_SCOPE)
216endfunction(llvm_expand_pseudo_components out_components)
217
218# This is a variant intended for the final user:
219# Map LINK_COMPONENTS to actual libnames.
220function(llvm_map_components_to_libnames out_libs)
221  set( link_components ${ARGN} )
222  if(NOT LLVM_AVAILABLE_LIBS)
223    # Inside LLVM itself available libs are in a global property.
224    get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
225  endif()
226  string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
227
228  get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
229
230  # Generally in our build system we avoid order-dependence. Unfortunately since
231  # not all targets create the same set of libraries we actually need to ensure
232  # that all build targets associated with a target are added before we can
233  # process target dependencies.
234  if(NOT LLVM_TARGETS_CONFIGURED)
235    foreach(c ${link_components})
236      is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
237      if(iltl_result)
238        message(FATAL_ERROR "Specified target library before target registration is complete.")
239      endif()
240    endforeach()
241  endif()
242
243  # Expand some keywords:
244  list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
245  list(FIND link_components "engine" engine_required)
246  if( NOT engine_required EQUAL -1 )
247    list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
248    if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
249      list(APPEND link_components "jit")
250      list(APPEND link_components "native")
251    else()
252      list(APPEND link_components "interpreter")
253    endif()
254  endif()
255  list(FIND link_components "native" native_required)
256  if( NOT native_required EQUAL -1 )
257    if( NOT have_native_backend EQUAL -1 )
258      list(APPEND link_components ${LLVM_NATIVE_ARCH})
259    endif()
260  endif()
261
262  # Translate symbolic component names to real libraries:
263  llvm_expand_pseudo_components(link_components ${link_components})
264  foreach(c ${link_components})
265    get_property(c_rename GLOBAL PROPERTY LLVM_COMPONENT_NAME_${c})
266    if(c_rename)
267        set(c ${c_rename})
268    endif()
269    if( c STREQUAL "native" )
270      # already processed
271    elseif( c STREQUAL "backend" )
272      # same case as in `native'.
273    elseif( c STREQUAL "engine" )
274      # already processed
275    elseif( c STREQUAL "all" )
276      get_property(all_components GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
277      list(APPEND expanded_components ${all_components})
278    else()
279      # Canonize the component name:
280      string(TOUPPER "${c}" capitalized)
281      list(FIND capitalized_libs LLVM${capitalized} lib_idx)
282      if( lib_idx LESS 0 )
283        # The component is unknown. Maybe is an omitted target?
284        is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
285        if(iltl_result)
286          # A missing library to a directly referenced omitted target would be bad.
287          message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
288        else()
289          # If it is not an omitted target we should assume it is a component
290          # that hasn't yet been processed by CMake. Missing components will
291          # cause errors later in the configuration, so we can safely assume
292          # that this is valid here.
293          list(APPEND expanded_components LLVM${c})
294        endif()
295      else( lib_idx LESS 0 )
296        list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
297        list(APPEND expanded_components ${canonical_lib})
298      endif( lib_idx LESS 0 )
299    endif( c STREQUAL "native" )
300  endforeach(c)
301
302  set(${out_libs} ${expanded_components} PARENT_SCOPE)
303endfunction()
304
305# Perform a post-order traversal of the dependency graph.
306# This duplicates the algorithm used by llvm-config, originally
307# in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
308function(expand_topologically name required_libs visited_libs)
309  list(FIND visited_libs ${name} found)
310  if( found LESS 0 )
311    list(APPEND visited_libs ${name})
312    set(visited_libs ${visited_libs} PARENT_SCOPE)
313
314    #
315    get_property(libname GLOBAL PROPERTY LLVM_COMPONENT_NAME_${name})
316    if(libname)
317      set(cname LLVM${libname})
318    elseif(TARGET ${name})
319      set(cname ${name})
320    elseif(TARGET LLVM${name})
321      set(cname LLVM${name})
322    else()
323      message(FATAL_ERROR "unknown component ${name}")
324    endif()
325
326    get_property(lib_deps TARGET ${cname} PROPERTY LLVM_LINK_COMPONENTS)
327    foreach( lib_dep ${lib_deps} )
328      expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
329      set(required_libs ${required_libs} PARENT_SCOPE)
330      set(visited_libs ${visited_libs} PARENT_SCOPE)
331    endforeach()
332
333    list(APPEND required_libs ${cname})
334    set(required_libs ${required_libs} PARENT_SCOPE)
335  endif()
336endfunction()
337
338# Expand dependencies while topologically sorting the list of libraries:
339function(llvm_expand_dependencies out_libs)
340  set(expanded_components ${ARGN})
341
342  set(required_libs)
343  set(visited_libs)
344  foreach( lib ${expanded_components} )
345    expand_topologically(${lib} "${required_libs}" "${visited_libs}")
346  endforeach()
347
348  if(required_libs)
349    list(REVERSE required_libs)
350  endif()
351  set(${out_libs} ${required_libs} PARENT_SCOPE)
352endfunction()
353
354function(explicit_map_components_to_libraries out_libs)
355  llvm_map_components_to_libnames(link_libs ${ARGN})
356  llvm_expand_dependencies(expanded_components ${link_libs})
357  # Return just the libraries included in this build:
358  set(result)
359  foreach(c ${expanded_components})
360    if( TARGET ${c} )
361      set(result ${result} ${c})
362    endif()
363  endforeach(c)
364  set(${out_libs} ${result} PARENT_SCOPE)
365endfunction(explicit_map_components_to_libraries)
366