Jared Duke | eacbdfc | 2024-11-13 00:43:59 +0000 | [diff] [blame] | 1 | // Copyright 2024 Google Inc. All rights reserved. |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package systemfeatures |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "sort" |
| 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/genrule" |
Jared Duke | e404970 | 2024-12-23 22:14:58 +0000 | [diff] [blame^] | 23 | |
| 24 | "github.com/google/blueprint/proptools" |
Jared Duke | eacbdfc | 2024-11-13 00:43:59 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | pctx = android.NewPackageContext("android/soong/systemfeatures") |
| 29 | ) |
| 30 | |
| 31 | func init() { |
| 32 | registerSystemFeaturesComponents(android.InitRegistrationContext) |
| 33 | } |
| 34 | |
| 35 | func registerSystemFeaturesComponents(ctx android.RegistrationContext) { |
| 36 | ctx.RegisterModuleType("java_system_features_srcs", JavaSystemFeaturesSrcsFactory) |
| 37 | } |
| 38 | |
| 39 | type javaSystemFeaturesSrcs struct { |
| 40 | android.ModuleBase |
| 41 | properties struct { |
| 42 | // The fully qualified class name for the generated code, e.g., com.android.Foo |
| 43 | Full_class_name string |
Jared Duke | e404970 | 2024-12-23 22:14:58 +0000 | [diff] [blame^] | 44 | // Whether to generate only a simple metadata class with details about the full API surface. |
| 45 | // This is useful for tools that rely on the mapping from feature names to their generated |
| 46 | // method names, but don't want the fully generated API class (e.g., for linting). |
| 47 | |
| 48 | Metadata_only *bool |
Jared Duke | eacbdfc | 2024-11-13 00:43:59 +0000 | [diff] [blame] | 49 | } |
| 50 | outputFiles android.WritablePaths |
| 51 | } |
| 52 | |
| 53 | var _ genrule.SourceFileGenerator = (*javaSystemFeaturesSrcs)(nil) |
| 54 | var _ android.SourceFileProducer = (*javaSystemFeaturesSrcs)(nil) |
| 55 | |
| 56 | func (m *javaSystemFeaturesSrcs) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 57 | // Create a file name appropriate for the given fully qualified (w/ package) class name. |
| 58 | classNameParts := strings.Split(m.properties.Full_class_name, ".") |
| 59 | outputDir := android.PathForModuleGen(ctx) |
| 60 | outputFileName := classNameParts[len(classNameParts)-1] + ".java" |
| 61 | outputFile := android.PathForModuleGen(ctx, outputFileName).OutputPath |
| 62 | |
| 63 | // Collect all RELEASE_SYSTEM_FEATURE_$K:$V build flags into a list of "$K:$V" pairs. |
| 64 | var features []string |
| 65 | for k, v := range ctx.Config().ProductVariables().BuildFlags { |
| 66 | if strings.HasPrefix(k, "RELEASE_SYSTEM_FEATURE_") { |
| 67 | shortFeatureName := strings.TrimPrefix(k, "RELEASE_SYSTEM_FEATURE_") |
| 68 | features = append(features, fmt.Sprintf("%s:%s", shortFeatureName, v)) |
| 69 | } |
| 70 | } |
| 71 | // Ensure sorted outputs for consistency of flag ordering in ninja outputs. |
| 72 | sort.Strings(features) |
| 73 | |
| 74 | rule := android.NewRuleBuilder(pctx, ctx) |
| 75 | rule.Command().Text("rm -rf").Text(outputDir.String()) |
| 76 | rule.Command().Text("mkdir -p").Text(outputDir.String()) |
| 77 | rule.Command(). |
| 78 | BuiltTool("systemfeatures-gen-tool"). |
| 79 | Flag(m.properties.Full_class_name). |
| 80 | FlagForEachArg("--feature=", features). |
| 81 | FlagWithArg("--readonly=", fmt.Sprint(ctx.Config().ReleaseUseSystemFeatureBuildFlags())). |
Jared Duke | e404970 | 2024-12-23 22:14:58 +0000 | [diff] [blame^] | 82 | FlagWithArg("--metadata-only=", fmt.Sprint(proptools.Bool(m.properties.Metadata_only))). |
Jared Duke | eacbdfc | 2024-11-13 00:43:59 +0000 | [diff] [blame] | 83 | FlagWithOutput(" > ", outputFile) |
| 84 | rule.Build(ctx.ModuleName(), "Generating systemfeatures srcs filegroup") |
| 85 | |
| 86 | m.outputFiles = append(m.outputFiles, outputFile) |
| 87 | } |
| 88 | |
| 89 | func (m *javaSystemFeaturesSrcs) Srcs() android.Paths { |
| 90 | return m.outputFiles.Paths() |
| 91 | } |
| 92 | |
| 93 | func (m *javaSystemFeaturesSrcs) GeneratedSourceFiles() android.Paths { |
| 94 | return m.outputFiles.Paths() |
| 95 | } |
| 96 | |
| 97 | func (m *javaSystemFeaturesSrcs) GeneratedDeps() android.Paths { |
| 98 | return m.outputFiles.Paths() |
| 99 | } |
| 100 | |
| 101 | func (m *javaSystemFeaturesSrcs) GeneratedHeaderDirs() android.Paths { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | func JavaSystemFeaturesSrcsFactory() android.Module { |
| 106 | module := &javaSystemFeaturesSrcs{} |
| 107 | module.AddProperties(&module.properties) |
Jared Duke | e404970 | 2024-12-23 22:14:58 +0000 | [diff] [blame^] | 108 | module.properties.Metadata_only = proptools.BoolPtr(false) |
Jared Duke | eacbdfc | 2024-11-13 00:43:59 +0000 | [diff] [blame] | 109 | android.InitAndroidModule(module) |
| 110 | return module |
| 111 | } |