diff options
author | 2024-10-11 00:21:57 +0000 | |
---|---|---|
committer | 2024-10-11 22:59:41 +0000 | |
commit | dc6492f01bbfb4a0f3e10efe6fa8e7cb5a876982 (patch) | |
tree | 8582cf446fdf69200da4bf1866e657b860d0953f | |
parent | acd01347be288a67badcfc0c668742bbd194550b (diff) |
Set the appropriate deps property for the soong generated fs modules
This change:
- Adds a pre-deps bottom-up mutator that sets the appropriate deps
properties for the soong generate filesystem partition modules
- Makes `installInSysem` more genenric, so that it can be used for other
partitions
- Introduces `AppendDepsEntries` method in `android.PackagingBase` to
utilize it in the aforementioned mutator
- Modifies `fsDeps` from a 1D slice to a map (of partition to deps slice)
Test: m nothing --no-skip-soong-tests
Bug: 372771060
Change-Id: Ic251993d1d4c1caca544c5cebcaf29afd749da9e
-rw-r--r-- | android/neverallow.go | 9 | ||||
-rw-r--r-- | android/neverallow_test.go | 16 | ||||
-rw-r--r-- | android/visibility_test.go | 5 | ||||
-rw-r--r-- | filesystem/filesystem.go | 26 | ||||
-rw-r--r-- | fsgen/filesystem_creator.go | 113 | ||||
-rw-r--r-- | fsgen/filesystem_creator_test.go | 54 |
6 files changed, 179 insertions, 44 deletions
diff --git a/android/neverallow.go b/android/neverallow.go index e93763b7b..600175da7 100644 --- a/android/neverallow.go +++ b/android/neverallow.go @@ -60,6 +60,7 @@ func init() { AddNeverAllowRules(createCcStubsRule()) AddNeverAllowRules(createProhibitHeaderOnlyRule()) AddNeverAllowRules(createLimitNdkExportRule()...) + AddNeverAllowRules(createFilesystemIsAutoGeneratedRule()) } // Add a NeverAllow rule to the set of rules to apply. @@ -275,6 +276,14 @@ func createLimitNdkExportRule() []Rule { } } +func createFilesystemIsAutoGeneratedRule() Rule { + return NeverAllow(). + NotIn("build/soong/fsgen"). + ModuleType("filesystem", "android_system_image"). + WithMatcher("is_auto_generated", isSetMatcherInstance). + Because("is_auto_generated property is only allowed for filesystem modules in build/soong/fsgen directory") +} + func neverallowMutator(ctx BottomUpMutatorContext) { m, ok := ctx.Module().(Module) if !ok { diff --git a/android/neverallow_test.go b/android/neverallow_test.go index 192c92409..caec8c7d9 100644 --- a/android/neverallow_test.go +++ b/android/neverallow_test.go @@ -359,6 +359,21 @@ var neverallowTests = []struct { `headers_only can only be used for generating framework-minus-apex headers for non-updatable modules`, }, }, + // Test for the rule restricting use of is_auto_generated + { + name: `"is_auto_generated" outside allowed directory`, + fs: map[string][]byte{ + "a/b/Android.bp": []byte(` + filesystem { + name: "baaz", + is_auto_generated: true, + } + `), + }, + expectedErrors: []string{ + `is_auto_generated property is only allowed for filesystem modules in build/soong/fsgen directory`, + }, + }, } var prepareForNeverAllowTest = GroupFixturePreparers( @@ -367,6 +382,7 @@ var prepareForNeverAllowTest = GroupFixturePreparers( ctx.RegisterModuleType("java_library", newMockJavaLibraryModule) ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule) ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule) + ctx.RegisterModuleType("filesystem", newMockFilesystemModule) }), ) diff --git a/android/visibility_test.go b/android/visibility_test.go index 1a2eecafb..277be0f65 100644 --- a/android/visibility_test.go +++ b/android/visibility_test.go @@ -2098,8 +2098,9 @@ func (p *mockLibraryModule) GenerateAndroidBuildActions(ModuleContext) { } type mockFilesystemModuleProperties struct { - Partition_type *string - Deps []string + Partition_type *string + Deps []string + Is_auto_generated *bool } type mockFilesystemModule struct { diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go index 1e816a752..7d3b8e108 100644 --- a/filesystem/filesystem.go +++ b/filesystem/filesystem.go @@ -145,6 +145,10 @@ type FilesystemProperties struct { Unchecked_module *bool `blueprint:"mutated"` Erofs ErofsProperties + + // Determines if the module is auto-generated from Soong or not. If the module is + // auto-generated, its deps are exempted from visibility enforcement. + Is_auto_generated *bool } // Additional properties required to generate erofs FS partitions. @@ -179,13 +183,29 @@ func initFilesystemModule(module android.DefaultableModule, filesystemModule *fi android.InitDefaultableModule(module) } -var dependencyTag = struct { +type depTag struct { blueprint.BaseDependencyTag android.PackagingItemAlwaysDepTag -}{} +} + +var dependencyTag = depTag{} + +type depTagWithVisibilityEnforcementBypass struct { + depTag +} + +var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil) + +func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {} + +var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{} func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { - f.AddDeps(ctx, dependencyTag) + if proptools.Bool(f.properties.Is_auto_generated) { + f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass) + } else { + f.AddDeps(ctx, dependencyTag) + } } type fsType int diff --git a/fsgen/filesystem_creator.go b/fsgen/filesystem_creator.go index ed0c390ba..8acc74c27 100644 --- a/fsgen/filesystem_creator.go +++ b/fsgen/filesystem_creator.go @@ -43,36 +43,52 @@ func registerBuildComponents(ctx android.RegistrationContext) { func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) { ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState() + ctx.BottomUp("fs_set_deps", setDepsMutator) } var fsDepsMutex = sync.Mutex{} var collectFsDepsOnceKey = android.NewOnceKey("CollectFsDeps") var depCandidatesOnceKey = android.NewOnceKey("DepCandidates") +// List of partitions that the filesystem_creator module currently generates the partition +var soongGeneratedPartitions = []string{"system"} + +// Map of partition module name to its partition that may be generated by Soong. +// Note that it is not guaranteed that all modules returned by this function are successfully +// created. +func getAllSoongGeneratedPartitionNames(config android.Config) map[string]string { + ret := map[string]string{} + for _, partition := range soongGeneratedPartitions { + ret[generatedModuleNameForPartition(config, partition)] = partition + } + return ret +} + func collectDepsMutator(mctx android.BottomUpMutatorContext) { // These additional deps are added according to the cuttlefish system image bp. fsDeps := mctx.Config().Once(collectFsDepsOnceKey, func() interface{} { - deps := []string{ - "android_vintf_manifest", - "com.android.apex.cts.shim.v1_prebuilt", - "dex_bootjars", - "framework_compatibility_matrix.device.xml", - "idc_data", - "init.environ.rc-soong", - "keychars_data", - "keylayout_data", - "libclang_rt.asan", - "libcompiler_rt", - "libdmabufheap", - "libgsi", - "llndk.libraries.txt", - "logpersist.start", - "preloaded-classes", - "public.libraries.android.txt", - "update_engine_sideload", + deps := map[string][]string{ + "system": { + "com.android.apex.cts.shim.v1_prebuilt", + "dex_bootjars", + "framework_compatibility_matrix.device.xml", + "idc_data", + "init.environ.rc-soong", + "keychars_data", + "keylayout_data", + "libclang_rt.asan", + "libcompiler_rt", + "libdmabufheap", + "libgsi", + "llndk.libraries.txt", + "logpersist.start", + "preloaded-classes", + "public.libraries.android.txt", + "update_engine_sideload", + }, } return &deps - }).(*[]string) + }).(*map[string][]string) depCandidates := mctx.Config().Once(depCandidatesOnceKey, func() interface{} { partitionVars := mctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse @@ -82,10 +98,30 @@ func collectDepsMutator(mctx android.BottomUpMutatorContext) { m := mctx.Module() if slices.Contains(*depCandidates, m.Name()) { - if installInSystem(mctx, m) { - fsDepsMutex.Lock() - *fsDeps = append(*fsDeps, m.Name()) - fsDepsMutex.Unlock() + installPartition := getInstallPartition(m, mctx.DeviceConfig()) + fsDepsMutex.Lock() + if _, ok := (*fsDeps)[installPartition]; !ok { + (*fsDeps)[installPartition] = make([]string, 0) + } + if m.Enabled(mctx) { + (*fsDeps)[installPartition] = append((*fsDeps)[installPartition], m.Name()) + } + fsDepsMutex.Unlock() + } +} + +type depsStruct struct { + Deps []string +} + +func setDepsMutator(mctx android.BottomUpMutatorContext) { + fsDeps := mctx.Config().Get(collectFsDepsOnceKey).(*map[string][]string) + soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config()) + m := mctx.Module() + if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok { + deps := (*fsDeps)[partition] + if err := proptools.AppendMatchingProperties(m.GetProperties(), &depsStruct{Deps: deps}, nil); err != nil { + mctx.ModuleErrorf(err.Error()) } } } @@ -114,7 +150,7 @@ func filesystemCreatorFactory() android.Module { } func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) { - for _, partitionType := range []string{"system"} { + for _, partitionType := range soongGeneratedPartitions { if f.createPartition(ctx, partitionType) { f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType) } else { @@ -124,7 +160,7 @@ func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) { f.createDeviceModule(ctx) } -func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix string) string { +func generatedModuleName(cfg android.Config, suffix string) string { prefix := "soong" if cfg.HasDeviceProduct() { prefix = cfg.DeviceProduct() @@ -132,21 +168,21 @@ func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix strin return fmt.Sprintf("%s_generated_%s", prefix, suffix) } -func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string { - return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType)) +func generatedModuleNameForPartition(cfg android.Config, partitionType string) string { + return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType)) } func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) { baseProps := &struct { Name *string }{ - Name: proptools.StringPtr(f.generatedModuleName(ctx.Config(), "device")), + Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")), } // Currently, only the system partition module is created. partitionProps := &filesystem.PartitionNameProperties{} if android.InList("system", f.properties.Generated_partition_types) { - partitionProps.System_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system")) + partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system")) } ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps) @@ -158,7 +194,7 @@ func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partiti baseProps := &struct { Name *string }{ - Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)), + Name: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)), } fsProps := &filesystem.FilesystemProperties{} @@ -195,6 +231,8 @@ func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partiti fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true) + fsProps.Is_auto_generated = proptools.BoolPtr(true) + // Identical to that of the generic_system_image fsProps.Fsverity.Inputs = []string{ "etc/boot-image.prof", @@ -229,7 +267,7 @@ func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partiti } func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path { - partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType) + partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType) systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag) filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider) if !ok { @@ -273,7 +311,7 @@ var generatedFilesystemDepTag systemImageDepTagType func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) { for _, partitionType := range f.properties.Generated_partition_types { - ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType)) + ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType)) } } @@ -298,11 +336,8 @@ func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContex ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...) } -func installInSystem(ctx android.BottomUpMutatorContext, m android.Module) bool { - return m.PartitionTag(ctx.DeviceConfig()) == "system" && !m.InstallInData() && - !m.InstallInTestcases() && !m.InstallInSanitizerDir() && !m.InstallInVendorRamdisk() && - !m.InstallInDebugRamdisk() && !m.InstallInRecovery() && !m.InstallInOdm() && - !m.InstallInVendor() +func getInstallPartition(m android.Module, config android.DeviceConfig) string { + return m.PartitionTag(config) } // TODO: assemble baseProps and fsProps here @@ -312,9 +347,9 @@ func generateBpContent(ctx android.EarlyModuleContext, partitionType string) str return "" } - deps := ctx.Config().Get(collectFsDepsOnceKey).(*[]string) + deps := ctx.Config().Get(collectFsDepsOnceKey).(*map[string][]string) depProps := &android.PackagingProperties{ - Deps: android.NewSimpleConfigurable(android.SortedUniqueStrings(*deps)), + Deps: android.NewSimpleConfigurable(android.SortedUniqueStrings((*deps)[partitionType])), } result, err := proptools.RepackProperties([]interface{}{depProps}) diff --git a/fsgen/filesystem_creator_test.go b/fsgen/filesystem_creator_test.go index 554b66b7a..f96bdce36 100644 --- a/fsgen/filesystem_creator_test.go +++ b/fsgen/filesystem_creator_test.go @@ -17,6 +17,7 @@ package fsgen import ( "android/soong/android" "android/soong/filesystem" + "android/soong/java" "testing" "github.com/google/blueprint/proptools" @@ -28,6 +29,7 @@ func TestFileSystemCreatorSystemImageProps(t *testing.T) { result := android.GroupFixturePreparers( android.PrepareForIntegrationTestWithAndroid, android.PrepareForTestWithAndroidBuildComponents, + android.PrepareForTestWithAllowMissingDependencies, filesystem.PrepareForTestWithFilesystemBuildComponents, prepareForTestWithFsgenBuildComponents, android.FixtureModifyConfig(func(config android.Config) { @@ -86,3 +88,55 @@ func TestFileSystemCreatorSystemImageProps(t *testing.T) { proptools.String(fooSystem.FsProps().Type), ) } + +func TestFileSystemCreatorSetPartitionDeps(t *testing.T) { + result := android.GroupFixturePreparers( + android.PrepareForIntegrationTestWithAndroid, + android.PrepareForTestWithAndroidBuildComponents, + android.PrepareForTestWithAllowMissingDependencies, + filesystem.PrepareForTestWithFilesystemBuildComponents, + prepareForTestWithFsgenBuildComponents, + java.PrepareForTestWithJavaBuildComponents, + java.PrepareForTestWithJavaDefaultModules, + android.FixtureModifyConfig(func(config android.Config) { + config.TestProductVariables.PartitionVarsForSoongMigrationOnlyDoNotUse.ProductPackages = []string{"bar", "baz"} + config.TestProductVariables.PartitionVarsForSoongMigrationOnlyDoNotUse.PartitionQualifiedVariables = + map[string]android.PartitionQualifiedVariablesType{ + "system": { + BoardFileSystemType: "ext4", + }, + } + }), + android.FixtureMergeMockFs(android.MockFS{ + "external/avb/test/data/testkey_rsa4096.pem": nil, + "build/soong/fsgen/Android.bp": []byte(` + soong_filesystem_creator { + name: "foo", + } + `), + }), + ).RunTestWithBp(t, ` + java_library { + name: "bar", + srcs: ["A.java"], + } + java_library { + name: "baz", + srcs: ["A.java"], + product_specific: true, + } + `) + + android.AssertBoolEquals( + t, + "Generated system image expected to depend on system partition installed \"bar\"", + true, + java.CheckModuleHasDependency(t, result.TestContext, "test_product_generated_system_image", "android_common", "bar"), + ) + android.AssertBoolEquals( + t, + "Generated system image expected to not depend on product partition installed \"baz\"", + false, + java.CheckModuleHasDependency(t, result.TestContext, "test_product_generated_system_image", "android_common", "baz"), + ) +} |