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