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 list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
32 string(TOUPPER "${omitted_targets}" targets)
34 string(TOUPPER "${LLVM_ALL_TARGETS}" 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)
50 endfunction(is_llvm_target_library)
52 function(is_llvm_target_specifier library return_var)
53 is_llvm_target_library(${library} ${return_var} ${ARGN})
54 string(TOUPPER "${library}" capitalized_lib)
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)
67 macro(llvm_config executable)
68 cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
69 set(link_components ${ARG_UNPARSED_ARGUMENTS})
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.
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
82 if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
83 if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
84 set(link_components "")
86 list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
90 target_link_libraries(${executable} PRIVATE LLVM)
93 explicit_llvm_config(${executable} ${link_components})
97 function(explicit_llvm_config executable)
98 set( link_components ${ARGN} )
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})
107 # Use plain form for legacy user.
108 target_link_libraries(${executable} ${LIBRARIES})
110 endfunction(explicit_llvm_config)
114 function(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 )
118 endfunction(llvm_map_components_to_libraries)
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.
124 function(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)
130 if( TARGET LLVM${c}CodeGen )
131 list(APPEND expanded_components "${c}CodeGen")
133 if( TARGET LLVM${c} )
134 list(APPEND expanded_components "${c}")
136 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
139 if( TARGET LLVM${c}AsmPrinter )
140 list(APPEND expanded_components "${c}AsmPrinter")
142 if( TARGET LLVM${c}AsmParser )
143 list(APPEND expanded_components "${c}AsmParser")
145 if( TARGET LLVM${c}Desc )
146 list(APPEND expanded_components "${c}Desc")
148 if( TARGET LLVM${c}Disassembler )
149 list(APPEND expanded_components "${c}Disassembler")
151 if( TARGET LLVM${c}Info )
152 list(APPEND expanded_components "${c}Info")
154 if( TARGET LLVM${c}Utils )
155 list(APPEND expanded_components "${c}Utils")
157 elseif( c STREQUAL "nativecodegen" )
158 if( TARGET LLVM${LLVM_NATIVE_ARCH}CodeGen )
159 list(APPEND expanded_components "${LLVM_NATIVE_ARCH}CodeGen")
161 if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
162 list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Desc")
164 if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
165 list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Info")
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")
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")
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")
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")
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")
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")
210 list(APPEND expanded_components "${c}")
213 set(${out_components} ${expanded_components} PARENT_SCOPE)
214 endfunction(llvm_expand_pseudo_components out_components)
216 # This is a variant intended for the final user:
217 # Map LINK_COMPONENTS to actual libnames.
218 function(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)
224 string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
226 get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
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)
236 message(FATAL_ERROR "Specified target library before target registration is complete.")
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")
250 list(APPEND link_components "interpreter")
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})
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" )
265 elseif( c STREQUAL "backend" )
266 # same case as in `native'.
267 elseif( c STREQUAL "engine" )
269 elseif( c STREQUAL "all" )
270 list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
271 else( NOT idx LESS 0 )
272 # Canonize the component name:
273 string(TOUPPER "${c}" capitalized)
274 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
276 # The component is unknown. Maybe is an omitted target?
277 is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
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.")
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})
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" )
295 set(${out_libs} ${expanded_components} PARENT_SCOPE)
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.
301 function(expand_topologically name required_libs visited_libs)
302 list(FIND visited_libs ${name} found)
304 list(APPEND visited_libs ${name})
305 set(visited_libs ${visited_libs} PARENT_SCOPE)
307 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
308 foreach( lib_dep ${lib_deps} )
309 expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
310 set(required_libs ${required_libs} PARENT_SCOPE)
311 set(visited_libs ${visited_libs} PARENT_SCOPE)
314 list(APPEND required_libs ${name})
315 set(required_libs ${required_libs} PARENT_SCOPE)
319 # Expand dependencies while topologically sorting the list of libraries:
320 function(llvm_expand_dependencies out_libs)
321 set(expanded_components ${ARGN})
325 foreach( lib ${expanded_components} )
326 expand_topologically(${lib} "${required_libs}" "${visited_libs}")
330 list(REVERSE required_libs)
332 set(${out_libs} ${required_libs} PARENT_SCOPE)
335 function(explicit_map_components_to_libraries out_libs)
336 llvm_map_components_to_libnames(link_libs ${ARGN})
337 llvm_expand_dependencies(expanded_components ${link_libs})
338 # Return just the libraries included in this build:
340 foreach(c ${expanded_components})
342 set(result ${result} ${c})
345 set(${out_libs} ${result} PARENT_SCOPE)
346 endfunction(explicit_map_components_to_libraries)