Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 66773251 authored by Spandan Das's avatar Spandan Das
Browse files

Propagate strict_updatability_linting to transitive deps of updatable apexes

Create a topdown mutator to walk the deps of updatable apexes. If a dep
is lintable, set its strictUpdatabilityLinting to true

Creating a new mutator after apexInfoMutator makes it easy to maintain
an allowlist (e.g. override_apex does not require an entry in the
allowlist, its canonical name can be retrieved using ApexVariationName())

Test: In build/soong/, go test ./apex
Test: TH
Bug: 182349282

Change-Id: I1b390b0e3a8fb20754ce50c6b253d68d2b3f263b
parent 17854f57
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -76,6 +76,8 @@ func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
	ctx.BottomUp("apex", apexMutator).Parallel()
	ctx.BottomUp("apex_directly_in_any", apexDirectlyInAnyMutator).Parallel()
	ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
	// Register after apex_info mutator so that it can use ApexVariationName
	ctx.TopDown("apex_strict_updatability_lint", apexStrictUpdatibilityLintMutator).Parallel()
}

type apexBundleProperties struct {
@@ -1001,6 +1003,23 @@ func apexInfoMutator(mctx android.TopDownMutatorContext) {
	}
}

// apexStrictUpdatibilityLintMutator propagates strict_updatability_linting to transitive deps of a mainline module
// This check is enforced for updatable modules
func apexStrictUpdatibilityLintMutator(mctx android.TopDownMutatorContext) {
	if !mctx.Module().Enabled() {
		return
	}
	if apex, ok := mctx.Module().(*apexBundle); ok && apex.Updatable() {
		mctx.WalkDeps(func(child, parent android.Module) bool {
			if lintable, ok := child.(java.LintDepSetsIntf); ok {
				lintable.SetStrictUpdatabilityLinting(true)
			}
			// visit transitive deps
			return true
		})
	}
}

// apexUniqueVariationsMutator checks if any dependencies use unique apex variations. If so, use
// unique apex variations for this module. See android/apex.go for more about unique apex variant.
// TODO(jiyong): move this to android/apex.go?
+121 −0
Original line number Diff line number Diff line
@@ -8855,6 +8855,127 @@ func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, mo
	}
}

func TestApexStrictUpdtabilityLint(t *testing.T) {
	bpTemplate := `
		apex {
			name: "myapex",
			key: "myapex.key",
			java_libs: ["myjavalib"],
			updatable: %v,
			min_sdk_version: "29",
		}
		apex_key {
			name: "myapex.key",
		}
		java_library {
			name: "myjavalib",
			srcs: ["MyClass.java"],
			apex_available: [ "myapex" ],
			lint: {
				strict_updatability_linting: %v,
			},
			sdk_version: "current",
			min_sdk_version: "29",
		}
		`
	fs := android.MockFS{
		"lint-baseline.xml": nil,
	}

	testCases := []struct {
		testCaseName              string
		apexUpdatable             bool
		javaStrictUpdtabilityLint bool
		lintFileExists            bool
		disallowedFlagExpected    bool
	}{
		{
			testCaseName:              "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
			apexUpdatable:             true,
			javaStrictUpdtabilityLint: true,
			lintFileExists:            false,
			disallowedFlagExpected:    false,
		},
		{
			testCaseName:              "non-updatable apex respects strict_updatability of javalib",
			apexUpdatable:             false,
			javaStrictUpdtabilityLint: false,
			lintFileExists:            true,
			disallowedFlagExpected:    false,
		},
		{
			testCaseName:              "non-updatable apex respects strict updatability of javalib",
			apexUpdatable:             false,
			javaStrictUpdtabilityLint: true,
			lintFileExists:            true,
			disallowedFlagExpected:    true,
		},
		{
			testCaseName:              "updatable apex sets strict updatability of javalib to true",
			apexUpdatable:             true,
			javaStrictUpdtabilityLint: false, // will be set to true by mutator
			lintFileExists:            true,
			disallowedFlagExpected:    true,
		},
	}

	for _, testCase := range testCases {
		bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
		fixtures := []android.FixturePreparer{}
		if testCase.lintFileExists {
			fixtures = append(fixtures, fs.AddToFixture())
		}

		result := testApex(t, bp, fixtures...)
		myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
		sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
		disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")

		if disallowedFlagActual != testCase.disallowedFlagExpected {
			t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
		}
	}
}

// checks transtive deps of an apex coming from bootclasspath_fragment
func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
	bp := `
		apex {
			name: "myapex",
			key: "myapex.key",
			bootclasspath_fragments: ["mybootclasspathfragment"],
			updatable: true,
			min_sdk_version: "29",
		}
		apex_key {
			name: "myapex.key",
		}
		bootclasspath_fragment {
			name: "mybootclasspathfragment",
			contents: ["myjavalib"],
			apex_available: ["myapex"],
		}
		java_library {
			name: "myjavalib",
			srcs: ["MyClass.java"],
			apex_available: [ "myapex" ],
			sdk_version: "current",
			min_sdk_version: "29",
			compile_dex: true,
		}
		`
	fs := android.MockFS{
		"lint-baseline.xml": nil,
	}

	result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
	myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
	sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
	if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
		t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
	}
}

func TestMain(m *testing.M) {
	os.Exit(m.Run())
}