• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2025 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"""Implements the pw_cc_size_binary macro."""
15
16load("//pw_bloat/private:pw_bloat_report.bzl", "PwSizeBinaryInfo")
17load("//pw_build:pw_cc_binary.bzl", "pw_cc_binary")
18
19def pw_cc_size_binary(
20        name,
21        platform = "//pw_bloat:pw_size_report_platform",
22        **cc_binary_kwargs):
23    """Compiles a pw_cc_binary target with a configured platform.
24
25    By default, this uses the globally-configured
26    //pw_bloat:pw_size_report_platform alias to compile the binary. However,
27    this may be overridden on a per-binary basis.
28
29    This produces two sub-targets:
30      - {name}.base: A pw_cc_binary configured with the provided cc_binary_kwargs.
31      - {name}: The same binary built with the configured platform.
32    """
33    cc_binary_name = name + ".base"
34
35    pw_cc_binary(
36        name = cc_binary_name,
37        target_compatible_with = select({
38            # As these binaries are intended to be built for a size reporting
39            # target, they should not be included in sanitizer builds.
40            "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"],
41            "//conditions:default": [],
42        }),
43        **cc_binary_kwargs
44    )
45
46    _pw_platform_size_binary(
47        name = name,
48        platform = platform,
49        target = ":" + cc_binary_name,
50    )
51
52def _pw_target_platform_transition_impl(_, attr):
53    return {
54        "//command_line_option:platforms": str(attr.platform),
55    }
56
57_pw_target_platform_transition = transition(
58    implementation = _pw_target_platform_transition_impl,
59    inputs = [],
60    outputs = [
61        "//command_line_option:platforms",
62    ],
63)
64
65def _pw_platform_size_binary_impl(ctx):
66    linked_executable = ctx.actions.declare_file(ctx.attr.name)
67
68    ctx.actions.symlink(
69        output = linked_executable,
70        target_file = ctx.executable.target,
71        is_executable = True,
72    )
73
74    return [
75        DefaultInfo(
76            executable = linked_executable,
77        ),
78        PwSizeBinaryInfo(
79            binary = linked_executable,
80            bloaty_config = ctx.file._bloaty_config,
81        ),
82    ]
83
84_pw_platform_size_binary = rule(
85    implementation = _pw_platform_size_binary_impl,
86    attrs = {
87        "platform": attr.label(
88            mandatory = True,
89        ),
90        "target": attr.label(
91            mandatory = True,
92            executable = True,
93            cfg = _pw_target_platform_transition,
94        ),
95        "_allowlist_function_transition": attr.label(
96            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
97        ),
98        "_bloaty_config": attr.label(
99            default = Label("//pw_bloat:pw_size_report_bloaty_config"),
100            allow_single_file = True,
101            cfg = _pw_target_platform_transition,
102        ),
103    },
104    executable = True,
105)
106