Inseob Kim | 58c802f | 2024-06-11 10:59:00 +0900 | [diff] [blame] | 1 | // Copyright 2024 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 | |
mrziwang | 2aafdc5 | 2024-09-17 10:29:54 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "github.com/google/blueprint/proptools" |
| 19 | ) |
Inseob Kim | 58c802f | 2024-06-11 10:59:00 +0900 | [diff] [blame] | 20 | |
| 21 | func init() { |
| 22 | ctx := InitRegistrationContext |
| 23 | ctx.RegisterModuleType("product_config", productConfigFactory) |
| 24 | } |
| 25 | |
| 26 | type productConfigModule struct { |
| 27 | ModuleBase |
| 28 | } |
| 29 | |
| 30 | func (p *productConfigModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 31 | if ctx.ModuleName() != "product_config" || ctx.ModuleDir() != "build/soong" { |
| 32 | ctx.ModuleErrorf("There can only be one product_config module in build/soong") |
| 33 | return |
| 34 | } |
| 35 | outputFilePath := PathForModuleOut(ctx, p.Name()+".json").OutputPath |
| 36 | |
| 37 | // DeviceProduct can be null so calling ctx.Config().DeviceProduct() may cause null dereference |
| 38 | targetProduct := proptools.String(ctx.Config().config.productVariables.DeviceProduct) |
| 39 | if targetProduct != "" { |
| 40 | targetProduct += "." |
| 41 | } |
Qing Shen | 713c542 | 2024-08-23 04:09:18 +0000 | [diff] [blame] | 42 | |
| 43 | coverageSuffix := ctx.Config().CoverageSuffix() |
| 44 | soongVariablesPath := PathForOutput(ctx, "soong."+targetProduct+coverageSuffix+"variables") |
| 45 | extraVariablesPath := PathForOutput(ctx, "soong."+targetProduct+coverageSuffix+"extra.variables") |
Inseob Kim | 58c802f | 2024-06-11 10:59:00 +0900 | [diff] [blame] | 46 | |
| 47 | rule := NewRuleBuilder(pctx, ctx) |
| 48 | rule.Command().BuiltTool("merge_json"). |
| 49 | Output(outputFilePath). |
| 50 | Input(soongVariablesPath). |
| 51 | Input(extraVariablesPath). |
| 52 | rule.Build("product_config.json", "building product_config.json") |
| 53 | |
| 54 | ctx.SetOutputFiles(Paths{outputFilePath}, "") |
| 55 | } |
| 56 | |
| 57 | // product_config module exports product variables and extra variables as a JSON file. |
| 58 | func productConfigFactory() Module { |
| 59 | module := &productConfigModule{} |
| 60 | InitAndroidModule(module) |
| 61 | return module |
| 62 | } |