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