2 cmake_policy(SET CMP0057 NEW)
4 function(get_system_libs return_var)
5 message(AUTHOR_WARNING "get_system_libs no longer needed")
6 set(${return_var} "" PARENT_SCOPE)
10 function(link_system_libs target)
11 message(AUTHOR_WARNING "link_system_libs no longer needed")
14 # is_llvm_target_library(
16 # Name of the LLVM library to check
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
24 function(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})
37 string(TOUPPER "${omitted_targets}" targets)
39 string(TOUPPER "${LLVM_ALL_TARGETS}" 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)
55 endfunction(is_llvm_target_library)
57 function(is_llvm_target_specifier library return_var)
58 is_llvm_target_library(${library} ${return_var} ${ARGN})
59 string(TOUPPER "${library}" capitalized_lib)
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)
72 macro(llvm_config executable)
73 cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
74 set(link_components ${ARG_UNPARSED_ARGUMENTS})
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.
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
87 if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
88 if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
89 set(link_components "")
91 list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
95 target_link_libraries(${executable} PRIVATE LLVM)
98 explicit_llvm_config(${executable} ${link_components})
102 function(explicit_llvm_config executable)
103 set( link_components ${ARGN} )
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})
112 # Use plain form for legacy user.
113 target_link_libraries(${executable} ${LIBRARIES})
115 endfunction(explicit_llvm_config)
119 function(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 )
123 endfunction(llvm_map_components_to_libraries)
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.
129 function(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)
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")
141 if(LLVM${c} IN_LIST LLVM_AVAILABLE_LIBS)
142 list(APPEND expanded_components "${c}")
144 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
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}")
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}")
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")
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")
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")
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")
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")
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")
201 list(APPEND expanded_components "${c}")
204 set(${out_components} ${expanded_components} PARENT_SCOPE)
205 endfunction(llvm_expand_pseudo_components out_components)
207 # This is a variant intended for the final user:
208 # Map LINK_COMPONENTS to actual libnames.
209 function(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)
215 string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
217 get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
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)
227 message(FATAL_ERROR "Specified target library before target registration is complete.")
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")
239 list(APPEND link_components "interpreter")
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})
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})
253 if("${c}" STREQUAL "native" )
255 elseif("${c}" STREQUAL "backend" )
256 # same case as in `native'.
257 elseif("${c}" STREQUAL "engine" )
259 elseif("${c}" STREQUAL "all" )
260 get_property(all_components GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
261 list(APPEND expanded_components ${all_components})
263 # Canonize the component name:
264 string(TOUPPER "${c}" capitalized)
265 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
267 # The component is unknown. Maybe is an omitted target?
268 is_llvm_target_library("${c}" iltl_result OMITTED_TARGETS)
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.")
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})
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" )
286 set(${out_libs} ${expanded_components} PARENT_SCOPE)
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.
292 function(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)
298 get_property(libname GLOBAL PROPERTY LLVM_COMPONENT_NAME_${name})
300 set(cname LLVM${libname})
301 elseif(TARGET ${name})
303 elseif(TARGET LLVM${name})
304 set(cname LLVM${name})
306 message(FATAL_ERROR "unknown component ${name}")
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)
316 list(APPEND required_libs ${cname})
317 set(required_libs ${required_libs} PARENT_SCOPE)
321 # Expand dependencies while topologically sorting the list of libraries:
322 function(llvm_expand_dependencies out_libs)
323 set(expanded_components ${ARGN})
327 foreach( lib ${expanded_components} )
328 expand_topologically(${lib} "${required_libs}" "${visited_libs}")
332 list(REVERSE required_libs)
334 set(${out_libs} ${required_libs} PARENT_SCOPE)
337 function(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:
342 foreach(c ${expanded_components})
344 set(result ${result} ${c})
347 set(${out_libs} ${result} PARENT_SCOPE)
348 endfunction(explicit_map_components_to_libraries)