1// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18	"fmt"
19	"os"
20	"path/filepath"
21	"strings"
22
23	"github.com/google/blueprint"
24)
25
26func init() {
27	RegisterSingletonType("writedocs", DocsSingleton)
28}
29
30func DocsSingleton() Singleton {
31	return &docsSingleton{}
32}
33
34type docsSingleton struct{}
35
36func primaryBuilderPath(ctx SingletonContext) Path {
37	primaryBuilder, err := filepath.Rel(ctx.Config().BuildDir(), os.Args[0])
38	if err != nil {
39		ctx.Errorf("path to primary builder %q is not in build dir %q",
40			os.Args[0], ctx.Config().BuildDir())
41	}
42
43	return PathForOutput(ctx, primaryBuilder)
44}
45
46func (c *docsSingleton) GenerateBuildActions(ctx SingletonContext) {
47	var deps Paths
48	deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().moduleListFile))
49	deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().ProductVariablesFileName))
50
51	// Generate build system docs for the primary builder.  Generating docs reads the source
52	// files used to build the primary builder, but that dependency will be picked up through
53	// the dependency on the primary builder itself.  There are no dependencies on the
54	// Blueprints files, as any relevant changes to the Blueprints files would have caused
55	// a rebuild of the primary builder.
56	docsFile := PathForOutput(ctx, "docs", "soong_build.html")
57	primaryBuilder := primaryBuilderPath(ctx)
58	soongDocs := ctx.Rule(pctx, "soongDocs",
59		blueprint.RuleParams{
60			Command: fmt.Sprintf("rm -f ${outDir}/* && %s --soong_docs %s %s",
61				primaryBuilder.String(), docsFile.String(), strings.Join(os.Args[1:], " ")),
62			CommandDeps: []string{primaryBuilder.String()},
63			Description: fmt.Sprintf("%s docs $out", primaryBuilder.Base()),
64		},
65		"outDir")
66
67	ctx.Build(pctx, BuildParams{
68		Rule:   soongDocs,
69		Output: docsFile,
70		Inputs: deps,
71		Args: map[string]string{
72			"outDir": PathForOutput(ctx, "docs").String(),
73		},
74	})
75
76	// Add a phony target for building the documentation
77	ctx.Phony("soong_docs", docsFile)
78}
79