# build-aux/measurestack/__init__.py - Analyze stack sizes for compiled objects
#
# Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

import re
import sys

from . import app_main

# pylint: disable=unused-variable
__all__ = [
    "main",
]


re_c_obj_suffix = re.compile(r"\.c\.(?:o|obj)$")


def main() -> None:
    pico_platform = sys.argv[1]
    base_dir = sys.argv[2]
    obj_fnames = set(sys.argv[3:])

    c_fnames: set[str] = set()
    ci_fnames: set[str] = set()
    for obj_fname in obj_fnames:
        if re_c_obj_suffix.search(obj_fname):
            ci_fnames.add(re_c_obj_suffix.sub(".c.ci", obj_fname))
            with open(obj_fname + ".d", "r", encoding="utf-8") as fh:
                c_fnames.update(fh.read().replace("\\\n", " ").split(":")[-1].split())

    app_main.main(
        arg_pico_platform=pico_platform,
        arg_base_dir=base_dir,
        arg_ci_fnames=ci_fnames,
        arg_c_fnames=c_fnames,
    )