How can I run a Makefile in setup.py?
Asked Answered
Z

3

38

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.

Zingaro answered 18/11, 2009 at 10:15 Comment(0)
N
44

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.

Noncommittal answered 19/11, 2009 at 15:2 Comment(4)
Thanks for the answer. Saves me the pain? Sort of, I've already spent too much time looking for this answer...Lamella
after reading this answer I've implemented something similar and it works quite well (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.Refinery
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 #15853558Hypochondria
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. github.com/anakin1028/hostapd_binder/blob/master/setup.pyThallophyte
A
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

Abernathy answered 18/11, 2009 at 16:20 Comment(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.)Lamella
B
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.

Bridgeboard answered 28/10, 2011 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.