Skip to content

Commit dce91b4

Browse files
committed
some information about alternative build systems
1 parent 4148deb commit dce91b4

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

docs/api_cmake.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ modules. This is needed because quite a few steps are involved: nanobind must
88
build the module, a library component, link the two together, and add a
99
different set of compilation and linker flags depending on the target platform.
1010

11+
If you prefer another build system, then you have the following options:
12+
13+
- `Nicholas Junge <https://github.com/nicholasjng>`__ has created a `Bazel
14+
interface <https://github.com/nicholasjng/nanobind-Bazel>`__ to nanobind.
15+
Please report Bazel-specific issues there.
16+
17+
- You could create a new build system from scratch that takes care of these
18+
steps. See `this file
19+
<https://github.com/wjakob/nanobind/blob/master/src/nb_combined.cpp>`__ for
20+
inspiration on how to do this on Linux. Note that you will be on your own if
21+
you choose to go this route---I unfortunately do not have the time to respond
22+
to GitHub tickets related to custom build systems.
23+
1124
The section on :ref:`building extensions <building>` provided an introductory
1225
example of how to set up a basic build system via the
1326
:cmake:command:`nanobind_add_module` command, which is the :ref:`high level

docs/building.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This section assumes that you have followed the instructions to :ref:`install
77
<installing>` nanobind. The easiest way to compile a nanobind-based extension
88
involves a CMake-based build system. Other build systems can likely be used as
99
well, but they are not officially supported.
10+
(The first section of the :ref:`CMake API reference <api_cmake>` mentions
11+
some alternatives.)
1012

1113
Here, we will create a new package from scratch. If you already have an
1214
existing CMake build system, it should be straightforward to merge some of the

src/nb_combined.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* You can use this file to compile bindings using alternative build systems in
2+
case the dependence on CMake is problematic. The following instructions
3+
illustrate how to do this Linux. The commands will need to be updated to
4+
target other operating systems, but that is beyond the scope of this writeup.
5+
To investigate platform-dependent subtleties, read the CMake build system
6+
or run it and examine the commands it generates.
7+
8+
Step 1: compile libnanobind
9+
10+
$ NB_DIR=<path to nanobind directory>
11+
12+
$ CXXFLAGS="-std=c++17 -fvisibility=hidden \
13+
-DNDEBUG -DNB_COMPACT_ASSERTIONS \
14+
`python3-config --includes` -fPIC \
15+
-I $NB_DIR/include \
16+
-I $NB_DIR/ext/robin_map/include"
17+
18+
$ g++ src/nb_combined.cpp $CXXFLAGS -O3 -fno-strict-aliasing \
19+
-ffunction-sections -fdata-sections -c -o libnanobind.o
20+
21+
Step 2: compile the extension code
22+
23+
$ g++ my_ext.cpp $CXXFLAGS -Os -c -o my_ext.o
24+
25+
Step 3: link the extension code with libnanobind
26+
27+
$ g++ -shared -Wl,-s -Wl,--gc-sections my_ext.o libnanobind.o \
28+
-o my_ext.cpython-310-x86_64-linux-gnu.so
29+
30+
A few comments about these compilation parameters:
31+
32+
- the '-ffunction-sections/-fdata-sections/--gc-sections' parameter ensure
33+
that unused parts of libnanobind are removed from the compiled extension.
34+
35+
- the '-fno-strict-aliasing' part is needed by the libnanobind part. This
36+
flag generally applies to code that uses significant amounts of 'raw'
37+
CPython API code. You should ensure that libnanobind isn't merged with
38+
other code via link time optimization (LTO), otherwise you may need to
39+
specify '-fno-strict-aliasing' at the project level.
40+
41+
- The '-Wl,-s' parameter strips debug information from the generated shared
42+
library. Similarly, '-DNDEBUG' and '-DNB_COMPACT_ASSERTIONS' are parameters
43+
that improve performance and reduce binary size in release builds, but
44+
which would be omitted in debug builds.
45+
46+
- here, the libnanobind part uses -O3 (optimization for highest peformance),
47+
while the bindings use -Os (optimization for the smallest size). The
48+
assumption here is that the code in 'my_ext.cpp' is glue code that isn't
49+
performance sensitive but which can become very big in a size-optimized
50+
build. I generally this to be a good default, but you may have other
51+
preferences. If in doubt, benchmark to see what works best.
52+
53+
- The suffix of the final shared library depends on the Python version and
54+
platform. You can ask a specific Python build about the right extension via
55+
"import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"
56+
57+
- Some of the above details change if you want to create a limited API /
58+
stable ABI build, which is possible starting with Python 3.12. See the
59+
CMake build system for reference.
60+
*/
61+
62+
#include "nb_internals.cpp"
63+
#include "nb_func.cpp"
64+
#include "nb_type.cpp"
65+
#include "nb_enum.cpp"
66+
#include "nb_ndarray.cpp"
67+
#include "nb_static_property.cpp"
68+
#include "error.cpp"
69+
#include "common.cpp"
70+
#include "implicit.cpp"
71+
#include "trampoline.cpp"

0 commit comments

Comments
 (0)