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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
import os, json, logging
import shared
from tools.shared import execute
def calculate(temp_files, in_temp, stdout, stderr):
# Check if we need to include some libraries that we compile. (We implement libc ourselves in js, but
# compile a malloc implementation and stdlibc++.)
def read_symbols(path, exclude=None):
symbols = map(lambda line: line.strip().split(' ')[1], open(path).readlines())
if exclude:
symbols = filter(lambda symbol: symbol not in exclude, symbols)
return set(symbols)
lib_opts = ['-O2']
# XXX We also need to add libc symbols that use malloc, for example strdup. It's very rare to use just them and not
# a normal malloc symbol (like free, after calling strdup), so we haven't hit this yet, but it is possible.
libc_symbols = read_symbols(shared.path_from_root('system', 'lib', 'libc.symbols'))
libcextra_symbols = read_symbols(shared.path_from_root('system', 'lib', 'libcextra.symbols'))
libcxx_symbols = read_symbols(shared.path_from_root('system', 'lib', 'libcxx', 'symbols'), exclude=libc_symbols)
libcxxabi_symbols = read_symbols(shared.path_from_root('system', 'lib', 'libcxxabi', 'symbols'), exclude=libc_symbols)
gl_symbols = read_symbols(shared.path_from_root('system', 'lib', 'gl.symbols'))
# XXX we should disable EMCC_DEBUG when building libs, just like in the relooper
def build_libc(lib_filename, files):
o_s = []
prev_cxx = os.environ.get('EMMAKEN_CXX')
if prev_cxx: os.environ['EMMAKEN_CXX'] = ''
musl_internal_includes = ['-I', shared.path_from_root('system', 'lib', 'libc', 'musl', 'src', 'internal'), '-I', shared.path_from_root('system', 'lib', 'libc', 'musl', 'arch', 'js')]
for src in files:
o = in_temp(os.path.basename(src) + '.o')
execute([shared.PYTHON, shared.EMCC, shared.path_from_root('system', 'lib', src), '-o', o] + musl_internal_includes + lib_opts, stdout=stdout, stderr=stderr)
o_s.append(o)
if prev_cxx: os.environ['EMMAKEN_CXX'] = prev_cxx
shared.Building.link(o_s, in_temp(lib_filename))
return in_temp(lib_filename)
def build_libcxx(src_dirname, lib_filename, files):
o_s = []
for src in files:
o = in_temp(src + '.o')
srcfile = shared.path_from_root(src_dirname, src)
execute([shared.PYTHON, shared.EMXX, srcfile, '-o', o, '-std=c++11'] + lib_opts, stdout=stdout, stderr=stderr)
o_s.append(o)
shared.Building.link(o_s, in_temp(lib_filename))
return in_temp(lib_filename)
# libc
def create_libc():
logging.debug(' building libc for cache')
libc_files = [
'dlmalloc.c',
os.path.join('libcxx', 'new.cpp'),
]
musl_files = [
['ctype', [
'isdigit.c',
'isspace.c',
'isupper.c',
'tolower.c',
]],
['internal', [
'intscan.c',
'floatscan.c',
'shgetc.c',
]],
['math', [
'frexp.c',
'frexpf.c',
'frexpl.c',
'scalbn.c',
'scalbnl.c',
]],
['multibyte', [
'wctomb.c',
'wcrtomb.c',
]],
['stdio', [
'__overflow.c',
'__toread.c',
'__towrite.c',
'__uflow.c',
'fwrite.c',
'snprintf.c',
'sprintf.c',
'vfprintf.c',
'vsnprintf.c',
'vsprintf.c',
]],
['stdlib', [
'atof.c',
'atoi.c',
'atol.c',
'strtod.c',
'strtol.c',
]],
['string', [
'memchr.c',
'memcmp.c',
'strcasecmp.c',
'strcmp.c',
'strncasecmp.c',
'strncmp.c',
]]
]
for directory, sources in musl_files:
libc_files += [os.path.join('libc', 'musl', 'src', directory, source) for source in sources]
return build_libc('libc.bc', libc_files)
def apply_libc(need):
# libc needs some sign correction. # If we are in mode 0, switch to 2. We will add our lines
try:
if shared.Settings.CORRECT_SIGNS == 0: raise Exception('we need to change to 2')
except: # we fail if equal to 0 - so we need to switch to 2 - or if CORRECT_SIGNS is not even in Settings
shared.Settings.CORRECT_SIGNS = 2
if shared.Settings.CORRECT_SIGNS == 2:
shared.Settings.CORRECT_SIGNS_LINES = [shared.path_from_root('src', 'dlmalloc.c') + ':' + str(i+4) for i in [4816, 4191, 4246, 4199, 4205, 4235, 4227]]
# If we are in mode 1, we are correcting everything anyhow. If we are in mode 3, we will be corrected
# so all is well anyhow too.
return True
# libcextra
def create_libcextra():
logging.debug('building libcextra for cache')
musl_files = [
['compat', [
'strlwr.c',
'strtol_l.c',
'strupr.c'
]],
['ctype', [
'isalnum.c',
'isalpha.c',
'isascii.c',
'isblank.c',
'iscntrl.c',
'isgraph.c',
'islower.c',
'isprint.c',
'ispunct.c',
'isxdigit.c',
'iswalnum.c',
'iswalpha.c',
'iswblank.c',
'iswcntrl.c',
'iswctype.c',
'iswdigit.c',
'iswgraph.c',
'iswlower.c',
'iswprint.c',
'iswpunct.c',
'iswspace.c',
'iswupper.c',
'iswxdigit.c',
'toascii.c',
'toupper.c',
'towctrans.c',
'wcswidth.c',
'wctrans.c',
'wcwidth.c',
]],
['legacy', [
'err.c',
]],
['locale', [
'iconv.c',
'isalnum_l.c',
'isalpha_l.c',
'isblank_l.c',
'iscntrl_l.c',
'isdigit_l.c',
'isgraph_l.c',
'islower_l.c',
'isprint_l.c',
'ispunct_l.c',
'isspace_l.c',
'isupper_l.c',
'isxdigit_l.c',
'iswalnum_l.c',
'iswalpha_l.c',
'iswblank_l.c',
'iswcntrl_l.c',
'iswctype_l.c',
'iswdigit_l.c',
'iswgraph_l.c',
'iswlower_l.c',
'iswprint_l.c',
'iswpunct_l.c',
'iswspace_l.c',
'iswupper_l.c',
'iswxdigit_l.c',
'strcoll.c',
'strcasecmp_l.c',
'strfmon.c',
'strncasecmp_l.c',
'strxfrm.c',
'tolower_l.c',
'toupper_l.c',
'towctrans_l.c',
'towlower_l.c',
'towupper_l.c',
'wcscoll.c',
'wcscoll_l.c',
'wcsxfrm.c',
'wcsxfrm_l.c',
'wctrans_l.c',
'wctype_l.c',
]],
['math', [
'__cos.c',
'__cosdf.c',
'__sin.c',
'__sindf.c',
'ilogb.c',
'ilogbf.c',
'ilogbl.c',
'ldexp.c',
'ldexpf.c',
'ldexpl.c',
'logb.c',
'logbf.c',
'logbl.c',
'lgamma.c',
'lgamma_r.c',
'lgammaf.c',
'lgammaf_r.c',
'lgammal.c',
'scalbnf.c',
'signgam.c',
'tgamma.c',
'tgammaf.c',
'tgammal.c'
]],
['misc', [
'getopt.c',
'getopt_long.c',
]],
['multibyte', [
'btowc.c',
'internal.c',
'mblen.c',
'mbrlen.c',
'mbrtowc.c',
'mbsinit.c',
'mbsnrtowcs.c',
'mbsrtowcs.c',
'mbstowcs.c',
'mbtowc.c',
'wcsnrtombs.c',
'wcsrtombs.c',
'wcstombs.c',
'wctob.c',
]],
['regex', [
'fnmatch.c',
'regcomp.c',
'regerror.c',
'regexec.c',
'tre-mem.c',
]],
['stdio', [
'__string_read.c',
'asprintf.c',
'fwprintf.c',
'swprintf.c',
'vfwprintf.c',
'vswprintf.c',
'vwprintf.c',
'wprintf.c',
'fputwc.c',
'fputws.c',
'sscanf.c',
'vasprintf.c',
'vfscanf.c',
'vsscanf.c',
]],
['stdlib', [
'atoll.c',
'bsearch.c',
'ecvt.c',
'fcvt.c',
'gcvt.c',
'qsort.c',
'wcstod.c',
'wcstol.c',
]],
['string', [
'bcmp.c',
'bcopy.c',
'bzero.c',
'index.c',
'memccpy.c',
'memmem.c',
'mempcpy.c',
'memrchr.c',
'rindex.c',
'stpcpy.c',
'strcasestr.c',
'strchr.c',
'strchrnul.c',
'strcspn.c',
'strdup.c',
'strlcat.c',
'strlcpy.c',
'strncat.c',
'strndup.c',
'strnlen.c',
'strpbrk.c',
'strrchr.c',
'strsep.c',
'strspn.c',
'strstr.c',
'strtok.c',
'strtok_r.c',
'strverscmp.c',
'wcpcpy.c',
'wcpncpy.c',
'wcscasecmp.c',
'wcscasecmp_l.c',
'wcscat.c',
'wcschr.c',
'wcscmp.c',
'wcscpy.c',
'wcscspn.c',
'wcsdup.c',
'wcslen.c',
'wcsncasecmp.c',
'wcsncasecmp_l.c',
'wcsncat.c',
'wcsncmp.c',
'wcsncpy.c',
'wcsnlen.c',
'wcspbrk.c',
'wcsrchr.c',
'wcsspn.c',
'wcsstr.c',
'wcstok.c',
'wcswcs.c',
'wmemchr.c',
'wmemcmp.c',
'wmemcpy.c',
'wmemmove.c',
'wmemset.c',
]]
]
libcextra_files = []
for directory, sources in musl_files:
libcextra_files += [os.path.join('libc', 'musl', 'src', directory, source) for source in sources]
return build_libc('libcextra.bc', libcextra_files)
# libcxx
def create_libcxx():
logging.debug('building libcxx for cache')
libcxx_files = [
'algorithm.cpp',
'condition_variable.cpp',
'future.cpp',
'iostream.cpp',
'memory.cpp',
'random.cpp',
'stdexcept.cpp',
'system_error.cpp',
'utility.cpp',
'bind.cpp',
'debug.cpp',
'hash.cpp',
'mutex.cpp',
'string.cpp',
'thread.cpp',
'valarray.cpp',
'chrono.cpp',
'exception.cpp',
'ios.cpp',
'locale.cpp',
'regex.cpp',
'strstream.cpp'
]
|