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