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 { |
| 158 | return strings.HasPrefix(p2+"/", p1+"/") |
| 159 | } |
| 160 | |
| 161 | func (r subpackagesRule) String() string { |
| 162 | return fmt.Sprintf("//%s:__subpackages__", r.pkgPrefix) |
| 163 | } |
| 164 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 165 | // visibilityRule for //visibility:public |
| 166 | type publicRule struct{} |
| 167 | |
| 168 | func (r publicRule) matches(_ qualifiedModuleName) bool { |
| 169 | return true |
| 170 | } |
| 171 | |
| 172 | func (r publicRule) String() string { |
| 173 | return "//visibility:public" |
| 174 | } |
| 175 | |
| 176 | // visibilityRule for //visibility:private |
| 177 | type privateRule struct{} |
| 178 | |
| 179 | func (r privateRule) matches(_ qualifiedModuleName) bool { |
| 180 | return false |
| 181 | } |
| 182 | |
| 183 | func (r privateRule) String() string { |
| 184 | return "//visibility:private" |
| 185 | } |
| 186 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 187 | var visibilityRuleMap = NewOnceKey("visibilityRuleMap") |
| 188 | |
| 189 | // The map from qualifiedModuleName to visibilityRule. |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 190 | func moduleToVisibilityRuleMap(config Config) *sync.Map { |
| 191 | return config.Once(visibilityRuleMap, func() interface{} { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 192 | return &sync.Map{} |
| 193 | }).(*sync.Map) |
| 194 | } |
| 195 | |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 196 | // Marker interface that identifies dependencies that are excluded from visibility |
| 197 | // enforcement. |
| 198 | type ExcludeFromVisibilityEnforcementTag interface { |
| 199 | blueprint.DependencyTag |
| 200 | |
| 201 | // Method that differentiates this interface from others. |
| 202 | ExcludeFromVisibilityEnforcement() |
| 203 | } |
| 204 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 205 | // The rule checker needs to be registered before defaults expansion to correctly check that |
| 206 | // //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] | 207 | func RegisterVisibilityRuleChecker(ctx RegisterMutatorsContext) { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 208 | ctx.BottomUp("visibilityRuleChecker", visibilityRuleChecker).Parallel() |
| 209 | } |
| 210 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 211 | // Registers the function that gathers the visibility rules for each module. |
| 212 | // |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 213 | // 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] | 214 | // 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] | 215 | // the complete visibility lists from flat lists and after the package info is gathered to ensure |
| 216 | // that default_visibility is available. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 217 | func RegisterVisibilityRuleGatherer(ctx RegisterMutatorsContext) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 218 | ctx.BottomUp("visibilityRuleGatherer", visibilityRuleGatherer).Parallel() |
| 219 | } |
| 220 | |
| 221 | // This must be registered after the deps have been resolved. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 222 | func RegisterVisibilityRuleEnforcer(ctx RegisterMutatorsContext) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 223 | ctx.TopDown("visibilityRuleEnforcer", visibilityRuleEnforcer).Parallel() |
| 224 | } |
| 225 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 226 | // Checks the per-module visibility rule lists before defaults expansion. |
| 227 | func visibilityRuleChecker(ctx BottomUpMutatorContext) { |
| 228 | qualified := createQualifiedModuleName(ctx) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 229 | if m, ok := ctx.Module().(Module); ok { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 230 | visibilityProperties := m.visibilityProperties() |
| 231 | for _, p := range visibilityProperties { |
| 232 | if visibility := p.getStrings(); visibility != nil { |
| 233 | checkRules(ctx, qualified.pkg, p.getName(), visibility) |
| 234 | } |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 239 | func checkRules(ctx BaseModuleContext, currentPkg, property string, visibility []string) { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 240 | ruleCount := len(visibility) |
| 241 | if ruleCount == 0 { |
| 242 | // This prohibits an empty list as its meaning is unclear, e.g. it could mean no visibility and |
| 243 | // it could mean public visibility. Requiring at least one rule makes the owner's intent |
| 244 | // clearer. |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 245 | ctx.PropertyErrorf(property, "must contain at least one visibility rule") |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 246 | return |
| 247 | } |
| 248 | |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 249 | for i, v := range visibility { |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 250 | ok, pkg, name := splitRule(ctx, v, currentPkg, property) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 251 | if !ok { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 252 | continue |
| 253 | } |
| 254 | |
| 255 | if pkg == "visibility" { |
| 256 | switch name { |
| 257 | case "private", "public": |
| 258 | case "legacy_public": |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 259 | ctx.PropertyErrorf(property, "//visibility:legacy_public must not be used") |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 260 | continue |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 261 | case "override": |
| 262 | // This keyword does not create a rule so pretend it does not exist. |
| 263 | ruleCount -= 1 |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 264 | default: |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 265 | ctx.PropertyErrorf(property, "unrecognized visibility rule %q", v) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 266 | continue |
| 267 | } |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 268 | if name == "override" { |
| 269 | if i != 0 { |
| 270 | ctx.PropertyErrorf(property, `"%v" may only be used at the start of the visibility rules`, v) |
| 271 | } |
| 272 | } else if ruleCount != 1 { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 273 | ctx.PropertyErrorf(property, "cannot mix %q with any other visibility rules", v) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 274 | continue |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // If the current directory is not in the vendor tree then there are some additional |
| 279 | // restrictions on the rules. |
| 280 | if !isAncestor("vendor", currentPkg) { |
| 281 | if !isAllowedFromOutsideVendor(pkg, name) { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 282 | ctx.PropertyErrorf(property, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 283 | "%q is not allowed. Packages outside //vendor cannot make themselves visible to specific"+ |
| 284 | " targets within //vendor, they can only use //vendor:__subpackages__.", v) |
| 285 | continue |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Gathers the flattened visibility rules after defaults expansion, parses the visibility |
| 292 | // properties, stores them in a map by qualifiedModuleName for retrieval during enforcement. |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 293 | // |
| 294 | // 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] | 295 | func visibilityRuleGatherer(ctx BottomUpMutatorContext) { |
| 296 | m, ok := ctx.Module().(Module) |
| 297 | if !ok { |
| 298 | return |
| 299 | } |
| 300 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 301 | qualifiedModuleId := m.qualifiedModuleId(ctx) |
| 302 | currentPkg := qualifiedModuleId.pkg |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 303 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 304 | // Parse the visibility rules that control access to the module and store them by id |
| 305 | // for use when enforcing the rules. |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 306 | primaryProperty := m.base().primaryVisibilityProperty |
| 307 | if primaryProperty != nil { |
| 308 | if visibility := primaryProperty.getStrings(); visibility != nil { |
| 309 | rule := parseRules(ctx, currentPkg, primaryProperty.getName(), visibility) |
| 310 | if rule != nil { |
| 311 | moduleToVisibilityRuleMap(ctx.Config()).Store(qualifiedModuleId, rule) |
| 312 | } |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 317 | func parseRules(ctx BaseModuleContext, currentPkg, property string, visibility []string) compositeRule { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 318 | rules := make(compositeRule, 0, len(visibility)) |
| 319 | hasPrivateRule := false |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 320 | hasPublicRule := false |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 321 | hasNonPrivateRule := false |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 322 | for _, v := range visibility { |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 323 | ok, pkg, name := splitRule(ctx, v, currentPkg, property) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 324 | if !ok { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 325 | continue |
| 326 | } |
| 327 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 328 | var r visibilityRule |
| 329 | isPrivateRule := false |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 330 | if pkg == "visibility" { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 331 | switch name { |
| 332 | case "private": |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 333 | r = privateRule{} |
| 334 | isPrivateRule = true |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 335 | case "public": |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 336 | r = publicRule{} |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 337 | hasPublicRule = true |
Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 338 | case "override": |
| 339 | // Discard all preceding rules and any state based on them. |
| 340 | rules = nil |
| 341 | hasPrivateRule = false |
| 342 | hasPublicRule = false |
| 343 | hasNonPrivateRule = false |
| 344 | // This does not actually create a rule so continue onto the next rule. |
| 345 | continue |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 346 | } |
| 347 | } else { |
| 348 | switch name { |
| 349 | case "__pkg__": |
| 350 | r = packageRule{pkg} |
| 351 | case "__subpackages__": |
| 352 | r = subpackagesRule{pkg} |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 353 | default: |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 354 | continue |
| 355 | } |
| 356 | } |
| 357 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 358 | if isPrivateRule { |
| 359 | hasPrivateRule = true |
| 360 | } else { |
| 361 | hasNonPrivateRule = true |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | rules = append(rules, r) |
| 365 | } |
| 366 | |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 367 | if hasPrivateRule && hasNonPrivateRule { |
| 368 | ctx.PropertyErrorf("visibility", |
| 369 | "cannot mix \"//visibility:private\" with any other visibility rules") |
| 370 | return compositeRule{privateRule{}} |
| 371 | } |
| 372 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 373 | if hasPublicRule { |
| 374 | // Public overrides all other rules so just return it. |
| 375 | return compositeRule{publicRule{}} |
| 376 | } |
| 377 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 378 | return rules |
| 379 | } |
| 380 | |
| 381 | func isAllowedFromOutsideVendor(pkg string, name string) bool { |
| 382 | if pkg == "vendor" { |
| 383 | if name == "__subpackages__" { |
| 384 | return true |
| 385 | } |
| 386 | return false |
| 387 | } |
| 388 | |
| 389 | return !isAncestor("vendor", pkg) |
| 390 | } |
| 391 | |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 392 | func splitRule(ctx BaseModuleContext, ruleExpression string, currentPkg, property string) (bool, string, string) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 393 | // Make sure that the rule is of the correct format. |
| 394 | matches := visibilityRuleRegexp.FindStringSubmatch(ruleExpression) |
| 395 | if ruleExpression == "" || matches == nil { |
Paul Duffin | 0c83aba | 2020-05-01 18:13:36 +0100 | [diff] [blame] | 396 | // Visibility rule is invalid so ignore it. Keep going rather than aborting straight away to |
| 397 | // ensure all the rules on this module are checked. |
| 398 | ctx.PropertyErrorf(property, |
| 399 | "invalid visibility pattern %q must match"+ |
| 400 | " //<package>:<module>, //<package> or :<module>", |
| 401 | ruleExpression) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 402 | return false, "", "" |
| 403 | } |
| 404 | |
| 405 | // Extract the package and name. |
| 406 | pkg := matches[1] |
| 407 | name := matches[2] |
| 408 | |
| 409 | // Normalize the short hands |
| 410 | if pkg == "" { |
| 411 | pkg = currentPkg |
| 412 | } |
| 413 | if name == "" { |
| 414 | name = "__pkg__" |
| 415 | } |
| 416 | |
| 417 | return true, pkg, name |
| 418 | } |
| 419 | |
| 420 | func visibilityRuleEnforcer(ctx TopDownMutatorContext) { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 421 | if _, ok := ctx.Module().(Module); !ok { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 422 | return |
| 423 | } |
| 424 | |
| 425 | qualified := createQualifiedModuleName(ctx) |
| 426 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 427 | // Visit all the dependencies making sure that this module has access to them all. |
| 428 | ctx.VisitDirectDeps(func(dep Module) { |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 429 | // Ignore dependencies that have an ExcludeFromVisibilityEnforcementTag |
| 430 | tag := ctx.OtherModuleDependencyTag(dep) |
| 431 | if _, ok := tag.(ExcludeFromVisibilityEnforcementTag); ok { |
| 432 | return |
| 433 | } |
| 434 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 435 | depName := ctx.OtherModuleName(dep) |
| 436 | depDir := ctx.OtherModuleDir(dep) |
| 437 | depQualified := qualifiedModuleName{depDir, depName} |
| 438 | |
| 439 | // Targets are always visible to other targets in their own package. |
| 440 | if depQualified.pkg == qualified.pkg { |
| 441 | return |
| 442 | } |
| 443 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 444 | rule := effectiveVisibilityRules(ctx.Config(), depQualified) |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 445 | if !rule.matches(qualified) { |
Liz Kammer | e501bb4 | 2020-10-15 11:46:38 -0700 | [diff] [blame^] | 446 | 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] | 447 | } |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 448 | }) |
| 449 | } |
| 450 | |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 451 | // Default visibility is public. |
| 452 | var defaultVisibility = compositeRule{publicRule{}} |
| 453 | |
| 454 | // Return the effective visibility rules. |
| 455 | // |
| 456 | // If no rules have been specified this will return the default visibility rule |
| 457 | // which is currently //visibility:public. |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 458 | func effectiveVisibilityRules(config Config, qualified qualifiedModuleName) compositeRule { |
| 459 | moduleToVisibilityRule := moduleToVisibilityRuleMap(config) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 460 | value, ok := moduleToVisibilityRule.Load(qualified) |
| 461 | var rule compositeRule |
| 462 | if ok { |
| 463 | rule = value.(compositeRule) |
| 464 | } else { |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 465 | rule = packageDefaultVisibility(config, qualified) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 466 | } |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 467 | |
| 468 | // If no rule is specified then return the default visibility rule to avoid |
| 469 | // every caller having to treat nil as public. |
| 470 | if rule == nil { |
| 471 | rule = defaultVisibility |
| 472 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 473 | return rule |
| 474 | } |
| 475 | |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 476 | func createQualifiedModuleName(ctx BaseModuleContext) qualifiedModuleName { |
| 477 | moduleName := ctx.ModuleName() |
| 478 | dir := ctx.ModuleDir() |
| 479 | qualified := qualifiedModuleName{dir, moduleName} |
| 480 | return qualified |
| 481 | } |
Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 482 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 483 | func packageDefaultVisibility(config Config, moduleId qualifiedModuleName) compositeRule { |
| 484 | moduleToVisibilityRule := moduleToVisibilityRuleMap(config) |
Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 485 | packageQualifiedId := moduleId.getContainingPackageId() |
| 486 | for { |
| 487 | value, ok := moduleToVisibilityRule.Load(packageQualifiedId) |
| 488 | if ok { |
| 489 | return value.(compositeRule) |
| 490 | } |
| 491 | |
| 492 | if packageQualifiedId.isRootPackage() { |
| 493 | return nil |
| 494 | } |
| 495 | |
| 496 | packageQualifiedId = packageQualifiedId.getContainingPackageId() |
| 497 | } |
| 498 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 499 | |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 500 | type VisibilityRuleSet interface { |
| 501 | // Widen the visibility with some extra rules. |
| 502 | Widen(extra []string) error |
| 503 | |
| 504 | Strings() []string |
| 505 | } |
| 506 | |
| 507 | type visibilityRuleSet struct { |
| 508 | rules []string |
| 509 | } |
| 510 | |
| 511 | var _ VisibilityRuleSet = (*visibilityRuleSet)(nil) |
| 512 | |
| 513 | func (v *visibilityRuleSet) Widen(extra []string) error { |
| 514 | // Check the extra rules first just in case they are invalid. Otherwise, if |
| 515 | // the current visibility is public then the extra rules will just be ignored. |
| 516 | if len(extra) == 1 { |
| 517 | singularRule := extra[0] |
| 518 | switch singularRule { |
| 519 | case "//visibility:public": |
| 520 | // Public overrides everything so just discard any existing rules. |
| 521 | v.rules = extra |
| 522 | return nil |
| 523 | case "//visibility:private": |
| 524 | // Extending rule with private is an error. |
| 525 | return fmt.Errorf("%q does not widen the visibility", singularRule) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if len(v.rules) == 1 { |
| 530 | switch v.rules[0] { |
| 531 | case "//visibility:public": |
| 532 | // No point in adding rules to something which is already public. |
| 533 | return nil |
| 534 | case "//visibility:private": |
| 535 | // Adding any rules to private means it is no longer private so the |
| 536 | // private can be discarded. |
| 537 | v.rules = nil |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | v.rules = FirstUniqueStrings(append(v.rules, extra...)) |
| 542 | sort.Strings(v.rules) |
| 543 | return nil |
| 544 | } |
| 545 | |
| 546 | func (v *visibilityRuleSet) Strings() []string { |
| 547 | return v.rules |
| 548 | } |
| 549 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 550 | // Get the effective visibility rules, i.e. the actual rules that affect the visibility of the |
| 551 | // property irrespective of where they are defined. |
| 552 | // |
| 553 | // Includes visibility rules specified by package default_visibility and/or on defaults. |
| 554 | // Short hand forms, e.g. //:__subpackages__ are replaced with their full form, e.g. |
| 555 | // //package/containing/rule:__subpackages__. |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 556 | func EffectiveVisibilityRules(ctx BaseModuleContext, module Module) VisibilityRuleSet { |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 557 | moduleName := ctx.OtherModuleName(module) |
| 558 | dir := ctx.OtherModuleDir(module) |
| 559 | qualified := qualifiedModuleName{dir, moduleName} |
| 560 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 561 | rule := effectiveVisibilityRules(ctx.Config(), qualified) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 562 | |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 563 | // Modules are implicitly visible to other modules in the same package, |
| 564 | // without checking the visibility rules. Here we need to add that visibility |
| 565 | // explicitly. |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 566 | if !rule.matches(qualified) { |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 567 | if len(rule) == 1 { |
| 568 | if _, ok := rule[0].(privateRule); ok { |
| 569 | // If the rule is //visibility:private we can't append another |
| 570 | // visibility to it. Semantically we need to convert it to a package |
| 571 | // visibility rule for the location where the result is used, but since |
| 572 | // modules are implicitly visible within the package we get the same |
| 573 | // result without any rule at all, so just make it an empty list to be |
| 574 | // appended below. |
Paul Duffin | d99d997 | 2020-09-29 16:00:55 +0100 | [diff] [blame] | 575 | rule = nil |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 576 | } |
| 577 | } |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 578 | rule = append(rule, packageRule{dir}) |
| 579 | } |
| 580 | |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 581 | return &visibilityRuleSet{rule.Strings()} |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 582 | } |
Paul Duffin | 5ec73ec | 2020-05-01 17:52:01 +0100 | [diff] [blame] | 583 | |
| 584 | // Clear the default visibility properties so they can be replaced. |
| 585 | func clearVisibilityProperties(module Module) { |
| 586 | module.base().visibilityPropertyInfo = nil |
| 587 | } |
| 588 | |
| 589 | // Add a property that contains visibility rules so that they are checked for |
| 590 | // correctness. |
| 591 | func AddVisibilityProperty(module Module, name string, stringsProperty *[]string) { |
| 592 | addVisibilityProperty(module, name, stringsProperty) |
| 593 | } |
| 594 | |
| 595 | func addVisibilityProperty(module Module, name string, stringsProperty *[]string) visibilityProperty { |
| 596 | base := module.base() |
| 597 | property := newVisibilityProperty(name, stringsProperty) |
| 598 | base.visibilityPropertyInfo = append(base.visibilityPropertyInfo, property) |
| 599 | return property |
| 600 | } |
| 601 | |
| 602 | // Set the primary visibility property. |
| 603 | // |
| 604 | // Also adds the property to the list of properties to be validated. |
| 605 | func setPrimaryVisibilityProperty(module Module, name string, stringsProperty *[]string) { |
| 606 | module.base().primaryVisibilityProperty = addVisibilityProperty(module, name, stringsProperty) |
| 607 | } |