1################################################################################
2# Python modules
3# MLIR's Python modules are both directly used by the core project and are
4# available for use and embedding into external projects (in their own
5# namespace and with their own deps). In order to facilitate this, python
6# artifacts are split between declarations, which make a subset of
7# things available to be built and "add", which in line with the normal LLVM
8# nomenclature, adds libraries.
9################################################################################
10
11# Function: declare_mlir_python_sources
12# Declares pure python sources as part of a named grouping that can be built
13# later.
14# Arguments:
15#   ROOT_DIR: Directory where the python namespace begins (defaults to
16#     CMAKE_CURRENT_SOURCE_DIR). For non-relocatable sources, this will
17#     typically just be the root of the python source tree (current directory).
18#     For relocatable sources, this will point deeper into the directory that
19#     can be relocated. For generated sources, can be relative to
20#     CMAKE_CURRENT_BINARY_DIR. Generated and non generated sources cannot be
21#     mixed.
22#   ADD_TO_PARENT: Adds this source grouping to a previously declared source
23#     grouping. Source groupings form a DAG.
24#   SOURCES: List of specific source files relative to ROOT_DIR to include.
25#   SOURCES_GLOB: List of glob patterns relative to ROOT_DIR to include.
26#   DEST_PREFIX: Destination prefix to prepend to files in the python
27#     package directory namespace.
28function(declare_mlir_python_sources name)
29  cmake_parse_arguments(ARG
30    ""
31    "ROOT_DIR;ADD_TO_PARENT;DEST_PREFIX"
32    "SOURCES;SOURCES_GLOB"
33    ${ARGN})
34
35  if(NOT ARG_ROOT_DIR)
36    set(ARG_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
37  endif()
38  set(_install_destination "src/python/${name}")
39
40  # Process the glob.
41  set(_glob_sources)
42  if(ARG_SOURCES_GLOB)
43    set(_glob_spec ${ARG_SOURCES_GLOB})
44    list(TRANSFORM _glob_spec PREPEND "${ARG_ROOT_DIR}/")
45    file(GLOB_RECURSE _glob_sources
46      RELATIVE "${ARG_ROOT_DIR}"
47      ${_glob_spec}
48    )
49    list(APPEND ARG_SOURCES ${_glob_sources})
50  endif()
51
52  # We create a custom target to carry properties and dependencies for
53  # generated sources.
54  add_library(${name} INTERFACE)
55  set(_file_depends "${ARG_SOURCES}")
56  list(TRANSFORM _file_depends PREPEND "${ARG_ROOT_DIR}/")
57  set_target_properties(${name} PROPERTIES
58    # Yes: Leading-lowercase property names are load bearing and the recommended
59    # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
60    # Note that ROOT_DIR and FILE_DEPENDS are not exported because they are
61    # only relevant to in-tree uses.
62    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_DEST_PREFIX;mlir_python_ROOT_DIR;mlir_python_SOURCES;mlir_python_DEPENDS"
63    mlir_python_SOURCES_TYPE pure
64    mlir_python_ROOT_DIR "${ARG_ROOT_DIR}"
65    mlir_python_DEST_PREFIX "${ARG_DEST_PREFIX}"
66    mlir_python_SOURCES "${ARG_SOURCES}"
67    mlir_python_FILE_DEPENDS "${_file_depends}"
68    mlir_python_DEPENDS ""
69  )
70  # Note that an "include" directory has no meaning to such faux targets,
71  # but it is a CMake supported way to specify a directory search list in a
72  # way that works both in-tree and out. It has some super powers which are
73  # not possible to emulate with custom properties (because of the prohibition
74  # on using generator expressions in exported custom properties and the
75  # special dispensation for $<INSTALL_PREFIX>).
76  target_include_directories(${name} INTERFACE
77    "$<BUILD_INTERFACE:${ARG_ROOT_DIR}>"
78    "$<INSTALL_INTERFACE:${_install_destination}>"
79  )
80
81  # Add to parent.
82  if(ARG_ADD_TO_PARENT)
83    set_property(TARGET ${ARG_ADD_TO_PARENT} APPEND PROPERTY mlir_python_DEPENDS ${name})
84  endif()
85
86  # Install.
87  set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name})
88  if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
89    _mlir_python_install_sources(
90      ${name} "${ARG_ROOT_DIR}" "${_install_destination}"
91      ${ARG_SOURCES}
92    )
93  endif()
94endfunction()
95
96# Function: declare_mlir_python_extension
97# Declares a buildable python extension from C++ source files. The built
98# module is considered a python source file and included as everything else.
99# Arguments:
100#   ROOT_DIR: Root directory where sources are interpreted relative to.
101#     Defaults to CMAKE_CURRENT_SOURCE_DIR.
102#   MODULE_NAME: Local import name of the module (i.e. "_mlir").
103#   ADD_TO_PARENT: Same as for declare_mlir_python_sources.
104#   SOURCES: C++ sources making up the module.
105#   PRIVATE_LINK_LIBS: List of libraries to link in privately to the module
106#     regardless of how it is included in the project (generally should be
107#     static libraries that can be included with hidden visibility).
108#   EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
109#     on. These will be collected for all extensions and put into an
110#     aggregate dylib that is linked against.
111function(declare_mlir_python_extension name)
112  cmake_parse_arguments(ARG
113    ""
114    "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
115    "SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
116    ${ARGN})
117
118  if(NOT ARG_ROOT_DIR)
119    set(ARG_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
120  endif()
121  set(_install_destination "src/python/${name}")
122
123  add_library(${name} INTERFACE)
124  set_target_properties(${name} PROPERTIES
125    # Yes: Leading-lowercase property names are load bearing and the recommended
126    # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
127    # Note that ROOT_DIR and FILE_DEPENDS are not exported because they are
128    # only relevant to in-tree uses.
129    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_ROOT_DIR;mlir_python_EXTENSION_MODULE_NAME;mlir_python_CPP_SOURCES;mlir_python_PRIVATE_LINK_LIBS;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
130    mlir_python_SOURCES_TYPE extension
131    mlir_python_ROOT_DIR "${ARG_ROOT_DIR}"
132    mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
133    mlir_python_CPP_SOURCES "${ARG_SOURCES}"
134    mlir_python_PRIVATE_LINK_LIBS "${ARG_PRIVATE_LINK_LIBS}"
135    mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
136    mlir_python_FILE_DEPENDS ""
137    mlir_python_DEPENDS ""
138  )
139  # Note that an "include" directory has no meaning to such faux targets,
140  # but it is a CMake supported way to specify an install-prefix relative
141  # directory. It has some super powers which are not possible to emulate
142  # with custom properties (because of the prohibition on using generator
143  # expressions in exported custom properties and the special dispensation
144  # for $<INSTALL_PREFIX> and $<INSTALL_INTERFACE>). On imported targets,
145  # this is used as a single value, not as a list, so it must only have one
146  # item in it.
147  target_include_directories(${name} INTERFACE
148    "$<INSTALL_INTERFACE:${_install_destination}>"
149  )
150
151  # Add to parent.
152  if(ARG_ADD_TO_PARENT)
153    set_property(TARGET ${ARG_ADD_TO_PARENT} APPEND PROPERTY mlir_python_DEPENDS ${name})
154  endif()
155
156  # Install.
157  set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name})
158  if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
159    _mlir_python_install_sources(
160      ${name} "${ARG_ROOT_DIR}" "src/python/${name}"
161      ${ARG_SOURCES}
162    )
163  endif()
164endfunction()
165
166function(_mlir_python_install_sources name source_root_dir destination)
167  foreach(source_relative_path ${ARGN})
168    # Transform "a/b/c.py" -> "${install_prefix}/a/b" for installation.
169    get_filename_component(
170      dest_relative_path "${source_relative_path}" DIRECTORY
171      BASE_DIR "${source_root_dir}"
172    )
173    install(
174      FILES "${source_root_dir}/${source_relative_path}"
175      DESTINATION "${destination}/${dest_relative_path}"
176      COMPONENT "${name}"
177    )
178  endforeach()
179  get_target_export_arg(${name} MLIR export_to_mlirtargets UMBRELLA mlir-libraries)
180  install(TARGETS ${name}
181    COMPONENT ${name}
182    ${export_to_mlirtargets}
183  )
184endfunction()
185
186# Function: add_mlir_python_modules
187# Adds python modules to a project, building them from a list of declared
188# source groupings (see declare_mlir_python_sources and
189# declare_mlir_python_extension). One of these must be called for each
190# packaging root in use.
191# Arguments:
192#   ROOT_PREFIX: The directory in the build tree to emit sources. This will
193#     typically be something like ${MY_BINARY_DIR}/python_packages/foobar
194#     for non-relocatable modules or a deeper directory tree for relocatable.
195#   INSTALL_PREFIX: Prefix into the install tree for installing the package.
196#     Typically mirrors the path above but without an absolute path.
197#   DECLARED_SOURCES: List of declared source groups to include. The entire
198#     DAG of source modules is included.
199#   COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
200#     extension depend on (see mlir_python_add_common_capi_library).
201function(add_mlir_python_modules name)
202  cmake_parse_arguments(ARG
203    ""
204    "ROOT_PREFIX;INSTALL_PREFIX;COMMON_CAPI_LINK_LIBS"
205    "DECLARED_SOURCES"
206    ${ARGN})
207  # Helper to process an individual target.
208  function(_process_target modules_target sources_target)
209    get_target_property(_source_type ${sources_target} mlir_python_SOURCES_TYPE)
210
211    get_target_property(_python_root_dir ${sources_target} mlir_python_ROOT_DIR)
212    if(NOT _python_root_dir)
213      message(FATAL_ERROR "Target ${sources_target} lacks mlir_python_ROOT_DIR property")
214    endif()
215
216    if(_source_type STREQUAL "pure")
217      # Pure python sources to link into the tree.
218      get_target_property(_python_sources ${sources_target} mlir_python_SOURCES)
219      get_target_property(_specified_dest_prefix ${sources_target} mlir_python_DEST_PREFIX)
220      foreach(_source_relative_path ${_python_sources})
221        set(_dest_relative_path "${_source_relative_path}")
222        if(_specified_dest_prefix)
223          set(_dest_relative_path "${_specified_dest_prefix}/${_dest_relative_path}")
224        endif()
225        set(_src_path "${_python_root_dir}/${_source_relative_path}")
226        set(_dest_path "${ARG_ROOT_PREFIX}/${_dest_relative_path}")
227
228        get_filename_component(_dest_dir "${_dest_path}" DIRECTORY)
229        get_filename_component(_install_path "${ARG_INSTALL_PREFIX}/${_dest_relative_path}" DIRECTORY)
230
231        file(MAKE_DIRECTORY "${_dest_dir}")
232        add_custom_command(
233          TARGET ${modules_target} PRE_BUILD
234          COMMENT "Copying python source ${_src_path} -> ${_dest_path}"
235          DEPENDS "${_src_path}"
236          BYPRODUCTS "${_dest_path}"
237          COMMAND "${CMAKE_COMMAND}" -E create_symlink
238              "${_src_path}" "${_dest_path}"
239        )
240        install(
241          FILES "${_src_path}"
242          DESTINATION "${_install_path}"
243          COMPONENT ${modules_target}
244        )
245      endforeach()
246    elseif(_source_type STREQUAL "extension")
247      # Native CPP extension.
248      get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
249      get_target_property(_cpp_sources ${sources_target} mlir_python_CPP_SOURCES)
250      get_target_property(_private_link_libs ${sources_target} mlir_python_PRIVATE_LINK_LIBS)
251      # Transform relative source to based on root dir.
252      list(TRANSFORM _cpp_sources PREPEND "${_python_root_dir}/")
253      set(_extension_target "${name}.extension.${_module_name}.dso")
254      add_mlir_python_extension(${_extension_target} "${_module_name}"
255        INSTALL_COMPONENT ${modules_target}
256        INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
257        OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
258        SOURCES ${_cpp_sources}
259        LINK_LIBS PRIVATE
260          ${_private_link_libs}
261          ${ARG_COMMON_CAPI_LINK_LIBS}
262      )
263      add_dependencies(${name} ${_extension_target})
264      mlir_python_setup_extension_rpath(${_extension_target})
265    else()
266      message(SEND_ERROR "Unrecognized source type '${_source_type}' for python source target ${sources_target}")
267      return()
268    endif()
269  endfunction()
270
271  _flatten_mlir_python_targets(_flat_targets ${ARG_DECLARED_SOURCES})
272  # Collect dependencies.
273  set(_depends)
274  foreach(sources_target ${_flat_targets})
275    get_target_property(_local_depends ${sources_target} mlir_python_FILE_DEPENDS)
276    if(_local_depends)
277      list(APPEND _depends ${_local_depends})
278    endif()
279  endforeach()
280
281  # Build the modules target.
282  add_custom_target(${name} ALL DEPENDS ${_depends})
283  foreach(sources_target ${_flat_targets})
284    _process_target(${name} ${sources_target})
285  endforeach()
286
287  # Create an install target.
288  if (NOT LLVM_ENABLE_IDE)
289    add_llvm_install_targets(
290      install-${name}
291      DEPENDS ${name}
292      COMPONENT ${name})
293  endif()
294endfunction()
295
296# Function: declare_mlir_dialect_python_bindings
297# Helper to generate source groups for dialects, including both static source
298# files and a TD_FILE to generate wrappers.
299#
300# This will generate a source group named ${ADD_TO_PARENT}.${DIALECT_NAME}.
301#
302# Arguments:
303#   ROOT_DIR: Same as for declare_mlir_python_sources().
304#   ADD_TO_PARENT: Same as for declare_mlir_python_sources(). Unique names
305#     for the subordinate source groups are derived from this.
306#   TD_FILE: Tablegen file to generate source for (relative to ROOT_DIR).
307#   DIALECT_NAME: Python name of the dialect.
308#   SOURCES: Same as declare_mlir_python_sources().
309#   SOURCES_GLOB: Same as declare_mlir_python_sources().
310#   DEPENDS: Additional dependency targets.
311function(declare_mlir_dialect_python_bindings)
312  cmake_parse_arguments(ARG
313    ""
314    "ROOT_DIR;ADD_TO_PARENT;TD_FILE;DIALECT_NAME"
315    "SOURCES;SOURCES_GLOB;DEPENDS"
316    ${ARGN})
317  # Sources.
318  set(_dialect_target "${ARG_ADD_TO_PARENT}.${ARG_DIALECT_NAME}")
319  declare_mlir_python_sources(${_dialect_target}
320    ROOT_DIR "${ARG_ROOT_DIR}"
321    ADD_TO_PARENT "${ARG_ADD_TO_PARENT}"
322    SOURCES "${ARG_SOURCES}"
323    SOURCES_GLOB "${ARG_SOURCES_GLOB}"
324  )
325
326  # Tablegen
327  if(ARG_TD_FILE)
328    set(tblgen_target "${ARG_ADD_TO}.${ARG_DIALECT_NAME}.tablegen")
329    set(td_file "${ARG_ROOT_DIR}/${ARG_TD_FILE}")
330    get_filename_component(relative_td_directory "${ARG_TD_FILE}" DIRECTORY)
331    file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${relative_td_directory}")
332    set(dialect_filename "${relative_td_directory}/_${ARG_DIALECT_NAME}_ops_gen.py")
333    set(LLVM_TARGET_DEFINITIONS ${td_file})
334    mlir_tablegen("${dialect_filename}" -gen-python-op-bindings
335                  -bind-dialect=${ARG_DIALECT_NAME})
336    add_public_tablegen_target(${tblgen_target})
337    if(ARG_DEPENDS)
338      add_dependencies(${tblgen_target} ${ARG_DEPENDS})
339    endif()
340
341    # Generated.
342    declare_mlir_python_sources("${ARG_ADD_TO_PARENT}.${ARG_DIALECT_NAME}.ops_gen"
343      ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
344      ADD_TO_PARENT "${_dialect_target}"
345      SOURCES "${dialect_filename}"
346    )
347  endif()
348endfunction()
349
350# Function: mlir_python_setup_extension_rpath
351# Sets RPATH properties on a target, assuming that it is being output to
352# an _mlir_libs directory with all other libraries. For static linkage,
353# the RPATH will just be the origin. If linking dynamically, then the LLVM
354# library directory will be added.
355# Arguments:
356#   RELATIVE_INSTALL_ROOT: If building dynamically, an RPATH entry will be
357#     added to the install tree lib/ directory by first traversing this
358#     path relative to the installation location. Typically a number of ".."
359#     entries, one for each level of the install path.
360function(mlir_python_setup_extension_rpath target)
361  cmake_parse_arguments(ARG
362    ""
363    "RELATIVE_INSTALL_ROOT"
364    ""
365    ${ARGN})
366
367  # RPATH handling.
368  # For the build tree, include the LLVM lib directory and the current
369  # directory for RPATH searching. For install, just the current directory
370  # (assumes that needed dependencies have been installed).
371  if(NOT APPLE AND NOT UNIX)
372    return()
373  endif()
374
375  set(_origin_prefix "\$ORIGIN")
376  if(APPLE)
377    set(_origin_prefix "@loader_path")
378  endif()
379  set_target_properties(${target} PROPERTIES
380    BUILD_WITH_INSTALL_RPATH OFF
381    BUILD_RPATH "${_origin_prefix}"
382    INSTALL_RPATH "${_origin_prefix}"
383  )
384
385  # For static builds, that is all that is needed: all dependencies will be in
386  # the one directory. For shared builds, then we also need to add the global
387  # lib directory. This will be absolute for the build tree and relative for
388  # install.
389  # When we have access to CMake >= 3.20, there is a helper to calculate this.
390  if(BUILD_SHARED_LIBS AND ARG_RELATIVE_INSTALL_ROOT)
391    get_filename_component(_real_lib_dir "${LLVM_LIBRARY_OUTPUT_INTDIR}" REALPATH)
392    set_property(TARGET ${target} APPEND PROPERTY
393      BUILD_RPATH "${_real_lib_dir}")
394    set_property(TARGET ${target} APPEND PROPERTY
395      INSTALL_RPATH "${_origin_prefix}/${ARG_RELATIVE_INSTALL_ROOT}/lib${LLVM_LIBDIR_SUFFIX}")
396  endif()
397endfunction()
398
399# Function: add_mlir_python_common_capi_library
400# Adds a shared library which embeds dependent CAPI libraries needed to link
401# all extensions.
402# Arguments:
403#   INSTALL_COMPONENT: Name of the install component. Typically same as the
404#     target name passed to add_mlir_python_modules().
405#   INSTALL_DESTINATION: Prefix into the install tree in which to install the
406#     library.
407#   OUTPUT_DIRECTORY: Full path in the build tree in which to create the
408#     library. Typically, this will be the common _mlir_libs directory where
409#     all extensions are emitted.
410#   RELATIVE_INSTALL_ROOT: See mlir_python_setup_extension_rpath().
411#   DECLARED_SOURCES: Source groups from which to discover dependent
412#     EMBED_CAPI_LINK_LIBS.
413#   EMBED_LIBS: Additional libraries to embed (must be built with OBJECTS and
414#     have an "obj.${name}" object library associated).
415function(add_mlir_python_common_capi_library name)
416  cmake_parse_arguments(ARG
417    ""
418    "INSTALL_COMPONENT;INSTALL_DESTINATION;OUTPUT_DIRECTORY;RELATIVE_INSTALL_ROOT"
419    "DECLARED_SOURCES;EMBED_LIBS"
420    ${ARGN})
421  # Collect all explicit and transitive embed libs.
422  set(_embed_libs ${ARG_EMBED_LIBS})
423  _flatten_mlir_python_targets(_all_source_targets ${ARG_DECLARED_SOURCES})
424  foreach(t ${_all_source_targets})
425    get_target_property(_local_embed_libs ${t} mlir_python_EMBED_CAPI_LINK_LIBS)
426    if(_local_embed_libs)
427      list(APPEND _embed_libs ${_local_embed_libs})
428    endif()
429  endforeach()
430  list(REMOVE_DUPLICATES _embed_libs)
431
432  # Generate the aggregate .so that everything depends on.
433  add_mlir_aggregate(${name}
434    SHARED
435    DISABLE_INSTALL
436    EMBED_LIBS ${_embed_libs}
437  )
438
439  if(MSVC)
440    set_property(TARGET ${name} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
441  endif()
442  set_target_properties(${name} PROPERTIES
443    LIBRARY_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
444    BINARY_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
445    # Needed for windows (and don't hurt others).
446    RUNTIME_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
447    ARCHIVE_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}"
448  )
449  mlir_python_setup_extension_rpath(${name}
450    RELATIVE_INSTALL_ROOT "${ARG_RELATIVE_INSTALL_ROOT}"
451  )
452  install(TARGETS ${name}
453    COMPONENT ${ARG_INSTALL_COMPONENT}
454    LIBRARY DESTINATION "${ARG_INSTALL_DESTINATION}"
455    RUNTIME DESTINATION "${ARG_INSTALL_DESTINATION}"
456  )
457
458endfunction()
459
460function(_flatten_mlir_python_targets output_var)
461  set(_flattened)
462  foreach(t ${ARGN})
463    get_target_property(_source_type ${t} mlir_python_SOURCES_TYPE)
464    get_target_property(_depends ${t} mlir_python_DEPENDS)
465    if(_source_type)
466      list(APPEND _flattened "${t}")
467      if(_depends)
468        _flatten_mlir_python_targets(_local_flattened ${_depends})
469        list(APPEND _flattened ${_local_flattened})
470      endif()
471    endif()
472  endforeach()
473  list(REMOVE_DUPLICATES _flattened)
474  set(${output_var} "${_flattened}" PARENT_SCOPE)
475endfunction()
476
477################################################################################
478# Build python extension
479################################################################################
480function(add_mlir_python_extension libname extname)
481  cmake_parse_arguments(ARG
482  ""
483  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
484  "SOURCES;LINK_LIBS"
485  ${ARGN})
486  if (ARG_UNPARSED_ARGUMENTS)
487    message(FATAL_ERROR " Unhandled arguments to add_mlir_python_extension(${libname}, ... : ${ARG_UNPARSED_ARGUMENTS}")
488  endif()
489  if ("${ARG_SOURCES}" STREQUAL "")
490    message(FATAL_ERROR " Missing SOURCES argument to add_mlir_python_extension(${libname}, ...")
491  endif()
492
493  # The actual extension library produces a shared-object or DLL and has
494  # sources that must be compiled in accordance with pybind11 needs (RTTI and
495  # exceptions).
496  pybind11_add_module(${libname}
497    ${ARG_SOURCES}
498  )
499
500  # The extension itself must be compiled with RTTI and exceptions enabled.
501  # Also, some warning classes triggered by pybind11 are disabled.
502  target_compile_options(${libname} PRIVATE
503    $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
504      # Enable RTTI and exceptions.
505      -frtti -fexceptions
506    >
507    $<$<CXX_COMPILER_ID:MSVC>:
508      # Enable RTTI and exceptions.
509      /EHsc /GR>
510  )
511
512  # Configure the output to match python expectations.
513  set_target_properties(
514    ${libname} PROPERTIES
515    LIBRARY_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
516    OUTPUT_NAME "${extname}"
517    NO_SONAME ON
518  )
519
520  if(WIN32)
521    # Need to also set the RUNTIME_OUTPUT_DIRECTORY on Windows in order to
522    # control where the .dll gets written.
523    set_target_properties(
524      ${libname} PROPERTIES
525      RUNTIME_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
526      ARCHIVE_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
527    )
528  endif()
529
530  # Python extensions depends *only* on the public API and LLVMSupport unless
531  # if further dependencies are added explicitly.
532  target_link_libraries(${libname}
533    PRIVATE
534    ${ARG_LINK_LIBS}
535    ${PYEXT_LIBADD}
536  )
537
538  target_link_options(${libname}
539    PRIVATE
540      # On Linux, disable re-export of any static linked libraries that
541      # came through.
542      $<$<PLATFORM_ID:Linux>:LINKER:--exclude-libs,ALL>
543  )
544
545  ################################################################################
546  # Install
547  ################################################################################
548  if (ARG_INSTALL_DIR)
549    install(TARGETS ${libname}
550      COMPONENT ${ARG_INSTALL_COMPONENT}
551      LIBRARY DESTINATION ${ARG_INSTALL_DIR}
552      ARCHIVE DESTINATION ${ARG_INSTALL_DIR}
553      # NOTE: Even on DLL-platforms, extensions go in the lib directory tree.
554      RUNTIME DESTINATION ${ARG_INSTALL_DIR}
555    )
556  endif()
557endfunction()
558