1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker# 3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker# the License at 6*61c4878aSAndroid Build Coastguard Worker# 7*61c4878aSAndroid Build Coastguard Worker# https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker# 9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker# the License. 14*61c4878aSAndroid Build Coastguard Worker"""Pigweed's customized cc_library wrappers.""" 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Workerload("@bazel_tools//tools/cpp:toolchain_utils.bzl", "use_cpp_toolchain") 17*61c4878aSAndroid Build Coastguard Workerload( 18*61c4878aSAndroid Build Coastguard Worker "//pw_build/bazel_internal:pigweed_internal.bzl", 19*61c4878aSAndroid Build Coastguard Worker _compile_cc = "compile_cc", 20*61c4878aSAndroid Build Coastguard Worker _link_cc = "link_cc", 21*61c4878aSAndroid Build Coastguard Worker) 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard Workerdef pw_cc_binary(**kwargs): 24*61c4878aSAndroid Build Coastguard Worker """Wrapper for cc_binary providing some defaults. 25*61c4878aSAndroid Build Coastguard Worker 26*61c4878aSAndroid Build Coastguard Worker Specifically, this wrapper adds deps on backend_impl targets for pw_assert 27*61c4878aSAndroid Build Coastguard Worker and pw_log. 28*61c4878aSAndroid Build Coastguard Worker 29*61c4878aSAndroid Build Coastguard Worker Args: 30*61c4878aSAndroid Build Coastguard Worker **kwargs: Passed to cc_binary. 31*61c4878aSAndroid Build Coastguard Worker """ 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker # TODO: b/234877642 - Remove this implicit dependency once we have a better 34*61c4878aSAndroid Build Coastguard Worker # way to handle the facades without introducing a circular dependency into 35*61c4878aSAndroid Build Coastguard Worker # the build. 36*61c4878aSAndroid Build Coastguard Worker kwargs["deps"] = kwargs.get("deps", []) + [str(Label("//pw_build:default_link_extra_lib"))] 37*61c4878aSAndroid Build Coastguard Worker native.cc_binary(**kwargs) 38*61c4878aSAndroid Build Coastguard Worker 39*61c4878aSAndroid Build Coastguard Workerdef _pw_cc_binary_with_map_impl(ctx): 40*61c4878aSAndroid Build Coastguard Worker [cc_info] = _compile_cc( 41*61c4878aSAndroid Build Coastguard Worker ctx, 42*61c4878aSAndroid Build Coastguard Worker ctx.files.srcs, 43*61c4878aSAndroid Build Coastguard Worker [], 44*61c4878aSAndroid Build Coastguard Worker deps = ctx.attr.deps + [ctx.attr.link_extra_lib, ctx.attr.malloc], 45*61c4878aSAndroid Build Coastguard Worker includes = ctx.attr.includes, 46*61c4878aSAndroid Build Coastguard Worker defines = ctx.attr.defines, 47*61c4878aSAndroid Build Coastguard Worker local_defines = ctx.attr.local_defines, 48*61c4878aSAndroid Build Coastguard Worker ) 49*61c4878aSAndroid Build Coastguard Worker 50*61c4878aSAndroid Build Coastguard Worker map_file = ctx.actions.declare_file(ctx.label.name + ".map") 51*61c4878aSAndroid Build Coastguard Worker map_flags = ["-Wl,-Map=" + map_file.path] 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker return _link_cc( 54*61c4878aSAndroid Build Coastguard Worker ctx, 55*61c4878aSAndroid Build Coastguard Worker [cc_info.linking_context], 56*61c4878aSAndroid Build Coastguard Worker ctx.attr.linkstatic, 57*61c4878aSAndroid Build Coastguard Worker ctx.attr.stamp, 58*61c4878aSAndroid Build Coastguard Worker user_link_flags = ctx.attr.linkopts + map_flags, 59*61c4878aSAndroid Build Coastguard Worker additional_outputs = [map_file], 60*61c4878aSAndroid Build Coastguard Worker ) 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Workerpw_cc_binary_with_map = rule( 63*61c4878aSAndroid Build Coastguard Worker implementation = _pw_cc_binary_with_map_impl, 64*61c4878aSAndroid Build Coastguard Worker doc = """Links a binary like cc_binary does but generates a linker map file 65*61c4878aSAndroid Build Coastguard Worker and provides it as an output after the executable in the DefaultInfo list 66*61c4878aSAndroid Build Coastguard Worker returned by this rule. 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Worker This rule makes an effort to somewhat mimic cc_binary args and behavior but 69*61c4878aSAndroid Build Coastguard Worker doesn't fully support all options currently. Make variable substitution and 70*61c4878aSAndroid Build Coastguard Worker tokenization handling isn't implemented by this rule on any of it's attrs. 71*61c4878aSAndroid Build Coastguard Worker 72*61c4878aSAndroid Build Coastguard Worker Args: 73*61c4878aSAndroid Build Coastguard Worker ctx: Rule context. 74*61c4878aSAndroid Build Coastguard Worker """, 75*61c4878aSAndroid Build Coastguard Worker attrs = { 76*61c4878aSAndroid Build Coastguard Worker "defines": attr.string_list( 77*61c4878aSAndroid Build Coastguard Worker doc = "List of defines to add to the compile line.", 78*61c4878aSAndroid Build Coastguard Worker ), 79*61c4878aSAndroid Build Coastguard Worker "deps": attr.label_list( 80*61c4878aSAndroid Build Coastguard Worker providers = [CcInfo], 81*61c4878aSAndroid Build Coastguard Worker doc = "The list of other libraries to be linked in to the binary target.", 82*61c4878aSAndroid Build Coastguard Worker ), 83*61c4878aSAndroid Build Coastguard Worker "includes": attr.string_list( 84*61c4878aSAndroid Build Coastguard Worker doc = "List of include dirs to add to the compile line.", 85*61c4878aSAndroid Build Coastguard Worker ), 86*61c4878aSAndroid Build Coastguard Worker "link_extra_lib": attr.label( 87*61c4878aSAndroid Build Coastguard Worker default = "@bazel_tools//tools/cpp:link_extra_lib", 88*61c4878aSAndroid Build Coastguard Worker doc = "Control linking of extra libraries.", 89*61c4878aSAndroid Build Coastguard Worker ), 90*61c4878aSAndroid Build Coastguard Worker "linkopts": attr.string_list( 91*61c4878aSAndroid Build Coastguard Worker doc = "Add these flags to the C++ linker command.", 92*61c4878aSAndroid Build Coastguard Worker ), 93*61c4878aSAndroid Build Coastguard Worker "linkstatic": attr.bool( 94*61c4878aSAndroid Build Coastguard Worker doc = "True if binary should be link statically", 95*61c4878aSAndroid Build Coastguard Worker ), 96*61c4878aSAndroid Build Coastguard Worker "local_defines": attr.string_list( 97*61c4878aSAndroid Build Coastguard Worker doc = "List of defines to add to the compile line.", 98*61c4878aSAndroid Build Coastguard Worker ), 99*61c4878aSAndroid Build Coastguard Worker "malloc": attr.label( 100*61c4878aSAndroid Build Coastguard Worker default = "@bazel_tools//tools/cpp:malloc", 101*61c4878aSAndroid Build Coastguard Worker doc = "Override the default dependency on malloc.", 102*61c4878aSAndroid Build Coastguard Worker ), 103*61c4878aSAndroid Build Coastguard Worker "srcs": attr.label_list( 104*61c4878aSAndroid Build Coastguard Worker allow_files = True, 105*61c4878aSAndroid Build Coastguard Worker doc = "The list of C and C++ files that are processed to create the target.", 106*61c4878aSAndroid Build Coastguard Worker ), 107*61c4878aSAndroid Build Coastguard Worker "stamp": attr.int( 108*61c4878aSAndroid Build Coastguard Worker default = -1, 109*61c4878aSAndroid Build Coastguard Worker doc = "Whether to encode build information into the binary.", 110*61c4878aSAndroid Build Coastguard Worker ), 111*61c4878aSAndroid Build Coastguard Worker }, 112*61c4878aSAndroid Build Coastguard Worker executable = True, 113*61c4878aSAndroid Build Coastguard Worker provides = [DefaultInfo], 114*61c4878aSAndroid Build Coastguard Worker fragments = ["cpp"], 115*61c4878aSAndroid Build Coastguard Worker toolchains = use_cpp_toolchain(), 116*61c4878aSAndroid Build Coastguard Worker) 117