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