@@ -233,33 +233,35 @@ will:
233233
234234.. _type-visibility :
235235
236- How can I avoid conflicts with other projects using nanobind?
237- -------------------------------------------------------------
238-
239- Suppose that a type binding in your project conflicts with another extension, for
240- example because both expose a common type (e.g., ``std::latch ``). nanobind will
241- warn whenever it detects such a conflict:
236+ How can I control whether two extension modules see each other's types?
237+ -----------------------------------------------------------------------
238+
239+ nanobind creates a variety of internal data structures to support the bindings
240+ you ask it to make. These are not isolated to a single extension module,
241+ because it's useful for large binding projects to be able to split related
242+ bindings into multiple extensions without losing their ability to work with
243+ one another's types. Instead, extension modules are divided into nanobind
244+ *domains * based on two attributes: an automatically determined string (the
245+ nanobind "ABI tag") that captures compatibility-relevant aspects of their
246+ build environments and nanobind versions, and an ``NB_DOMAIN `` string that
247+ may be provided at build time (as shown below).
248+ If multiple extension modules share the same ABI tag and the same ``NB_DOMAIN ``
249+ string, they will wind up in the same nanobind domain, which allows them to
250+ work together exactly as if they were one big extension.
251+
252+ Sometimes, this causes problems. For example, you might expose a binding
253+ for a commonly used type (such as ``std::latch ``) that some other nanobind
254+ extension in your Python interpreter also happens to provide a binding for.
255+ nanobind will warn whenever it detects such a conflict:
242256
243257.. code-block :: text
244258
245259 RuntimeWarning: nanobind: type 'latch' was already registered!
246260
247261 In the worst case, this could actually break both packages (especially if the
248- bindings of the two packages expose an inconsistent/incompatible API).
249-
250- The higher-level issue here is that nanobind will by default try to make type
251- bindings visible across extensions because this is helpful to partition large
252- binding projects into smaller parts. Such information exchange requires that
253- the extensions:
254-
255- - use the same nanobind *ABI version * (see the :ref: `Changelog <changelog >` for details).
256- - use the same compiler (extensions built with GCC and Clang are isolated from each other).
257- - use ABI-compatible versions of the C++ library.
258- - use the stable ABI interface consistently (stable and unstable builds are isolated from each other).
259- - use debug/release mode consistently (debug and release builds are isolated from each other).
260-
261- In addition, nanobind provides a feature to intentionally scope extensions to a
262- named domain to avoid conflicts with other extensions. To do so, specify the
262+ bindings of the two packages expose an inconsistent/incompatible API). So it's
263+ useful to be able to enforce a boundary between extensions sometimes, even if
264+ they would otherwise be ABI-compatible. To do so, you can specify the
263265``NB_DOMAIN `` parameter in CMake:
264266
265267.. code-block :: cmake
@@ -268,8 +270,54 @@ named domain to avoid conflicts with other extensions. To do so, specify the
268270 NB_DOMAIN my_project
269271 my_ext.cpp)
270272
271- In this case, inter-extension type visibility is furthermore restricted to
272- extensions in the ``"my_project" `` domain.
273+ Two extensions can only be in the same nanobind domain if either they both
274+ specify the same value for that parameter (``"my_project" `` in this case) or
275+ neither one specifies the parameter.
276+
277+ As mentioned above, two extensions can also only be in the same nanobind domain
278+ if they share the same ABI tag. This is determined in two parts, as follows:
279+
280+ - They must use compatible *platform ABI *, so that (for example) a
281+ ``std::vector<int> `` created in one can be safely used in the other.
282+ That means:
283+
284+ - They must use the same C++ standard library (MSVC, libc++, or libstdc++),
285+ and the same ABI version of it. For example, extensions that use libstdc++
286+ must match in terms of whether they use the pre- or post-C++11 ABI, and
287+ extensions that use libc++ must use the same `libc++ ABI version
288+ <https://libcxx.llvm.org/DesignDocs/ABIVersioning.html> `__.
289+
290+ - On Windows, they must use the same compiler (MSVC, mingw, or cygwin).
291+
292+ - If compiled using MSVC, they must use the same major version of the
293+ compiler, the same multi-threading style (dynamic ``/MD ``,
294+ static ``/MT ``, or single-threaded), and they must match in terms of
295+ whether or not they are built in debugging mode.
296+
297+ - They must use compatible *nanobind ABI *, so that the nanobind internal data
298+ structures created in one can be safely used in the other. That means:
299+
300+ - They must use the same nanobind *ABI version *; see the
301+ :ref: `Changelog <changelog >` for details.
302+
303+ - They must be consistent in their use of Python's stable ABI: either
304+ both built against the stable ABI (cmake ``STABLE_ABI `` flag) or both not.
305+
306+ - They must be consistent in their use of free-threading: either both
307+ built with free-threading support (cmake ``FREE_THREADED `` flag) or both not.
308+
309+ - They must either both use released versions of nanobind or both be built
310+ from Git development snapshots, rather than a mix of the two.
311+
312+ If you want to share some types between two extensions that have the same
313+ platform ABI (the first category in the above list), but are in different
314+ nanobind domains due to using different nanobind ABI or different specified
315+ ``NB_DOMAIN `` strings, all is not lost! The :ref: `interoperability support
316+ <interop>` between nanobind and other binding libraries also provides
317+ interoperability between different nanobind domains, as long as the platform
318+ ABI matches. It must be specifically enabled, and there are a few things it
319+ can't do, but for most purposes it's hard to tell the difference from operating
320+ within the same domain. See the linked documentation for more details.
273321
274322Can I use nanobind without RTTI or C++ exceptions?
275323--------------------------------------------------
0 commit comments