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