From f9b1da0fcb4b76b04c96a355bec7845cdf87ba7e Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Wed, 18 Dec 2019 19:51:55 +0000 Subject: [PATCH] Dedup registration code for module types and singletons The registration of module types and singletons is duplicated between init() functions that register them for use in the build runtime and test context creation code that registers them for testing. This is a proof of concept for a mechanism that will allow the code to be shared. It defines a RegistrationContext interface that is implemented by both the TestContext and the new initRegistrationContext type. An instance of the the latter is available through the InitRegistrationContext variable. The intent is that the registration of the module types and singleton types will be extracted from the init() function into a separate function that takes a RegistrationContext parameter. That method is called from init() passing in the InitRegistrationContext and from a test passing in the TestContext. Something like this: func init() { RegisterBuildComponents(android.InitRegistrationContext) } func RegisterBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType(....) .... } A test would do something like this: ctx := android.NewTestContext() RegisterBuildComponents(ctx) Test: m nothing Change-Id: I97173cabb6d6cf7ce98fdb5f73418438b1997b35 --- android/register.go | 40 ++++++++++++++++++++++++++++++++++++++++ apex/apex_test.go | 8 ++------ java/aar.go | 8 ++++++-- java/app.go | 20 ++++++++++++-------- java/java.go | 40 ++++++++++++++++++++++------------------ java/java_test.go | 23 +++-------------------- sdk/testing.go | 6 ++---- sysprop/sysprop_test.go | 4 ++-- 8 files changed, 89 insertions(+), 60 deletions(-) diff --git a/android/register.go b/android/register.go index d79982a31..86943f295 100644 --- a/android/register.go +++ b/android/register.go @@ -114,3 +114,43 @@ func ModuleTypeFactories() map[string]ModuleFactory { } return ret } + +// Interface for registering build components. +// +// Provided to allow registration of build components to be shared between the runtime +// and test environments. +type RegistrationContext interface { + RegisterModuleType(name string, factory ModuleFactory) + RegisterSingletonType(name string, factory SingletonFactory) +} + +// Used to register build components from an init() method, e.g. +// +// init() { +// RegisterBuildComponents(android.InitRegistrationContext) +// } +// +// func RegisterBuildComponents(ctx android.RegistrationContext) { +// ctx.RegisterModuleType(...) +// ... +// } +// +// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function +// allows it to be used to initialize test context, e.g. +// +// ctx := android.NewTestContext() +// RegisterBuildComponents(ctx) +var InitRegistrationContext RegistrationContext = initRegistrationContext{} + +// Make sure the TestContext implements RegistrationContext. +var _ RegistrationContext = (*TestContext)(nil) + +type initRegistrationContext struct{} + +func (ctx initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) { + RegisterModuleType(name, factory) +} + +func (ctx initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) { + RegisterSingletonType(name, factory) +} diff --git a/apex/apex_test.go b/apex/apex_test.go index b2d891dbe..18409001e 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -303,14 +303,10 @@ func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*andr ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory) ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory) - ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) - ctx.RegisterModuleType("java_import", java.ImportFactory) + java.RegisterJavaBuildComponents(ctx) ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory) - ctx.RegisterModuleType("android_app", java.AndroidAppFactory) - ctx.RegisterModuleType("android_app_import", java.AndroidAppImportFactory) - ctx.RegisterModuleType("override_android_app", java.OverrideAndroidAppModuleFactory) + java.RegisterAppBuildComponents(ctx) ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { diff --git a/java/aar.go b/java/aar.go index e0bea5df3..201e590be 100644 --- a/java/aar.go +++ b/java/aar.go @@ -34,8 +34,12 @@ type AndroidLibraryDependency interface { } func init() { - android.RegisterModuleType("android_library_import", AARImportFactory) - android.RegisterModuleType("android_library", AndroidLibraryFactory) + RegisterAARBuildComponents(android.InitRegistrationContext) +} + +func RegisterAARBuildComponents(ctx android.RegistrationContext) { + ctx.RegisterModuleType("android_library_import", AARImportFactory) + ctx.RegisterModuleType("android_library", AndroidLibraryFactory) } // diff --git a/java/app.go b/java/app.go index ae637fd7f..94f6bb10a 100755 --- a/java/app.go +++ b/java/app.go @@ -33,18 +33,22 @@ import ( var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"} func init() { - android.RegisterModuleType("android_app", AndroidAppFactory) - android.RegisterModuleType("android_test", AndroidTestFactory) - android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) - android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) - android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) - android.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) - android.RegisterModuleType("android_app_import", AndroidAppImportFactory) - android.RegisterModuleType("android_test_import", AndroidTestImportFactory) + RegisterAppBuildComponents(android.InitRegistrationContext) initAndroidAppImportVariantGroupTypes() } +func RegisterAppBuildComponents(ctx android.RegistrationContext) { + ctx.RegisterModuleType("android_app", AndroidAppFactory) + ctx.RegisterModuleType("android_test", AndroidTestFactory) + ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) + ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) + ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) + ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) + ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) + ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) +} + // AndroidManifest.xml merging // package splits diff --git a/java/java.go b/java/java.go index 052e06f6f..4c80ba77a 100644 --- a/java/java.go +++ b/java/java.go @@ -34,24 +34,7 @@ import ( ) func init() { - android.RegisterModuleType("java_defaults", DefaultsFactory) - - android.RegisterModuleType("java_library", LibraryFactory) - android.RegisterModuleType("java_library_static", LibraryStaticFactory) - android.RegisterModuleType("java_library_host", LibraryHostFactory) - android.RegisterModuleType("java_binary", BinaryFactory) - android.RegisterModuleType("java_binary_host", BinaryHostFactory) - android.RegisterModuleType("java_test", TestFactory) - android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory) - android.RegisterModuleType("java_test_host", TestHostFactory) - android.RegisterModuleType("java_import", ImportFactory) - android.RegisterModuleType("java_import_host", ImportFactoryHost) - android.RegisterModuleType("java_device_for_host", DeviceForHostFactory) - android.RegisterModuleType("java_host_for_device", HostForDeviceFactory) - android.RegisterModuleType("dex_import", DexImportFactory) - - android.RegisterSingletonType("logtags", LogtagsSingleton) - android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory) + RegisterJavaBuildComponents(android.InitRegistrationContext) // Register sdk member types. android.RegisterSdkMemberType(&headerLibrarySdkMemberType{ @@ -71,6 +54,27 @@ func init() { }) } +func RegisterJavaBuildComponents(ctx android.RegistrationContext) { + ctx.RegisterModuleType("java_defaults", DefaultsFactory) + + ctx.RegisterModuleType("java_library", LibraryFactory) + ctx.RegisterModuleType("java_library_static", LibraryStaticFactory) + ctx.RegisterModuleType("java_library_host", LibraryHostFactory) + ctx.RegisterModuleType("java_binary", BinaryFactory) + ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) + ctx.RegisterModuleType("java_test", TestFactory) + ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory) + ctx.RegisterModuleType("java_test_host", TestHostFactory) + ctx.RegisterModuleType("java_import", ImportFactory) + ctx.RegisterModuleType("java_import_host", ImportFactoryHost) + ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) + ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) + ctx.RegisterModuleType("dex_import", DexImportFactory) + + ctx.RegisterSingletonType("logtags", LogtagsSingleton) + ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory) +} + func (j *Module) checkSdkVersion(ctx android.ModuleContext) { if j.SocSpecific() || j.DeviceSpecific() || (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) { diff --git a/java/java_test.go b/java/java_test.go index 49838c7c7..32527365c 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -63,27 +63,12 @@ func testConfig(env map[string]string, bp string, fs map[string][]byte) android. func testContext() *android.TestContext { ctx := android.NewTestArchContext() - ctx.RegisterModuleType("android_app", AndroidAppFactory) - ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) - ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) - ctx.RegisterModuleType("android_library", AndroidLibraryFactory) - ctx.RegisterModuleType("android_test", AndroidTestFactory) - ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) - ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) - ctx.RegisterModuleType("java_binary", BinaryFactory) - ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) - ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) - ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) - ctx.RegisterModuleType("java_library", LibraryFactory) - ctx.RegisterModuleType("java_library_host", LibraryHostFactory) - ctx.RegisterModuleType("java_test", TestFactory) - ctx.RegisterModuleType("java_import", ImportFactory) - ctx.RegisterModuleType("java_import_host", ImportFactoryHost) - ctx.RegisterModuleType("java_defaults", DefaultsFactory) + RegisterJavaBuildComponents(ctx) + RegisterAppBuildComponents(ctx) + RegisterAARBuildComponents(ctx) ctx.RegisterModuleType("java_system_modules", SystemModulesFactory) ctx.RegisterModuleType("java_genrule", genRuleFactory) ctx.RegisterModuleType("java_plugin", PluginFactory) - ctx.RegisterModuleType("dex_import", DexImportFactory) ctx.RegisterModuleType("filegroup", android.FileGroupFactory) ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) ctx.RegisterModuleType("droiddoc", DroiddocFactory) @@ -92,8 +77,6 @@ func testContext() *android.TestContext { ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory) ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory) ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory) - ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) - ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) diff --git a/sdk/testing.go b/sdk/testing.go index 77ba2e690..3dc88c1c9 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -78,10 +78,8 @@ func testSdkContext(bp string, fs map[string][]byte) (*android.TestContext, andr ctx.RegisterModuleType("package", android.PackageFactory) // from java package - ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) - ctx.RegisterModuleType("java_defaults", java.DefaultsFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) - ctx.RegisterModuleType("java_import", java.ImportFactory) + java.RegisterJavaBuildComponents(ctx) + java.RegisterAppBuildComponents(ctx) ctx.RegisterModuleType("droidstubs", java.DroidstubsFactory) ctx.RegisterModuleType("prebuilt_stubs_sources", java.PrebuiltStubsSourcesFactory) diff --git a/sysprop/sysprop_test.go b/sysprop/sysprop_test.go index 6ac3f4c96..d44f4cf2f 100644 --- a/sysprop/sysprop_test.go +++ b/sysprop/sysprop_test.go @@ -56,8 +56,8 @@ func TestMain(m *testing.M) { func testContext(config android.Config) *android.TestContext { ctx := android.NewTestArchContext() - ctx.RegisterModuleType("android_app", java.AndroidAppFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) + java.RegisterJavaBuildComponents(ctx) + java.RegisterAppBuildComponents(ctx) ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory) ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)