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

Commit 8609a556 authored by Nouby Mohamed's avatar Nouby Mohamed Committed by Jihoon Kang
Browse files

Added EXTRA_ALLOWED_DEPS_TXT to allow arbitrary allowedlist text files that...

Added EXTRA_ALLOWED_DEPS_TXT to allow arbitrary allowedlist text files that enforces min_sdk_version for apex dependencies to avoid regression

Test: Ran APEX soong tests and manual test
Bug: 333868045

Merged-In: I7b0c7f32687d922dde41150f947c1b993998338f
Change-Id: I7b0c7f32687d922dde41150f947c1b993998338f
parent 0fdc7558
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -2086,6 +2086,10 @@ func (c *config) OdmPropFiles(ctx PathContext) Paths {
	return PathsForSource(ctx, c.productVariables.OdmPropFiles)
}

func (c *config) ExtraAllowedDepsTxt() string {
	return String(c.productVariables.ExtraAllowedDepsTxt)
}

func (c *config) EnableUffdGc() string {
	return String(c.productVariables.EnableUffdGc)
}
+2 −0
Original line number Diff line number Diff line
@@ -523,6 +523,8 @@ type ProductVariables struct {

	PartitionVarsForSoongMigrationOnlyDoNotUse PartitionVariables

	ExtraAllowedDepsTxt *string `json:",omitempty"`

	AdbKeys *string `json:",omitempty"`
}

+25 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package apex

import (
	"encoding/json"
	"strings"

	"github.com/google/blueprint"

@@ -58,9 +59,9 @@ var (

	// Diff two given lists while ignoring comments in the allowed deps file.
	diffAllowedApexDepsInfoRule = pctx.AndroidStaticRule("diffAllowedApexDepsInfoRule", blueprint.RuleParams{
		Description: "Diff ${allowed_deps} and ${new_allowed_deps}",
		Description: "Diff ${allowed_deps_list} and ${new_allowed_deps}",
		Command: `
			if grep -v '^#' ${allowed_deps} | diff -B - ${new_allowed_deps}; then
			if grep -v -h '^#' ${allowed_deps_list} | sort -u -f| diff -B -u - ${new_allowed_deps}; then
			   touch ${out};
			else
				echo -e "\n******************************";
@@ -81,10 +82,15 @@ var (
				exit 1;
			fi;
		`,
	}, "allowed_deps", "new_allowed_deps")
	}, "allowed_deps_list", "new_allowed_deps")
)

func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContext) {
	allowedDepsSources := []android.OptionalPath{android.ExistentPathForSource(ctx, "packages/modules/common/build/allowed_deps.txt")}
	extraAllowedDepsPath := ctx.Config().ExtraAllowedDepsTxt()
	if extraAllowedDepsPath != "" {
		allowedDepsSources = append(allowedDepsSources, android.ExistentPathForSource(ctx, extraAllowedDepsPath))
	}
	updatableFlatLists := android.Paths{}
	ctx.VisitAllModules(func(module android.Module) {
		if binaryInfo, ok := module.(android.ApexBundleDepsInfoIntf); ok {
@@ -96,37 +102,42 @@ func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContex
			}
		}
	})

	allowedDepsSource := android.ExistentPathForSource(ctx, "packages/modules/common/build/allowed_deps.txt")
	newAllowedDeps := android.PathForOutput(ctx, "apex", "depsinfo", "new-allowed-deps.txt")
	s.allowedApexDepsInfoCheckResult = android.PathForOutput(ctx, newAllowedDeps.Rel()+".check")

	if !allowedDepsSource.Valid() {
	hasOneValidDepsPath := false
	for _, allowedDepsSource := range allowedDepsSources {
		if allowedDepsSource.Valid() {
			hasOneValidDepsPath = true
			updatableFlatLists = append(updatableFlatLists, allowedDepsSource.Path())
		}
	}
	allowedDepsStrList := make([]string, len(allowedDepsSources))
	for _, value := range allowedDepsSources {
		allowedDepsStrList = append(allowedDepsStrList, value.String())
	}
	allowedDepsListString := strings.Join(allowedDepsStrList, " ")
	if !hasOneValidDepsPath {
		// Unbundled projects may not have packages/modules/common/ checked out; ignore those.
		ctx.Build(pctx, android.BuildParams{
			Rule:   android.Touch,
			Output: s.allowedApexDepsInfoCheckResult,
		})
	} else {
		allowedDeps := allowedDepsSource.Path()

		ctx.Build(pctx, android.BuildParams{
			Rule:   generateApexDepsInfoFilesRule,
			Inputs: append(updatableFlatLists, allowedDeps),
			Inputs: updatableFlatLists,
			Output: newAllowedDeps,
		})

		ctx.Build(pctx, android.BuildParams{
			Rule:   diffAllowedApexDepsInfoRule,
			Input:  newAllowedDeps,
			Output: s.allowedApexDepsInfoCheckResult,
			Args: map[string]string{
				"allowed_deps":     allowedDeps.String(),
				"allowed_deps_list": allowedDepsListString,
				"new_allowed_deps":  newAllowedDeps.String(),
			},
		})
	}

	ctx.Phony("apex-allowed-deps-check", s.allowedApexDepsInfoCheckResult)
}

+145 −0
Original line number Diff line number Diff line
@@ -2185,6 +2185,151 @@ func TestTrackAllowedDeps(t *testing.T) {
		flatlist, "yourlib(minSdkVersion:29)")
}

func TestTrackCustomAllowedDepsInvalidDefaultTxt(t *testing.T) {
	ctx := testApex(t, `
		apex {
			name: "myapex",
			key: "myapex.key",
			updatable: true,
			native_shared_libs: [
				"mylib",
				"yourlib",
			],
			min_sdk_version: "29",
		}

		apex {
			name: "myapex2",
			key: "myapex.key",
			updatable: false,
			native_shared_libs: ["yourlib"],
		}

		apex_key {
			name: "myapex.key",
			public_key: "testkey.avbpubkey",
			private_key: "testkey.pem",
		}

		cc_library {
			name: "mylib",
			srcs: ["mylib.cpp"],
			shared_libs: ["libbar"],
			min_sdk_version: "29",
			apex_available: ["myapex"],
		}

		cc_library {
			name: "libbar",
			stubs: { versions: ["29", "30"] },
		}

		cc_library {
			name: "yourlib",
			srcs: ["mylib.cpp"],
			min_sdk_version: "29",
			apex_available: ["myapex", "myapex2", "//apex_available:platform"],
		}
	`, withFiles(android.MockFS{
		"packages/modules/common/build/custom_allowed_deps.txt": nil,
	}),
		android.FixtureModifyProductVariables(
			func(variables android.FixtureProductVariables) {
				variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
			},
		))

	depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
	inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
	android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
		"out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
	android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
		"out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")

	myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
	flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
		myapex.Output("depsinfo/flatlist.txt")), "\n")
	android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
		flatlist, "libbar(minSdkVersion:(no version)) (external)")
	android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
		flatlist, "mylib:(minSdkVersion:29)")
	android.AssertStringListContains(t, "track platform-available lib",
		flatlist, "yourlib(minSdkVersion:29)")
}

func TestTrackCustomAllowedDepsWithDefaultTxt(t *testing.T) {
	ctx := testApex(t, `
		apex {
			name: "myapex",
			key: "myapex.key",
			updatable: true,
			native_shared_libs: [
				"mylib",
				"yourlib",
			],
			min_sdk_version: "29",
		}

		apex {
			name: "myapex2",
			key: "myapex.key",
			updatable: false,
			native_shared_libs: ["yourlib"],
		}

		apex_key {
			name: "myapex.key",
			public_key: "testkey.avbpubkey",
			private_key: "testkey.pem",
		}

		cc_library {
			name: "mylib",
			srcs: ["mylib.cpp"],
			shared_libs: ["libbar"],
			min_sdk_version: "29",
			apex_available: ["myapex"],
		}

		cc_library {
			name: "libbar",
			stubs: { versions: ["29", "30"] },
		}

		cc_library {
			name: "yourlib",
			srcs: ["mylib.cpp"],
			min_sdk_version: "29",
			apex_available: ["myapex", "myapex2", "//apex_available:platform"],
		}
	`, withFiles(android.MockFS{
		"packages/modules/common/build/custom_allowed_deps.txt": nil,
		"packages/modules/common/build/allowed_deps.txt":        nil,
	}),
		android.FixtureModifyProductVariables(
			func(variables android.FixtureProductVariables) {
				variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
			},
		))

	depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
	inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
	android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
		"out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
	android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
		"out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")

	myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
	flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
		myapex.Output("depsinfo/flatlist.txt")), "\n")
	android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
		flatlist, "libbar(minSdkVersion:(no version)) (external)")
	android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
		flatlist, "mylib:(minSdkVersion:29)")
	android.AssertStringListContains(t, "track platform-available lib",
		flatlist, "yourlib(minSdkVersion:29)")
}

func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
	ctx := testApex(t, `
		apex {