This repository was archived by the owner on Aug 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_amalg.py
More file actions
747 lines (664 loc) · 25.2 KB
/
build_amalg.py
File metadata and controls
747 lines (664 loc) · 25.2 KB
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
import glob
import os
import platform
import re
import string
import subprocess
import sys
LIBQBE_C_FILES = [
"all.h",
"amd64/all.h",
"arm64/all.h",
"rv64/all.h",
"abi.c",
"alias.c",
"cfg.c",
"copy.c",
"emit.c",
"fold.c",
"live.c",
"load.c",
"main.c",
"mem.c",
"parse.c",
"rega.c",
"simpl.c",
"spill.c",
"ssa.c",
"util.c",
"amd64/emit.c",
"amd64/isel.c",
"amd64/sysv.c",
"amd64/targ.c",
"amd64/winabi.c",
"arm64/abi.c",
"arm64/emit.c",
"arm64/isel.c",
"arm64/targ.c",
"rv64/abi.c",
"rv64/emit.c",
"rv64/isel.c",
"rv64/targ.c",
"../libqbe_impl.c",
]
def namespace_static_funcs(ns, file, contents):
lines = contents.splitlines()
function_names = []
for i, line in enumerate(lines):
if (
i < len(lines) - 1
and line.startswith("static ")
and lines[i + 1]
and not lines[i + 1].startswith("static")
and lines[i + 1][0] in string.ascii_lowercase
):
fn = lines[i + 1].partition("(")[0]
if fn == "amd64_memargs" or fn == "arm64_memargs" or fn == "rv64_memargs":
continue
function_names.append(fn)
for fn in function_names:
contents = re.sub(r"\b" + fn + "\\(", ns + fn + "(", contents)
contents = re.sub(
r"qsort\((.*) " + fn + r"\);", r"qsort(\1 " + ns + fn + ");", contents
)
contents = re.sub(
r"loopiter\((.*) " + fn + r"\);", r"loopiter(\1 " + ns + fn + ");", contents
)
return contents
def rename_asserts(contents):
return contents.replace('assert(', 'LQ_ASSERT(')
def emit_renames(ns, contents):
ns_up = ns.upper()
for x in ["E", "Ki", "Ka"]:
contents = re.sub(r"\b" + x + r"\b", ns_up + x, contents)
for x in ["omap", "rname"]:
contents = re.sub(r"\b" + x + r"\b", ns + x, contents)
return contents + "\n#undef CMP\n\n"
def abi_renames(ns, contents):
ns_up = ns.upper()
for x in ["Cstk", "Cptr", "Cstk1", "Cstk2", "Cfpint",
"Class", "AClass", "RAlloc", "Insl", "Params"]:
contents = re.sub(r"\b" + x + r"\b", ns_up + x, contents)
for x in ["gpreg", "fpreg"]:
contents = re.sub(r"\b" + x + r"\b", ns + x, contents)
return contents
def arm64_reg_rename(contents):
regs = (
["R%d" % i for i in range(16)]
+ ["IP0", "IP1"]
+ ["R%d" % i for i in range(18, 29)]
+ ["V%d" % i for i in range(31)]
+ ["FP", "LR", "SP", "NFPR", "NGPR", "NGPS", "NFPS", "NCLR"]
)
for i in regs:
contents = re.sub(r"\b%s\b" % i, "QBE_ARM64_%s" % i, contents)
return contents
def amd64_reg_rename(contents):
regs = (
[
"RAX",
"RCX",
"RDX",
"RSI",
"RDI",
"R8",
"R9",
"R10",
"R11",
"RBX",
"R12",
"R13",
"R14",
"R15",
"RBP",
"RSP",
]
+ ["XMM%d" % i for i in range(16)]
+ ["NFPR", "NGPR", "NGPS", "NFPS", "NCLR", "RGLOB"]
)
for i in regs:
contents = re.sub(r"\b%s\b" % i, "QBE_AMD64_%s" % i, contents)
return contents
def rv64_reg_rename(contents):
regs = (
["T%d" % i for i in range(7)]
+ ["A%d" % i for i in range(8)]
+ ["S%d" % i for i in range(12)]
+ ["FT%d" % i for i in range(12)]
+ ["FA%d" % i for i in range(8)]
+ ["FS%d" % i for i in range(12)]
+ [
"FP",
"SP",
"GP",
"TP",
"RA",
"NFPR",
"NGPR",
"NGPS",
"NFPS",
"NCLR",
"RGLOB",
]
)
for i in regs:
contents = re.sub(r"\b%s\b" % i, "QBE_RV64_%s" % i, contents)
return contents
def make_instr_prototypes(ops_h_contents):
external_only = []
for x in ops_h_contents.splitlines():
if "INTERNAL OPERATIONS" in x:
break
if x.startswith("O("):
external_only.append(x)
def is_no_return(op):
# Also blit and call, but those are handled specially and aren't in the
# public section of ops.h.
return op == "vastart" or op.startswith("store") or op.startswith("dbgloc")
def is_single_arg_op(type_string_arg_1):
return type_string_arg_1.count('x') + type_string_arg_1.count('e') == 4
class_order = [
'lq_type_word',
'lq_type_long',
'lq_type_single',
'lq_type_double',
]
def only_single_size_class(arg0):
is_single = arg0.count('e') == 3
if is_single:
index_of_non_e = -1
for i in range(4):
if arg0[i] != 'e':
index_of_non_e = i
break
return (True, class_order[index_of_non_e])
else:
return (False, None)
decls = ""
defns = ""
for op in external_only:
assert op.startswith("O(")
toks = re.split("[ \t,T()]+", op[2:])
op = toks[0]
arg0 = "".join(toks[1:5])
arg1 = "".join(toks[5:9])
if is_single_arg_op(arg1):
# No non-return single arg ops, but some don't need a size_class
# (because there's only one possibility).
is_single_size_class, size_class0 = only_single_size_class(arg0)
if is_single_size_class:
proto = "LqRef lq_i_%s(LqRef arg0 /*%s*/)" % (op, "".join(arg0))
defns += proto + " { return _normal_one_op_instr(O%s, %s, arg0); }\n" % (
op, size_class0)
else:
proto = "LqRef lq_i_%s(LqType size_class, LqRef arg0 /*%s*/)" % (op, "".join(arg0))
defns += proto + " { return _normal_one_op_instr(O%s, size_class, arg0); }\n" % (op)
else:
if is_no_return(op):
proto = "void lq_i_%s(LqRef arg0 /*%s*/, LqRef arg1 /*%s*/)" % (
op, "".join(arg0), "".join(arg1))
defns += proto + " { _normal_two_op_void_instr(O%s, arg0, arg1); }\n" % op
else:
# None of these have trivial size classes, only the single op
# ones have that case.
proto = "LqRef lq_i_%s(LqType size_class, LqRef arg0 /*%s*/, LqRef arg1 /*%s*/)" % (
op, "".join(arg0), "".join(arg1))
defns += proto + " { return _normal_two_op_instr(O%s, size_class, arg0, arg1); }\n" % op
decls += proto + ";\n"
return (decls, defns)
def replace_noreturn(contents):
attr = "__attribute__((noreturn));"
result = []
for x in contents.splitlines():
if x.endswith(attr):
x = "LQ_NO_RETURN " + x.replace(attr, ";")
result.append(x)
return "\n".join(result)
def label_renames(contents):
# MSVC apparently uses the same namespace for labels and enums.
# This should probably just be changed upstream.
contents = contents.replace("goto Ins;", "goto Label_Ins;")
contents = re.sub(r"\bIns:", "Label_Ins:", contents)
contents = contents.replace("goto Ref;", "goto Label_Ref;")
contents = re.sub(r"\bRef:", "Label_Ref:", contents)
contents = contents.replace("goto Mem;", "goto Label_Mem;")
contents = re.sub(r"\bMem:", "Label_Mem:", contents)
return contents
def remove_function(contents, func_ret_type, func_name):
lines = contents.splitlines()
result = []
in_function = False
for i, x in enumerate(lines):
if x.startswith(func_name + "(") and lines[i - 1] == func_ret_type:
result.pop()
in_function = True
continue
elif in_function and x.startswith("}"):
in_function = False
continue
elif in_function:
continue
result.append(x)
return "\n".join(result)
def remove_data(contents, look_for):
return "\n".join([x for x in contents.splitlines() if not x.startswith(look_for)])
def remove_lines_range(contents, start, end):
lines = contents.splitlines()
result = []
in_removal = False
for i, x in enumerate(lines):
if x.startswith(start):
in_removal = True
continue
elif in_removal and x.startswith(end):
in_removal = False
continue
elif in_removal:
continue
result.append(x)
return "\n".join(result)
def fix_missing_static(contents, funcname):
result = []
lines = contents.splitlines()
for i, x in enumerate(lines):
if x.startswith(funcname + "(") and lines[i - 1] == "void":
result[i - 1] = "static " + result[i - 1]
result.append(x)
return "\n".join(result)
def staticize_util_data(contents):
result = []
for line in contents.splitlines():
if line.startswith("Typ *typ;") or line.startswith("Ins insb[NIns],"):
line = "static " + line
result.append(line)
return "\n".join(result)
def staticize_main_data(contents):
result = []
for line in contents.splitlines():
if (
line.startswith("extern Target T")
or line.startswith("Target T;")
or line.startswith("char debug[")
):
line = "static " + line.replace("extern ", "")
result.append(line)
return "\n".join(result)
def staticize_parse_data(contents):
result = []
for line in contents.splitlines():
if line.startswith("Op optab"):
line = "static " + line.replace("extern ", "")
result.append(line)
return "\n".join(result)
def staticize_targets(contents):
result = []
for line in contents.splitlines():
if line.startswith("Target T_"):
line = "static " + line
result.append(line)
return "\n".join(result)
def staticize_prototypes(contents):
result = []
for line in contents.splitlines():
if line.startswith("void parse(FILE"):
continue
if (
line.startswith("void ")
or line.startswith("uint32_t ")
or line.startswith("char *")
or line.startswith("int ")
or line.startswith("uint ")
or line.startswith("bits ")
or line.startswith("Ins *")
or line.startswith("Ref ")
or line.startswith("Blk *")
) and line.endswith(");"):
line = "static " + line
elif (
line.startswith("extern Target T")
or line.startswith("extern char debug")
or line.startswith("extern Typ *")
or line.startswith("extern Ins ")
or line.startswith("extern Op ")
):
line = "static " + line.replace("extern ", "")
result.append(line)
return "\n".join(result)
def main():
QBE_ROOT = os.path.join(os.getcwd(), "qbe")
if not os.path.exists(QBE_ROOT):
subprocess.check_call(["git", "clone", "git://c9x.me/qbe.git"])
for patch in glob.glob("patches/*.patch"):
subprocess.check_call(["git", "am", os.path.join("..", patch)], cwd=QBE_ROOT)
with open(os.path.join(QBE_ROOT, "ops.h"), "r") as f:
ops_h_contents = f.read()
with open(os.path.join(QBE_ROOT, "LICENSE"), "r") as f:
license_contents = f.read()
instr_decls, instr_defns = make_instr_prototypes(ops_h_contents)
with open("libqbe.in.h", "r") as header_in:
header_contents = header_in.read()
header_contents = header_contents.replace(
"%%%INSTRUCTION_DECLARATIONS%%%\n", instr_decls
)
with open("libqbe.h", "w", newline="\n") as header_out:
header_out.write(header_contents)
with open("libqbe.c", "w", newline="\n") as amalg:
amalg.write("/*\n\nQBE LICENSE:\n\n")
amalg.write(license_contents)
amalg.write("\n---\n\n")
amalg.write(
"All other libqbe code under the same license,\n"
"© 2025 Scott Graham <scott.libqbe@h4ck3r.net>\n\n"
)
amalg.write("*/\n\n")
amalg.write(
"""\
#ifdef _MSC_VER
#define LQ_NO_RETURN __declspec(noreturn)
#pragma warning(push)
#pragma warning(disable: 4146)
#pragma warning(disable: 4200)
#pragma warning(disable: 4244)
#pragma warning(disable: 4245)
#pragma warning(disable: 4389)
#pragma warning(disable: 4459)
#pragma warning(disable: 4701)
#else
#define LQ_NO_RETURN __attribute__((noreturn))
#endif
#ifndef LQ_ASSERT
#include <assert.h>
#define LQ_ASSERT(x) assert(x)
#endif
"""
)
for file in LIBQBE_C_FILES:
with open(os.path.join(QBE_ROOT, file), "rb") as f:
contents = f.read().decode("utf-8")
ns = "qbe_" + file.replace("/", "_").replace(".c", "") + "_"
if file.endswith(".c"):
contents = namespace_static_funcs(ns, file, contents)
contents = label_renames(contents)
contents = staticize_targets(contents)
contents = rename_asserts(contents)
if file == "arm64/all.h" or file.startswith("arm64/"):
contents = arm64_reg_rename(contents)
if file == "amd64/all.h" or file.startswith("amd64/"):
contents = amd64_reg_rename(contents)
if file == "rv64/all.h" or file.startswith("rv64/"):
contents = rv64_reg_rename(contents)
if file.endswith("/emit.c"):
contents = emit_renames(ns, contents)
if file == "emit.c": # This should be changed upstream.
contents = fix_missing_static(contents, "emitlnk")
if file == "cfg.c": # This should be changed upstream.
contents = fix_missing_static(contents, "multloop")
if file == "util.c":
contents = staticize_util_data(contents)
if file == "main.c":
contents = staticize_main_data(contents)
if file == "parse.c":
contents = staticize_parse_data(contents)
if file == "amd64/targ.c":
contents = contents.replace(
"Amd64Op amd64_op", "static Amd64Op amd64_op"
)
if file == "amd64/sysv.c":
contents = contents.replace(
"int amd64_sysv_rsave", "static int amd64_sysv_rsave"
)
contents = contents.replace(
"int amd64_sysv_rclob", "static int amd64_sysv_rclob"
)
if file == "amd64/winabi.c":
contents = contents.replace(
"int amd64_winabi_rsave", "static int amd64_winabi_rsave"
)
contents = contents.replace(
"int amd64_winabi_rclob", "static int amd64_winabi_rclob"
)
if file == "arm64/targ.c":
contents = contents.replace("int arm64_rsave", "static int arm64_rsave")
contents = contents.replace("int arm64_rclob", "static int arm64_rclob")
if file == "rv64/targ.c":
contents = contents.replace("Rv64Op rv64_op", "static Rv64Op rv64_op")
contents = contents.replace("int rv64_rsave", "static int rv64_rsave")
contents = contents.replace("int rv64_rclob", "static int rv64_rclob")
if file.endswith("all.h"):
contents = staticize_prototypes(contents)
# MSVC annoyingly doesn't handle static forward declarations
# without a size properly and just dies at the point of
# declaration. We can't easily restructure to get the ops,
# regcounts, etc. before the decl, so just hardcode and rely on
# the MAKESUREs to make sure they match.
contents = contents.replace(
"extern Amd64Op amd64_op[];", "static Amd64Op amd64_op[138];"
)
contents = contents.replace(
"extern int amd64_sysv_rsave[];", "static int amd64_sysv_rsave[25];"
)
contents = contents.replace(
"extern int amd64_sysv_rclob[];", "static int amd64_sysv_rclob[6];"
)
contents = contents.replace(
"extern int amd64_winabi_rsave[];", "static int amd64_winabi_rsave[23];"
)
contents = contents.replace(
"extern int amd64_winabi_rclob[];", "static int amd64_winabi_rclob[8];"
)
contents = contents.replace(
"extern int arm64_rsave[];", "static int arm64_rsave[44];"
)
contents = contents.replace(
"extern int arm64_rclob[];", "static int arm64_rclob[19];"
)
contents = contents.replace(
"extern Rv64Op rv64_op[];", "static Rv64Op rv64_op[138];"
)
contents = contents.replace(
"extern int rv64_rsave[];", "static int rv64_rsave[34];"
)
contents = contents.replace(
"extern int rv64_rclob[];", "static int rv64_rclob[24];"
)
if (file.endswith("/abi.c") or
file.endswith("amd64/sysv.c") or
file.endswith("amd64/winabi.c")):
contents = abi_renames(ns, contents)
if file == "main.c":
contents = remove_function(contents, "int", "main")
contents = remove_lines_range(contents, "static Target *tlist", "};")
if file == "parse.c":
contents = remove_function(contents, "void", "parse")
contents = remove_function(
contents, "static void", "qbe_parse_parsedatref"
)
contents = remove_function(
contents, "static void", "qbe_parse_parsedat"
)
contents = remove_function(
contents, "static Ref", "qbe_parse_tmpref"
)
contents = remove_function(
contents, "static void", "qbe_parse_parsetyp"
)
contents = remove_function(
contents, "static void", "qbe_parse_parsedatstr"
)
contents = remove_function(
contents, "static void", "qbe_parse_parsefields"
)
contents = remove_function(contents, "static void", "qbe_parse_expect")
contents = remove_function(
contents, "static Blk *", "qbe_parse_findblk"
)
contents = remove_function(
contents, "static PState", "qbe_parse_parseline"
)
contents = remove_function(contents, "static int", "qbe_parse_parselnk")
contents = remove_function(contents, "static int", "qbe_parse_nextnl")
contents = remove_function(contents, "static int", "qbe_parse_next")
contents = remove_function(contents, "static int", "qbe_parse_peek")
contents = remove_function(contents, "static int", "qbe_parse_lex")
contents = remove_function(
contents, "static int64_t", "qbe_parse_getint"
)
contents = remove_function(
contents, "static int", "qbe_parse_parserefl"
)
contents = remove_function(contents, "void", "err")
contents = remove_function(contents, "static int", "qbe_parse_findtyp")
contents = remove_function(contents, "static int", "qbe_parse_parsecls")
contents = remove_function(contents, "static Ref", "qbe_parse_parseref")
contents = remove_function(contents, "static Fn *", "qbe_parse_parsefn")
contents = remove_function(contents, "static void", "qbe_parse_lexinit")
contents = remove_data(contents, "static uint ntyp;")
contents = remove_data(contents, "static int nblk;")
contents = remove_data(contents, "static FILE *inf;")
contents = remove_data(contents, "static Blk *blkh")
contents = remove_data(contents, "static int thead;")
contents = remove_data(contents, "static uchar lexh")
contents = remove_data(contents, "static char *inpath;")
contents = remove_data(contents, "static int lnum;")
contents = remove_data(contents, "static int *tmph;")
contents = remove_data(contents, "static int tmphcap;")
contents = remove_lines_range(contents, "static struct {", "} tokval;")
contents = remove_lines_range(contents, "static char *kwmap", "};")
contents = replace_noreturn(contents)
amalg.write("/*** START FILE: %s ***/\n" % file)
for line in contents.splitlines():
if line.startswith('#include "all.h"'):
amalg.write("/* skipping all.h */\n")
continue
if line.startswith('#include "../all.h"'):
amalg.write("/* skipping ../all.h */\n")
continue
if line.startswith('#include "config.h"'):
amalg.write("/* skipping config.h */\n")
continue
if line.startswith("#include <getopt.h>"):
amalg.write("/* skipping getopt.h */\n")
continue
if line.startswith("#include <assert.h>"):
amalg.write("/* skipping assert.h */\n")
continue
if line.strip().startswith(
'#include "ops.h"'
) or line.strip().startswith('#include "../ops.h"'):
amalg.write("/* " + 60 * "-" + "including ops.h */\n")
amalg.write(ops_h_contents)
amalg.write("/* " + 60 * "-" + "end of ops.h */\n")
continue
amalg.write(line)
amalg.write("\n")
amalg.write("/*** END FILE: %s ***/\n" % file)
amalg.write(instr_defns)
amalg.write(
"""\
#ifdef _MSC_VER
#pragma warning(pop)
#endif
"""
)
if sys.platform == "win32":
subprocess.check_call(
[
"cl",
"/D_CRT_SECURE_NO_WARNINGS",
"/nologo",
"/W4",
"/WX",
"/c",
"libqbe.c",
]
)
subprocess.check_call(
[
"cl",
"/O2",
"/D_CRT_SECURE_NO_WARNINGS",
"/nologo",
"/W4",
"/WX",
"/c",
"libqbe.c",
]
)
os.remove("libqbe.obj")
print("win32 build ok")
elif sys.platform == "darwin":
subprocess.check_call(
["clang", "-Wall", "-Wextra", "-Werror", "-pedantic", "-c", "libqbe.c"]
)
subprocess.check_call(
[
"clang",
"-O3",
"-Wall",
"-Wextra",
"-Werror",
"-pedantic",
"-c",
"libqbe.c",
]
)
os.remove("libqbe.o")
print("darwin build ok")
elif sys.platform == "linux":
# Check we can build with gcc and clang
subprocess.check_call(
["gcc", "-Wall", "-Wextra", "-Werror", "-pedantic", "-c", "libqbe.c"]
)
subprocess.check_call(
["gcc", "-O2", "-Wall", "-Wextra", "-Werror", "-pedantic", "-c", "libqbe.c"]
)
subprocess.check_call(
["clang", "-Wall", "-Wextra", "-Werror", "-pedantic", "-c", "libqbe.c"]
)
subprocess.check_call(
[
"clang",
"-O3",
"-Wall",
"-Wextra",
"-Werror",
"-pedantic",
"-c",
"libqbe.c",
]
)
# And can link from C++.
subprocess.check_call(
[
"clang++",
"-Wall",
"-Wextra",
"-Werror",
"libqbe.o",
"in_cpp_link_test.cc",
"-o",
"in_cpp",
]
)
# And that the the only exported symbols from libqbe are those we expect
# (prefixed by `lq_`).
symsp = subprocess.run(["readelf", "-s", "libqbe.o"], capture_output=True)
os.remove("libqbe.o")
os.remove("in_cpp")
syms = str(symsp.stdout, encoding="utf-8").splitlines()
syms = [l for l in syms if "GLOBAL " in l]
syms = [l for l in syms if "UND " not in l]
for s in syms:
symname = s.split()[-1]
if not symname.startswith("lq_"):
print("Unexpected symbol:", symname)
sys.exit(1)
print("These are the global exported symbols from libqbe.o. They look ok, but")
print("confirm that they match libqbe.h (only).")
print("-" * 80)
for s in syms:
print(s)
print("-" * 80)
print("libqbe.[ch] ready for distribution")
if __name__ == "__main__":
main()