xref: /aosp_15_r20/external/bazelbuild-rules_go/go/private/providers.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1*9bb1b549SSpandan Das# Copyright 2014 The Bazel Authors. All rights reserved.
2*9bb1b549SSpandan Das#
3*9bb1b549SSpandan Das# Licensed under the Apache License, Version 2.0 (the "License");
4*9bb1b549SSpandan Das# you may not use this file except in compliance with the License.
5*9bb1b549SSpandan Das# You may obtain a copy of the License at
6*9bb1b549SSpandan Das#
7*9bb1b549SSpandan Das#    http://www.apache.org/licenses/LICENSE-2.0
8*9bb1b549SSpandan Das#
9*9bb1b549SSpandan Das# Unless required by applicable law or agreed to in writing, software
10*9bb1b549SSpandan Das# distributed under the License is distributed on an "AS IS" BASIS,
11*9bb1b549SSpandan Das# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9bb1b549SSpandan Das# See the License for the specific language governing permissions and
13*9bb1b549SSpandan Das# limitations under the License.
14*9bb1b549SSpandan Das
15*9bb1b549SSpandan Das# A represenatation of the inputs to a go package.
16*9bb1b549SSpandan Das# This is a configuration independent provider.
17*9bb1b549SSpandan Das# You must call resolve with a mode to produce a GoSource.
18*9bb1b549SSpandan Das# See go/providers.rst#GoLibrary for full documentation.
19*9bb1b549SSpandan DasGoLibrary = provider()
20*9bb1b549SSpandan Das
21*9bb1b549SSpandan Das# The filtered inputs and dependencies needed to build a GoArchive
22*9bb1b549SSpandan Das# This is a configuration specific provider.
23*9bb1b549SSpandan Das# It has no transitive information.
24*9bb1b549SSpandan Das# See go/providers.rst#GoSource for full documentation.
25*9bb1b549SSpandan DasGoSource = provider()
26*9bb1b549SSpandan Das
27*9bb1b549SSpandan Das# This compiled form of a package used in transitive dependencies.
28*9bb1b549SSpandan Das# This is a configuration specific provider.
29*9bb1b549SSpandan Das# See go/providers.rst#GoArchiveData for full documentation.
30*9bb1b549SSpandan DasGoArchiveData = provider()
31*9bb1b549SSpandan Das
32*9bb1b549SSpandan Das# The compiled form of a GoLibrary, with everything needed to link it into a binary.
33*9bb1b549SSpandan Das# This is a configuration specific provider.
34*9bb1b549SSpandan Das# See go/providers.rst#GoArchive for full documentation.
35*9bb1b549SSpandan DasGoArchive = provider()
36*9bb1b549SSpandan Das
37*9bb1b549SSpandan DasGoPath = provider()
38*9bb1b549SSpandan Das
39*9bb1b549SSpandan DasGoSDK = provider(
40*9bb1b549SSpandan Das    doc = "Contains information about the Go SDK used in the toolchain",
41*9bb1b549SSpandan Das    fields = {
42*9bb1b549SSpandan Das        "goos": "The host OS the SDK was built for.",
43*9bb1b549SSpandan Das        "goarch": "The host architecture the SDK was built for.",
44*9bb1b549SSpandan Das        "experiments": "Go experiments to enable via GOEXPERIMENT.",
45*9bb1b549SSpandan Das        "root_file": "A file in the SDK root directory",
46*9bb1b549SSpandan Das        "libs": ("List of pre-compiled .a files for the standard library " +
47*9bb1b549SSpandan Das                 "built for the execution platform."),
48*9bb1b549SSpandan Das        "headers": ("List of .h files from pkg/include that may be included " +
49*9bb1b549SSpandan Das                    "in assembly sources."),
50*9bb1b549SSpandan Das        "srcs": ("List of source files for importable packages in the " +
51*9bb1b549SSpandan Das                 "standard library. Internal, vendored, and tool packages " +
52*9bb1b549SSpandan Das                 "may not be included."),
53*9bb1b549SSpandan Das        "package_list": ("A file containing a list of importable packages " +
54*9bb1b549SSpandan Das                         "in the standard library."),
55*9bb1b549SSpandan Das        "tools": ("List of executable files in the SDK built for " +
56*9bb1b549SSpandan Das                  "the execution platform, excluding the go binary file"),
57*9bb1b549SSpandan Das        "go": "The go binary file",
58*9bb1b549SSpandan Das        "version": "The Go SDK version",
59*9bb1b549SSpandan Das    },
60*9bb1b549SSpandan Das)
61*9bb1b549SSpandan Das
62*9bb1b549SSpandan DasGoStdLib = provider()
63*9bb1b549SSpandan Das
64*9bb1b549SSpandan DasGoConfigInfo = provider()
65*9bb1b549SSpandan Das
66*9bb1b549SSpandan DasGoContextInfo = provider()
67*9bb1b549SSpandan Das
68*9bb1b549SSpandan DasCgoContextInfo = provider()
69*9bb1b549SSpandan Das
70*9bb1b549SSpandan DasEXPLICIT_PATH = "explicit"
71*9bb1b549SSpandan Das
72*9bb1b549SSpandan DasINFERRED_PATH = "inferred"
73*9bb1b549SSpandan Das
74*9bb1b549SSpandan DasEXPORT_PATH = "export"
75*9bb1b549SSpandan Das
76*9bb1b549SSpandan Dasdef get_source(dep):
77*9bb1b549SSpandan Das    if type(dep) == "struct":
78*9bb1b549SSpandan Das        return dep
79*9bb1b549SSpandan Das    return dep[GoSource]
80*9bb1b549SSpandan Das
81*9bb1b549SSpandan Dasdef get_archive(dep):
82*9bb1b549SSpandan Das    if type(dep) == "struct":
83*9bb1b549SSpandan Das        return dep
84*9bb1b549SSpandan Das    return dep[GoArchive]
85*9bb1b549SSpandan Das
86*9bb1b549SSpandan Dasdef effective_importpath_pkgpath(lib):
87*9bb1b549SSpandan Das    """Returns import and package paths for a given lib with modifications for display.
88*9bb1b549SSpandan Das
89*9bb1b549SSpandan Das    This is used when we need to represent sources in a manner compatible with Go
90*9bb1b549SSpandan Das    build (e.g., for packaging or coverage data listing). _test suffixes are
91*9bb1b549SSpandan Das    removed, and vendor directories from importmap may be modified.
92*9bb1b549SSpandan Das
93*9bb1b549SSpandan Das    Args:
94*9bb1b549SSpandan Das      lib: GoLibrary or GoArchiveData
95*9bb1b549SSpandan Das
96*9bb1b549SSpandan Das    Returns:
97*9bb1b549SSpandan Das      A tuple of effective import path and effective package path. Both are ""
98*9bb1b549SSpandan Das      for synthetic archives (e.g., generated testmain).
99*9bb1b549SSpandan Das    """
100*9bb1b549SSpandan Das    if lib.pathtype not in (EXPLICIT_PATH, EXPORT_PATH):
101*9bb1b549SSpandan Das        return "", ""
102*9bb1b549SSpandan Das    importpath = lib.importpath
103*9bb1b549SSpandan Das    importmap = lib.importmap
104*9bb1b549SSpandan Das    if importpath.endswith("_test"):
105*9bb1b549SSpandan Das        importpath = importpath[:-len("_test")]
106*9bb1b549SSpandan Das    if importmap.endswith("_test"):
107*9bb1b549SSpandan Das        importmap = importmap[:-len("_test")]
108*9bb1b549SSpandan Das    parts = importmap.split("/")
109*9bb1b549SSpandan Das    if "vendor" not in parts:
110*9bb1b549SSpandan Das        # Unusual case not handled by go build. Just return importpath.
111*9bb1b549SSpandan Das        return importpath, importpath
112*9bb1b549SSpandan Das    elif len(parts) > 2 and lib.label.workspace_root == "external/" + parts[0]:
113*9bb1b549SSpandan Das        # Common case for importmap set by Gazelle in external repos.
114*9bb1b549SSpandan Das        return importpath, importmap[len(parts[0]):]
115*9bb1b549SSpandan Das    else:
116*9bb1b549SSpandan Das        # Vendor directory somewhere in the main repo. Leave it alone.
117*9bb1b549SSpandan Das        return importpath, importmap
118