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