xref: /aosp_15_r20/external/pigweed/pw_sensor/sensor.bzl (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 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"""A rule for translating pw_sensor YAML definitions into C++.
15"""
16
17def _pw_sensor_library_impl(ctx):
18    out_header = ctx.actions.declare_file(ctx.attr.out_header)
19    sources = ctx.files.srcs
20    inputs = ctx.files.inputs
21
22    sensor_desc_target = ctx.attr._sensor_desc[DefaultInfo]
23    generator_target = ctx.attr.generator[DefaultInfo]
24
25    args = []
26
27    # Add include paths for the generator
28    for generator_include in ctx.attr.generator_includes:
29        args.extend(["-I", generator_include])
30
31    # Add the generator and args
32    args.extend([
33        "-g",
34        (
35            "python3 " + generator_target.files_to_run.executable.path + " " +
36            " ".join(ctx.attr.generator_args)
37        ),
38    ])
39
40    # Add the output file
41    args.extend(["-o", out_header.path])
42
43    for source in sources:
44        args.append(source.path)
45
46    # Run the generator
47    ctx.actions.run(
48        inputs = (
49            sources + inputs + generator_target.files.to_list() +
50            sensor_desc_target.files.to_list()
51        ),
52        outputs = [out_header],
53        executable = sensor_desc_target.files_to_run.executable,
54        arguments = args,
55        mnemonic = "SensorCodeGen",
56        progress_message = "Generating " + out_header.path,
57    )
58
59    # Get compilation contexts from dependencies
60    dep_compilation_contexts = [
61        dep[CcInfo].compilation_context
62        for dep in ctx.attr.deps
63    ]
64    compilation_context = cc_common.create_compilation_context(
65        includes = depset([ctx.bin_dir.path]),
66        headers = depset([out_header]),
67    )
68
69    # Return a library made up of the generated header
70    return [
71        DefaultInfo(files = depset([out_header])),
72        CcInfo(
73            compilation_context = cc_common.merge_compilation_contexts(
74                compilation_contexts = dep_compilation_contexts + [compilation_context],
75            ),
76        ),
77    ]
78
79pw_sensor_library = rule(
80    implementation = _pw_sensor_library_impl,
81    attrs = {
82        "deps": attr.label_list(),
83        "generator": attr.label(
84            default = str(Label("//pw_sensor/py:constants_generator")),
85            executable = True,
86            cfg = "exec",
87        ),
88        "generator_args": attr.string_list(
89            default = ["--package", "pw.sensor"],
90        ),
91        "generator_includes": attr.string_list(),
92        "inputs": attr.label_list(allow_files = True),
93        "out_header": attr.string(),
94        "srcs": attr.label_list(allow_files = True),
95        "_sensor_desc": attr.label(
96            default = str(Label("//pw_sensor/py:sensor_desc")),
97            executable = True,
98            cfg = "exec",
99        ),
100    },
101    provides = [CcInfo],
102    toolchains = ["@rules_python//python:exec_tools_toolchain_type"],
103)
104