• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Bazel output support."""
15
16from typing import Any
17
18import pathlib
19
20try:
21    from pw_build_mcuxpresso.components import Project
22except ImportError:
23    # Load from this directory if pw_build_mcuxpresso is not available.
24    from components import Project  # type: ignore
25
26
27def _bazel_str(val: Any) -> str:
28    """Returns string in Bazel format with correct escaping."""
29    return str(val).replace('"', r'\"').replace('$', r'\$')
30
31
32def _bazel_str_out(name: str, val: Any, indent: int = 0) -> None:
33    """Outputs string in Bazel format with correct escaping."""
34    print('    ' * indent + f'{name} = "{_bazel_str(val)}",')
35
36
37def _bazel_str_list_out(name: str, vals: list[Any], indent: int = 0) -> None:
38    """Outputs list of strings in Bazel format with correct escaping."""
39    if not vals:
40        return
41
42    print('    ' * indent + f'{name} = [')
43    for val in vals:
44        print('    ' * (indent + 1) + f'"{_bazel_str(val)}",')
45    print('    ' * indent + '],')
46
47
48def _bazel_path_list_out(
49    name: str,
50    vals: list[pathlib.Path],
51    path_prefix: str | None = None,
52    indent: int = 0,
53) -> None:
54    """Outputs list of paths in Bazel format with common prefix."""
55    if path_prefix is not None:
56        str_vals = [f'{path_prefix}{str(val)}' for val in vals]
57    else:
58        str_vals = [str(f) for f in vals]
59
60    _bazel_str_list_out(name, sorted(set(str_vals)), indent=indent)
61
62
63def bazel_output(project: Project, name: str, path_prefix: str | None = None):
64    """Output Bazel target for a project with the specified components.
65
66    Args:
67        project: MCUXpresso project to output.
68        name: target name to output.
69        path_prefix: string prefix to prepend to all paths.
70    """
71    print('cc_library(')
72    _bazel_str_out('name', name, indent=1)
73    _bazel_path_list_out(
74        'srcs',
75        project.sources + project.libs,
76        path_prefix=path_prefix,
77        indent=1,
78    )
79    _bazel_path_list_out(
80        'hdrs', project.headers, path_prefix=path_prefix, indent=1
81    )
82    _bazel_str_list_out('defines', project.defines, indent=1)
83    _bazel_path_list_out(
84        'includes', project.include_dirs, path_prefix=path_prefix, indent=1
85    )
86
87    print(')')
88