Bob Badour | f879224 | 2022-02-01 11:54:20 -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" |
| 19 | "compress/gzip" |
| 20 | "encoding/xml" |
| 21 | "flag" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "io/fs" |
| 25 | "os" |
| 26 | "path/filepath" |
| 27 | "strings" |
| 28 | |
| 29 | "android/soong/tools/compliance" |
| 30 | |
| 31 | "github.com/google/blueprint/deptools" |
| 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | outputFile = flag.String("o", "-", "Where to write the NOTICE xml or xml.gz file. (default stdout)") |
| 36 | depsFile = flag.String("d", "", "Where to write the deps file") |
| 37 | stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root") |
| 38 | |
| 39 | failNoneRequested = fmt.Errorf("\nNo license metadata files requested") |
| 40 | failNoLicenses = fmt.Errorf("No licenses found") |
| 41 | ) |
| 42 | |
| 43 | type context struct { |
| 44 | stdout io.Writer |
| 45 | stderr io.Writer |
| 46 | rootFS fs.FS |
| 47 | stripPrefix string |
| 48 | deps *[]string |
| 49 | } |
| 50 | |
| 51 | func init() { |
| 52 | flag.Usage = func() { |
| 53 | fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...} |
| 54 | |
| 55 | Outputs an xml NOTICE.xml or gzipped NOTICE.xml.gz file if the -o filename ends |
| 56 | with ".gz". |
| 57 | |
| 58 | Options: |
| 59 | `, filepath.Base(os.Args[0])) |
| 60 | flag.PrintDefaults() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func main() { |
| 65 | flag.Parse() |
| 66 | |
| 67 | // Must specify at least one root target. |
| 68 | if flag.NArg() == 0 { |
| 69 | flag.Usage() |
| 70 | os.Exit(2) |
| 71 | } |
| 72 | |
| 73 | if len(*outputFile) == 0 { |
| 74 | flag.Usage() |
| 75 | fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n") |
| 76 | os.Exit(2) |
| 77 | } else { |
| 78 | dir, err := filepath.Abs(filepath.Dir(*outputFile)) |
| 79 | if err != nil { |
| 80 | fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err) |
| 81 | os.Exit(1) |
| 82 | } |
| 83 | fi, err := os.Stat(dir) |
| 84 | if err != nil { |
| 85 | fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err) |
| 86 | os.Exit(1) |
| 87 | } |
| 88 | if !fi.IsDir() { |
| 89 | fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile) |
| 90 | os.Exit(1) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | var ofile io.Writer |
| 95 | var closer io.Closer |
| 96 | ofile = os.Stdout |
| 97 | var obuf *bytes.Buffer |
| 98 | if *outputFile != "-" { |
| 99 | obuf = &bytes.Buffer{} |
| 100 | ofile = obuf |
| 101 | } |
| 102 | if strings.HasSuffix(*outputFile, ".gz") { |
| 103 | ofile, _ = gzip.NewWriterLevel(obuf, gzip.BestCompression) |
| 104 | closer = ofile.(io.Closer) |
| 105 | } |
| 106 | |
| 107 | var deps []string |
| 108 | |
| 109 | ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, &deps} |
| 110 | |
| 111 | err := xmlNotice(ctx, flag.Args()...) |
| 112 | if err != nil { |
| 113 | if err == failNoneRequested { |
| 114 | flag.Usage() |
| 115 | } |
| 116 | fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 117 | os.Exit(1) |
| 118 | } |
| 119 | if closer != nil { |
| 120 | closer.Close() |
| 121 | } |
| 122 | |
| 123 | if *outputFile != "-" { |
| 124 | err := os.WriteFile(*outputFile, obuf.Bytes(), 0666) |
| 125 | if err != nil { |
| 126 | fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err) |
| 127 | os.Exit(1) |
| 128 | } |
| 129 | } |
| 130 | if *depsFile != "" { |
| 131 | err := deptools.WriteDepFile(*depsFile, *outputFile, deps) |
| 132 | if err != nil { |
| 133 | fmt.Fprintf(os.Stderr, "could not write deps to %q: %s\n", *depsFile, err) |
| 134 | os.Exit(1) |
| 135 | } |
| 136 | } |
| 137 | os.Exit(0) |
| 138 | } |
| 139 | |
| 140 | // xmlNotice implements the xmlnotice utility. |
| 141 | func xmlNotice(ctx *context, files ...string) error { |
| 142 | // Must be at least one root file. |
| 143 | if len(files) < 1 { |
| 144 | return failNoneRequested |
| 145 | } |
| 146 | |
| 147 | // Read the license graph from the license metadata files (*.meta_lic). |
| 148 | licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files) |
| 149 | if err != nil { |
| 150 | return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err) |
| 151 | } |
| 152 | if licenseGraph == nil { |
| 153 | return failNoLicenses |
| 154 | } |
| 155 | |
| 156 | // rs contains all notice resolutions. |
| 157 | rs := compliance.ResolveNotices(licenseGraph) |
| 158 | |
| 159 | ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs) |
| 160 | if err != nil { |
| 161 | return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err) |
| 162 | } |
| 163 | |
| 164 | fmt.Fprintln(ctx.stdout, "<?xml version=\"1.0\" encoding=\"utf-8\"?>") |
| 165 | fmt.Fprintln(ctx.stdout, "<licenses>") |
| 166 | |
| 167 | for installPath := range ni.InstallPaths() { |
| 168 | var p string |
| 169 | if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) { |
| 170 | p = installPath[len(ctx.stripPrefix):] |
| 171 | if 0 == len(p) { |
| 172 | p = "root" |
| 173 | } |
| 174 | } else { |
| 175 | p = installPath |
| 176 | } |
| 177 | for _, h := range ni.InstallHashes(installPath) { |
| 178 | for _, lib := range ni.InstallHashLibs(installPath, h) { |
| 179 | fmt.Fprintf(ctx.stdout, "<file-name contentId=\"%s\" lib=\"", h.String()) |
| 180 | xml.EscapeText(ctx.stdout, []byte(lib)) |
| 181 | fmt.Fprintf(ctx.stdout, "\">") |
| 182 | xml.EscapeText(ctx.stdout, []byte(p)) |
| 183 | fmt.Fprintln(ctx.stdout, "</file-name>") |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | for h := range ni.Hashes() { |
| 188 | fmt.Fprintf(ctx.stdout, "<file-content contentId=\"%s\"><![CDATA[", h) |
| 189 | xml.EscapeText(ctx.stdout, ni.HashText(h)) |
| 190 | fmt.Fprintf(ctx.stdout, "]]></file-content>\n\n") |
| 191 | } |
| 192 | fmt.Fprintln(ctx.stdout, "</licenses>") |
| 193 | |
| 194 | *ctx.deps = ni.InputNoticeFiles() |
| 195 | |
| 196 | return nil |
| 197 | } |