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