blob: e5fb541ca10d3df47451f64ae682a8855e33c5d8 [file] [log] [blame]
Philip Howard567ee002013-03-27 22:22:00 +00001#!/usr/bin/env python
2
Mark Liffiton37ca9702017-08-13 12:52:10 -05003import os
4import sys
5
Mark Liffiton36449072017-08-12 16:47:21 -05006from setuptools import setup, Extension
Mark Liffitonf43bfce2017-08-12 23:34:01 -05007from setuptools.command.build_py import build_py
Mark Liffiton37ca9702017-08-13 12:52:10 -05008from setuptools.command.sdist import sdist
9from distutils.spawn import find_executable
Phil Howard60cc6422015-03-11 12:20:54 +000010from glob import glob
Philip Howard567ee002013-03-27 22:22:00 +000011
Phil Howard3f99ba12016-02-29 12:12:28 +000012sources = glob('WiringPi/devLib/*.c')
13sources += glob('WiringPi/wiringPi/*.c')
Joshua Yang8d7c2232018-02-22 13:52:33 +090014
15# Exclude wiringPi.c. This setup is only for ODROID WiringPi.
16sources = list(set(sources) - set(glob('WiringPi/wiringPi/wiringPi.c')))
Joshua Yang1831d402018-11-29 16:03:23 +090017# Exclude original SPI and I2C files.
18sources = list(set(sources) - set(glob('WiringPi/wiringPi/wiringPiSPI.c')))
19sources = list(set(sources) - set(glob('WiringPi/wiringPi/wiringPiI2C.c')))
20# Exclude rht03.
21sources = list(set(sources) - set(glob('WiringPi/wiringPi/rht03.c')))
Joshua Yang8d7c2232018-02-22 13:52:33 +090022# Exclude template file.
23sources = list(set(sources) - set(glob('WiringPi/wiringPi/odroid_template.c')))
24
Mark Liffiton37ca9702017-08-13 12:52:10 -050025# If we have swig, use it. Otherwise, use the pre-generated
26# wrapper from the source distribution.
27if find_executable('swig'):
28 sources += ['wiringpi.i']
29elif os.path.exists('wiringpi_wrap.c'):
30 sources += ['wiringpi_wrap.c']
31else:
32 print("Error: Building this module requires either that swig is installed\n"
33 " (e.g., 'sudo apt install swig') or that wiringpi_wrap.c from the\n"
34 " source distribution (on pypi) is available.")
35 sys.exit(1)
Phil Howard3f99ba12016-02-29 12:12:28 +000036
Mark Liffiton36449072017-08-12 16:47:21 -050037try:
38 sources.remove('WiringPi/devLib/piFaceOld.c')
39except ValueError:
40 # the file is already excluded in the source distribution
41 pass
Phil Howard3f99ba12016-02-29 12:12:28 +000042
Mark Liffitonf43bfce2017-08-12 23:34:01 -050043
44# Fix so that build_ext runs before build_py
45# Without this, wiringpi.py is generated too late and doesn't
46# end up in the distribution when running setup.py bdist or bdist_wheel.
47# Based on:
48# https://stackoverflow.com/a/29551581/7938656
49# and
50# https://blog.niteoweb.com/setuptools-run-custom-code-in-setup-py/
Mark Liffiton37ca9702017-08-13 12:52:10 -050051class build_py_ext_first(build_py):
Mark Liffitonf43bfce2017-08-12 23:34:01 -050052 def run(self):
53 self.run_command("build_ext")
54 return build_py.run(self)
55
56
Mark Liffiton37ca9702017-08-13 12:52:10 -050057# Make sure wiringpi_wrap.c is available for the source dist, also.
58class sdist_ext_first(sdist):
59 def run(self):
60 self.run_command("build_ext")
61 return sdist.run(self)
62
63
Phil Howard77ce6cd2016-03-09 11:49:38 +000064_wiringpi = Extension(
65 '_wiringpi',
Phil Howard22041762014-07-18 11:48:01 +000066 include_dirs=['WiringPi/wiringPi','WiringPi/devLib'],
neuralassembly91b71d72017-03-29 17:25:14 +090067 sources=sources,
68 extra_link_args=['-lcrypt', '-lrt']
Philip Howard567ee002013-03-27 22:22:00 +000069)
70
71setup(
Phil Howard77ce6cd2016-03-09 11:49:38 +000072 name = 'wiringpi',
Mark Liffiton37ca9702017-08-13 12:52:10 -050073 version = '2.44.4',
Phil Howard77ce6cd2016-03-09 11:49:38 +000074 ext_modules = [ _wiringpi ],
75 py_modules = ["wiringpi"],
Philip Howard567ee002013-03-27 22:22:00 +000076 install_requires=[],
Mark Liffiton37ca9702017-08-13 12:52:10 -050077 cmdclass = {'build_py' : build_py_ext_first, 'sdist' : sdist_ext_first},
Philip Howard567ee002013-03-27 22:22:00 +000078)