From 49f6701e9a1f7474522d06aa8b15795fe21885e6 Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Fri, 17 Apr 2020 13:43:10 +0900 Subject: [PATCH] apex: Don't run apex mutators if disabled apex_deps/apex/apex_flattened mutators don't have to run if module is disabled. For some branches which have no valid targets, apex modules are disabled by the os mutator. e.g. aosp-build-tools Some enforcement checks which run during those mutators may fail because build-environment doesn't provide full context information. For example, "Platform_version_all_codenames" config variable is not set while apex.min_sdk_version uses one of those codenames. Bug: 152655956 Test: m Change-Id: I47e27d2d025ba4c36534708b113ce77c4cb2397b --- apex/apex.go | 11 ++++++++++- apex/apex_test.go | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/apex/apex.go b/apex/apex.go index fd3fb2bbf..3542c6a64 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -836,10 +836,13 @@ func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { // Mark the direct and transitive dependencies of apex bundles so that they // can be built for the apex bundles. func apexDepsMutator(mctx android.TopDownMutatorContext) { + if !mctx.Module().Enabled() { + return + } var apexBundles []android.ApexInfo var directDep bool if a, ok := mctx.Module().(*apexBundle); ok && !a.vndkApex { - apexBundles = []android.ApexInfo{android.ApexInfo{ + apexBundles = []android.ApexInfo{{ ApexName: mctx.ModuleName(), MinSdkVersion: a.minSdkVersion(mctx), }} @@ -877,6 +880,9 @@ func inAnySdk(module android.Module) bool { // Create apex variations if a module is included in APEX(s). func apexMutator(mctx android.BottomUpMutatorContext) { + if !mctx.Module().Enabled() { + return + } if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() { am.CreateApexVariations(mctx) } else if a, ok := mctx.Module().(*apexBundle); ok && !a.vndkApex { @@ -914,6 +920,9 @@ func addFlattenedFileContextsInfos(ctx android.BaseModuleContext, fileContextsIn } func apexFlattenedMutator(mctx android.BottomUpMutatorContext) { + if !mctx.Module().Enabled() { + return + } if ab, ok := mctx.Module().(*apexBundle); ok { var variants []string switch proptools.StringDefault(ab.properties.Payload_type, "image") { diff --git a/apex/apex_test.go b/apex/apex_test.go index 0c8937edd..cbff0f151 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -4151,6 +4151,27 @@ func TestApexWithJniLibs(t *testing.T) { }) } +func TestApexMutatorsDontRunIfDisabled(t *testing.T) { + ctx, _ := testApex(t, ` + apex { + name: "myapex", + key: "myapex.key", + } + apex_key { + name: "myapex.key", + public_key: "testkey.avbpubkey", + private_key: "testkey.pem", + } + `, func(fs map[string][]byte, config android.Config) { + delete(config.Targets, android.Android) + config.AndroidCommonTarget = android.Target{} + }) + + if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) { + t.Errorf("Expected variants: %v, but got: %v", expected, got) + } +} + func TestApexWithJniLibs_Errors(t *testing.T) { testApexError(t, `jni_libs: "xxx" is not a cc_library`, ` apex {