| 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 ( | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 18 | "fmt" | 
|  | 19 | "path/filepath" | 
| Colin Cross | aa1cab0 | 2022-01-28 14:49:24 -0800 | [diff] [blame] | 20 | "strings" | 
| Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 21 | ) | 
|  | 22 |  | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 23 | func modulesOutputDirs(ctx BuilderContext, modules ...Module) []string { | 
|  | 24 | dirs := make([]string, 0, len(modules)) | 
|  | 25 | for _, module := range modules { | 
|  | 26 | paths, err := outputFilesForModule(ctx, module, "") | 
|  | 27 | if err != nil { | 
|  | 28 | continue | 
|  | 29 | } | 
|  | 30 | for _, path := range paths { | 
|  | 31 | if path != nil { | 
|  | 32 | dirs = append(dirs, filepath.Dir(path.String())) | 
|  | 33 | } | 
|  | 34 | } | 
|  | 35 | } | 
|  | 36 | return SortedUniqueStrings(dirs) | 
| Bob Badour | de6a087 | 2022-04-01 18:00:00 +0000 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 39 | func modulesLicenseMetadata(ctx BuilderContext, modules ...Module) Paths { | 
|  | 40 | result := make(Paths, 0, len(modules)) | 
|  | 41 | for _, module := range modules { | 
|  | 42 | if mf := module.base().licenseMetadataFile; mf != nil { | 
|  | 43 | result = append(result, mf) | 
|  | 44 | } | 
|  | 45 | } | 
|  | 46 | return result | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | // buildNoticeOutputFromLicenseMetadata writes out a notice file. | 
| Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 50 | func buildNoticeOutputFromLicenseMetadata( | 
|  | 51 | ctx BuilderContext, tool, ruleName string, outputFile WritablePath, | 
|  | 52 | libraryName string, stripPrefix []string, modules ...Module) { | 
| Bob Badour | de6a087 | 2022-04-01 18:00:00 +0000 | [diff] [blame] | 53 | depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", ".")) | 
|  | 54 | rule := NewRuleBuilder(pctx, ctx) | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 55 | if len(modules) == 0 { | 
|  | 56 | if mctx, ok := ctx.(ModuleContext); ok { | 
|  | 57 | modules = []Module{mctx.Module()} | 
|  | 58 | } else { | 
| Bob Badour | a5ea247 | 2022-05-20 16:37:26 -0700 | [diff] [blame] | 59 | panic(fmt.Errorf("%s %q needs a module to generate the notice for", ruleName, libraryName)) | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 60 | } | 
|  | 61 | } | 
|  | 62 | if libraryName == "" { | 
|  | 63 | libraryName = modules[0].Name() | 
|  | 64 | } | 
|  | 65 | cmd := rule.Command(). | 
|  | 66 | BuiltTool(tool). | 
| Bob Badour | de6a087 | 2022-04-01 18:00:00 +0000 | [diff] [blame] | 67 | FlagWithOutput("-o ", outputFile). | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 68 | FlagWithDepFile("-d ", depsFile) | 
| Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 69 | if len(stripPrefix) > 0 { | 
|  | 70 | cmd = cmd.FlagForEachArg("--strip_prefix ", stripPrefix) | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 71 | } | 
|  | 72 | outputs := modulesOutputDirs(ctx, modules...) | 
|  | 73 | if len(outputs) > 0 { | 
|  | 74 | cmd = cmd.FlagForEachArg("--strip_prefix ", outputs) | 
|  | 75 | } | 
|  | 76 | if libraryName != "" { | 
|  | 77 | cmd = cmd.FlagWithArg("--product ", libraryName) | 
|  | 78 | } | 
|  | 79 | cmd = cmd.Inputs(modulesLicenseMetadata(ctx, modules...)) | 
| Bob Badour | a5ea247 | 2022-05-20 16:37:26 -0700 | [diff] [blame] | 80 | rule.Build(ruleName, "container notice file") | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | // BuildNoticeTextOutputFromLicenseMetadata writes out a notice text file based | 
|  | 84 | // on the license metadata files for the input `modules` defaulting to the | 
|  | 85 | // current context module if none given. | 
| Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 86 | func BuildNoticeTextOutputFromLicenseMetadata( | 
|  | 87 | ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string, | 
|  | 88 | stripPrefix []string, modules ...Module) { | 
|  | 89 | buildNoticeOutputFromLicenseMetadata(ctx, "textnotice", "text_notice_"+ruleName, | 
|  | 90 | outputFile, libraryName, stripPrefix, modules...) | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | // BuildNoticeHtmlOutputFromLicenseMetadata writes out a notice text file based | 
|  | 94 | // on the license metadata files for the input `modules` defaulting to the | 
|  | 95 | // current context module if none given. | 
| Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 96 | func BuildNoticeHtmlOutputFromLicenseMetadata( | 
|  | 97 | ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string, | 
|  | 98 | stripPrefix []string, modules ...Module) { | 
|  | 99 | buildNoticeOutputFromLicenseMetadata(ctx, "htmlnotice", "html_notice_"+ruleName, | 
|  | 100 | outputFile, libraryName, stripPrefix, modules...) | 
| Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | // BuildNoticeXmlOutputFromLicenseMetadata writes out a notice text file based | 
|  | 104 | // on the license metadata files for the input `modules` defaulting to the | 
|  | 105 | // current context module if none given. | 
| Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 106 | func BuildNoticeXmlOutputFromLicenseMetadata( | 
|  | 107 | ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string, | 
|  | 108 | stripPrefix []string, modules ...Module) { | 
|  | 109 | buildNoticeOutputFromLicenseMetadata(ctx, "xmlnotice", "xml_notice_"+ruleName, | 
|  | 110 | outputFile, libraryName, stripPrefix, modules...) | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 111 | } |