blob: 850359328281b037892eb5baa8a03fc8c2ea5a96 [file] [log] [blame]
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001// 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
Jaewoong Jung98772792019-07-01 17:15:13 -070030type NoticeOutputs struct {
31 Merged OptionalPath
32 TxtOutput OptionalPath
33 HtmlOutput OptionalPath
34 HtmlGzOutput OptionalPath
35}
36
Jaewoong Jung5b425e22019-06-17 17:40:56 -070037var (
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{
Jaewoong Jung98772792019-07-01 17:15:13 -070045 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`,
Jaewoong Jung5b425e22019-06-17 17:40:56 -070049 CommandDeps: []string{"${generate_notice}", "${minigzip}"},
50 Description: "produce notice file $out",
Jaewoong Jung98772792019-07-01 17:15:13 -070051 }, "txtOut", "htmlOut", "title", "inputDir")
Jaewoong Jung5b425e22019-06-17 17:40:56 -070052)
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 OutputPath, installFilename string,
Jaewoong Jung98772792019-07-01 17:15:13 -070064 noticePaths []Path) NoticeOutputs {
Jaewoong Jung5b425e22019-06-17 17:40:56 -070065 // 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.
Jaewoong Jung98772792019-07-01 17:15:13 -070078 txtOuptut := PathForModuleOut(ctx, "NOTICE_txt", "NOTICE.txt")
79 htmlOutput := PathForModuleOut(ctx, "NOTICE_html", "NOTICE.html")
80 htmlGzOutput := PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
Jaewoong Jung5b425e22019-06-17 17:40:56 -070081 title := "Notices for " + ctx.ModuleName()
82 ctx.Build(pctx, BuildParams{
Jaewoong Jung98772792019-07-01 17:15:13 -070083 Rule: generateNoticeRule,
84 Description: "generate notice output",
85 Input: mergedNotice,
86 Output: htmlGzOutput,
87 ImplicitOutputs: WritablePaths{txtOuptut, htmlOutput},
Jaewoong Jung5b425e22019-06-17 17:40:56 -070088 Args: map[string]string{
Jaewoong Jung98772792019-07-01 17:15:13 -070089 "txtOut": txtOuptut.String(),
90 "htmlOut": htmlOutput.String(),
Jaewoong Jung5b425e22019-06-17 17:40:56 -070091 "title": title,
92 "inputDir": PathForModuleOut(ctx, "NOTICE_FILES/src").String(),
93 },
94 })
95
Jaewoong Jung98772792019-07-01 17:15:13 -070096 return NoticeOutputs{
97 Merged: OptionalPathForPath(mergedNotice),
98 TxtOutput: OptionalPathForPath(txtOuptut),
99 HtmlOutput: OptionalPathForPath(htmlOutput),
100 HtmlGzOutput: OptionalPathForPath(htmlGzOutput),
101 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700102}