forked from openwsn-berkeley/openwsn-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
183 lines (146 loc) · 6.19 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import glob
import logging
import os
import platform
import shutil
import subprocess
import sys
import setuptools
from setuptools import Extension, Command
from setuptools.command.build_ext import build_ext
try:
import colorama as c
colors = True
c.init()
except ImportError:
colors = False
here = os.path.abspath('.')
# Filename for the C extension module library
c_module_name = 'openmote'
enable_configure = False
if sys.version_info.major < 3:
raise Exception("Must be using Python 3.6 or higher")
if sys.version_info.minor < 6:
raise Exception("Must be using Python 3.6 or higher")
# Command line flags forwarded to CMake (for debug purpose)
cmake_cmd_args = []
for f in sys.argv:
if f.startswith('-D'):
cmake_cmd_args.append(f)
elif f.startswith('--configure'):
enable_configure = True
sys.argv.remove(f)
for f in cmake_cmd_args:
sys.argv.remove(f)
def _get_env_variable(name, default='OFF'):
if name not in os.environ.keys():
return default
return os.environ[name]
class CMakeExtension(Extension):
def __init__(self, name, cmake_lists_dir='.', sources=None, **kwa):
if sources is None:
_sources = []
else:
_sources = sources
Extension.__init__(self, name, sources=_sources, **kwa)
self.cmake_lists_dir = os.path.abspath(cmake_lists_dir)
class CMakeBuild(build_ext):
def build_extensions(self):
# Ensure that CMake is present and working
try:
out = subprocess.check_output(['cmake', '--version'])
logging.info(out)
except OSError:
raise RuntimeError('Cannot find CMake executable')
for ext in self.extensions:
ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cfg = 'Debug'
cmake_args = [
'-DCMAKE_BUILD_TYPE=%s' % cfg,
# Ask CMake to place the resulting library in the directory containing the extension
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), ext_dir),
# Other intermediate static libraries are placed in a temporary build directory instead
'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), self.build_temp),
]
# We can handle some platform-specific settings at our discretion
if platform.system() == 'Windows':
plat = ('x64' if platform.architecture()[0] == '64bit' else 'Win32')
# When we use CMAKE_BUILD_TYPE=DEBUG we need python3x_d.lib libraries for linking, but Windows often
# doesn't include those debug libraries. To prevent issues, force build type to Release
cfg = 'Release'
cmake_args += [
# These options are likely to be needed under Windows
'-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE',
'-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), ext_dir),
]
# Assuming that Visual Studio and MinGW are supported compilers
if self.compiler.compiler_type == 'msvc':
cmake_args += [
'-DCMAKE_GENERATOR_PLATFORM=%s' % plat,
]
else:
cmake_args += [
'-G', 'MinGW Makefiles',
]
cmake_args += cmake_cmd_args
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# Config
subprocess.check_call(
['cmake', ext.cmake_lists_dir, '-DBOARD:STRING=python',
'-DPROJECT:STRING=oos_openwsn'] + cmake_args,
cwd=self.build_temp)
if enable_configure:
if platform.system() == 'Windows':
subprocess.check_call(['cmake-gui', ext.cmake_lists_dir], cwd=self.build_temp)
else:
subprocess.check_call(['ccmake', ext.cmake_lists_dir], cwd=self.build_temp)
# Build
try:
subprocess.check_call(['cmake', '--build', '.', '--config', cfg], cwd=self.build_temp)
except subprocess.CalledProcessError:
if colors:
raise Exception(c.Fore.RED + "Build process failed" + c.Fore.RESET)
else:
raise Exception("Build process failed")
else:
if colors:
print(c.Fore.GREEN + "Build process succeeded" + c.Fore.RESET)
else:
print("Build process succeeded")
class CleanCommand(Command):
"""Custom clean command to tidy up the project root."""
CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info'.split(' ')
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
global here
for path_spec in self.CLEAN_FILES:
# Make paths absolute and relative to this path
abs_paths = glob.glob(os.path.normpath(os.path.join(here, path_spec)))
for path in [str(p) for p in abs_paths]:
if not path.startswith(here):
# Die if path in CLEAN_FILES is absolute + outside this directory
raise ValueError("%s is not a path inside %s" % (path, here))
print('removing %s' % os.path.relpath(path))
shutil.rmtree(path)
setuptools.setup(name='openmote',
version=1.0,
python_requires='>=3.6',
description='An instance of an OpenWSN mote',
author='Timothy Claeys',
author_email='[email protected]',
ext_modules=[CMakeExtension(c_module_name)],
cmdclass={
'build_ext': CMakeBuild,
'clean': CleanCommand
},
classifiers=[
"Programming Language :: Python :: 3.6+",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
zip_safe=False)