xref: /aosp_15_r20/external/pigweed/pw_system/py/console.bzl (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker"""Bazel macros for launching the pw_system console."""
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Workerload("@bazel_skylib//rules:native_binary.bzl", "native_binary")
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Workerdef device_simulator_console(
19*61c4878aSAndroid Build Coastguard Worker        name,
20*61c4878aSAndroid Build Coastguard Worker        script,
21*61c4878aSAndroid Build Coastguard Worker        host_binary,
22*61c4878aSAndroid Build Coastguard Worker        pw_console_config = None,
23*61c4878aSAndroid Build Coastguard Worker        socket_addr = "default",
24*61c4878aSAndroid Build Coastguard Worker        **kwargs):
25*61c4878aSAndroid Build Coastguard Worker    """Launch the host_binary and connect to it with the pw_system console.
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker    This macro is a wrapper around bazel_skylib native_binary to
28*61c4878aSAndroid Build Coastguard Worker    launch a device_sim.py script.
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker    Args:
31*61c4878aSAndroid Build Coastguard Worker      name: The name of this device_simulator_console rule.
32*61c4878aSAndroid Build Coastguard Worker      script: The py_binary target that calls upstream Pigweed's
33*61c4878aSAndroid Build Coastguard Worker        pw_console.device_sim main or main_with_compiled_protos. See:
34*61c4878aSAndroid Build Coastguard Worker        https://cs.opensource.google/pigweed/pigweed/+/main:pw_system/py/pw_system/device_sim.py
35*61c4878aSAndroid Build Coastguard Worker      host_binary: The host_device_simulator_binary target to run.
36*61c4878aSAndroid Build Coastguard Worker      socket_addr: Optional --socket-addr value to pass to pw-system-console.
37*61c4878aSAndroid Build Coastguard Worker      pw_console_config: Optional file group target containing a
38*61c4878aSAndroid Build Coastguard Worker        pw_console.yaml config file. For example:
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker          filegroup(
41*61c4878aSAndroid Build Coastguard Worker              name = "pw_console_config",
42*61c4878aSAndroid Build Coastguard Worker              srcs = [".pw_console.yaml"],
43*61c4878aSAndroid Build Coastguard Worker          )
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Worker      **kwargs: Passed to native_binary.
46*61c4878aSAndroid Build Coastguard Worker    """
47*61c4878aSAndroid Build Coastguard Worker    data = [host_binary]
48*61c4878aSAndroid Build Coastguard Worker    args = kwargs.get("args", []) + [
49*61c4878aSAndroid Build Coastguard Worker        "--sim-binary",
50*61c4878aSAndroid Build Coastguard Worker        "$(rootpath " + host_binary + ")",
51*61c4878aSAndroid Build Coastguard Worker        "--socket-addr",
52*61c4878aSAndroid Build Coastguard Worker        socket_addr,
53*61c4878aSAndroid Build Coastguard Worker    ]
54*61c4878aSAndroid Build Coastguard Worker    if pw_console_config:
55*61c4878aSAndroid Build Coastguard Worker        data.append(pw_console_config)
56*61c4878aSAndroid Build Coastguard Worker        args.append("--config-file")
57*61c4878aSAndroid Build Coastguard Worker        args.append("$(rootpath " + pw_console_config + ")")
58*61c4878aSAndroid Build Coastguard Worker
59*61c4878aSAndroid Build Coastguard Worker    native_binary(
60*61c4878aSAndroid Build Coastguard Worker        name = name,
61*61c4878aSAndroid Build Coastguard Worker        src = script,
62*61c4878aSAndroid Build Coastguard Worker        args = args,
63*61c4878aSAndroid Build Coastguard Worker        data = data,
64*61c4878aSAndroid Build Coastguard Worker        # Note: out is mandatory in older bazel-skylib versions.
65*61c4878aSAndroid Build Coastguard Worker        out = name + ".exe",
66*61c4878aSAndroid Build Coastguard Worker        **kwargs
67*61c4878aSAndroid Build Coastguard Worker    )
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Workerdef device_console(
70*61c4878aSAndroid Build Coastguard Worker        name,
71*61c4878aSAndroid Build Coastguard Worker        script,
72*61c4878aSAndroid Build Coastguard Worker        binary,
73*61c4878aSAndroid Build Coastguard Worker        baudrate = "115200",
74*61c4878aSAndroid Build Coastguard Worker        pw_console_config = None,
75*61c4878aSAndroid Build Coastguard Worker        **kwargs):
76*61c4878aSAndroid Build Coastguard Worker    """Connect to a device running the binary with the pw_system console.
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard Worker    This macro is a wrapper around bazel_skylib native_binary to
79*61c4878aSAndroid Build Coastguard Worker    launch the console.py script with the basic set of arguments.
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard Worker    Args:
82*61c4878aSAndroid Build Coastguard Worker      name: The name of this device_simulator_console rule.
83*61c4878aSAndroid Build Coastguard Worker      script: The py_binary target that calls upstream Pigweed's
84*61c4878aSAndroid Build Coastguard Worker        pw_system.console main or main_with_compiled_protos. See:
85*61c4878aSAndroid Build Coastguard Worker        https://cs.opensource.google/pigweed/pigweed/+/main:pw_system/py/pw_system/console.py
86*61c4878aSAndroid Build Coastguard Worker      binary: The binary to pass along to pw-system-console as the
87*61c4878aSAndroid Build Coastguard Worker        "--token-databases" argument.
88*61c4878aSAndroid Build Coastguard Worker      baudrate: Value to pass as the "--baudrate" arg to
89*61c4878aSAndroid Build Coastguard Worker        pw-system-console. Defaults to 115200.
90*61c4878aSAndroid Build Coastguard Worker      pw_console_config: Optional file group target containing a
91*61c4878aSAndroid Build Coastguard Worker        pw_console.yaml config file. For example:
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard Worker          filegroup(
94*61c4878aSAndroid Build Coastguard Worker              name = "pw_console_config",
95*61c4878aSAndroid Build Coastguard Worker              srcs = [".pw_console.yaml"],
96*61c4878aSAndroid Build Coastguard Worker          )
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Worker      **kwargs: Passed to native_binary.
99*61c4878aSAndroid Build Coastguard Worker    """
100*61c4878aSAndroid Build Coastguard Worker    data = [binary]
101*61c4878aSAndroid Build Coastguard Worker    args = kwargs.get("args", []) + [
102*61c4878aSAndroid Build Coastguard Worker        "--baudrate",
103*61c4878aSAndroid Build Coastguard Worker        baudrate,
104*61c4878aSAndroid Build Coastguard Worker        "--token-databases",
105*61c4878aSAndroid Build Coastguard Worker        "$(rootpath " + binary + ")",
106*61c4878aSAndroid Build Coastguard Worker    ]
107*61c4878aSAndroid Build Coastguard Worker    if pw_console_config:
108*61c4878aSAndroid Build Coastguard Worker        data.append(pw_console_config)
109*61c4878aSAndroid Build Coastguard Worker        args.append("--config-file")
110*61c4878aSAndroid Build Coastguard Worker        args.append("$(rootpath " + pw_console_config + ")")
111*61c4878aSAndroid Build Coastguard Worker
112*61c4878aSAndroid Build Coastguard Worker    native_binary(
113*61c4878aSAndroid Build Coastguard Worker        name = name,
114*61c4878aSAndroid Build Coastguard Worker        src = script,
115*61c4878aSAndroid Build Coastguard Worker        args = args,
116*61c4878aSAndroid Build Coastguard Worker        data = data,
117*61c4878aSAndroid Build Coastguard Worker        # Note: out is mandatory in older bazel-skylib versions.
118*61c4878aSAndroid Build Coastguard Worker        out = name + ".exe",
119*61c4878aSAndroid Build Coastguard Worker        **kwargs
120*61c4878aSAndroid Build Coastguard Worker    )
121