How to build cv2 in debug non-optimized mode?

  • debian 10
  • gcc/g++ version 8.3.0 (Debian 8.3.0-6)
  • Python 3.9.0

I want to build the final cv2 shared library (e.g. cv2.cpython-39-x86_64-linux-gnu.so) with debug symbols and without optimizations. I have tried various strategies like editing the opencv/CMakeLists.txt as well as running a build with something like this:

export ENABLE_HEADLESS=1
export CMAKE_ARGS='-DCMAKE_BUILD_TYPE=DEBUG -DCMAKE_C_FLAGS_DEBUG="-g -O0" -DCMAKE_CXX_FLAGS_DEBUG="-g -O0"'
TMPDIR=$PWD/build pip wheel --no-clean --verbose .

No matter what I do, the final library seems to be built in Release mode and ignores my optimizations. How do I do this?