Jason Mock Jason Mock - 1 year ago 108
Python Question

Distutils - How to disable including debug symbols when building an extension

I've written a boost-python extension that currently is being built via distutils.

Unfortunately I have been unable to find a way, within distutils, to build the extension without debug symbols or have the symbols stripped from the extension upon installation.

Note: I am not passing --debug or -g to build command of distutils (e.g., python setup.py build), and my compiler is gcc on Linux.

Exampled setup.py:

from distutils.core import setup
from distutils.extension import Extension

ext_modules = [
Extension("MyPyExt", ["MyPyExt.cpp"],
libraries = ["boost_python"])

Answer Source

I've found a way but is a bit hacky:

from distutils import sysconfig
from distutils.core import setup
import platform

if platform.system() != 'Windows':  # When compilinig con visual no -g is added to params
    cflags = sysconfig.get_config_var('CFLAGS')
    opt = sysconfig.get_config_var('OPT')
    sysconfig._config_vars['CFLAGS'] = cflags.replace(' -g ', ' ')
    sysconfig._config_vars['OPT'] = opt.replace(' -g ', ' ')

if platform.system() == 'Linux':  # In macos there seems not to be -g in LDSHARED
    ldshared = sysconfig.get_config_var('LDSHARED')
    sysconfig._config_vars['LDSHARED'] = ldshared.replace(' -g ', ' ')