license metadata html notice files
Introduce the below command-line tool:
htmlnotice outputs a NOTICE.html file constructed from the license
texts of the transitive closure of dependencies.
Bug: 68860345
Bug: 151177513
Bug: 151953481
Bug: 213388645
Bug: 210912771
Test: m all
Test: m systemlicense
Test: m htmlnotice; out/soong/host/linux-x85/htmlnotice ...
where ... is the path to the .meta_lic file for the system image. In my
case if
$ export PRODUCT=$(realpath $ANDROID_PRODUCT_OUT --relative-to=$PWD)
... can be expressed as:
${PRODUCT}/gen/META/lic_intermediates/${PRODUCT}/system.img.meta_lic
Change-Id: Idbbeb2939d8cbf497237516fe468004fcd2d72a1
diff --git a/tools/compliance/Android.bp b/tools/compliance/Android.bp
index 5684f2f..0d7cf10 100644
--- a/tools/compliance/Android.bp
+++ b/tools/compliance/Android.bp
@@ -46,6 +46,13 @@
}
blueprint_go_binary {
+ name: "htmlnotice",
+ srcs: ["cmd/htmlnotice.go"],
+ deps: ["compliance-module"],
+ testSrcs: ["cmd/htmlnotice_test.go"],
+}
+
+blueprint_go_binary {
name: "textnotice",
srcs: ["cmd/textnotice.go"],
deps: ["compliance-module"],
diff --git a/tools/compliance/cmd/htmlnotice.go b/tools/compliance/cmd/htmlnotice.go
new file mode 100644
index 0000000..cff1ff8
--- /dev/null
+++ b/tools/compliance/cmd/htmlnotice.go
@@ -0,0 +1,216 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "bytes"
+ "compliance"
+ "flag"
+ "fmt"
+ "html"
+ "io"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+var (
+ outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
+ includeTOC = flag.Bool("toc", true, "Whether to include a table of contents.")
+ stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
+ title = flag.String("title", "", "The title of the notice file.")
+
+ failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
+ failNoLicenses = fmt.Errorf("No licenses found")
+)
+
+type context struct {
+ stdout io.Writer
+ stderr io.Writer
+ rootFS fs.FS
+ includeTOC bool
+ stripPrefix string
+ title string
+}
+
+func init() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
+
+Outputs an html NOTICE.html file.
+
+Options:
+`, filepath.Base(os.Args[0]))
+ flag.PrintDefaults()
+ }
+}
+
+func main() {
+ flag.Parse()
+
+ // Must specify at least one root target.
+ if flag.NArg() == 0 {
+ flag.Usage()
+ os.Exit(2)
+ }
+
+ if len(*outputFile) == 0 {
+ flag.Usage()
+ fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n")
+ os.Exit(2)
+ } else {
+ dir, err := filepath.Abs(filepath.Dir(*outputFile))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "cannot determine path to %q: %w\n", *outputFile, err)
+ os.Exit(1)
+ }
+ fi, err := os.Stat(dir)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %w\n", dir, *outputFile, err)
+ os.Exit(1)
+ }
+ if !fi.IsDir() {
+ fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile)
+ os.Exit(1)
+ }
+ }
+
+ var ofile io.Writer
+ ofile = os.Stdout
+ if *outputFile != "-" {
+ ofile = &bytes.Buffer{}
+ }
+
+ ctx := &context{ofile, os.Stderr, os.DirFS("."), *includeTOC, *stripPrefix, *title}
+
+ err := htmlNotice(ctx, flag.Args()...)
+ if err != nil {
+ if err == failNoneRequested {
+ flag.Usage()
+ }
+ fmt.Fprintf(os.Stderr, "%s\n", err.Error())
+ os.Exit(1)
+ }
+ if *outputFile != "-" {
+ err := os.WriteFile(*outputFile, ofile.(*bytes.Buffer).Bytes(), 0666)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "could not write output to %q: %w\n", *outputFile, err)
+ os.Exit(1)
+ }
+ }
+ os.Exit(0)
+}
+
+// htmlNotice implements the htmlnotice utility.
+func htmlNotice(ctx *context, files ...string) error {
+ // Must be at least one root file.
+ if len(files) < 1 {
+ return failNoneRequested
+ }
+
+ // Read the license graph from the license metadata files (*.meta_lic).
+ licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files)
+ if err != nil {
+ return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
+ }
+ if licenseGraph == nil {
+ return failNoLicenses
+ }
+
+ // rs contains all notice resolutions.
+ rs := compliance.ResolveNotices(licenseGraph)
+
+ ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs)
+ if err != nil {
+ return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err)
+ }
+
+ fmt.Fprintln(ctx.stdout, "<!DOCTYPE html>")
+ fmt.Fprintln(ctx.stdout, "<html><head>\n")
+ fmt.Fprintln(ctx.stdout, "<style type=\"text/css\">")
+ fmt.Fprintln(ctx.stdout, "body { padding: 2px; margin: 0; }")
+ fmt.Fprintln(ctx.stdout, "ul { list-style-type: none; margin: 0; padding: 0; }")
+ fmt.Fprintln(ctx.stdout, "li { padding-left: 1em; }")
+ fmt.Fprintln(ctx.stdout, ".file-list { margin-left: 1em; }")
+ fmt.Fprintln(ctx.stdout, "</style>\n")
+ if 0 < len(ctx.title) {
+ fmt.Fprintf(ctx.stdout, "<title>%s</title>\n", html.EscapeString(ctx.title))
+ }
+ fmt.Fprintln(ctx.stdout, "</head>")
+ fmt.Fprintln(ctx.stdout, "<body>")
+
+ if 0 < len(ctx.title) {
+ fmt.Fprintf(ctx.stdout, " <h1>%s</h1>\n", html.EscapeString(ctx.title))
+ }
+ ids := make(map[string]string)
+ if ctx.includeTOC {
+ fmt.Fprintln(ctx.stdout, " <ul class=\"toc\">")
+ i := 0
+ for installPath := range ni.InstallPaths() {
+ id := fmt.Sprintf("id%d", i)
+ i++
+ ids[installPath] = id
+ var p string
+ if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
+ p = installPath[len(ctx.stripPrefix):]
+ if 0 == len(p) {
+ if 0 < len(ctx.title) {
+ p = ctx.title
+ } else {
+ p = "root"
+ }
+ }
+ } else {
+ p = installPath
+ }
+ fmt.Fprintf(ctx.stdout, " <li id=\"%s\"><strong>%s</strong>\n <ul>\n", id, html.EscapeString(p))
+ for _, h := range ni.InstallHashes(installPath) {
+ libs := ni.InstallHashLibs(installPath, h)
+ fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", h.String(), html.EscapeString(strings.Join(libs, ", ")))
+ }
+ fmt.Fprintln(ctx.stdout, " </ul>")
+ }
+ fmt.Fprintln(ctx.stdout, " </ul><!-- toc -->")
+ }
+ for h := range ni.Hashes() {
+ fmt.Fprintln(ctx.stdout, " <hr>")
+ for _, libName := range ni.HashLibs(h) {
+ fmt.Fprintf(ctx.stdout, " <strong>%s</strong> used by:\n <ul class=\"file-list\">\n", html.EscapeString(libName))
+ for _, installPath := range ni.HashLibInstalls(h, libName) {
+ if id, ok := ids[installPath]; ok {
+ if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
+ fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath[len(ctx.stripPrefix):]))
+ } else {
+ fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath))
+ }
+ } else {
+ if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
+ fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath[len(ctx.stripPrefix):]))
+ } else {
+ fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath))
+ }
+ }
+ }
+ fmt.Fprintf(ctx.stdout, " </ul>\n")
+ }
+ fmt.Fprintf(ctx.stdout, " </ul>\n <a id=\"%s\"/><pre class=\"license-text\">", h.String())
+ fmt.Fprintln(ctx.stdout, html.EscapeString(string(ni.HashText(h))))
+ fmt.Fprintln(ctx.stdout, " </pre><!-- license-text -->")
+ }
+ fmt.Fprintln(ctx.stdout, "</body></html>")
+
+ return nil
+}
diff --git a/tools/compliance/cmd/htmlnotice_test.go b/tools/compliance/cmd/htmlnotice_test.go
new file mode 100644
index 0000000..8d3ea02
--- /dev/null
+++ b/tools/compliance/cmd/htmlnotice_test.go
@@ -0,0 +1,812 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "fmt"
+ "html"
+ "os"
+ "regexp"
+ "strings"
+ "testing"
+)
+
+var (
+ horizontalRule = regexp.MustCompile(`^\s*<hr>\s*$`)
+ bodyTag = regexp.MustCompile(`^\s*<body>\s*$`)
+ boilerPlate = regexp.MustCompile(`^\s*(?:<ul class="file-list">|<ul>|</.*)\s*$`)
+ tocTag = regexp.MustCompile(`^\s*<ul class="toc">\s*$`)
+ libraryName = regexp.MustCompile(`^\s*<strong>(.*)</strong>\s\s*used\s\s*by\s*:\s*$`)
+ licenseText = regexp.MustCompile(`^\s*<a id="[^"]{32}"/><pre class="license-text">(.*)$`)
+ titleTag = regexp.MustCompile(`^\s*<title>(.*)</title>\s*$`)
+ h1Tag = regexp.MustCompile(`^\s*<h1>(.*)</h1>\s*$`)
+ usedByTarget = regexp.MustCompile(`^\s*<li>(?:<a href="#id[0-9]+">)?((?:out/(?:[^/<]*/)+)[^/<]*)(?:</a>)?\s*$`)
+ installTarget = regexp.MustCompile(`^\s*<li id="id[0-9]+"><strong>(.*)</strong>\s*$`)
+ libReference = regexp.MustCompile(`^\s*<li><a href="#[^"]{32}">(.*)</a>\s*$`)
+)
+
+func Test(t *testing.T) {
+ tests := []struct {
+ condition string
+ name string
+ roots []string
+ includeTOC bool
+ stripPrefix string
+ title string
+ expectedOut []matcher
+ }{
+ {
+ condition: "firstparty",
+ name: "apex",
+ roots: []string{"highest.apex.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/liba.so"},
+ usedBy{"highest.apex/lib/libb.so"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "apex+toc",
+ roots: []string{"highest.apex.meta_lic"},
+ includeTOC: true,
+ expectedOut: []matcher{
+ toc{},
+ target{"highest.apex"},
+ uses{"Android"},
+ target{"highest.apex/bin/bin1"},
+ uses{"Android"},
+ target{"highest.apex/bin/bin2"},
+ uses{"Android"},
+ target{"highest.apex/lib/liba.so"},
+ uses{"Android"},
+ target{"highest.apex/lib/libb.so"},
+ uses{"Android"},
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/liba.so"},
+ usedBy{"highest.apex/lib/libb.so"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "apex-with-title",
+ roots: []string{"highest.apex.meta_lic"},
+ title: "Emperor",
+ expectedOut: []matcher{
+ pageTitle{"Emperor"},
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/liba.so"},
+ usedBy{"highest.apex/lib/libb.so"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "apex-with-title+toc",
+ roots: []string{"highest.apex.meta_lic"},
+ includeTOC: true,
+ title: "Emperor",
+ expectedOut: []matcher{
+ pageTitle{"Emperor"},
+ toc{},
+ target{"highest.apex"},
+ uses{"Android"},
+ target{"highest.apex/bin/bin1"},
+ uses{"Android"},
+ target{"highest.apex/bin/bin2"},
+ uses{"Android"},
+ target{"highest.apex/lib/liba.so"},
+ uses{"Android"},
+ target{"highest.apex/lib/libb.so"},
+ uses{"Android"},
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/liba.so"},
+ usedBy{"highest.apex/lib/libb.so"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "container",
+ roots: []string{"container.zip.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/bin2"},
+ usedBy{"container.zip/liba.so"},
+ usedBy{"container.zip/libb.so"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "application",
+ roots: []string{"application.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"application"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "binary",
+ roots: []string{"bin/bin1.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"bin/bin1"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "firstparty",
+ name: "library",
+ roots: []string{"lib/libd.so.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"lib/libd.so"},
+ firstParty{},
+ },
+ },
+ {
+ condition: "notice",
+ name: "apex",
+ roots: []string{"highest.apex.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/libb.so"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/lib/liba.so"},
+ library{"External"},
+ usedBy{"highest.apex/bin/bin1"},
+ notice{},
+ },
+ },
+ {
+ condition: "notice",
+ name: "container",
+ roots: []string{"container.zip.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/bin2"},
+ usedBy{"container.zip/libb.so"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/liba.so"},
+ library{"External"},
+ usedBy{"container.zip/bin1"},
+ notice{},
+ },
+ },
+ {
+ condition: "notice",
+ name: "application",
+ roots: []string{"application.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"application"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"application"},
+ notice{},
+ },
+ },
+ {
+ condition: "notice",
+ name: "binary",
+ roots: []string{"bin/bin1.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"bin/bin1"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"bin/bin1"},
+ library{"External"},
+ usedBy{"bin/bin1"},
+ notice{},
+ },
+ },
+ {
+ condition: "notice",
+ name: "library",
+ roots: []string{"lib/libd.so.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"External"},
+ usedBy{"lib/libd.so"},
+ notice{},
+ },
+ },
+ {
+ condition: "reciprocal",
+ name: "apex",
+ roots: []string{"highest.apex.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/libb.so"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/lib/liba.so"},
+ library{"External"},
+ usedBy{"highest.apex/bin/bin1"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "reciprocal",
+ name: "container",
+ roots: []string{"container.zip.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/bin2"},
+ usedBy{"container.zip/libb.so"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/liba.so"},
+ library{"External"},
+ usedBy{"container.zip/bin1"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "reciprocal",
+ name: "application",
+ roots: []string{"application.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"application"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"application"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "reciprocal",
+ name: "binary",
+ roots: []string{"bin/bin1.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"bin/bin1"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"bin/bin1"},
+ library{"External"},
+ usedBy{"bin/bin1"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "reciprocal",
+ name: "library",
+ roots: []string{"lib/libd.so.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"External"},
+ usedBy{"lib/libd.so"},
+ notice{},
+ },
+ },
+ {
+ condition: "restricted",
+ name: "apex",
+ roots: []string{"highest.apex.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/bin/bin2"},
+ firstParty{},
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/libb.so"},
+ library{"Device"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/lib/liba.so"},
+ restricted{},
+ hr{},
+ library{"External"},
+ usedBy{"highest.apex/bin/bin1"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "restricted",
+ name: "container",
+ roots: []string{"container.zip.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/bin2"},
+ firstParty{},
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip/bin2"},
+ usedBy{"container.zip/libb.so"},
+ library{"Device"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/liba.so"},
+ restricted{},
+ hr{},
+ library{"External"},
+ usedBy{"container.zip/bin1"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "restricted",
+ name: "application",
+ roots: []string{"application.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"application"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"application"},
+ restricted{},
+ },
+ },
+ {
+ condition: "restricted",
+ name: "binary",
+ roots: []string{"bin/bin1.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"bin/bin1"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"bin/bin1"},
+ restricted{},
+ hr{},
+ library{"External"},
+ usedBy{"bin/bin1"},
+ reciprocal{},
+ },
+ },
+ {
+ condition: "restricted",
+ name: "library",
+ roots: []string{"lib/libd.so.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"External"},
+ usedBy{"lib/libd.so"},
+ notice{},
+ },
+ },
+ {
+ condition: "proprietary",
+ name: "apex",
+ roots: []string{"highest.apex.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex/bin/bin2"},
+ usedBy{"highest.apex/lib/libb.so"},
+ restricted{},
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex"},
+ usedBy{"highest.apex/bin/bin1"},
+ firstParty{},
+ hr{},
+ library{"Android"},
+ usedBy{"highest.apex/bin/bin2"},
+ library{"Device"},
+ usedBy{"highest.apex/bin/bin1"},
+ usedBy{"highest.apex/lib/liba.so"},
+ library{"External"},
+ usedBy{"highest.apex/bin/bin1"},
+ proprietary{},
+ },
+ },
+ {
+ condition: "proprietary",
+ name: "container",
+ roots: []string{"container.zip.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip/bin2"},
+ usedBy{"container.zip/libb.so"},
+ restricted{},
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip"},
+ usedBy{"container.zip/bin1"},
+ firstParty{},
+ hr{},
+ library{"Android"},
+ usedBy{"container.zip/bin2"},
+ library{"Device"},
+ usedBy{"container.zip/bin1"},
+ usedBy{"container.zip/liba.so"},
+ library{"External"},
+ usedBy{"container.zip/bin1"},
+ proprietary{},
+ },
+ },
+ {
+ condition: "proprietary",
+ name: "application",
+ roots: []string{"application.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"application"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"application"},
+ proprietary{},
+ },
+ },
+ {
+ condition: "proprietary",
+ name: "binary",
+ roots: []string{"bin/bin1.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"Android"},
+ usedBy{"bin/bin1"},
+ firstParty{},
+ hr{},
+ library{"Device"},
+ usedBy{"bin/bin1"},
+ library{"External"},
+ usedBy{"bin/bin1"},
+ proprietary{},
+ },
+ },
+ {
+ condition: "proprietary",
+ name: "library",
+ roots: []string{"lib/libd.so.meta_lic"},
+ expectedOut: []matcher{
+ hr{},
+ library{"External"},
+ usedBy{"lib/libd.so"},
+ notice{},
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.condition+" "+tt.name, func(t *testing.T) {
+ stdout := &bytes.Buffer{}
+ stderr := &bytes.Buffer{}
+
+ rootFiles := make([]string, 0, len(tt.roots))
+ for _, r := range tt.roots {
+ rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
+ }
+
+ ctx := context{stdout, stderr, os.DirFS("."), tt.includeTOC, tt.stripPrefix, tt.title}
+
+ err := htmlNotice(&ctx, rootFiles...)
+ if err != nil {
+ t.Fatalf("htmlnotice: error = %w, stderr = %v", err, stderr)
+ return
+ }
+ if stderr.Len() > 0 {
+ t.Errorf("htmlnotice: gotStderr = %v, want none", stderr)
+ }
+
+ t.Logf("got stdout: %s", stdout.String())
+
+ t.Logf("want stdout: %s", matcherList(tt.expectedOut).String())
+
+ out := bufio.NewScanner(stdout)
+ lineno := 0
+ inBody := false
+ hasTitle := false
+ ttle, expectTitle := tt.expectedOut[0].(pageTitle)
+ for out.Scan() {
+ line := out.Text()
+ if strings.TrimLeft(line, " ") == "" {
+ continue
+ }
+ if !inBody {
+ if expectTitle {
+ if tl := checkTitle(line); 0 < len(tl) {
+ if tl != ttle.t {
+ t.Errorf("htmlnotice: unexpected title: got %q, want %q", tl, ttle.t)
+ }
+ hasTitle = true
+ }
+ }
+ if bodyTag.MatchString(line) {
+ inBody = true
+ if expectTitle && !hasTitle {
+ t.Errorf("htmlnotice: missing title: got no <title> tag, want <title>%s</title>", ttle.t)
+ }
+ }
+ continue
+ }
+ if boilerPlate.MatchString(line) {
+ continue
+ }
+ if len(tt.expectedOut) <= lineno {
+ t.Errorf("htmlnotice: unexpected output at line %d: got %q, want nothing (wanted %d lines)", lineno+1, line, len(tt.expectedOut))
+ } else if !tt.expectedOut[lineno].isMatch(line) {
+ t.Errorf("htmlnotice: unexpected output at line %d: got %q, want %q", lineno+1, line, tt.expectedOut[lineno].String())
+ }
+ lineno++
+ }
+ if !inBody {
+ t.Errorf("htmlnotice: missing body: got no <body> tag, want <body> tag followed by %s", matcherList(tt.expectedOut).String())
+ return
+ }
+ for ; lineno < len(tt.expectedOut); lineno++ {
+ t.Errorf("htmlnotice: missing output line %d: ended early, want %q", lineno+1, tt.expectedOut[lineno].String())
+ }
+ })
+ }
+}
+
+func checkTitle(line string) string {
+ groups := titleTag.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return ""
+ }
+ return groups[1]
+}
+
+type matcher interface {
+ isMatch(line string) bool
+ String() string
+}
+
+type pageTitle struct {
+ t string
+}
+
+func (m pageTitle) isMatch(line string) bool {
+ groups := h1Tag.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return false
+ }
+ return groups[1] == html.EscapeString(m.t)
+}
+
+func (m pageTitle) String() string {
+ return " <h1>" + html.EscapeString(m.t) + "</h1>"
+}
+
+type toc struct{}
+
+func (m toc) isMatch(line string) bool {
+ return tocTag.MatchString(line)
+}
+
+func (m toc) String() string {
+ return ` <ul class="toc">`
+}
+
+type target struct {
+ name string
+}
+
+func (m target) isMatch(line string) bool {
+ groups := installTarget.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return false
+ }
+ return strings.HasPrefix(groups[1], "out/") && strings.HasSuffix(groups[1], "/"+html.EscapeString(m.name))
+}
+
+func (m target) String() string {
+ return ` <li id="id#"><strong>` + html.EscapeString(m.name) + `</strong>`
+}
+
+type uses struct {
+ name string
+}
+
+func (m uses) isMatch(line string) bool {
+ groups := libReference.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return false
+ }
+ return groups[1] == html.EscapeString(m.name)
+}
+
+func (m uses) String() string {
+ return ` <li><a href="#hash">` + html.EscapeString(m.name) + `</a>`
+}
+
+type hr struct{}
+
+func (m hr) isMatch(line string) bool {
+ return horizontalRule.MatchString(line)
+}
+
+func (m hr) String() string {
+ return " <hr>"
+}
+
+type library struct {
+ name string
+}
+
+func (m library) isMatch(line string) bool {
+ groups := libraryName.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return false
+ }
+ return groups[1] == html.EscapeString(m.name)
+}
+
+func (m library) String() string {
+ return " <strong>" + html.EscapeString(m.name) + "</strong> used by:"
+}
+
+type usedBy struct {
+ name string
+}
+
+func (m usedBy) isMatch(line string) bool {
+ groups := usedByTarget.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return false
+ }
+ return strings.HasPrefix(groups[1], "out/") && strings.HasSuffix(groups[1], "/"+html.EscapeString(m.name))
+}
+
+func (m usedBy) String() string {
+ return " <li>out/.../" + html.EscapeString(m.name)
+}
+
+func matchesText(line, text string) bool {
+ groups := licenseText.FindStringSubmatch(line)
+ if len(groups) != 2 {
+ return false
+ }
+ return groups[1] == html.EscapeString(text)
+}
+
+func expectedText(text string) string {
+ return ` <a href="#hash"/><pre class="license-text">` + html.EscapeString(text)
+}
+
+type firstParty struct{}
+
+func (m firstParty) isMatch(line string) bool {
+ return matchesText(line, "&&&First Party License&&&")
+}
+
+func (m firstParty) String() string {
+ return expectedText("&&&First Party License&&&")
+}
+
+type notice struct{}
+
+func (m notice) isMatch(line string) bool {
+ return matchesText(line, "%%%Notice License%%%")
+}
+
+func (m notice) String() string {
+ return expectedText("%%%Notice License%%%")
+}
+
+type reciprocal struct{}
+
+func (m reciprocal) isMatch(line string) bool {
+ return matchesText(line, "$$$Reciprocal License$$$")
+}
+
+func (m reciprocal) String() string {
+ return expectedText("$$$Reciprocal License$$$")
+}
+
+type restricted struct{}
+
+func (m restricted) isMatch(line string) bool {
+ return matchesText(line, "###Restricted License###")
+}
+
+func (m restricted) String() string {
+ return expectedText("###Restricted License###")
+}
+
+type proprietary struct{}
+
+func (m proprietary) isMatch(line string) bool {
+ return matchesText(line, "@@@Proprietary License@@@")
+}
+
+func (m proprietary) String() string {
+ return expectedText("@@@Proprietary License@@@")
+}
+
+type matcherList []matcher
+
+func (l matcherList) String() string {
+ var sb strings.Builder
+ for _, m := range l {
+ s := m.String()
+ if s[:3] == s[len(s)-3:] {
+ fmt.Fprintln(&sb)
+ }
+ fmt.Fprintf(&sb, "%s\n", s)
+ if s[:3] == s[len(s)-3:] {
+ fmt.Fprintln(&sb)
+ }
+ }
+ return sb.String()
+}
diff --git a/tools/compliance/noticeindex.go b/tools/compliance/noticeindex.go
index 06c2627..5b7f376 100644
--- a/tools/compliance/noticeindex.go
+++ b/tools/compliance/noticeindex.go
@@ -54,8 +54,8 @@
text map[hash][]byte
// hashLibInstall maps hashes to libraries to install paths.
hashLibInstall map[hash]map[string]map[string]struct{}
- // installLibHash maps install paths to libraries to hashes.
- installLibHash map[string]map[string]map[hash]struct{}
+ // installHashLib maps install paths to libraries to hashes.
+ installHashLib map[string]map[hash]map[string]struct{}
// libHash maps libraries to hashes.
libHash map[string]map[hash]struct{}
// targetHash maps target nodes to hashes.
@@ -75,7 +75,7 @@
make(map[string]hash),
make(map[hash][]byte),
make(map[hash]map[string]map[string]struct{}),
- make(map[string]map[string]map[hash]struct{}),
+ make(map[string]map[hash]map[string]struct{}),
make(map[string]map[hash]struct{}),
make(map[*TargetNode]map[hash]struct{}),
make(map[string]string),
@@ -115,15 +115,15 @@
ni.libHash[libName][h] = struct{}{}
}
for _, installPath := range installPaths {
- if _, ok := ni.installLibHash[installPath]; !ok {
- ni.installLibHash[installPath] = make(map[string]map[hash]struct{})
- ni.installLibHash[installPath][libName] = make(map[hash]struct{})
- ni.installLibHash[installPath][libName][h] = struct{}{}
- } else if _, ok = ni.installLibHash[installPath][libName]; !ok {
- ni.installLibHash[installPath][libName] = make(map[hash]struct{})
- ni.installLibHash[installPath][libName][h] = struct{}{}
- } else if _, ok = ni.installLibHash[installPath][libName][h]; !ok {
- ni.installLibHash[installPath][libName][h] = struct{}{}
+ if _, ok := ni.installHashLib[installPath]; !ok {
+ ni.installHashLib[installPath] = make(map[hash]map[string]struct{})
+ ni.installHashLib[installPath][h] = make(map[string]struct{})
+ ni.installHashLib[installPath][h][libName] = struct{}{}
+ } else if _, ok = ni.installHashLib[installPath][h]; !ok {
+ ni.installHashLib[installPath][h] = make(map[string]struct{})
+ ni.installHashLib[installPath][h][libName] = struct{}{}
+ } else if _, ok = ni.installHashLib[installPath][h][libName]; !ok {
+ ni.installHashLib[installPath][h][libName] = struct{}{}
}
if _, ok := ni.hashLibInstall[h]; !ok {
ni.hashLibInstall[h] = make(map[string]map[string]struct{})
@@ -197,7 +197,7 @@
hl = append(hl, h)
}
if len(hl) > 0 {
- sort.Sort(hashList{ni, libName, &hl})
+ sort.Sort(hashList{ni, libName, "", &hl})
for _, h := range hl {
c <- h
}
@@ -230,6 +230,46 @@
return installs
}
+// InstallPaths returns the ordered channel of indexed install paths.
+func (ni *NoticeIndex) InstallPaths() chan string {
+ c := make(chan string)
+ go func() {
+ paths := make([]string, 0, len(ni.installHashLib))
+ for path := range ni.installHashLib {
+ paths = append(paths, path)
+ }
+ sort.Strings(paths)
+ for _, installPath := range paths {
+ c <- installPath
+ }
+ close(c)
+ }()
+ return c
+}
+
+// InstallHashes returns the ordered array of hashes attached to `installPath`.
+func (ni *NoticeIndex) InstallHashes(installPath string) []hash {
+ result := make([]hash, 0, len(ni.installHashLib[installPath]))
+ for h := range ni.installHashLib[installPath] {
+ result = append(result, h)
+ }
+ if len(result) > 0 {
+ sort.Sort(hashList{ni, "", installPath, &result})
+ }
+ return result
+}
+
+// InstallHashLibs returns the ordered array of library names attached to
+// `installPath` as hash `h`.
+func (ni *NoticeIndex) InstallHashLibs(installPath string, h hash) []string {
+ result := make([]string, 0, len(ni.installHashLib[installPath][h]))
+ for libName := range ni.installHashLib[installPath][h] {
+ result = append(result, libName)
+ }
+ sort.Strings(result)
+ return result
+}
+
// HashText returns the file content of the license text hashed as `h`.
func (ni *NoticeIndex) HashText(h hash) []byte {
return ni.text[h]
@@ -492,9 +532,10 @@
// hashList orders an array of hashes
type hashList struct {
- ni *NoticeIndex
- libName string
- hashes *[]hash
+ ni *NoticeIndex
+ libName string
+ installPath string
+ hashes *[]hash
}
// Len returns the count of elements in the slice.
@@ -511,6 +552,14 @@
if 0 < len(l.libName) {
insti = len(l.ni.hashLibInstall[(*l.hashes)[i]][l.libName])
instj = len(l.ni.hashLibInstall[(*l.hashes)[j]][l.libName])
+ } else {
+ libsi := l.ni.InstallHashLibs(l.installPath, (*l.hashes)[i])
+ libsj := l.ni.InstallHashLibs(l.installPath, (*l.hashes)[j])
+ libsis := strings.Join(libsi, " ")
+ libsjs := strings.Join(libsj, " ")
+ if libsis != libsjs {
+ return libsis < libsjs
+ }
}
if insti == instj {
leni := len(l.ni.text[(*l.hashes)[i]])