1 function(get_system_libs return_var)
2 message(AUTHOR_WARNING "get_system_libs no longer needed")
3 set(${return_var} "" PARENT_SCOPE)
7 function(link_system_libs target)
8 message(AUTHOR_WARNING "link_system_libs no longer needed")
11 # is_llvm_target_library(
13 # Name of the LLVM library to check
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
21 function(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})
34 string(TOUPPER "${omitted_targets}" targets)
36 string(TOUPPER "${LLVM_ALL_TARGETS}" 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)
52 endfunction(is_llvm_target_library)
54 function(is_llvm_target_specifier library return_var)
55 is_llvm_target_library(${library} ${return_var} ${ARGN})
56 string(TOUPPER "${library}" capitalized_lib)
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)
69 macro(llvm_config executable)
70 cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
71 set(link_components ${ARG_UNPARSED_ARGUMENTS})
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.
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
84 if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
85 if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
86 set(link_components "")
88 list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
92 target_link_libraries(${executable} PRIVATE LLVM)
95 explicit_llvm_config(${executable} ${link_components})
99 function(explicit_llvm_config executable)
100 set( link_components ${ARGN} )
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})
109 # Use plain form for legacy user.
110 target_link_libraries(${executable} ${LIBRARIES})
112 endfunction(explicit_llvm_config)
116 function(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 )
120 endfunction(llvm_map_components_to_libraries)
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.
126 function(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)
132 if( TARGET LLVM${c}CodeGen )
133 list(APPEND expanded_components "${c}CodeGen")
135 if( TARGET LLVM${c} )
136 list(APPEND expanded_components "${c}")
138 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
141 if( TARGET LLVM${c}AsmPrinter )
142 list(APPEND expanded_components "${c}AsmPrinter")
144 if( TARGET LLVM${c}AsmParser )
145 list(APPEND expanded_components "${c}AsmParser")
147 if( TARGET LLVM${c}Desc )
148 list(APPEND expanded_components "${c}Desc")
150 if( TARGET LLVM${c}Disassembler )
151 list(APPEND expanded_components "${c}Disassembler")
153 if( TARGET LLVM${c}Info )
154 list(APPEND expanded_components "${c}Info")
156 if( TARGET LLVM${c}Utils )
157 list(APPEND expanded_components "${c}Utils")
159 elseif( c STREQUAL "nativecodegen" )
160 if( TARGET LLVM${LLVM_NATIVE_ARCH}CodeGen )
161 list(APPEND expanded_components "${LLVM_NATIVE_ARCH}CodeGen")
163 if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
164 list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Desc")
166 if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
167 list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Info")
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")
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")
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")
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")
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")
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")
212 list(APPEND expanded_components "${c}")
215 set(${out_components} ${expanded_components} PARENT_SCOPE)
216 endfunction(llvm_expand_pseudo_components out_components)
218 # This is a variant intended for the final user:
219 # Map LINK_COMPONENTS to actual libnames.
220 function(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)
226 string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
228 get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
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)
238 message(FATAL_ERROR "Specified target library before target registration is complete.")
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")
252 list(APPEND link_components "interpreter")
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})
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})
269 if( c STREQUAL "native" )
271 elseif( c STREQUAL "backend" )
272 # same case as in `native'.
273 elseif( c STREQUAL "engine" )
275 elseif( c STREQUAL "all" )
276 get_property(all_components GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
277 list(APPEND expanded_components ${all_components})
279 # Canonize the component name:
280 string(TOUPPER "${c}" capitalized)
281 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
283 # The component is unknown. Maybe is an omitted target?
284 is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
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.")
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})
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" )
302 set(${out_libs} ${expanded_components} PARENT_SCOPE)
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.
308 function(expand_topologically name required_libs visited_libs)
309 list(FIND visited_libs ${name} found)
311 list(APPEND visited_libs ${name})
312 set(visited_libs ${visited_libs} PARENT_SCOPE)
315 get_property(libname GLOBAL PROPERTY LLVM_COMPONENT_NAME_${name})
317 set(cname LLVM${libname})
318 elseif(TARGET ${name})
320 elseif(TARGET LLVM${name})
321 set(cname LLVM${name})
323 message(FATAL_ERROR "unknown component ${name}")
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)
333 list(APPEND required_libs ${cname})
334 set(required_libs ${required_libs} PARENT_SCOPE)
338 # Expand dependencies while topologically sorting the list of libraries:
339 function(llvm_expand_dependencies out_libs)
340 set(expanded_components ${ARGN})
344 foreach( lib ${expanded_components} )
345 expand_topologically(${lib} "${required_libs}" "${visited_libs}")
349 list(REVERSE required_libs)
351 set(${out_libs} ${required_libs} PARENT_SCOPE)
354 function(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:
359 foreach(c ${expanded_components})
361 set(result ${result} ${c})
364 set(${out_libs} ${result} PARENT_SCOPE)
365 endfunction(explicit_map_components_to_libraries)