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
15package android
16
17import (
18	"github.com/google/blueprint"
19	"github.com/google/blueprint/proptools"
20)
21
22func init() {
23	RegisterPackageBuildComponents(InitRegistrationContext)
24}
25
26var PrepareForTestWithPackageModule = FixtureRegisterWithContext(RegisterPackageBuildComponents)
27
28// Register the package module type.
29func RegisterPackageBuildComponents(ctx RegistrationContext) {
30	ctx.RegisterModuleType("package", PackageFactory)
31}
32
33type packageProperties struct {
34	// Specifies the default visibility for all modules defined in this package.
35	Default_visibility []string
36	// Specifies the default license terms for all modules defined in this package.
37	Default_applicable_licenses []string
38	Default_team                *string `android:"path"`
39}
40
41type packageModule struct {
42	ModuleBase
43
44	properties packageProperties
45}
46
47func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) {
48	// Nothing to do.
49}
50
51func (p *packageModule) DepsMutator(ctx BottomUpMutatorContext) {
52	// Add the dependency to do a validity check
53	if p.properties.Default_team != nil {
54		ctx.AddDependency(ctx.Module(), nil, *p.properties.Default_team)
55	}
56}
57
58func (p *packageModule) GenerateBuildActions(ctx blueprint.ModuleContext) {
59	// Nothing to do.
60}
61
62func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName {
63	// Override to create a package id.
64	return newPackageId(ctx.ModuleDir())
65}
66
67func PackageFactory() Module {
68	module := &packageModule{}
69
70	module.AddProperties(&module.properties)
71
72	// The name is the relative path from build root to the directory containing this
73	// module. Set that name at the earliest possible moment that information is available
74	// which is in a LoadHook.
75	AddLoadHook(module, func(ctx LoadHookContext) {
76		module.nameProperties.Name = proptools.StringPtr("//" + ctx.ModuleDir())
77	})
78
79	// The default_visibility property needs to be checked and parsed by the visibility module during
80	// its checking and parsing phases so make it the primary visibility property.
81	setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility)
82
83	// The default_applicable_licenses property needs to be checked and parsed by the licenses module during
84	// its checking and parsing phases so make it the primary licenses property.
85	setPrimaryLicensesProperty(module, "default_applicable_licenses", &module.properties.Default_applicable_licenses)
86
87	return module
88}
89