Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 19 | "compress/gzip" |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 20 | "flag" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "io/fs" |
| 24 | "os" |
| 25 | "path/filepath" |
| 26 | "strings" |
Colin Cross | 38a6193 | 2022-01-27 15:26:49 -0800 | [diff] [blame] | 27 | |
| 28 | "android/soong/tools/compliance" |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 29 | |
| 30 | "github.com/google/blueprint/deptools" |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var ( |
| 34 | outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)") |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 35 | depsFile = flag.String("d", "", "Where to write the deps file") |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 36 | product = flag.String("product", "", "The name of the product for which the notice is generated.") |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 37 | 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 Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 39 | |
| 40 | failNoneRequested = fmt.Errorf("\nNo license metadata files requested") |
| 41 | failNoLicenses = fmt.Errorf("No licenses found") |
| 42 | ) |
| 43 | |
| 44 | type context struct { |
| 45 | stdout io.Writer |
| 46 | stderr io.Writer |
| 47 | rootFS fs.FS |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 48 | product string |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 49 | stripPrefix []string |
| 50 | title string |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 51 | deps *[]string |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 54 | func (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 Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 59 | p = ctx.product |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 60 | } |
| 61 | if 0 == len(p) { |
| 62 | continue |
| 63 | } |
| 64 | return p |
| 65 | } |
| 66 | } |
| 67 | return installPath |
| 68 | } |
| 69 | |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 70 | func init() { |
| 71 | flag.Usage = func() { |
| 72 | fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...} |
| 73 | |
| 74 | Outputs a text NOTICE file. |
| 75 | |
| 76 | Options: |
| 77 | `, filepath.Base(os.Args[0])) |
| 78 | flag.PrintDefaults() |
| 79 | } |
| 80 | } |
| 81 | |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 82 | // newMultiString creates a flag that allows multiple values in an array. |
| 83 | func 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. |
| 90 | type multiString []string |
| 91 | |
| 92 | func (ms *multiString) String() string { return strings.Join(*ms, ", ") } |
| 93 | func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil } |
| 94 | |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 95 | func 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 Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 111 | fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err) |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 112 | os.Exit(1) |
| 113 | } |
| 114 | fi, err := os.Stat(dir) |
| 115 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 116 | fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err) |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 117 | 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 Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 126 | var closer io.Closer |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 127 | ofile = os.Stdout |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 128 | var obuf *bytes.Buffer |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 129 | if *outputFile != "-" { |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 130 | 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 Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 138 | var deps []string |
| 139 | |
Bob Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 140 | ctx := &context{ofile, os.Stderr, os.DirFS("."), *product, *stripPrefix, *title, &deps} |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 141 | |
| 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 Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 150 | if closer != nil { |
| 151 | closer.Close() |
| 152 | } |
| 153 | |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 154 | if *outputFile != "-" { |
Bob Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 155 | err := os.WriteFile(*outputFile, obuf.Bytes(), 0666) |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 156 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 157 | fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err) |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 158 | os.Exit(1) |
| 159 | } |
| 160 | } |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 161 | 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 Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 168 | os.Exit(0) |
| 169 | } |
| 170 | |
| 171 | // textNotice implements the textNotice utility. |
| 172 | func 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 Badour | 49dd4f7 | 2022-02-04 14:49:01 -0800 | [diff] [blame] | 195 | if 0 < len(ctx.title) { |
| 196 | fmt.Fprintf(ctx.stdout, "%s\n\n", ctx.title) |
| 197 | } |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 198 | 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 Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 203 | fmt.Fprintf(ctx.stdout, " %s\n", ctx.strip(installPath)) |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 204 | } |
| 205 | fmt.Fprintln(ctx.stdout) |
| 206 | } |
| 207 | ctx.stdout.Write(ni.HashText(h)) |
| 208 | fmt.Fprintln(ctx.stdout) |
| 209 | } |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame] | 210 | |
| 211 | *ctx.deps = ni.InputNoticeFiles() |
| 212 | |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 213 | return nil |
| 214 | } |