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 (
Wei Sheng Shih77807b32022-04-01 02:05:36 +000018 "path/filepath"
Colin Crossaa1cab02022-01-28 14:49:24 -080019 "strings"
Wei Sheng Shih77807b32022-04-01 02:05:36 +000020
21 "github.com/google/blueprint"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070022)
23
Wei Sheng Shih77807b32022-04-01 02:05:36 +000024func init() {
25 pctx.SourcePathVariable("merge_notices", "build/soong/scripts/mergenotice.py")
26 pctx.SourcePathVariable("generate_notice", "build/soong/scripts/generate-notice-files.py")
27
28 pctx.HostBinToolVariable("minigzip", "minigzip")
29}
30
31type NoticeOutputs struct {
32 Merged OptionalPath
33 TxtOutput OptionalPath
34 HtmlOutput OptionalPath
35 HtmlGzOutput OptionalPath
36}
37
38var (
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{
46 Command: `rm -rf $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` +
47 `mkdir -p $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` +
48 `${generate_notice} --text-output $txtOut --html-output $htmlOut -t "$title" -s $inputDir && ` +
49 `${minigzip} -c $htmlOut > $out`,
50 CommandDeps: []string{"${generate_notice}", "${minigzip}"},
51 Description: "produce notice file $out",
52 }, "txtOut", "htmlOut", "title", "inputDir")
53)
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
64func BuildNoticeOutput(ctx ModuleContext, installPath InstallPath, installFilename string,
65 noticePaths []Path) NoticeOutputs {
66 // 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.
79 txtOuptut := PathForModuleOut(ctx, "NOTICE_txt", "NOTICE.txt")
80 htmlOutput := PathForModuleOut(ctx, "NOTICE_html", "NOTICE.html")
81 htmlGzOutput := PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
82 title := "Notices for " + ctx.ModuleName()
83 ctx.Build(pctx, BuildParams{
84 Rule: generateNoticeRule,
85 Description: "generate notice output",
86 Input: mergedNotice,
87 Output: htmlGzOutput,
88 ImplicitOutputs: WritablePaths{txtOuptut, htmlOutput},
89 Args: map[string]string{
90 "txtOut": txtOuptut.String(),
91 "htmlOut": htmlOutput.String(),
92 "title": title,
93 "inputDir": PathForModuleOut(ctx, "NOTICE_FILES/src").String(),
94 },
95 })
96
97 return NoticeOutputs{
98 Merged: OptionalPathForPath(mergedNotice),
99 TxtOutput: OptionalPathForPath(txtOuptut),
100 HtmlOutput: OptionalPathForPath(htmlOutput),
101 HtmlGzOutput: OptionalPathForPath(htmlGzOutput),
102 }
103}
104
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)
Wei Sheng Shih77807b32022-04-01 02:05:36 +0000115 rule.Build("container_notice", "container notice file")
Dan Willemsen9fe14102021-07-13 21:52:04 -0700116}