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