blob: eebb13de3f1ab539accfd125d6c2d03043d8432f [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 Badoure6fdd142021-12-09 22:10:43 -080019 "flag"
20 "fmt"
21 "io"
22 "io/fs"
23 "os"
24 "path/filepath"
25 "strings"
Colin Cross38a61932022-01-27 15:26:49 -080026
27 "android/soong/tools/compliance"
Colin Crossbb45f8c2022-01-28 15:18:19 -080028
29 "github.com/google/blueprint/deptools"
Bob Badoure6fdd142021-12-09 22:10:43 -080030)
31
32var (
33 outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
Colin Crossbb45f8c2022-01-28 15:18:19 -080034 depsFile = flag.String("d", "", "Where to write the deps file")
Bob Badoure6fdd142021-12-09 22:10:43 -080035 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
41type context struct {
42 stdout io.Writer
43 stderr io.Writer
44 rootFS fs.FS
45 stripPrefix string
Colin Crossbb45f8c2022-01-28 15:18:19 -080046 deps *[]string
Bob Badoure6fdd142021-12-09 22:10:43 -080047}
48
49func init() {
50 flag.Usage = func() {
51 fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
52
53Outputs a text NOTICE file.
54
55Options:
56`, filepath.Base(os.Args[0]))
57 flag.PrintDefaults()
58 }
59}
60
61func 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 Cross179ec3e2022-01-27 15:47:09 -080077 fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err)
Bob Badoure6fdd142021-12-09 22:10:43 -080078 os.Exit(1)
79 }
80 fi, err := os.Stat(dir)
81 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -080082 fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err)
Bob Badoure6fdd142021-12-09 22:10:43 -080083 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 Crossbb45f8c2022-01-28 15:18:19 -080097 var deps []string
98
99 ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, &deps}
Bob Badoure6fdd142021-12-09 22:10:43 -0800100
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 Cross179ec3e2022-01-27 15:47:09 -0800112 fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err)
Bob Badoure6fdd142021-12-09 22:10:43 -0800113 os.Exit(1)
114 }
115 }
Colin Crossbb45f8c2022-01-28 15:18:19 -0800116 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 Badoure6fdd142021-12-09 22:10:43 -0800123 os.Exit(0)
124}
125
126// textNotice implements the textNotice utility.
127func 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 Crossbb45f8c2022-01-28 15:18:19 -0800166
167 *ctx.deps = ni.InputNoticeFiles()
168
Bob Badoure6fdd142021-12-09 22:10:43 -0800169 return nil
170}