xref: /aosp_15_r20/external/pigweed/pw_build/py/pw_build/gn_target.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 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"""Utilities for manipulating GN targets."""
15
16import re
17
18from typing import Iterable
19
20
21class GnTarget:
22    """Represents a Pigweed Build target.
23
24    Attributes:
25        attrs:  Map of GN variable names to lists of values. Recognized GN list
26                variables include 'public', 'sources', 'inputs', 'deps', etc.
27        origin: Reference to the data used to generate this target, e.g. a
28                Bazel label.
29    """
30
31    def __init__(self, target_type: str, target_name: str) -> None:
32        """Creates a GN target.
33
34        Args:
35            target_type: Type of the GN target, like 'pw_source_set'.
36            target_name: Name of the GN target.
37        """
38        self.attrs: dict[str, list[str]] = {}
39        self.origin: str | None = None
40        self._type = target_type
41        self._name = target_name
42
43    def type(self) -> str:
44        """Returns this object's GN target_type."""
45        return self._type
46
47    def name(self) -> str:
48        """Returns this object's GN target_name."""
49        return self._name
50
51    def build_args(self) -> Iterable[str]:
52        """Returns a list of GN build arguments referenced in paths.
53
54        Third party source code is referenced by Pigweed build files using build
55        arguments, e.g. 'public = [ "$dir_pw_third_party_foo/bar/baz.h" ]'. This
56        method returns any build arguments used to locate source, header or
57        input files so that the BUILD.gn file can import the build argument
58        definition.
59        """
60        build_arg_pat = re.compile(r'(\$dir_pw_third_party_[^/:]*)')
61        for field in ['public', 'sources', 'inputs', 'include_dirs']:
62            for source_path in self.attrs.get(field, []):
63                match = re.search(build_arg_pat, source_path)
64                if match:
65                    yield match.group(1)
66