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