blob: b8dacfb1f6db713e63e43f730c0d36c3cfc1b3f2 [file] [log] [blame]
Jared Dukeeacbdfc2024-11-13 00:43:59 +00001// 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
14package systemfeatures
15
16import (
17 "fmt"
18 "sort"
19 "strings"
20
21 "android/soong/android"
22 "android/soong/genrule"
Jared Dukee4049702024-12-23 22:14:58 +000023
24 "github.com/google/blueprint/proptools"
Jared Dukeeacbdfc2024-11-13 00:43:59 +000025)
26
27var (
28 pctx = android.NewPackageContext("android/soong/systemfeatures")
29)
30
31func init() {
32 registerSystemFeaturesComponents(android.InitRegistrationContext)
33}
34
35func registerSystemFeaturesComponents(ctx android.RegistrationContext) {
36 ctx.RegisterModuleType("java_system_features_srcs", JavaSystemFeaturesSrcsFactory)
37}
38
39type 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 Dukee4049702024-12-23 22:14:58 +000044 // 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 Dukeeacbdfc2024-11-13 00:43:59 +000049 }
50 outputFiles android.WritablePaths
51}
52
53var _ genrule.SourceFileGenerator = (*javaSystemFeaturesSrcs)(nil)
54var _ android.SourceFileProducer = (*javaSystemFeaturesSrcs)(nil)
55
56func (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 Dukee4049702024-12-23 22:14:58 +000082 FlagWithArg("--metadata-only=", fmt.Sprint(proptools.Bool(m.properties.Metadata_only))).
Jared Dukeeacbdfc2024-11-13 00:43:59 +000083 FlagWithOutput(" > ", outputFile)
84 rule.Build(ctx.ModuleName(), "Generating systemfeatures srcs filegroup")
85
86 m.outputFiles = append(m.outputFiles, outputFile)
87}
88
89func (m *javaSystemFeaturesSrcs) Srcs() android.Paths {
90 return m.outputFiles.Paths()
91}
92
93func (m *javaSystemFeaturesSrcs) GeneratedSourceFiles() android.Paths {
94 return m.outputFiles.Paths()
95}
96
97func (m *javaSystemFeaturesSrcs) GeneratedDeps() android.Paths {
98 return m.outputFiles.Paths()
99}
100
101func (m *javaSystemFeaturesSrcs) GeneratedHeaderDirs() android.Paths {
102 return nil
103}
104
105func JavaSystemFeaturesSrcsFactory() android.Module {
106 module := &javaSystemFeaturesSrcs{}
107 module.AddProperties(&module.properties)
Jared Dukee4049702024-12-23 22:14:58 +0000108 module.properties.Metadata_only = proptools.BoolPtr(false)
Jared Dukeeacbdfc2024-11-13 00:43:59 +0000109 android.InitAndroidModule(module)
110 return module
111}