blob: 2f59ee0ef3cc3be202041a316a26f8ed3deca162 [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 Badour6ea14572022-01-23 17:15:46 -080019 "flag"
20 "fmt"
21 "html"
22 "io"
23 "io/fs"
24 "os"
25 "path/filepath"
26 "strings"
Colin Cross38a61932022-01-27 15:26:49 -080027
28 "android/soong/tools/compliance"
Bob Badour6ea14572022-01-23 17:15:46 -080029)
30
31var (
32 outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
33 includeTOC = flag.Bool("toc", true, "Whether to include a table of contents.")
34 stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
35 title = flag.String("title", "", "The title of the notice file.")
36
37 failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
38 failNoLicenses = fmt.Errorf("No licenses found")
39)
40
41type context struct {
42 stdout io.Writer
43 stderr io.Writer
44 rootFS fs.FS
45 includeTOC bool
46 stripPrefix string
47 title string
48}
49
50func init() {
51 flag.Usage = func() {
52 fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
53
54Outputs an html NOTICE.html file.
55
56Options:
57`, filepath.Base(os.Args[0]))
58 flag.PrintDefaults()
59 }
60}
61
62func main() {
63 flag.Parse()
64
65 // Must specify at least one root target.
66 if flag.NArg() == 0 {
67 flag.Usage()
68 os.Exit(2)
69 }
70
71 if len(*outputFile) == 0 {
72 flag.Usage()
73 fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n")
74 os.Exit(2)
75 } else {
76 dir, err := filepath.Abs(filepath.Dir(*outputFile))
77 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -080078 fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err)
Bob Badour6ea14572022-01-23 17:15:46 -080079 os.Exit(1)
80 }
81 fi, err := os.Stat(dir)
82 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -080083 fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err)
Bob Badour6ea14572022-01-23 17:15:46 -080084 os.Exit(1)
85 }
86 if !fi.IsDir() {
87 fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile)
88 os.Exit(1)
89 }
90 }
91
92 var ofile io.Writer
93 ofile = os.Stdout
94 if *outputFile != "-" {
95 ofile = &bytes.Buffer{}
96 }
97
98 ctx := &context{ofile, os.Stderr, os.DirFS("."), *includeTOC, *stripPrefix, *title}
99
100 err := htmlNotice(ctx, flag.Args()...)
101 if err != nil {
102 if err == failNoneRequested {
103 flag.Usage()
104 }
105 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
106 os.Exit(1)
107 }
108 if *outputFile != "-" {
109 err := os.WriteFile(*outputFile, ofile.(*bytes.Buffer).Bytes(), 0666)
110 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -0800111 fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err)
Bob Badour6ea14572022-01-23 17:15:46 -0800112 os.Exit(1)
113 }
114 }
115 os.Exit(0)
116}
117
118// htmlNotice implements the htmlnotice utility.
119func htmlNotice(ctx *context, files ...string) error {
120 // Must be at least one root file.
121 if len(files) < 1 {
122 return failNoneRequested
123 }
124
125 // Read the license graph from the license metadata files (*.meta_lic).
126 licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files)
127 if err != nil {
128 return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
129 }
130 if licenseGraph == nil {
131 return failNoLicenses
132 }
133
134 // rs contains all notice resolutions.
135 rs := compliance.ResolveNotices(licenseGraph)
136
137 ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs)
138 if err != nil {
139 return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err)
140 }
141
142 fmt.Fprintln(ctx.stdout, "<!DOCTYPE html>")
Colin Cross179ec3e2022-01-27 15:47:09 -0800143 fmt.Fprintln(ctx.stdout, "<html><head>")
Bob Badour6ea14572022-01-23 17:15:46 -0800144 fmt.Fprintln(ctx.stdout, "<style type=\"text/css\">")
145 fmt.Fprintln(ctx.stdout, "body { padding: 2px; margin: 0; }")
146 fmt.Fprintln(ctx.stdout, "ul { list-style-type: none; margin: 0; padding: 0; }")
147 fmt.Fprintln(ctx.stdout, "li { padding-left: 1em; }")
148 fmt.Fprintln(ctx.stdout, ".file-list { margin-left: 1em; }")
Colin Cross179ec3e2022-01-27 15:47:09 -0800149 fmt.Fprintln(ctx.stdout, "</style>")
Bob Badour6ea14572022-01-23 17:15:46 -0800150 if 0 < len(ctx.title) {
151 fmt.Fprintf(ctx.stdout, "<title>%s</title>\n", html.EscapeString(ctx.title))
152 }
153 fmt.Fprintln(ctx.stdout, "</head>")
154 fmt.Fprintln(ctx.stdout, "<body>")
155
156 if 0 < len(ctx.title) {
157 fmt.Fprintf(ctx.stdout, " <h1>%s</h1>\n", html.EscapeString(ctx.title))
158 }
159 ids := make(map[string]string)
160 if ctx.includeTOC {
161 fmt.Fprintln(ctx.stdout, " <ul class=\"toc\">")
162 i := 0
163 for installPath := range ni.InstallPaths() {
164 id := fmt.Sprintf("id%d", i)
165 i++
166 ids[installPath] = id
167 var p string
168 if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
169 p = installPath[len(ctx.stripPrefix):]
170 if 0 == len(p) {
171 if 0 < len(ctx.title) {
172 p = ctx.title
173 } else {
174 p = "root"
175 }
176 }
177 } else {
178 p = installPath
179 }
180 fmt.Fprintf(ctx.stdout, " <li id=\"%s\"><strong>%s</strong>\n <ul>\n", id, html.EscapeString(p))
181 for _, h := range ni.InstallHashes(installPath) {
182 libs := ni.InstallHashLibs(installPath, h)
183 fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", h.String(), html.EscapeString(strings.Join(libs, ", ")))
184 }
185 fmt.Fprintln(ctx.stdout, " </ul>")
186 }
187 fmt.Fprintln(ctx.stdout, " </ul><!-- toc -->")
188 }
189 for h := range ni.Hashes() {
190 fmt.Fprintln(ctx.stdout, " <hr>")
191 for _, libName := range ni.HashLibs(h) {
192 fmt.Fprintf(ctx.stdout, " <strong>%s</strong> used by:\n <ul class=\"file-list\">\n", html.EscapeString(libName))
193 for _, installPath := range ni.HashLibInstalls(h, libName) {
194 if id, ok := ids[installPath]; ok {
195 if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
196 fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath[len(ctx.stripPrefix):]))
197 } else {
198 fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath))
199 }
200 } else {
201 if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
202 fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath[len(ctx.stripPrefix):]))
203 } else {
204 fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath))
205 }
206 }
207 }
208 fmt.Fprintf(ctx.stdout, " </ul>\n")
209 }
210 fmt.Fprintf(ctx.stdout, " </ul>\n <a id=\"%s\"/><pre class=\"license-text\">", h.String())
211 fmt.Fprintln(ctx.stdout, html.EscapeString(string(ni.HashText(h))))
212 fmt.Fprintln(ctx.stdout, " </pre><!-- license-text -->")
213 }
214 fmt.Fprintln(ctx.stdout, "</body></html>")
215
216 return nil
217}