forked from tulip-control/dd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.py
235 lines (211 loc) · 6.63 KB
/
download.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
"""Retrieve and build dependencies of C extensions."""
import ctypes
import hashlib
import os
import shutil
import subprocess
import sys
import tarfile
try:
import urllib2
except ImportError:
import urllib.request, urllib.error, urllib.parse
urllib2 = urllib.request
try:
import cysignals
except ImportError:
cysignals = None
try:
from Cython.Build import cythonize
pyx = '.pyx'
except ImportError:
print('`import cython` failed')
pyx = '.c'
from setuptools.extension import Extension
EXTENSIONS = ['cudd', 'cudd_zdd', 'buddy', 'sylvan']
# CUDD
CUDD_VERSION = '3.0.0'
CUDD_TARBALL = 'cudd-{v}.tar.gz'.format(v=CUDD_VERSION)
CUDD_URL = (
'https://sourceforge.net/projects/cudd-mirror/files/'
'cudd-{v}.tar.gz/download').format(v=CUDD_VERSION)
CUDD_SHA256 = (
'b8e966b4562c96a03e7fbea239729587'
'd7b395d53cadcc39a7203b49cf7eeb69')
CC = 'gcc'
FILE_PATH = os.path.dirname(os.path.realpath(__file__))
CUDD_PATH = os.path.join(
FILE_PATH,
'cudd-{v}'.format(v=CUDD_VERSION))
CUDD_DIRS = ['cudd', 'dddmp', 'epd', 'mtr', 'st', 'util']
CUDD_INCLUDE = ['.'] + CUDD_DIRS
CUDD_LINK = ['cudd/.libs', 'dddmp/.libs']
CUDD_LIB = ['cudd', 'dddmp']
CUDD_CFLAGS = [
# '-arch x86_64',
'-fPIC',
'-std=c99',
'-DBSD',
'-DHAVE_IEEE_754',
'-mtune=native', '-pthread', '-fwrapv',
'-fno-strict-aliasing',
'-Wall', '-W', '-O3']
sizeof_long = ctypes.sizeof(ctypes.c_long)
sizeof_void_p = ctypes.sizeof(ctypes.c_void_p)
CUDD_CFLAGS.extend([
'-DSIZEOF_LONG={long}'.format(long=sizeof_long),
'-DSIZEOF_VOID_P={void}'.format(void=sizeof_void_p)])
# add -fPIC
XCFLAGS = (
'XCFLAGS=-fPIC -mtune=native -DHAVE_IEEE_754 -DBSD '
'-DSIZEOF_VOID_P={void} -DSIZEOF_LONG={long}'.format(
long=sizeof_long, void=sizeof_void_p))
# Sylvan
SYLVAN_PATH = os.path.join(
FILE_PATH,
'sylvan')
SYLVAN_INCLUDE = [
[SYLVAN_PATH, 'src'],
[FILE_PATH, 'dd']]
SYLVAN_LINK = [[SYLVAN_PATH, 'src/.libs']]
def extensions(args):
"""Return C extensions, cythonize as needed.
@param args: known args from `argparse.parse_known_args`
"""
directives = dict(
language_level=2,
embedsignature=True)
cudd_cflags = list(CUDD_CFLAGS)
sylvan_cflags = list()
# detect optional `cysignals` (unless dist)
compile_time_env = dict(
USE_CYSIGNALS=not (
cysignals is None or
args.sdist or args.bdist_wheel))
# tell gcc to compile line tracing
if args.linetrace:
print('compile Cython extensions with line tracing')
directives['linetrace'] = True
cudd_cflags.append('-DCYTHON_TRACE=1')
sylvan_cflags.append('-DCYTHON_TRACE=1')
# directives['binding'] = True
os.environ['CC'] = CC
path = args.cudd if args.cudd else CUDD_PATH
cudd_include = [(path, s) for s in CUDD_INCLUDE]
cudd_link = [(path, s) for s in CUDD_LINK]
_copy_cudd_license(args)
_copy_extern_licenses(args)
extensions = dict(
cudd=Extension(
'dd.cudd',
sources=['dd/cudd' + pyx],
include_dirs=_join(cudd_include),
library_dirs=_join(cudd_link),
libraries=CUDD_LIB,
extra_compile_args=cudd_cflags),
cudd_zdd=Extension(
'dd.cudd_zdd',
sources=['dd/cudd_zdd' + pyx],
include_dirs=_join(cudd_include),
library_dirs=_join(cudd_link),
libraries=CUDD_LIB,
extra_compile_args=cudd_cflags),
buddy=Extension(
'dd.buddy',
sources=['dd/buddy' + pyx],
libraries=['bdd']),
sylvan=Extension(
'dd.sylvan',
sources=['dd/sylvan' + pyx],
include_dirs=_join(SYLVAN_INCLUDE),
library_dirs=_join(SYLVAN_LINK),
libraries=['sylvan'],
extra_compile_args=sylvan_cflags))
for ext in EXTENSIONS:
if getattr(args, ext) is None:
extensions.pop(ext)
if pyx == '.pyx':
ext_modules = list()
for k, v in extensions.items():
c = cythonize(
[v],
compiler_directives=directives,
compile_time_env=compile_time_env)
ext_modules.append(c[0])
else:
ext_modules = list(extensions.values())
return ext_modules
def _copy_cudd_license(args):
"""Include CUDD's license in wheels."""
path = args.cudd if args.cudd else CUDD_PATH
license = os.path.join(path, 'LICENSE')
included = os.path.join('dd', 'CUDD_LICENSE')
yes = (
args.bdist_wheel and
getattr(args, 'cudd') is not None)
if yes:
shutil.copyfile(license, included)
elif os.path.isfile(included):
os.remove(included)
def _copy_extern_licenses(args):
"""Include in wheels licenses related to building CUDD.
To fetch the license files, invoke `make download_licenses`.
"""
licenses = [
'GLIBC_COPYING.LIB',
'GLIBC_LICENSES',
'PYTHON_LICENSE']
path = os.path.join(FILE_PATH, 'extern')
yes = (
args.bdist_wheel and
getattr(args, 'cudd') is not None)
for name in licenses:
license = os.path.join(path, name)
included = os.path.join('dd', name)
if yes:
shutil.copyfile(license, included)
elif os.path.isfile(included):
os.remove(included)
def _join(paths):
"""Return `list` of paths, after joining each.
@param paths: container of pieces of paths,
each path is obtained by joining its pieces
using `os.path.join`
@type paths: `list` of `list` of `str`
@return: `list` of paths
@rtype: `list` of `str`
"""
return [os.path.join(*x) for x in paths]
def fetch(url, sha256, fname=None):
print('++ download: {url}'.format(url=url))
u = urllib2.urlopen(url)
if fname is None:
fname = CUDD_TARBALL
with open(fname, 'wb') as f:
f.write(u.read())
with open(fname, 'rb') as f:
s = f.read()
h = hashlib.sha256(s)
x = h.hexdigest()
assert x == sha256, (x, sha256)
print('-- done downloading.')
return fname
def untar(fname):
"""Extract contents of tar file `fname`."""
print('++ unpack: {f}'.format(f=fname))
with tarfile.open(fname) as tar:
tar.extractall()
print('-- done unpacking.')
def make_cudd():
"""Compile CUDD."""
path = CUDD_PATH
cmd = ["./configure", "CFLAGS=-fPIC -std=c99"]
subprocess.call(cmd, cwd=path)
subprocess.call(['make', '-j4'], cwd=path)
def fetch_cudd():
"""Retrieve, unpack, patch, and compile CUDD."""
fname = fetch(CUDD_URL, CUDD_SHA256)
untar(fname)
make_cudd()
if __name__ == '__main__':
fetch_cudd()