xref: /aosp_15_r20/build/bazel/rules/cc/cc_prebuilt_binary.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1"""Copyright (C) 2022 The Android Open Source Project
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7     http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15
16load("//build/bazel/platforms:platform_utils.bzl", "platforms")
17load(":stripped_cc_common.bzl", "common_strip_attrs", "stripped_impl")
18
19def is_target_host(ctx):
20    return not platforms.is_target_android(ctx.attr._platform_utils)
21
22def _cc_prebuilt_binary_impl(ctx):
23    # If the target is host, Soong just manually does a symlink
24    if is_target_host(ctx):
25        exec = ctx.actions.declare_file(ctx.attr.name)
26        ctx.actions.symlink(
27            output = exec,
28            target_file = ctx.files.src[0],
29        )
30    else:
31        exec = stripped_impl(ctx, ctx.file.src)
32    return [
33        DefaultInfo(
34            files = depset([exec]),
35            executable = exec,
36        ),
37    ]
38
39cc_prebuilt_binary = rule(
40    implementation = _cc_prebuilt_binary_impl,
41    attrs = dict(
42        common_strip_attrs,  # HACK: inlining common_strip_attrs
43        src = attr.label(
44            mandatory = True,
45            allow_single_file = True,
46        ),
47        _platform_utils = attr.label(default = Label("//build/bazel/platforms:platform_utils")),
48    ),
49    executable = True,
50    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
51)
52