Track dependencies when reading notice files

Track which files are read by the notice file indexer and add a flag
to textnotice and htmlnotice to support writing them out.

Bug: 207445310
Test: textnotice_test
Test: htmlnotice_test
Change-Id: Ib74706b8a87a5ed9268a0fe37982ecf89f4e227d
diff --git a/tools/compliance/cmd/htmlnotice/htmlnotice.go b/tools/compliance/cmd/htmlnotice/htmlnotice.go
index 2f59ee0..1f55546 100644
--- a/tools/compliance/cmd/htmlnotice/htmlnotice.go
+++ b/tools/compliance/cmd/htmlnotice/htmlnotice.go
@@ -26,10 +26,13 @@
 	"strings"
 
 	"android/soong/tools/compliance"
+
+	"github.com/google/blueprint/deptools"
 )
 
 var (
 	outputFile  = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
+	depsFile    = flag.String("d", "", "Where to write the deps file")
 	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.")
@@ -45,6 +48,7 @@
 	includeTOC  bool
 	stripPrefix string
 	title       string
+	deps        *[]string
 }
 
 func init() {
@@ -95,7 +99,9 @@
 		ofile = &bytes.Buffer{}
 	}
 
-	ctx := &context{ofile, os.Stderr, os.DirFS("."), *includeTOC, *stripPrefix, *title}
+	var deps []string
+
+	ctx := &context{ofile, os.Stderr, os.DirFS("."), *includeTOC, *stripPrefix, *title, &deps}
 
 	err := htmlNotice(ctx, flag.Args()...)
 	if err != nil {
@@ -112,6 +118,13 @@
 			os.Exit(1)
 		}
 	}
+	if *depsFile != "" {
+		err := deptools.WriteDepFile(*depsFile, *outputFile, deps)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "could not write deps to %q: %s\n", *depsFile, err)
+			os.Exit(1)
+		}
+	}
 	os.Exit(0)
 }
 
@@ -213,5 +226,7 @@
 	}
 	fmt.Fprintln(ctx.stdout, "</body></html>")
 
+	*ctx.deps = ni.InputNoticeFiles()
+
 	return nil
 }