github.com/ajguerrer/rules_go@v0.20.3/go/private/providers.bzl (about) 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 15 load("@io_bazel_rules_go//go/private:mode.bzl", "mode_string") 16 17 GoLibrary = provider() 18 """ 19 A represenatation of the inputs to a go package. 20 This is a configuration independent provider. 21 You must call resolve with a mode to produce a GoSource. 22 See go/providers.rst#GoLibrary for full documentation. 23 """ 24 25 GoSource = provider() 26 """ 27 The filtered inputs and dependencies needed to build a GoArchive 28 This is a configuration specific provider. 29 It has no transitive information. 30 See go/providers.rst#GoSource for full documentation. 31 """ 32 33 GoArchiveData = provider() 34 """ 35 This compiled form of a package used in transitive dependencies. 36 This is a configuration specific provider. 37 See go/providers.rst#GoArchiveData for full documentation. 38 """ 39 40 GoArchive = provider() 41 """ 42 The compiled form of a GoLibrary, with everything needed to link it into a binary. 43 This is a configuration specific provider. 44 See go/providers.rst#GoArchive for full documentation. 45 """ 46 47 GoAspectProviders = provider() 48 49 GoPath = provider() 50 51 GoSDK = provider( 52 doc = "Contains information about the Go SDK used in the toolchain", 53 fields = { 54 "goos": "The host OS the SDK was built for.", 55 "goarch": "The host architecture the SDK was built for.", 56 "root_file": "A file in the SDK root directory", 57 "libs": ("List of pre-compiled .a files for the standard library " + 58 "built for the execution platform."), 59 "headers": ("List of .h files from pkg/include that may be included " + 60 "in assembly sources."), 61 "srcs": ("List of source files for importable packages in the " + 62 "standard library. Internal, vendored, and tool packages " + 63 "may not be included."), 64 "package_list": ("A file containing a list of importable packages " + 65 "in the standard library."), 66 "tools": ("List of executable files in the SDK built for " + 67 "the execution platform, excluding the go binary file"), 68 "go": "The go binary file", 69 }, 70 ) 71 72 GoStdLib = provider() 73 74 CgoContextData = provider() 75 76 EXPLICIT_PATH = "explicit" 77 78 INFERRED_PATH = "inferred" 79 80 EXPORT_PATH = "export" 81 82 def get_source(dep): 83 if type(dep) == "struct": 84 return dep 85 if GoAspectProviders in dep: 86 return dep[GoAspectProviders].source 87 return dep[GoSource] 88 89 def get_archive(dep): 90 if type(dep) == "struct": 91 return dep 92 if GoAspectProviders in dep: 93 return dep[GoAspectProviders].archive 94 return dep[GoArchive] 95 96 def effective_importpath_pkgpath(lib): 97 """Returns import and package paths for a given lib with some 98 modifications for display. 99 100 This is used when we need to represent sources in a manner compatible with Go 101 build (e.g., for packaging or coverage data listing). _test suffixes are 102 removed, and vendor directories from importmap may be modified. 103 104 Args: 105 lib: GoLibrary or GoArchiveData 106 107 Returns: 108 A tuple of effective import path and effective package path. Both are "" 109 for synthetic archives (e.g., generated testmain). 110 """ 111 if lib.pathtype not in (EXPLICIT_PATH, EXPORT_PATH): 112 return "", "" 113 importpath = lib.importpath 114 importmap = lib.importmap 115 if importpath.endswith("_test"): 116 importpath = importpath[:-len("_test")] 117 if importmap.endswith("_test"): 118 importmap = importmap[:-len("_test")] 119 parts = importmap.split("/") 120 if "vendor" not in parts: 121 # Unusual case not handled by go build. Just return importpath. 122 return importpath, importpath 123 elif len(parts) > 2 and lib.label.workspace_root == "external/" + parts[0]: 124 # Common case for importmap set by Gazelle in external repos. 125 return importpath, importmap[len(parts[0]):] 126 else: 127 # Vendor directory somewhere in the main repo. Leave it alone. 128 return importpath, importmap