Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [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 ( |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 18 | "path/filepath" |
| 19 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 20 | "android/soong/bazel" |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Paul Duffin | cdfcec9 | 2020-05-01 11:57:12 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
Paul Duffin | c132742 | 2020-01-14 12:15:29 +0000 | [diff] [blame] | 26 | RegisterPackageBuildComponents(InitRegistrationContext) |
| 27 | } |
| 28 | |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 29 | var PrepareForTestWithPackageModule = FixtureRegisterWithContext(RegisterPackageBuildComponents) |
| 30 | |
Paul Duffin | cdfcec9 | 2020-05-01 11:57:12 +0100 | [diff] [blame] | 31 | // Register the package module type. |
Paul Duffin | c132742 | 2020-01-14 12:15:29 +0000 | [diff] [blame] | 32 | func RegisterPackageBuildComponents(ctx RegistrationContext) { |
| 33 | ctx.RegisterModuleType("package", PackageFactory) |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type packageProperties struct { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 37 | // Specifies the default visibility for all modules defined in this package. |
| 38 | Default_visibility []string |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 39 | // Specifies the default license terms for all modules defined in this package. |
| 40 | Default_applicable_licenses []string |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 43 | type bazelPackageAttributes struct { |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 44 | Default_visibility []string |
| 45 | Default_package_metadata bazel.LabelListAttribute |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 48 | type packageModule struct { |
| 49 | ModuleBase |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 50 | BazelModuleBase |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 51 | |
Paul Duffin | cdfcec9 | 2020-05-01 11:57:12 +0100 | [diff] [blame] | 52 | properties packageProperties |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 55 | var _ Bazelable = &packageModule{} |
| 56 | |
| 57 | func (p *packageModule) ConvertWithBp2build(ctx TopDownMutatorContext) { |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 58 | 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 Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 77 | ctx.CreateBazelTargetModule( |
| 78 | bazel.BazelTargetModuleProperties{ |
| 79 | Rule_class: "package", |
| 80 | }, |
| 81 | CommonAttributes{}, |
| 82 | &bazelPackageAttributes{ |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 83 | Default_package_metadata: defaultPackageMetadata, |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 84 | // FIXME(asmundak): once b/221436821 is resolved |
| 85 | Default_visibility: []string{"//visibility:public"}, |
| 86 | }) |
| 87 | } |
| 88 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 89 | func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) { |
| 90 | // Nothing to do. |
| 91 | } |
| 92 | |
| 93 | func (p *packageModule) GenerateBuildActions(ctx blueprint.ModuleContext) { |
| 94 | // Nothing to do. |
| 95 | } |
| 96 | |
| 97 | func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName { |
| 98 | // Override to create a package id. |
| 99 | return newPackageId(ctx.ModuleDir()) |
| 100 | } |
| 101 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 102 | func PackageFactory() Module { |
| 103 | module := &packageModule{} |
| 104 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 105 | module.AddProperties(&module.properties, &module.commonProperties.BazelConversionStatus) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 106 | |
Paul Duffin | cdfcec9 | 2020-05-01 11:57:12 +0100 | [diff] [blame] | 107 | // 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 Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 114 | // The default_visibility property needs to be checked and parsed by the visibility module during |
Paul Duffin | 5ec73ec | 2020-05-01 17:52:01 +0100 | [diff] [blame] | 115 | // its checking and parsing phases so make it the primary visibility property. |
| 116 | setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 117 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 118 | // 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 Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 122 | InitBazelModule(module) |
| 123 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 124 | return module |
| 125 | } |