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