aboutsummaryrefslogtreecommitdiff
path: root/go/private/rules/sdk.bzl
blob: 899cd0a7fcd90096b3c24e04b83a4a953d3a4f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
    "//go/private:providers.bzl",
    "GoSDK",
)

def _go_sdk_impl(ctx):
    package_list = ctx.file.package_list
    if package_list == None:
        package_list = ctx.actions.declare_file("packages.txt")
        _build_package_list(ctx, ctx.files.srcs, ctx.file.root_file, package_list)
    return [GoSDK(
        goos = ctx.attr.goos,
        goarch = ctx.attr.goarch,
        experiments = ctx.attr.experiments,
        root_file = ctx.file.root_file,
        package_list = package_list,
        libs = ctx.files.libs,
        headers = ctx.files.headers,
        srcs = ctx.files.srcs,
        tools = ctx.files.tools,
        go = ctx.executable.go,
        version = ctx.attr.version,
    )]

go_sdk = rule(
    _go_sdk_impl,
    attrs = {
        "goos": attr.string(
            mandatory = True,
            doc = "The host OS the SDK was built for",
        ),
        "goarch": attr.string(
            mandatory = True,
            doc = "The host architecture the SDK was built for",
        ),
        "experiments": attr.string_list(
            mandatory = False,
            doc = "Go experiments to enable via GOEXPERIMENT",
        ),
        "root_file": attr.label(
            mandatory = True,
            allow_single_file = True,
            doc = "A file in the SDK root directory. Used to determine GOROOT.",
        ),
        "package_list": attr.label(
            allow_single_file = True,
            doc = ("A text file containing a list of packages in the " +
                   "standard library that may be imported."),
        ),
        "libs": attr.label_list(
            # allow_files is not set to [".a"] because that wouldn't allow
            # for zero files to be present, as is the case in Go 1.20+.
            # See also https://github.com/bazelbuild/bazel/issues/7516
            allow_files = True,
            doc = ("Pre-compiled .a files for the standard library, " +
                   "built for the execution platform"),
        ),
        "headers": attr.label_list(
            allow_files = [".h"],
            doc = (".h files from pkg/include that may be included in " +
                   "assembly sources"),
        ),
        "srcs": attr.label_list(
            allow_files = True,
            doc = "Source files for packages in the standard library",
        ),
        "tools": attr.label_list(
            allow_files = True,
            cfg = "exec",
            doc = ("List of executable files in the SDK built for " +
                   "the execution platform, excluding the go binary"),
        ),
        "go": attr.label(
            mandatory = True,
            allow_single_file = True,
            executable = True,
            cfg = "exec",
            doc = "The go binary",
        ),
        "version": attr.string(
            doc = "The version of the Go SDK.",
        ),
    },
    doc = ("Collects information about a Go SDK. The SDK must have a normal " +
           "GOROOT directory structure."),
    provides = [GoSDK],
)

def _package_list_impl(ctx):
    _build_package_list(ctx, ctx.files.srcs, ctx.file.root_file, ctx.outputs.out)
    return [DefaultInfo(files = depset([ctx.outputs.out]))]

package_list = rule(
    _package_list_impl,
    attrs = {
        "srcs": attr.label_list(
            allow_files = True,
            doc = "Source files for packages in the standard library",
        ),
        "root_file": attr.label(
            mandatory = True,
            allow_single_file = True,
            doc = "A file in the SDK root directory. Used to determine GOROOT.",
        ),
        "out": attr.output(
            mandatory = True,
            doc = "File to write. Must be 'packages.txt'.",
            # Gazelle depends on this file directly. It has to be an output
            # attribute because Bazel has no other way of knowing what rule
            # produces this file.
            # TODO(jayconrod): Update Gazelle and simplify this.
        ),
    },
)

def _build_package_list(ctx, srcs, root_file, out):
    packages = {}
    src_dir = root_file.dirname + "/src/"
    for src in srcs:
        pkg_src_dir = src.dirname
        if not pkg_src_dir.startswith(src_dir):
            continue
        pkg_name = pkg_src_dir[len(src_dir):]
        packages[pkg_name] = None
    content = "\n".join(sorted(packages.keys())) + "\n"
    ctx.actions.write(out, content)