From 6f6828235f16ab4165b78cb62caa64be663ddcb1 Mon Sep 17 00:00:00 2001 From: "Lukacs T. Berki" Date: Thu, 1 Apr 2021 18:27:31 +0200 Subject: [PATCH] Add dumping the module graph in JSON format. Test: Presubmits + eyeballing the output. Change-Id: I26688957905f82a5437d90a7a51f7598a321a6e0 --- context.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/context.go b/context.go index 01fa89f..c900e28 100644 --- a/context.go +++ b/context.go @@ -17,6 +17,7 @@ package blueprint import ( "bytes" "context" + "encoding/json" "errors" "fmt" "io" @@ -2254,6 +2255,64 @@ func (c *Context) updateDependencies() (errs []error) { return } +type jsonVariationMap map[string]string + +type jsonModuleName struct { + Name string + Variations jsonVariationMap + DependencyVariations jsonVariationMap +} + +type jsonDep struct { + jsonModuleName + Tag string +} + +type jsonModule struct { + jsonModuleName + Deps []jsonDep + Type string + Blueprint string +} + +func toJsonVariationMap(vm variationMap) jsonVariationMap { + return jsonVariationMap(vm) +} + +func jsonModuleNameFromModuleInfo(m *moduleInfo) *jsonModuleName { + return &jsonModuleName{ + Name: m.Name(), + Variations: toJsonVariationMap(m.variant.variations), + DependencyVariations: toJsonVariationMap(m.variant.dependencyVariations), + } +} + +func jsonModuleFromModuleInfo(m *moduleInfo) *jsonModule { + return &jsonModule{ + jsonModuleName: *jsonModuleNameFromModuleInfo(m), + Deps: make([]jsonDep, 0), + Type: m.typeName, + Blueprint: m.relBlueprintsFile, + } +} + +func (c *Context) PrintJSONGraph(w io.Writer) { + modules := make([]*jsonModule, 0) + for _, m := range c.modulesSorted { + jm := jsonModuleFromModuleInfo(m) + for _, d := range m.directDeps { + jm.Deps = append(jm.Deps, jsonDep{ + jsonModuleName: *jsonModuleNameFromModuleInfo(d.module), + Tag: fmt.Sprintf("%T %+v", d.tag, d.tag), + }) + } + + modules = append(modules, jm) + } + + json.NewEncoder(w).Encode(modules) +} + // PrepareBuildActions generates an internal representation of all the build // actions that need to be performed. This process involves invoking the // GenerateBuildActions method on each of the Module objects created during the @@ -2272,6 +2331,7 @@ func (c *Context) updateDependencies() (errs []error) { // by the modules and singletons via the ModuleContext.AddNinjaFileDeps(), // SingletonContext.AddNinjaFileDeps(), and PackageContext.AddNinjaFileDeps() // methods. + func (c *Context) PrepareBuildActions(config interface{}) (deps []string, errs []error) { pprof.Do(c.Context, pprof.Labels("blueprint", "PrepareBuildActions"), func(ctx context.Context) { c.buildActionsReady = false