| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 ( | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 18 | "errors" | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 19 | "fmt" | 
|  | 20 | "path/filepath" | 
|  | 21 | "sort" | 
|  | 22 | "strconv" | 
|  | 23 | "strings" | 
|  | 24 | "sync" | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | "github.com/google/blueprint" | 
|  | 27 | ) | 
|  | 28 |  | 
|  | 29 | // This file implements namespaces | 
|  | 30 | const ( | 
|  | 31 | namespacePrefix = "//" | 
|  | 32 | modulePrefix    = ":" | 
|  | 33 | ) | 
|  | 34 |  | 
|  | 35 | func init() { | 
|  | 36 | RegisterModuleType("soong_namespace", NamespaceFactory) | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | // threadsafe sorted list | 
|  | 40 | type sortedNamespaces struct { | 
|  | 41 | lock   sync.Mutex | 
|  | 42 | items  []*Namespace | 
|  | 43 | sorted bool | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | func (s *sortedNamespaces) add(namespace *Namespace) { | 
|  | 47 | s.lock.Lock() | 
|  | 48 | defer s.lock.Unlock() | 
|  | 49 | if s.sorted { | 
|  | 50 | panic("It is not supported to call sortedNamespaces.add() after sortedNamespaces.sortedItems()") | 
|  | 51 | } | 
|  | 52 | s.items = append(s.items, namespace) | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | func (s *sortedNamespaces) sortedItems() []*Namespace { | 
|  | 56 | s.lock.Lock() | 
|  | 57 | defer s.lock.Unlock() | 
|  | 58 | if !s.sorted { | 
|  | 59 | less := func(i int, j int) bool { | 
|  | 60 | return s.items[i].Path < s.items[j].Path | 
|  | 61 | } | 
|  | 62 | sort.Slice(s.items, less) | 
|  | 63 | s.sorted = true | 
|  | 64 | } | 
|  | 65 | return s.items | 
|  | 66 | } | 
|  | 67 |  | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 68 | func (s *sortedNamespaces) index(namespace *Namespace) int { | 
|  | 69 | for i, candidate := range s.sortedItems() { | 
|  | 70 | if namespace == candidate { | 
|  | 71 | return i | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 | return -1 | 
|  | 75 | } | 
|  | 76 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 77 | // A NameResolver implements blueprint.NameInterface, and implements the logic to | 
|  | 78 | // find a module from namespaces based on a query string. | 
|  | 79 | // A query string can be a module name or can be be "//namespace_path:module_path" | 
|  | 80 | type NameResolver struct { | 
|  | 81 | rootNamespace *Namespace | 
|  | 82 |  | 
|  | 83 | // id counter for atomic.AddInt32 | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 84 | nextNamespaceId int32 | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 85 |  | 
|  | 86 | // All namespaces, without duplicates. | 
|  | 87 | sortedNamespaces sortedNamespaces | 
|  | 88 |  | 
|  | 89 | // Map from dir to namespace. Will have duplicates if two dirs are part of the same namespace. | 
|  | 90 | namespacesByDir sync.Map // if generics were supported, this would be sync.Map[string]*Namespace | 
|  | 91 |  | 
|  | 92 | // func telling whether to export a namespace to Kati | 
|  | 93 | namespaceExportFilter func(*Namespace) bool | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | func NewNameResolver(namespaceExportFilter func(*Namespace) bool) *NameResolver { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 97 | r := &NameResolver{ | 
| Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 98 | namespacesByDir:       sync.Map{}, | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 99 | namespaceExportFilter: namespaceExportFilter, | 
|  | 100 | } | 
|  | 101 | r.rootNamespace = r.newNamespace(".") | 
|  | 102 | r.rootNamespace.visibleNamespaces = []*Namespace{r.rootNamespace} | 
|  | 103 | r.addNamespace(r.rootNamespace) | 
|  | 104 |  | 
|  | 105 | return r | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | func (r *NameResolver) newNamespace(path string) *Namespace { | 
|  | 109 | namespace := NewNamespace(path) | 
|  | 110 |  | 
|  | 111 | namespace.exportToKati = r.namespaceExportFilter(namespace) | 
|  | 112 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 113 | return namespace | 
|  | 114 | } | 
|  | 115 |  | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 116 | func (r *NameResolver) addNewNamespaceForModule(module *NamespaceModule, path string) error { | 
|  | 117 | fileName := filepath.Base(path) | 
|  | 118 | if fileName != "Android.bp" { | 
|  | 119 | return errors.New("A namespace may only be declared in a file named Android.bp") | 
|  | 120 | } | 
|  | 121 | dir := filepath.Dir(path) | 
|  | 122 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 123 | namespace := r.newNamespace(dir) | 
|  | 124 | module.namespace = namespace | 
|  | 125 | module.resolver = r | 
|  | 126 | namespace.importedNamespaceNames = module.properties.Imports | 
|  | 127 | return r.addNamespace(namespace) | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | func (r *NameResolver) addNamespace(namespace *Namespace) (err error) { | 
|  | 131 | existingNamespace, exists := r.namespaceAt(namespace.Path) | 
|  | 132 | if exists { | 
|  | 133 | if existingNamespace.Path == namespace.Path { | 
|  | 134 | return fmt.Errorf("namespace %v already exists", namespace.Path) | 
|  | 135 | } else { | 
|  | 136 | // It would probably confuse readers if namespaces were declared anywhere but | 
|  | 137 | // the top of the file, so we forbid declaring namespaces after anything else. | 
|  | 138 | return fmt.Errorf("a namespace must be the first module in the file") | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 | r.sortedNamespaces.add(namespace) | 
|  | 142 |  | 
|  | 143 | r.namespacesByDir.Store(namespace.Path, namespace) | 
|  | 144 | return nil | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | // non-recursive check for namespace | 
|  | 148 | func (r *NameResolver) namespaceAt(path string) (namespace *Namespace, found bool) { | 
|  | 149 | mapVal, found := r.namespacesByDir.Load(path) | 
|  | 150 | if !found { | 
|  | 151 | return nil, false | 
|  | 152 | } | 
|  | 153 | return mapVal.(*Namespace), true | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | // recursive search upward for a namespace | 
|  | 157 | func (r *NameResolver) findNamespace(path string) (namespace *Namespace) { | 
|  | 158 | namespace, found := r.namespaceAt(path) | 
|  | 159 | if found { | 
|  | 160 | return namespace | 
|  | 161 | } | 
|  | 162 | parentDir := filepath.Dir(path) | 
|  | 163 | if parentDir == path { | 
|  | 164 | return nil | 
|  | 165 | } | 
|  | 166 | namespace = r.findNamespace(parentDir) | 
|  | 167 | r.namespacesByDir.Store(path, namespace) | 
|  | 168 | return namespace | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | func (r *NameResolver) NewModule(ctx blueprint.NamespaceContext, moduleGroup blueprint.ModuleGroup, module blueprint.Module) (namespace blueprint.Namespace, errs []error) { | 
|  | 172 | // if this module is a namespace, then save it to our list of namespaces | 
|  | 173 | newNamespace, ok := module.(*NamespaceModule) | 
|  | 174 | if ok { | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 175 | err := r.addNewNamespaceForModule(newNamespace, ctx.ModulePath()) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 176 | if err != nil { | 
|  | 177 | return nil, []error{err} | 
|  | 178 | } | 
|  | 179 | return nil, nil | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | // if this module is not a namespace, then save it into the appropriate namespace | 
|  | 183 | ns := r.findNamespaceFromCtx(ctx) | 
|  | 184 |  | 
|  | 185 | _, errs = ns.moduleContainer.NewModule(ctx, moduleGroup, module) | 
|  | 186 | if len(errs) > 0 { | 
|  | 187 | return nil, errs | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | amod, ok := module.(Module) | 
|  | 191 | if ok { | 
|  | 192 | // inform the module whether its namespace is one that we want to export to Make | 
|  | 193 | amod.base().commonProperties.NamespaceExportedToMake = ns.exportToKati | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | return ns, nil | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | func (r *NameResolver) AllModules() []blueprint.ModuleGroup { | 
|  | 200 | childLists := [][]blueprint.ModuleGroup{} | 
|  | 201 | totalCount := 0 | 
|  | 202 | for _, namespace := range r.sortedNamespaces.sortedItems() { | 
|  | 203 | newModules := namespace.moduleContainer.AllModules() | 
|  | 204 | totalCount += len(newModules) | 
|  | 205 | childLists = append(childLists, newModules) | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | allModules := make([]blueprint.ModuleGroup, 0, totalCount) | 
|  | 209 | for _, childList := range childLists { | 
|  | 210 | allModules = append(allModules, childList...) | 
|  | 211 | } | 
|  | 212 | return allModules | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | // parses a fully-qualified path (like "//namespace_path:module_name") into a namespace name and a | 
|  | 216 | // module name | 
|  | 217 | func (r *NameResolver) parseFullyQualifiedName(name string) (namespaceName string, moduleName string, ok bool) { | 
|  | 218 | if !strings.HasPrefix(name, namespacePrefix) { | 
|  | 219 | return "", "", false | 
|  | 220 | } | 
|  | 221 | name = strings.TrimPrefix(name, namespacePrefix) | 
|  | 222 | components := strings.Split(name, modulePrefix) | 
|  | 223 | if len(components) != 2 { | 
|  | 224 | return "", "", false | 
|  | 225 | } | 
|  | 226 | return components[0], components[1], true | 
|  | 227 |  | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | func (r *NameResolver) getNamespacesToSearchForModule(sourceNamespace *Namespace) (searchOrder []*Namespace) { | 
|  | 231 | return sourceNamespace.visibleNamespaces | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | func (r *NameResolver) ModuleFromName(name string, namespace blueprint.Namespace) (group blueprint.ModuleGroup, found bool) { | 
|  | 235 | // handle fully qualified references like "//namespace_path:module_name" | 
|  | 236 | nsName, moduleName, isAbs := r.parseFullyQualifiedName(name) | 
|  | 237 | if isAbs { | 
|  | 238 | namespace, found := r.namespaceAt(nsName) | 
|  | 239 | if !found { | 
|  | 240 | return blueprint.ModuleGroup{}, false | 
|  | 241 | } | 
|  | 242 | container := namespace.moduleContainer | 
|  | 243 | return container.ModuleFromName(moduleName, nil) | 
|  | 244 | } | 
|  | 245 | for _, candidate := range r.getNamespacesToSearchForModule(namespace.(*Namespace)) { | 
|  | 246 | group, found = candidate.moduleContainer.ModuleFromName(name, nil) | 
|  | 247 | if found { | 
|  | 248 | return group, true | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 | return blueprint.ModuleGroup{}, false | 
|  | 252 |  | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | func (r *NameResolver) Rename(oldName string, newName string, namespace blueprint.Namespace) []error { | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 256 | return namespace.(*Namespace).moduleContainer.Rename(oldName, newName, namespace) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
|  | 259 | // resolve each element of namespace.importedNamespaceNames and put the result in namespace.visibleNamespaces | 
|  | 260 | func (r *NameResolver) FindNamespaceImports(namespace *Namespace) (err error) { | 
|  | 261 | namespace.visibleNamespaces = make([]*Namespace, 0, 2+len(namespace.importedNamespaceNames)) | 
|  | 262 | // search itself first | 
|  | 263 | namespace.visibleNamespaces = append(namespace.visibleNamespaces, namespace) | 
|  | 264 | // search its imports next | 
|  | 265 | for _, name := range namespace.importedNamespaceNames { | 
|  | 266 | imp, ok := r.namespaceAt(name) | 
|  | 267 | if !ok { | 
|  | 268 | return fmt.Errorf("namespace %v does not exist", name) | 
|  | 269 | } | 
|  | 270 | namespace.visibleNamespaces = append(namespace.visibleNamespaces, imp) | 
|  | 271 | } | 
|  | 272 | // search the root namespace last | 
|  | 273 | namespace.visibleNamespaces = append(namespace.visibleNamespaces, r.rootNamespace) | 
|  | 274 | return nil | 
|  | 275 | } | 
|  | 276 |  | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 277 | func (r *NameResolver) chooseId(namespace *Namespace) { | 
|  | 278 | id := r.sortedNamespaces.index(namespace) | 
|  | 279 | if id < 0 { | 
|  | 280 | panic(fmt.Sprintf("Namespace not found: %v\n", namespace.id)) | 
|  | 281 | } | 
|  | 282 | namespace.id = strconv.Itoa(id) | 
|  | 283 | } | 
|  | 284 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 285 | func (r *NameResolver) MissingDependencyError(depender string, dependerNamespace blueprint.Namespace, depName string) (err error) { | 
|  | 286 | text := fmt.Sprintf("%q depends on undefined module %q", depender, depName) | 
|  | 287 |  | 
|  | 288 | _, _, isAbs := r.parseFullyQualifiedName(depName) | 
|  | 289 | if isAbs { | 
|  | 290 | // if the user gave a fully-qualified name, we don't need to look for other | 
|  | 291 | // modules that they might have been referring to | 
|  | 292 | return fmt.Errorf(text) | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | // determine which namespaces the module can be found in | 
|  | 296 | foundInNamespaces := []string{} | 
|  | 297 | for _, namespace := range r.sortedNamespaces.sortedItems() { | 
|  | 298 | _, found := namespace.moduleContainer.ModuleFromName(depName, nil) | 
|  | 299 | if found { | 
|  | 300 | foundInNamespaces = append(foundInNamespaces, namespace.Path) | 
|  | 301 | } | 
|  | 302 | } | 
|  | 303 | if len(foundInNamespaces) > 0 { | 
|  | 304 | // determine which namespaces are visible to dependerNamespace | 
|  | 305 | dependerNs := dependerNamespace.(*Namespace) | 
|  | 306 | searched := r.getNamespacesToSearchForModule(dependerNs) | 
|  | 307 | importedNames := []string{} | 
|  | 308 | for _, ns := range searched { | 
|  | 309 | importedNames = append(importedNames, ns.Path) | 
|  | 310 | } | 
|  | 311 | text += fmt.Sprintf("\nModule %q is defined in namespace %q which can read these %v namespaces: %q", depender, dependerNs.Path, len(importedNames), importedNames) | 
|  | 312 | text += fmt.Sprintf("\nModule %q can be found in these namespaces: %q", depName, foundInNamespaces) | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | return fmt.Errorf(text) | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | func (r *NameResolver) GetNamespace(ctx blueprint.NamespaceContext) blueprint.Namespace { | 
|  | 319 | return r.findNamespaceFromCtx(ctx) | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | func (r *NameResolver) findNamespaceFromCtx(ctx blueprint.NamespaceContext) *Namespace { | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 323 | return r.findNamespace(filepath.Dir(ctx.ModulePath())) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 326 | func (r *NameResolver) UniqueName(ctx blueprint.NamespaceContext, name string) (unique string) { | 
|  | 327 | prefix := r.findNamespaceFromCtx(ctx).id | 
|  | 328 | if prefix != "" { | 
|  | 329 | prefix = prefix + "-" | 
|  | 330 | } | 
|  | 331 | return prefix + name | 
|  | 332 | } | 
|  | 333 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 334 | var _ blueprint.NameInterface = (*NameResolver)(nil) | 
|  | 335 |  | 
|  | 336 | type Namespace struct { | 
|  | 337 | blueprint.NamespaceMarker | 
|  | 338 | Path string | 
|  | 339 |  | 
|  | 340 | // names of namespaces listed as imports by this namespace | 
|  | 341 | importedNamespaceNames []string | 
|  | 342 | // all namespaces that should be searched when a module in this namespace declares a dependency | 
|  | 343 | visibleNamespaces []*Namespace | 
|  | 344 |  | 
|  | 345 | id string | 
|  | 346 |  | 
|  | 347 | exportToKati bool | 
|  | 348 |  | 
|  | 349 | moduleContainer blueprint.NameInterface | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | func NewNamespace(path string) *Namespace { | 
|  | 353 | return &Namespace{Path: path, moduleContainer: blueprint.NewSimpleNameInterface()} | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | var _ blueprint.Namespace = (*Namespace)(nil) | 
|  | 357 |  | 
|  | 358 | type NamespaceModule struct { | 
|  | 359 | ModuleBase | 
|  | 360 |  | 
|  | 361 | namespace *Namespace | 
|  | 362 | resolver  *NameResolver | 
|  | 363 |  | 
|  | 364 | properties struct { | 
|  | 365 | Imports []string | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | func (n *NamespaceModule) DepsMutator(context BottomUpMutatorContext) { | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | func (n *NamespaceModule) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | func (n *NamespaceModule) GenerateBuildActions(ctx blueprint.ModuleContext) { | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | func (n *NamespaceModule) Name() (name string) { | 
|  | 379 | return *n.nameProperties.Name | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | func NamespaceFactory() Module { | 
|  | 383 | module := &NamespaceModule{} | 
|  | 384 |  | 
|  | 385 | name := "soong_namespace" | 
|  | 386 | module.nameProperties.Name = &name | 
|  | 387 |  | 
|  | 388 | module.AddProperties(&module.properties) | 
|  | 389 | return module | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | func RegisterNamespaceMutator(ctx RegisterMutatorsContext) { | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 393 | ctx.BottomUp("namespace_deps", namespaceMutator).Parallel() | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 396 | func namespaceMutator(ctx BottomUpMutatorContext) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 397 | module, ok := ctx.Module().(*NamespaceModule) | 
|  | 398 | if ok { | 
|  | 399 | err := module.resolver.FindNamespaceImports(module.namespace) | 
|  | 400 | if err != nil { | 
|  | 401 | ctx.ModuleErrorf(err.Error()) | 
|  | 402 | } | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 403 |  | 
|  | 404 | module.resolver.chooseId(module.namespace) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 405 | } | 
|  | 406 | } |