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