Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 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 main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
Bob Badour | 608bdff | 2022-02-01 12:02:30 -0800 | [diff] [blame] | 19 | "compress/gzip" |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 20 | "flag" |
| 21 | "fmt" |
| 22 | "html" |
| 23 | "io" |
| 24 | "io/fs" |
| 25 | "os" |
| 26 | "path/filepath" |
Bob Badour | ab5cfbd | 2022-10-17 17:40:04 -0700 | [diff] [blame] | 27 | "sort" |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 28 | "strings" |
Colin Cross | 38a6193 | 2022-01-27 15:26:49 -0800 | [diff] [blame] | 29 | |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 30 | "android/soong/response" |
Colin Cross | 38a6193 | 2022-01-27 15:26:49 -0800 | [diff] [blame] | 31 | "android/soong/tools/compliance" |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 32 | |
| 33 | "github.com/google/blueprint/deptools" |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | var ( |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 37 | failNoneRequested = fmt.Errorf("\nNo license metadata files requested") |
| 38 | failNoLicenses = fmt.Errorf("No licenses found") |
| 39 | ) |
| 40 | |
| 41 | type context struct { |
| 42 | stdout io.Writer |
| 43 | stderr io.Writer |
| 44 | rootFS fs.FS |
| 45 | includeTOC bool |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 46 | product string |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 47 | stripPrefix []string |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 48 | title string |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 49 | deps *[]string |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 52 | func (ctx context) strip(installPath string) string { |
| 53 | for _, prefix := range ctx.stripPrefix { |
| 54 | if strings.HasPrefix(installPath, prefix) { |
| 55 | p := strings.TrimPrefix(installPath, prefix) |
| 56 | if 0 == len(p) { |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 57 | p = ctx.product |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 58 | } |
| 59 | if 0 == len(p) { |
| 60 | continue |
| 61 | } |
| 62 | return p |
| 63 | } |
| 64 | } |
| 65 | return installPath |
| 66 | } |
| 67 | |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 68 | // newMultiString creates a flag that allows multiple values in an array. |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 69 | func newMultiString(flags *flag.FlagSet, name, usage string) *multiString { |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 70 | var f multiString |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 71 | flags.Var(&f, name, usage) |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 72 | return &f |
| 73 | } |
| 74 | |
| 75 | // multiString implements the flag `Value` interface for multiple strings. |
| 76 | type multiString []string |
| 77 | |
| 78 | func (ms *multiString) String() string { return strings.Join(*ms, ", ") } |
| 79 | func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil } |
| 80 | |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 81 | func main() { |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 82 | var expandedArgs []string |
| 83 | for _, arg := range os.Args[1:] { |
| 84 | if strings.HasPrefix(arg, "@") { |
| 85 | f, err := os.Open(strings.TrimPrefix(arg, "@")) |
| 86 | if err != nil { |
| 87 | fmt.Fprintln(os.Stderr, err.Error()) |
| 88 | os.Exit(1) |
| 89 | } |
| 90 | |
| 91 | respArgs, err := response.ReadRspFile(f) |
| 92 | f.Close() |
| 93 | if err != nil { |
| 94 | fmt.Fprintln(os.Stderr, err.Error()) |
| 95 | os.Exit(1) |
| 96 | } |
| 97 | expandedArgs = append(expandedArgs, respArgs...) |
| 98 | } else { |
| 99 | expandedArgs = append(expandedArgs, arg) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | flags := flag.NewFlagSet("flags", flag.ExitOnError) |
| 104 | |
| 105 | flags.Usage = func() { |
| 106 | fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...} |
| 107 | |
| 108 | Outputs an html NOTICE.html or gzipped NOTICE.html.gz file if the -o filename |
| 109 | ends with ".gz". |
| 110 | |
| 111 | Options: |
| 112 | `, filepath.Base(os.Args[0])) |
| 113 | flags.PrintDefaults() |
| 114 | } |
| 115 | |
| 116 | outputFile := flags.String("o", "-", "Where to write the NOTICE text file. (default stdout)") |
| 117 | depsFile := flags.String("d", "", "Where to write the deps file") |
| 118 | includeTOC := flags.Bool("toc", true, "Whether to include a table of contents.") |
| 119 | product := flags.String("product", "", "The name of the product for which the notice is generated.") |
| 120 | stripPrefix := newMultiString(flags, "strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)") |
| 121 | title := flags.String("title", "", "The title of the notice file.") |
| 122 | |
| 123 | flags.Parse(expandedArgs) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 124 | |
| 125 | // Must specify at least one root target. |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 126 | if flags.NArg() == 0 { |
| 127 | flags.Usage() |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 128 | os.Exit(2) |
| 129 | } |
| 130 | |
| 131 | if len(*outputFile) == 0 { |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 132 | flags.Usage() |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 133 | fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n") |
| 134 | os.Exit(2) |
| 135 | } else { |
| 136 | dir, err := filepath.Abs(filepath.Dir(*outputFile)) |
| 137 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 138 | fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 139 | os.Exit(1) |
| 140 | } |
| 141 | fi, err := os.Stat(dir) |
| 142 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 143 | fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 144 | os.Exit(1) |
| 145 | } |
| 146 | if !fi.IsDir() { |
| 147 | fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile) |
| 148 | os.Exit(1) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | var ofile io.Writer |
Bob Badour | 608bdff | 2022-02-01 12:02:30 -0800 | [diff] [blame] | 153 | var closer io.Closer |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 154 | ofile = os.Stdout |
Bob Badour | 608bdff | 2022-02-01 12:02:30 -0800 | [diff] [blame] | 155 | var obuf *bytes.Buffer |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 156 | if *outputFile != "-" { |
Bob Badour | 608bdff | 2022-02-01 12:02:30 -0800 | [diff] [blame] | 157 | obuf = &bytes.Buffer{} |
| 158 | ofile = obuf |
| 159 | } |
| 160 | if strings.HasSuffix(*outputFile, ".gz") { |
| 161 | ofile, _ = gzip.NewWriterLevel(obuf, gzip.BestCompression) |
| 162 | closer = ofile.(io.Closer) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 165 | var deps []string |
| 166 | |
Bob Badour | c778e4c | 2022-03-22 13:05:19 -0700 | [diff] [blame] | 167 | ctx := &context{ofile, os.Stderr, compliance.FS, *includeTOC, *product, *stripPrefix, *title, &deps} |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 168 | |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 169 | err := htmlNotice(ctx, flags.Args()...) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 170 | if err != nil { |
| 171 | if err == failNoneRequested { |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 172 | flags.Usage() |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 173 | } |
| 174 | fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 175 | os.Exit(1) |
| 176 | } |
Bob Badour | 608bdff | 2022-02-01 12:02:30 -0800 | [diff] [blame] | 177 | if closer != nil { |
| 178 | closer.Close() |
| 179 | } |
| 180 | |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 181 | if *outputFile != "-" { |
Bob Badour | 608bdff | 2022-02-01 12:02:30 -0800 | [diff] [blame] | 182 | err := os.WriteFile(*outputFile, obuf.Bytes(), 0666) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 183 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 184 | fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 185 | os.Exit(1) |
| 186 | } |
| 187 | } |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 188 | if *depsFile != "" { |
| 189 | err := deptools.WriteDepFile(*depsFile, *outputFile, deps) |
| 190 | if err != nil { |
| 191 | fmt.Fprintf(os.Stderr, "could not write deps to %q: %s\n", *depsFile, err) |
| 192 | os.Exit(1) |
| 193 | } |
| 194 | } |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 195 | os.Exit(0) |
| 196 | } |
| 197 | |
| 198 | // htmlNotice implements the htmlnotice utility. |
| 199 | func htmlNotice(ctx *context, files ...string) error { |
| 200 | // Must be at least one root file. |
| 201 | if len(files) < 1 { |
| 202 | return failNoneRequested |
| 203 | } |
| 204 | |
| 205 | // Read the license graph from the license metadata files (*.meta_lic). |
| 206 | licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files) |
| 207 | if err != nil { |
| 208 | return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err) |
| 209 | } |
| 210 | if licenseGraph == nil { |
| 211 | return failNoLicenses |
| 212 | } |
| 213 | |
| 214 | // rs contains all notice resolutions. |
| 215 | rs := compliance.ResolveNotices(licenseGraph) |
| 216 | |
| 217 | ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs) |
| 218 | if err != nil { |
| 219 | return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err) |
| 220 | } |
| 221 | |
| 222 | fmt.Fprintln(ctx.stdout, "<!DOCTYPE html>") |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 223 | fmt.Fprintln(ctx.stdout, "<html><head>") |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 224 | fmt.Fprintln(ctx.stdout, "<style type=\"text/css\">") |
| 225 | fmt.Fprintln(ctx.stdout, "body { padding: 2px; margin: 0; }") |
| 226 | fmt.Fprintln(ctx.stdout, "ul { list-style-type: none; margin: 0; padding: 0; }") |
| 227 | fmt.Fprintln(ctx.stdout, "li { padding-left: 1em; }") |
| 228 | fmt.Fprintln(ctx.stdout, ".file-list { margin-left: 1em; }") |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 229 | fmt.Fprintln(ctx.stdout, "</style>") |
Bob Badour | e9b38c1 | 2022-02-09 15:56:59 -0800 | [diff] [blame] | 230 | if len(ctx.title) > 0 { |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 231 | fmt.Fprintf(ctx.stdout, "<title>%s</title>\n", html.EscapeString(ctx.title)) |
Bob Badour | e9b38c1 | 2022-02-09 15:56:59 -0800 | [diff] [blame] | 232 | } else if len(ctx.product) > 0 { |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 233 | fmt.Fprintf(ctx.stdout, "<title>%s</title>\n", html.EscapeString(ctx.product)) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 234 | } |
| 235 | fmt.Fprintln(ctx.stdout, "</head>") |
| 236 | fmt.Fprintln(ctx.stdout, "<body>") |
| 237 | |
Bob Badour | e9b38c1 | 2022-02-09 15:56:59 -0800 | [diff] [blame] | 238 | if len(ctx.title) > 0 { |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 239 | fmt.Fprintf(ctx.stdout, " <h1>%s</h1>\n", html.EscapeString(ctx.title)) |
Bob Badour | e9b38c1 | 2022-02-09 15:56:59 -0800 | [diff] [blame] | 240 | } else if len(ctx.product) > 0 { |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 241 | fmt.Fprintf(ctx.stdout, " <h1>%s</h1>\n", html.EscapeString(ctx.product)) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 242 | } |
| 243 | ids := make(map[string]string) |
| 244 | if ctx.includeTOC { |
| 245 | fmt.Fprintln(ctx.stdout, " <ul class=\"toc\">") |
| 246 | i := 0 |
| 247 | for installPath := range ni.InstallPaths() { |
| 248 | id := fmt.Sprintf("id%d", i) |
| 249 | i++ |
| 250 | ids[installPath] = id |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 251 | fmt.Fprintf(ctx.stdout, " <li id=\"%s\"><strong>%s</strong>\n <ul>\n", id, html.EscapeString(ctx.strip(installPath))) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 252 | for _, h := range ni.InstallHashes(installPath) { |
| 253 | libs := ni.InstallHashLibs(installPath, h) |
| 254 | fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", h.String(), html.EscapeString(strings.Join(libs, ", "))) |
| 255 | } |
| 256 | fmt.Fprintln(ctx.stdout, " </ul>") |
| 257 | } |
| 258 | fmt.Fprintln(ctx.stdout, " </ul><!-- toc -->") |
| 259 | } |
| 260 | for h := range ni.Hashes() { |
| 261 | fmt.Fprintln(ctx.stdout, " <hr>") |
| 262 | for _, libName := range ni.HashLibs(h) { |
| 263 | fmt.Fprintf(ctx.stdout, " <strong>%s</strong> used by:\n <ul class=\"file-list\">\n", html.EscapeString(libName)) |
| 264 | for _, installPath := range ni.HashLibInstalls(h, libName) { |
| 265 | if id, ok := ids[installPath]; ok { |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 266 | fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(ctx.strip(installPath))) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 267 | } else { |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 268 | fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(ctx.strip(installPath))) |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | fmt.Fprintf(ctx.stdout, " </ul>\n") |
| 272 | } |
| 273 | fmt.Fprintf(ctx.stdout, " </ul>\n <a id=\"%s\"/><pre class=\"license-text\">", h.String()) |
| 274 | fmt.Fprintln(ctx.stdout, html.EscapeString(string(ni.HashText(h)))) |
| 275 | fmt.Fprintln(ctx.stdout, " </pre><!-- license-text -->") |
| 276 | } |
| 277 | fmt.Fprintln(ctx.stdout, "</body></html>") |
| 278 | |
Bob Badour | ab5cfbd | 2022-10-17 17:40:04 -0700 | [diff] [blame] | 279 | *ctx.deps = ni.InputFiles() |
| 280 | sort.Strings(*ctx.deps) |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 281 | |
Bob Badour | 6ea1457 | 2022-01-23 17:15:46 -0800 | [diff] [blame] | 282 | return nil |
| 283 | } |