1package bootstrap
2
3import (
4	"fmt"
5	"path/filepath"
6
7	"github.com/google/blueprint"
8	"github.com/google/blueprint/bootstrap/bpdoc"
9	"github.com/google/blueprint/pathtools"
10)
11
12func writeDocs(ctx *blueprint.Context, srcDir, filename string) error {
13	// Find the module that's marked as the "primary builder", which means it's
14	// creating the binary that we'll use to generate the non-bootstrap
15	// build.ninja file.
16	var primaryBuilders []*goBinary
17	var minibp *goBinary
18	ctx.VisitAllModulesIf(isBootstrapBinaryModule,
19		func(module blueprint.Module) {
20			binaryModule := module.(*goBinary)
21			if binaryModule.properties.PrimaryBuilder {
22				primaryBuilders = append(primaryBuilders, binaryModule)
23			}
24			if ctx.ModuleName(binaryModule) == "minibp" {
25				minibp = binaryModule
26			}
27		})
28
29	if minibp == nil {
30		panic("missing minibp")
31	}
32
33	var primaryBuilder *goBinary
34	switch len(primaryBuilders) {
35	case 0:
36		// If there's no primary builder module then that means we'll use minibp
37		// as the primary builder.
38		primaryBuilder = minibp
39
40	case 1:
41		primaryBuilder = primaryBuilders[0]
42
43	default:
44		return fmt.Errorf("multiple primary builder modules present")
45	}
46
47	pkgFiles := make(map[string][]string)
48	ctx.VisitDepsDepthFirst(primaryBuilder, func(module blueprint.Module) {
49		switch m := module.(type) {
50		case (*goPackage):
51			pkgFiles[m.properties.PkgPath] = pathtools.PrefixPaths(m.properties.Srcs,
52				filepath.Join(srcDir, ctx.ModuleDir(m)))
53		default:
54			panic(fmt.Errorf("unknown dependency type %T", module))
55		}
56	})
57
58	return bpdoc.Write(filename, pkgFiles, ctx.ModuleTypePropertyStructs())
59}
60