[Distutils] Compile C code and install to script dir?

Floris Bruynooghe floris.bruynooghe at gmail.com
Sat Sep 12 11:45:17 CEST 2009


On Fri, Sep 11, 2009 at 01:15:07PM -0700, OB wrote:
> I am trying to get distutils to compile a single C script and install the
> binary to the same script directory as the entry point scripts that are
> generated for the python part of the module. I would like to use all the
> includes, CFLAGS, etc that are used when compiling extension modules, but
> this C script is not an extension. Is there a way to do this through setup,
> or do I need to use distutils.ccompiler.new_compiler() and try to hunt down
> CFLAGS, default libs, etc?

(Ignoring the setuptools part of your question as I don't use that,
but I'm pretty sure it's very similar if not the same)

If you don't want to create the compiler the easiest is to extend the
build_ext command:

"""
from distutils.command.build_ext import build_ext

class MyBuildExt(build_ext):
    def run(self):
        build_ext.run(self)
        self.compiler.link_executable(['hello.c'], 'hello')

setup(name='foo',
      ...
      cmdclass = {'build_ext': MyBuildExt})
"""

Here build_ext.run() will be setting all the include dirs, macros,
libs, rpaths etc into the compiler, which is what you want I think.
Obviously play around with the arguments to link_executable(), or you
could do it in 2 steps and create the object files first with
self.compiler.compile() if you want/need.

If you don't want to extend build_ext then you've got to create the
compiler with distutils.ccompiler.new_compiler() and
distutils.sysconfig.customize_compiler().  Hunting down the settings
is a bit harder then, you'll be reading build_ext.finalize_options()
and build_ext.run() to figure this out.

Regards
Floris

-- 
Debian GNU/Linux -- The Power of Freedom
www.debian.org | www.gnu.org | www.kernel.org


More information about the Distutils-SIG mailing list