xref: /aosp_15_r20/external/pigweed/pw_sensor/sensor.gni (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.
14import("//build_overrides/pigweed.gni")
15
16import("$dir_pw_build/cc_library.gni")
17import("$dir_pw_build/python_action.gni")
18
19# Generates a sensor library
20#
21# Args:
22#   out_header: The path/to/header.h to generate
23#   sources: YAML files defining sensors
24#   inputs: [optional] YAML files included by the sensors, these will be
25#     used to optimize re-building.
26#   generator: [optional] Python generator script, if not set, the default
27#     Pigweed generator will be used.
28#   generator_args: [optional] Command line arguments to pass to the generator.
29#   generator_includes: [optional] Include paths to pass to the generator. These
30#     are used to resolve the sensor dependencies.
31#   public_deps: [optional] Public dependencies to pass to the final generated
32#     target.
33template("pw_sensor_library") {
34  current_dir = rebase_path(get_path_info(".", "abspath"))
35
36  # Get the output header path
37  assert(defined(invoker.out_header) && invoker.out_header != "",
38         "pw_sensor_library requires an out_header name")
39  out_header = "$target_gen_dir/${invoker.out_header}"
40
41  # Get the source yaml files
42  assert(defined(invoker.sources) && invoker.sources != [],
43         "pw_sensor_library requires .yaml source files")
44  source_files = []
45  foreach(src, invoker.sources) {
46    source_files += [ "$current_dir/$src" ]
47  }
48
49  # Get the optional inputs
50  in_inputs = []
51  foreach(file, invoker.inputs) {
52    in_inputs += [ "$current_dir/$file" ]
53  }
54
55  # Get the include paths for the generator
56  include_list = []
57  foreach(file, invoker.generator_includes) {
58    include_list += [
59      "-I",
60      rebase_path(file, root_build_dir),
61    ]
62  }
63
64  # Get the generator args if provided
65  generator_args = []
66  if (defined(invoker.generator_args)) {
67    generator_args = invoker.generator_args
68  }
69
70  # Get the generator if provided (use the default otherwise)
71  if (defined(invoker.generator)) {
72    generator = rebase_path(invoker.generator, root_build_dir)
73  } else {
74    generator =
75        rebase_path("$dir_pw_sensor/py/pw_sensor/constants_generator.py")
76    if (!defined(invoker.generator_args)) {
77      generator_args = [
78        "--package",
79        "pw.sensor",
80      ]
81    }
82  }
83  generator_args_string = string_join(" ", generator_args)
84
85  pw_python_action("${target_name}_generate_header") {
86    script = "$dir_pw_sensor/py/pw_sensor/sensor_desc.py"
87    python_deps = [ "$dir_pw_sensor/py" ]
88    args = [] + include_list + [
89             "-g",
90             "python3 $generator $generator_args_string",
91             "-o",
92             rebase_path(out_header),
93           ] + source_files
94    inputs = source_files + [ generator ] + in_inputs
95    outputs = [ out_header ]
96  }
97
98  config("${target_name}_config") {
99    include_dirs = [ target_gen_dir ]
100  }
101
102  in_public_deps = []
103  if (defined(invoker.public_deps)) {
104    in_public_deps = invoker.public_deps
105  }
106
107  pw_source_set(target_name) {
108    public = [ out_header ]
109    public_configs = [ ":${target_name}_config" ]
110    public_deps = [ ":${target_name}_generate_header" ] + in_public_deps
111  }
112}
113