xref: /aosp_15_r20/external/cronet/build/config/siso/cros.star (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# -*- bazel-starlark -*-
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Siso configuration for ChromeOS builds."""
6
7load("@builtin//lib/gn.star", "gn")
8load("@builtin//path.star", "path")
9load("@builtin//struct.star", "module")
10
11def __custom_toolchain(ctx):
12    if not "args.gn" in ctx.metadata:
13        print("no args.gn")
14        return None
15    gn_args = gn.args(ctx)
16    if gn_args.get("target_os") != '"chromeos"':
17        return None
18    if not "cros_target_cxx" in gn_args:
19        print("no cros_target_cxx")
20        return None
21    cros_target_cxx = gn_args.get("cros_target_cxx")
22    cros_target_cxx = cros_target_cxx.strip('"')
23    cros_target_cxx = ctx.fs.canonpath(cros_target_cxx)
24    toolchain = path.dir(path.dir(cros_target_cxx))
25    if not toolchain:
26        fail("failed to detect cros custom toolchain. cros_target_cxx = %s" % gn_args.get("cros_target_cxx"))
27    return toolchain
28
29def __custom_sysroot(ctx):
30    if not "args.gn" in ctx.metadata:
31        print("no args.gn")
32        return None
33    gn_args = gn.args(ctx)
34    if gn_args.get("target_os") != '"chromeos"':
35        return None
36    if not "target_sysroot" in gn_args:
37        print("no target_sysroot")
38        return None
39    sysroot = gn_args.get("target_sysroot")
40    sysroot = sysroot.strip('"')
41    sysroot = ctx.fs.canonpath(sysroot)
42    if not sysroot:
43        fail("failed to detect cros custom sysroot. target_sysroot = %s" % gn_args.get("target_sysroot"))
44    return sysroot
45
46def __filegroups(ctx):
47    fg = {}
48    toolchain = __custom_toolchain(ctx)
49    print("toolchain = %s" % toolchain)
50    if toolchain:
51        fg[toolchain + ":headers"] = {
52            "type": "glob",
53            "includes": ["*"],
54        }
55    sysroot = __custom_sysroot(ctx)
56    print("sysroot = %s" % sysroot)
57    if sysroot:
58        fg[path.join(sysroot, "usr/include") + ":include"] = {
59            "type": "glob",
60            "includes": ["*"],
61            # needs bits/stab.def, c++/*
62        }
63        fg[path.join(sysroot, "usr/lib") + ":headers"] = {
64            "type": "glob",
65            "includes": ["*.h", "crtbegin.o"],
66        }
67        fg[path.join(sysroot, "usr/lib64") + ":headers"] = {
68            "type": "glob",
69            "includes": ["*.h"],
70        }
71    print(fg)
72    return fg
73
74def __cros_compiler(ctx, cmd):
75    tool_inputs = cmd.tool_inputs
76    for i, arg in enumerate(cmd.args):
77        if arg.startswith("-fprofile-sample-use="):
78            # profile data is in ninja input (direct or indirect),
79            # but siso doesn't include ninja inputs for deps=gcc
80            # (it would include lots of unnecessary inputs)
81            # so just add profdata by checking command line flag.
82            profdata = ctx.fs.canonpath(arg.removeprefix("-fprofile-sample-use="))
83            tool_inputs.append(profdata)
84    ctx.actions.fix(tool_inputs = tool_inputs)
85
86__handlers = {
87    "cros_compiler": __cros_compiler,
88}
89
90def __step_config(ctx, step_config):
91    if __custom_toolchain(ctx):
92        step_config["rules"].extend([
93            {
94                "name": "clang-cros/cxx",
95                "action": "(.*_)?cxx",
96                "command_prefix": "../../build/cros_cache/chrome-sdk/",
97                "remote": True,
98                "handler": "cros_compiler",
99                "canonicalize_dir": True,
100                "timeout": "5m",
101            },
102            {
103                "name": "clang-cros/cc",
104                "action": "(.*_)?cc",
105                "command_prefix": "../../build/cros_cache/chrome-sdk/",
106                "remote": True,
107                "handler": "cros_compiler",
108                "canonicalize_dir": True,
109                "timeout": "5m",
110            },
111        ])
112    sysroot = __custom_sysroot(ctx)
113    if sysroot:
114        step_config["input_deps"].update({
115            sysroot + ":headers": [
116                path.join(sysroot, "usr/include") + ":include",
117                path.join(sysroot, "usr/lib") + ":headers",
118                path.join(sysroot, "usr/lib64") + ":headers",
119            ],
120        })
121    return step_config
122
123cros = module(
124    "cros",
125    custom_toolchain = __custom_toolchain,
126    custom_sysroot = __custom_sysroot,
127    filegroups = __filegroups,
128    handlers = __handlers,
129    step_config = __step_config,
130)
131