Search code examples
pythonrpmdistutilspython-extensions

Relative include paths when building an RPM for a python extension using distutils


Using python 2.6, I'm attempting to build an RPM for a python C extension module.

The setup.py that I'm using contains something like:

from distutils.core import setup, Extension

foo_ext = Extension('foo',
                    sources=['foo.c', '../src/common.c'],
                    include_dirs=['../includes'])

setup(... , ext_modules=[foo_ext])

If I run python setup.py build from /devel/foo for example, it compiles and builds without any errors, and gcc is called with the correct paths, i.e.:

gcc ... -I/devel/includes ...

When I use python setup.py bdist_rpm instead, then the relative paths used above are converted to absolute paths relative to the RPM build dir, this results in gcc trying to compile using:

gcc ... -I/devel/foo/build/bdist.linux-x86_64/rpm/BUILD/includes ...

Compilation then fails as a required .h file is not found in the include path.

Any suggestions or workarounds to this?


Solution

  • Solved by setting an environment variable during the first pass through the script, which is then read again when building the RPM:

    import os
    from distutils.core import setup, Extension
    
    src_path = os.path.abspath('../src/common.c')
    inc_path = os.path.abspath('../includes')
    
    if 'SRC_PATH' not in os.environ:
        os.environ['SRC_PATH'] = src_path
        os.environ['INC_PATH'] = inc_path
    else:
        src_path = os.environ['SRC_PATH']
        inc_path = os.environ['INC_PATH']
    
    foo_ext = Extension('foo',
                    sources=['foo.c', src_path],
                    include_dirs=[inc_path])
    
    setup(... , ext_modules=[foo_ext])