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 "path/filepath" 19 20 "android/soong/bazel" 21 "github.com/google/blueprint" 22 "github.com/google/blueprint/proptools" 23) 24 25func init() { 26 RegisterPackageBuildComponents(InitRegistrationContext) 27} 28 29var PrepareForTestWithPackageModule = FixtureRegisterWithContext(RegisterPackageBuildComponents) 30 31// Register the package module type. 32func RegisterPackageBuildComponents(ctx RegistrationContext) { 33 ctx.RegisterModuleType("package", PackageFactory) 34} 35 36type packageProperties struct { 37 // Specifies the default visibility for all modules defined in this package. 38 Default_visibility []string 39 // Specifies the default license terms for all modules defined in this package. 40 Default_applicable_licenses []string 41} 42 43type bazelPackageAttributes struct { 44 Default_visibility []string 45 Default_package_metadata bazel.LabelListAttribute 46} 47 48type packageModule struct { 49 ModuleBase 50 BazelModuleBase 51 52 properties packageProperties 53} 54 55var _ Bazelable = &packageModule{} 56 57func (p *packageModule) ConvertWithBp2build(ctx TopDownMutatorContext) { 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 77 ctx.CreateBazelTargetModule( 78 bazel.BazelTargetModuleProperties{ 79 Rule_class: "package", 80 }, 81 CommonAttributes{}, 82 &bazelPackageAttributes{ 83 Default_package_metadata: defaultPackageMetadata, 84 // FIXME(asmundak): once b/221436821 is resolved 85 Default_visibility: []string{"//visibility:public"}, 86 }) 87} 88 89func (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 102func PackageFactory() Module { 103 module := &packageModule{} 104 105 module.AddProperties(&module.properties, &module.commonProperties.BazelConversionStatus) 106 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 114 // The default_visibility property needs to be checked and parsed by the visibility module during 115 // its checking and parsing phases so make it the primary visibility property. 116 setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility) 117 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 122 InitBazelModule(module) 123 124 return module 125} 126