|
| 1 | +.. SPDX-FileCopyrightText: 2023 The meson-python developers |
| 2 | +.. |
| 3 | +.. SPDX-License-Identifier: MIT |
| 4 | +
|
| 5 | +.. _how-to-guides-debug-builds: |
| 6 | + |
| 7 | +****************** |
| 8 | +Using debug builds |
| 9 | +****************** |
| 10 | + |
| 11 | +For development work on native code in your Python package, you may want to use |
| 12 | +a debug build. To do so, we need to use the ``buildtype`` option, which is |
| 13 | +equivalent to ``-Ddebug=true -Doptimization=0``, to ``meson setup``. In addition, |
| 14 | +it is likely most useful to set up an editable build with a fixed build |
| 15 | +directory. That way, the shared libraries in the installed debug build will |
| 16 | +contain correct paths, rather than paths pointing to a temporary build |
| 17 | +directory which ``meson-python`` will otherwise use. IDEs and other tools will |
| 18 | +then be able to show correct file locations and line numbers during debugging. |
| 19 | + |
| 20 | +We can do all that with the following ``pip`` invocation: |
| 21 | + |
| 22 | +.. code-block:: console |
| 23 | +
|
| 24 | + $ pip install -e . --no-build-isolation -Csetup-args=-Dbuildtype=debug -Cbuilddir=build-dbg |
| 25 | +
|
| 26 | +This debug build of your package will work with either a regular or debug build |
| 27 | +of your Python interpreter. A debug Python interpreter isn't necessary, but may |
| 28 | +be useful. It can be built from source, or you may be able to get it from your |
| 29 | +package manager of choice (typically under a package name like ``python-dbg``). |
| 30 | +Note that using a debug Python interpreter does not yield a debug build of your |
| 31 | +own package - you must use ``-Dbuildtype=debug`` or an equivalent flag for that. |
| 32 | + |
| 33 | +.. warning:: |
| 34 | + |
| 35 | + Inside a Conda environment, environment variables like ``CFLAGS`` and |
| 36 | + ``CXXFLAGS`` are usually set when the environment is activated. These |
| 37 | + environment variables contain optimization flags like ``-O2``, which will |
| 38 | + override the optimization level implied by ``-Dbuildtype=debug``. In order |
| 39 | + to avoid this, unset these variables: |
| 40 | + |
| 41 | + .. code-block:: console |
| 42 | +
|
| 43 | + $ unset CFLAGS |
| 44 | + $ unset CXXFLAGS |
| 45 | +
|
| 46 | +Finally, note changing the buildtype from its default value of ``release`` to |
| 47 | +``debug`` will also cause ``meson-python`` to enable (or better, not disable) |
| 48 | +assertions by not defining the ``NDEBUG`` macro (see ``b_ndebug`` under |
| 49 | +:ref:`explanations-default-options`). |
0 commit comments