views:

40

answers:

1

I have written a Python extension library in C and I am currently using distutils to build it. I also have a Python script that generates a .h file, which I would like to include with my extension.

Is it possible to setup a dependency like this with distutils? Will it be able to notice when my script changes, regenerate the .h file, and recompile the extension?

+1  A: 

You can do this by overrinding build_ext command from distutils.

from distutils.core import setup, Extension
from distutils.command.build_ext import build_ext as _build_ext

module=Extension(....) # The way to build your extension

class build_ext(_build_ext):
    description = "Custom Build Process"

    def initialize_options(self):
        _build_ext.initialize_options(self)
    def finalize_options(self):
        _build_ext.finalize_options(self)

    def run(self):
        # Code to generate your .h
        .....

        # Start classic Extension build
        _build_ext.run(self)

setup(...
      ext_modules = [module],
      cmdclass = { "build_ext": build_ext},
      ...)

So every time your build the extension, the .h is regenerated.

ohe
Thanks! I incorporated distutils.dep_util.newer with your example to get the desired behavior.