-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomp_backend.py
More file actions
1406 lines (1211 loc) · 47.9 KB
/
Copy pathcomp_backend.py
File metadata and controls
1406 lines (1211 loc) · 47.9 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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pathlib
import subprocess
import os.path
import time
import sys
import shutil
import argparse
from typing import Any, List, Optional, TextIO, Type, Union, Tuple, Dict, overload
BUILD_DIR: pathlib.Path
BIN_DIR: pathlib.Path
WORKDIR: pathlib.Path
Product = Union['Obj', 'Cmd', 'Exe']
DIRECTORIES: Dict[str, None] = dict()
TARGET_GROUPS: Dict[str, List[Product]] = dict()
OBJECTS: Dict[str, 'Obj'] = dict()
EXECUTABLES: Dict[str, 'Exe'] = dict()
CUSTOMS: Dict[str, 'Cmd'] = dict()
use_copyto = False
g_context: Optional['Context'] = None
class Package:
def __init__(self, compile_flags: str, link_flags: str, dlls: List[str],
sources: List[str], path: pathlib.Path) -> None:
self.compile_flags = compile_flags
self.link_flags = link_flags
self.dlls = dlls
self.sources = sources
self.path = path
class Context:
def __init__(self, directory: Optional[str]=None,
group: Optional[str]=None,
defines: List[Union[str, Tuple[str, str]]]=[],
includes: List[str]=[],
namespace: Optional[str]=None) -> None:
self.group = group
self.directory = directory and str(pathlib.PurePath(directory))
self.defines = defines
self.includes = includes
self.namespace = namespace or ""
def __enter__(self) -> 'Context':
global g_context
if g_context is not None:
raise RuntimeError("Recursive Context not allowed")
g_context = self
return self
def __exit__(self, t, v, tb) -> None:
global g_context
assert g_context is self
g_context = None
return
class Obj:
def __init__(self, name: str, source: str, cmp_flags: str="", namespace: str="",
depends: List[Union[str, Product]]=[]) -> None:
self.name = name
self.namespace = namespace
self.source = source
self.cmp_flags=cmp_flags
self.depends: List[str] = []
self.cmd = "<unkown>"
self.headers: Optional[Dict[str, None]] = None
for dep in depends:
if isinstance(dep, str):
self.depends.append(dep)
else:
self.depends.append(dep.product)
@property
def product(self) -> str:
return str(BUILD_DIR / self.name)
class Cmd:
def __init__(self, name: Union[str, List[str]], cmd: str,
depends: List[Union[str, Product]],
directory: str) -> None:
self.depends: List[str] = []
for dep in depends:
if isinstance(dep, str):
self.depends.append(dep)
else:
self.depends.append(dep.product)
self.name = name
if isinstance(name, list):
assert len(name) > 0
self.cmd = cmd
self.dir = directory
self.rule: Optional[str] = None
@property
def all_products(self) -> List[str]:
p = pathlib.PurePath(self.dir)
if isinstance(self.name, str):
return [str(p / self.name)]
return [str(p / name) for name in self.name]
@property
def product(self) -> str:
if isinstance(self.name, list):
name = self.name[0]
else:
name = self.name
return str(pathlib.PurePath(self.dir) / name)
class DummyCmd(Cmd):
def __init__(self, name: str, parent: Cmd, directory: str) -> None:
self.name = name
self.dir = directory
self.parent = parent
self.depends: List[str] = [parent.product]
self.cmd = ""
self.rule: Optional[str] = None
@property
def product(self) -> str:
return str(pathlib.PurePath(self.dir) / self.name)
class Exe:
def __init__(self, name: str, objs: List[Obj], cmds: List[Cmd],
link_flags: str, dll: bool, directory: str) -> None:
self.directory = directory
self.name = name
self.objs = objs
self.cmds = cmds
self.cmd = '<unkown>'
self.link_flags = link_flags
self.dll = dll
@property
def product(self) -> str:
return str(pathlib.PurePath(self.directory) / self.name)
class BackendBase:
builddir: pathlib.Path
bindir: pathlib.Path
workdir: pathlib.Path
clflags: str
linkflags: str
@property
def msvc(self) -> bool:
return False
@property
def mingw(self) -> bool:
return False
@property
def clang(self) -> bool:
return False
@property
def ecmascript(self) -> bool:
return False
def embed_files(self, files: List[str]) -> Optional[str]:
return None
class EmCC(BackendBase):
name = "emcc"
@property
def ecmascript(self) -> bool:
return True
def handle_obj_line(self, obj: Obj, line: str) -> bool:
return False
def compile_obj(self, obj: Obj) -> str:
return f"emcc -c -o {obj.product} {obj.cmp_flags} {obj.source}"
def compile_obj_ninja(self) -> str:
return " deps = gcc\n depfile = $out.d\n" + \
" command = emcc.bat -MMD -MF $out.d -c $in -o $out $cl_flags"
def link_exe_ninja(self) -> str:
return " command = emcc.bat -o $out $in $link_flags"
def link_dll_ninja(self) -> str:
raise RuntimeError("Link dll not supported for emcc")
def find_headers(self, obj: Obj) -> List[str]:
raise RuntimeError("find_headers not supported for emcc")
def link_exe(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"emcc.bat -o {exe.product} {exe.link_flags} {row}"
def link_dll(self, exe: Exe) -> str:
raise RuntimeError("Not supported for emcc")
def include(self, directory: str) -> str:
return f"-I{directory}"
def libpath(self, directory: str) -> str:
return f"-L{directory}"
def define(self, key: str, val: Optional[str]) -> str:
if val:
return f"-D{key}={val}"
return f"-D{key}"
def import_lib_cmd(self, out: str, dll: str, symbols: List[str]) -> Cmd:
raise RuntimeError("Not supported for emcc")
def embed_files(self, files: List[str]) -> Optional[str]:
paths = [pathlib.Path(s).absolute().relative_to(WORKDIR) for s in files]
return ' '.join([f'--embed-file="{p}"@"/{pathlib.PurePosixPath(p)}"' for p in paths])
class ZigCC(BackendBase):
name = "zigcc"
@property
def clang(self) -> bool:
return True
def handle_obj_line(self, obj: Obj, line: str) -> bool:
return False
def compile_obj(self, obj: Obj) -> str:
return f"zig.exe cc -c -o {obj.product} {obj.cmp_flags} {obj.source}"
def compile_obj_ninja(self) -> str:
return " deps = gcc\n depfile = $out.d\n" + \
" command = zig.exe cc -MMD -MF $out.d -c $in -o $out $cl_flags"
def link_exe_ninja(self) -> str:
return " command = zig.exe cc -o $out $in $link_flags"
def link_dll_ninja(self) -> str:
return " command = zig.exe cc -shared -o $out $in $link_flags"
def find_headers(self, obj: Obj) -> List[str]:
cmd = f"zig.exe cc -MM {obj.cmp_flags} {obj.source}"
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode != 0:
print(res)
raise RuntimeError("Failed generating headers")
line = res.stdout.decode().replace("\\\r\n", "")
return line.split()[2:]
def link_exe(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"zig.exe cc -o {exe.product} {exe.link_flags} {row}"
def link_dll(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"zig.exe cc -shared -o {exe.product} {exe.link_flags} {row}"
def include(self, directory: str) -> str:
return f"-I{directory}"
def libpath(self, directory: str) -> str:
return f"-L{directory}"
def define(self, key: str, val: Optional[str]) -> str:
if val:
return f"-D{key}={val}"
return f"-D{key}"
def import_lib_cmd(self, out: str, dll: str, symbols: List[str]) -> Cmd:
def_path = BUILD_DIR / pathlib.PurePath(out).with_suffix(".def").name
rel_def_path = def_path.relative_to(BUILD_DIR)
def_file = Command(str(rel_def_path), f"python comp.py --deffile {def_path} {' '.join(symbols)}")
out_path = BUILD_DIR / out
cmd = f"zig.exe dlltool -D {dll} -d {def_path} -l {out_path}"
return Command(out, cmd, def_file)
class Mingw(BackendBase):
name = "mingw"
def __init__(self) -> None:
self._pending_line = False
@property
def mingw(self) -> bool:
return True
def handle_obj_line(self, obj: Obj, line: str) -> bool:
if obj.headers is None:
obj.headers = dict()
pref = f"{obj.product}:"
offset = 0
if line.startswith(pref):
line = line[len(pref):]
offset = 1
elif not self._pending_line:
return False
self._pending_line = False
if line.endswith("\\"):
self._pending_line = True
line = line[:-2]
for h in line.split()[offset:]:
try:
header = str(pathlib.Path(h).resolve().relative_to(WORKDIR))
obj.headers[header] = None
except Exception:
pass
return True
def compile_obj(self, obj: Obj) -> str:
return f"g++.exe -MMD -MF - -c -o {obj.product} {obj.cmp_flags} {obj.source}"
def compile_obj_ninja(self) -> str:
return " deps = gcc\n depfile = $out.d\n" + \
" command = g++.exe -MMD -MF $out.d -c $in -o $out $cl_flags"
def link_exe_ninja(self) -> str:
return " command = g++.exe -o $out $in $link_flags"
def link_dll_ninja(self) -> str:
return " command = g++.exe -shared -o $out $in $link_flags"
def find_headers(self, obj: Obj) -> List[str]:
cmd = f"g++.exe -MM {obj.cmp_flags} {obj.source}"
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode != 0:
print(res)
raise RuntimeError("Failed generating headers")
line = res.stdout.decode().replace("\\\r\n", "")
return line.split()[2:]
def link_exe(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"g++.exe -o {exe.product} {exe.link_flags} {row}"
def link_dll(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"g++.exe -shared -o {exe.product} {exe.link_flags} {row}"
def include(self, directory: str) -> str:
return f"-I{directory}"
def libpath(self, directory: str) -> str:
return f"-L{directory}"
def define(self, key: str, val: Optional[str]) -> str:
if val:
return f"-D{key}={val}"
return f"-D{key}"
def import_lib_cmd(self, out: str, dll: str, symbols: List[str]) -> Cmd:
def_path = BUILD_DIR / pathlib.PurePath(out).with_suffix(".def").name
rel_def_path = def_path.relative_to(BUILD_DIR)
def_file = Command(str(rel_def_path), f"python comp.py --deffile {def_path} {' '.join(symbols)}")
out_path = BUILD_DIR / out
cmd = f"dlltool.exe -D {dll} -d {def_path} -l {out_path}"
return Command(out, cmd, def_file)
class Msvc(BackendBase):
name = "msvc"
@property
def msvc(self) -> bool:
return True
def handle_obj_line(self, obj: Obj, line: str) -> bool:
if obj.headers is None:
obj.headers = dict()
if line.startswith("Note: including file"):
path = pathlib.Path(line[21:].strip()).resolve()
try:
path = path.relative_to(WORKDIR)
obj.headers[str(path)] = None
except ValueError:
pass
return True
return False
def compile_obj(self, obj: Obj) -> str:
return f"cl.exe /c /nologo /showIncludes /Fo:{obj.product} {obj.cmp_flags} {obj.source}"
def compile_obj_ninja(self) -> str:
return " deps = msvc\n" + \
" command = cl.exe /nologo /showIncludes /c $in /Fo:$out $cl_flags"
def link_exe_ninja(self) -> str:
return " command = link.exe /nologo /OUT:$out $link_flags $in"
def link_dll_ninja(self) -> str:
return " command = link.exe /nologo /OUT:$out /DLL $link_flags $in"
def find_headers(self, obj: Obj) -> List[str]:
cl = shutil.which("cl.exe")
if cl is None:
raise RuntimeError("Failed to find cl.exe executable")
cmd = f"{cl} /P /showIncludes /FiNUL {obj.cmp_flags} {obj.source}"
print(cmd)
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode != 0:
print(res, file=sys.stderr)
raise RuntimeError("Failed generating headers")
headers = dict()
for line in res.stderr.decode().replace("\r\n", "\n").split("\n"):
if not line.startswith("Note: including file:"):
continue
path = pathlib.Path(line[21:].strip()).resolve()
try:
path = path.relative_to(WORKDIR)
headers[str(path)] = None
except ValueError:
continue
return list(headers.keys())
def link_exe(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"link /nologo /OUT:{exe.product} {exe.link_flags} {row}"
def link_dll(self, exe: Exe) -> str:
row = " ".join([f"{obj.product}" for obj in exe.objs] + [f"{cmd.product}" for cmd in exe.cmds])
return f"link /nologo /OUT:{exe.product} /DLL {exe.link_flags} {row}"
def include(self, directory: str) -> str:
return f"/I{directory}"
def libpath(self, directory: str) -> str:
return f"/LIBPATH:{directory}"
def define(self, key: str, val: Optional[str]) -> str:
if val:
return f"/D{key}={val}"
return f"/D{key}"
def import_lib_cmd(self, out: str, dll: str, symbols: List[str]) -> Cmd:
exports = " ".join([f"/EXPORT:{s}={s}" for s in symbols])
cmd = f"lib /NOLOGO /MACHINE:X64 /DEF /OUT:{BUILD_DIR / out} /NAME:{dll} {exports}"
return Command(out, cmd)
def Object(name: str, source: Union[str, pathlib.PurePath],
cmp_flags: Optional[str]=None, namespace: str="",
depends: List[Union[str, Product]]=[],
defines: List[Union[str, Tuple[str, str]]]=[],
includes: List[str]=[], extra_cmp_flags: Optional[str]=None,
packages: List[Package]=[],
context: Optional[Context]=g_context) -> Obj:
if context is None:
context = g_context
if cmp_flags is None:
cmp_flags = backend().clflags
if context is not None:
defines = defines + context.defines
includes = includes + context.includes
if not namespace:
namespace = context.namespace
for pkg in packages:
cmp_flags += " " + pkg.compile_flags
if extra_cmp_flags is not None:
cmp_flags += " " + extra_cmp_flags
for define in defines:
if isinstance(define, tuple):
key, val = define
else:
key, val = define, None
cmp_flags += " " + backend().define(key, val)
for directory in includes:
cmp_flags += " " + backend().include(str(pathlib.PurePath(directory)))
source = str(pathlib.PurePath(source))
if namespace:
name = str(pathlib.PurePath(namespace) / name)
obj = Obj(name, source, cmp_flags, namespace, depends)
if namespace:
DIRECTORIES[str(BUILD_DIR / namespace)] = None
if name in OBJECTS:
other = OBJECTS[name]
if other.cmp_flags == obj.cmp_flags and other.source == obj.source:
return other
raise RuntimeError(f"Duplicate object {name}")
OBJECTS[name] = obj
return obj
def Executable(name: str,
*sources: Union[str, pathlib.PurePath, Obj, Cmd],
cmp_flags: Optional[str]=None,
link_flags: Optional[str]=None,
namespace: str="", dll: bool=False,
extra_link_flags: Optional[str]=None,
defines: List[Union[str, Tuple[str, str]]]=[],
includes: List[str]=[],
directory: Optional[str]=None,
group: Optional[str]=None,
packages: List[Package]=[],
context: Optional[Context]=None) -> Exe:
if context is None:
context = g_context
if cmp_flags is None:
cmp_flags = backend().clflags
if link_flags is None:
link_flags = backend().linkflags
if extra_link_flags is not None:
link_flags += " " + extra_link_flags
for pkg in packages:
if pkg.compile_flags:
cmp_flags += " " + pkg.compile_flags
if pkg.link_flags:
link_flags += " " + pkg.link_flags
if pkg.sources:
sources = sources + tuple(pkg.sources)
objs = []
cmds = []
for src in sources:
if isinstance(src, (str, pathlib.PurePath)):
obj_name = str(pathlib.PurePath(src).with_suffix('.obj').name)
obj = Object(obj_name, src, cmp_flags, namespace,
defines=defines,
includes=includes, context=context)
else:
if isinstance(src, Cmd):
if src not in cmds:
cmds.append(src)
continue
assert isinstance(src, Obj)
obj = src
if obj not in objs:
objs.append(obj)
if directory is None:
if context is not None:
directory = context.directory or str(BIN_DIR)
else:
directory = str(BIN_DIR)
else:
directory = str(pathlib.PurePath(directory))
if group is None:
if context is not None:
group = context.group or "all"
else:
group = "all"
exe = Exe(name, objs, cmds, link_flags, dll, directory)
if exe.product in EXECUTABLES:
raise RuntimeError(f"Duplicate exe {exe.product}")
EXECUTABLES[exe.product] = exe
if directory is not None:
DIRECTORIES[directory] = None
if group not in TARGET_GROUPS:
TARGET_GROUPS[group] = [exe]
else:
TARGET_GROUPS[group].append(exe)
return exe
@overload
def Command(output: List[str], cmd: str, *depends: Union[str, Product],
directory: Optional[str]=None) -> List[Cmd]:
...
@overload
def Command(output: str, cmd: str, *depends: Union[str, Product],
directory: Optional[str]=None) -> Cmd:
...
def Command(output: Union[str, List[str]], cmd: str, *depends: Union[str, Product],
directory: Optional[str]=None) -> Union[Cmd, List[Cmd]]:
if directory is None:
directory = str(BUILD_DIR)
else:
directory = str(pathlib.PurePath(directory))
DIRECTORIES[directory] = None
if isinstance(output, list):
assert len(output) > 1
command = Cmd(output, cmd, list(depends), directory)
if command.product in CUSTOMS:
raise RuntimeError(f"Duplicate command output {command.product}")
CUSTOMS[command.product] = command
if directory == str(BIN_DIR):
TARGET_GROUPS["all"].append(command)
res = [command]
for name in output[1:]:
dummy = DummyCmd(name, command, directory)
if dummy.product in CUSTOMS:
raise RuntimeError(f"Duplicate command output {command.product}")
CUSTOMS[dummy.product] = dummy
res.append(dummy)
return res
command = Cmd(output, cmd, list(depends), directory)
if command.product in CUSTOMS:
raise RuntimeError(f"Duplicate command {command.product}")
CUSTOMS[command.product] = command
if directory == str(BIN_DIR):
TARGET_GROUPS["all"].append(command)
return command
def CopyToBin(*src: str) -> List[Cmd]:
global use_copyto
use_copyto = True
res = []
for file in src:
f = pathlib.PurePath(file)
name = pathlib.PurePath(file).name
dest = BIN_DIR / name
cmd = Command(name, f"python comp.py --copyto {f} {dest}", str(f),
directory=str(BIN_DIR))
cmd.rule = "copyto"
res.append(cmd)
return res
def ImportLib(lib: str, dll: str, symbols: List[str]) -> Cmd:
return backend().import_lib_cmd(lib, dll, symbols)
Backend = Union[Msvc, Mingw, ZigCC, EmCC]
BACKEND: Backend
ACTIVE_BACKEND: str
def define(key: str, val: Optional[str]=None) -> str:
return backend().define(key, val)
COMPCACHE: Optional[dict[str, Any]] = None
def comp_cache() -> dict[str, Any]:
global COMPCACHE
if COMPCACHE is not None:
return COMPCACHE
import json
try:
with open(WORKDIR / 'comp_cache.json', 'r') as file:
COMPCACHE = json.load(file)
except Exception:
COMPCACHE = dict()
return COMPCACHE # type: ignore
def write_comp_cache() -> None:
try:
import json
with open(WORKDIR / 'comp_cache.json', 'w') as file:
file.write(json.dumps(COMPCACHE, indent=4))
except Exception as e:
print(f"Failed writing cache: {e}", file=sys.stderr)
def set_headers_and_last_cmd(obj: Obj) -> None:
try:
data = comp_cache()
if backend().name not in data:
data[backend().name] = dict()
if obj.name not in data[backend().name]:
data[backend().name][obj.name] = {"cmd": obj.cmd, "headers": obj.headers}
else:
data[backend().name][obj.name]['cmd'] = obj.cmd
data[backend().name][obj.name]['headers'] = obj.headers and list(obj.headers.keys())
except Exception as e:
print(f"Failed updating cache: {e}", file=sys.stderr)
def set_last_cmd(obj: Product) -> None:
try:
data = comp_cache()
if backend().name not in data:
data[backend().name] = dict()
if obj.name not in data[backend().name]:
data[backend().name][obj.name] = {"cmd": obj.cmd}
else:
data[backend().name][obj.name]['cmd'] = obj.cmd
except Exception as e:
print(f"Failed updating cache: {e}", file=sys.stderr)
def last_cmd(prod: Product) -> Optional[str]:
try:
data = comp_cache()
cache = data[backend().name][prod.name]
if isinstance(cache['cmd'], str):
return cache['cmd']
except Exception:
pass
return None
def find_headers(obj: Obj) -> Optional[Dict[str, None]]:
data = None
try:
data = comp_cache()
cache = data[backend().name][obj.name]
#assert 'stamp' in cache and isinstance(cache['stamp'], float)
assert 'headers' in cache and isinstance(cache['headers'], list)
return {h: None for h in cache['headers']}
t = os.path.getmtime(obj.source)
for header in cache['headers']:
assert isinstance(header, str)
stamp = os.path.getmtime(header)
if stamp > t:
t = stamp
if t < cache['stamp']:
return cache['headers']
except Exception:
cache = None
return None
created: List[pathlib.Path] = []
for dep in obj.depends:
path = pathlib.Path(dep)
if path.suffix in [".h", ".hpp", ".inl"]:
if not path.exists():
try:
path.touch()
created.append(path)
except Exception:
pass
try:
headers = backend().find_headers(obj)
for ix in range(len(headers)):
header = headers[ix]
try:
header = str(pathlib.Path(header).resolve().relative_to(WORKDIR))
headers[ix] = header
except Exception:
pass
finally:
for path in created:
try:
path.unlink()
except Exception:
pass
if data is None:
data = dict()
if backend().name not in data:
data[backend().name] = dict()
data[backend().name][obj.name] = {'stamp': time.time(), 'headers': headers}
try:
with open('comp_cache.json', 'w') as file:
json.dump(data, file, indent=4)
except Exception:
pass
return headers
def resolve_dirs() -> None:
dirs = list(DIRECTORIES.keys())
while len(dirs) > 0:
d = dirs.pop()
path = pathlib.PurePath(d)
parents = [str(p) for p in path.parents[:-1]]
for parent in parents:
DIRECTORIES[parent] = None
dirs.extend(parents)
def ninjafile(dest: TextIO = sys.stdout) -> None:
resolve_dirs()
def esc(s : Union[str, pathlib.Path]) -> str:
return str(s).replace(" ", "$ ").replace(":", "$:")
print("ninja_required_version = 1.5", file=dest)
print(f"builddir = {esc(backend().builddir)}\n", file=dest)
print(f"link_flags = {backend().linkflags}\n", file=dest)
for group, targets in TARGET_GROUPS.items():
deps = " ".join(f'{esc(p.product)}' for p in targets)
print(f"build {group}: phony {deps}\n", file=dest)
if len(OBJECTS) > 0:
print(f"cl_flags = {backend().clflags}", file=dest)
print(f"rule cl", file=dest)
print(backend().compile_obj_ninja(), file=dest)
if len(EXECUTABLES) > 0:
print(f"link_flags = {backend().linkflags}", file=dest)
print("rule link_exe", file=dest)
print(backend().link_exe_ninja(), file=dest)
if any(True for a in EXECUTABLES.values() if a.dll):
print("rule link_dll", file=dest)
print(backend().link_dll_ninja(), file=dest)
if use_copyto:
print(f"rule copyto", file=dest)
print(" command = python comp.py --copyto $in $out", file=dest)
print(file=dest)
for key, obj in OBJECTS.items():
print(f"build {esc(obj.product)}: cl {esc(obj.source)}", end="", file=dest)
if obj.depends:
deps = ' '.join((esc(d) for d in obj.depends))
print(f" || {deps}", file=dest)
else:
print(file=dest)
if obj.cmp_flags != backend().clflags:
print(f" cl_flags = {obj.cmp_flags}", file=dest)
print(file=dest)
for key, exe in EXECUTABLES.items():
row = " ".join([f"{esc(obj.product)}" for obj in exe.objs] +
[f"{esc(cmd.product)}" for cmd in exe.cmds])
if exe.dll:
print(f"build {esc(exe.product)}: link_dll {row}", file=dest)
else:
print(f"build {esc(exe.product)}: link_exe {row}", file=dest)
if exe.link_flags != backend().linkflags:
print(f" link_flags = {exe.link_flags}", file=dest)
print(file=dest)
ix = 0
for key, obj in CUSTOMS.items():
if isinstance(obj, DummyCmd):
continue
if obj.rule is not None:
rule = obj.rule
else:
rule = f"custom_{ix}"
ix += 1
print(f"rule {rule}", file=dest)
print(f" command = {obj.cmd}", file=dest)
prods = ' '.join(esc(p) for p in obj.all_products)
deps = ' '.join(esc(d) for d in obj.depends)
print(f"build {prods}: {rule} {deps}", file=dest)
def makefile(dest: TextIO = sys.stdout) -> None:
resolve_dirs()
for obj in OBJECTS.values():
p = pathlib.Path(obj.source).resolve()
if not p.is_file():
raise RuntimeError(f"File '{p}' does not exist")
for group, targets in TARGET_GROUPS.items():
print(f"{group}:", end="", file=dest)
for prod in targets:
print(" \\", file=dest)
print(f"\t{prod.product}", end="", file=dest)
print("\n", file=dest)
for d in DIRECTORIES:
path = pathlib.PurePath(d)
parents = [str(p) for p in path.parents[:-1]]
if len(parents) > 0:
parent_str = " " + " ".join(parents)
else:
parent_str = ""
print(f"{path}:{parent_str}\n\t-@ if NOT EXIST \"{path}\" mkdir \"{path}\"", file=dest)
print(file=dest)
for key, obj in OBJECTS.items():
if obj.namespace:
bld_dir = str(BUILD_DIR / obj.namespace)
else:
bld_dir = str(BUILD_DIR)
if obj.headers is None or last_cmd(obj) != obj.cmd:
headers = [str(BUILD_DIR / "Makefile")]
else:
headers = list(obj.headers.keys())
depends = " ".join([obj.source] + headers + [bld_dir] + obj.depends)
print(f"{obj.product}: {depends}", file=dest)
print(f"\t{obj.cmd}", file=dest)
print(file=dest)
for key, exe in EXECUTABLES.items():
row = " ".join([f"{obj.product}" for obj in exe.objs] +
[f"{cmd.product}" for cmd in exe.cmds] +
([str(BUILD_DIR / "Makefile")] if last_cmd(exe) != exe.cmd else []))
print(f"{exe.product}: {row} {exe.directory}", file=dest)
print(f"\t{exe.cmd}", file=dest)
print(file=dest)
for key, cmd in CUSTOMS.items():
row = " ".join(cmd.depends + [cmd.dir] + ([str(BUILD_DIR / "Makefile")]
if last_cmd(cmd) != cmd.cmd else []))
print(f"{cmd.product}: {row}", file=dest)
print(f"\t{cmd.cmd}", file=dest)
print(file=dest)
print(f"clean:\n\tdel /S /Q {BUILD_DIR}\\*\n\tdel /S /Q {BIN_DIR}\\*", file=dest)
def compile_commands(dest: TextIO = sys.stdout) -> None:
directory = str(WORKDIR.resolve())
res = []
for obj in OBJECTS.values():
json = dict()
json["directory"] = directory
json["command"] = backend().compile_obj(obj)
json["file"] = str(pathlib.Path(obj.source).resolve())
res.append(json)
import json
print(json.dumps(res, indent=4), file=dest)
all_backends: Dict[str, Type['Backend']] = {"msvc": Msvc, "mingw": Mingw, 'zigcc': ZigCC, 'emcc': EmCC}
active_backends: Dict[str, 'Backend'] = {}
args = None
parser = None
def get_parser() -> 'argparse.ArgumentParser':
global parser
if parser is None:
parser = argparse.ArgumentParser(description="Generate makefile and compile commands.")
parser.add_argument("--compiledb", "-c", action="store_true",
help="Generate compile commands database instead of Makefile")
parser.add_argument("--backend", "-b", choices = [key for key in active_backends],
type=str.lower,
default=None, help="Specify the backend to use.")
parser.add_argument("--target", "-t", help="Specify target to build", action="store",
default="all")
parser.add_argument("--fast", "-f", help="Speed up by not validating header cache",
action="store_true", default=False)
parser.add_argument("--deffile", help="Generate a .def file", action="store",
nargs="+", default=None)
parser.add_argument("--copyto", help="Copy file", action="store",
nargs="+", default=None)
parser.add_argument("--make", help="Use nmake instead of ninja", action="store_true",
default=False)
return parser
def get_args() -> 'argparse.Namespace':
global args
if args is None:
parser = get_parser()
args = parser.parse_args()
return args
def add_backend(name: str, backend_name: str, builddir: str, bindir: str,
workdir: pathlib.Path, clflags: str, linkflags: str) -> None:
if backend_name.lower() not in all_backends:
raise RuntimeError("Invalid backend")
backend = all_backends[name.lower()]()
backend.builddir = pathlib.Path(builddir)
backend.bindir = pathlib.Path(bindir)
backend.workdir = workdir
backend.clflags = clflags
backend.linkflags = linkflags
active_backends[name.lower()] = backend
def set_backend(name: str) -> None:
global BUILD_DIR, BIN_DIR, WORKDIR, BACKEND, ACTIVE_BACKEND
backend = get_args().backend
if backend is not None:
name = backend
if name.lower() not in active_backends:
raise RuntimeError("Invalid backend")
backend = active_backends[name.lower()]
BUILD_DIR = backend.builddir
BIN_DIR = backend.bindir
WORKDIR = backend.workdir
DIRECTORIES[str(BUILD_DIR)] = None
DIRECTORIES[str(BIN_DIR)] = None
TARGET_GROUPS["all"] = []
BACKEND = backend
ACTIVE_BACKEND = name
os.chdir(WORKDIR)
run = True
if get_args().deffile:
run = False
args = get_args().deffile
dest = args[0]
with open(dest, 'w') as file:
print("EXPORTS", file=file)
for arg in args[1:]:
print(arg, file=file)
elif get_args().copyto:
run = False
args = get_args().copyto
if len(args) != 2:
print("Invalid copyto args", file=sys.stderr)
exit(1)
shutil.copy2(args[0], args[1])
if not run:
exit(0)
return
def backend() -> Backend:
return BACKEND
def build_dir() -> str:
return str(BUILD_DIR)
def bin_dir() -> str:
return str(BIN_DIR)
def generate() -> None:
if get_args().compiledb:
compile_commands()
else:
makefile()
def get_make() -> Tuple[str, List[str]]:
if get_args().make:
s = 'nmake.exe'
args = ['/NOLOGO', get_args().target]
else:
s = 'ninja.exe'
if get_args().target == 'clean':
args = ['-t', 'clean']
else:
args = [get_args().target]
make = shutil.which(s)
if make is None:
raise RuntimeError(f"Could not find {s}")
return make, args