blob: 194a734d354e67d589008d05dda4e4e198fd80fc [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"
Colin Crossaa1cab02022-01-28 14:49:24 -080019 "strings"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070020
21 "github.com/google/blueprint"
22)
23
24func init() {
25 pctx.SourcePathVariable("merge_notices", "build/soong/scripts/mergenotice.py")
Bob Badour3911e6a2020-02-10 17:08:47 -080026 pctx.SourcePathVariable("generate_notice", "build/soong/scripts/generate-notice-files.py")
Jaewoong Jung5b425e22019-06-17 17:40:56 -070027
28 pctx.HostBinToolVariable("minigzip", "minigzip")
29}
30
Jaewoong Jung98772792019-07-01 17:15:13 -070031type NoticeOutputs struct {
32 Merged OptionalPath
33 TxtOutput OptionalPath
34 HtmlOutput OptionalPath
35 HtmlGzOutput OptionalPath
36}
37
Jaewoong Jung5b425e22019-06-17 17:40:56 -070038var (
39 mergeNoticesRule = pctx.AndroidStaticRule("mergeNoticesRule", blueprint.RuleParams{
40 Command: `${merge_notices} --output $out $in`,
41 CommandDeps: []string{"${merge_notices}"},
42 Description: "merge notice files into $out",
43 })
44
45 generateNoticeRule = pctx.AndroidStaticRule("generateNoticeRule", blueprint.RuleParams{
Jaewoong Jung825c8142019-07-15 10:57:26 -070046 Command: `rm -rf $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` +
47 `mkdir -p $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` +
Jaewoong Jung98772792019-07-01 17:15:13 -070048 `${generate_notice} --text-output $txtOut --html-output $htmlOut -t "$title" -s $inputDir && ` +
49 `${minigzip} -c $htmlOut > $out`,
Jaewoong Jung5b425e22019-06-17 17:40:56 -070050 CommandDeps: []string{"${generate_notice}", "${minigzip}"},
51 Description: "produce notice file $out",
Jaewoong Jung98772792019-07-01 17:15:13 -070052 }, "txtOut", "htmlOut", "title", "inputDir")
Jaewoong Jung5b425e22019-06-17 17:40:56 -070053)
54
55func MergeNotices(ctx ModuleContext, mergedNotice WritablePath, noticePaths []Path) {
56 ctx.Build(pctx, BuildParams{
57 Rule: mergeNoticesRule,
58 Description: "merge notices",
59 Inputs: noticePaths,
60 Output: mergedNotice,
61 })
62}
63
Colin Cross70dda7e2019-10-01 22:05:35 -070064func BuildNoticeOutput(ctx ModuleContext, installPath InstallPath, installFilename string,
Jaewoong Jung98772792019-07-01 17:15:13 -070065 noticePaths []Path) NoticeOutputs {
Jaewoong Jung5b425e22019-06-17 17:40:56 -070066 // Merge all NOTICE files into one.
67 // TODO(jungjw): We should just produce a well-formatted NOTICE.html file in a single pass.
68 //
69 // generate-notice-files.py, which processes the merged NOTICE file, has somewhat strict rules
70 // about input NOTICE file paths.
71 // 1. Their relative paths to the src root become their NOTICE index titles. We want to use
72 // on-device paths as titles, and so output the merged NOTICE file the corresponding location.
73 // 2. They must end with .txt extension. Otherwise, they're ignored.
74 noticeRelPath := InstallPathToOnDevicePath(ctx, installPath.Join(ctx, installFilename+".txt"))
75 mergedNotice := PathForModuleOut(ctx, filepath.Join("NOTICE_FILES/src", noticeRelPath))
76 MergeNotices(ctx, mergedNotice, noticePaths)
77
78 // Transform the merged NOTICE file into a gzipped HTML file.
Jaewoong Jung98772792019-07-01 17:15:13 -070079 txtOuptut := PathForModuleOut(ctx, "NOTICE_txt", "NOTICE.txt")
80 htmlOutput := PathForModuleOut(ctx, "NOTICE_html", "NOTICE.html")
81 htmlGzOutput := PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
Jaewoong Jung5b425e22019-06-17 17:40:56 -070082 title := "Notices for " + ctx.ModuleName()
83 ctx.Build(pctx, BuildParams{
Jaewoong Jung98772792019-07-01 17:15:13 -070084 Rule: generateNoticeRule,
85 Description: "generate notice output",
86 Input: mergedNotice,
87 Output: htmlGzOutput,
88 ImplicitOutputs: WritablePaths{txtOuptut, htmlOutput},
Jaewoong Jung5b425e22019-06-17 17:40:56 -070089 Args: map[string]string{
Jaewoong Jung98772792019-07-01 17:15:13 -070090 "txtOut": txtOuptut.String(),
91 "htmlOut": htmlOutput.String(),
Jaewoong Jung5b425e22019-06-17 17:40:56 -070092 "title": title,
93 "inputDir": PathForModuleOut(ctx, "NOTICE_FILES/src").String(),
94 },
95 })
96
Jaewoong Jung98772792019-07-01 17:15:13 -070097 return NoticeOutputs{
98 Merged: OptionalPathForPath(mergedNotice),
99 TxtOutput: OptionalPathForPath(txtOuptut),
100 HtmlOutput: OptionalPathForPath(htmlOutput),
101 HtmlGzOutput: OptionalPathForPath(htmlGzOutput),
102 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700103}
Dan Willemsen9fe14102021-07-13 21:52:04 -0700104
Colin Crossaa1cab02022-01-28 14:49:24 -0800105// BuildNoticeTextOutputFromLicenseMetadata writes out a notice text file based on the module's
106// generated license metadata file.
107func BuildNoticeTextOutputFromLicenseMetadata(ctx ModuleContext, outputFile WritablePath) {
108 depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", "."))
109 rule := NewRuleBuilder(pctx, ctx)
110 rule.Command().
111 BuiltTool("textnotice").
112 FlagWithOutput("-o ", outputFile).
113 FlagWithDepFile("-d ", depsFile).
114 Input(ctx.Module().base().licenseMetadataFile)
115 rule.Build("container_notice", "container notice file")
Dan Willemsen9fe14102021-07-13 21:52:04 -0700116}