summaryrefslogtreecommitdiff
path: root/java/java.go
diff options
context:
space:
mode:
author Jihoon Kang <jihoonkang@google.com> 2023-06-28 01:16:23 +0000
committer Jihoon Kang <jihoonkang@google.com> 2023-10-13 18:24:41 +0000
commit063ec003f98a1cb45ddbda607dd41563d204f85d (patch)
tree007941d946ad6f7380b3596856d195fb2357cd9d /java/java.go
parentd30ac8a32a9ff28db781db20e6dbe08d0b04b394 (diff)
Add current api check as validation for from-text stub generation
Currently, there is no build action ensuring that the API text files are up to date, unless a user runs `m checkapi` or `m update-api`. This means that the user must run `m update-api` after making a change that modifies api surface(s), so that the API text file reflects the local change. This adds additional layer of action to developers, and it is not guaranteed that the developer will always run `m update-api` after making an api surface-affecting changes. To prevent such mistake, this change adds droidstub-level api check as validation for from-text stub generation. With this change, the build will fail if the API text file is not up to date and the user must run `m update-api` when making api surface-affecting local changes. The validation is done by adding all droidstubs modules associated with the java_api_contributions passed to java_api_library via `api_contributions` as dependency and setting the current api timestamp files as the validations for the from-text stubs generating build rule. The full api surface libraries will not run the validations to avoid circular dependency. However, all java_sdk_library generated java_api_library modules will run validations, mapped to the droidstubs in the same api domaion. If the user sets the environment variable `DISABLE_STUB_VALIDATION=true`, validation actions are not run. Validation actions run by default. Test: m nothing --build-from-text-stub and run ninja query to verify `check_current_api.timestamp`s are listed as validation \ DISABLE_STUB_VALIDATION=true m nothing --build-from-text-stub and run ninja query to verify that validation actions are not added Bug: 288624417 Change-Id: I329e6438fe8f3ac30d8c6a971d57853ed6b0d150
Diffstat (limited to 'java/java.go')
-rw-r--r--java/java.go43
1 files changed, 41 insertions, 2 deletions
diff --git a/java/java.go b/java/java.go
index 35fd7c2b8..1cb79433f 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1664,6 +1664,8 @@ type ApiLibrary struct {
extractedSrcJar android.WritablePath
// .dex of stubs, used for hiddenapi processing
dexJarFile OptionalDexJarPath
+
+ validationPaths android.Paths
}
type JavaApiLibraryProperties struct {
@@ -1699,6 +1701,12 @@ type JavaApiLibraryProperties struct {
// The jar will also be passed to metalava as a classpath to
// generate compilable stubs.
System_modules *string
+
+ // If true, the module runs validation on the API signature files provided
+ // by the modules passed via api_contributions by checking if the files are
+ // in sync with the source Java files. However, the environment variable
+ // DISABLE_STUB_VALIDATION has precedence over this property.
+ Enable_validation *bool
}
func ApiLibraryFactory() android.Module {
@@ -1787,6 +1795,12 @@ func (al *ApiLibrary) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBui
}
}
+func (al *ApiLibrary) addValidation(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, validationPaths android.Paths) {
+ for _, validationPath := range validationPaths {
+ cmd.Validation(validationPath)
+ }
+}
+
// This method extracts the stub class files from the stub jar file provided
// from full_api_surface_stub module instead of compiling the srcjar generated from invoking metalava.
// This method is used because metalava can generate compilable from-text stubs only when
@@ -1823,8 +1837,28 @@ func (al *ApiLibrary) extractApiSrcs(ctx android.ModuleContext, rule *android.Ru
func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
apiContributions := al.properties.Api_contributions
+ addValidations := !ctx.Config().IsEnvTrue("DISABLE_STUB_VALIDATION") &&
+ proptools.BoolDefault(al.properties.Enable_validation, true)
for _, apiContributionName := range apiContributions {
ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName)
+
+ // Add the java_api_contribution module generating droidstubs module
+ // as dependency when validation adding conditions are met and
+ // the java_api_contribution module name has ".api.contribution" suffix.
+ // All droidstubs-generated modules possess the suffix in the name,
+ // but there is no such guarantee for tests.
+ if addValidations {
+ if strings.HasSuffix(apiContributionName, ".api.contribution") {
+ ctx.AddDependency(ctx.Module(), metalavaCurrentApiTimestampTag, strings.TrimSuffix(apiContributionName, ".api.contribution"))
+ } else {
+ ctx.ModuleErrorf("Validation is enabled for module %s but a "+
+ "current timestamp provider is not found for the api "+
+ "contribution %s",
+ ctx.ModuleName(),
+ apiContributionName,
+ )
+ }
+ }
}
ctx.AddVariationDependencies(nil, libTag, al.properties.Libs...)
ctx.AddVariationDependencies(nil, staticLibTag, al.properties.Static_libs...)
@@ -1862,8 +1896,7 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
SandboxInputs()
- var stubsDir android.OptionalPath
- stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
+ stubsDir := android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
rule.Command().Text("rm -rf").Text(stubsDir.String())
rule.Command().Text("mkdir -p").Text(stubsDir.String())
@@ -1895,6 +1928,10 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
case systemModulesTag:
module := dep.(SystemModulesProvider)
systemModulesPaths = append(systemModulesPaths, module.HeaderJars()...)
+ case metalavaCurrentApiTimestampTag:
+ if currentApiTimestampProvider, ok := dep.(currentApiTimestampProvider); ok {
+ al.validationPaths = append(al.validationPaths, currentApiTimestampProvider.CurrentApiTimestamp())
+ }
}
})
@@ -1918,6 +1955,8 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
cmd.FlagWithInput("--migrate-nullness ", previousApi)
}
+ al.addValidation(ctx, cmd, al.validationPaths)
+
al.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar")
al.stubsJarWithoutStaticLibs = android.PathForModuleOut(ctx, "metalava", "stubs.jar")
al.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName(), fmt.Sprintf("%s.jar", ctx.ModuleName()))