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