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