-
Notifications
You must be signed in to change notification settings - Fork 53
/
setup.py
467 lines (409 loc) · 14.5 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import os
import sys
VARS = {'$CPYTHON':''}
TOPDIR = os.path.abspath(os.path.dirname(__file__))
TEST = False
CLEAN = False
BOOT = False
DEBUG = False
VALGRIND = False
SANDBOX = False
CORE = ['tokenize','parse','encode','py2bc']
MODULES = []
def main():
chksize()
if len(sys.argv) < 2:
print HELP
return
global TEST,CLEAN,BOOT,DEBUG,VALGRIND,SANDBOX
TEST = 'test' in sys.argv
CLEAN = 'clean' in sys.argv
BOOT = 'boot' in sys.argv
DEBUG = 'debug' in sys.argv
VALGRIND = 'valgrind' in sys.argv
SANDBOX = 'sandbox' in sys.argv
CLEAN = CLEAN or BOOT
TEST = TEST or BOOT
get_libs()
build_mymain()
build = None
if "linux" in sys.platform:
vars_linux()
build = build_gcc
elif "darwin" in sys.platform:
vars_osx()
build = build_gcc
elif "win" in sys.platform:
build = build_vs
#full list of compilers in distutils.ccompiler.show_compilers()
if "-cunix" in sys.argv:
build = build_gcc
elif '-cmsvc' in sys.argv:
build = build_vs
elif '-cmingw32' in sys.argv:
vars_windows()
build = build_gcc
if build == None:
print "couldn't detect OS or incorrect compiler command. defaulting to GCC."
build = build_gcc
cmd = sys.argv[1]
if cmd == "tinypy":
build()
elif cmd == '64k':
build_64k()
elif cmd == 'blob':
build_blob()
elif cmd == "build":
build_blob()
build_cpython()
elif cmd == "install":
install_cpython()
else:
print 'invalid command'
HELP = """
python setup.py command [options] [modules]
Commands:
tinypy - build a vanilla tinypy interpreter binary
64k - generate a 64k version of the tinypy source
blob - generate a single tinypy.c and tinypy.h
build - build CPython module
install - install CPython module
Options:
test - run tests during build
clean - rebuild all .tpc during build
boot - fully bootstrap and test tinypy
debug - build with debug options on
valgrind - run tests through valgrind
-cCOMPILER - build for a specific platform (-cmingw32, -clinux, -cmsvc, -cunix)
sandbox - enable sandbox
Modules:
math - build math module
random - build random module *
pygame - build pygame module **
marshal - build marshal module ***
jit - build jit module ***
re - build re module ***
??? - build other modules in the modules folder
* coming soon!!
** proof-of-concept included
*** vaporware
"""
def vars_osx():
VARS['$RM'] = 'rm -f'
VARS['$VM'] = './vm'
VARS['$TINYPY'] = './tinypy'
VARS['$SYS'] = '-osx'
VARS['$FLAGS'] = ''
VARS['$WFLAGS'] = '-std=c89 -Wall'
if 'pygame' in MODULES:
VARS['$FLAGS'] += ' `sdl-config --cflags --libs` '
def vars_linux():
VARS['$RM'] = 'rm -f'
VARS['$VM'] = './vm'
VARS['$TINYPY'] = './tinypy'
VARS['$SYS'] = '-linux'
VARS['$FLAGS'] = ''
VARS['$WFLAGS'] = '-std=c89 -Wall -Wc++-compat'
#-Wwrite-strings - i think this is included in -Wc++-compat
if 'pygame' in MODULES:
VARS['$FLAGS'] += ' `sdl-config --cflags --libs` '
if SANDBOX:
VARS['$SYS'] += " -sandbox "
VARS['$FLAGS'] += " -DTP_SANDBOX "
def vars_windows():
VARS['$RM'] = 'del'
VARS['$VM'] = 'vm'
VARS['$TINYPY'] = 'tinypy'
VARS['$FLAGS'] = '-lmingw32'
VARS['$WFLAGS'] = '-Wwrite-strings -Wall'
VARS['$SYS'] = '-mingw32'
VARS['$CPYTHON'] = "-c mingw32"
if 'pygame' in MODULES:
VARS['$FLAGS'] += ' -Ic:\\mingw\\include\\SDL -lSDLmain -lSDL '
def do_cmd(cmd):
for k,v in VARS.items():
cmd = cmd.replace(k,v)
if '$' in cmd:
print 'vars_error',cmd
sys.exit(-1)
if VALGRIND and (cmd.startswith("./") or cmd.startswith("../")):
cmd = "valgrind " + cmd
print cmd
r = os.system(cmd)
if r:
print 'exit_status',r
sys.exit(r)
def do_chdir(dest):
print 'cd',dest
os.chdir(dest)
def build_bc(opt=False):
out = []
for mod in CORE:
out.append("""unsigned char tp_%s[] = {"""%mod)
fname = mod+".tpc"
data = open(fname,'rb').read()
cols = 16
for n in xrange(0,len(data),cols):
out.append(",".join([str(ord(v)) for v in data[n:n+cols]])+',')
out.append("""};""")
out.append("")
f = open('bc.c','wb')
f.write('\n'.join(out))
f.close()
def open_tinypy(fname,*args):
return open(os.path.join(TOPDIR,'tinypy',fname),*args)
def build_blob():
mods = CORE[:]
do_chdir(os.path.join(TOPDIR,'tinypy'))
for mod in mods: py2bc('python py2bc.py $SRC $DEST',mod)
do_chdir(os.path.join(TOPDIR))
out = []
out.append("/*")
out.extend([v.rstrip() for v in open(os.path.join(TOPDIR,'LICENSE.txt'),'r')])
out.append("*/")
out.append("")
out.append("#ifndef TINYPY_H")
out.append("#define TINYPY_H")
out.extend([v.rstrip() for v in open_tinypy('tp.h','r')])
for fname in ['list.c','dict.c','misc.c','string.c','builtins.c',
'gc.c','ops.c','vm.c','tp.c', 'sandbox.c']:
for line in open_tinypy(fname,'r'):
line = line.rstrip()
if not len(line): continue
if line[0] == '/': continue
if line[0] == ' ': continue
if line[0] == '\t': continue
if line[-1] != '{': continue
if 'enum' in line: continue
if '=' in line: continue
if '#' in line: continue
line = line.replace('{',';')
# Do not include prototypes already defined earlier, or gcc will
# warn about doubled prototypes in user code.
if '(' in line:
line2 = line[:line.find('(') + 1]
got_already = False
for already in out:
if already.startswith(line2):
got_already = True
break
if got_already: continue
out.append(line)
out.append("#endif")
out.append('')
dest = os.path.join(TOPDIR,'build','tinypy.h')
print 'writing %s'%dest
f = open(dest,'w')
f.write('\n'.join(out))
f.close()
# we leave all the tinypy.h stuff at the top so that
# if someone wants to include tinypy.c they don't have to have
# tinypy.h cluttering up their folder
if not os.path.exists(os.path.join(TOPDIR, 'tinypy', 'bc.c')):
do_chdir(os.path.join(TOPDIR,'tinypy'))
build_bc()
do_chdir(os.path.join(TOPDIR))
for fname in ['list.c','dict.c','misc.c','string.c','builtins.c',
'gc.c','ops.c','vm.c','bc.c','tp.c','sandbox.c']:
for line in open_tinypy(fname,'r'):
line = line.rstrip()
if line.find('#include "') != -1: continue
out.append(line)
out.append('')
dest = os.path.join(TOPDIR,'build','tinypy.c')
print 'writing %s'%dest
f = open(dest,'w')
f.write('\n'.join(out))
f.close()
def py2bc(cmd,mod):
src = '%s.py'%mod
dest = '%s.tpc'%mod
if CLEAN or not os.path.exists(dest) or os.stat(src).st_mtime > os.stat(dest).st_mtime:
cmd = cmd.replace('$SRC',src)
cmd = cmd.replace('$DEST',dest)
do_cmd(cmd)
else:
print '#',dest,'is up to date'
def build_gcc():
mods = CORE[:]
do_chdir(os.path.join(TOPDIR,'tinypy'))
nopos = ' -nopos '
if DEBUG: nopos = ''
if TEST:
mods.append('tests')
mods.append('asm')
mods.append('disasm')
do_cmd("gcc $WFLAGS -g vmmain.c $FLAGS -lm -o vm")
if BOOT:
do_cmd('python tests.py $SYS')
for mod in mods:
py2bc('python py2bc.py $SRC $DEST',mod)
else:
for mod in mods:
py2bc('python py2bc.py $SRC $DEST'+nopos,mod)
if BOOT:
do_cmd('$VM tests.tpc $SYS')
for mod in mods: py2bc('$VM py2bc.tpc $SRC $DEST',mod)
build_bc()
do_cmd("gcc $WFLAGS -g tpmain.c $FLAGS -lm -o tinypy")
#second pass - builts optimized binaries and stuff
if BOOT:
do_cmd('$TINYPY tests.py $SYS')
for mod in mods: py2bc('$TINYPY py2bc.py $SRC $DEST'+nopos,mod)
build_bc(True)
if BOOT:
do_cmd("gcc $WFLAGS -O2 tpmain.c $FLAGS -lm -o tinypy")
do_cmd('$TINYPY tests.py $SYS')
print("# OK - we'll try -O3 for extra speed ...")
do_cmd("gcc $WFLAGS -O3 tpmain.c $FLAGS -lm -o tinypy")
do_cmd('$TINYPY tests.py $SYS')
if DEBUG:
do_cmd("gcc $WFLAGS -g mymain.c $FLAGS -lm -o ../build/tinypy")
else:
do_cmd("gcc $WFLAGS -O3 mymain.c $FLAGS -lm -o ../build/tinypy")
if TEST:
do_cmd(os.path.join('..','build','tinypy')+' tests.py $SYS')
test_mods(os.path.join('..','build','tinypy')+' $TESTS')
do_chdir('..')
print("# OK")
def get_libs():
modules = os.listdir('modules')
for m in modules[:]:
if m not in sys.argv: modules.remove(m)
global MODULES
MODULES = modules
def build_mymain():
src = os.path.join(TOPDIR,'tinypy','tpmain.c')
out = open(src,'r').read()
dest = os.path.join(TOPDIR,'tinypy','mymain.c')
vs = []
for m in MODULES:
vs.append('#include "../modules/%s/init.c"'%m)
out = out.replace('/* INCLUDE */','\n'.join(vs))
vs = []
for m in MODULES:
vs.append('%s_init(tp);'%m)
out = out.replace('/* INIT */','\n'.join(vs))
f = open(dest,'w')
f.write(out)
f.close()
return True
def test_mods(cmd):
for m in MODULES:
tests = os.path.join('..','modules',m,'tests.py')
if not os.path.exists(tests): continue
cmd = cmd.replace('$TESTS',tests)
do_cmd(cmd)
def build_vs():
# How to compile on windows with Visual Studio:
# Call the batch script that sets environement variables for Visual Studio and
# then run this script.
# For VS 2005 the script is:
# "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
# For VS 2008: "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
# Doesn't compile with vc6 (no variadic macros)
# Note: /MD option causes to dynamically link with msvcrt80.dll. This dramatically
# reduces size (for vm.exe 159k => 49k). Downside is that msvcrt80.dll must be
# present on the system (and not all windows machine have it). You can either re-distribute
# msvcrt80.dll or statically link with C runtime by changing /MD to /MT.
mods = CORE[:]; mods.append('tests')
os.chdir(os.path.join(TOPDIR,'tinypy'))
do_cmd('cl vmmain.c /Od /Zi /MD /Fdvm.pdb /Fmvm.map /Fevm.exe')
do_cmd('python tests.py -win')
for mod in mods: py2bc('python py2bc.py $SRC $DEST',mod)
do_cmd('vm.exe tests.tpc -win')
for mod in mods: py2bc('vm.exe py2bc.tpc $SRC $DEST',mod)
build_bc()
do_cmd('cl /Od tpmain.c /Zi /MD /Fdtinypy.pdb /Fmtinypy.map /Fetinypy.exe')
#second pass - builts optimized binaries and stuff
do_cmd('tinypy.exe tests.py -win')
for mod in mods: py2bc('tinypy.exe py2bc.py $SRC $DEST'+nopos,mod)
build_bc(True)
do_cmd('cl /Os vmmain.c /D "NDEBUG" /Gy /GL /Zi /MD /Fdvm.pdb /Fmvm.map /Fevm.exe /link /opt:ref /opt:icf')
do_cmd('cl /Os tpmain.c /D "NDEBUG" /Gy /GL /Zi /MD /Fdtinypy.pdb /Fmtinypy.map /Fetinypy.exe /link /opt:ref,icf /OPT:NOWIN98')
do_cmd("tinypy.exe tests.py -win")
do_cmd("dir *.exe")
def shrink(fname):
f = open(fname,'r'); lines = f.readlines(); f.close()
out = []
fixes = [
'vm','gc','params','STR',
'int','float','return','free','delete','init',
'abs','round','system','pow','div','raise','hash','index','printf','main']
passing = False
for line in lines:
#quit if we've already converted
if '\t' in line: return ''.join(lines)
#change " " into "\t" and remove blank lines
if len(line.strip()) == 0: continue
line = line.rstrip()
l1,l2 = len(line),len(line.lstrip())
line = "\t"*((l1-l2)/4)+line.lstrip()
#remove comments
if '.c' in fname or '.h' in fname:
#start block comment
if line.strip()[:2] == '/*':
passing = True;
#end block comment
if line.strip()[-2:] == '*/':
passing = False;
continue
#skip lines inside block comments
if passing:
continue
if '.py' in fname:
if line.strip()[:1] == '#': continue
#remove the "namespace penalty" from tinypy ...
for name in fixes:
line = line.replace('TP_'+name,'t'+name)
line = line.replace('tp_'+name,'t'+name)
line = line.replace('TP_','')
line = line.replace('tp_','')
out.append(line)
return '\n'.join(out)+'\n'
def chksize():
t1,t2 = 0,0
for fname in [
'tokenize.py','parse.py','encode.py','py2bc.py',
'tp.h','list.c','dict.c','misc.c','string.c','builtins.c',
'gc.c','ops.c','vm.c','tp.c','tpmain.c',
]:
fname = os.path.join(TOPDIR,'tinypy',fname)
f = open(fname,'r'); t1 += len(f.read()); f.close()
txt = shrink(fname)
t2 += len(txt)
print "#",t1,t2,t2-65536
return t2
def build_64k():
for fname in [
'tokenize.py','parse.py','encode.py','py2bc.py',
'tp.h','list.c','dict.c','misc.c','string.c','builtins.c',
'gc.c','ops.c','vm.c','tp.c','tpmain.c',
]:
src = os.path.join(TOPDIR,'tinypy',fname)
dest = os.path.join(TOPDIR,'build',fname)
txt = shrink(src)
f = open(dest,'w')
f.write(txt)
f.close()
print '%s saved to %s'%(src,dest)
def build_cpython():
try: from distutils.core import setup, Extension
except: print "cannot import distutils"
do_chdir(os.path.join(TOPDIR,'cpython'))
setup(name = "tinypy",
version = "0.8",
description = "tinypy module for CPython",
url = "http://www.tinypy.org/",
ext_modules = [Extension("tinypy", ["cpython.c"], define_macros = [('CPYTHON_MOD', None)])])
def install_cpython():
try: from distutils.core import setup, Extension
except: print "cannot import distutils"
do_chdir(os.path.join(TOPDIR,'cpython'))
setup(name = "tinypy",
version = "0.8",
description = "tinypy module for CPython",
url = "http://www.tinypy.org/",
ext_modules = [Extension("tinypy", ["cpython.c"], define_macros = [('CPYTHON_MOD', None)])])
if __name__ == '__main__':
main()