this repo has no description
at trunk 68 lines 1.7 kB view raw
1#!/usr/bin/env python3 2class Gen: 3 def __init__(self, stream): 4 self.stream = stream 5 6 def writeline(self, line): 7 self.stream.write(line) 8 self.stream.write("\n") 9 10 def rule(self, name, **kwargs): 11 self.writeline(f"rule {name}") 12 if "description" not in kwargs: 13 kwargs["description"] = f"{name} $out" 14 for key, value in kwargs.items(): 15 self.writeline(f" {key} = {value}") 16 self.writeline("") 17 18 def var(self, name, value): 19 self.writeline(f"{name} = {value}") 20 21 def __getattr__(self, name): 22 def rule(output, input): 23 self.writeline(f"build {output}: {name} {input}") 24 25 return rule 26 27 28def remove_ext(name): 29 return name.rpartition(".")[0] 30 31 32CC = "gcc" 33CFLAGS = "-O0 -g -Wall -Wextra -pedantic -fno-strict-aliasing -std=c99" 34OUTDIR = "bin" 35SRCS = [ 36 "mmap-demo.c", 37 "compiling-integers.c", 38 "compiling-immediates.c", 39 "compiling-unary.c", 40 "compiling-binary.c", 41 "compiling-reader.c", 42 "compiling-let.c", 43 "compiling-if.c", 44 "compiling-heap.c", 45 "compiling-procedures.c", 46 "compiling-closures.c", 47 "compiling-elf.c", 48] 49NINJA = "build.ninja" 50BINS = {src: f"./{OUTDIR}/{remove_ext(src)}" for src in SRCS} 51 52with open(NINJA, "w+") as f: 53 g = Gen(f) 54 g.var("CC", CC) 55 g.var("CFLAGS", CFLAGS) 56 g.rule("CC", command="$CC $CFLAGS ${opts} $in -o $out") 57 g.rule( 58 "REGEN", 59 command=f"python3 {__file__}", 60 description="Regenerating build.ninja...", 61 ) 62 63 for src in SRCS: 64 g.CC(BINS[src], src) 65 66 g.REGEN(NINJA, f"| {__file__}") 67 g.phony("all", " ".join(BINS.values())) 68 g.writeline("default all")