xref: /aosp_15_r20/external/bazelbuild-rules_go/go/private/actions/compilepkg.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2019 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    "link_mode_args",
18)
19load("//go/private/actions:utils.bzl", "quote_opts")
20
21def _archive(v):
22    importpaths = [v.data.importpath]
23    importpaths.extend(v.data.importpath_aliases)
24    return "{}={}={}".format(
25        ":".join(importpaths),
26        v.data.importmap,
27        v.data.export_file.path if v.data.export_file else v.data.file.path,
28    )
29
30def _embedroot_arg(src):
31    return src.root.path
32
33def _embedlookupdir_arg(src):
34    root_relative = src.dirname[len(src.root.path):]
35    if root_relative.startswith("/"):
36        root_relative = root_relative[len("/"):]
37    return root_relative
38
39def emit_compilepkg(
40        go,
41        sources = None,
42        cover = None,
43        embedsrcs = [],
44        importpath = "",
45        importmap = "",
46        archives = [],
47        cgo = False,
48        cgo_inputs = depset(),
49        cppopts = [],
50        copts = [],
51        cxxopts = [],
52        objcopts = [],
53        objcxxopts = [],
54        clinkopts = [],
55        out_lib = None,
56        out_export = None,
57        out_cgo_export_h = None,
58        gc_goopts = [],
59        testfilter = None,  # TODO: remove when test action compiles packages
60        recompile_internal_deps = []):
61    """Compiles a complete Go package."""
62    if sources == None:
63        fail("sources is a required parameter")
64    if out_lib == None:
65        fail("out_lib is a required parameter")
66
67    inputs = (sources + embedsrcs + [go.package_list] +
68              [archive.data.export_file for archive in archives] +
69              go.sdk.tools + go.sdk.headers + go.stdlib.libs)
70    outputs = [out_lib, out_export]
71    env = go.env
72
73    args = go.builder_args(go, "compilepkg")
74    args.add_all(sources, before_each = "-src")
75    args.add_all(embedsrcs, before_each = "-embedsrc", expand_directories = False)
76    args.add_all(
77        sources + [out_lib] + embedsrcs,
78        map_each = _embedroot_arg,
79        before_each = "-embedroot",
80        uniquify = True,
81        expand_directories = False,
82    )
83    args.add_all(
84        sources + [out_lib],
85        map_each = _embedlookupdir_arg,
86        before_each = "-embedlookupdir",
87        uniquify = True,
88        expand_directories = False,
89    )
90    if cover and go.coverdata:
91        inputs.append(go.coverdata.data.export_file)
92        args.add("-arc", _archive(go.coverdata))
93        if go.mode.race:
94            args.add("-cover_mode", "atomic")
95        else:
96            args.add("-cover_mode", "set")
97        args.add("-cover_format", go.cover_format)
98        args.add_all(cover, before_each = "-cover")
99    args.add_all(archives, before_each = "-arc", map_each = _archive)
100    if recompile_internal_deps:
101        args.add_all(recompile_internal_deps, before_each = "-recompile_internal_deps")
102    if importpath:
103        args.add("-importpath", importpath)
104    else:
105        args.add("-importpath", go.label.name)
106    if importmap:
107        args.add("-p", importmap)
108    args.add("-package_list", go.package_list)
109
110    args.add("-o", out_lib)
111    args.add("-x", out_export)
112    if go.nogo:
113        args.add("-nogo", go.nogo)
114        inputs.append(go.nogo)
115    if out_cgo_export_h:
116        args.add("-cgoexport", out_cgo_export_h)
117        outputs.append(out_cgo_export_h)
118    if testfilter:
119        args.add("-testfilter", testfilter)
120
121    gc_flags = list(gc_goopts)
122    gc_flags.extend(go.mode.gc_goopts)
123    asm_flags = []
124    if go.mode.race:
125        gc_flags.append("-race")
126    if go.mode.msan:
127        gc_flags.append("-msan")
128    if go.mode.debug:
129        gc_flags.extend(["-N", "-l"])
130    gc_flags.extend(go.toolchain.flags.compile)
131    gc_flags.extend(link_mode_args(go.mode))
132    asm_flags.extend(link_mode_args(go.mode))
133    args.add("-gcflags", quote_opts(gc_flags))
134    args.add("-asmflags", quote_opts(asm_flags))
135
136    env = go.env
137    if cgo:
138        inputs.extend(cgo_inputs.to_list())  # OPT: don't expand depset
139        inputs.extend(go.crosstool)
140        env["CC"] = go.cgo_tools.c_compiler_path
141        if cppopts:
142            args.add("-cppflags", quote_opts(cppopts))
143        if copts:
144            args.add("-cflags", quote_opts(copts))
145        if cxxopts:
146            args.add("-cxxflags", quote_opts(cxxopts))
147        if objcopts:
148            args.add("-objcflags", quote_opts(objcopts))
149        if objcxxopts:
150            args.add("-objcxxflags", quote_opts(objcxxopts))
151        if clinkopts:
152            args.add("-ldflags", quote_opts(clinkopts))
153
154    go.actions.run(
155        inputs = inputs,
156        outputs = outputs,
157        mnemonic = "GoCompilePkg",
158        executable = go.toolchain._builder,
159        arguments = [args],
160        env = go.env,
161    )
162