blob: 9e9229f2bd7eb5b85eeaaa269a23801aa67c432c [file] [log] [blame]
Bob Badoure6fdd142021-12-09 22:10:43 -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 Badour682e1ba2022-02-02 15:15:56 -080019 "compress/gzip"
Bob Badoure6fdd142021-12-09 22:10:43 -080020 "flag"
21 "fmt"
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"
Colin Crossbb45f8c2022-01-28 15:18:19 -080029
30 "github.com/google/blueprint/deptools"
Bob Badoure6fdd142021-12-09 22:10:43 -080031)
32
33var (
34 outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
Colin Crossbb45f8c2022-01-28 15:18:19 -080035 depsFile = flag.String("d", "", "Where to write the deps file")
Bob Badour49dd4f72022-02-04 14:49:01 -080036 product = flag.String("product", "", "The name of the product for which the notice is generated.")
Bob Badour682e1ba2022-02-02 15:15:56 -080037 stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
38 title = flag.String("title", "", "The title of the notice file.")
Bob Badoure6fdd142021-12-09 22:10:43 -080039
40 failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
41 failNoLicenses = fmt.Errorf("No licenses found")
42)
43
44type context struct {
45 stdout io.Writer
46 stderr io.Writer
47 rootFS fs.FS
Bob Badour49dd4f72022-02-04 14:49:01 -080048 product string
Bob Badour682e1ba2022-02-02 15:15:56 -080049 stripPrefix []string
50 title string
Colin Crossbb45f8c2022-01-28 15:18:19 -080051 deps *[]string
Bob Badoure6fdd142021-12-09 22:10:43 -080052}
53
Bob Badour682e1ba2022-02-02 15:15:56 -080054func (ctx context) strip(installPath string) string {
55 for _, prefix := range ctx.stripPrefix {
56 if strings.HasPrefix(installPath, prefix) {
57 p := strings.TrimPrefix(installPath, prefix)
58 if 0 == len(p) {
Bob Badour49dd4f72022-02-04 14:49:01 -080059 p = ctx.product
Bob Badour682e1ba2022-02-02 15:15:56 -080060 }
61 if 0 == len(p) {
62 continue
63 }
64 return p
65 }
66 }
67 return installPath
68}
69
Bob Badoure6fdd142021-12-09 22:10:43 -080070func init() {
71 flag.Usage = func() {
72 fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
73
74Outputs a text NOTICE file.
75
76Options:
77`, filepath.Base(os.Args[0]))
78 flag.PrintDefaults()
79 }
80}
81
Bob Badour682e1ba2022-02-02 15:15:56 -080082// newMultiString creates a flag that allows multiple values in an array.
83func newMultiString(name, usage string) *multiString {
84 var f multiString
85 flag.Var(&f, name, usage)
86 return &f
87}
88
89// multiString implements the flag `Value` interface for multiple strings.
90type multiString []string
91
92func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
93func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
94
Bob Badoure6fdd142021-12-09 22:10:43 -080095func main() {
96 flag.Parse()
97
98 // Must specify at least one root target.
99 if flag.NArg() == 0 {
100 flag.Usage()
101 os.Exit(2)
102 }
103
104 if len(*outputFile) == 0 {
105 flag.Usage()
106 fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n")
107 os.Exit(2)
108 } else {
109 dir, err := filepath.Abs(filepath.Dir(*outputFile))
110 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -0800111 fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err)
Bob Badoure6fdd142021-12-09 22:10:43 -0800112 os.Exit(1)
113 }
114 fi, err := os.Stat(dir)
115 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -0800116 fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err)
Bob Badoure6fdd142021-12-09 22:10:43 -0800117 os.Exit(1)
118 }
119 if !fi.IsDir() {
120 fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile)
121 os.Exit(1)
122 }
123 }
124
125 var ofile io.Writer
Bob Badour682e1ba2022-02-02 15:15:56 -0800126 var closer io.Closer
Bob Badoure6fdd142021-12-09 22:10:43 -0800127 ofile = os.Stdout
Bob Badour682e1ba2022-02-02 15:15:56 -0800128 var obuf *bytes.Buffer
Bob Badoure6fdd142021-12-09 22:10:43 -0800129 if *outputFile != "-" {
Bob Badour682e1ba2022-02-02 15:15:56 -0800130 obuf = &bytes.Buffer{}
131 ofile = obuf
132 }
133 if strings.HasSuffix(*outputFile, ".gz") {
134 ofile, _ = gzip.NewWriterLevel(obuf, gzip.BestCompression)
135 closer = ofile.(io.Closer)
Bob Badoure6fdd142021-12-09 22:10:43 -0800136 }
137
Colin Crossbb45f8c2022-01-28 15:18:19 -0800138 var deps []string
139
Bob Badour49dd4f72022-02-04 14:49:01 -0800140 ctx := &context{ofile, os.Stderr, os.DirFS("."), *product, *stripPrefix, *title, &deps}
Bob Badoure6fdd142021-12-09 22:10:43 -0800141
142 err := textNotice(ctx, flag.Args()...)
143 if err != nil {
144 if err == failNoneRequested {
145 flag.Usage()
146 }
147 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
148 os.Exit(1)
149 }
Bob Badour682e1ba2022-02-02 15:15:56 -0800150 if closer != nil {
151 closer.Close()
152 }
153
Bob Badoure6fdd142021-12-09 22:10:43 -0800154 if *outputFile != "-" {
Bob Badour682e1ba2022-02-02 15:15:56 -0800155 err := os.WriteFile(*outputFile, obuf.Bytes(), 0666)
Bob Badoure6fdd142021-12-09 22:10:43 -0800156 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -0800157 fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err)
Bob Badoure6fdd142021-12-09 22:10:43 -0800158 os.Exit(1)
159 }
160 }
Colin Crossbb45f8c2022-01-28 15:18:19 -0800161 if *depsFile != "" {
162 err := deptools.WriteDepFile(*depsFile, *outputFile, deps)
163 if err != nil {
164 fmt.Fprintf(os.Stderr, "could not write deps to %q: %s\n", *depsFile, err)
165 os.Exit(1)
166 }
167 }
Bob Badoure6fdd142021-12-09 22:10:43 -0800168 os.Exit(0)
169}
170
171// textNotice implements the textNotice utility.
172func textNotice(ctx *context, files ...string) error {
173 // Must be at least one root file.
174 if len(files) < 1 {
175 return failNoneRequested
176 }
177
178 // Read the license graph from the license metadata files (*.meta_lic).
179 licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files)
180 if err != nil {
181 return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
182 }
183 if licenseGraph == nil {
184 return failNoLicenses
185 }
186
187 // rs contains all notice resolutions.
188 rs := compliance.ResolveNotices(licenseGraph)
189
190 ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs)
191 if err != nil {
192 return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err)
193 }
194
Bob Badour49dd4f72022-02-04 14:49:01 -0800195 if 0 < len(ctx.title) {
196 fmt.Fprintf(ctx.stdout, "%s\n\n", ctx.title)
197 }
Bob Badoure6fdd142021-12-09 22:10:43 -0800198 for h := range ni.Hashes() {
199 fmt.Fprintln(ctx.stdout, "==============================================================================")
200 for _, libName := range ni.HashLibs(h) {
201 fmt.Fprintf(ctx.stdout, "%s used by:\n", libName)
202 for _, installPath := range ni.HashLibInstalls(h, libName) {
Bob Badour682e1ba2022-02-02 15:15:56 -0800203 fmt.Fprintf(ctx.stdout, " %s\n", ctx.strip(installPath))
Bob Badoure6fdd142021-12-09 22:10:43 -0800204 }
205 fmt.Fprintln(ctx.stdout)
206 }
207 ctx.stdout.Write(ni.HashText(h))
208 fmt.Fprintln(ctx.stdout)
209 }
Colin Crossbb45f8c2022-01-28 15:18:19 -0800210
211 *ctx.deps = ni.InputNoticeFiles()
212
Bob Badoure6fdd142021-12-09 22:10:43 -0800213 return nil
214}