summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jihoon Kang <jihoonkang@google.com> 2023-06-01 22:17:32 +0000
committer Jihoon Kang <jihoonkang@google.com> 2023-06-05 21:42:48 +0000
commit381c2fa27c08417b587727cc26b1c033b4fa9974 (patch)
tree8aa0005b9ecfe0d1ba43a040afb097ae0b53ebea
parentf508c252bb0bba6df49ceca5dbde28e9aa48cac1 (diff)
Introduce "Exclude_static_libs" property for Java modules
Exclude_static_libs property can be used to specify static libs that should not be used to build the module. The list acts as filter for static_libs. Bug: 285410821 Test: go test ./java Change-Id: Iee7f160ba88b5f64bdd265c30d47c9f51feb0f5e
-rw-r--r--android/neverallow.go9
-rw-r--r--android/neverallow_test.go24
-rw-r--r--java/base.go5
-rw-r--r--java/java_test.go31
4 files changed, 66 insertions, 3 deletions
diff --git a/android/neverallow.go b/android/neverallow.go
index 5b5e61328..f2e8c8534 100644
--- a/android/neverallow.go
+++ b/android/neverallow.go
@@ -59,6 +59,7 @@ func init() {
AddNeverAllowRules(createProhibitFrameworkAccessRules()...)
AddNeverAllowRules(createBp2BuildRule())
AddNeverAllowRules(createCcStubsRule())
+ AddNeverAllowRules(createJavaExcludeStaticLibsRule())
}
// Add a NeverAllow rule to the set of rules to apply.
@@ -253,6 +254,14 @@ func createProhibitFrameworkAccessRules() []Rule {
}
}
+func createJavaExcludeStaticLibsRule() Rule {
+ return NeverAllow().
+ NotIn("build/soong").
+ ModuleType("java_library").
+ WithMatcher("exclude_static_libs", isSetMatcherInstance).
+ Because("exclude_static_libs property is only allowed for java modules defined in build/soong")
+}
+
func neverallowMutator(ctx BottomUpMutatorContext) {
m, ok := ctx.Module().(Module)
if !ok {
diff --git a/android/neverallow_test.go b/android/neverallow_test.go
index ddd982d15..1639bbf40 100644
--- a/android/neverallow_test.go
+++ b/android/neverallow_test.go
@@ -344,6 +344,23 @@ var neverallowTests = []struct {
`module "outside_allowed_list": violates neverallow`,
},
},
+ // Test for the rule restricting use of exclude_static_libs
+ {
+ name: `"exclude_static_libs" outside allowed directory`,
+ fs: map[string][]byte{
+ "a/b/Android.bp": []byte(`
+ java_library {
+ name: "baz",
+ exclude_static_libs: [
+ "bar",
+ ],
+ }
+ `),
+ },
+ expectedErrors: []string{
+ `exclude_static_libs property is only allowed for java modules defined in build/soong`,
+ },
+ },
}
var prepareForNeverAllowTest = GroupFixturePreparers(
@@ -430,9 +447,10 @@ func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
}
type mockJavaLibraryProperties struct {
- Libs []string
- Sdk_version *string
- Uncompress_dex *bool
+ Libs []string
+ Sdk_version *string
+ Uncompress_dex *bool
+ Exclude_static_libs []string
}
type mockJavaLibraryModule struct {
diff --git a/java/base.go b/java/base.go
index dd02ef8af..78c8d6011 100644
--- a/java/base.go
+++ b/java/base.go
@@ -79,6 +79,9 @@ type CommonProperties struct {
// list of java libraries that will be compiled into the resulting jar
Static_libs []string `android:"arch_variant"`
+ // list of java libraries that should not be used to build this module
+ Exclude_static_libs []string `android:"arch_variant"`
+
// manifest file to be included in resulting jar
Manifest *string `android:"path"`
@@ -724,6 +727,8 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) {
}
libDeps := ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
+
+ j.properties.Static_libs = android.RemoveListFromList(j.properties.Static_libs, j.properties.Exclude_static_libs)
ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
// Add dependency on libraries that provide additional hidden api annotations.
diff --git a/java/java_test.go b/java/java_test.go
index cd5c343ce..561b187d0 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -2322,3 +2322,34 @@ java_test_host {
t.Errorf("Expected args[\"extraTestRunnerConfigs\"] to equal %q, was %q", expected, args["extraTestRunnerConfigs"])
}
}
+
+func TestJavaExcludeStaticLib(t *testing.T) {
+ ctx, _ := testJava(t, `
+ java_library {
+ name: "bar",
+ }
+ java_library {
+ name: "foo",
+ }
+ java_library {
+ name: "baz",
+ static_libs: [
+ "foo",
+ "bar",
+ ],
+ exclude_static_libs: [
+ "bar",
+ ],
+ }
+ `)
+
+ // "bar" not included as dependency of "baz"
+ CheckModuleDependencies(t, ctx, "baz", "android_common", []string{
+ `core-lambda-stubs`,
+ `ext`,
+ `foo`,
+ `framework`,
+ `stable-core-platform-api-stubs-system-modules`,
+ `stable.core.platform.api.stubs`,
+ })
+}