Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | pctx.SourcePathVariable("merge_notices", "build/soong/scripts/mergenotice.py") |
Bob Badour | 3911e6a | 2020-02-10 17:08:47 -0800 | [diff] [blame^] | 25 | pctx.SourcePathVariable("generate_notice", "build/soong/scripts/generate-notice-files.py") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 26 | |
| 27 | pctx.HostBinToolVariable("minigzip", "minigzip") |
| 28 | } |
| 29 | |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 30 | type NoticeOutputs struct { |
| 31 | Merged OptionalPath |
| 32 | TxtOutput OptionalPath |
| 33 | HtmlOutput OptionalPath |
| 34 | HtmlGzOutput OptionalPath |
| 35 | } |
| 36 | |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 37 | var ( |
| 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 Jung | 825c814 | 2019-07-15 10:57:26 -0700 | [diff] [blame] | 45 | Command: `rm -rf $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` + |
| 46 | `mkdir -p $$(dirname $txtOut) $$(dirname $htmlOut) $$(dirname $out) && ` + |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 47 | `${generate_notice} --text-output $txtOut --html-output $htmlOut -t "$title" -s $inputDir && ` + |
| 48 | `${minigzip} -c $htmlOut > $out`, |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 49 | CommandDeps: []string{"${generate_notice}", "${minigzip}"}, |
| 50 | Description: "produce notice file $out", |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 51 | }, "txtOut", "htmlOut", "title", "inputDir") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 52 | ) |
| 53 | |
| 54 | func 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 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 63 | func BuildNoticeOutput(ctx ModuleContext, installPath InstallPath, installFilename string, |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 64 | noticePaths []Path) NoticeOutputs { |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 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. |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 78 | txtOuptut := PathForModuleOut(ctx, "NOTICE_txt", "NOTICE.txt") |
| 79 | htmlOutput := PathForModuleOut(ctx, "NOTICE_html", "NOTICE.html") |
| 80 | htmlGzOutput := PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 81 | title := "Notices for " + ctx.ModuleName() |
| 82 | ctx.Build(pctx, BuildParams{ |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 83 | Rule: generateNoticeRule, |
| 84 | Description: "generate notice output", |
| 85 | Input: mergedNotice, |
| 86 | Output: htmlGzOutput, |
| 87 | ImplicitOutputs: WritablePaths{txtOuptut, htmlOutput}, |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 88 | Args: map[string]string{ |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 89 | "txtOut": txtOuptut.String(), |
| 90 | "htmlOut": htmlOutput.String(), |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 91 | "title": title, |
| 92 | "inputDir": PathForModuleOut(ctx, "NOTICE_FILES/src").String(), |
| 93 | }, |
| 94 | }) |
| 95 | |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 96 | return NoticeOutputs{ |
| 97 | Merged: OptionalPathForPath(mergedNotice), |
| 98 | TxtOutput: OptionalPathForPath(txtOuptut), |
| 99 | HtmlOutput: OptionalPathForPath(htmlOutput), |
| 100 | HtmlGzOutput: OptionalPathForPath(htmlGzOutput), |
| 101 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 102 | } |