LaMont Jones | 11d2d09 | 2023-02-22 01:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package apex |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | mtctx = android.NewPackageContext("android/soong/multitree_apex") |
| 29 | ) |
| 30 | |
| 31 | func init() { |
| 32 | RegisterModulesSingleton(android.InitRegistrationContext) |
| 33 | } |
| 34 | |
| 35 | func RegisterModulesSingleton(ctx android.RegistrationContext) { |
| 36 | ctx.RegisterSingletonType("apex_multitree_singleton", multitreeAnalysisSingletonFactory) |
| 37 | } |
| 38 | |
| 39 | var PrepareForTestWithApexMultitreeSingleton = android.FixtureRegisterWithContext(RegisterModulesSingleton) |
| 40 | |
| 41 | func multitreeAnalysisSingletonFactory() android.Singleton { |
| 42 | return &multitreeAnalysisSingleton{} |
| 43 | } |
| 44 | |
| 45 | type multitreeAnalysisSingleton struct { |
| 46 | multitreeApexMetadataPath android.OutputPath |
| 47 | } |
| 48 | |
| 49 | type ApexMultitreeMetadataEntry struct { |
| 50 | // The name of the apex. |
| 51 | Name string |
| 52 | |
| 53 | // TODO: Add other properties as needed. |
| 54 | } |
| 55 | |
| 56 | type ApexMultitreeMetadata struct { |
| 57 | // Information about the installable apexes. |
| 58 | Apexes map[string]ApexMultitreeMetadataEntry |
| 59 | } |
| 60 | |
| 61 | func (p *multitreeAnalysisSingleton) GenerateBuildActions(context android.SingletonContext) { |
| 62 | data := ApexMultitreeMetadata{ |
| 63 | Apexes: make(map[string]ApexMultitreeMetadataEntry, 0), |
| 64 | } |
| 65 | context.VisitAllModules(func(module android.Module) { |
| 66 | // If this module is not being installed, ignore it. |
| 67 | if !module.Enabled() || module.IsSkipInstall() { |
| 68 | return |
| 69 | } |
| 70 | // Actual apexes provide ApexBundleInfoProvider. |
| 71 | if _, ok := context.ModuleProvider(module, ApexBundleInfoProvider).(ApexBundleInfo); !ok { |
| 72 | return |
| 73 | } |
| 74 | bundle, ok := module.(*apexBundle) |
| 75 | if ok && !bundle.testApex && !bundle.vndkApex && bundle.primaryApexType { |
| 76 | name := module.Name() |
| 77 | entry := ApexMultitreeMetadataEntry{ |
| 78 | Name: name, |
| 79 | } |
| 80 | data.Apexes[name] = entry |
| 81 | } |
| 82 | }) |
| 83 | p.multitreeApexMetadataPath = android.PathForOutput(context, "multitree_apex_metadata.json") |
| 84 | |
| 85 | jsonStr, err := json.Marshal(data) |
| 86 | if err != nil { |
| 87 | context.Errorf(err.Error()) |
| 88 | } |
| 89 | android.WriteFileRule(context, p.multitreeApexMetadataPath, string(jsonStr)) |
| 90 | // This seems cleaner, but doesn't emit the phony rule in testing. |
| 91 | // context.Phony("multitree_apex_metadata", p.multitreeApexMetadataPath) |
| 92 | |
| 93 | context.Build(mtctx, android.BuildParams{ |
| 94 | Rule: blueprint.Phony, |
| 95 | Description: "phony rule for multitree_apex_metadata", |
| 96 | Inputs: []android.Path{p.multitreeApexMetadataPath}, |
| 97 | Output: android.PathForPhony(context, "multitree_apex_metadata"), |
| 98 | }) |
| 99 | } |