platform_build_soong/android/package.go

192 lines
5.6 KiB
Go
Raw Normal View History

// Copyright 2019 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import (
"fmt"
"sync/atomic"
"github.com/google/blueprint"
)
func init() {
RegisterModuleType("package", PackageFactory)
}
// The information maintained about each package.
type packageInfo struct {
// The module from which this information was populated. If `duplicated` = true then this is the
// module that has been renamed and must be used to report errors.
module *packageModule
// If true this indicates that there are two package statements in the same package which is not
// allowed and will cause the build to fail. This flag is set by packageRenamer and checked in
// packageErrorReporter
duplicated bool
}
type packageProperties struct {
Name string `blueprint:"mutated"`
// Specifies the default visibility for all modules defined in this package.
Default_visibility []string
}
type packageModule struct {
ModuleBase
properties packageProperties
packageInfo *packageInfo
}
func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) {
// Nothing to do.
}
func (p *packageModule) GenerateBuildActions(ctx blueprint.ModuleContext) {
// Nothing to do.
}
func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName {
// Override to create a package id.
return newPackageId(ctx.ModuleDir())
}
func (p *packageModule) Name() string {
return p.properties.Name
}
func (p *packageModule) setName(name string) {
p.properties.Name = name
}
// Counter to ensure package modules are created with a unique name within whatever namespace they
// belong.
var packageCount uint32 = 0
func PackageFactory() Module {
module := &packageModule{}
// Get a unique if for the package. Has to be done atomically as the creation of the modules are
// done in parallel.
id := atomic.AddUint32(&packageCount, 1)
name := fmt.Sprintf("soong_package_%d", id)
module.properties.Name = name
module.AddProperties(&module.properties)
Refactor visibility to support visibility on defaults modules Existing modules, either general one or package ones have a single visibility property, called visibility in general, and default_visibility on package, that controls access to that module, or in the case of package sets the default visibility of all modules in that package. The property is checked and gathered during the similarly named phases of visibility processing. The defaults module will be different as it will have two properties. The first, visibility, will not affect the visibility of the module, it only affects the visibility of modules that 'extend' the defaults. So, it will need checking but not parsing. The second property, defaults_visibility, will affect the visibility of the module and so will need both checking and parsing. The current implementation does not handle those cases because: 1) It does not differentiate between the property that affects the module and those that do not. It checks and gathers all of them with the last property gathered overriding the rules for the previous properties. 2) It relies on overriding methods in MethodBase in order to change the default behavior for the package module. That works because packageModule embeds ModuleBase but will not work for DefaultsModuleBase as it does not embed ModuleBase and instead is embedded alongside it so attempting to override a method in MethodBase leads to ambiguity. This change addresses the issues as follows: 1) It adds a new visibility() []string method to get access to the primary visibility rules, i.e. the ones that affect the module. 2) It adds two fields, 'visibilityPropertyInfo []visibilityProperty' to provide information about all the properties that need checking, and 'primaryVisibilityProperty visibilityProperty' to specify the property that affects the module. The PackageFactory() and InitAndroidModule(Module) functions are modified to initialize the fields. The override of the visibilityProperties() method for packageModule is removed and the default implementations of visibilityProperties() and visibility() on ModuleBase return information from the two new fields. The InitDefaultsModule is updated to also initialize the two new fields. It uses nil for primaryVisibilityProperty for now but that will be changed to return defaults_visibility. It also uses the commonProperties structure created for the defaults directly instead of having to search for it through properties(). Changed the visibilityProperty to take a pointer to the property that can be used to retrieve the value rather than a lambda function. Bug: 130796911 Test: m nothing Change-Id: Icadd470a5f692a48ec61de02bf3dfde3e2eea2ef
2019-07-24 21:24:38 +08:00
// The default_visibility property needs to be checked and parsed by the visibility module during
// its checking and parsing phases.
module.primaryVisibilityProperty =
newVisibilityProperty("default_visibility", &module.properties.Default_visibility)
module.visibilityPropertyInfo = []visibilityProperty{module.primaryVisibilityProperty}
return module
}
// Registers the function that renames the packages.
func registerPackageRenamer(ctx RegisterMutatorsContext) {
ctx.BottomUp("packageRenamer", packageRenamer).Parallel()
ctx.BottomUp("packageErrorReporter", packageErrorReporter).Parallel()
}
// Renames the package to match the package directory.
//
// This also creates a PackageInfo object for each package and uses that to detect and remember
// duplicates for later error reporting.
func packageRenamer(ctx BottomUpMutatorContext) {
m, ok := ctx.Module().(*packageModule)
if !ok {
return
}
packageName := "//" + ctx.ModuleDir()
pi := newPackageInfo(ctx, packageName, m)
if pi.module != m {
// Remember that the package was duplicated but do not rename as that will cause an error to
// be logged with the generated name. Similarly, reporting the error here will use the generated
// name as renames are only processed after this phase.
pi.duplicated = true
} else {
// This is the first package module in this package so rename it to match the package name.
m.setName(packageName)
ctx.Rename(packageName)
// Store a package info reference in the module.
m.packageInfo = pi
}
}
// Logs any deferred errors.
func packageErrorReporter(ctx BottomUpMutatorContext) {
m, ok := ctx.Module().(*packageModule)
if !ok {
return
}
packageDir := ctx.ModuleDir()
packageName := "//" + packageDir
// Get the PackageInfo for the package. Should have been populated in the packageRenamer phase.
pi := findPackageInfo(ctx, packageName)
if pi == nil {
ctx.ModuleErrorf("internal error, expected package info to be present for package '%s'",
packageName)
return
}
if pi.module != m {
// The package module has been duplicated but this is not the module that has been renamed so
// ignore it. An error will be logged for the renamed module which will ensure that the error
// message uses the correct name.
return
}
// Check to see whether there are duplicate package modules in the package.
if pi.duplicated {
ctx.ModuleErrorf("package {...} specified multiple times")
return
}
}
type defaultPackageInfoKey string
func newPackageInfo(
ctx BaseModuleContext, packageName string, module *packageModule) *packageInfo {
key := NewCustomOnceKey(defaultPackageInfoKey(packageName))
return ctx.Config().Once(key, func() interface{} {
return &packageInfo{module: module}
}).(*packageInfo)
}
// Get the PackageInfo for the package name (starts with //, no trailing /), is nil if no package
// module type was specified.
func findPackageInfo(ctx BaseModuleContext, packageName string) *packageInfo {
key := NewCustomOnceKey(defaultPackageInfoKey(packageName))
pi := ctx.Config().Once(key, func() interface{} {
return nil
})
if pi == nil {
return nil
} else {
return pi.(*packageInfo)
}
}