• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/make/tools/generate-notice-files.py")
26
27	pctx.HostBinToolVariable("minigzip", "minigzip")
28}
29
30var (
31	mergeNoticesRule = pctx.AndroidStaticRule("mergeNoticesRule", blueprint.RuleParams{
32		Command:     `${merge_notices} --output $out $in`,
33		CommandDeps: []string{"${merge_notices}"},
34		Description: "merge notice files into $out",
35	})
36
37	generateNoticeRule = pctx.AndroidStaticRule("generateNoticeRule", blueprint.RuleParams{
38		Command: `rm -rf $tmpDir $$(dirname $out) && ` +
39			`mkdir -p $tmpDir $$(dirname $out) && ` +
40			`${generate_notice} --text-output $tmpDir/NOTICE.txt --html-output $tmpDir/NOTICE.html -t "$title" -s $inputDir && ` +
41			`${minigzip} -c $tmpDir/NOTICE.html > $out`,
42		CommandDeps: []string{"${generate_notice}", "${minigzip}"},
43		Description: "produce notice file $out",
44	}, "tmpDir", "title", "inputDir")
45)
46
47func MergeNotices(ctx ModuleContext, mergedNotice WritablePath, noticePaths []Path) {
48	ctx.Build(pctx, BuildParams{
49		Rule:        mergeNoticesRule,
50		Description: "merge notices",
51		Inputs:      noticePaths,
52		Output:      mergedNotice,
53	})
54}
55
56func BuildNoticeOutput(ctx ModuleContext, installPath OutputPath, installFilename string,
57	noticePaths []Path) ModuleOutPath {
58	// Merge all NOTICE files into one.
59	// TODO(jungjw): We should just produce a well-formatted NOTICE.html file in a single pass.
60	//
61	// generate-notice-files.py, which processes the merged NOTICE file, has somewhat strict rules
62	// about input NOTICE file paths.
63	// 1. Their relative paths to the src root become their NOTICE index titles. We want to use
64	// on-device paths as titles, and so output the merged NOTICE file the corresponding location.
65	// 2. They must end with .txt extension. Otherwise, they're ignored.
66	noticeRelPath := InstallPathToOnDevicePath(ctx, installPath.Join(ctx, installFilename+".txt"))
67	mergedNotice := PathForModuleOut(ctx, filepath.Join("NOTICE_FILES/src", noticeRelPath))
68	MergeNotices(ctx, mergedNotice, noticePaths)
69
70	// Transform the merged NOTICE file into a gzipped HTML file.
71	noticeOutput := PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
72	tmpDir := PathForModuleOut(ctx, "NOTICE_tmp")
73	title := "Notices for " + ctx.ModuleName()
74	ctx.Build(pctx, BuildParams{
75		Rule:        generateNoticeRule,
76		Description: "generate notice output",
77		Input:       mergedNotice,
78		Output:      noticeOutput,
79		Args: map[string]string{
80			"tmpDir":   tmpDir.String(),
81			"title":    title,
82			"inputDir": PathForModuleOut(ctx, "NOTICE_FILES/src").String(),
83		},
84	})
85
86	return noticeOutput
87}
88