19f3f6d7bSStella Laurenzo################################################################################
2310c9496SStella Laurenzo# Python modules
3310c9496SStella Laurenzo# MLIR's Python modules are both directly used by the core project and are
4310c9496SStella Laurenzo# available for use and embedding into external projects (in their own
5310c9496SStella Laurenzo# namespace and with their own deps). In order to facilitate this, python
6310c9496SStella Laurenzo# artifacts are split between declarations, which make a subset of
7310c9496SStella Laurenzo# things available to be built and "add", which in line with the normal LLVM
8310c9496SStella Laurenzo# nomenclature, adds libraries.
9310c9496SStella Laurenzo################################################################################
10310c9496SStella Laurenzo
11310c9496SStella Laurenzo# Function: declare_mlir_python_sources
12310c9496SStella Laurenzo# Declares pure python sources as part of a named grouping that can be built
13310c9496SStella Laurenzo# later.
14310c9496SStella Laurenzo# Arguments:
15310c9496SStella Laurenzo#   ROOT_DIR: Directory where the python namespace begins (defaults to
16310c9496SStella Laurenzo#     CMAKE_CURRENT_SOURCE_DIR). For non-relocatable sources, this will
17310c9496SStella Laurenzo#     typically just be the root of the python source tree (current directory).
18310c9496SStella Laurenzo#     For relocatable sources, this will point deeper into the directory that
19310c9496SStella Laurenzo#     can be relocated. For generated sources, can be relative to
20310c9496SStella Laurenzo#     CMAKE_CURRENT_BINARY_DIR. Generated and non generated sources cannot be
21310c9496SStella Laurenzo#     mixed.
22310c9496SStella Laurenzo#   ADD_TO_PARENT: Adds this source grouping to a previously declared source
23310c9496SStella Laurenzo#     grouping. Source groupings form a DAG.
24310c9496SStella Laurenzo#   SOURCES: List of specific source files relative to ROOT_DIR to include.
25310c9496SStella Laurenzo#   SOURCES_GLOB: List of glob patterns relative to ROOT_DIR to include.
26310c9496SStella Laurenzofunction(declare_mlir_python_sources name)
27310c9496SStella Laurenzo  cmake_parse_arguments(ARG
28310c9496SStella Laurenzo    ""
29ac521d9eSStella Stamenova    "ROOT_DIR;ADD_TO_PARENT"
30310c9496SStella Laurenzo    "SOURCES;SOURCES_GLOB"
31310c9496SStella Laurenzo    ${ARGN})
32310c9496SStella Laurenzo
33310c9496SStella Laurenzo  if(NOT ARG_ROOT_DIR)
34310c9496SStella Laurenzo    set(ARG_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
35310c9496SStella Laurenzo  endif()
36132bc6e2SStella Laurenzo  set(_install_destination "src/python/${name}")
37310c9496SStella Laurenzo
38310c9496SStella Laurenzo  # Process the glob.
39310c9496SStella Laurenzo  set(_glob_sources)
40310c9496SStella Laurenzo  if(ARG_SOURCES_GLOB)
41310c9496SStella Laurenzo    set(_glob_spec ${ARG_SOURCES_GLOB})
42310c9496SStella Laurenzo    list(TRANSFORM _glob_spec PREPEND "${ARG_ROOT_DIR}/")
43310c9496SStella Laurenzo    file(GLOB_RECURSE _glob_sources
44310c9496SStella Laurenzo      RELATIVE "${ARG_ROOT_DIR}"
45310c9496SStella Laurenzo      ${_glob_spec}
46310c9496SStella Laurenzo    )
47310c9496SStella Laurenzo    list(APPEND ARG_SOURCES ${_glob_sources})
48310c9496SStella Laurenzo  endif()
49310c9496SStella Laurenzo
50310c9496SStella Laurenzo  # We create a custom target to carry properties and dependencies for
51310c9496SStella Laurenzo  # generated sources.
52132bc6e2SStella Laurenzo  add_library(${name} INTERFACE)
53310c9496SStella Laurenzo  set_target_properties(${name} PROPERTIES
54132bc6e2SStella Laurenzo    # Yes: Leading-lowercase property names are load bearing and the recommended
55132bc6e2SStella Laurenzo    # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
56ac521d9eSStella Stamenova    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_DEPENDS"
57132bc6e2SStella Laurenzo    mlir_python_SOURCES_TYPE pure
58132bc6e2SStella Laurenzo    mlir_python_DEPENDS ""
59132bc6e2SStella Laurenzo  )
60ac521d9eSStella Stamenova
61ac521d9eSStella Stamenova  # Use the interface include directories and sources on the target to carry the
62ac521d9eSStella Stamenova  # properties we would like to export. These support generator expressions and
63ac521d9eSStella Stamenova  # allow us to properly specify paths in both the local build and install scenarios.
64ac521d9eSStella Stamenova  # The one caveat here is that because we don't directly build against the interface
65ac521d9eSStella Stamenova  # library, we need to specify the INCLUDE_DIRECTORIES and SOURCES properties as well
66ac521d9eSStella Stamenova  # via private properties because the evaluation would happen at configuration time
67ac521d9eSStella Stamenova  # instead of build time.
68ac521d9eSStella Stamenova  # Eventually this could be done using a FILE_SET simplifying the logic below.
69ac521d9eSStella Stamenova  # FILE_SET is available in cmake 3.23+, so it is not an option at the moment.
70132bc6e2SStella Laurenzo  target_include_directories(${name} INTERFACE
71132bc6e2SStella Laurenzo    "$<BUILD_INTERFACE:${ARG_ROOT_DIR}>"
72132bc6e2SStella Laurenzo    "$<INSTALL_INTERFACE:${_install_destination}>"
73310c9496SStella Laurenzo  )
74ac521d9eSStella Stamenova  set_property(TARGET ${name} PROPERTY INCLUDE_DIRECTORIES ${ARG_ROOT_DIR})
75ac521d9eSStella Stamenova
76ac521d9eSStella Stamenova  if(ARG_SOURCES)
77ac521d9eSStella Stamenova    list(TRANSFORM ARG_SOURCES PREPEND "${ARG_ROOT_DIR}/" OUTPUT_VARIABLE _build_sources)
78ac521d9eSStella Stamenova    list(TRANSFORM ARG_SOURCES PREPEND "${_install_destination}/" OUTPUT_VARIABLE _install_sources)
79ac521d9eSStella Stamenova    target_sources(${name}
80ac521d9eSStella Stamenova      INTERFACE
81ac521d9eSStella Stamenova        "$<INSTALL_INTERFACE:${_install_sources}>"
82ac521d9eSStella Stamenova        "$<BUILD_INTERFACE:${_build_sources}>"
83ac521d9eSStella Stamenova      PRIVATE ${_build_sources}
84ac521d9eSStella Stamenova    )
85ac521d9eSStella Stamenova  endif()
86310c9496SStella Laurenzo
87310c9496SStella Laurenzo  # Add to parent.
88310c9496SStella Laurenzo  if(ARG_ADD_TO_PARENT)
89132bc6e2SStella Laurenzo    set_property(TARGET ${ARG_ADD_TO_PARENT} APPEND PROPERTY mlir_python_DEPENDS ${name})
90132bc6e2SStella Laurenzo  endif()
91132bc6e2SStella Laurenzo
92132bc6e2SStella Laurenzo  # Install.
93132bc6e2SStella Laurenzo  set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name})
94132bc6e2SStella Laurenzo  if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
95132bc6e2SStella Laurenzo    _mlir_python_install_sources(
96132bc6e2SStella Laurenzo      ${name} "${ARG_ROOT_DIR}" "${_install_destination}"
97132bc6e2SStella Laurenzo      ${ARG_SOURCES}
98132bc6e2SStella Laurenzo    )
99310c9496SStella Laurenzo  endif()
100310c9496SStella Laurenzoendfunction()
101310c9496SStella Laurenzo
102310c9496SStella Laurenzo# Function: declare_mlir_python_extension
103310c9496SStella Laurenzo# Declares a buildable python extension from C++ source files. The built
104310c9496SStella Laurenzo# module is considered a python source file and included as everything else.
105310c9496SStella Laurenzo# Arguments:
106132bc6e2SStella Laurenzo#   ROOT_DIR: Root directory where sources are interpreted relative to.
107132bc6e2SStella Laurenzo#     Defaults to CMAKE_CURRENT_SOURCE_DIR.
108310c9496SStella Laurenzo#   MODULE_NAME: Local import name of the module (i.e. "_mlir").
109310c9496SStella Laurenzo#   ADD_TO_PARENT: Same as for declare_mlir_python_sources.
110310c9496SStella Laurenzo#   SOURCES: C++ sources making up the module.
111310c9496SStella Laurenzo#   PRIVATE_LINK_LIBS: List of libraries to link in privately to the module
112310c9496SStella Laurenzo#     regardless of how it is included in the project (generally should be
113310c9496SStella Laurenzo#     static libraries that can be included with hidden visibility).
114310c9496SStella Laurenzo#   EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
115310c9496SStella Laurenzo#     on. These will be collected for all extensions and put into an
116310c9496SStella Laurenzo#     aggregate dylib that is linked against.
117310c9496SStella Laurenzofunction(declare_mlir_python_extension name)
118310c9496SStella Laurenzo  cmake_parse_arguments(ARG
119310c9496SStella Laurenzo    ""
120132bc6e2SStella Laurenzo    "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
121310c9496SStella Laurenzo    "SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
122310c9496SStella Laurenzo    ${ARGN})
123310c9496SStella Laurenzo
124132bc6e2SStella Laurenzo  if(NOT ARG_ROOT_DIR)
125132bc6e2SStella Laurenzo    set(ARG_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
126132bc6e2SStella Laurenzo  endif()
127132bc6e2SStella Laurenzo  set(_install_destination "src/python/${name}")
128132bc6e2SStella Laurenzo
129132bc6e2SStella Laurenzo  add_library(${name} INTERFACE)
130310c9496SStella Laurenzo  set_target_properties(${name} PROPERTIES
131132bc6e2SStella Laurenzo    # Yes: Leading-lowercase property names are load bearing and the recommended
132132bc6e2SStella Laurenzo    # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
133ac521d9eSStella Stamenova    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
134132bc6e2SStella Laurenzo    mlir_python_SOURCES_TYPE extension
135132bc6e2SStella Laurenzo    mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
136132bc6e2SStella Laurenzo    mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
137132bc6e2SStella Laurenzo    mlir_python_DEPENDS ""
138132bc6e2SStella Laurenzo  )
139ac521d9eSStella Stamenova
140ac521d9eSStella Stamenova  # Set the interface source and link_libs properties of the target
141ac521d9eSStella Stamenova  # These properties support generator expressions and are automatically exported
142ac521d9eSStella Stamenova  list(TRANSFORM ARG_SOURCES PREPEND "${ARG_ROOT_DIR}/" OUTPUT_VARIABLE _build_sources)
143ac521d9eSStella Stamenova  list(TRANSFORM ARG_SOURCES PREPEND "${_install_destination}/" OUTPUT_VARIABLE _install_sources)
144ac521d9eSStella Stamenova  target_sources(${name} INTERFACE
145ac521d9eSStella Stamenova    "$<BUILD_INTERFACE:${_build_sources}>"
146ac521d9eSStella Stamenova    "$<INSTALL_INTERFACE:${_install_sources}>"
147ac521d9eSStella Stamenova  )
148ac521d9eSStella Stamenova  target_link_libraries(${name} INTERFACE
149ac521d9eSStella Stamenova    ${ARG_PRIVATE_LINK_LIBS}
150310c9496SStella Laurenzo  )
151310c9496SStella Laurenzo
152310c9496SStella Laurenzo  # Add to parent.
153310c9496SStella Laurenzo  if(ARG_ADD_TO_PARENT)
154132bc6e2SStella Laurenzo    set_property(TARGET ${ARG_ADD_TO_PARENT} APPEND PROPERTY mlir_python_DEPENDS ${name})
155310c9496SStella Laurenzo  endif()
156132bc6e2SStella Laurenzo
157132bc6e2SStella Laurenzo  # Install.
158132bc6e2SStella Laurenzo  set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name})
159132bc6e2SStella Laurenzo  if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
160132bc6e2SStella Laurenzo    _mlir_python_install_sources(
161ac521d9eSStella Stamenova      ${name} "${ARG_ROOT_DIR}" "${_install_destination}"
162132bc6e2SStella Laurenzo      ${ARG_SOURCES}
163132bc6e2SStella Laurenzo    )
164132bc6e2SStella Laurenzo  endif()
165132bc6e2SStella Laurenzoendfunction()
166132bc6e2SStella Laurenzo
167132bc6e2SStella Laurenzofunction(_mlir_python_install_sources name source_root_dir destination)
168132bc6e2SStella Laurenzo  foreach(source_relative_path ${ARGN})
169132bc6e2SStella Laurenzo    # Transform "a/b/c.py" -> "${install_prefix}/a/b" for installation.
170132bc6e2SStella Laurenzo    get_filename_component(
171ac521d9eSStella Stamenova      dest_relative_dir "${source_relative_path}" DIRECTORY
172132bc6e2SStella Laurenzo      BASE_DIR "${source_root_dir}"
173132bc6e2SStella Laurenzo    )
174132bc6e2SStella Laurenzo    install(
175132bc6e2SStella Laurenzo      FILES "${source_root_dir}/${source_relative_path}"
176ac521d9eSStella Stamenova      DESTINATION "${destination}/${dest_relative_dir}"
177132bc6e2SStella Laurenzo      COMPONENT "${name}"
178132bc6e2SStella Laurenzo    )
179132bc6e2SStella Laurenzo  endforeach()
180132bc6e2SStella Laurenzo  get_target_export_arg(${name} MLIR export_to_mlirtargets UMBRELLA mlir-libraries)
181132bc6e2SStella Laurenzo  install(TARGETS ${name}
182132bc6e2SStella Laurenzo    COMPONENT ${name}
183132bc6e2SStella Laurenzo    ${export_to_mlirtargets}
184132bc6e2SStella Laurenzo  )
185310c9496SStella Laurenzoendfunction()
186310c9496SStella Laurenzo
187310c9496SStella Laurenzo# Function: add_mlir_python_modules
188310c9496SStella Laurenzo# Adds python modules to a project, building them from a list of declared
189310c9496SStella Laurenzo# source groupings (see declare_mlir_python_sources and
190310c9496SStella Laurenzo# declare_mlir_python_extension). One of these must be called for each
191310c9496SStella Laurenzo# packaging root in use.
192310c9496SStella Laurenzo# Arguments:
193310c9496SStella Laurenzo#   ROOT_PREFIX: The directory in the build tree to emit sources. This will
194310c9496SStella Laurenzo#     typically be something like ${MY_BINARY_DIR}/python_packages/foobar
195310c9496SStella Laurenzo#     for non-relocatable modules or a deeper directory tree for relocatable.
196310c9496SStella Laurenzo#   INSTALL_PREFIX: Prefix into the install tree for installing the package.
197310c9496SStella Laurenzo#     Typically mirrors the path above but without an absolute path.
198310c9496SStella Laurenzo#   DECLARED_SOURCES: List of declared source groups to include. The entire
199310c9496SStella Laurenzo#     DAG of source modules is included.
200310c9496SStella Laurenzo#   COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
201310c9496SStella Laurenzo#     extension depend on (see mlir_python_add_common_capi_library).
202310c9496SStella Laurenzofunction(add_mlir_python_modules name)
203310c9496SStella Laurenzo  cmake_parse_arguments(ARG
204310c9496SStella Laurenzo    ""
205310c9496SStella Laurenzo    "ROOT_PREFIX;INSTALL_PREFIX;COMMON_CAPI_LINK_LIBS"
206310c9496SStella Laurenzo    "DECLARED_SOURCES"
207310c9496SStella Laurenzo    ${ARGN})
208310c9496SStella Laurenzo  # Helper to process an individual target.
209310c9496SStella Laurenzo  function(_process_target modules_target sources_target)
210132bc6e2SStella Laurenzo    get_target_property(_source_type ${sources_target} mlir_python_SOURCES_TYPE)
211132bc6e2SStella Laurenzo
212310c9496SStella Laurenzo    if(_source_type STREQUAL "pure")
213310c9496SStella Laurenzo      # Pure python sources to link into the tree.
214aa78c529SStella Laurenzo      set(_pure_sources_target "${modules_target}.sources.${sources_target}")
215ac521d9eSStella Stamenova      add_mlir_python_sources_target(${_pure_sources_target}
216ac521d9eSStella Stamenova        INSTALL_COMPONENT ${modules_target}
217ac521d9eSStella Stamenova        INSTALL_DIR ${ARG_INSTALL_PREFIX}
218ac521d9eSStella Stamenova        OUTPUT_DIRECTORY ${ARG_ROOT_PREFIX}
219ac521d9eSStella Stamenova        SOURCES_TARGETS ${sources_target}
220310c9496SStella Laurenzo      )
221ac521d9eSStella Stamenova      add_dependencies(${modules_target} ${_pure_sources_target})
222310c9496SStella Laurenzo    elseif(_source_type STREQUAL "extension")
223310c9496SStella Laurenzo      # Native CPP extension.
224132bc6e2SStella Laurenzo      get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
225132bc6e2SStella Laurenzo      # Transform relative source to based on root dir.
226ac521d9eSStella Stamenova      set(_extension_target "${modules_target}.extension.${_module_name}.dso")
227310c9496SStella Laurenzo      add_mlir_python_extension(${_extension_target} "${_module_name}"
228310c9496SStella Laurenzo        INSTALL_COMPONENT ${modules_target}
229310c9496SStella Laurenzo        INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
230310c9496SStella Laurenzo        OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
231310c9496SStella Laurenzo        LINK_LIBS PRIVATE
232ac521d9eSStella Stamenova          ${sources_target}
233310c9496SStella Laurenzo          ${ARG_COMMON_CAPI_LINK_LIBS}
234310c9496SStella Laurenzo      )
235ac521d9eSStella Stamenova      add_dependencies(${modules_target} ${_extension_target})
236310c9496SStella Laurenzo      mlir_python_setup_extension_rpath(${_extension_target})
237310c9496SStella Laurenzo    else()
238310c9496SStella Laurenzo      message(SEND_ERROR "Unrecognized source type '${_source_type}' for python source target ${sources_target}")
239310c9496SStella Laurenzo      return()
240310c9496SStella Laurenzo    endif()
241310c9496SStella Laurenzo  endfunction()
242310c9496SStella Laurenzo
243310c9496SStella Laurenzo  # Build the modules target.
244ac521d9eSStella Stamenova  add_custom_target(${name} ALL)
245ac521d9eSStella Stamenova  _flatten_mlir_python_targets(_flat_targets ${ARG_DECLARED_SOURCES})
246310c9496SStella Laurenzo  foreach(sources_target ${_flat_targets})
247310c9496SStella Laurenzo    _process_target(${name} ${sources_target})
248310c9496SStella Laurenzo  endforeach()
249310c9496SStella Laurenzo
250310c9496SStella Laurenzo  # Create an install target.
251310c9496SStella Laurenzo  if(NOT LLVM_ENABLE_IDE)
252310c9496SStella Laurenzo    add_llvm_install_targets(
253310c9496SStella Laurenzo      install-${name}
254310c9496SStella Laurenzo      DEPENDS ${name}
255310c9496SStella Laurenzo      COMPONENT ${name})
256310c9496SStella Laurenzo  endif()
257310c9496SStella Laurenzoendfunction()
258310c9496SStella Laurenzo
259310c9496SStella Laurenzo# Function: declare_mlir_dialect_python_bindings
260310c9496SStella Laurenzo# Helper to generate source groups for dialects, including both static source
261310c9496SStella Laurenzo# files and a TD_FILE to generate wrappers.
262310c9496SStella Laurenzo#
263310c9496SStella Laurenzo# This will generate a source group named ${ADD_TO_PARENT}.${DIALECT_NAME}.
264310c9496SStella Laurenzo#
265310c9496SStella Laurenzo# Arguments:
266310c9496SStella Laurenzo#   ROOT_DIR: Same as for declare_mlir_python_sources().
267310c9496SStella Laurenzo#   ADD_TO_PARENT: Same as for declare_mlir_python_sources(). Unique names
268310c9496SStella Laurenzo#     for the subordinate source groups are derived from this.
269310c9496SStella Laurenzo#   TD_FILE: Tablegen file to generate source for (relative to ROOT_DIR).
270310c9496SStella Laurenzo#   DIALECT_NAME: Python name of the dialect.
271310c9496SStella Laurenzo#   SOURCES: Same as declare_mlir_python_sources().
272310c9496SStella Laurenzo#   SOURCES_GLOB: Same as declare_mlir_python_sources().
273310c9496SStella Laurenzo#   DEPENDS: Additional dependency targets.
274310c9496SStella Laurenzofunction(declare_mlir_dialect_python_bindings)
275310c9496SStella Laurenzo  cmake_parse_arguments(ARG
276310c9496SStella Laurenzo    ""
277310c9496SStella Laurenzo    "ROOT_DIR;ADD_TO_PARENT;TD_FILE;DIALECT_NAME"
278310c9496SStella Laurenzo    "SOURCES;SOURCES_GLOB;DEPENDS"
279310c9496SStella Laurenzo    ${ARGN})
280310c9496SStella Laurenzo  # Sources.
281310c9496SStella Laurenzo  set(_dialect_target "${ARG_ADD_TO_PARENT}.${ARG_DIALECT_NAME}")
282310c9496SStella Laurenzo  declare_mlir_python_sources(${_dialect_target}
283310c9496SStella Laurenzo    ROOT_DIR "${ARG_ROOT_DIR}"
284310c9496SStella Laurenzo    ADD_TO_PARENT "${ARG_ADD_TO_PARENT}"
285310c9496SStella Laurenzo    SOURCES "${ARG_SOURCES}"
286310c9496SStella Laurenzo    SOURCES_GLOB "${ARG_SOURCES_GLOB}"
287310c9496SStella Laurenzo  )
288310c9496SStella Laurenzo
289310c9496SStella Laurenzo  # Tablegen
290310c9496SStella Laurenzo  if(ARG_TD_FILE)
291ac521d9eSStella Stamenova    set(tblgen_target "${_dialect_target}.tablegen")
292310c9496SStella Laurenzo    set(td_file "${ARG_ROOT_DIR}/${ARG_TD_FILE}")
293310c9496SStella Laurenzo    get_filename_component(relative_td_directory "${ARG_TD_FILE}" DIRECTORY)
294132bc6e2SStella Laurenzo    file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${relative_td_directory}")
295310c9496SStella Laurenzo    set(dialect_filename "${relative_td_directory}/_${ARG_DIALECT_NAME}_ops_gen.py")
296310c9496SStella Laurenzo    set(LLVM_TARGET_DEFINITIONS ${td_file})
297ac521d9eSStella Stamenova    mlir_tablegen("${dialect_filename}"
298ac521d9eSStella Stamenova      -gen-python-op-bindings -bind-dialect=${ARG_DIALECT_NAME}
299ac521d9eSStella Stamenova      DEPENDS ${ARG_DEPENDS}
300ac521d9eSStella Stamenova    )
301310c9496SStella Laurenzo    add_public_tablegen_target(${tblgen_target})
302310c9496SStella Laurenzo
303310c9496SStella Laurenzo    # Generated.
304ac521d9eSStella Stamenova    declare_mlir_python_sources("${_dialect_target}.ops_gen"
305310c9496SStella Laurenzo      ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
306310c9496SStella Laurenzo      ADD_TO_PARENT "${_dialect_target}"
307310c9496SStella Laurenzo      SOURCES "${dialect_filename}"
308310c9496SStella Laurenzo    )
309310c9496SStella Laurenzo  endif()
310310c9496SStella Laurenzoendfunction()
311310c9496SStella Laurenzo
3123f71765aSAlex Zinenko# Function: declare_mlir_dialect_extension_python_bindings
3133f71765aSAlex Zinenko# Helper to generate source groups for dialect extensions, including both
3143f71765aSAlex Zinenko# static source files and a TD_FILE to generate wrappers.
3153f71765aSAlex Zinenko#
3163f71765aSAlex Zinenko# This will generate a source group named ${ADD_TO_PARENT}.${EXTENSION_NAME}.
3173f71765aSAlex Zinenko#
3183f71765aSAlex Zinenko# Arguments:
3193f71765aSAlex Zinenko#   ROOT_DIR: Same as for declare_mlir_python_sources().
3203f71765aSAlex Zinenko#   ADD_TO_PARENT: Same as for declare_mlir_python_sources(). Unique names
3213f71765aSAlex Zinenko#     for the subordinate source groups are derived from this.
3223f71765aSAlex Zinenko#   TD_FILE: Tablegen file to generate source for (relative to ROOT_DIR).
3233f71765aSAlex Zinenko#   DIALECT_NAME: Python name of the dialect.
3243f71765aSAlex Zinenko#   EXTENSION_NAME: Python name of the dialect extension.
3253f71765aSAlex Zinenko#   SOURCES: Same as declare_mlir_python_sources().
3263f71765aSAlex Zinenko#   SOURCES_GLOB: Same as declare_mlir_python_sources().
3273f71765aSAlex Zinenko#   DEPENDS: Additional dependency targets.
3283f71765aSAlex Zinenkofunction(declare_mlir_dialect_extension_python_bindings)
3293f71765aSAlex Zinenko  cmake_parse_arguments(ARG
3303f71765aSAlex Zinenko    ""
3313f71765aSAlex Zinenko    "ROOT_DIR;ADD_TO_PARENT;TD_FILE;DIALECT_NAME;EXTENSION_NAME"
3323f71765aSAlex Zinenko    "SOURCES;SOURCES_GLOB;DEPENDS"
3333f71765aSAlex Zinenko    ${ARGN})
3343f71765aSAlex Zinenko  # Source files.
3353f71765aSAlex Zinenko  set(_extension_target "${ARG_ADD_TO_PARENT}.${ARG_EXTENSION_NAME}")
3363f71765aSAlex Zinenko  declare_mlir_python_sources(${_extension_target}
3373f71765aSAlex Zinenko    ROOT_DIR "${ARG_ROOT_DIR}"
3383f71765aSAlex Zinenko    ADD_TO_PARENT "${ARG_ADD_TO_PARENT}"
3393f71765aSAlex Zinenko    SOURCES "${ARG_SOURCES}"
3403f71765aSAlex Zinenko    SOURCES_GLOB "${ARG_SOURCES_GLOB}"
3413f71765aSAlex Zinenko  )
3423f71765aSAlex Zinenko
3433f71765aSAlex Zinenko  # Tablegen
3443f71765aSAlex Zinenko  if(ARG_TD_FILE)
3453f71765aSAlex Zinenko    set(tblgen_target "${ARG_ADD_TO_PARENT}.${ARG_EXTENSION_NAME}.tablegen")
3463f71765aSAlex Zinenko    set(td_file "${ARG_ROOT_DIR}/${ARG_TD_FILE}")
3473f71765aSAlex Zinenko    get_filename_component(relative_td_directory "${ARG_TD_FILE}" DIRECTORY)
3483f71765aSAlex Zinenko    file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${relative_td_directory}")
3493f71765aSAlex Zinenko    set(output_filename "${relative_td_directory}/_${ARG_EXTENSION_NAME}_ops_gen.py")
3503f71765aSAlex Zinenko    set(LLVM_TARGET_DEFINITIONS ${td_file})
3513f71765aSAlex Zinenko    mlir_tablegen("${output_filename}" -gen-python-op-bindings
3523f71765aSAlex Zinenko                  -bind-dialect=${ARG_DIALECT_NAME}
3533f71765aSAlex Zinenko                  -dialect-extension=${ARG_EXTENSION_NAME})
3543f71765aSAlex Zinenko    add_public_tablegen_target(${tblgen_target})
3553f71765aSAlex Zinenko    if(ARG_DEPENDS)
3563f71765aSAlex Zinenko      add_dependencies(${tblgen_target} ${ARG_DEPENDS})
3573f71765aSAlex Zinenko    endif()
3583f71765aSAlex Zinenko
3593f71765aSAlex Zinenko    declare_mlir_python_sources("${_extension_target}.ops_gen"
3603f71765aSAlex Zinenko      ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
3613f71765aSAlex Zinenko      ADD_TO_PARENT "${_extension_target}"
3623f71765aSAlex Zinenko      SOURCES "${output_filename}"
3633f71765aSAlex Zinenko    )
3643f71765aSAlex Zinenko  endif()
3653f71765aSAlex Zinenkoendfunction()
3663f71765aSAlex Zinenko
367310c9496SStella Laurenzo# Function: mlir_python_setup_extension_rpath
368310c9496SStella Laurenzo# Sets RPATH properties on a target, assuming that it is being output to
369310c9496SStella Laurenzo# an _mlir_libs directory with all other libraries. For static linkage,
370310c9496SStella Laurenzo# the RPATH will just be the origin. If linking dynamically, then the LLVM
371310c9496SStella Laurenzo# library directory will be added.
372310c9496SStella Laurenzo# Arguments:
373310c9496SStella Laurenzo#   RELATIVE_INSTALL_ROOT: If building dynamically, an RPATH entry will be
374310c9496SStella Laurenzo#     added to the install tree lib/ directory by first traversing this
375310c9496SStella Laurenzo#     path relative to the installation location. Typically a number of ".."
376310c9496SStella Laurenzo#     entries, one for each level of the install path.
377310c9496SStella Laurenzofunction(mlir_python_setup_extension_rpath target)
378310c9496SStella Laurenzo  cmake_parse_arguments(ARG
379310c9496SStella Laurenzo    ""
380310c9496SStella Laurenzo    "RELATIVE_INSTALL_ROOT"
381310c9496SStella Laurenzo    ""
382310c9496SStella Laurenzo    ${ARGN})
383310c9496SStella Laurenzo
384310c9496SStella Laurenzo  # RPATH handling.
385310c9496SStella Laurenzo  # For the build tree, include the LLVM lib directory and the current
386310c9496SStella Laurenzo  # directory for RPATH searching. For install, just the current directory
387310c9496SStella Laurenzo  # (assumes that needed dependencies have been installed).
388310c9496SStella Laurenzo  if(NOT APPLE AND NOT UNIX)
389310c9496SStella Laurenzo    return()
390310c9496SStella Laurenzo  endif()
391310c9496SStella Laurenzo
392310c9496SStella Laurenzo  set(_origin_prefix "\$ORIGIN")
393310c9496SStella Laurenzo  if(APPLE)
394310c9496SStella Laurenzo    set(_origin_prefix "@loader_path")
395310c9496SStella Laurenzo  endif()
396310c9496SStella Laurenzo  set_target_properties(${target} PROPERTIES
397310c9496SStella Laurenzo    BUILD_WITH_INSTALL_RPATH OFF
398310c9496SStella Laurenzo    BUILD_RPATH "${_origin_prefix}"
399310c9496SStella Laurenzo    INSTALL_RPATH "${_origin_prefix}"
400310c9496SStella Laurenzo  )
401310c9496SStella Laurenzo
402310c9496SStella Laurenzo  # For static builds, that is all that is needed: all dependencies will be in
403310c9496SStella Laurenzo  # the one directory. For shared builds, then we also need to add the global
404310c9496SStella Laurenzo  # lib directory. This will be absolute for the build tree and relative for
405310c9496SStella Laurenzo  # install.
406310c9496SStella Laurenzo  # When we have access to CMake >= 3.20, there is a helper to calculate this.
407310c9496SStella Laurenzo  if(BUILD_SHARED_LIBS AND ARG_RELATIVE_INSTALL_ROOT)
408310c9496SStella Laurenzo    get_filename_component(_real_lib_dir "${LLVM_LIBRARY_OUTPUT_INTDIR}" REALPATH)
409310c9496SStella Laurenzo    set_property(TARGET ${target} APPEND PROPERTY
410310c9496SStella Laurenzo      BUILD_RPATH "${_real_lib_dir}")
411310c9496SStella Laurenzo    set_property(TARGET ${target} APPEND PROPERTY
412310c9496SStella Laurenzo      INSTALL_RPATH "${_origin_prefix}/${ARG_RELATIVE_INSTALL_ROOT}/lib${LLVM_LIBDIR_SUFFIX}")
413310c9496SStella Laurenzo  endif()
414310c9496SStella Laurenzoendfunction()
415310c9496SStella Laurenzo
416310c9496SStella Laurenzo# Function: add_mlir_python_common_capi_library
417310c9496SStella Laurenzo# Adds a shared library which embeds dependent CAPI libraries needed to link
418310c9496SStella Laurenzo# all extensions.
419310c9496SStella Laurenzo# Arguments:
420310c9496SStella Laurenzo#   INSTALL_COMPONENT: Name of the install component. Typically same as the
421310c9496SStella Laurenzo#     target name passed to add_mlir_python_modules().
422310c9496SStella Laurenzo#   INSTALL_DESTINATION: Prefix into the install tree in which to install the
423310c9496SStella Laurenzo#     library.
424310c9496SStella Laurenzo#   OUTPUT_DIRECTORY: Full path in the build tree in which to create the
425310c9496SStella Laurenzo#     library. Typically, this will be the common _mlir_libs directory where
426310c9496SStella Laurenzo#     all extensions are emitted.
427310c9496SStella Laurenzo#   RELATIVE_INSTALL_ROOT: See mlir_python_setup_extension_rpath().
428ac521d9eSStella Stamenova#   DECLARED_HEADERS: Source groups from which to discover headers that belong
429ac521d9eSStella Stamenova#     to the library and should be installed with it.
430310c9496SStella Laurenzo#   DECLARED_SOURCES: Source groups from which to discover dependent
431310c9496SStella Laurenzo#     EMBED_CAPI_LINK_LIBS.
432310c9496SStella Laurenzo#   EMBED_LIBS: Additional libraries to embed (must be built with OBJECTS and
433310c9496SStella Laurenzo#     have an "obj.${name}" object library associated).
434310c9496SStella Laurenzofunction(add_mlir_python_common_capi_library name)
435310c9496SStella Laurenzo  cmake_parse_arguments(ARG
436310c9496SStella Laurenzo    ""
437310c9496SStella Laurenzo    "INSTALL_COMPONENT;INSTALL_DESTINATION;OUTPUT_DIRECTORY;RELATIVE_INSTALL_ROOT"
438ac521d9eSStella Stamenova    "DECLARED_HEADERS;DECLARED_SOURCES;EMBED_LIBS"
439310c9496SStella Laurenzo    ${ARGN})
440310c9496SStella Laurenzo  # Collect all explicit and transitive embed libs.
441310c9496SStella Laurenzo  set(_embed_libs ${ARG_EMBED_LIBS})
442310c9496SStella Laurenzo  _flatten_mlir_python_targets(_all_source_targets ${ARG_DECLARED_SOURCES})
443310c9496SStella Laurenzo  foreach(t ${_all_source_targets})
444132bc6e2SStella Laurenzo    get_target_property(_local_embed_libs ${t} mlir_python_EMBED_CAPI_LINK_LIBS)
445310c9496SStella Laurenzo    if(_local_embed_libs)
446310c9496SStella Laurenzo      list(APPEND _embed_libs ${_local_embed_libs})
447310c9496SStella Laurenzo    endif()
448310c9496SStella Laurenzo  endforeach()
449310c9496SStella Laurenzo  list(REMOVE_DUPLICATES _embed_libs)
450310c9496SStella Laurenzo
451fe6d9937SStella Laurenzo  # Generate the aggregate .so that everything depends on.
452fe6d9937SStella Laurenzo  add_mlir_aggregate(${name}
453310c9496SStella Laurenzo    SHARED
454310c9496SStella Laurenzo    DISABLE_INSTALL
455fe6d9937SStella Laurenzo    EMBED_LIBS ${_embed_libs}
456310c9496SStella Laurenzo  )
457fe6d9937SStella Laurenzo
458ac521d9eSStella Stamenova  # Process any headers associated with the library
459ac521d9eSStella Stamenova  _flatten_mlir_python_targets(_flat_header_targets ${ARG_DECLARED_HEADERS})
460ac521d9eSStella Stamenova  set(_header_sources_target "${name}.sources")
461ac521d9eSStella Stamenova  add_mlir_python_sources_target(${_header_sources_target}
462ac521d9eSStella Stamenova    INSTALL_COMPONENT ${ARG_INSTALL_COMPONENT}
463ac521d9eSStella Stamenova    INSTALL_DIR "${ARG_INSTALL_DESTINATION}/include"
464ac521d9eSStella Stamenova    OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}/include"
465ac521d9eSStella Stamenova    SOURCES_TARGETS ${_flat_header_targets}
466ac521d9eSStella Stamenova  )
467ac521d9eSStella Stamenova  add_dependencies(${name} ${_header_sources_target})
468ac521d9eSStella Stamenova
469310c9496SStella Laurenzo  if(MSVC)
470310c9496SStella Laurenzo    set_property(TARGET ${name} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
471310c9496SStella Laurenzo  endif()
472310c9496SStella Laurenzo  set_target_properties(${name} PROPERTIES
473310c9496SStella Laurenzo    LIBRARY_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
474310c9496SStella Laurenzo    BINARY_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
475cb7b0381SStella Laurenzo    # Needed for windows (and don't hurt others).
476cb7b0381SStella Laurenzo    RUNTIME_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
477cb7b0381SStella Laurenzo    ARCHIVE_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
478310c9496SStella Laurenzo  )
479310c9496SStella Laurenzo  mlir_python_setup_extension_rpath(${name}
480310c9496SStella Laurenzo    RELATIVE_INSTALL_ROOT "${ARG_RELATIVE_INSTALL_ROOT}"
481310c9496SStella Laurenzo  )
482310c9496SStella Laurenzo  install(TARGETS ${name}
483310c9496SStella Laurenzo    COMPONENT ${ARG_INSTALL_COMPONENT}
484310c9496SStella Laurenzo    LIBRARY DESTINATION "${ARG_INSTALL_DESTINATION}"
485310c9496SStella Laurenzo    RUNTIME DESTINATION "${ARG_INSTALL_DESTINATION}"
486310c9496SStella Laurenzo  )
487310c9496SStella Laurenzoendfunction()
488310c9496SStella Laurenzo
489310c9496SStella Laurenzofunction(_flatten_mlir_python_targets output_var)
490310c9496SStella Laurenzo  set(_flattened)
491310c9496SStella Laurenzo  foreach(t ${ARGN})
492132bc6e2SStella Laurenzo    get_target_property(_source_type ${t} mlir_python_SOURCES_TYPE)
493132bc6e2SStella Laurenzo    get_target_property(_depends ${t} mlir_python_DEPENDS)
494310c9496SStella Laurenzo    if(_source_type)
495310c9496SStella Laurenzo      list(APPEND _flattened "${t}")
496310c9496SStella Laurenzo      if(_depends)
497310c9496SStella Laurenzo        _flatten_mlir_python_targets(_local_flattened ${_depends})
498310c9496SStella Laurenzo        list(APPEND _flattened ${_local_flattened})
499310c9496SStella Laurenzo      endif()
500310c9496SStella Laurenzo    endif()
501310c9496SStella Laurenzo  endforeach()
502310c9496SStella Laurenzo  list(REMOVE_DUPLICATES _flattened)
503310c9496SStella Laurenzo  set(${output_var} "${_flattened}" PARENT_SCOPE)
504310c9496SStella Laurenzoendfunction()
505310c9496SStella Laurenzo
506ac521d9eSStella Stamenova# Function: add_mlir_python_sources_target
507ac521d9eSStella Stamenova# Adds a target corresponding to an interface target that carries source
508ac521d9eSStella Stamenova# information. This target is responsible for "building" the sources by
509ac521d9eSStella Stamenova# placing them in the correct locations in the build and install trees.
510ac521d9eSStella Stamenova# Arguments:
511ac521d9eSStella Stamenova#   INSTALL_COMPONENT: Name of the install component. Typically same as the
512ac521d9eSStella Stamenova#     target name passed to add_mlir_python_modules().
513ac521d9eSStella Stamenova#   INSTALL_DESTINATION: Prefix into the install tree in which to install the
514ac521d9eSStella Stamenova#     library.
515ac521d9eSStella Stamenova#   OUTPUT_DIRECTORY: Full path in the build tree in which to create the
516ac521d9eSStella Stamenova#     library. Typically, this will be the common _mlir_libs directory where
517ac521d9eSStella Stamenova#     all extensions are emitted.
518ac521d9eSStella Stamenova#   SOURCES_TARGETS: List of interface libraries that carry source information.
519ac521d9eSStella Stamenovafunction(add_mlir_python_sources_target name)
520ac521d9eSStella Stamenova  cmake_parse_arguments(ARG
521ac521d9eSStella Stamenova  ""
522ac521d9eSStella Stamenova  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
523ac521d9eSStella Stamenova  "SOURCES_TARGETS"
524ac521d9eSStella Stamenova  ${ARGN})
525ac521d9eSStella Stamenova
526ac521d9eSStella Stamenova  if(ARG_UNPARSED_ARGUMENTS)
527ac521d9eSStella Stamenova    message(FATAL_ERROR "Unhandled arguments to add_mlir_python_sources_target(${name}, ... : ${ARG_UNPARSED_ARGUMENTS}")
528ac521d9eSStella Stamenova  endif()
529ac521d9eSStella Stamenova
530ac521d9eSStella Stamenova  add_custom_target(${name})
531ac521d9eSStella Stamenova
532ac521d9eSStella Stamenova  # On Windows create_symlink requires special permissions. Use copy_if_different instead.
533ac521d9eSStella Stamenova  if(CMAKE_HOST_WIN32)
534ac521d9eSStella Stamenova    set(_link_or_copy copy_if_different)
535ac521d9eSStella Stamenova  else()
536ac521d9eSStella Stamenova    set(_link_or_copy create_symlink)
537ac521d9eSStella Stamenova  endif()
538ac521d9eSStella Stamenova
539ac521d9eSStella Stamenova  foreach(_sources_target ${ARG_SOURCES_TARGETS})
540ac521d9eSStella Stamenova    add_dependencies(${name} ${_sources_target})
541ac521d9eSStella Stamenova
542ac521d9eSStella Stamenova    get_target_property(_src_paths ${_sources_target} SOURCES)
543ac521d9eSStella Stamenova    if(NOT _src_paths)
544ac521d9eSStella Stamenova      get_target_property(_src_paths ${_sources_target} INTERFACE_SOURCES)
545ac521d9eSStella Stamenova      if(NOT _src_paths)
546ac521d9eSStella Stamenova        break()
547ac521d9eSStella Stamenova      endif()
548ac521d9eSStella Stamenova    endif()
549ac521d9eSStella Stamenova
550ac521d9eSStella Stamenova    get_target_property(_root_dir ${_sources_target} INCLUDE_DIRECTORIES)
551ac521d9eSStella Stamenova    if(NOT _root_dir)
552ac521d9eSStella Stamenova      get_target_property(_root_dir ${_sources_target} INTERFACE_INCLUDE_DIRECTORIES)
553ac521d9eSStella Stamenova    endif()
554ac521d9eSStella Stamenova
555ac521d9eSStella Stamenova    foreach(_src_path ${_src_paths})
556ac521d9eSStella Stamenova      file(RELATIVE_PATH _source_relative_path "${_root_dir}" "${_src_path}")
557ac521d9eSStella Stamenova      set(_dest_path "${ARG_OUTPUT_DIRECTORY}/${_source_relative_path}")
558ac521d9eSStella Stamenova
559ac521d9eSStella Stamenova      get_filename_component(_dest_dir "${_dest_path}" DIRECTORY)
560ac521d9eSStella Stamenova      file(MAKE_DIRECTORY "${_dest_dir}")
561ac521d9eSStella Stamenova
562ac521d9eSStella Stamenova      add_custom_command(
563ac521d9eSStella Stamenova        TARGET ${name} PRE_BUILD
564ac521d9eSStella Stamenova        COMMENT "Copying python source ${_src_path} -> ${_dest_path}"
565ac521d9eSStella Stamenova        DEPENDS "${_src_path}"
566ac521d9eSStella Stamenova        BYPRODUCTS "${_dest_path}"
567ac521d9eSStella Stamenova        COMMAND "${CMAKE_COMMAND}" -E ${_link_or_copy}
568ac521d9eSStella Stamenova            "${_src_path}" "${_dest_path}"
569ac521d9eSStella Stamenova      )
570ac521d9eSStella Stamenova      if(ARG_INSTALL_DIR)
571*2aa6d56dSStella Laurenzo        # We have to install each file individually because we need to preserve
572*2aa6d56dSStella Laurenzo        # the relative directory structure in the install destination.
573*2aa6d56dSStella Laurenzo        # As an example, ${_source_relative_path} may be dialects/math.py
574*2aa6d56dSStella Laurenzo        # which would be transformed to ${ARG_INSTALL_DIR}/dialects
575*2aa6d56dSStella Laurenzo        # here. This could be moved outside of the loop and cleaned up
576*2aa6d56dSStella Laurenzo        # if we used FILE_SETS (introduced in CMake 3.23).
577*2aa6d56dSStella Laurenzo        get_filename_component(_install_destination "${ARG_INSTALL_DIR}/${_source_relative_path}" DIRECTORY)
578ac521d9eSStella Stamenova        install(
579*2aa6d56dSStella Laurenzo          FILES ${_src_path}
580*2aa6d56dSStella Laurenzo          DESTINATION "${_install_destination}"
581ac521d9eSStella Stamenova          COMPONENT ${ARG_INSTALL_COMPONENT}
582ac521d9eSStella Stamenova        )
583ac521d9eSStella Stamenova      endif()
584ac521d9eSStella Stamenova    endforeach()
585*2aa6d56dSStella Laurenzo  endforeach()
586ac521d9eSStella Stamenovaendfunction()
587ac521d9eSStella Stamenova
588310c9496SStella Laurenzo################################################################################
5899f3f6d7bSStella Laurenzo# Build python extension
5909f3f6d7bSStella Laurenzo################################################################################
5919f3f6d7bSStella Laurenzofunction(add_mlir_python_extension libname extname)
5929f3f6d7bSStella Laurenzo  cmake_parse_arguments(ARG
5939f3f6d7bSStella Laurenzo  ""
594310c9496SStella Laurenzo  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
5959f3f6d7bSStella Laurenzo  "SOURCES;LINK_LIBS"
5969f3f6d7bSStella Laurenzo  ${ARGN})
5979f3f6d7bSStella Laurenzo  if(ARG_UNPARSED_ARGUMENTS)
5989f3f6d7bSStella Laurenzo    message(FATAL_ERROR "Unhandled arguments to add_mlir_python_extension(${libname}, ... : ${ARG_UNPARSED_ARGUMENTS}")
5999f3f6d7bSStella Laurenzo  endif()
6009f3f6d7bSStella Laurenzo
6019f3f6d7bSStella Laurenzo  # The actual extension library produces a shared-object or DLL and has
6029f3f6d7bSStella Laurenzo  # sources that must be compiled in accordance with pybind11 needs (RTTI and
6039f3f6d7bSStella Laurenzo  # exceptions).
60455e76c70SMike Urbach  pybind11_add_module(${libname}
6059f3f6d7bSStella Laurenzo    ${ARG_SOURCES}
6069f3f6d7bSStella Laurenzo  )
6079f3f6d7bSStella Laurenzo
6089f3f6d7bSStella Laurenzo  # The extension itself must be compiled with RTTI and exceptions enabled.
6099f3f6d7bSStella Laurenzo  # Also, some warning classes triggered by pybind11 are disabled.
6109f3f6d7bSStella Laurenzo  target_compile_options(${libname} PRIVATE
6119f3f6d7bSStella Laurenzo    $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
6129f3f6d7bSStella Laurenzo      # Enable RTTI and exceptions.
6139f3f6d7bSStella Laurenzo      -frtti -fexceptions
6149f3f6d7bSStella Laurenzo    >
6159f3f6d7bSStella Laurenzo    $<$<CXX_COMPILER_ID:MSVC>:
6169f3f6d7bSStella Laurenzo      # Enable RTTI and exceptions.
6179f3f6d7bSStella Laurenzo      /EHsc /GR>
6189f3f6d7bSStella Laurenzo  )
6199f3f6d7bSStella Laurenzo
6209f3f6d7bSStella Laurenzo  # Configure the output to match python expectations.
6219f3f6d7bSStella Laurenzo  set_target_properties(
6229f3f6d7bSStella Laurenzo    ${libname} PROPERTIES
623310c9496SStella Laurenzo    LIBRARY_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
6249f3f6d7bSStella Laurenzo    OUTPUT_NAME "${extname}"
6251de7a17fSStella Laurenzo    NO_SONAME ON
6269f3f6d7bSStella Laurenzo  )
6279f3f6d7bSStella Laurenzo
6289f3f6d7bSStella Laurenzo  if(WIN32)
6299f3f6d7bSStella Laurenzo    # Need to also set the RUNTIME_OUTPUT_DIRECTORY on Windows in order to
6309f3f6d7bSStella Laurenzo    # control where the .dll gets written.
6319f3f6d7bSStella Laurenzo    set_target_properties(
6329f3f6d7bSStella Laurenzo      ${libname} PROPERTIES
633310c9496SStella Laurenzo      RUNTIME_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
6345821047aSJohn Demme      ARCHIVE_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
6359f3f6d7bSStella Laurenzo    )
6369f3f6d7bSStella Laurenzo  endif()
6379f3f6d7bSStella Laurenzo
6389f3f6d7bSStella Laurenzo  target_link_libraries(${libname}
6399f3f6d7bSStella Laurenzo    PRIVATE
6409f3f6d7bSStella Laurenzo    ${ARG_LINK_LIBS}
6419f3f6d7bSStella Laurenzo  )
6429f3f6d7bSStella Laurenzo
6439f3f6d7bSStella Laurenzo  target_link_options(${libname}
6449f3f6d7bSStella Laurenzo    PRIVATE
6459f3f6d7bSStella Laurenzo      # On Linux, disable re-export of any static linked libraries that
6469f3f6d7bSStella Laurenzo      # came through.
6479f3f6d7bSStella Laurenzo      $<$<PLATFORM_ID:Linux>:LINKER:--exclude-libs,ALL>
6489f3f6d7bSStella Laurenzo  )
6499f3f6d7bSStella Laurenzo
650784a5bccSStella Stamenova  if(WIN32)
651784a5bccSStella Stamenova    # On Windows, pyconfig.h (and by extension python.h) hardcode the version of the
652784a5bccSStella Stamenova    # python library which will be used for linkage depending on the flavor of the build.
653784a5bccSStella Stamenova    # pybind11 has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
654784a5bccSStella Stamenova    # is not passed in as a compile definition, pybind11 undefs _DEBUG when including
655784a5bccSStella Stamenova    # python.h, so that the release python library would be used).
656784a5bccSStella Stamenova    # Since mlir uses pybind11, we can leverage their workaround by never directly
657784a5bccSStella Stamenova    # pyconfig.h or python.h and instead relying on the pybind11 headers to include the
658784a5bccSStella Stamenova    # necessary python headers. This results in mlir always linking against the
659784a5bccSStella Stamenova    # release python library via the (undocumented) cmake property Python3_LIBRARY_RELEASE.
660784a5bccSStella Stamenova    target_link_libraries(${libname} PRIVATE ${Python3_LIBRARY_RELEASE})
661784a5bccSStella Stamenova  endif()
662784a5bccSStella Stamenova
6639f3f6d7bSStella Laurenzo  ################################################################################
6649f3f6d7bSStella Laurenzo  # Install
6659f3f6d7bSStella Laurenzo  ################################################################################
6669f3f6d7bSStella Laurenzo  if(ARG_INSTALL_DIR)
6679f3f6d7bSStella Laurenzo    install(TARGETS ${libname}
668310c9496SStella Laurenzo      COMPONENT ${ARG_INSTALL_COMPONENT}
6699f3f6d7bSStella Laurenzo      LIBRARY DESTINATION ${ARG_INSTALL_DIR}
6709f3f6d7bSStella Laurenzo      ARCHIVE DESTINATION ${ARG_INSTALL_DIR}
6719f3f6d7bSStella Laurenzo      # NOTE: Even on DLL-platforms, extensions go in the lib directory tree.
6729f3f6d7bSStella Laurenzo      RUNTIME DESTINATION ${ARG_INSTALL_DIR}
6739f3f6d7bSStella Laurenzo    )
6749f3f6d7bSStella Laurenzo  endif()
6759f3f6d7bSStella Laurenzoendfunction()
676