Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. 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 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "regexp" |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 20 | "sort" |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 21 | "strings" |
| 22 | "sync" |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // Enforces visibility rules between modules. |
| 28 | // |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 29 | // Multi stage process: |
| 30 | // * First stage works bottom up, before defaults expansion, to check the syntax of the visibility |
| 31 | // rules that have been specified. |
| 32 | // |
| 33 | // * Second stage works bottom up to extract the package info for each package and store them in a |
| 34 | // map by package name. See package.go for functionality for this. |
| 35 | // |
| 36 | // * Third stage works bottom up to extract visibility information from the modules, parse it, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 37 | // create visibilityRule structures and store them in a map keyed by the module's |
| 38 | // qualifiedModuleName instance, i.e. //<pkg>:<name>. The map is stored in the context rather |
| 39 | // than a global variable for testing. Each test has its own Config so they do not share a map |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 40 | // and so can be run in parallel. If a module has no visibility specified then it uses the |
| 41 | // default package visibility if specified. |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 42 | // |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 43 | // * Fourth stage works top down and iterates over all the deps for each module. If the dep is in |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 44 | // the same package then it is automatically visible. Otherwise, for each dep it first extracts |
| 45 | // its visibilityRule from the config map. If one could not be found then it assumes that it is |
| 46 | // publicly visible. Otherwise, it calls the visibility rule to check that the module can see |
| 47 | // the dependency. If it cannot then an error is reported. |
| 48 | // |
| 49 | // TODO(b/130631145) - Make visibility work properly with prebuilts. |
| 50 | // TODO(b/130796911) - Make visibility work properly with defaults. |
| 51 | |
| 52 | // Patterns for the values that can be specified in visibility property. |
| 53 | const ( |
| 54 | packagePattern = `//([^/:]+(?:/[^/:]+)*)` |
| 55 | namePattern = `:([^/:]+)` |
| 56 | visibilityRulePattern = `^(?:` + packagePattern + `)?(?:` + namePattern + `)?$` |
| 57 | ) |
| 58 | |
| 59 | var visibilityRuleRegexp = regexp.MustCompile(visibilityRulePattern) |
| 60 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 61 | // A visibility rule is associated with a module and determines which other modules it is visible |
| 62 | // to, i.e. which other modules can depend on the rule's module. |
| 63 | type visibilityRule interface { |
| 64 | // Check to see whether this rules matches m. |
| 65 | // Returns true if it does, false otherwise. |
| 66 | matches(m qualifiedModuleName) bool |
| 67 | |
| 68 | String() string |
| 69 | } |
| 70 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 71 | // Describes the properties provided by a module that contain visibility rules. |
| 72 | type visibilityPropertyImpl struct { |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 73 | name string |
| 74 | stringsProperty *[]string |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | type visibilityProperty interface { |
| 78 | getName() string |
| 79 | getStrings() []string |
| 80 | } |
| 81 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 82 | func newVisibilityProperty(name string, stringsProperty *[]string) visibilityProperty { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 83 | return visibilityPropertyImpl{ |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 84 | name: name, |
| 85 | stringsProperty: stringsProperty, |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | func (p visibilityPropertyImpl) getName() string { |
| 90 | return p.name |
| 91 | } |
| 92 | |
| 93 | func (p visibilityPropertyImpl) getStrings() []string { |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 94 | return *p.stringsProperty |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 97 | // A compositeRule is a visibility rule composed from a list of atomic visibility rules. |
| 98 | // |
| 99 | // The list corresponds to the list of strings in the visibility property after defaults expansion. |
| 100 | // Even though //visibility:public is not allowed together with other rules in the visibility list |
| 101 | // of a single module, it is allowed here to permit a module to override an inherited visibility |
| 102 | // spec with public visibility. |
| 103 | // |
| 104 | // //visibility:private is not allowed in the same way, since we'd need to check for it during the |
| 105 | // defaults expansion to make that work. No non-private visibility rules are allowed in a |
| 106 | // compositeRule containing a privateRule. |
| 107 | // |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 108 | // This array will only be [] if all the rules are invalid and will behave as if visibility was |
| 109 | // ["//visibility:private"]. |
| 110 | type compositeRule []visibilityRule |
| 111 | |
| 112 | // A compositeRule matches if and only if any of its rules matches. |
| 113 | func (c compositeRule) matches(m qualifiedModuleName) bool { |
| 114 | for _, r := range c { |
| 115 | if r.matches(m) { |
| 116 | return true |
| 117 | } |
| 118 | } |
| 119 | return false |
| 120 | } |
| 121 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 122 | func (c compositeRule) String() string { |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 123 | return "[" + strings.Join(c.Strings(), ", ") + "]" |
| 124 | } |
| 125 | |
| 126 | func (c compositeRule) Strings() []string { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 127 | s := make([]string, 0, len(c)) |
| 128 | for _, r := range c { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 129 | s = append(s, r.String()) |
| 130 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 131 | return s |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // A packageRule is a visibility rule that matches modules in a specific package (i.e. directory). |
| 135 | type packageRule struct { |
| 136 | pkg string |
| 137 | } |
| 138 | |
| 139 | func (r packageRule) matches(m qualifiedModuleName) bool { |
| 140 | return m.pkg == r.pkg |
| 141 | } |
| 142 | |
| 143 | func (r packageRule) String() string { |
Martin Stjernholm | 01407c5 | 2020-05-13 01:54:21 +0100 | [diff] [blame] | 144 | return fmt.Sprintf("//%s", r.pkg) // :__pkg__ is the default, so skip it. |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // A subpackagesRule is a visibility rule that matches modules in a specific package (i.e. |
| 148 | // directory) or any of its subpackages (i.e. subdirectories). |
| 149 | type subpackagesRule struct { |
| 150 | pkgPrefix string |
| 151 | } |
| 152 | |
| 153 | func (r subpackagesRule) matches(m qualifiedModuleName) bool { |
| 154 | return isAncestor(r.pkgPrefix, m.pkg) |
| 155 | } |
| 156 | |
| 157 | func isAncestor(p1 string, p2 string) bool { |
Cole Faust | 3ac7db8 | 2023-01-12 10:36:17 -0800 | [diff] [blame^] | 158 | // Equivalent to strings.HasPrefix(p2+"/", p1+"/"), but without the string copies |
| 159 | // The check for a trailing slash is so that we don't consider sibling |
| 160 | // directories with common prefixes to be ancestors, e.g. "fooo/bar" should not be |
| 161 | // a descendant of "foo". |
| 162 | return strings.HasPrefix(p2, p1) && (len(p2) == len(p1) || p2[len(p1)] == '/') |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | func (r subpackagesRule) String() string { |
| 166 | return fmt.Sprintf("//%s:__subpackages__", r.pkgPrefix) |
| 167 | } |
| 168 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 169 | // visibilityRule for //visibility:public |
| 170 | type publicRule struct{} |
| 171 | |
| 172 | func (r publicRule) matches(_ qualifiedModuleName) bool { |
| 173 | return true |
| 174 | } |
| 175 | |
| 176 | func (r publicRule) String() string { |
| 177 | return "//visibility:public" |
| 178 | } |
| 179 | |
| 180 | // visibilityRule for //visibility:private |
| 181 | type privateRule struct{} |
| 182 | |
| 183 | func (r privateRule) matches(_ qualifiedModuleName) bool { |
| 184 | return false |
| 185 | } |
| 186 | |
| 187 | func (r privateRule) String() string { |
| 188 | return "//visibility:private" |
| 189 | } |
| 190 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 191 | var visibilityRuleMap = NewOnceKey("visibilityRuleMap") |
| 192 | |
| 193 | // The map from qualifiedModuleName to visibilityRule. |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 194 | func moduleToVisibilityRuleMap(config Config) *sync.Map { |
| 195 | return config.Once(visibilityRuleMap, func() interface{} { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 196 | return &sync.Map{} |
| 197 | }).(*sync.Map) |
| 198 | } |
| 199 | |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 200 | // Marker interface that identifies dependencies that are excluded from visibility |
| 201 | // enforcement. |
| 202 | type ExcludeFromVisibilityEnforcementTag interface { |
| 203 | blueprint.DependencyTag |
| 204 | |
| 205 | // Method that differentiates this interface from others. |
| 206 | ExcludeFromVisibilityEnforcement() |
| 207 | } |
| 208 | |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 209 | // The visibility mutators. |
| 210 | var PrepareForTestWithVisibility = FixtureRegisterWithContext(registerVisibilityMutators) |
| 211 | |
| 212 | func registerVisibilityMutators(ctx RegistrationContext) { |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 213 | ctx.PreArchMutators(RegisterVisibilityRuleChecker) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 214 | ctx.PreArchMutators(RegisterVisibilityRuleGatherer) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 215 | ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer) |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 216 | } |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 217 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 218 | // The rule checker needs to be registered before defaults expansion to correctly check that |
| 219 | // //visibility:xxx isn't combined with other packages in the same list in any one module. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 220 | func RegisterVisibilityRuleChecker(ctx RegisterMutatorsContext) { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 221 | ctx.BottomUp("visibilityRuleChecker", visibilityRuleChecker).Parallel() |
| 222 | } |
| 223 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 224 | // Registers the function that gathers the visibility rules for each module. |
| 225 | // |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 226 | // Visibility is not dependent on arch so this must be registered before the arch phase to avoid |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 227 | // having to process multiple variants for each module. This goes after defaults expansion to gather |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 228 | // the complete visibility lists from flat lists and after the package info is gathered to ensure |
| 229 | // that default_visibility is available. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 230 | func RegisterVisibilityRuleGatherer(ctx RegisterMutatorsContext) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 231 | ctx.BottomUp("visibilityRuleGatherer", visibilityRuleGatherer).Parallel() |
| 232 | } |
| 233 | |
| 234 | // This must be registered after the deps have been resolved. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 235 | func RegisterVisibilityRuleEnforcer(ctx RegisterMutatorsContext) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 236 | ctx.TopDown("visibilityRuleEnforcer", visibilityRuleEnforcer).Parallel() |
| 237 | } |
| 238 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 239 | // Checks the per-module visibility rule lists before defaults expansion. |
| 240 | func visibilityRuleChecker(ctx BottomUpMutatorContext) { |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 241 | qualified := createQualifiedModuleName(ctx.ModuleName(), ctx.ModuleDir()) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 242 | if m, ok := ctx.Module().(Module); ok { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 243 | visibilityProperties := m.visibilityProperties() |
| 244 | for _, p := range visibilityProperties { |
| 245 | if visibility := p.getStrings(); visibility != nil { |
| 246 | checkRules(ctx, qualified.pkg, p.getName(), visibility) |
| 247 | } |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 252 | func checkRules(ctx BaseModuleContext, currentPkg, property string, visibility []string) { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 253 | ruleCount := len(visibility) |
| 254 | if ruleCount == 0 { |
| 255 | // This prohibits an empty list as its meaning is unclear, e.g. it could mean no visibility and |
| 256 | // it could mean public visibility. Requiring at least one rule makes the owner's intent |
| 257 | // clearer. |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 258 | ctx.PropertyErrorf(property, "must contain at least one visibility rule") |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 259 | return |
| 260 | } |
| 261 | |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 262 | for i, v := range visibility { |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 263 | ok, pkg, name := splitRule(ctx, v, currentPkg, property) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 264 | if !ok { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 265 | continue |
| 266 | } |
| 267 | |
| 268 | if pkg == "visibility" { |
| 269 | switch name { |
| 270 | case "private", "public": |
| 271 | case "legacy_public": |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 272 | ctx.PropertyErrorf(property, "//visibility:legacy_public must not be used") |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 273 | continue |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 274 | case "override": |
| 275 | // This keyword does not create a rule so pretend it does not exist. |
| 276 | ruleCount -= 1 |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 277 | default: |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 278 | ctx.PropertyErrorf(property, "unrecognized visibility rule %q", v) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 279 | continue |
| 280 | } |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 281 | if name == "override" { |
| 282 | if i != 0 { |
| 283 | ctx.PropertyErrorf(property, `"%v" may only be used at the start of the visibility rules`, v) |
| 284 | } |
| 285 | } else if ruleCount != 1 { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 286 | ctx.PropertyErrorf(property, "cannot mix %q with any other visibility rules", v) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 287 | continue |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // If the current directory is not in the vendor tree then there are some additional |
| 292 | // restrictions on the rules. |
| 293 | if !isAncestor("vendor", currentPkg) { |
| 294 | if !isAllowedFromOutsideVendor(pkg, name) { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 295 | ctx.PropertyErrorf(property, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 296 | "%q is not allowed. Packages outside //vendor cannot make themselves visible to specific"+ |
| 297 | " targets within //vendor, they can only use //vendor:__subpackages__.", v) |
| 298 | continue |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // Gathers the flattened visibility rules after defaults expansion, parses the visibility |
| 305 | // properties, stores them in a map by qualifiedModuleName for retrieval during enforcement. |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 306 | // |
| 307 | // See ../README.md#Visibility for information on the format of the visibility rules. |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 308 | func visibilityRuleGatherer(ctx BottomUpMutatorContext) { |
| 309 | m, ok := ctx.Module().(Module) |
| 310 | if !ok { |
| 311 | return |
| 312 | } |
| 313 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 314 | qualifiedModuleId := m.qualifiedModuleId(ctx) |
| 315 | currentPkg := qualifiedModuleId.pkg |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 316 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 317 | // Parse the visibility rules that control access to the module and store them by id |
| 318 | // for use when enforcing the rules. |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 319 | primaryProperty := m.base().primaryVisibilityProperty |
| 320 | if primaryProperty != nil { |
| 321 | if visibility := primaryProperty.getStrings(); visibility != nil { |
| 322 | rule := parseRules(ctx, currentPkg, primaryProperty.getName(), visibility) |
| 323 | if rule != nil { |
| 324 | moduleToVisibilityRuleMap(ctx.Config()).Store(qualifiedModuleId, rule) |
| 325 | } |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 330 | func parseRules(ctx BaseModuleContext, currentPkg, property string, visibility []string) compositeRule { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 331 | rules := make(compositeRule, 0, len(visibility)) |
| 332 | hasPrivateRule := false |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 333 | hasPublicRule := false |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 334 | hasNonPrivateRule := false |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 335 | for _, v := range visibility { |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 336 | ok, pkg, name := splitRule(ctx, v, currentPkg, property) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 337 | if !ok { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 338 | continue |
| 339 | } |
| 340 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 341 | var r visibilityRule |
| 342 | isPrivateRule := false |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 343 | if pkg == "visibility" { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 344 | switch name { |
| 345 | case "private": |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 346 | r = privateRule{} |
| 347 | isPrivateRule = true |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 348 | case "public": |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 349 | r = publicRule{} |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 350 | hasPublicRule = true |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 351 | case "override": |
| 352 | // Discard all preceding rules and any state based on them. |
| 353 | rules = nil |
| 354 | hasPrivateRule = false |
| 355 | hasPublicRule = false |
| 356 | hasNonPrivateRule = false |
| 357 | // This does not actually create a rule so continue onto the next rule. |
| 358 | continue |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 359 | } |
| 360 | } else { |
| 361 | switch name { |
| 362 | case "__pkg__": |
| 363 | r = packageRule{pkg} |
| 364 | case "__subpackages__": |
| 365 | r = subpackagesRule{pkg} |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 366 | default: |
Liz Kammer | 873f4b6 | 2020-10-15 08:42:01 -0700 | [diff] [blame] | 367 | ctx.PropertyErrorf(property, "invalid visibility pattern %q. Must match "+ |
| 368 | " //<package>:<scope>, //<package> or :<scope> "+ |
| 369 | "where <scope> is one of \"__pkg__\", \"__subpackages__\"", |
| 370 | v) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 374 | if isPrivateRule { |
| 375 | hasPrivateRule = true |
| 376 | } else { |
| 377 | hasNonPrivateRule = true |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | rules = append(rules, r) |
| 381 | } |
| 382 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 383 | if hasPrivateRule && hasNonPrivateRule { |
| 384 | ctx.PropertyErrorf("visibility", |
| 385 | "cannot mix \"//visibility:private\" with any other visibility rules") |
| 386 | return compositeRule{privateRule{}} |
| 387 | } |
| 388 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 389 | if hasPublicRule { |
| 390 | // Public overrides all other rules so just return it. |
| 391 | return compositeRule{publicRule{}} |
| 392 | } |
| 393 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 394 | return rules |
| 395 | } |
| 396 | |
| 397 | func isAllowedFromOutsideVendor(pkg string, name string) bool { |
| 398 | if pkg == "vendor" { |
| 399 | if name == "__subpackages__" { |
| 400 | return true |
| 401 | } |
| 402 | return false |
| 403 | } |
| 404 | |
| 405 | return !isAncestor("vendor", pkg) |
| 406 | } |
| 407 | |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 408 | func splitRule(ctx BaseModuleContext, ruleExpression string, currentPkg, property string) (bool, string, string) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 409 | // Make sure that the rule is of the correct format. |
| 410 | matches := visibilityRuleRegexp.FindStringSubmatch(ruleExpression) |
| 411 | if ruleExpression == "" || matches == nil { |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 412 | // Visibility rule is invalid so ignore it. Keep going rather than aborting straight away to |
| 413 | // ensure all the rules on this module are checked. |
| 414 | ctx.PropertyErrorf(property, |
| 415 | "invalid visibility pattern %q must match"+ |
Liz Kammer | 873f4b6 | 2020-10-15 08:42:01 -0700 | [diff] [blame] | 416 | " //<package>:<scope>, //<package> or :<scope> "+ |
| 417 | "where <scope> is one of \"__pkg__\", \"__subpackages__\"", |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 418 | ruleExpression) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 419 | return false, "", "" |
| 420 | } |
| 421 | |
| 422 | // Extract the package and name. |
| 423 | pkg := matches[1] |
| 424 | name := matches[2] |
| 425 | |
| 426 | // Normalize the short hands |
| 427 | if pkg == "" { |
| 428 | pkg = currentPkg |
| 429 | } |
| 430 | if name == "" { |
| 431 | name = "__pkg__" |
| 432 | } |
| 433 | |
| 434 | return true, pkg, name |
| 435 | } |
| 436 | |
| 437 | func visibilityRuleEnforcer(ctx TopDownMutatorContext) { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 438 | if _, ok := ctx.Module().(Module); !ok { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 439 | return |
| 440 | } |
| 441 | |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 442 | qualified := createQualifiedModuleName(ctx.ModuleName(), ctx.ModuleDir()) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 443 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 444 | // Visit all the dependencies making sure that this module has access to them all. |
| 445 | ctx.VisitDirectDeps(func(dep Module) { |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 446 | // Ignore dependencies that have an ExcludeFromVisibilityEnforcementTag |
| 447 | tag := ctx.OtherModuleDependencyTag(dep) |
| 448 | if _, ok := tag.(ExcludeFromVisibilityEnforcementTag); ok { |
| 449 | return |
| 450 | } |
| 451 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 452 | depName := ctx.OtherModuleName(dep) |
| 453 | depDir := ctx.OtherModuleDir(dep) |
| 454 | depQualified := qualifiedModuleName{depDir, depName} |
| 455 | |
| 456 | // Targets are always visible to other targets in their own package. |
| 457 | if depQualified.pkg == qualified.pkg { |
| 458 | return |
| 459 | } |
| 460 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 461 | rule := effectiveVisibilityRules(ctx.Config(), depQualified) |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 462 | if !rule.matches(qualified) { |
Liz Kammer | e501bb4 | 2020-10-15 11:46:38 -0700 | [diff] [blame] | 463 | ctx.ModuleErrorf("depends on %s which is not visible to this module\nYou may need to add %q to its visibility", depQualified, "//"+ctx.ModuleDir()) |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 464 | } |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 465 | }) |
| 466 | } |
| 467 | |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 468 | // Default visibility is public. |
| 469 | var defaultVisibility = compositeRule{publicRule{}} |
| 470 | |
| 471 | // Return the effective visibility rules. |
| 472 | // |
| 473 | // If no rules have been specified this will return the default visibility rule |
| 474 | // which is currently //visibility:public. |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 475 | func effectiveVisibilityRules(config Config, qualified qualifiedModuleName) compositeRule { |
| 476 | moduleToVisibilityRule := moduleToVisibilityRuleMap(config) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 477 | value, ok := moduleToVisibilityRule.Load(qualified) |
| 478 | var rule compositeRule |
| 479 | if ok { |
| 480 | rule = value.(compositeRule) |
| 481 | } else { |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 482 | rule = packageDefaultVisibility(config, qualified) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 483 | } |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 484 | |
| 485 | // If no rule is specified then return the default visibility rule to avoid |
| 486 | // every caller having to treat nil as public. |
| 487 | if rule == nil { |
| 488 | rule = defaultVisibility |
| 489 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 490 | return rule |
| 491 | } |
| 492 | |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 493 | func createQualifiedModuleName(moduleName, dir string) qualifiedModuleName { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 494 | qualified := qualifiedModuleName{dir, moduleName} |
| 495 | return qualified |
| 496 | } |
Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 497 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 498 | func packageDefaultVisibility(config Config, moduleId qualifiedModuleName) compositeRule { |
| 499 | moduleToVisibilityRule := moduleToVisibilityRuleMap(config) |
Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 500 | packageQualifiedId := moduleId.getContainingPackageId() |
| 501 | for { |
| 502 | value, ok := moduleToVisibilityRule.Load(packageQualifiedId) |
| 503 | if ok { |
| 504 | return value.(compositeRule) |
| 505 | } |
| 506 | |
| 507 | if packageQualifiedId.isRootPackage() { |
| 508 | return nil |
| 509 | } |
| 510 | |
| 511 | packageQualifiedId = packageQualifiedId.getContainingPackageId() |
| 512 | } |
| 513 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 514 | |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 515 | type VisibilityRuleSet interface { |
| 516 | // Widen the visibility with some extra rules. |
| 517 | Widen(extra []string) error |
| 518 | |
| 519 | Strings() []string |
| 520 | } |
| 521 | |
| 522 | type visibilityRuleSet struct { |
| 523 | rules []string |
| 524 | } |
| 525 | |
| 526 | var _ VisibilityRuleSet = (*visibilityRuleSet)(nil) |
| 527 | |
| 528 | func (v *visibilityRuleSet) Widen(extra []string) error { |
| 529 | // Check the extra rules first just in case they are invalid. Otherwise, if |
| 530 | // the current visibility is public then the extra rules will just be ignored. |
| 531 | if len(extra) == 1 { |
| 532 | singularRule := extra[0] |
| 533 | switch singularRule { |
| 534 | case "//visibility:public": |
| 535 | // Public overrides everything so just discard any existing rules. |
| 536 | v.rules = extra |
| 537 | return nil |
| 538 | case "//visibility:private": |
| 539 | // Extending rule with private is an error. |
| 540 | return fmt.Errorf("%q does not widen the visibility", singularRule) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | if len(v.rules) == 1 { |
| 545 | switch v.rules[0] { |
| 546 | case "//visibility:public": |
| 547 | // No point in adding rules to something which is already public. |
| 548 | return nil |
| 549 | case "//visibility:private": |
| 550 | // Adding any rules to private means it is no longer private so the |
| 551 | // private can be discarded. |
| 552 | v.rules = nil |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | v.rules = FirstUniqueStrings(append(v.rules, extra...)) |
| 557 | sort.Strings(v.rules) |
| 558 | return nil |
| 559 | } |
| 560 | |
| 561 | func (v *visibilityRuleSet) Strings() []string { |
| 562 | return v.rules |
| 563 | } |
| 564 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 565 | // Get the effective visibility rules, i.e. the actual rules that affect the visibility of the |
| 566 | // property irrespective of where they are defined. |
| 567 | // |
| 568 | // Includes visibility rules specified by package default_visibility and/or on defaults. |
| 569 | // Short hand forms, e.g. //:__subpackages__ are replaced with their full form, e.g. |
| 570 | // //package/containing/rule:__subpackages__. |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 571 | func EffectiveVisibilityRules(ctx BaseModuleContext, module Module) VisibilityRuleSet { |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 572 | moduleName := ctx.OtherModuleName(module) |
| 573 | dir := ctx.OtherModuleDir(module) |
| 574 | qualified := qualifiedModuleName{dir, moduleName} |
| 575 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 576 | rule := effectiveVisibilityRules(ctx.Config(), qualified) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 577 | |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 578 | // Modules are implicitly visible to other modules in the same package, |
| 579 | // without checking the visibility rules. Here we need to add that visibility |
| 580 | // explicitly. |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 581 | if !rule.matches(qualified) { |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 582 | if len(rule) == 1 { |
| 583 | if _, ok := rule[0].(privateRule); ok { |
| 584 | // If the rule is //visibility:private we can't append another |
| 585 | // visibility to it. Semantically we need to convert it to a package |
| 586 | // visibility rule for the location where the result is used, but since |
| 587 | // modules are implicitly visible within the package we get the same |
| 588 | // result without any rule at all, so just make it an empty list to be |
| 589 | // appended below. |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 590 | rule = nil |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 591 | } |
| 592 | } |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 593 | rule = append(rule, packageRule{dir}) |
| 594 | } |
| 595 | |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 596 | return &visibilityRuleSet{rule.Strings()} |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 597 | } |
Paul Duffin | 5ec73ec | 2020-05-01 17:52:01 +0100 | [diff] [blame] | 598 | |
| 599 | // Clear the default visibility properties so they can be replaced. |
| 600 | func clearVisibilityProperties(module Module) { |
| 601 | module.base().visibilityPropertyInfo = nil |
| 602 | } |
| 603 | |
| 604 | // Add a property that contains visibility rules so that they are checked for |
| 605 | // correctness. |
| 606 | func AddVisibilityProperty(module Module, name string, stringsProperty *[]string) { |
| 607 | addVisibilityProperty(module, name, stringsProperty) |
| 608 | } |
| 609 | |
| 610 | func addVisibilityProperty(module Module, name string, stringsProperty *[]string) visibilityProperty { |
| 611 | base := module.base() |
| 612 | property := newVisibilityProperty(name, stringsProperty) |
| 613 | base.visibilityPropertyInfo = append(base.visibilityPropertyInfo, property) |
| 614 | return property |
| 615 | } |
| 616 | |
| 617 | // Set the primary visibility property. |
| 618 | // |
| 619 | // Also adds the property to the list of properties to be validated. |
| 620 | func setPrimaryVisibilityProperty(module Module, name string, stringsProperty *[]string) { |
| 621 | module.base().primaryVisibilityProperty = addVisibilityProperty(module, name, stringsProperty) |
| 622 | } |