diff options
| author | 2024-11-20 15:26:44 +0000 | |
|---|---|---|
| committer | 2024-11-20 15:26:44 +0000 | |
| commit | d166acd2d49b19eb457f0afa773d7ac7b603923e (patch) | |
| tree | 07beb2f2e5b9f0a8f923a3970af8677b7d406407 | |
| parent | 636144e5e2860e974452ff3ab080adaddca1c51a (diff) | |
| parent | 83c83d518ce9495784c8472662d0702e17031434 (diff) | |
Merge "Introduce a java_system_features_srcs wrapper for codegen" into main am: bf3242d2d3 am: 83c83d518c
Original change: https://android-review.googlesource.com/c/platform/build/soong/+/3345578
Change-Id: Ief21304b023e403045a582a353b8d71a9dc254e5
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
| -rw-r--r-- | android/config.go | 4 | ||||
| -rw-r--r-- | systemfeatures/Android.bp | 18 | ||||
| -rw-r--r-- | systemfeatures/OWNERS | 2 | ||||
| -rw-r--r-- | systemfeatures/system_features.go | 102 | ||||
| -rw-r--r-- | systemfeatures/system_features_test.go | 51 |
5 files changed, 177 insertions, 0 deletions
diff --git a/android/config.go b/android/config.go index 94285f822..dff3ea5bd 100644 --- a/android/config.go +++ b/android/config.go @@ -285,6 +285,10 @@ func (c Config) ReleaseCreateAconfigStorageFile() bool { return c.config.productVariables.GetBuildFlagBool("RELEASE_CREATE_ACONFIG_STORAGE_FILE") } +func (c Config) ReleaseUseSystemFeatureBuildFlags() bool { + return c.config.productVariables.GetBuildFlagBool("RELEASE_USE_SYSTEM_FEATURE_BUILD_FLAGS") +} + // A DeviceConfig object represents the configuration for a particular device // being built. For now there will only be one of these, but in the future there // may be multiple devices being built. diff --git a/systemfeatures/Android.bp b/systemfeatures/Android.bp new file mode 100644 index 000000000..a65a6b644 --- /dev/null +++ b/systemfeatures/Android.bp @@ -0,0 +1,18 @@ +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +bootstrap_go_package { + name: "soong-systemfeatures", + pkgPath: "android/soong/systemfeatures", + deps: [ + "blueprint", + "blueprint-proptools", + "soong", + "soong-android", + "soong-java", + ], + srcs: ["system_features.go"], + testSrcs: ["system_features_test.go"], + pluginFor: ["soong_build"], +} diff --git a/systemfeatures/OWNERS b/systemfeatures/OWNERS new file mode 100644 index 000000000..3e4480647 --- /dev/null +++ b/systemfeatures/OWNERS @@ -0,0 +1,2 @@ +jdduke@google.com +shayba@google.com diff --git a/systemfeatures/system_features.go b/systemfeatures/system_features.go new file mode 100644 index 000000000..0c1a56676 --- /dev/null +++ b/systemfeatures/system_features.go @@ -0,0 +1,102 @@ +// Copyright 2024 Google Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package systemfeatures + +import ( + "fmt" + "sort" + "strings" + + "android/soong/android" + "android/soong/genrule" +) + +var ( + pctx = android.NewPackageContext("android/soong/systemfeatures") +) + +func init() { + registerSystemFeaturesComponents(android.InitRegistrationContext) +} + +func registerSystemFeaturesComponents(ctx android.RegistrationContext) { + ctx.RegisterModuleType("java_system_features_srcs", JavaSystemFeaturesSrcsFactory) +} + +type javaSystemFeaturesSrcs struct { + android.ModuleBase + properties struct { + // The fully qualified class name for the generated code, e.g., com.android.Foo + Full_class_name string + } + outputFiles android.WritablePaths +} + +var _ genrule.SourceFileGenerator = (*javaSystemFeaturesSrcs)(nil) +var _ android.SourceFileProducer = (*javaSystemFeaturesSrcs)(nil) + +func (m *javaSystemFeaturesSrcs) GenerateAndroidBuildActions(ctx android.ModuleContext) { + // Create a file name appropriate for the given fully qualified (w/ package) class name. + classNameParts := strings.Split(m.properties.Full_class_name, ".") + outputDir := android.PathForModuleGen(ctx) + outputFileName := classNameParts[len(classNameParts)-1] + ".java" + outputFile := android.PathForModuleGen(ctx, outputFileName).OutputPath + + // Collect all RELEASE_SYSTEM_FEATURE_$K:$V build flags into a list of "$K:$V" pairs. + var features []string + for k, v := range ctx.Config().ProductVariables().BuildFlags { + if strings.HasPrefix(k, "RELEASE_SYSTEM_FEATURE_") { + shortFeatureName := strings.TrimPrefix(k, "RELEASE_SYSTEM_FEATURE_") + features = append(features, fmt.Sprintf("%s:%s", shortFeatureName, v)) + } + } + // Ensure sorted outputs for consistency of flag ordering in ninja outputs. + sort.Strings(features) + + rule := android.NewRuleBuilder(pctx, ctx) + rule.Command().Text("rm -rf").Text(outputDir.String()) + rule.Command().Text("mkdir -p").Text(outputDir.String()) + rule.Command(). + BuiltTool("systemfeatures-gen-tool"). + Flag(m.properties.Full_class_name). + FlagForEachArg("--feature=", features). + FlagWithArg("--readonly=", fmt.Sprint(ctx.Config().ReleaseUseSystemFeatureBuildFlags())). + FlagWithOutput(" > ", outputFile) + rule.Build(ctx.ModuleName(), "Generating systemfeatures srcs filegroup") + + m.outputFiles = append(m.outputFiles, outputFile) +} + +func (m *javaSystemFeaturesSrcs) Srcs() android.Paths { + return m.outputFiles.Paths() +} + +func (m *javaSystemFeaturesSrcs) GeneratedSourceFiles() android.Paths { + return m.outputFiles.Paths() +} + +func (m *javaSystemFeaturesSrcs) GeneratedDeps() android.Paths { + return m.outputFiles.Paths() +} + +func (m *javaSystemFeaturesSrcs) GeneratedHeaderDirs() android.Paths { + return nil +} + +func JavaSystemFeaturesSrcsFactory() android.Module { + module := &javaSystemFeaturesSrcs{} + module.AddProperties(&module.properties) + android.InitAndroidModule(module) + return module +} diff --git a/systemfeatures/system_features_test.go b/systemfeatures/system_features_test.go new file mode 100644 index 000000000..558bb958e --- /dev/null +++ b/systemfeatures/system_features_test.go @@ -0,0 +1,51 @@ +// Copyright 2024 Google Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package systemfeatures + +import ( + "android/soong/android" + + "testing" +) + +func TestJavaSystemFeaturesSrcs(t *testing.T) { + bp := ` +java_system_features_srcs { + name: "system-features-srcs", + full_class_name: "com.android.test.RoSystemFeatures", +} +` + + res := android.GroupFixturePreparers( + android.FixtureRegisterWithContext(registerSystemFeaturesComponents), + android.PrepareForTestWithBuildFlag("RELEASE_USE_SYSTEM_FEATURE_BUILD_FLAGS", "true"), + android.PrepareForTestWithBuildFlag("RELEASE_SYSTEM_FEATURE_AUTOMOTIVE", "0"), + android.PrepareForTestWithBuildFlag("RELEASE_SYSTEM_FEATURE_TELEVISION", "UNAVAILABLE"), + android.PrepareForTestWithBuildFlag("RELEASE_SYSTEM_FEATURE_WATCH", ""), + android.PrepareForTestWithBuildFlag("RELEASE_NOT_SYSTEM_FEATURE_FOO", "BAR"), + ).RunTestWithBp(t, bp) + + module := res.ModuleForTests("system-features-srcs", "") + cmd := module.Rule("system-features-srcs").RuleParams.Command + android.AssertStringDoesContain(t, "Expected fully class name", cmd, " com.android.test.RoSystemFeatures ") + android.AssertStringDoesContain(t, "Expected readonly flag", cmd, "--readonly=true") + android.AssertStringDoesContain(t, "Expected AUTOMOTIVE feature flag", cmd, "--feature=AUTOMOTIVE:0 ") + android.AssertStringDoesContain(t, "Expected TELEVISION feature flag", cmd, "--feature=TELEVISION:UNAVAILABLE ") + android.AssertStringDoesContain(t, "Expected WATCH feature flag", cmd, "--feature=WATCH: ") + android.AssertStringDoesNotContain(t, "Unexpected FOO arg from non-system feature flag", cmd, "FOO") + + systemFeaturesModule := module.Module().(*javaSystemFeaturesSrcs) + expectedOutputPath := "out/soong/.intermediates/system-features-srcs/gen/RoSystemFeatures.java" + android.AssertPathsRelativeToTopEquals(t, "Expected output file", []string{expectedOutputPath}, systemFeaturesModule.Srcs()) +} |