37

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.

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267

3 Answers3

43

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.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
Walter
  • 7,809
  • 1
  • 30
  • 30
  • 1
    Thanks for the answer. Saves me the pain? Sort of, I've already spent too much time looking for this answer... – Georg Schölly Nov 19 '09 at 15:43
  • 8
    after reading this answer I've implemented something similar and it works quite well (https://github.com/Turbo87/py-xcsoar/blob/master/setup.py). the code runs a Makefile that creates two executables and the modified setup.py then even installs these executables onto the system. same would be possible for installing any kind of library too. – TBieniek Nov 13 '13 at 18:03
  • 1
    Note that this doesn't seem to play well with pip, however if you change `distutils.command.install` to `setuptools.command.install` it does, taken from http://stackoverflow.com/questions/15853058/run-custom-task-when-call-pip-install – wxs Oct 24 '16 at 18:56
  • 2
    Thanks a lot! This is what I needed. I also write a simple setup.py for building the hostapd. hope this is useful for one struggling in this issue. https://github.com/anakin1028/hostapd_binder/blob/master/setup.py – Anakin Tung May 24 '17 at 07:26
2

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

resi
  • 1,738
  • 2
  • 13
  • 14
  • 1
    It's not an extension that I want to build but just a C library that won't get linked with Python. (It's an extension to sqlite.) – Georg Schölly Nov 18 '09 at 20:33
0

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.

merwok
  • 6,779
  • 1
  • 28
  • 42