summaryrefslogtreecommitdiff
path: root/tools/mkdeps.py
blob: 89df9d8e93b090792604f61a615cc51672466d69 (plain)
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
#!/usr/bin/env python3

import subprocess
import glob
import os
import sys
import re
from argparse import ArgumentParser

__version__ = "0.1"


def main():

    FIX_RE = r"([^:]+:)"

    parser = ArgumentParser(description="mkdeps for SDCC",
                            epilog="Copyright (C) 2021 Juan J Martinez <jjm@usebox.net>",
                            )

    parser.add_argument(
        "--version", action="version", version="%(prog)s " + __version__)

    parser.add_argument("-b", "--build-dir", dest="build", default=None, type=str,
                        help="Directory to build")
    parser.add_argument("-I", "--include", dest="include", default=None, type=str,
                        help="Include directory (for multiple directories, use : as separator)")
    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
                        help="Enable verbose output")

    parser.add_argument("dir", help="Directory to scan")
    parser.add_argument(
        "deps", help="Dependencies file (to include in Makefile)")

    args = parser.parse_args()

    fix_path = args.dir if args.build is None else args.build
    fix_path = r"%s\1" % (fix_path.strip("/") + "/").replace("\\", "\\\\")
    inc = ["-I%s" % d for d in args.include.split(":")]
    cmd = ["sdcc", "-MM"] + inc
    result = []
    for d in sorted(glob.glob(os.path.join(args.dir, "*.c"))):
        if args.verbose:
            print("running %r" % ' '.join(cmd + [d]))
        out = subprocess.run(cmd + [d], stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        if out.returncode:
            sys.exit("Error: %s" % out.stderr)

        out = out.stdout.decode('utf-8').strip("\n\r")
        result.append(re.sub(FIX_RE, fix_path, out))

    try:
        old = open(args.deps, "rt").read()
    except Exception:
        if args.verbose:
            print("%r not found, will generate" % args.deps)
        old = None

    new = '\n'.join(result)

    if new != old:
        with open(args.deps, "wt") as fd:
            fd.write(new)
    else:
        if args.verbose:
            print("%r didn't change, skipping" % args.deps)


if __name__ == "__main__":
    main()