forked from Xilinx/PYNQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
361 lines (301 loc) · 12.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Copyright (c) 2016, Xilinx, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
__author__ = "Giuseppe Natale, Yun Rock Qu"
__copyright__ = "Copyright 2016, Xilinx"
__email__ = "[email protected]"
from setuptools import setup, Extension, find_packages
from distutils.dir_util import copy_tree
import glob
import re
import shutil
import subprocess
import sys
import os
import warnings
from datetime import datetime
# Parse version number
def find_version(file_path):
with open(file_path, 'r') as fp:
version_file = fp.read()
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise NameError("Version string must be defined in {}.".format(file_path))
# Board specific package delivery setup
def exclude_from_files(exclude, path):
return [file for file in os.listdir(path)
if os.path.isfile(os.path.join(path, file))
and file != exclude]
def find_overlays(path):
return [f for f in os.listdir(path)
if os.path.isdir(os.path.join(path, f))
and len(glob.glob(os.path.join(path, f, "*.bit"))) > 0]
def collect_pynq_overlays():
overlay_files = []
overlay_dirs = find_overlays(board_folder)
for ol in overlay_dirs:
copy_tree(os.path.join(board_folder, ol),
os.path.join("pynq/overlays", ol))
newdir = os.path.join("pynq/overlays", ol)
files = exclude_from_files('makefile', newdir)
overlay_files.extend(
[os.path.join("..", newdir, f) for f in files])
return overlay_files
pynq_package_files = []
if 'BOARD' not in os.environ:
warnings.warn("BOARD variable must be set to get board specific overlays.",
UserWarning)
board = None
board_folder = None
else:
board = os.environ['BOARD']
board_folder = 'boards/{}/'.format(board)
if not os.path.isdir(board_folder):
warnings.warn("Could not find board folder {}".format(board),
UserWarning)
board_folder = None
else:
pynq_package_files.extend(collect_pynq_overlays())
# Extend data_files with Microblaze C BSPs and libraries
microblaze_data_dirs = ['pynq/lib/pynqmicroblaze/modules',
'pynq/lib/arduino/bsp_iop_arduino',
'pynq/lib/pmod/bsp_iop_pmod',
'pynq/lib/rpi/bsp_iop_rpi']
for mbdir in microblaze_data_dirs:
pynq_package_files.extend(
[os.path.join("..", root, f)
for root, _, files in os.walk(mbdir) for f in files]
)
# Device family constants
ZYNQ_ARCH = "armv7l"
ZU_ARCH = "aarch64"
if 'PYNQ_BUILD_ARCH' in os.environ:
CPU_ARCH = os.environ['PYNQ_BUILD_ARCH']
else:
CPU_ARCH = os.uname().machine
CPU_ARCH_IS_SUPPORTED = CPU_ARCH in [ZYNQ_ARCH, ZU_ARCH]
# Notebook delivery
default_nb_dir = '/home/xilinx/jupyter_notebooks'
if 'PYNQ_JUPYTER_NOTEBOOKS' in os.environ:
notebooks_dir = os.environ['PYNQ_JUPYTER_NOTEBOOKS']
elif os.path.exists(default_nb_dir):
notebooks_dir = default_nb_dir
else:
notebooks_dir = None
if notebooks_dir is not None:
notebooks_getting_started_dst_dir = os.path.join(notebooks_dir, 'getting_started')
notebooks_getting_started_dst_img_dir = os.path.join(notebooks_getting_started_dst_dir, 'images')
notebooks_getting_started_src_dir = os.path.join('docs', 'source')
notebooks_getting_started_src_img_dir = os.path.join(notebooks_getting_started_src_dir, 'images')
else:
notebooks_getting_started_dst_dir = None
notebooks_getting_started_dst_img_dir = None
notebooks_getting_started_src_dir = None
notebooks_getting_started_src_img_dir = None
# Video source files
_video_src = ['pynq/lib/_pynq/_video/_video.c',
'pynq/lib/_pynq/_video/_capture.c',
'pynq/lib/_pynq/_video/_display.c',
'pynq/lib/_pynq/_video/py_xvtc.c',
'pynq/lib/_pynq/_video/utils.c',
'pynq/lib/_pynq/_video/py_xgpio.c',
'pynq/lib/_pynq/_video/video_capture.c',
'pynq/lib/_pynq/_video/video_display.c']
_video_gpio = \
['pynq/lib/_pynq/_video/bsp/gpio/xgpio.c',
'pynq/lib/_pynq/_video/bsp/gpio/xgpio_extra.c',
'pynq/lib/_pynq/_video/bsp/gpio/xgpio_intr.c',
'pynq/lib/_pynq/_video/bsp/gpio/xgpio_selftest.c']
_video_vtc = \
['pynq/lib/_pynq/_video/bsp/vtc/xvtc.c',
'pynq/lib/_pynq/_video/bsp/vtc/xvtc_intr.c',
'pynq/lib/_pynq/_video/bsp/vtc/xvtc_selftest.c']
_common_src = \
['pynq/lib/_pynq/common/xil_stubs.c']
_bsp_includes = \
['pynq/lib/_pynq/embeddedsw/lib/bsp/standalone/src/common',
'pynq/lib/_pynq/embeddedsw/lib/bsp/standalone/src/arm/common',
'pynq/lib/_pynq/embeddedsw/lib/bsp/standalone/src/arm/common/gcc']
if CPU_ARCH == ZYNQ_ARCH:
_bsp_includes.append(
'pynq/lib/_pynq/embeddedsw/lib/bsp/standalone/src/arm/cortexa9')
elif CPU_ARCH == ZU_ARCH:
_bsp_includes.append(
'pynq/lib/_pynq/embeddedsw/lib/bsp/standalone/src/arm/cortexa53/64bit')
getting_started_notebooks = \
['jupyter_notebooks.ipynb',
'python_environment.ipynb',
'jupyter_notebooks_advanced_features.ipynb']
# Merge BSP src to _video src
video = []
video.extend(_video_gpio)
video.extend(_video_vtc)
video.extend(_video_src)
video.extend(_common_src)
# Copy notebooks in pynq/notebooks
def copy_common_notebooks():
if notebooks_dir is None:
return None
common_folders_files = [f for f in os.listdir('pynq/notebooks/')]
for basename in common_folders_files:
dst_folder_file = os.path.join(notebooks_dir, basename)
src_folder_file = os.path.join('pynq/notebooks/', basename)
if os.path.isdir(dst_folder_file):
shutil.rmtree(dst_folder_file)
elif os.path.isfile(dst_folder_file):
os.remove(dst_folder_file)
if os.path.isdir(src_folder_file):
copy_tree(src_folder_file, dst_folder_file)
elif os.path.isfile(src_folder_file):
shutil.copy(src_folder_file, dst_folder_file)
# Copy notebooks in boards/BOARD/notebooks
def copy_board_notebooks():
if notebooks_dir is None or board_folder is None:
return None
src_folder = os.path.join(board_folder, 'notebooks')
dst_folder = notebooks_dir
if os.path.isdir(src_folder):
copy_tree(src_folder, dst_folder)
# Copy notebooks in boards/BOARD/OVERLAY/notebooks
def copy_overlay_notebooks():
if notebooks_dir is None or board_folder is None:
return None
if os.path.isdir(board_folder):
overlay_notebook_folders = [
(os.path.join(notebooks_dir, overlay),
os.path.join(board_folder, overlay, 'notebooks/'))
for overlay in list(os.listdir(board_folder))
if os.path.isdir(os.path.join(board_folder, overlay, 'notebooks'))]
for dst_folder, src_folder in overlay_notebook_folders:
if os.path.exists(dst_folder):
shutil.rmtree(dst_folder)
copy_tree(src_folder, dst_folder)
# Copy documentation files in docs/source and docs/source/images
def copy_documentation_files():
if notebooks_dir is None:
return None
doc_files = list()
doc_files.append((notebooks_getting_started_dst_dir,
[os.path.join(notebooks_getting_started_src_dir, nb)
for nb in getting_started_notebooks]))
doc_files.extend([(notebooks_getting_started_dst_img_dir,
[os.path.join(root, f) for f in files])
for root, dirs, files in os.walk(
notebooks_getting_started_src_img_dir)])
if not os.path.exists(notebooks_getting_started_dst_img_dir):
os.makedirs(notebooks_getting_started_dst_img_dir)
for dst, files in doc_files:
for f in files:
shutil.copy(f, dst)
# Rename and copy getting started notebooks
def rename_notebooks():
if notebooks_dir is None:
return None
for ix, getting_started_nb in enumerate(getting_started_notebooks):
new_nb_name = '{}_{}'.format(ix + 1, getting_started_nb)
src_file = os.path.join(notebooks_getting_started_dst_dir,
getting_started_nb)
dst_file = os.path.join(notebooks_getting_started_dst_dir,
new_nb_name)
shutil.move(src_file, dst_file)
# Change ownership of the notebook folder
def change_ownership():
subprocess.run(['chown', '-R', 'xilinx:xilinx', notebooks_dir])
# Backup notebooks
def backup_notebooks():
if notebooks_dir is None:
return None
notebooks_dir_backup = '{}_{}'.format(notebooks_dir,
datetime.now().strftime(
"%Y_%m_%d_%H_%M_%S"))
copy_tree(notebooks_dir, notebooks_dir_backup)
return notebooks_dir_backup
# Run Makefiles here
def run_make(src_path, dst_path, output_lib):
status = subprocess.check_call(["make", "-C", src_path])
if status != 0:
raise RuntimeError("Error while running make for {}".format(output_lib))
shutil.copyfile(src_path + output_lib, dst_path + output_lib)
if CPU_ARCH_IS_SUPPORTED:
if CPU_ARCH == ZYNQ_ARCH:
run_make("pynq/lib/_pynq/_audio/", "pynq/lib/",
"libaudio.so")
run_make("pynq/lib/_pynq/_xiic/", "pynq/lib/",
"libiic.so")
elif CPU_ARCH == ZU_ARCH:
run_make("pynq/lib/_pynq/_displayport/", "pynq/lib/video/",
"libdisplayport.so")
run_make("pynq/lib/_pynq/_xhdmi/", "pynq/lib/video/",
"libxhdmi.so")
run_make("pynq/lib/_pynq/_xiic/", "pynq/lib/",
"libiic.so")
if notebooks_dir:
backup_notebooks()
copy_common_notebooks()
copy_board_notebooks()
copy_overlay_notebooks()
copy_documentation_files()
rename_notebooks()
change_ownership()
if CPU_ARCH == ZYNQ_ARCH:
ext_modules = [
Extension('pynq.lib._video', video,
include_dirs=['pynq/lib/_pynq/_video',
'pynq/lib/_pynq/_video/bsp/vtc',
'pynq/lib/_pynq/_video/bsp/gpio',
'pynq/lib/_pynq/common/armv7l'] + _bsp_includes
),
]
else:
ext_modules = []
pynq_version = find_version('pynq/__init__.py')
pynq_package_files.extend(['tests/*', 'js/*', '*.bin', '*.so', '*.pdm'])
setup(name='pynq',
version=pynq_version,
description='(PY)thon productivity for zy(NQ)',
author='Xilinx PYNQ Development Team',
author_email='[email protected]',
url='https://github.com/Xilinx/PYNQ',
packages=find_packages(),
download_url='https://github.com/Xilinx/PYNQ',
package_data={
'': pynq_package_files,
},
entry_points={
'console_scripts': [
'start_pl_server.py = pynq.pl:_start_server',
'stop_pl_server.py = pynq.pl:_stop_server'
]
},
ext_modules=ext_modules
)
if board:
print('Restarting PL server')
subprocess.run(['systemctl', 'restart', 'pl_server'])