1// Copyright 2023 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 15package python 16 17import ( 18 "github.com/bazelbuild/bazel-gazelle/config" 19 "github.com/bazelbuild/bazel-gazelle/rule" 20 "github.com/emirpasic/gods/sets/treeset" 21 godsutils "github.com/emirpasic/gods/utils" 22 "path/filepath" 23) 24 25// targetBuilder builds targets to be generated by Gazelle. 26type targetBuilder struct { 27 kind string 28 name string 29 pythonProjectRoot string 30 bzlPackage string 31 srcs *treeset.Set 32 siblingSrcs *treeset.Set 33 deps *treeset.Set 34 resolvedDeps *treeset.Set 35 visibility *treeset.Set 36 main *string 37 imports []string 38 testonly bool 39} 40 41// newTargetBuilder constructs a new targetBuilder. 42func newTargetBuilder(kind, name, pythonProjectRoot, bzlPackage string, siblingSrcs *treeset.Set) *targetBuilder { 43 return &targetBuilder{ 44 kind: kind, 45 name: name, 46 pythonProjectRoot: pythonProjectRoot, 47 bzlPackage: bzlPackage, 48 srcs: treeset.NewWith(godsutils.StringComparator), 49 siblingSrcs: siblingSrcs, 50 deps: treeset.NewWith(moduleComparator), 51 resolvedDeps: treeset.NewWith(godsutils.StringComparator), 52 visibility: treeset.NewWith(godsutils.StringComparator), 53 } 54} 55 56// addSrc adds a single src to the target. 57func (t *targetBuilder) addSrc(src string) *targetBuilder { 58 t.srcs.Add(src) 59 return t 60} 61 62// addSrcs copies all values from the provided srcs to the target. 63func (t *targetBuilder) addSrcs(srcs *treeset.Set) *targetBuilder { 64 it := srcs.Iterator() 65 for it.Next() { 66 t.srcs.Add(it.Value().(string)) 67 } 68 return t 69} 70 71// addModuleDependency adds a single module dep to the target. 72func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder { 73 fileName := dep.Name + ".py" 74 if dep.From != "" { 75 fileName = dep.From + ".py" 76 } 77 if t.siblingSrcs.Contains(fileName) && fileName != filepath.Base(dep.Filepath) { 78 // importing another module from the same package, converting to absolute imports to make 79 // dependency resolution easier 80 dep.Name = importSpecFromSrc(t.pythonProjectRoot, t.bzlPackage, fileName).Imp 81 } 82 t.deps.Add(dep) 83 return t 84} 85 86// addModuleDependencies copies all values from the provided deps to the target. 87func (t *targetBuilder) addModuleDependencies(deps *treeset.Set) *targetBuilder { 88 it := deps.Iterator() 89 for it.Next() { 90 t.addModuleDependency(it.Value().(module)) 91 } 92 return t 93} 94 95// addResolvedDependency adds a single dependency the target that has already 96// been resolved or generated. The Resolver step doesn't process it further. 97func (t *targetBuilder) addResolvedDependency(dep string) *targetBuilder { 98 t.resolvedDeps.Add(dep) 99 return t 100} 101 102// addVisibility adds a visibility to the target. 103func (t *targetBuilder) addVisibility(visibility string) *targetBuilder { 104 t.visibility.Add(visibility) 105 return t 106} 107 108// setMain sets the main file to the target. 109func (t *targetBuilder) setMain(main string) *targetBuilder { 110 t.main = &main 111 return t 112} 113 114// setTestonly sets the testonly attribute to true. 115func (t *targetBuilder) setTestonly() *targetBuilder { 116 t.testonly = true 117 return t 118} 119 120// generateImportsAttribute generates the imports attribute. 121// These are a list of import directories to be added to the PYTHONPATH. In our 122// case, the value we add is on Bazel sub-packages to be able to perform imports 123// relative to the root project package. 124func (t *targetBuilder) generateImportsAttribute() *targetBuilder { 125 if t.pythonProjectRoot == "" { 126 // When gazelle:python_root is not set or is at the root of the repo, we don't need 127 // to set imports, because that's the Bazel's default. 128 return t 129 } 130 p, _ := filepath.Rel(t.bzlPackage, t.pythonProjectRoot) 131 p = filepath.Clean(p) 132 if p == "." { 133 return t 134 } 135 t.imports = []string{p} 136 return t 137} 138 139// build returns the assembled *rule.Rule for the target. 140func (t *targetBuilder) build() *rule.Rule { 141 r := rule.NewRule(t.kind, t.name) 142 if !t.srcs.Empty() { 143 r.SetAttr("srcs", t.srcs.Values()) 144 } 145 if !t.visibility.Empty() { 146 r.SetAttr("visibility", t.visibility.Values()) 147 } 148 if t.main != nil { 149 r.SetAttr("main", *t.main) 150 } 151 if t.imports != nil { 152 r.SetAttr("imports", t.imports) 153 } 154 if !t.deps.Empty() { 155 r.SetPrivateAttr(config.GazelleImportsKey, t.deps) 156 } 157 if t.testonly { 158 r.SetAttr("testonly", true) 159 } 160 r.SetPrivateAttr(resolvedDepsKey, t.resolvedDeps) 161 return r 162} 163