forked from msipos/ripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·251 lines (215 loc) · 7.27 KB
/
build.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
#!/usr/bin/python
# Possible build flags are "force", "quiet", "nodebug", "nogc", "profile"
DATA_TYPES = ['Array1', 'Double', 'Flags', 'Integer', 'Map', 'Range', 'Set',
'String', 'Tuple']
STDLIB = ['Err', 'Math', 'Out', 'Std', 'Test', 'TextFile']
MODULES = DATA_TYPES + STDLIB + ['Gd', 'Gsl', 'Sdl']
DEF_MODULES = DATA_TYPES + STDLIB
# BUILD SCRIPT FROM HERE ON
import os, sys, tools
CC = ["gcc"]
LD = ["ld"]
CFLAGS = ["-Wall", "-Wstrict-aliasing=0",
"-Wfatal-errors", "-std=gnu99", "-I."]
LFLAGS = ["-lgc"]
if "nogc" not in sys.argv:
CFLAGS.append("-DCLIB_GC")
if "force" in sys.argv:
tools.set_forcing(True)
if "quiet" in sys.argv:
tools.set_quiet(True)
if "verbose" in sys.argv:
tools.set_verbose(True)
if "profile" in sys.argv:
CFLAGS.append("-pg")
CFLAGS.append("-fno-omit-frame-pointer")
CFLAGS.append("-O3")
CFLAGS.append("-DNDEBUG")
if "nodebug" in sys.argv:
CFLAGS.append("-O3")
CFLAGS.append("-DNDEBUG")
else:
CFLAGS.append("-g")
# Construct required directories
required_dirs = ['bin', 'product', 'product/include', 'product/include/clib',
'product/include/vm', 'product/include/modules']
for d in required_dirs:
tools.mkdir_safe(d)
def link_objs(objs, output):
# Link objects into the output program
if tools.depends(output, objs):
arr = tools.flatten([LD, '-r', objs, '-o', output])
tools.pprint('LD', output)
tools.call(arr)
def cons_obj(target, src, depends):
tdepends = [src] + depends
# Check timestamps
if tools.depends(target, tdepends):
# Build object
tools.pprint('CC', src, target)
tools.call([CC, CFLAGS, '-c', src, '-o', target])
def cons_yacc(target, src, depends):
tdepends = [src] + depends
# Check timestamps
if tools.depends(target, tdepends):
tools.pprint("YAC", src, target)
tools.call(['bison', src])
def cons_flex(target, src, depends):
tdepends = [src] + depends
# Check timestamps
if tools.depends(target, tdepends):
tools.pprint("LEX", src, target)
tools.call(['flex', src])
def cons_bin(target, objs, depends):
tdepends = objs + depends
if tools.depends(target, tdepends):
tools.pprint('BIN', target)
tools.call([CC, CFLAGS, LFLAGS, objs, '-o', target])
def cons_gen(gen_program, target, t):
if tools.depends(target, [gen_program]):
tools.pprint('GEN', gen_program, target)
tools.call([gen_program, t, '>', target])
def cons_objs(srcs, depends):
results = []
for src in srcs:
root, ext = os.path.splitext(src)
dest = root + '.o'
cons_obj(dest, src, depends)
results.append(dest)
return results
def copy_file(dest, src):
import shutil
if tools.depends(dest, [src]):
tools.pprint('CP', src, dest)
shutil.copy(src, dest)
# Temporary files
junkyard = []
# Clib
clib_hs = [
'clib/array.h',
'clib/clib.h',
'clib/dict.h',
'clib/hash.h',
'clib/mem.h',
'clib/stringbuf.h',
'clib/utf8.h'
]
clib_srcs = [
'clib/array.c',
'clib/dict.c',
'clib/hash.c',
'clib/mem.c',
'clib/utf8.c',
'clib/stringbuf.c'
]
clib_objs = cons_objs(clib_srcs, clib_hs)
###############################################################################
# RIPE TOOLS
###############################################################################
cons_yacc('ripe/parser.c', 'ripe/parser.y',
['ripe/ripe.h'] + clib_hs)
cons_flex('ripe/scanner.c', 'ripe/scanner.l',
['ripe/parser.h'] + clib_hs)
ripe_hs = [
'ripe/ripe.h',
'ripe/parser.h',
'ripe/scanner.h'
]
ripe_srcs = [
'ripe/ripe.c',
'ripe/cli.c',
'ripe/build-tree.c',
'ripe/parser.c',
'ripe/scanner.c',
'ripe/astnode.c',
'ripe/operator.c',
'ripe/generator.c'
]
ripe_objs = cons_objs(ripe_srcs, ripe_hs + clib_hs)
cons_bin('product/ripe', ripe_objs + clib_objs, [])
###############################################################################
# VM
###############################################################################
func_gen_objs = cons_objs(['vm/func-gen/func-gen.c'], [])
cons_bin('bin/func-gen', func_gen_objs, [])
cons_gen('bin/func-gen', 'vm/func-generated.c', 'c')
cons_gen('bin/func-gen', 'vm/func-generated.h', 'h')
ops_gen_objs = cons_objs(['vm/ops-gen/ops-gen.c'], [])
cons_bin('bin/ops-gen', ops_gen_objs, [])
cons_gen('bin/ops-gen', 'vm/ops-generated.c', 'c')
cons_gen('bin/ops-gen', 'vm/ops-generated.h', 'h')
vm_hs = [
'vm/vm.h',
'vm/value_inline.c',
'vm/ops-generated.h',
'vm/func-generated.h',
]
# VM
vm_srcs = [
'vm/vm.c',
'vm/common.c',
'vm/sym-table.c',
'vm/ops.c',
'vm/ops-generated.c',
'vm/util.c',
'vm/klass.c',
'vm/stack.c',
'vm/builtin/Object.c',
'vm/builtin/Function.c',
'vm/func-generated.c',
'vm/builtin/HashTable.c',
'vm/builtin/String.c',
'vm/builtin/Integer.c',
'vm/builtin/Double.c',
'vm/builtin/Arrays.c',
'vm/builtin/Range.c',
'vm/builtin/Complex.c',
'vm/builtin/Tuple.c'
]
vm_objs = cons_objs(vm_srcs, vm_hs + clib_hs)
# Construct VM object
link_objs(vm_objs + clib_objs, "product/vm.o")
include_headers = clib_hs + vm_hs + ['modules/modules.h']
for header in include_headers:
copy_file('product/include/' + header, header)
f = open('product/ripe.conf', 'w')
f.write('cflags=%s\n' % " ".join(CFLAGS))
f.write('lflags=%s\n' % " ".join(LFLAGS))
f.write('modules=%s\n' % (" ".join(DEF_MODULES)))
f.close()
##############################################################################
# BUILD MODULES
##############################################################################
module_deps = vm_hs + clib_hs + ['modules/modules.h', 'product/ripe']
def cons_module(src, dest, module, extra_CFLAGS=''):
if tools.depends(dest, module_deps + [src]):
tools.pprint('MOD', src, dest)
args = ['product/ripe', '-n', module, '-c', src, '-o', dest,
'-f', '"%s"' % extra_CFLAGS]
if tools.is_verbose():
args.append('-v')
tools.call(args)
def build_module(module):
import os.path
out = 'product/%s.o' % module
# Deal with metafile
metafile = 'modules/%s/%s.meta' % (module, module)
if os.path.exists(metafile):
copy_file('product/%s.meta' % module, metafile)
meta = tools.load_meta(metafile)
extra_CFLAGS = ''
if meta.has_key('includes'):
extra_CFLAGS = meta['includes']
path = 'modules/%s/%s.rip' % (module, module)
if os.path.exists(path):
cons_module(path, out, module, extra_CFLAGS)
return
path = 'modules/%s/%s.c' % (module, module)
if os.path.exists(path):
cons_module(path, out, module, extra_CFLAGS)
return
for module in MODULES:
build_module(module)
# Cleanup
for filename in junkyard:
tools.delete_safe(filename)