Merge changes from topic "android_stubs_current_refactor"

* changes:
  Cleanup android.JavaApiLibraryName references
  Redefine core and core platform API surface java_library
  Introduce product variable Build_from_text_stub
  Introduce "Exclude_static_libs" property for Java modules
This commit is contained in:
Jihoon Kang
2023-06-06 19:16:25 +00:00
committed by Gerrit Code Review
10 changed files with 195 additions and 57 deletions

View File

@@ -517,6 +517,8 @@ func NewConfig(cmdArgs CmdArgs, availableEnv map[string]string) (Config, error)
config: config,
}
config.productVariables.Build_from_text_stub = boolPtr(config.buildFromTextStub)
// Soundness check of the build and source directories. This won't catch strange
// configurations with symlinks, but at least checks the obvious case.
absBuildDir, err := filepath.Abs(cmdArgs.SoongOutDir)
@@ -2017,6 +2019,7 @@ func (c *config) BuildFromTextStub() bool {
func (c *config) SetBuildFromTextStub(b bool) {
c.buildFromTextStub = b
c.productVariables.Build_from_text_stub = boolPtr(b)
}
func (c *config) AddForceEnabledModules(forceEnabled []string) {

View File

@@ -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 {

View File

@@ -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(
@@ -433,6 +450,7 @@ type mockJavaLibraryProperties struct {
Libs []string
Sdk_version *string
Uncompress_dex *bool
Exclude_static_libs []string
}
type mockJavaLibraryModule struct {

View File

@@ -84,12 +84,6 @@ func (k SdkKind) String() string {
}
}
// JavaLibraryName returns the soong module containing the Java APIs of that API surface.
func (k SdkKind) JavaLibraryName(c Config) string {
name := k.DefaultJavaLibraryName()
return JavaApiLibraryName(c, name)
}
// JavaApiLibraryName returns the name of .txt equivalent of a java_library, but does
// not check if either module exists.
// TODO: Return .txt (single-tree or multi-tree equivalents) based on config
@@ -335,11 +329,10 @@ func init() {
// Export the name of the soong modules representing the various Java API surfaces.
func javaSdkMakeVars(ctx MakeVarsContext) {
ctx.Strict("ANDROID_PUBLIC_STUBS", SdkPublic.JavaLibraryName(ctx.Config()))
ctx.Strict("ANDROID_SYSTEM_STUBS", SdkSystem.JavaLibraryName(ctx.Config()))
ctx.Strict("ANDROID_TEST_STUBS", SdkTest.JavaLibraryName(ctx.Config()))
ctx.Strict("ANDROID_MODULE_LIB_STUBS", SdkModule.JavaLibraryName(ctx.Config()))
ctx.Strict("ANDROID_SYSTEM_SERVER_STUBS", SdkSystemServer.JavaLibraryName(ctx.Config()))
// TODO (jihoonkang): Create a .txt equivalent for core.current.stubs
ctx.Strict("ANDROID_CORE_STUBS", SdkCore.JavaLibraryName(ctx.Config()))
ctx.Strict("ANDROID_PUBLIC_STUBS", SdkPublic.DefaultJavaLibraryName())
ctx.Strict("ANDROID_SYSTEM_STUBS", SdkSystem.DefaultJavaLibraryName())
ctx.Strict("ANDROID_TEST_STUBS", SdkTest.DefaultJavaLibraryName())
ctx.Strict("ANDROID_MODULE_LIB_STUBS", SdkModule.DefaultJavaLibraryName())
ctx.Strict("ANDROID_SYSTEM_SERVER_STUBS", SdkSystemServer.DefaultJavaLibraryName())
ctx.Strict("ANDROID_CORE_STUBS", SdkCore.DefaultJavaLibraryName())
}

View File

@@ -109,6 +109,11 @@ type variableProperties struct {
Cflags []string
}
Build_from_text_stub struct {
Static_libs []string
Exclude_static_libs []string
}
// debuggable is true for eng and userdebug builds, and can be used to turn on additional
// debugging features that don't significantly impact runtime behavior. userdebug builds
// are used for dogfooding and performance testing, and should be as similar to user builds
@@ -286,6 +291,7 @@ type productVariables struct {
Uml *bool `json:",omitempty"`
Arc *bool `json:",omitempty"`
MinimizeJavaDebugInfo *bool `json:",omitempty"`
Build_from_text_stub *bool `json:",omitempty"`
Check_elf_files *bool `json:",omitempty"`
@@ -526,6 +532,7 @@ func (v *productVariables) SetDefaultConfig() {
Malloc_pattern_fill_contents: boolPtr(false),
Safestack: boolPtr(false),
TrimmedApex: boolPtr(false),
Build_from_text_stub: boolPtr(false),
BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},

View File

@@ -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.
@@ -1923,22 +1928,22 @@ type moduleWithSdkDep interface {
func (m *Module) getSdkLinkType(ctx android.BaseModuleContext, name string) (ret sdkLinkType, stubs bool) {
switch name {
case android.SdkCore.JavaLibraryName(ctx.Config()),
android.JavaApiLibraryName(ctx.Config(), "legacy.core.platform.api.stubs"),
android.JavaApiLibraryName(ctx.Config(), "stable.core.platform.api.stubs"),
case android.SdkCore.DefaultJavaLibraryName(),
"legacy.core.platform.api.stubs",
"stable.core.platform.api.stubs",
"stub-annotations", "private-stub-annotations-jar",
android.JavaApiLibraryName(ctx.Config(), "core-lambda-stubs"),
"core-lambda-stubs",
"core-generated-annotation-stubs":
return javaCore, true
case android.SdkPublic.JavaLibraryName(ctx.Config()):
case android.SdkPublic.DefaultJavaLibraryName():
return javaSdk, true
case android.SdkSystem.JavaLibraryName(ctx.Config()):
case android.SdkSystem.DefaultJavaLibraryName():
return javaSystem, true
case android.SdkModule.JavaLibraryName(ctx.Config()):
case android.SdkModule.DefaultJavaLibraryName():
return javaModule, true
case android.SdkSystemServer.JavaLibraryName(ctx.Config()):
case android.SdkSystemServer.DefaultJavaLibraryName():
return javaSystemServer, true
case android.SdkTest.JavaLibraryName(ctx.Config()):
case android.SdkTest.DefaultJavaLibraryName():
return javaSystem, true
}

View File

@@ -33,19 +33,45 @@ dist_targets = [
"win_sdk",
]
java_library {
name: "core.current.stubs",
java_defaults {
name: "core.current.stubs.defaults",
visibility: ["//visibility:public"],
sdk_version: "none",
system_modules: "none",
dist: {
targets: dist_targets,
},
}
java_library {
name: "core.current.stubs.from-source",
defaults: [
"core.current.stubs.defaults",
],
static_libs: [
"art.module.public.api.stubs",
"conscrypt.module.public.api.stubs",
"i18n.module.public.api.stubs",
],
sdk_version: "none",
system_modules: "none",
}
dist: {
targets: dist_targets,
java_library {
name: "core.current.stubs",
defaults: [
"core.current.stubs.defaults",
],
static_libs: [
"core.current.stubs.from-source",
],
product_variables: {
build_from_text_stub: {
static_libs: [
"core.current.stubs.from-text",
],
exclude_static_libs: [
"core.current.stubs.from-source",
],
},
},
}
@@ -199,18 +225,46 @@ core_platform_visibility = ["//visibility:public"]
// API annotations are available to the dex tools that enable enforcement of runtime
// accessibility. b/119068555
java_library {
name: "legacy.core.platform.api.stubs",
name: "legacy.core.platform.api.stubs.from-source",
visibility: core_platform_visibility,
hostdex: true,
compile_dex: true,
sdk_version: "none",
system_modules: "none",
defaults: [
"core.platform.api.stubs.defaults",
],
static_libs: [
"art.module.public.api.stubs.module_lib",
"conscrypt.module.platform.api.stubs",
"legacy.i18n.module.platform.api.stubs",
],
}
java_library {
name: "legacy.core.platform.api.stubs",
visibility: core_platform_visibility,
defaults: [
"core.platform.api.stubs.defaults",
],
static_libs: [
"legacy.core.platform.api.stubs.from-source",
],
product_variables: {
build_from_text_stub: {
static_libs: [
"stable.core.platform.api.stubs.from-text",
],
exclude_static_libs: [
"stable.core.platform.api.stubs.from-source",
],
},
},
}
java_defaults {
name: "core.platform.api.stubs.defaults",
hostdex: true,
compile_dex: true,
sdk_version: "none",
system_modules: "none",
patch_module: "java.base",
}
@@ -233,20 +287,38 @@ java_library {
}
java_library {
name: "stable.core.platform.api.stubs",
name: "stable.core.platform.api.stubs.from-source",
visibility: core_platform_visibility,
hostdex: true,
compile_dex: true,
sdk_version: "none",
system_modules: "none",
defaults: [
"core.platform.api.stubs.defaults",
],
static_libs: [
"art.module.public.api.stubs.module_lib",
// conscrypt only has a stable version, so it is okay to depend on it here:
"conscrypt.module.platform.api.stubs",
"stable.i18n.module.platform.api.stubs",
],
patch_module: "java.base",
}
java_library {
name: "stable.core.platform.api.stubs",
visibility: core_platform_visibility,
defaults: [
"core.platform.api.stubs.defaults",
],
static_libs: [
"stable.core.platform.api.stubs.from-source",
],
product_variables: {
build_from_text_stub: {
static_libs: [
"stable.core.platform.api.stubs.from-text",
],
exclude_static_libs: [
"stable.core.platform.api.stubs.from-source",
],
},
},
}
// Same as stable.core.platform.api.stubs, but android annotations are

View File

@@ -236,12 +236,12 @@ func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[*Hidden
testStubModules = append(testStubModules, "sdk_test_current_android")
} else {
// Use stub modules built from source
publicStubModules = append(publicStubModules, android.SdkPublic.JavaLibraryName(config))
systemStubModules = append(systemStubModules, android.SdkSystem.JavaLibraryName(config))
testStubModules = append(testStubModules, android.SdkTest.JavaLibraryName(config))
publicStubModules = append(publicStubModules, android.SdkPublic.DefaultJavaLibraryName())
systemStubModules = append(systemStubModules, android.SdkSystem.DefaultJavaLibraryName())
testStubModules = append(testStubModules, android.SdkTest.DefaultJavaLibraryName())
}
// We do not have prebuilts of the core platform api yet
corePlatformStubModules = append(corePlatformStubModules, android.JavaApiLibraryName(config, "legacy.core.platform.api.stubs"))
corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
// Allow products to define their own stubs for custom product jars that apps can use.
publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...)

View File

@@ -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`,
})
}

View File

@@ -151,7 +151,7 @@ func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext android.SdkContext)
systemModules := android.JavaApiLibraryName(ctx.Config(), fmt.Sprintf("core-%s-stubs-system-modules", systemModulesKind))
return sdkDep{
useModule: true,
bootclasspath: []string{module, android.JavaApiLibraryName(ctx.Config(), config.DefaultLambdaStubsLibrary)},
bootclasspath: []string{module, config.DefaultLambdaStubsLibrary},
systemModules: systemModules,
java9Classpath: []string{module},
frameworkResModule: "framework-res",
@@ -193,20 +193,20 @@ func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext android.SdkContext)
noFrameworksLibs: true,
}
case android.SdkPublic, android.SdkSystem, android.SdkTest:
return toModule(sdkVersion.Kind.JavaLibraryName(ctx.Config()), sdkFrameworkAidlPath(ctx))
return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), sdkFrameworkAidlPath(ctx))
case android.SdkCore:
return sdkDep{
useModule: true,
bootclasspath: []string{android.SdkCore.JavaLibraryName(ctx.Config()), android.JavaApiLibraryName(ctx.Config(), config.DefaultLambdaStubsLibrary)},
bootclasspath: []string{android.SdkCore.DefaultJavaLibraryName(), config.DefaultLambdaStubsLibrary},
systemModules: android.JavaApiLibraryName(ctx.Config(), "core-public-stubs-system-modules"),
noFrameworksLibs: true,
}
case android.SdkModule:
// TODO(146757305): provide .apk and .aidl that have more APIs for modules
return toModule(sdkVersion.Kind.JavaLibraryName(ctx.Config()), nonUpdatableFrameworkAidlPath(ctx))
return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), nonUpdatableFrameworkAidlPath(ctx))
case android.SdkSystemServer:
// TODO(146757305): provide .apk and .aidl that have more APIs for modules
return toModule(sdkVersion.Kind.JavaLibraryName(ctx.Config()), sdkFrameworkAidlPath(ctx))
return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), sdkFrameworkAidlPath(ctx))
default:
panic(fmt.Errorf("invalid sdk %q", sdkVersion.Raw))
}
@@ -269,9 +269,9 @@ func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
func createSdkFrameworkAidl(ctx android.SingletonContext) {
stubsModules := []string{
android.SdkPublic.JavaLibraryName(ctx.Config()),
android.SdkTest.JavaLibraryName(ctx.Config()),
android.SdkSystem.JavaLibraryName(ctx.Config()),
android.SdkPublic.DefaultJavaLibraryName(),
android.SdkTest.DefaultJavaLibraryName(),
android.SdkSystem.DefaultJavaLibraryName(),
}
combinedAidl := sdkFrameworkAidlPath(ctx)
@@ -286,7 +286,7 @@ func createSdkFrameworkAidl(ctx android.SingletonContext) {
// Creates a version of framework.aidl for the non-updatable part of the platform.
func createNonUpdatableFrameworkAidl(ctx android.SingletonContext) {
stubsModules := []string{android.SdkModule.JavaLibraryName(ctx.Config())}
stubsModules := []string{android.SdkModule.DefaultJavaLibraryName()}
combinedAidl := nonUpdatableFrameworkAidlPath(ctx)
tempPath := tempPathForRestat(ctx, combinedAidl)