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