8

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

setup(name="myPy",
      ext_modules = [
         Extension("MyPyExt", ["MyPyExt.cpp"],
                   libraries = ["boost_python"])
      ])
Jason Mock
  • 823
  • 1
  • 10
  • 19
  • 1
    You can add `extra_link_args=['-Wl,--strip-all']` to the `Extension`. Not portable, and, strangely, MinGW64 on Windows strips the resulting module by default, but gcc on Linux does not. – eudoxos Nov 01 '12 at 06:17
  • 1
    Oops, sorry, MinGW does not include it by default either, it was only specified in my `setup.py`. – eudoxos Nov 01 '12 at 06:24
  • OK. I'll give that a try. Over the weekend I looked through the distutils package some, but I haven't been able to narrow in on where that's being defaulted. From everything I'm seeing, it doesn't look like it should be. – Jason Mock Nov 06 '12 at 16:14
  • What is not being defaulted? if you add `extra_link_flags` as I wrote above, you will have symbols stripped. – eudoxos Nov 06 '12 at 19:08
  • 1
    What's being defaulted is the use of '-g' on the calls to gcc to build debug, without me passing --debug or -g to 'python setup.py build/install'. – Jason Mock Nov 06 '12 at 20:32
  • 1
    There is a relevant comment in the source here: http://hg.python.org/cpython/file/859ef54bdce2/Lib/distutils/unixccompiler.py#l29 – eudoxos Nov 07 '12 at 08:32

3 Answers3

10

You can use the option extra_compile_args to pass arguments to the compiler which are appended last and thus have highest priority. Thus, if you include -g0 (no debug symbols) in extra_compile_args, this overrides the -g set by Distutils/Setuptools. In your example:

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

setup(
    name="myPy",
    ext_modules = [Extension(
        "MyPyExt",
        ["MyPyExt.cpp"],
        libraries = ["boost_python"],
        extra_compile_args = ["-g0"]
        )]
    )

See also: How may I override the compiler (gcc) flags that setup.py uses by default?

Community
  • 1
  • 1
Wrzlprmft
  • 4,234
  • 1
  • 28
  • 54
5

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 ', ' ')

setup(...)
hithwen
  • 2,154
  • 28
  • 46
  • Considering the things I've seen done with iMake and cmake, that isn't really that bad to me. I'm going to assume that it works since I don't have access to the original code where I was trying to do this anymore. – Jason Mock Feb 13 '14 at 15:28
  • Thank you @hithwen, it works well with python3.5, installed by MacPorts, running on MacOS 10.13.4. – Tora May 02 '18 at 13:58
0

@hithwen's solution works well with python3.5, installed by MacPorts, running on MacOS 10.13.4.

However, platform.system() returns 'Darwin', not 'Linux' in my case.
On Ubuntu 16.04, -g seems to appear at the beginning or at the end. So a little bit tweaked:

import sys
import re
from distutils import sysconfig
from distutils.core import setup, Extension

if sys.platform == 'linux' or sys.platform == 'darwin':
  sysconfig.get_config_var(None)  # to fill up _config_vars
  d = sysconfig._config_vars
  for x in ['OPT', 'CFLAGS', 'PY_CFLAGS', 'PY_CORE_CFLAGS', 'CONFIGURE_CFLAGS', 'LDSHARED']:
    d[x] = re.sub(' -g ', ' ', d[x])
    d[x] = re.sub('^-g ', '',  d[x])
    d[x] = re.sub(' -g$', '',  d[x])

setup(...)

Thank you @hithwen!

Tora
  • 970
  • 1
  • 8
  • 15