From f88d8e032f494cc0d649f38fa8574103700dc59c Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Thu, 7 May 2020 20:21:34 +0100 Subject: [PATCH] Syntax check generated Android.bp snapshot Checks the syntax of the Android.bp file generated for the sdk snapshot. While this will not detect all possible problems with the generated Android.bp file it will detect some and it is far better to detect those issues during generation than when it is unpacked and used. Bug: 155628860 Test: m nothing Change-Id: Ieec86a7a49fa2e3bd8b9f83aca540114232a3476 --- android/module.go | 11 +++++++++++ sdk/update.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/android/module.go b/android/module.go index e431adaf4..e7959e82f 100644 --- a/android/module.go +++ b/android/module.go @@ -104,6 +104,8 @@ type EarlyModuleContext interface { type BaseModuleContext interface { EarlyModuleContext + blueprintBaseModuleContext() blueprint.BaseModuleContext + OtherModuleName(m blueprint.Module) string OtherModuleDir(m blueprint.Module) string OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) @@ -1442,6 +1444,10 @@ func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.Depen return b.bp.GetDirectDepWithTag(name, tag) } +func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext { + return b.bp +} + type moduleContext struct { bp blueprint.ModuleContext baseModuleContext @@ -2364,3 +2370,8 @@ type IdeInfo struct { Installed_paths []string `json:"installed,omitempty"` SrcJars []string `json:"srcjars,omitempty"` } + +func CheckBlueprintSyntax(ctx BaseModuleContext, filename string, contents string) []error { + bpctx := ctx.blueprintBaseModuleContext() + return blueprint.CheckBlueprintSyntax(bpctx.ModuleFactories(), filename, contents) +} diff --git a/sdk/update.go b/sdk/update.go index 476a4a5ea..1ba58064d 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -350,6 +350,9 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) andro bp = newGeneratedFile(ctx, "snapshot", "Android.bp") generateBpContents(&bp.generatedContents, bpFile) + contents := bp.content.String() + syntaxCheckSnapshotBpFile(ctx, contents) + bp.build(pctx, ctx, nil) filesToZip := builder.filesToZip @@ -394,6 +397,36 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) andro return outputZipFile } +// Check the syntax of the generated Android.bp file contents and if they are +// invalid then log an error with the contents (tagged with line numbers) and the +// errors that were found so that it is easy to see where the problem lies. +func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) { + errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents) + if len(errs) != 0 { + message := &strings.Builder{} + _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot: + +Generated Android.bp contents +======================================================================== +`) + for i, line := range strings.Split(contents, "\n") { + _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line) + } + + _, _ = fmt.Fprint(message, ` +======================================================================== + +Errors found: +`) + + for _, err := range errs { + _, _ = fmt.Fprintf(message, "%s\n", err.Error()) + } + + ctx.ModuleErrorf("%s", message.String()) + } +} + func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) { err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice) if err != nil {