blob: 67b9aa3ad1af4da0b296de112188e6a80798bb7e [file] [log] [blame]
Colin Crosse87040b2017-12-11 15:52:26 -08001// 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 {
Lukacs T. Berki7690c092021-02-26 14:27:36 +010037 buildDir := absolutePath(ctx.Config().BuildDir())
Lukacs T. Berki81a69832021-03-10 18:11:57 +010038 binary := absolutePath(os.Args[0])
39 primaryBuilder, err := filepath.Rel(buildDir, binary)
Colin Crosse87040b2017-12-11 15:52:26 -080040 if err != nil {
Lukacs T. Berki81a69832021-03-10 18:11:57 +010041 ctx.Errorf("path to primary builder %q is not in build dir %q (%q)",
42 os.Args[0], ctx.Config().BuildDir(), err)
Colin Crosse87040b2017-12-11 15:52:26 -080043 }
44
45 return PathForOutput(ctx, primaryBuilder)
46}
47
48func (c *docsSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsons8f232a22020-06-23 17:37:05 -040049 var deps Paths
50 deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().moduleListFile))
51 deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().ProductVariablesFileName))
52
Jingwen Chenebb0b572020-11-02 00:24:57 -050053 // The dexpreopt configuration may not exist, but if it does, it's a dependency
54 // of soong_build.
55 dexpreoptConfigPath := ctx.Config().DexpreoptGlobalConfigPath(ctx)
56 if dexpreoptConfigPath.Valid() {
57 deps = append(deps, dexpreoptConfigPath.Path())
58 }
59
Colin Crosse87040b2017-12-11 15:52:26 -080060 // Generate build system docs for the primary builder. Generating docs reads the source
61 // files used to build the primary builder, but that dependency will be picked up through
62 // the dependency on the primary builder itself. There are no dependencies on the
63 // Blueprints files, as any relevant changes to the Blueprints files would have caused
64 // a rebuild of the primary builder.
65 docsFile := PathForOutput(ctx, "docs", "soong_build.html")
66 primaryBuilder := primaryBuilderPath(ctx)
67 soongDocs := ctx.Rule(pctx, "soongDocs",
68 blueprint.RuleParams{
Jaewoong Jung6c296882019-02-20 07:12:30 -080069 Command: fmt.Sprintf("rm -f ${outDir}/* && %s --soong_docs %s %s",
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +010070 primaryBuilder.String(),
71 docsFile.String(),
72 "\""+strings.Join(os.Args[1:], "\" \"")+"\""),
Colin Crosse87040b2017-12-11 15:52:26 -080073 CommandDeps: []string{primaryBuilder.String()},
74 Description: fmt.Sprintf("%s docs $out", primaryBuilder.Base()),
Jaewoong Jung6c296882019-02-20 07:12:30 -080075 },
76 "outDir")
Colin Crosse87040b2017-12-11 15:52:26 -080077
78 ctx.Build(pctx, BuildParams{
79 Rule: soongDocs,
80 Output: docsFile,
Chris Parsons8f232a22020-06-23 17:37:05 -040081 Inputs: deps,
Jaewoong Jung6c296882019-02-20 07:12:30 -080082 Args: map[string]string{
83 "outDir": PathForOutput(ctx, "docs").String(),
84 },
Colin Crosse87040b2017-12-11 15:52:26 -080085 })
86
87 // Add a phony target for building the documentation
Colin Crossc3d87d32020-06-04 13:25:17 -070088 ctx.Phony("soong_docs", docsFile)
Colin Crosse87040b2017-12-11 15:52:26 -080089}