#!/usr/bin/env python3 from argparse import ArgumentParser from ninja.ninja_syntax import Writer as Ninja from os import getenv from pathlib import Path def main(): arg_parser = ArgumentParser() arg_parser.add_argument("arch_name") arg_parser.add_argument("board_name") arg_parser.add_argument("libkernel") arg_parser.add_argument("srcdir") args = arg_parser.parse_args() libkernel = Path(args.libkernel).resolve() / "lib/libvernos_kernel.a" srcdir = Path(args.srcdir).resolve() objdir = Path("obj") objdir.mkdir() linker_script = str(srcdir / f"{args.board_name}.ld") objs: list[str] = [] with open("build.ninja", "w") as f: ninja = Ninja(f) # Inherit some environment variables. for var in ["AS", "ASFLAGS", "LD", "LDFLAGS"]: ninja.variable(var, getenv(var)) # Add rules to assemble and link. ninja.rule("as", "$AS $ASFLAGS -g -o $out $in") ninja.rule("ld", f"$LD $LDFLAGS -T{linker_script} -o $out $in") for path in srcdir.iterdir(): if path.suffix != ".s": continue obj = str(objdir / f"{path.stem}.o") ninja.build(obj, "as", str(path)) objs.append(obj) ninja.build("kernel.elf", "ld", objs + [str(libkernel)]) if __name__ == "__main__": main()