# vim: ft=python

# Normally I prefer listing every source file so that ninja can pick
# up on added files and regenerate appropriately, but libc has _so_
# many files that it's unweildy. So if a file is added or removed in
# libc, remember to run configure again.

def glob(ext, root=''):
    from glob import glob
    from pathlib import Path

    base = Path(module_root) / root

    def resolve(path):
        return str(Path(path).relative_to(base))

    return list(map(resolve, glob(f"{base}/**/*.{ext}", recursive=True)))

sources = []
for ext in ("c", "cpp", "s", "inc"):
    sources += [f for f in glob(ext) + glob(ext + ".cog") if not "crt" in f]

headers = []
for ext in ("h",):
    headers += glob(ext, "include") + glob(ext + ".cog", "include")

libc = module("libc",
    kind = "lib",
    deps = [ "j6", "crt0.o" ],
    basename = "libc",
    include_phase = "late",
    sources = sources,
    public_headers = headers,
    )

libc.variables["ccflags"] = [
    "${ccflags}",
    "-fvisibility=default",
    "-DPRINTF_SUPPORT_DECIMAL_SPECIFIERS=0",
    "-DPRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS=0",
]

crt = module("crt0.o",
    kind = "cp",
    sources = [
        "arch/amd64/crt/crt0.s",
    ])
