xref: /aosp_15_r20/external/cronet/build/config/siso/devtools_frontend.star (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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            },
41        ])
42    return step_config
43
44def _ts_library(ctx, cmd):
45    # Handler for https://crsrc.org/c/third_party/devtools-frontend/src/third_party/typescript/ts_library.py
46    # Note that this is a different script from https://crsrc.org/c/tools/typescript/ts_library.py
47    deps = []
48    sources = []
49    tsconfig_path = None
50    flag = None
51    for i, arg in enumerate(cmd.args):
52        if flag != "" and arg.startswith("-"):
53            flag = ""
54        if arg == "--tsconfig_output_location":
55            tsconfig_path = ctx.fs.canonpath(cmd.args[i + 1])
56            continue
57        if arg in ("--deps", "--sources"):
58            flag = arg
59            continue
60        if flag == "--deps":
61            deps.append(arg)
62            continue
63        if flag == "--sources":
64            sources.append(ctx.fs.canonpath(arg))
65            continue
66    if not tsconfig_path:
67        fail("missing --tsconfig_output_location")
68    tsconfig = {"files": [], "references": []}
69    tsconfig_dir = path.dir(tsconfig_path)
70    for s in sources:
71        tsconfig["files"].append(path.rel(tsconfig_dir, s))
72    for d in deps:
73        tsconfig["references"].append({"path": d})
74        refpath = path.join(tsconfig_dir, d)
75        refdir = path.dir(refpath)
76
77        # TODO: crbug.com/1503020 - Fix devtools_entrypoint to propagate .d.ts output.
78        dpath = path.join(refdir, path.base(refdir) + ".d.ts")
79        if ctx.fs.exists(dpath):
80            sources.append(dpath)
81
82    inputs = tsc.scandeps(ctx, tsconfig_path, tsconfig)
83
84    # Sources and imported files might be located in different dirs. source vs gen.
85    # Try to collect the corresponding files in source or gen dir.
86    # TODO: crbug.com/1505319 - Fix devtools_module import issues.
87    files = {}
88    gen_dir = None
89
90    # Infer source files from gen file.
91    for f in cmd.inputs + inputs:
92        if f.startswith("out/"):
93            # Remove out/{subdir}/gen.
94            splits = f.split("/", 3)
95            if len(splits) < 4:
96                continue
97            gen_dir = path.join(splits[0], splits[1], splits[2])
98            f = splits[3]
99            if ctx.fs.exists(f) and not f in files:
100                files[f] = True
101                continue
102            if f.endswith(".js"):
103                f = f.removesuffix(".js") + ".d.ts"
104                if ctx.fs.exists(f) and not f in files:
105                    files[f] = True
106
107    # Infer gen files from source file.
108    if gen_dir:
109        for f in cmd.inputs + inputs:
110            if f.endswith(".ts"):
111                f = path.join(gen_dir, f)
112                f = f.removesuffix(".ts") + ".d.ts"
113                if ctx.fs.exists(f) and not f in files:
114                    files[f] = True
115            if f.endswith(".js"):
116                f = path.join(gen_dir, f)
117                f = f.removesuffix(".js") + ".d.ts"
118                if ctx.fs.exists(f) and not f in files:
119                    files[f] = True
120
121    ctx.actions.fix(inputs = cmd.inputs + inputs + sources + files.keys())
122
123devtools_frontend = module(
124    "devtools_frontend",
125    step_config = __step_config,
126    handlers = {
127        "devtools_frontend/typescript_ts_library": _ts_library,
128    },
129    filegroups = __filegroups,
130)
131