blob: 6363acfc02cd60bbf723e987f783f3549c5eafbc [file] [log] [blame]
Bob Badour6ea14572022-01-23 17:15:46 -08001// 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
15package main
16
17import (
18 "bytes"
Bob Badour608bdff2022-02-01 12:02:30 -080019 "compress/gzip"
Bob Badour6ea14572022-01-23 17:15:46 -080020 "flag"
21 "fmt"
22 "html"
23 "io"
24 "io/fs"
25 "os"
26 "path/filepath"
27 "strings"
Colin Cross38a61932022-01-27 15:26:49 -080028
29 "android/soong/tools/compliance"
Colin Crossbb45f8c2022-01-28 15:18:19 -080030
31 "github.com/google/blueprint/deptools"
Bob Badour6ea14572022-01-23 17:15:46 -080032)
33
34var (
35 outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
Colin Crossbb45f8c2022-01-28 15:18:19 -080036 depsFile = flag.String("d", "", "Where to write the deps file")
Bob Badour6ea14572022-01-23 17:15:46 -080037 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
45type context struct {
46 stdout io.Writer
47 stderr io.Writer
48 rootFS fs.FS
49 includeTOC bool
50 stripPrefix string
51 title string
Colin Crossbb45f8c2022-01-28 15:18:19 -080052 deps *[]string
Bob Badour6ea14572022-01-23 17:15:46 -080053}
54
55func init() {
56 flag.Usage = func() {
57 fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
58
Bob Badour608bdff2022-02-01 12:02:30 -080059Outputs an html NOTICE.html or gzipped NOTICE.html.gz file if the -o filename
60ends with ".gz".
Bob Badour6ea14572022-01-23 17:15:46 -080061
62Options:
63`, filepath.Base(os.Args[0]))
64 flag.PrintDefaults()
65 }
66}
67
68func 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 Cross179ec3e2022-01-27 15:47:09 -080084 fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err)
Bob Badour6ea14572022-01-23 17:15:46 -080085 os.Exit(1)
86 }
87 fi, err := os.Stat(dir)
88 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -080089 fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err)
Bob Badour6ea14572022-01-23 17:15:46 -080090 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 Badour608bdff2022-02-01 12:02:30 -080099 var closer io.Closer
Bob Badour6ea14572022-01-23 17:15:46 -0800100 ofile = os.Stdout
Bob Badour608bdff2022-02-01 12:02:30 -0800101 var obuf *bytes.Buffer
Bob Badour6ea14572022-01-23 17:15:46 -0800102 if *outputFile != "-" {
Bob Badour608bdff2022-02-01 12:02:30 -0800103 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 Badour6ea14572022-01-23 17:15:46 -0800109 }
110
Colin Crossbb45f8c2022-01-28 15:18:19 -0800111 var deps []string
112
113 ctx := &context{ofile, os.Stderr, os.DirFS("."), *includeTOC, *stripPrefix, *title, &deps}
Bob Badour6ea14572022-01-23 17:15:46 -0800114
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 Badour608bdff2022-02-01 12:02:30 -0800123 if closer != nil {
124 closer.Close()
125 }
126
Bob Badour6ea14572022-01-23 17:15:46 -0800127 if *outputFile != "-" {
Bob Badour608bdff2022-02-01 12:02:30 -0800128 err := os.WriteFile(*outputFile, obuf.Bytes(), 0666)
Bob Badour6ea14572022-01-23 17:15:46 -0800129 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -0800130 fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err)
Bob Badour6ea14572022-01-23 17:15:46 -0800131 os.Exit(1)
132 }
133 }
Colin Crossbb45f8c2022-01-28 15:18:19 -0800134 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 Badour6ea14572022-01-23 17:15:46 -0800141 os.Exit(0)
142}
143
144// htmlNotice implements the htmlnotice utility.
145func 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 Cross179ec3e2022-01-27 15:47:09 -0800169 fmt.Fprintln(ctx.stdout, "<html><head>")
Bob Badour6ea14572022-01-23 17:15:46 -0800170 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 Cross179ec3e2022-01-27 15:47:09 -0800175 fmt.Fprintln(ctx.stdout, "</style>")
Bob Badour6ea14572022-01-23 17:15:46 -0800176 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 Crossbb45f8c2022-01-28 15:18:19 -0800242 *ctx.deps = ni.InputNoticeFiles()
243
Bob Badour6ea14572022-01-23 17:15:46 -0800244 return nil
245}