From 7d6dd8bb3324e81b5c4afc80b062baad2fed1770 Mon Sep 17 00:00:00 2001 From: Anton Hansson Date: Mon, 6 Mar 2023 11:26:17 +0000 Subject: [PATCH] Add filegroup_defaults module Allows specifying shared filegroup attributes in a central place. Test: filegroup_test.go Change-Id: I82feac37ae6998313a0004f8af58f3decf7a514e --- android/filegroup.go | 25 +++++++++++++++++++++++-- android/filegroup_test.go | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/android/filegroup.go b/android/filegroup.go index 0f6e00e86..278d46d07 100644 --- a/android/filegroup.go +++ b/android/filegroup.go @@ -26,13 +26,18 @@ import ( ) func init() { - RegisterModuleType("filegroup", FileGroupFactory) + RegisterFilegroupBuildComponents(InitRegistrationContext) } var PrepareForTestWithFilegroup = FixtureRegisterWithContext(func(ctx RegistrationContext) { - ctx.RegisterModuleType("filegroup", FileGroupFactory) + RegisterFilegroupBuildComponents(ctx) }) +func RegisterFilegroupBuildComponents(ctx RegistrationContext) { + ctx.RegisterModuleType("filegroup", FileGroupFactory) + ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory) +} + var convertedProtoLibrarySuffix = "_bp2build_converted" // IsFilegroup checks that a module is a filegroup type @@ -178,6 +183,7 @@ type fileGroupProperties struct { type fileGroup struct { ModuleBase BazelModuleBase + DefaultableModuleBase FileGroupAsLibrary properties fileGroupProperties srcs Paths @@ -195,6 +201,7 @@ func FileGroupFactory() Module { module.AddProperties(&module.properties) InitAndroidModule(module) InitBazelModule(module) + InitDefaultableModule(module) return module } @@ -326,3 +333,17 @@ func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGrou } return nil, false } + +// Defaults +type FileGroupDefaults struct { + ModuleBase + DefaultsModuleBase +} + +func FileGroupDefaultsFactory() Module { + module := &FileGroupDefaults{} + module.AddProperties(&fileGroupProperties{}) + InitDefaultsModule(module) + + return module +} diff --git a/android/filegroup_test.go b/android/filegroup_test.go index 8292d5e67..893da5774 100644 --- a/android/filegroup_test.go +++ b/android/filegroup_test.go @@ -58,3 +58,24 @@ func TestFileGroupWithPathProp(t *testing.T) { AssertStringEquals(t, "src full path", expectedOutputfile, fg.srcs[0].String()) } } + +func TestFilegroupDefaults(t *testing.T) { + bp := FixtureAddTextFile("p/Android.bp", ` + filegroup_defaults { + name: "defaults", + visibility: ["//x"], + } + filegroup { + name: "foo", + defaults: ["defaults"], + visibility: ["//y"], + } + `) + result := GroupFixturePreparers( + PrepareForTestWithFilegroup, + PrepareForTestWithDefaults, + PrepareForTestWithVisibility, + bp).RunTest(t) + rules := effectiveVisibilityRules(result.Config, qualifiedModuleName{pkg: "p", name: "foo"}) + AssertDeepEquals(t, "visibility", []string{"//x", "//y"}, rules.Strings()) +}