|
| 1 | +/* Copyright 2020 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 | + |
| 16 | +// Package symbol generates a `starlark_library` target for every `.bzl` file in |
| 17 | +// each package. At the root of the module, a single starlark_bundle is |
| 18 | +// populated with deps that include all other starlark_libraries. |
| 19 | +// |
| 20 | +// The original code for this gazelle extension started from |
| 21 | +// https://github.com/bazelbuild/bazel-skylib/blob/main/gazelle/bzl/gazelle.go. |
| 22 | +package starlarkbundle |
| 23 | + |
| 24 | +import ( |
| 25 | + "flag" |
| 26 | + "log" |
| 27 | + "maps" |
| 28 | + |
| 29 | + "github.com/bazelbuild/bazel-gazelle/config" |
| 30 | + "github.com/bazelbuild/bazel-gazelle/label" |
| 31 | + "github.com/bazelbuild/bazel-gazelle/language" |
| 32 | + "github.com/bazelbuild/bazel-gazelle/repo" |
| 33 | + "github.com/bazelbuild/bazel-gazelle/resolve" |
| 34 | + "github.com/bazelbuild/bazel-gazelle/rule" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + languageName = "starlark_bundle" |
| 39 | + starlarkBundleRootDirectiveName = "starlark_bundle_root" |
| 40 | +) |
| 41 | + |
| 42 | +type starlarkBundleLang struct { |
| 43 | + starlarkBundleRoot *string |
| 44 | + starlarkLibraries map[label.Label]*rule.Rule |
| 45 | +} |
| 46 | + |
| 47 | +// NewLanguage is called by Gazelle to install this language extension in a |
| 48 | +// binary. |
| 49 | +func NewLanguage() language.Language { |
| 50 | + return &starlarkBundleLang{ |
| 51 | + starlarkLibraries: make(map[label.Label]*rule.Rule), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Name returns the name of the language. This should be a prefix of the kinds |
| 56 | +// of rules generated by the language, e.g., "go" for the Go extension since it |
| 57 | +// generates "go_library" rules. |
| 58 | +func (*starlarkBundleLang) Name() string { |
| 59 | + return languageName |
| 60 | +} |
| 61 | + |
| 62 | +// The following methods are implemented to satisfy the |
| 63 | +// https://pkg.go.dev/github.com/bazelbuild/bazel-gazelle/resolve?tab=doc#Resolver |
| 64 | +// interface, but are otherwise unused. |
| 65 | +func (l *starlarkBundleLang) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) { |
| 66 | +} |
| 67 | + |
| 68 | +func (*starlarkBundleLang) CheckFlags(fs *flag.FlagSet, c *config.Config) error { |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (*starlarkBundleLang) KnownDirectives() []string { |
| 73 | + return []string{starlarkBundleRootDirectiveName} |
| 74 | +} |
| 75 | + |
| 76 | +func (l *starlarkBundleLang) Configure(c *config.Config, rel string, f *rule.File) { |
| 77 | + if f != nil { |
| 78 | + for _, d := range f.Directives { |
| 79 | + if d.Key == starlarkBundleRootDirectiveName { |
| 80 | + if l.starlarkBundleRoot != nil { |
| 81 | + log.Fatalf("gazelle:%s should only be set once (refusing to override %q with %q)", starlarkBundleRootDirectiveName, *l.starlarkBundleRoot, d.Value) |
| 82 | + } |
| 83 | + l.starlarkBundleRoot = &rel |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Kinds returns a map of maps rule names (kinds) and information on how to |
| 90 | +// match and merge attributes that may be found in rules of those kinds. All |
| 91 | +// kinds of rules generated for this language may be found here. |
| 92 | +func (*starlarkBundleLang) Kinds() map[string]rule.KindInfo { |
| 93 | + kinds := map[string]rule.KindInfo{} |
| 94 | + maps.Copy(kinds, starlarkLibraryKindInfo) |
| 95 | + return kinds |
| 96 | +} |
| 97 | + |
| 98 | +// Loads returns .bzl files and symbols they define. Every rule generated by |
| 99 | +// GenerateRules, now or in the past, should be loadable from one of these |
| 100 | +// files. |
| 101 | +func (*starlarkBundleLang) Loads() []rule.LoadInfo { |
| 102 | + return []rule.LoadInfo{ |
| 103 | + starlarkLibraryLoadInfo, |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// Fix repairs deprecated usage of language-specific rules in f. This is called |
| 108 | +// before the file is indexed. Unless c.ShouldFix is true, fixes that delete or |
| 109 | +// rename rules should not be performed. |
| 110 | +func (*starlarkBundleLang) Fix(c *config.Config, f *rule.File) { |
| 111 | +} |
| 112 | + |
| 113 | +// Imports returns a list of ImportSpecs that can be used to import the rule r. |
| 114 | +// This is used to populate RuleIndex. |
| 115 | +// |
| 116 | +// If nil is returned, the rule will not be indexed. If any non-nil slice is |
| 117 | +// returned, including an empty slice, the rule will be indexed. |
| 118 | +func (b *starlarkBundleLang) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec { |
| 119 | + switch r.Kind() { |
| 120 | + case starlarkLibraryKind: |
| 121 | + return starlarkLibraryImports(c, r, f) |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +// Embeds returns a list of labels of rules that the given rule embeds. If a |
| 127 | +// rule is embedded by another importable rule of the same language, only the |
| 128 | +// embedding rule will be indexed. The embedding rule will inherit the imports |
| 129 | +// of the embedded rule. Since SkyLark doesn't support embedding this should |
| 130 | +// always return nil. |
| 131 | +func (*starlarkBundleLang) Embeds(r *rule.Rule, from label.Label) []label.Label { |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// Resolve translates imported libraries for a given rule into Bazel |
| 136 | +// dependencies. Information about imported libraries is returned for each rule |
| 137 | +// generated by language.GenerateRules in language.GenerateResult.Imports. |
| 138 | +// Resolve generates a "deps" attribute (or the appropriate language-specific |
| 139 | +// equivalent) for each import according to language-specific rules and |
| 140 | +// heuristics. |
| 141 | +func (*starlarkBundleLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, importsRaw interface{}, from label.Label) { |
| 142 | + switch r.Kind() { |
| 143 | + case starlarkLibraryKind: |
| 144 | + starlarkLibraryResolve(c, ix, rc, r, importsRaw, from) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// GenerateRules extracts build metadata from source files in a directory. |
| 149 | +// GenerateRules is called in each directory where an update is requested in |
| 150 | +// depth-first post-order. |
| 151 | +// |
| 152 | +// args contains the arguments for GenerateRules. This is passed as a struct to |
| 153 | +// avoid breaking implementations in the future when new fields are added. |
| 154 | +// |
| 155 | +// A GenerateResult struct is returned. Optional fields may be added to this |
| 156 | +// type in the future. |
| 157 | +// |
| 158 | +// Any non-fatal errors this function encounters should be logged using |
| 159 | +// log.Print. |
| 160 | +func (l *starlarkBundleLang) GenerateRules(args language.GenerateArgs) (result language.GenerateResult) { |
| 161 | + if l.starlarkBundleRoot == nil { |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + for _, r := range []language.GenerateResult{ |
| 166 | + starlarkLibararyGenerate(args), |
| 167 | + } { |
| 168 | + result.Gen = append(result.Gen, r.Gen...) |
| 169 | + result.Imports = append(result.Imports, r.Imports...) |
| 170 | + result.Empty = append(result.Empty, r.Empty...) |
| 171 | + } |
| 172 | + |
| 173 | + return |
| 174 | +} |
0 commit comments