xref: /aosp_15_r20/external/bazelbuild-rules_go/go/private/actions/binary.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2014 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load(
16    "//go/private:mode.bzl",
17    "LINKMODE_C_ARCHIVE",
18    "LINKMODE_C_SHARED",
19    "LINKMODE_PLUGIN",
20)
21load(
22    "//go/private:common.bzl",
23    "ARCHIVE_EXTENSION",
24    "has_shared_lib_extension",
25)
26
27def emit_binary(
28        go,
29        name = "",
30        source = None,
31        test_archives = [],
32        gc_linkopts = [],
33        version_file = None,
34        info_file = None,
35        executable = None):
36    """See go/toolchains.rst#binary for full documentation."""
37
38    if name == "" and executable == None:
39        fail("either name or executable must be set")
40
41    archive = go.archive(go, source)
42    if not executable:
43        extension = go.exe_extension
44        if go.mode.link == LINKMODE_C_SHARED:
45            name = "lib" + name  # shared libraries need a "lib" prefix in their name
46            extension = go.shared_extension
47        elif go.mode.link == LINKMODE_C_ARCHIVE:
48            extension = ARCHIVE_EXTENSION
49        elif go.mode.link == LINKMODE_PLUGIN:
50            extension = go.shared_extension
51        executable = go.declare_file(go, path = name, ext = extension)
52    go.link(
53        go,
54        archive = archive,
55        test_archives = test_archives,
56        executable = executable,
57        gc_linkopts = gc_linkopts,
58        version_file = version_file,
59        info_file = info_file,
60    )
61    cgo_dynamic_deps = [
62        d
63        for d in archive.cgo_deps.to_list()
64        if has_shared_lib_extension(d.basename)
65    ]
66    runfiles = go._ctx.runfiles(files = cgo_dynamic_deps).merge(archive.runfiles)
67
68    return archive, executable, runfiles
69