xref: /aosp_15_r20/external/angle/build/config/siso/devtools_frontend.star (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1load("@builtin//encoding.star", "json")
2load("@builtin//path.star", "path")
3load("@builtin//struct.star", "module")
4load("./config.star", "config")
5load("./tsc.star", "tsc")
6
7# TODO: crbug.com/1478909 - Specify typescript inputs in GN config.
8def __filegroups(ctx):
9    return {
10        "third_party/devtools-frontend/src/node_modules/typescript:typescript": {
11            "type": "glob",
12            "includes": ["*"],
13        },
14        "third_party/devtools-frontend/src/node_modules:node_modules": {
15            "type": "glob",
16            "includes": ["*.js", "*.json", "*.ts"],
17        },
18    }
19
20def __step_config(ctx, step_config):
21    step_config["input_deps"].update({
22        "third_party/devtools-frontend/src/third_party/typescript/ts_library.py": [
23            "third_party/devtools-frontend/src/node_modules/typescript:typescript",
24            "third_party/devtools-frontend/src/node_modules:node_modules",
25        ],
26    })
27
28    # TODO: b/308405411 - Enable remote-devtools-frontend-typescript by default.
29    if config.get(ctx, "remote-devtools-frontend-typescript"):
30        step_config["rules"].extend([
31            {
32                "name": "devtools-frontend/typescript/ts_library",
33                "command_prefix": "python3 ../../third_party/devtools-frontend/src/third_party/typescript/ts_library.py",
34                "exclude_input_patterns": [
35                    "*.stamp",
36                ],
37                "remote": True,
38                "handler": "devtools_frontend/typescript_ts_library",
39                "output_local": True,
40                "timeout": "2m",
41            },
42        ])
43    return step_config
44
45def _ts_library(ctx, cmd):
46    # Handler for https://crsrc.org/c/third_party/devtools-frontend/src/third_party/typescript/ts_library.py
47    # Note that this is a different script from https://crsrc.org/c/tools/typescript/ts_library.py
48    deps = []
49    sources = []
50    tsconfig_path = None
51    flag = None
52    for i, arg in enumerate(cmd.args):
53        if flag != "" and arg.startswith("-"):
54            flag = ""
55        if arg == "--tsconfig_output_location":
56            tsconfig_path = ctx.fs.canonpath(cmd.args[i + 1])
57            continue
58        if arg in ("--deps", "--sources"):
59            flag = arg
60            continue
61        if flag == "--deps":
62            deps.append(arg)
63            continue
64        if flag == "--sources":
65            sources.append(ctx.fs.canonpath(arg))
66            continue
67    if not tsconfig_path:
68        fail("missing --tsconfig_output_location")
69    tsconfig = {"files": [], "references": []}
70    tsconfig_dir = path.dir(tsconfig_path)
71    for s in sources:
72        tsconfig["files"].append(path.rel(tsconfig_dir, s))
73    for d in deps:
74        tsconfig["references"].append({"path": d})
75        refpath = path.join(tsconfig_dir, d)
76        refdir = path.dir(refpath)
77
78        # TODO: crbug.com/1503020 - Fix devtools_entrypoint to propagate .d.ts output.
79        dpath = path.join(refdir, path.base(refdir) + ".d.ts")
80        if ctx.fs.exists(dpath):
81            sources.append(dpath)
82
83    inputs = tsc.scandeps(ctx, tsconfig_path, tsconfig)
84
85    # Sources and imported files might be located in different dirs. source vs gen.
86    # Try to collect the corresponding files in source or gen dir.
87    # TODO: crbug.com/1505319 - Fix devtools_module import issues.
88    files = {}
89    gen_dir = None
90
91    # Infer source files from gen file.
92    for f in cmd.inputs + inputs:
93        if f.startswith("out/"):
94            # Remove out/{subdir}/gen.
95            splits = f.split("/", 3)
96            if len(splits) < 4:
97                continue
98            gen_dir = path.join(splits[0], splits[1], splits[2])
99            f = splits[3]
100            if ctx.fs.exists(f) and not f in files:
101                files[f] = True
102                continue
103            if f.endswith(".js"):
104                f = f.removesuffix(".js") + ".d.ts"
105                if ctx.fs.exists(f) and not f in files:
106                    files[f] = True
107
108    # Infer gen files from source file.
109    if gen_dir:
110        for f in cmd.inputs + inputs:
111            if f.endswith(".ts"):
112                f = path.join(gen_dir, f)
113                f = f.removesuffix(".ts") + ".d.ts"
114                if ctx.fs.exists(f) and not f in files:
115                    files[f] = True
116            if f.endswith(".js"):
117                f = path.join(gen_dir, f)
118                f = f.removesuffix(".js") + ".d.ts"
119                if ctx.fs.exists(f) and not f in files:
120                    files[f] = True
121
122    ctx.actions.fix(inputs = cmd.inputs + inputs + sources + files.keys())
123
124devtools_frontend = module(
125    "devtools_frontend",
126    step_config = __step_config,
127    handlers = {
128        "devtools_frontend/typescript_ts_library": _ts_library,
129    },
130    filegroups = __filegroups,
131)
132