1// Copyright 2019 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	"path/filepath"
19
20	"github.com/google/blueprint"
21)
22
23func init() {
24	pctx.SourcePathVariable("merge_notices", "build/soong/scripts/mergenotice.py")
25	pctx.SourcePathVariable("generate_notice", "build/soong/scripts/generate-notice-files.py")
26
27	pctx.HostBinToolVariable("minigzip", "minigzip")
28}
29
30type NoticeOutputs struct {
31	Merged       OptionalPath
32	TxtOutput    OptionalPath
33	HtmlOutput   OptionalPath
34	HtmlGzOutput OptionalPath
35}
36
37var (
38	mergeNoticesRule = pctx.AndroidStaticRule("mergeNoticesRule", blueprint.RuleParams{
39		Command:     `${merge_notices} --output $out $in`,
40		CommandDeps: []string{"${merge_notices}"},
41		Description: "merge notice files into $out",
42	})
43
44	generateNoticeRule = pctx.AndroidStaticRule("generateNoticeRule", blueprint.RuleParams{
45		Command: `rm -rf $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` +
46			`mkdir -p $$(dirname $txtOut) $$(dirname $htmlOut)  $$(dirname $out) && ` +
47			`${generate_notice} --text-output $txtOut --html-output $htmlOut -t "$title" -s $inputDir && ` +
48			`${minigzip} -c $htmlOut > $out`,
49		CommandDeps: []string{"${generate_notice}", "${minigzip}"},
50		Description: "produce notice file $out",
51	}, "txtOut", "htmlOut", "title", "inputDir")
52)
53
54func MergeNotices(ctx ModuleContext, mergedNotice WritablePath, noticePaths []Path) {
55	ctx.Build(pctx, BuildParams{
56		Rule:        mergeNoticesRule,
57		Description: "merge notices",
58		Inputs:      noticePaths,
59		Output:      mergedNotice,
60	})
61}
62
63func BuildNoticeOutput(ctx ModuleContext, installPath InstallPath, installFilename string,
64	noticePaths []Path) NoticeOutputs {
65	// Merge all NOTICE files into one.
66	// TODO(jungjw): We should just produce a well-formatted NOTICE.html file in a single pass.
67	//
68	// generate-notice-files.py, which processes the merged NOTICE file, has somewhat strict rules
69	// about input NOTICE file paths.
70	// 1. Their relative paths to the src root become their NOTICE index titles. We want to use
71	// on-device paths as titles, and so output the merged NOTICE file the corresponding location.
72	// 2. They must end with .txt extension. Otherwise, they're ignored.
73	noticeRelPath := InstallPathToOnDevicePath(ctx, installPath.Join(ctx, installFilename+".txt"))
74	mergedNotice := PathForModuleOut(ctx, filepath.Join("NOTICE_FILES/src", noticeRelPath))
75	MergeNotices(ctx, mergedNotice, noticePaths)
76
77	// Transform the merged NOTICE file into a gzipped HTML file.
78	txtOuptut := PathForModuleOut(ctx, "NOTICE_txt", "NOTICE.txt")
79	htmlOutput := PathForModuleOut(ctx, "NOTICE_html", "NOTICE.html")
80	htmlGzOutput := PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
81	title := "Notices for " + ctx.ModuleName()
82	ctx.Build(pctx, BuildParams{
83		Rule:            generateNoticeRule,
84		Description:     "generate notice output",
85		Input:           mergedNotice,
86		Output:          htmlGzOutput,
87		ImplicitOutputs: WritablePaths{txtOuptut, htmlOutput},
88		Args: map[string]string{
89			"txtOut":   txtOuptut.String(),
90			"htmlOut":  htmlOutput.String(),
91			"title":    title,
92			"inputDir": PathForModuleOut(ctx, "NOTICE_FILES/src").String(),
93		},
94	})
95
96	return NoticeOutputs{
97		Merged:       OptionalPathForPath(mergedNotice),
98		TxtOutput:    OptionalPathForPath(txtOuptut),
99		HtmlOutput:   OptionalPathForPath(htmlOutput),
100		HtmlGzOutput: OptionalPathForPath(htmlGzOutput),
101	}
102}
103