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 | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 19 | "flag" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "io/fs" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "strings" |
Colin Cross | 38a6193 | 2022-01-27 15:26:49 -0800 | [diff] [blame] | 26 | |
| 27 | "android/soong/tools/compliance" |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame^] | 28 | |
| 29 | "github.com/google/blueprint/deptools" |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | 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^] | 34 | depsFile = flag.String("d", "", "Where to write the deps file") |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 35 | stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root") |
| 36 | |
| 37 | failNoneRequested = fmt.Errorf("\nNo license metadata files requested") |
| 38 | failNoLicenses = fmt.Errorf("No licenses found") |
| 39 | ) |
| 40 | |
| 41 | type context struct { |
| 42 | stdout io.Writer |
| 43 | stderr io.Writer |
| 44 | rootFS fs.FS |
| 45 | stripPrefix string |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame^] | 46 | deps *[]string |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func init() { |
| 50 | flag.Usage = func() { |
| 51 | fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...} |
| 52 | |
| 53 | Outputs a text NOTICE file. |
| 54 | |
| 55 | Options: |
| 56 | `, filepath.Base(os.Args[0])) |
| 57 | flag.PrintDefaults() |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func main() { |
| 62 | flag.Parse() |
| 63 | |
| 64 | // Must specify at least one root target. |
| 65 | if flag.NArg() == 0 { |
| 66 | flag.Usage() |
| 67 | os.Exit(2) |
| 68 | } |
| 69 | |
| 70 | if len(*outputFile) == 0 { |
| 71 | flag.Usage() |
| 72 | fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n") |
| 73 | os.Exit(2) |
| 74 | } else { |
| 75 | dir, err := filepath.Abs(filepath.Dir(*outputFile)) |
| 76 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 77 | 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] | 78 | os.Exit(1) |
| 79 | } |
| 80 | fi, err := os.Stat(dir) |
| 81 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 82 | 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] | 83 | os.Exit(1) |
| 84 | } |
| 85 | if !fi.IsDir() { |
| 86 | fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile) |
| 87 | os.Exit(1) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | var ofile io.Writer |
| 92 | ofile = os.Stdout |
| 93 | if *outputFile != "-" { |
| 94 | ofile = &bytes.Buffer{} |
| 95 | } |
| 96 | |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame^] | 97 | var deps []string |
| 98 | |
| 99 | ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, &deps} |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 100 | |
| 101 | err := textNotice(ctx, flag.Args()...) |
| 102 | if err != nil { |
| 103 | if err == failNoneRequested { |
| 104 | flag.Usage() |
| 105 | } |
| 106 | fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 107 | os.Exit(1) |
| 108 | } |
| 109 | if *outputFile != "-" { |
| 110 | err := os.WriteFile(*outputFile, ofile.(*bytes.Buffer).Bytes(), 0666) |
| 111 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 112 | 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] | 113 | os.Exit(1) |
| 114 | } |
| 115 | } |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame^] | 116 | if *depsFile != "" { |
| 117 | err := deptools.WriteDepFile(*depsFile, *outputFile, deps) |
| 118 | if err != nil { |
| 119 | fmt.Fprintf(os.Stderr, "could not write deps to %q: %s\n", *depsFile, err) |
| 120 | os.Exit(1) |
| 121 | } |
| 122 | } |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 123 | os.Exit(0) |
| 124 | } |
| 125 | |
| 126 | // textNotice implements the textNotice utility. |
| 127 | func textNotice(ctx *context, files ...string) error { |
| 128 | // Must be at least one root file. |
| 129 | if len(files) < 1 { |
| 130 | return failNoneRequested |
| 131 | } |
| 132 | |
| 133 | // Read the license graph from the license metadata files (*.meta_lic). |
| 134 | licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files) |
| 135 | if err != nil { |
| 136 | return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err) |
| 137 | } |
| 138 | if licenseGraph == nil { |
| 139 | return failNoLicenses |
| 140 | } |
| 141 | |
| 142 | // rs contains all notice resolutions. |
| 143 | rs := compliance.ResolveNotices(licenseGraph) |
| 144 | |
| 145 | ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs) |
| 146 | if err != nil { |
| 147 | return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err) |
| 148 | } |
| 149 | |
| 150 | for h := range ni.Hashes() { |
| 151 | fmt.Fprintln(ctx.stdout, "==============================================================================") |
| 152 | for _, libName := range ni.HashLibs(h) { |
| 153 | fmt.Fprintf(ctx.stdout, "%s used by:\n", libName) |
| 154 | for _, installPath := range ni.HashLibInstalls(h, libName) { |
| 155 | if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) { |
| 156 | fmt.Fprintf(ctx.stdout, " %s\n", installPath[len(ctx.stripPrefix):]) |
| 157 | } else { |
| 158 | fmt.Fprintf(ctx.stdout, " %s\n", installPath) |
| 159 | } |
| 160 | } |
| 161 | fmt.Fprintln(ctx.stdout) |
| 162 | } |
| 163 | ctx.stdout.Write(ni.HashText(h)) |
| 164 | fmt.Fprintln(ctx.stdout) |
| 165 | } |
Colin Cross | bb45f8c | 2022-01-28 15:18:19 -0800 | [diff] [blame^] | 166 | |
| 167 | *ctx.deps = ni.InputNoticeFiles() |
| 168 | |
Bob Badour | e6fdd14 | 2021-12-09 22:10:43 -0800 | [diff] [blame] | 169 | return nil |
| 170 | } |