blob: 7fbc700417538e4f9cdc3c8f61ff2a2cac1bfe63 [file] [log] [blame]
Paul Duffine2453c72019-05-31 14:00:04 +01001// 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 (
Wei Li2c9e8d62023-05-05 01:07:15 -070018 "path/filepath"
19
Sasha Smundak8bea2672022-08-04 13:31:14 -070020 "android/soong/bazel"
Paul Duffine2453c72019-05-31 14:00:04 +010021 "github.com/google/blueprint"
Paul Duffincdfcec92020-05-01 11:57:12 +010022 "github.com/google/blueprint/proptools"
Paul Duffine2453c72019-05-31 14:00:04 +010023)
24
25func init() {
Paul Duffinc1327422020-01-14 12:15:29 +000026 RegisterPackageBuildComponents(InitRegistrationContext)
27}
28
Paul Duffincfd33742021-02-27 11:59:02 +000029var PrepareForTestWithPackageModule = FixtureRegisterWithContext(RegisterPackageBuildComponents)
30
Paul Duffincdfcec92020-05-01 11:57:12 +010031// Register the package module type.
Paul Duffinc1327422020-01-14 12:15:29 +000032func RegisterPackageBuildComponents(ctx RegistrationContext) {
33 ctx.RegisterModuleType("package", PackageFactory)
Paul Duffine2453c72019-05-31 14:00:04 +010034}
35
36type packageProperties struct {
Paul Duffine2453c72019-05-31 14:00:04 +010037 // Specifies the default visibility for all modules defined in this package.
38 Default_visibility []string
Bob Badour37af0462021-01-07 03:34:31 +000039 // Specifies the default license terms for all modules defined in this package.
40 Default_applicable_licenses []string
Paul Duffine2453c72019-05-31 14:00:04 +010041}
42
Sasha Smundak8bea2672022-08-04 13:31:14 -070043type bazelPackageAttributes struct {
Wei Li2c9e8d62023-05-05 01:07:15 -070044 Default_visibility []string
45 Default_package_metadata bazel.LabelListAttribute
Sasha Smundak8bea2672022-08-04 13:31:14 -070046}
47
Paul Duffine2453c72019-05-31 14:00:04 +010048type packageModule struct {
49 ModuleBase
Sasha Smundak8bea2672022-08-04 13:31:14 -070050 BazelModuleBase
Paul Duffine2453c72019-05-31 14:00:04 +010051
Paul Duffincdfcec92020-05-01 11:57:12 +010052 properties packageProperties
Paul Duffine2453c72019-05-31 14:00:04 +010053}
54
Sasha Smundak8bea2672022-08-04 13:31:14 -070055var _ Bazelable = &packageModule{}
56
57func (p *packageModule) ConvertWithBp2build(ctx TopDownMutatorContext) {
Wei Li2c9e8d62023-05-05 01:07:15 -070058 defaultPackageMetadata := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses))
59 // If METADATA file exists in the package, add it to package(default_package_metadata=) using a
60 // filegroup(name="default_metadata_file") which can be accessed later on each module in Bazel
61 // using attribute "applicable_licenses".
62 // Attribute applicable_licenses of filegroup "default_metadata_file" has to be set to [],
63 // otherwise Bazel reports cyclic reference error.
64 if existed, _, _ := ctx.Config().fs.Exists(filepath.Join(ctx.ModuleDir(), "METADATA")); existed {
65 ctx.CreateBazelTargetModule(
66 bazel.BazelTargetModuleProperties{
67 Rule_class: "filegroup",
68 },
69 CommonAttributes{Name: "default_metadata_file"},
70 &bazelFilegroupAttributes{
71 Srcs: bazel.MakeLabelListAttribute(BazelLabelForModuleSrc(ctx, []string{"METADATA"})),
72 Applicable_licenses: bazel.LabelListAttribute{Value: bazel.LabelList{Includes: []bazel.Label{}}, EmitEmptyList: true},
73 })
74 defaultPackageMetadata.Value.Add(&bazel.Label{Label: ":default_metadata_file"})
75 }
76
Sasha Smundak8bea2672022-08-04 13:31:14 -070077 ctx.CreateBazelTargetModule(
78 bazel.BazelTargetModuleProperties{
79 Rule_class: "package",
80 },
81 CommonAttributes{},
82 &bazelPackageAttributes{
Wei Li2c9e8d62023-05-05 01:07:15 -070083 Default_package_metadata: defaultPackageMetadata,
Sasha Smundak8bea2672022-08-04 13:31:14 -070084 // FIXME(asmundak): once b/221436821 is resolved
85 Default_visibility: []string{"//visibility:public"},
86 })
87}
88
Paul Duffine2453c72019-05-31 14:00:04 +010089func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) {
90 // Nothing to do.
91}
92
93func (p *packageModule) GenerateBuildActions(ctx blueprint.ModuleContext) {
94 // Nothing to do.
95}
96
97func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName {
98 // Override to create a package id.
99 return newPackageId(ctx.ModuleDir())
100}
101
Paul Duffine2453c72019-05-31 14:00:04 +0100102func PackageFactory() Module {
103 module := &packageModule{}
104
Sasha Smundak8bea2672022-08-04 13:31:14 -0700105 module.AddProperties(&module.properties, &module.commonProperties.BazelConversionStatus)
Paul Duffin63c6e182019-07-24 14:24:38 +0100106
Paul Duffincdfcec92020-05-01 11:57:12 +0100107 // The name is the relative path from build root to the directory containing this
108 // module. Set that name at the earliest possible moment that information is available
109 // which is in a LoadHook.
110 AddLoadHook(module, func(ctx LoadHookContext) {
111 module.nameProperties.Name = proptools.StringPtr("//" + ctx.ModuleDir())
112 })
113
Paul Duffin63c6e182019-07-24 14:24:38 +0100114 // The default_visibility property needs to be checked and parsed by the visibility module during
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100115 // its checking and parsing phases so make it the primary visibility property.
116 setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility)
Paul Duffin63c6e182019-07-24 14:24:38 +0100117
Bob Badour37af0462021-01-07 03:34:31 +0000118 // The default_applicable_licenses property needs to be checked and parsed by the licenses module during
119 // its checking and parsing phases so make it the primary licenses property.
120 setPrimaryLicensesProperty(module, "default_applicable_licenses", &module.properties.Default_applicable_licenses)
121
Sasha Smundak8bea2672022-08-04 13:31:14 -0700122 InitBazelModule(module)
123
Paul Duffine2453c72019-05-31 14:00:04 +0100124 return module
125}