Philip Howard | 567ee00 | 2013-03-27 22:22:00 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Mark Liffiton | 37ca970 | 2017-08-13 12:52:10 -0500 | [diff] [blame] | 3 | import os |
| 4 | import sys |
| 5 | |
Mark Liffiton | 3644907 | 2017-08-12 16:47:21 -0500 | [diff] [blame] | 6 | from setuptools import setup, Extension |
Mark Liffiton | f43bfce | 2017-08-12 23:34:01 -0500 | [diff] [blame] | 7 | from setuptools.command.build_py import build_py |
Mark Liffiton | 37ca970 | 2017-08-13 12:52:10 -0500 | [diff] [blame] | 8 | from setuptools.command.sdist import sdist |
| 9 | from distutils.spawn import find_executable |
Phil Howard | 60cc642 | 2015-03-11 12:20:54 +0000 | [diff] [blame] | 10 | from glob import glob |
Philip Howard | 567ee00 | 2013-03-27 22:22:00 +0000 | [diff] [blame] | 11 | |
Phil Howard | 3f99ba1 | 2016-02-29 12:12:28 +0000 | [diff] [blame] | 12 | sources = glob('WiringPi/devLib/*.c') |
| 13 | sources += glob('WiringPi/wiringPi/*.c') |
Joshua Yang | 8d7c223 | 2018-02-22 13:52:33 +0900 | [diff] [blame] | 14 | |
Joshua Yang | 1831d40 | 2018-11-29 16:03:23 +0900 | [diff] [blame] | 15 | # Exclude rht03. |
| 16 | sources = list(set(sources) - set(glob('WiringPi/wiringPi/rht03.c'))) |
Joshua Yang | 8d7c223 | 2018-02-22 13:52:33 +0900 | [diff] [blame] | 17 | # Exclude template file. |
| 18 | sources = list(set(sources) - set(glob('WiringPi/wiringPi/odroid_template.c'))) |
| 19 | |
Mark Liffiton | 37ca970 | 2017-08-13 12:52:10 -0500 | [diff] [blame] | 20 | # If we have swig, use it. Otherwise, use the pre-generated |
| 21 | # wrapper from the source distribution. |
| 22 | if find_executable('swig'): |
| 23 | sources += ['wiringpi.i'] |
| 24 | elif os.path.exists('wiringpi_wrap.c'): |
| 25 | sources += ['wiringpi_wrap.c'] |
| 26 | else: |
| 27 | print("Error: Building this module requires either that swig is installed\n" |
| 28 | " (e.g., 'sudo apt install swig') or that wiringpi_wrap.c from the\n" |
| 29 | " source distribution (on pypi) is available.") |
| 30 | sys.exit(1) |
Phil Howard | 3f99ba1 | 2016-02-29 12:12:28 +0000 | [diff] [blame] | 31 | |
Mark Liffiton | 3644907 | 2017-08-12 16:47:21 -0500 | [diff] [blame] | 32 | try: |
| 33 | sources.remove('WiringPi/devLib/piFaceOld.c') |
| 34 | except ValueError: |
| 35 | # the file is already excluded in the source distribution |
| 36 | pass |
Phil Howard | 3f99ba1 | 2016-02-29 12:12:28 +0000 | [diff] [blame] | 37 | |
Mark Liffiton | f43bfce | 2017-08-12 23:34:01 -0500 | [diff] [blame] | 38 | |
| 39 | # Fix so that build_ext runs before build_py |
| 40 | # Without this, wiringpi.py is generated too late and doesn't |
| 41 | # end up in the distribution when running setup.py bdist or bdist_wheel. |
| 42 | # Based on: |
| 43 | # https://stackoverflow.com/a/29551581/7938656 |
| 44 | # and |
| 45 | # https://blog.niteoweb.com/setuptools-run-custom-code-in-setup-py/ |
Mark Liffiton | 37ca970 | 2017-08-13 12:52:10 -0500 | [diff] [blame] | 46 | class build_py_ext_first(build_py): |
Mark Liffiton | f43bfce | 2017-08-12 23:34:01 -0500 | [diff] [blame] | 47 | def run(self): |
| 48 | self.run_command("build_ext") |
| 49 | return build_py.run(self) |
| 50 | |
| 51 | |
Mark Liffiton | 37ca970 | 2017-08-13 12:52:10 -0500 | [diff] [blame] | 52 | # Make sure wiringpi_wrap.c is available for the source dist, also. |
| 53 | class sdist_ext_first(sdist): |
| 54 | def run(self): |
| 55 | self.run_command("build_ext") |
| 56 | return sdist.run(self) |
| 57 | |
| 58 | |
Phil Howard | 77ce6cd | 2016-03-09 11:49:38 +0000 | [diff] [blame] | 59 | _wiringpi = Extension( |
| 60 | '_wiringpi', |
Phil Howard | 2204176 | 2014-07-18 11:48:01 +0000 | [diff] [blame] | 61 | include_dirs=['WiringPi/wiringPi','WiringPi/devLib'], |
neuralassembly | 91b71d7 | 2017-03-29 17:25:14 +0900 | [diff] [blame] | 62 | sources=sources, |
Mark Liffiton | 8600c39 | 2018-04-29 09:12:55 -0500 | [diff] [blame^] | 63 | swig_opts=['-threads'], |
neuralassembly | 91b71d7 | 2017-03-29 17:25:14 +0900 | [diff] [blame] | 64 | extra_link_args=['-lcrypt', '-lrt'] |
Philip Howard | 567ee00 | 2013-03-27 22:22:00 +0000 | [diff] [blame] | 65 | ) |
| 66 | |
| 67 | setup( |
Phil Howard | 77ce6cd | 2016-03-09 11:49:38 +0000 | [diff] [blame] | 68 | name = 'wiringpi', |
Mark Liffiton | 8600c39 | 2018-04-29 09:12:55 -0500 | [diff] [blame^] | 69 | version = '2.44.5', |
Phil Howard | 77ce6cd | 2016-03-09 11:49:38 +0000 | [diff] [blame] | 70 | ext_modules = [ _wiringpi ], |
| 71 | py_modules = ["wiringpi"], |
Philip Howard | 567ee00 | 2013-03-27 22:22:00 +0000 | [diff] [blame] | 72 | install_requires=[], |
Mark Liffiton | 37ca970 | 2017-08-13 12:52:10 -0500 | [diff] [blame] | 73 | cmdclass = {'build_py' : build_py_ext_first, 'sdist' : sdist_ext_first}, |
Philip Howard | 567ee00 | 2013-03-27 22:22:00 +0000 | [diff] [blame] | 74 | ) |