summaryrefslogtreecommitdiff
path: root/cmake/modules/LLVM-Config.cmake
blob: 9cce1a2d4bca456556921673970a8fc13b17461f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function(get_system_libs return_var)
  message(AUTHOR_WARNING "get_system_libs no longer needed")
  set(${return_var} "" PARENT_SCOPE)
endfunction()


function(link_system_libs target)
  message(AUTHOR_WARNING "link_system_libs no longer needed")
endfunction()

# is_llvm_target_library(
#   library
#     Name of the LLVM library to check
#   return_var
#     Output variable name
#   ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS
#     ALL_TARGETS - default looks at the full list of known targets
#     INCLUDED_TARGETS - looks only at targets being configured
#     OMITTED_TARGETS - looks only at targets that are not being configured
# )
function(is_llvm_target_library library return_var)
  cmake_parse_arguments(ARG "ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS" "" "" ${ARGN})
  # Sets variable `return_var' to ON if `library' corresponds to a
  # LLVM supported target. To OFF if it doesn't.
  set(${return_var} OFF PARENT_SCOPE)
  string(TOUPPER "${library}" capitalized_lib)
  if(ARG_INCLUDED_TARGETS)
    string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" targets)
  elseif(ARG_OMITTED_TARGETS)
    set(omitted_targets ${LLVM_ALL_TARGETS})
    list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
    string(TOUPPER "${omitted_targets}" targets)
  else()
    string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
  endif()
  foreach(t ${targets})
    if( capitalized_lib STREQUAL t OR
        capitalized_lib STREQUAL "${t}" OR
        capitalized_lib STREQUAL "${t}DESC" OR
        capitalized_lib STREQUAL "${t}CODEGEN" OR
        capitalized_lib STREQUAL "${t}ASMPARSER" OR
        capitalized_lib STREQUAL "${t}ASMPRINTER" OR
        capitalized_lib STREQUAL "${t}DISASSEMBLER" OR
        capitalized_lib STREQUAL "${t}INFO" OR
        capitalized_lib STREQUAL "${t}UTILS" )
      set(${return_var} ON PARENT_SCOPE)
      break()
    endif()
  endforeach()
endfunction(is_llvm_target_library)

function(is_llvm_target_specifier library return_var)
  is_llvm_target_library(${library} ${return_var} ${ARGN})
  string(TOUPPER "${library}" capitalized_lib)
  if(NOT ${return_var})
    if( capitalized_lib STREQUAL "ALLTARGETSASMPARSERS" OR
        capitalized_lib STREQUAL "ALLTARGETSDESCS" OR
        capitalized_lib STREQUAL "ALLTARGETSDISASSEMBLERS" OR
        capitalized_lib STREQUAL "ALLTARGETSINFOS" OR
        capitalized_lib STREQUAL "NATIVE" OR
        capitalized_lib STREQUAL "NATIVECODEGEN" )
      set(${return_var} ON PARENT_SCOPE)
    endif()
  endif()
endfunction()

macro(llvm_config executable)
  cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
  set(link_components ${ARG_UNPARSED_ARGUMENTS})

  if(USE_SHARED)
    # If USE_SHARED is specified, then we link against libLLVM,
    # but also against the component libraries below. This is
    # done in case libLLVM does not contain all of the components
    # the target requires.
    #
    # Strip LLVM_DYLIB_COMPONENTS out of link_components.
    # To do this, we need special handling for "all", since that
    # may imply linking to libraries that are not included in
    # libLLVM.

    if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
      if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
        set(link_components "")
      else()
        list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
      endif()
    endif()

    target_link_libraries(${executable} LLVM)
  endif()

  explicit_llvm_config(${executable} ${link_components})
endmacro(llvm_config)


function(explicit_llvm_config executable)
  set( link_components ${ARGN} )

  llvm_map_components_to_libnames(LIBRARIES ${link_components})
  get_target_property(t ${executable} TYPE)
  if(t STREQUAL "STATIC_LIBRARY")
    target_link_libraries(${executable} INTERFACE ${LIBRARIES})
  elseif(t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
    target_link_libraries(${executable} PRIVATE ${LIBRARIES})
  else()
    # Use plain form for legacy user.
    target_link_libraries(${executable} ${LIBRARIES})
  endif()
endfunction(explicit_llvm_config)


# This is Deprecated
function(llvm_map_components_to_libraries OUT_VAR)
  message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
  explicit_map_components_to_libraries(result ${ARGN})
  set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
endfunction(llvm_map_components_to_libraries)

# This is a variant intended for the final user:
# Map LINK_COMPONENTS to actual libnames.
function(llvm_map_components_to_libnames out_libs)
  set( link_components ${ARGN} )
  if(NOT LLVM_AVAILABLE_LIBS)
    # Inside LLVM itself available libs are in a global property.
    get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
  endif()
  string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)

  get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)

  # Generally in our build system we avoid order-dependence. Unfortunately since
  # not all targets create the same set of libraries we actually need to ensure
  # that all build targets associated with a target are added before we can
  # process target dependencies.
  if(NOT LLVM_TARGETS_CONFIGURED)
    foreach(c ${link_components})
      is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
      if(iltl_result)
        message(FATAL_ERROR "Specified target library before target registration is complete.")
      endif()
    endforeach()
  endif()

  # Expand some keywords:
  list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
  list(FIND link_components "engine" engine_required)
  if( NOT engine_required EQUAL -1 )
    list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
    if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
      list(APPEND link_components "jit")
      list(APPEND link_components "native")
    else()
      list(APPEND link_components "interpreter")
    endif()
  endif()
  list(FIND link_components "native" native_required)
  if( NOT native_required EQUAL -1 )
    if( NOT have_native_backend EQUAL -1 )
      list(APPEND link_components ${LLVM_NATIVE_ARCH})
    endif()
  endif()

  # Translate symbolic component names to real libraries:
  foreach(c ${link_components})
    # add codegen, asmprinter, asmparser, disassembler
    list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
    if( NOT idx LESS 0 )
      if( TARGET LLVM${c}CodeGen )
        list(APPEND expanded_components "LLVM${c}CodeGen")
      else()
        if( TARGET LLVM${c} )
          list(APPEND expanded_components "LLVM${c}")
        else()
          message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
        endif()
      endif()
      if( TARGET LLVM${c}AsmParser )
        list(APPEND expanded_components "LLVM${c}AsmParser")
      endif()
      if( TARGET LLVM${c}AsmPrinter )
        list(APPEND expanded_components "LLVM${c}AsmPrinter")
      endif()
      if( TARGET LLVM${c}Desc )
        list(APPEND expanded_components "LLVM${c}Desc")
      endif()
      if( TARGET LLVM${c}Disassembler )
        list(APPEND expanded_components "LLVM${c}Disassembler")
      endif()
      if( TARGET LLVM${c}Info )
        list(APPEND expanded_components "LLVM${c}Info")
      endif()
      if( TARGET LLVM${c}Utils )
        list(APPEND expanded_components "LLVM${c}Utils")
      endif()
    elseif( c STREQUAL "native" )
      # already processed
    elseif( c STREQUAL "nativecodegen" )
      list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
      if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
        list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Desc")
      endif()
      if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
        list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Info")
      endif()
    elseif( c STREQUAL "backend" )
      # same case as in `native'.
    elseif( c STREQUAL "engine" )
      # already processed
    elseif( c STREQUAL "all" )
      list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
    elseif( c STREQUAL "AllTargetsAsmPrinters" )
      # Link all the asm printers from all the targets
      foreach(t ${LLVM_TARGETS_TO_BUILD})
        if( TARGET LLVM${t}AsmPrinter )
          list(APPEND expanded_components "LLVM${t}AsmPrinter")
        endif()
      endforeach(t)
    elseif( c STREQUAL "AllTargetsAsmParsers" )
      # Link all the asm parsers from all the targets
      foreach(t ${LLVM_TARGETS_TO_BUILD})
        if( TARGET LLVM${t}AsmParser )
          list(APPEND expanded_components "LLVM${t}AsmParser")
        endif()
      endforeach(t)
    elseif( c STREQUAL "AllTargetsDescs" )
      # Link all the descs from all the targets
      foreach(t ${LLVM_TARGETS_TO_BUILD})
        if( TARGET LLVM${t}Desc )
          list(APPEND expanded_components "LLVM${t}Desc")
        endif()
      endforeach(t)
    elseif( c STREQUAL "AllTargetsDisassemblers" )
      # Link all the disassemblers from all the targets
      foreach(t ${LLVM_TARGETS_TO_BUILD})
        if( TARGET LLVM${t}Disassembler )
          list(APPEND expanded_components "LLVM${t}Disassembler")
        endif()
      endforeach(t)
    elseif( c STREQUAL "AllTargetsInfos" )
      # Link all the infos from all the targets
      foreach(t ${LLVM_TARGETS_TO_BUILD})
        if( TARGET LLVM${t}Info )
          list(APPEND expanded_components "LLVM${t}Info")
        endif()
      endforeach(t)
    else( NOT idx LESS 0 )
      # Canonize the component name:
      string(TOUPPER "${c}" capitalized)
      list(FIND capitalized_libs LLVM${capitalized} lib_idx)
      if( lib_idx LESS 0 )
        # The component is unknown. Maybe is an omitted target?
        is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
        if(iltl_result)
          # A missing library to a directly referenced omitted target would be bad.
          message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
        else()
          # If it is not an omitted target we should assume it is a component
          # that hasn't yet been processed by CMake. Missing components will
          # cause errors later in the configuration, so we can safely assume
          # that this is valid here.
          list(APPEND expanded_components LLVM${c})
        endif()
      else( lib_idx LESS 0 )
        list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
        list(APPEND expanded_components ${canonical_lib})
      endif( lib_idx LESS 0 )
    endif( NOT idx LESS 0 )
  endforeach(c)

  set(${out_libs} ${expanded_components} PARENT_SCOPE)
endfunction()

# Perform a post-order traversal of the dependency graph.
# This duplicates the algorithm used by llvm-config, originally
# in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
function(expand_topologically name required_libs visited_libs)
  list(FIND visited_libs ${name} found)
  if( found LESS 0 )
    list(APPEND visited_libs ${name})
    set(visited_libs ${visited_libs} PARENT_SCOPE)

    get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
    foreach( lib_dep ${lib_deps} )
      expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
      set(required_libs ${required_libs} PARENT_SCOPE)
      set(visited_libs ${visited_libs} PARENT_SCOPE)
    endforeach()

    list(APPEND required_libs ${name})
    set(required_libs ${required_libs} PARENT_SCOPE)
  endif()
endfunction()

# Expand dependencies while topologically sorting the list of libraries:
function(llvm_expand_dependencies out_libs)
  set(expanded_components ${ARGN})

  set(required_libs)
  set(visited_libs)
  foreach( lib ${expanded_components} )
    expand_topologically(${lib} "${required_libs}" "${visited_libs}")
  endforeach()

  list(REVERSE required_libs)
  set(${out_libs} ${required_libs} PARENT_SCOPE)
endfunction()

function(explicit_map_components_to_libraries out_libs)
  llvm_map_components_to_libnames(link_libs ${ARGN})
  llvm_expand_dependencies(expanded_components ${link_libs})
  # Return just the libraries included in this build:
  set(result)
  foreach(c ${expanded_components})
    if( TARGET ${c} )
      set(result ${result} ${c})
    endif()
  endforeach(c)
  set(${out_libs} ${result} PARENT_SCOPE)
endfunction(explicit_map_components_to_libraries)