xref: /aosp_15_r20/external/pigweed/pw_build/pw_cc_binary.bzl (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.
14"""Pigweed's customized cc_library wrappers."""
15
16load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "use_cpp_toolchain")
17load(
18    "//pw_build/bazel_internal:pigweed_internal.bzl",
19    _compile_cc = "compile_cc",
20    _link_cc = "link_cc",
21)
22
23def pw_cc_binary(**kwargs):
24    """Wrapper for cc_binary providing some defaults.
25
26    Specifically, this wrapper adds deps on backend_impl targets for pw_assert
27    and pw_log.
28
29    Args:
30      **kwargs: Passed to cc_binary.
31    """
32
33    # TODO: b/234877642 - Remove this implicit dependency once we have a better
34    # way to handle the facades without introducing a circular dependency into
35    # the build.
36    kwargs["deps"] = kwargs.get("deps", []) + [str(Label("//pw_build:default_link_extra_lib"))]
37    native.cc_binary(**kwargs)
38
39def _pw_cc_binary_with_map_impl(ctx):
40    [cc_info] = _compile_cc(
41        ctx,
42        ctx.files.srcs,
43        [],
44        deps = ctx.attr.deps + [ctx.attr.link_extra_lib, ctx.attr.malloc],
45        includes = ctx.attr.includes,
46        defines = ctx.attr.defines,
47        local_defines = ctx.attr.local_defines,
48    )
49
50    map_file = ctx.actions.declare_file(ctx.label.name + ".map")
51    map_flags = ["-Wl,-Map=" + map_file.path]
52
53    return _link_cc(
54        ctx,
55        [cc_info.linking_context],
56        ctx.attr.linkstatic,
57        ctx.attr.stamp,
58        user_link_flags = ctx.attr.linkopts + map_flags,
59        additional_outputs = [map_file],
60    )
61
62pw_cc_binary_with_map = rule(
63    implementation = _pw_cc_binary_with_map_impl,
64    doc = """Links a binary like cc_binary does but generates a linker map file
65    and provides it as an output after the executable in the DefaultInfo list
66    returned by this rule.
67
68    This rule makes an effort to somewhat mimic cc_binary args and behavior but
69    doesn't fully support all options currently. Make variable substitution and
70    tokenization handling isn't implemented by this rule on any of it's attrs.
71
72    Args:
73        ctx: Rule context.
74    """,
75    attrs = {
76        "defines": attr.string_list(
77            doc = "List of defines to add to the compile line.",
78        ),
79        "deps": attr.label_list(
80            providers = [CcInfo],
81            doc = "The list of other libraries to be linked in to the binary target.",
82        ),
83        "includes": attr.string_list(
84            doc = "List of include dirs to add to the compile line.",
85        ),
86        "link_extra_lib": attr.label(
87            default = "@bazel_tools//tools/cpp:link_extra_lib",
88            doc = "Control linking of extra libraries.",
89        ),
90        "linkopts": attr.string_list(
91            doc = "Add these flags to the C++ linker command.",
92        ),
93        "linkstatic": attr.bool(
94            doc = "True if binary should be link statically",
95        ),
96        "local_defines": attr.string_list(
97            doc = "List of defines to add to the compile line.",
98        ),
99        "malloc": attr.label(
100            default = "@bazel_tools//tools/cpp:malloc",
101            doc = "Override the default dependency on malloc.",
102        ),
103        "srcs": attr.label_list(
104            allow_files = True,
105            doc = "The list of C and C++ files that are processed to create the target.",
106        ),
107        "stamp": attr.int(
108            default = -1,
109            doc = "Whether to encode build information into the binary.",
110        ),
111    },
112    executable = True,
113    provides = [DefaultInfo],
114    fragments = ["cpp"],
115    toolchains = use_cpp_toolchain(),
116)
117