| LaMont Jones | c6aef16 | 2024-05-20 10:37:25 -0700 | [diff] [blame] | 1 | // Copyright 2023 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 build_flags |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
| 26 | type BuildFlagDeclarationsProviderData struct { |
| 27 | IntermediateCacheOutputPath android.WritablePath |
| 28 | IntermediateDumpOutputPath android.WritablePath |
| 29 | } |
| 30 | |
| 31 | var BuildFlagDeclarationsProviderKey = blueprint.NewProvider[BuildFlagDeclarationsProviderData]() |
| 32 | |
| 33 | type DeclarationsModule struct { |
| 34 | android.ModuleBase |
| 35 | android.DefaultableModuleBase |
| 36 | |
| 37 | // Properties for "aconfig_declarations" |
| 38 | properties struct { |
| 39 | // aconfig files, relative to this Android.bp file |
| 40 | Srcs []string `android:"path"` |
| 41 | } |
| 42 | |
| 43 | intermediatePath android.WritablePath |
| 44 | } |
| 45 | |
| 46 | func DeclarationsFactory() android.Module { |
| 47 | module := &DeclarationsModule{} |
| 48 | |
| 49 | android.InitAndroidModule(module) |
| 50 | android.InitDefaultableModule(module) |
| 51 | module.AddProperties(&module.properties) |
| 52 | |
| 53 | return module |
| 54 | } |
| 55 | |
| 56 | func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { |
| 57 | switch tag { |
| 58 | case "": |
| 59 | // The default output of this module is the intermediates format, which is |
| 60 | // not installable and in a private format that no other rules can handle |
| 61 | // correctly. |
| 62 | return []android.Path{module.intermediatePath}, nil |
| 63 | default: |
| 64 | return nil, fmt.Errorf("unsupported build_flags_declarations module reference tag %q", tag) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func joinAndPrefix(prefix string, values []string) string { |
| 69 | var sb strings.Builder |
| 70 | for _, v := range values { |
| 71 | sb.WriteString(prefix) |
| 72 | sb.WriteString(v) |
| 73 | } |
| 74 | return sb.String() |
| 75 | } |
| 76 | |
| 77 | func optionalVariable(prefix string, value string) string { |
| 78 | var sb strings.Builder |
| 79 | if value != "" { |
| 80 | sb.WriteString(prefix) |
| 81 | sb.WriteString(value) |
| 82 | } |
| 83 | return sb.String() |
| 84 | } |
| 85 | |
| 86 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 87 | // Intermediate format |
| 88 | declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) |
| 89 | intermediateCacheFilePath := android.PathForModuleOut(ctx, "build_flag_intermediate.pb") |
| 90 | inputFiles := make([]android.Path, len(declarationFiles)) |
| 91 | copy(inputFiles, declarationFiles) |
| 92 | |
| 93 | // TODO(lamont): generate the rc_proto.FlagArtifacts message for the sources. |
| 94 | args := map[string]string{ |
| 95 | "release_version": ctx.Config().ReleaseVersion(), |
| 96 | "declarations": android.JoinPathsWithPrefix(declarationFiles, "--decl "), |
| 97 | } |
| 98 | ctx.Build(pctx, android.BuildParams{ |
| 99 | Rule: buildFlagRule, |
| 100 | Output: intermediateCacheFilePath, |
| 101 | Inputs: inputFiles, |
| 102 | Description: "build_flag_declarations", |
| 103 | Args: args, |
| 104 | }) |
| 105 | |
| 106 | intermediateDumpFilePath := android.PathForModuleOut(ctx, "build_flag_intermediate.textproto") |
| 107 | ctx.Build(pctx, android.BuildParams{ |
| 108 | Rule: buildFlagTextRule, |
| 109 | Output: intermediateDumpFilePath, |
| 110 | Input: intermediateCacheFilePath, |
| 111 | Description: "build_flag_declarations_text", |
| 112 | }) |
| 113 | |
| 114 | android.SetProvider(ctx, BuildFlagDeclarationsProviderKey, BuildFlagDeclarationsProviderData{ |
| 115 | IntermediateCacheOutputPath: intermediateCacheFilePath, |
| 116 | IntermediateDumpOutputPath: intermediateDumpFilePath, |
| 117 | }) |
| 118 | } |