1include(GNUInstallDirs)
2include(LLVMDistributionSupport)
3
4# Clear out any pre-existing compile_commands file before processing. This
5# allows for generating a clean compile_commands on each configure.
6file(REMOVE ${CMAKE_BINARY_DIR}/tablegen_compile_commands.yml)
7
8function(mlir_tablegen ofn)
9  tablegen(MLIR ${ARGV})
10  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
11      PARENT_SCOPE)
12
13  # Get the current set of include paths for this td file.
14  cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES" ${ARGN})
15  get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
16  list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
17  # Filter out any empty include items.
18  list(REMOVE_ITEM tblgen_includes "")
19
20  # Build the absolute path for the current input file.
21  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
22    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
23  else()
24    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
25  endif()
26
27  # Append the includes used for this file to the tablegen_compile_commands
28  # file.
29  file(APPEND ${CMAKE_BINARY_DIR}/tablegen_compile_commands.yml
30      "--- !FileInfo:\n"
31      "  filepath: \"${LLVM_TARGET_DEFINITIONS_ABSOLUTE}\"\n"
32      "  includes: \"${CMAKE_CURRENT_SOURCE_DIR};${tblgen_includes}\"\n"
33  )
34endfunction()
35
36# Clear out any pre-existing compile_commands file before processing. This
37# allows for generating a clean compile_commands on each configure.
38file(REMOVE ${CMAKE_BINARY_DIR}/pdll_compile_commands.yml)
39
40# Declare a PDLL library in the current directory.
41function(add_mlir_pdll_library target inputFile ofn)
42  set(LLVM_TARGET_DEFINITIONS ${inputFile})
43
44  tablegen(MLIR_PDLL ${ofn} -x=cpp ${ARGN})
45  set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
46      PARENT_SCOPE)
47
48  # Get the current set of include paths for this pdll file.
49  cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES" ${ARGN})
50  get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
51  list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
52  # Filter out any empty include items.
53  list(REMOVE_ITEM tblgen_includes "")
54
55  # Build the absolute path for the current input file.
56  if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
57    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${inputFile})
58  else()
59    set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/${inputFile})
60  endif()
61
62  # Append the includes used for this file to the pdll_compilation_commands
63  # file.
64  file(APPEND ${CMAKE_BINARY_DIR}/pdll_compile_commands.yml
65      "--- !FileInfo:\n"
66      "  filepath: \"${LLVM_TARGET_DEFINITIONS_ABSOLUTE}\"\n"
67      "  includes: \"${CMAKE_CURRENT_SOURCE_DIR};${tblgen_includes}\"\n"
68  )
69
70  add_public_tablegen_target(${target})
71endfunction()
72
73# Declare a dialect in the include directory
74function(add_mlir_dialect dialect dialect_namespace)
75  set(LLVM_TARGET_DEFINITIONS ${dialect}.td)
76  mlir_tablegen(${dialect}.h.inc -gen-op-decls)
77  mlir_tablegen(${dialect}.cpp.inc -gen-op-defs)
78  mlir_tablegen(${dialect}Types.h.inc -gen-typedef-decls -typedefs-dialect=${dialect_namespace})
79  mlir_tablegen(${dialect}Types.cpp.inc -gen-typedef-defs -typedefs-dialect=${dialect_namespace})
80  mlir_tablegen(${dialect}Dialect.h.inc -gen-dialect-decls -dialect=${dialect_namespace})
81  mlir_tablegen(${dialect}Dialect.cpp.inc -gen-dialect-defs -dialect=${dialect_namespace})
82  add_public_tablegen_target(MLIR${dialect}IncGen)
83  add_dependencies(mlir-headers MLIR${dialect}IncGen)
84endfunction()
85
86# Declare a dialect in the include directory
87function(add_mlir_interface interface)
88  set(LLVM_TARGET_DEFINITIONS ${interface}.td)
89  mlir_tablegen(${interface}.h.inc -gen-op-interface-decls)
90  mlir_tablegen(${interface}.cpp.inc -gen-op-interface-defs)
91  add_public_tablegen_target(MLIR${interface}IncGen)
92  add_dependencies(mlir-generic-headers MLIR${interface}IncGen)
93endfunction()
94
95
96# Generate Documentation
97function(add_mlir_doc doc_filename output_file output_directory command)
98  set(LLVM_TARGET_DEFINITIONS ${doc_filename}.td)
99  tablegen(MLIR ${output_file}.md ${command} ${ARGN})
100  set(GEN_DOC_FILE ${MLIR_BINARY_DIR}/docs/${output_directory}${output_file}.md)
101  add_custom_command(
102          OUTPUT ${GEN_DOC_FILE}
103          COMMAND ${CMAKE_COMMAND} -E copy
104                  ${CMAKE_CURRENT_BINARY_DIR}/${output_file}.md
105                  ${GEN_DOC_FILE}
106          DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output_file}.md)
107  add_custom_target(${output_file}DocGen DEPENDS ${GEN_DOC_FILE})
108  add_dependencies(mlir-doc ${output_file}DocGen)
109endfunction()
110
111# Declare an mlir library which can be compiled in libMLIR.so
112# In addition to everything that llvm_add_librar accepts, this
113# also has the following option:
114# EXCLUDE_FROM_LIBMLIR
115#   Don't include this library in libMLIR.so.  This option should be used
116#   for test libraries, executable-specific libraries, or rarely used libraries
117#   with large dependencies.
118# ENABLE_AGGREGATION
119#   Forces generation of an OBJECT library, exports additional metadata,
120#   and installs additional object files needed to include this as part of an
121#   aggregate shared library.
122#   TODO: Make this the default for all MLIR libraries once all libraries
123#   are compatible with building an object library.
124function(add_mlir_library name)
125  cmake_parse_arguments(ARG
126    "SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
127    ""
128    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
129    ${ARGN})
130  set(srcs)
131  if(MSVC_IDE OR XCODE)
132    # Add public headers
133    file(RELATIVE_PATH lib_path
134      ${MLIR_SOURCE_DIR}/lib/
135      ${CMAKE_CURRENT_SOURCE_DIR}
136    )
137    if(NOT lib_path MATCHES "^[.][.]")
138      file( GLOB_RECURSE headers
139        ${MLIR_SOURCE_DIR}/include/mlir/${lib_path}/*.h
140        ${MLIR_SOURCE_DIR}/include/mlir/${lib_path}/*.def
141      )
142      set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
143
144      file( GLOB_RECURSE tds
145        ${MLIR_SOURCE_DIR}/include/mlir/${lib_path}/*.td
146      )
147      source_group("TableGen descriptions" FILES ${tds})
148      set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
149
150      if(headers OR tds)
151        set(srcs ${headers} ${tds})
152      endif()
153    endif()
154  endif(MSVC_IDE OR XCODE)
155  if(srcs OR ARG_ADDITIONAL_HEADERS)
156    set(srcs
157      ADDITIONAL_HEADERS
158      ${srcs}
159      ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
160      )
161  endif()
162
163  # Is an object library needed.
164  set(NEEDS_OBJECT_LIB OFF)
165  if(ARG_ENABLE_AGGREGATION)
166    set(NEEDS_OBJECT_LIB ON)
167  endif()
168
169  # Determine type of library.
170  if(ARG_SHARED)
171    set(LIBTYPE SHARED)
172  else()
173    # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
174    # so we need to handle it here.
175    if(BUILD_SHARED_LIBS)
176      set(LIBTYPE SHARED)
177    else()
178      set(LIBTYPE STATIC)
179    endif()
180    # Test libraries and such shouldn't be include in libMLIR.so
181    if(NOT ARG_EXCLUDE_FROM_LIBMLIR)
182      set(NEEDS_OBJECT_LIB ON)
183      set_property(GLOBAL APPEND PROPERTY MLIR_STATIC_LIBS ${name})
184      set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
185      set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
186    endif()
187  endif()
188
189  if(NEEDS_OBJECT_LIB AND NOT XCODE)
190    # The Xcode generator doesn't handle object libraries correctly.
191    # We special case xcode when building aggregates.
192    list(APPEND LIBTYPE OBJECT)
193  endif()
194
195  # MLIR libraries uniformly depend on LLVMSupport.  Just specify it once here.
196  list(APPEND ARG_LINK_COMPONENTS Support)
197
198  # LINK_COMPONENTS is necessary to allow libLLVM.so to be properly
199  # substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB
200  # Perhaps this should be in llvm_add_library instead?  However, it fails
201  # on libclang-cpp.so
202  get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
203  foreach(lib ${ARG_LINK_LIBS})
204    if(${lib} IN_LIST llvm_component_libs)
205      message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries.  Please use LINK_COMPONENTS instead.")
206    endif()
207  endforeach()
208
209  list(APPEND ARG_DEPENDS mlir-generic-headers)
210  llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS})
211
212  if(TARGET ${name})
213    target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
214    if(NOT ARG_DISABLE_INSTALL)
215      add_mlir_library_install(${name})
216    endif()
217  else()
218    # Add empty "phony" target
219    add_custom_target(${name})
220  endif()
221  set_target_properties(${name} PROPERTIES FOLDER "MLIR libraries")
222
223  # Setup aggregate.
224  if(ARG_ENABLE_AGGREGATION)
225    # Compute and store the properties needed to build aggregates.
226    set(AGGREGATE_OBJECTS)
227    set(AGGREGATE_OBJECT_LIB)
228    set(AGGREGATE_DEPS)
229    if(XCODE)
230      # XCode has limited support for object libraries. Instead, add dep flags
231      # that force the entire library to be embedded.
232      list(APPEND AGGREGATE_DEPS "-force_load" "${name}")
233    else()
234      list(APPEND AGGREGATE_OBJECTS "$<TARGET_OBJECTS:obj.${name}>")
235      list(APPEND AGGREGATE_OBJECT_LIB "obj.${name}")
236    endif()
237
238    # For each declared dependency, transform it into a generator expression
239    # which excludes it if the ultimate link target is excluding the library.
240    set(NEW_LINK_LIBRARIES)
241    get_target_property(CURRENT_LINK_LIBRARIES  ${name} LINK_LIBRARIES)
242    get_mlir_filtered_link_libraries(NEW_LINK_LIBRARIES ${CURRENT_LINK_LIBRARIES})
243    set_target_properties(${name} PROPERTIES LINK_LIBRARIES "${NEW_LINK_LIBRARIES}")
244    list(APPEND AGGREGATE_DEPS ${NEW_LINK_LIBRARIES})
245    set_target_properties(${name} PROPERTIES
246      EXPORT_PROPERTIES "MLIR_AGGREGATE_OBJECT_LIB_IMPORTED;MLIR_AGGREGATE_DEP_LIBS_IMPORTED"
247      MLIR_AGGREGATE_OBJECTS "${AGGREGATE_OBJECTS}"
248      MLIR_AGGREGATE_DEPS "${AGGREGATE_DEPS}"
249      MLIR_AGGREGATE_OBJECT_LIB_IMPORTED "${AGGREGATE_OBJECT_LIB}"
250      MLIR_AGGREGATE_DEP_LIBS_IMPORTED "${CURRENT_LINK_LIBRARIES}"
251    )
252
253    # In order for out-of-tree projects to build aggregates of this library,
254    # we need to install the OBJECT library.
255    if(MLIR_INSTALL_AGGREGATE_OBJECTS AND NOT ARG_DISABLE_INSTALL)
256      add_mlir_library_install(obj.${name})
257    endif()
258  endif()
259endfunction(add_mlir_library)
260
261# Sets a variable with a transformed list of link libraries such individual
262# libraries will be dynamically excluded when evaluated on a final library
263# which defines an MLIR_AGGREGATE_EXCLUDE_LIBS which contains any of the
264# libraries. Each link library can be a generator expression but must not
265# resolve to an arity > 1 (i.e. it can be optional).
266function(get_mlir_filtered_link_libraries output)
267  set(_results)
268  foreach(linklib ${ARGN})
269    # In English, what this expression does:
270    # For each link library, resolve the property MLIR_AGGREGATE_EXCLUDE_LIBS
271    # on the context target (i.e. the executable or shared library being linked)
272    # and, if it is not in that list, emit the library name. Otherwise, empty.
273    list(APPEND _results
274      "$<$<NOT:$<IN_LIST:${linklib},$<GENEX_EVAL:$<TARGET_PROPERTY:MLIR_AGGREGATE_EXCLUDE_LIBS>>>>:${linklib}>"
275    )
276  endforeach()
277  set(${output} "${_results}" PARENT_SCOPE)
278endfunction(get_mlir_filtered_link_libraries)
279
280# Declares an aggregate library. Such a library is a combination of arbitrary
281# regular add_mlir_library() libraries with the special feature that they can
282# be configured to statically embed some subset of their dependencies, as is
283# typical when creating a .so/.dylib/.dll or a mondo static library.
284#
285# It is always safe to depend on the aggregate directly in order to compile/link
286# against the superset of embedded entities and transitive deps.
287#
288# Arguments:
289#   PUBLIC_LIBS: list of dependent libraries to add to the
290#     INTERFACE_LINK_LIBRARIES property, exporting them to users. This list
291#     will be transitively filtered to exclude any EMBED_LIBS.
292#   EMBED_LIBS: list of dependent libraries that should be embedded directly
293#     into this library. Each of these must be an add_mlir_library() library
294#     without DISABLE_AGGREGATE.
295#
296# Note: This is a work in progress and is presently only sufficient for certain
297# non nested cases involving the C-API.
298function(add_mlir_aggregate name)
299  cmake_parse_arguments(ARG
300    "SHARED;STATIC"
301    ""
302    "PUBLIC_LIBS;EMBED_LIBS"
303    ${ARGN})
304  set(_libtype)
305  if(ARG_STATIC)
306    list(APPEND _libtype STATIC)
307  endif()
308  if(ARG_SHARED)
309    list(APPEND _libtype SHARED)
310  endif()
311  set(_debugmsg)
312
313  set(_embed_libs)
314  set(_objects)
315  set(_deps)
316  foreach(lib ${ARG_EMBED_LIBS})
317    # We have to handle imported vs in-tree differently:
318    #   in-tree: To support arbitrary ordering, the generator expressions get
319    #     set on the dependent target when it is constructed and then just
320    #     eval'd here. This means we can build an aggregate from targets that
321    #     may not yet be defined, which is typical for in-tree.
322    #   imported: Exported properties do not support generator expressions, so
323    #     we imperatively query and manage the expansion here. This is fine
324    #     because imported targets will always be found/configured first and
325    #     do not need to support arbitrary ordering. If CMake every supports
326    #     exporting generator expressions, then this can be simplified.
327    set(_is_imported OFF)
328    if(TARGET ${lib})
329      get_target_property(_is_imported ${lib} IMPORTED)
330    endif()
331
332    if(NOT _is_imported)
333      # Evaluate the in-tree generator expressions directly (this allows target
334      # order independence, since these aren't evaluated until the generate
335      # phase).
336      # What these expressions do:
337      # In the context of this aggregate, resolve the list of OBJECTS and DEPS
338      # that each library advertises and patch it into the whole.
339      set(_local_objects $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${lib},MLIR_AGGREGATE_OBJECTS>>)
340      set(_local_deps $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${lib},MLIR_AGGREGATE_DEPS>>)
341    else()
342      # It is an imported target, which can only have flat strings populated
343      # (no generator expressions).
344      # Rebuild the generator expressions from the imported flat string lists.
345      if(NOT MLIR_INSTALL_AGGREGATE_OBJECTS)
346        message(SEND_ERROR "Cannot build aggregate from imported targets which were not installed via MLIR_INSTALL_AGGREGATE_OBJECTS (for ${lib}).")
347      endif()
348
349      get_property(_has_object_lib_prop TARGET ${lib} PROPERTY MLIR_AGGREGATE_OBJECT_LIB_IMPORTED SET)
350      get_property(_has_dep_libs_prop TARGET ${lib} PROPERTY MLIR_AGGREGATE_DEP_LIBS_IMPORTED SET)
351      if(NOT _has_object_lib_prop OR NOT _has_dep_libs_prop)
352        message(SEND_ERROR "Cannot create an aggregate out of imported ${lib}: It is missing properties indicating that it was built for aggregation")
353      endif()
354      get_target_property(_imp_local_object_lib ${lib} MLIR_AGGREGATE_OBJECT_LIB_IMPORTED)
355      get_target_property(_imp_dep_libs ${lib} MLIR_AGGREGATE_DEP_LIBS_IMPORTED)
356      set(_local_objects)
357      if(_imp_local_object_lib)
358        set(_local_objects "$<TARGET_OBJECTS:${_imp_local_object_lib}>")
359      endif()
360      # We should just be able to do this:
361      #   get_mlir_filtered_link_libraries(_local_deps ${_imp_dep_libs})
362      # However, CMake complains about the unqualified use of the one-arg
363      # $<TARGET_PROPERTY> expression. So we do the same thing but use the
364      # two-arg form which takes an explicit target.
365      foreach(_imp_dep_lib ${_imp_dep_libs})
366        # In English, what this expression does:
367        # For each link library, resolve the property MLIR_AGGREGATE_EXCLUDE_LIBS
368        # on the context target (i.e. the executable or shared library being linked)
369        # and, if it is not in that list, emit the library name. Otherwise, empty.
370        list(APPEND _local_deps
371          "$<$<NOT:$<IN_LIST:${_imp_dep_lib},$<GENEX_EVAL:$<TARGET_PROPERTY:${name},MLIR_AGGREGATE_EXCLUDE_LIBS>>>>:${_imp_dep_lib}>"
372        )
373      endforeach()
374    endif()
375
376    list(APPEND _embed_libs ${lib})
377    list(APPEND _objects ${_local_objects})
378    list(APPEND _deps ${_local_deps})
379
380    string(APPEND _debugmsg
381      ": EMBED_LIB ${lib}:\n"
382      "    OBJECTS = ${_local_objects}\n"
383      "    DEPS = ${_local_deps}\n\n")
384  endforeach()
385
386  add_mlir_library(${name}
387    ${_libtype}
388    ${ARG_UNPARSED_ARGUMENTS}
389    PARTIAL_SOURCES_INTENDED
390    EXCLUDE_FROM_LIBMLIR
391    LINK_LIBS PRIVATE
392    ${_deps}
393    ${ARG_PUBLIC_LIBS}
394  )
395  target_sources(${name} PRIVATE ${_objects})
396  # TODO: Should be transitive.
397  set_target_properties(${name} PROPERTIES
398    MLIR_AGGREGATE_EXCLUDE_LIBS "${_embed_libs}")
399  if(MSVC)
400    set_property(TARGET ${name} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
401  endif()
402
403  # Debugging generator expressions can be hard. Uncomment the below to emit
404  # files next to the library with a lot of debug information:
405  # string(APPEND _debugmsg
406  #   ": MAIN LIBRARY:\n"
407  #   "    OBJECTS = ${_objects}\n"
408  #   "    SOURCES = $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SOURCES>>\n"
409  #   "    DEPS = ${_deps}\n"
410  #   "    LINK_LIBRARIES = $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},LINK_LIBRARIES>>\n"
411  #   "    MLIR_AGGREGATE_EXCLUDE_LIBS = $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},MLIR_AGGREGATE_EXCLUDE_LIBS>>\n"
412  # )
413  # file(GENERATE OUTPUT
414  #   "${CMAKE_CURRENT_BINARY_DIR}/${name}.aggregate_debug.txt"
415  #   CONTENT "${_debugmsg}"
416  # )
417endfunction(add_mlir_aggregate)
418
419# Adds an MLIR library target for installation.
420# This is usually done as part of add_mlir_library but is broken out for cases
421# where non-standard library builds can be installed.
422function(add_mlir_library_install name)
423  if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
424  get_target_export_arg(${name} MLIR export_to_mlirtargets UMBRELLA mlir-libraries)
425  install(TARGETS ${name}
426    COMPONENT ${name}
427    ${export_to_mlirtargets}
428    LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
429    ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
430    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
431    # Note that CMake will create a directory like:
432    #   objects-${CMAKE_BUILD_TYPE}/obj.LibName
433    # and put object files there.
434    OBJECTS DESTINATION lib${LLVM_LIBDIR_SUFFIX}
435  )
436
437  if (NOT LLVM_ENABLE_IDE)
438    add_llvm_install_targets(install-${name}
439                            DEPENDS ${name}
440                            COMPONENT ${name})
441  endif()
442  set_property(GLOBAL APPEND PROPERTY MLIR_ALL_LIBS ${name})
443  endif()
444  set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name})
445endfunction()
446
447# Declare an mlir library which is part of the public C-API.
448function(add_mlir_public_c_api_library name)
449  add_mlir_library(${name}
450    ${ARGN}
451    EXCLUDE_FROM_LIBMLIR
452    ENABLE_AGGREGATION
453    ADDITIONAL_HEADER_DIRS
454    ${MLIR_MAIN_INCLUDE_DIR}/mlir-c
455  )
456  # API libraries compile with hidden visibility and macros that enable
457  # exporting from the DLL. Only apply to the obj lib, which only affects
458  # the exports via a shared library.
459  set_target_properties(obj.${name}
460    PROPERTIES
461    CXX_VISIBILITY_PRESET hidden
462  )
463  target_compile_definitions(obj.${name}
464    PRIVATE
465    -DMLIR_CAPI_BUILDING_LIBRARY=1
466  )
467endfunction()
468
469# Declare the library associated with a dialect.
470function(add_mlir_dialect_library name)
471  set_property(GLOBAL APPEND PROPERTY MLIR_DIALECT_LIBS ${name})
472  add_mlir_library(${ARGV} DEPENDS mlir-headers)
473endfunction(add_mlir_dialect_library)
474
475# Declare the library associated with a conversion.
476function(add_mlir_conversion_library name)
477  set_property(GLOBAL APPEND PROPERTY MLIR_CONVERSION_LIBS ${name})
478  add_mlir_library(${ARGV} DEPENDS mlir-headers)
479endfunction(add_mlir_conversion_library)
480
481# Declare the library associated with a translation.
482function(add_mlir_translation_library name)
483  set_property(GLOBAL APPEND PROPERTY MLIR_TRANSLATION_LIBS ${name})
484  add_mlir_library(${ARGV} DEPENDS mlir-headers)
485endfunction(add_mlir_translation_library)
486
487# Verification tools to aid debugging.
488function(mlir_check_link_libraries name)
489  if(TARGET ${name})
490    get_target_property(type ${name} TYPE)
491    if (${type} STREQUAL "INTERFACE_LIBRARY")
492      get_target_property(libs ${name} INTERFACE_LINK_LIBRARIES)
493    else()
494      get_target_property(libs ${name} LINK_LIBRARIES)
495    endif()
496    # message("${name} libs are: ${libs}")
497    set(linking_llvm 0)
498    foreach(lib ${libs})
499      if(lib)
500        if(${lib} MATCHES "^LLVM$")
501          set(linking_llvm 1)
502        endif()
503        if((${lib} MATCHES "^LLVM.+") AND ${linking_llvm})
504          # This will almost always cause execution problems, since the
505          # same symbol might be loaded from 2 separate libraries.  This
506          # often comes from referring to an LLVM library target
507          # explicitly in target_link_libraries()
508          message("WARNING: ${name} links LLVM and ${lib}!")
509        endif()
510      endif()
511    endforeach()
512  endif()
513endfunction(mlir_check_link_libraries)
514
515function(mlir_check_all_link_libraries name)
516  mlir_check_link_libraries(${name})
517  if(TARGET ${name})
518    get_target_property(libs ${name} LINK_LIBRARIES)
519    # message("${name} libs are: ${libs}")
520    foreach(lib ${libs})
521      mlir_check_link_libraries(${lib})
522    endforeach()
523  endif()
524endfunction(mlir_check_all_link_libraries)
525