blob: 474a874a4426ec873351595752618499f5c88f38 [file] [log] [blame]
Michael J. Spencer3a210e22010-09-13 23:59:48 +00001function(get_system_libs return_var)
NAKAMURA Takumi043cc542014-02-23 06:27:04 +00002 message(AUTHOR_WARNING "get_system_libs no longer needed")
3 set(${return_var} "" PARENT_SCOPE)
4endfunction()
Michael J. Spencer3a210e22010-09-13 23:59:48 +00005
6
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +00007function(link_system_libs target)
NAKAMURA Takumi043cc542014-02-23 06:27:04 +00008 message(AUTHOR_WARNING "link_system_libs no longer needed")
9endfunction()
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000010
Chris Bieneman405386b2017-02-08 20:58:37 +000011# 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# )
Michael J. Spencer3a210e22010-09-13 23:59:48 +000021function(is_llvm_target_library library return_var)
Chris Bieneman405386b2017-02-08 20:58:37 +000022 cmake_parse_arguments(ARG "ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS" "" "" ${ARGN})
Michael J. Spencer3a210e22010-09-13 23:59:48 +000023 # 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)
Chris Bieneman405386b2017-02-08 20:58:37 +000027 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)
33 else()
34 string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
35 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +000036 foreach(t ${targets})
Oscar Fuentes67044152011-03-15 14:53:53 +000037 if( capitalized_lib STREQUAL t OR
Chris Bieneman405386b2017-02-08 20:58:37 +000038 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" )
Michael J. Spencer3a210e22010-09-13 23:59:48 +000046 set(${return_var} ON PARENT_SCOPE)
47 break()
48 endif()
49 endforeach()
50endfunction(is_llvm_target_library)
51
Chris Bieneman405386b2017-02-08 20:58:37 +000052function(is_llvm_target_specifier library return_var)
53 is_llvm_target_library(${library} ${return_var} ${ARGN})
54 string(TOUPPER "${library}" capitalized_lib)
55 if(NOT ${return_var})
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)
63 endif()
64 endif()
65endfunction()
Michael J. Spencer3a210e22010-09-13 23:59:48 +000066
67macro(llvm_config executable)
Andrew Wilkinsf5148eb2015-09-05 08:27:33 +000068 cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
69 set(link_components ${ARG_UNPARSED_ARGUMENTS})
70
Pavel Labathd2c63832018-06-06 10:07:08 +000071 if(ARG_USE_SHARED)
Andrew Wilkinsf5148eb2015-09-05 08:27:33 +000072 # 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.
76 #
Andrew Wilkins724b31e2016-02-12 01:42:43 +000077 # Strip LLVM_DYLIB_COMPONENTS out of link_components.
Andrew Wilkinsf5148eb2015-09-05 08:27:33 +000078 # To do this, we need special handling for "all", since that
79 # may imply linking to libraries that are not included in
80 # libLLVM.
Andrew Wilkins724b31e2016-02-12 01:42:43 +000081
82 if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
83 if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
84 set(link_components "")
85 else()
86 list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
87 endif()
88 endif()
89
Shoaib Meenai4a6bb532017-12-05 21:49:56 +000090 target_link_libraries(${executable} PRIVATE LLVM)
Andrew Wilkinsf5148eb2015-09-05 08:27:33 +000091 endif()
92
93 explicit_llvm_config(${executable} ${link_components})
Michael J. Spencer3a210e22010-09-13 23:59:48 +000094endmacro(llvm_config)
95
96
97function(explicit_llvm_config executable)
98 set( link_components ${ARGN} )
99
NAKAMURA Takumib1e7bec2014-02-10 03:24:19 +0000100 llvm_map_components_to_libnames(LIBRARIES ${link_components})
NAKAMURA Takumi07f95b32014-02-26 06:53:16 +0000101 get_target_property(t ${executable} TYPE)
Shoaib Meenai5eb46492017-12-05 01:19:48 +0000102 if(t STREQUAL "STATIC_LIBRARY")
Chris Bieneman0c9684c2015-03-23 20:03:57 +0000103 target_link_libraries(${executable} INTERFACE ${LIBRARIES})
Shoaib Meenai4a6bb532017-12-05 21:49:56 +0000104 elseif(t STREQUAL "EXECUTABLE" OR t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
Chris Bieneman0c9684c2015-03-23 20:03:57 +0000105 target_link_libraries(${executable} PRIVATE ${LIBRARIES})
NAKAMURA Takumi07f95b32014-02-26 06:53:16 +0000106 else()
107 # Use plain form for legacy user.
108 target_link_libraries(${executable} ${LIBRARIES})
109 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000110endfunction(explicit_llvm_config)
111
112
Dan Liewb3768f62014-07-28 13:36:50 +0000113# This is Deprecated
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000114function(llvm_map_components_to_libraries OUT_VAR)
Dan Liewb3768f62014-07-28 13:36:50 +0000115 message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000116 explicit_map_components_to_libraries(result ${ARGN})
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000117 set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
118endfunction(llvm_map_components_to_libraries)
119
Dan Liewb3768f62014-07-28 13:36:50 +0000120# This is a variant intended for the final user:
NAKAMURA Takumic4c93c12014-02-04 14:42:04 +0000121# Map LINK_COMPONENTS to actual libnames.
122function(llvm_map_components_to_libnames out_libs)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000123 set( link_components ${ARGN} )
NAKAMURA Takumi6eeedf32014-02-21 14:17:07 +0000124 if(NOT LLVM_AVAILABLE_LIBS)
125 # Inside LLVM itself available libs are in a global property.
126 get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
127 endif()
128 string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000129
Chris Bieneman405386b2017-02-08 20:58:37 +0000130 get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
131
132 # Generally in our build system we avoid order-dependence. Unfortunately since
133 # not all targets create the same set of libraries we actually need to ensure
134 # that all build targets associated with a target are added before we can
135 # process target dependencies.
136 if(NOT LLVM_TARGETS_CONFIGURED)
137 foreach(c ${link_components})
138 is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
139 if(iltl_result)
140 message(FATAL_ERROR "Specified target library before target registration is complete.")
141 endif()
142 endforeach()
143 endif()
144
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000145 # Expand some keywords:
Oscar Fuentesdb3480c2011-03-23 17:42:13 +0000146 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000147 list(FIND link_components "engine" engine_required)
Oscar Fuentesdb3480c2011-03-23 17:42:13 +0000148 if( NOT engine_required EQUAL -1 )
149 list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
150 if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
151 list(APPEND link_components "jit")
152 list(APPEND link_components "native")
153 else()
154 list(APPEND link_components "interpreter")
155 endif()
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000156 endif()
157 list(FIND link_components "native" native_required)
Oscar Fuentesdb3480c2011-03-23 17:42:13 +0000158 if( NOT native_required EQUAL -1 )
159 if( NOT have_native_backend EQUAL -1 )
160 list(APPEND link_components ${LLVM_NATIVE_ARCH})
161 endif()
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000162 endif()
163
Oscar Fuentes44258d12010-09-28 22:38:39 +0000164 # Translate symbolic component names to real libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000165 foreach(c ${link_components})
166 # add codegen, asmprinter, asmparser, disassembler
167 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
168 if( NOT idx LESS 0 )
NAKAMURA Takumi83782f22014-02-21 14:16:52 +0000169 if( TARGET LLVM${c}CodeGen )
NAKAMURA Takumide2d83b2014-02-02 16:46:35 +0000170 list(APPEND expanded_components "LLVM${c}CodeGen")
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000171 else()
NAKAMURA Takumi83782f22014-02-21 14:16:52 +0000172 if( TARGET LLVM${c} )
NAKAMURA Takumide2d83b2014-02-02 16:46:35 +0000173 list(APPEND expanded_components "LLVM${c}")
174 else()
175 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
176 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000177 endif()
NAKAMURA Takumi83782f22014-02-21 14:16:52 +0000178 if( TARGET LLVM${c}AsmParser )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000179 list(APPEND expanded_components "LLVM${c}AsmParser")
180 endif()
Oleg Ranevskyyfc657592017-08-10 13:37:58 +0000181 if( TARGET LLVM${c}AsmPrinter )
182 list(APPEND expanded_components "LLVM${c}AsmPrinter")
183 endif()
NAKAMURA Takumi13764f72014-07-14 05:07:07 +0000184 if( TARGET LLVM${c}Desc )
185 list(APPEND expanded_components "LLVM${c}Desc")
186 endif()
NAKAMURA Takumi83782f22014-02-21 14:16:52 +0000187 if( TARGET LLVM${c}Disassembler )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000188 list(APPEND expanded_components "LLVM${c}Disassembler")
189 endif()
Chris Bieneman405386b2017-02-08 20:58:37 +0000190 if( TARGET LLVM${c}Info )
191 list(APPEND expanded_components "LLVM${c}Info")
192 endif()
193 if( TARGET LLVM${c}Utils )
194 list(APPEND expanded_components "LLVM${c}Utils")
195 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000196 elseif( c STREQUAL "native" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000197 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000198 elseif( c STREQUAL "nativecodegen" )
199 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
NAKAMURA Takumi13764f72014-07-14 05:07:07 +0000200 if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
201 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Desc")
202 endif()
203 if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
204 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Info")
205 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000206 elseif( c STREQUAL "backend" )
207 # same case as in `native'.
208 elseif( c STREQUAL "engine" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000209 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000210 elseif( c STREQUAL "all" )
NAKAMURA Takumi6eeedf32014-02-21 14:17:07 +0000211 list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
Pete Coopere6308652015-04-20 18:22:05 +0000212 elseif( c STREQUAL "AllTargetsAsmPrinters" )
213 # Link all the asm printers from all the targets
214 foreach(t ${LLVM_TARGETS_TO_BUILD})
215 if( TARGET LLVM${t}AsmPrinter )
216 list(APPEND expanded_components "LLVM${t}AsmPrinter")
217 endif()
218 endforeach(t)
219 elseif( c STREQUAL "AllTargetsAsmParsers" )
220 # Link all the asm parsers from all the targets
221 foreach(t ${LLVM_TARGETS_TO_BUILD})
222 if( TARGET LLVM${t}AsmParser )
223 list(APPEND expanded_components "LLVM${t}AsmParser")
224 endif()
225 endforeach(t)
226 elseif( c STREQUAL "AllTargetsDescs" )
227 # Link all the descs from all the targets
228 foreach(t ${LLVM_TARGETS_TO_BUILD})
229 if( TARGET LLVM${t}Desc )
230 list(APPEND expanded_components "LLVM${t}Desc")
231 endif()
232 endforeach(t)
233 elseif( c STREQUAL "AllTargetsDisassemblers" )
234 # Link all the disassemblers from all the targets
235 foreach(t ${LLVM_TARGETS_TO_BUILD})
236 if( TARGET LLVM${t}Disassembler )
237 list(APPEND expanded_components "LLVM${t}Disassembler")
238 endif()
239 endforeach(t)
240 elseif( c STREQUAL "AllTargetsInfos" )
241 # Link all the infos from all the targets
242 foreach(t ${LLVM_TARGETS_TO_BUILD})
243 if( TARGET LLVM${t}Info )
244 list(APPEND expanded_components "LLVM${t}Info")
245 endif()
246 endforeach(t)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000247 else( NOT idx LESS 0 )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000248 # Canonize the component name:
249 string(TOUPPER "${c}" capitalized)
250 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
251 if( lib_idx LESS 0 )
Chandler Carruth2e1513d2011-07-29 23:52:01 +0000252 # The component is unknown. Maybe is an omitted target?
Chris Bieneman405386b2017-02-08 20:58:37 +0000253 is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
254 if(iltl_result)
255 # A missing library to a directly referenced omitted target would be bad.
256 message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
257 else()
258 # If it is not an omitted target we should assume it is a component
259 # that hasn't yet been processed by CMake. Missing components will
260 # cause errors later in the configuration, so we can safely assume
261 # that this is valid here.
262 list(APPEND expanded_components LLVM${c})
Chandler Carruth2e1513d2011-07-29 23:52:01 +0000263 endif()
Oscar Fuentes44258d12010-09-28 22:38:39 +0000264 else( lib_idx LESS 0 )
NAKAMURA Takumi6eeedf32014-02-21 14:17:07 +0000265 list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
Chandler Carruth2e1513d2011-07-29 23:52:01 +0000266 list(APPEND expanded_components ${canonical_lib})
Oscar Fuentes44258d12010-09-28 22:38:39 +0000267 endif( lib_idx LESS 0 )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000268 endif( NOT idx LESS 0 )
269 endforeach(c)
NAKAMURA Takumic4c93c12014-02-04 14:42:04 +0000270
271 set(${out_libs} ${expanded_components} PARENT_SCOPE)
272endfunction()
273
Peter Zotov827f4eb2014-12-18 23:56:52 +0000274# Perform a post-order traversal of the dependency graph.
275# This duplicates the algorithm used by llvm-config, originally
276# in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
277function(expand_topologically name required_libs visited_libs)
278 list(FIND visited_libs ${name} found)
279 if( found LESS 0 )
280 list(APPEND visited_libs ${name})
281 set(visited_libs ${visited_libs} PARENT_SCOPE)
282
283 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
284 foreach( lib_dep ${lib_deps} )
285 expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
286 set(required_libs ${required_libs} PARENT_SCOPE)
287 set(visited_libs ${visited_libs} PARENT_SCOPE)
288 endforeach()
289
290 list(APPEND required_libs ${name})
291 set(required_libs ${required_libs} PARENT_SCOPE)
292 endif()
293endfunction()
294
NAKAMURA Takumic4c93c12014-02-04 14:42:04 +0000295# Expand dependencies while topologically sorting the list of libraries:
296function(llvm_expand_dependencies out_libs)
297 set(expanded_components ${ARGN})
Peter Zotov827f4eb2014-12-18 23:56:52 +0000298
299 set(required_libs)
300 set(visited_libs)
301 foreach( lib ${expanded_components} )
302 expand_topologically(${lib} "${required_libs}" "${visited_libs}")
303 endforeach()
304
Sven van Haastregtcddb21c2018-09-26 10:14:10 +0000305 if(required_libs)
306 list(REVERSE required_libs)
307 endif()
Peter Zotov827f4eb2014-12-18 23:56:52 +0000308 set(${out_libs} ${required_libs} PARENT_SCOPE)
NAKAMURA Takumic4c93c12014-02-04 14:42:04 +0000309endfunction()
310
311function(explicit_map_components_to_libraries out_libs)
312 llvm_map_components_to_libnames(link_libs ${ARGN})
313 llvm_expand_dependencies(expanded_components ${link_libs})
Oscar Fuentes44258d12010-09-28 22:38:39 +0000314 # Return just the libraries included in this build:
315 set(result)
316 foreach(c ${expanded_components})
NAKAMURA Takumi83782f22014-02-21 14:16:52 +0000317 if( TARGET ${c} )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000318 set(result ${result} ${c})
319 endif()
320 endforeach(c)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000321 set(${out_libs} ${result} PARENT_SCOPE)
322endfunction(explicit_map_components_to_libraries)