How can I run a Makefile in setup.py?

Question:

I need to compile ICU using it’s own build mechanism. Therefore the question:

How can I run a Makefile from setup.py? Obviously, I only want it to run during the build process, not while installing.

Asked By: Georg Schölly

||

Answers:

If you are building a python extension you can use the distutils/setuptools Extensions. For example:

from setuptools import Extension
# or:
# from distutils.extension import Extension
setup(...
      ext_modules = [Extension("pkg.icu",
                               ["icu-sqlite/icu.c"]),
                    ]
      )

There are lots of options to build extensions, see the docs: http://docs.python.org/distutils/setupscript.html

Answered By: resi

The method I normally use is to override the command in question:

from distutils.command.install import install as DistutilsInstall

class MyInstall(DistutilsInstall):
    def run(self):
        do_pre_install_stuff()
        DistutilsInstall.run(self)
        do_post_install_stuff()

...

setup(..., cmdclass={'install': MyInstall}, ...)

This took me quite a while to figure out from the distutils documentation and source, so I hope it saves you the pain.

Note: you can also use this cmdclass parameter to add new commands.

Answered By: Walter

It is possible to build C libraries with distutils (see the libraries parameter of distutils.core.setup), but you may have to duplicate options that are already in the Makefile, so the easiest thing to do is probably to extend the install command as explained in other replies and call make with the subprocess module.

Answered By: merwok
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.