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 "AllTargetsAsmParsers" )
175      # Link all the asm parsers from all the targets
176      foreach(t ${LLVM_TARGETS_TO_BUILD})
177        if( TARGET LLVM${t}AsmParser )
178          list(APPEND expanded_components "${t}AsmParser")
179        endif()
180      endforeach(t)
181    elseif( c STREQUAL "AllTargetsDescs" )
182      # Link all the descs from all the targets
183      foreach(t ${LLVM_TARGETS_TO_BUILD})
184        if( TARGET LLVM${t}Desc )
185          list(APPEND expanded_components "${t}Desc")
186        endif()
187      endforeach(t)
188    elseif( c STREQUAL "AllTargetsDisassemblers" )
189      # Link all the disassemblers from all the targets
190      foreach(t ${LLVM_TARGETS_TO_BUILD})
191        if( TARGET LLVM${t}Disassembler )
192          list(APPEND expanded_components "${t}Disassembler")
193        endif()
194      endforeach(t)
195    elseif( c STREQUAL "AllTargetsInfos" )
196      # Link all the infos from all the targets
197      foreach(t ${LLVM_TARGETS_TO_BUILD})
198        if( TARGET LLVM${t}Info )
199          list(APPEND expanded_components "${t}Info")
200        endif()
201      endforeach(t)
202    else()
203      list(APPEND expanded_components "${c}")
204    endif()
205  endforeach()
206  set(${out_components} ${expanded_components} PARENT_SCOPE)
207endfunction(llvm_expand_pseudo_components out_components)
208
209# This is a variant intended for the final user:
210# Map LINK_COMPONENTS to actual libnames.
211function(llvm_map_components_to_libnames out_libs)
212  set( link_components ${ARGN} )
213  if(NOT LLVM_AVAILABLE_LIBS)
214    # Inside LLVM itself available libs are in a global property.
215    get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
216  endif()
217  string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
218
219  get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
220
221  # Generally in our build system we avoid order-dependence. Unfortunately since
222  # not all targets create the same set of libraries we actually need to ensure
223  # that all build targets associated with a target are added before we can
224  # process target dependencies.
225  if(NOT LLVM_TARGETS_CONFIGURED)
226    foreach(c ${link_components})
227      is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
228      if(iltl_result)
229        message(FATAL_ERROR "Specified target library before target registration is complete.")
230      endif()
231    endforeach()
232  endif()
233
234  # Expand some keywords:
235  list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
236  list(FIND link_components "engine" engine_required)
237  if( NOT engine_required EQUAL -1 )
238    list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
239    if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
240      list(APPEND link_components "jit")
241      list(APPEND link_components "native")
242    else()
243      list(APPEND link_components "interpreter")
244    endif()
245  endif()
246  list(FIND link_components "native" native_required)
247  if( NOT native_required EQUAL -1 )
248    if( NOT have_native_backend EQUAL -1 )
249      list(APPEND link_components ${LLVM_NATIVE_ARCH})
250    endif()
251  endif()
252
253  # Translate symbolic component names to real libraries:
254  llvm_expand_pseudo_components(link_components ${link_components})
255  foreach(c ${link_components})
256    get_property(c_rename GLOBAL PROPERTY LLVM_COMPONENT_NAME_${c})
257    if(c_rename)
258        set(c ${c_rename})
259    endif()
260    if( c STREQUAL "native" )
261      # already processed
262    elseif( c STREQUAL "backend" )
263      # same case as in `native'.
264    elseif( c STREQUAL "engine" )
265      # already processed
266    elseif( c STREQUAL "all" )
267      get_property(all_components GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
268      list(APPEND expanded_components ${all_components})
269    else()
270      # Canonize the component name:
271      string(TOUPPER "${c}" capitalized)
272      list(FIND capitalized_libs LLVM${capitalized} lib_idx)
273      if( lib_idx LESS 0 )
274        # The component is unknown. Maybe is an omitted target?
275        is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
276        if(iltl_result)
277          # A missing library to a directly referenced omitted target would be bad.
278          message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
279        else()
280          # If it is not an omitted target we should assume it is a component
281          # that hasn't yet been processed by CMake. Missing components will
282          # cause errors later in the configuration, so we can safely assume
283          # that this is valid here.
284          list(APPEND expanded_components LLVM${c})
285        endif()
286      else( lib_idx LESS 0 )
287        list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
288        list(APPEND expanded_components ${canonical_lib})
289      endif( lib_idx LESS 0 )
290    endif( c STREQUAL "native" )
291  endforeach(c)
292
293  set(${out_libs} ${expanded_components} PARENT_SCOPE)
294endfunction()
295
296# Perform a post-order traversal of the dependency graph.
297# This duplicates the algorithm used by llvm-config, originally
298# in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
299function(expand_topologically name required_libs visited_libs)
300  list(FIND visited_libs ${name} found)
301  if( found LESS 0 )
302    list(APPEND visited_libs ${name})
303    set(visited_libs ${visited_libs} PARENT_SCOPE)
304
305    #
306    get_property(libname GLOBAL PROPERTY LLVM_COMPONENT_NAME_${name})
307    if(libname)
308      set(cname LLVM${libname})
309    elseif(TARGET ${name})
310      set(cname ${name})
311    elseif(TARGET LLVM${name})
312      set(cname LLVM${name})
313    else()
314      message(FATAL_ERROR "unknown component ${name}")
315    endif()
316
317    get_property(lib_deps TARGET ${cname} PROPERTY LLVM_LINK_COMPONENTS)
318    foreach( lib_dep ${lib_deps} )
319      expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
320      set(required_libs ${required_libs} PARENT_SCOPE)
321      set(visited_libs ${visited_libs} PARENT_SCOPE)
322    endforeach()
323
324    list(APPEND required_libs ${cname})
325    set(required_libs ${required_libs} PARENT_SCOPE)
326  endif()
327endfunction()
328
329# Expand dependencies while topologically sorting the list of libraries:
330function(llvm_expand_dependencies out_libs)
331  set(expanded_components ${ARGN})
332
333  set(required_libs)
334  set(visited_libs)
335  foreach( lib ${expanded_components} )
336    expand_topologically(${lib} "${required_libs}" "${visited_libs}")
337  endforeach()
338
339  if(required_libs)
340    list(REVERSE required_libs)
341  endif()
342  set(${out_libs} ${required_libs} PARENT_SCOPE)
343endfunction()
344
345function(explicit_map_components_to_libraries out_libs)
346  llvm_map_components_to_libnames(link_libs ${ARGN})
347  llvm_expand_dependencies(expanded_components ${link_libs})
348  # Return just the libraries included in this build:
349  set(result)
350  foreach(c ${expanded_components})
351    if( TARGET ${c} )
352      set(result ${result} ${c})
353    endif()
354  endforeach(c)
355  set(${out_libs} ${result} PARENT_SCOPE)
356endfunction(explicit_map_components_to_libraries)
357