|
| 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