Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [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 ( |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 18 | "bytes" |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 19 | "flag" |
| 20 | "fmt" |
| 21 | "io" |
Bob Badour | c778e4c | 2022-03-22 13:05:19 -0700 | [diff] [blame] | 22 | "io/fs" |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 23 | "os" |
| 24 | "path/filepath" |
| 25 | "sort" |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 26 | "strings" |
Colin Cross | 38a6193 | 2022-01-27 15:26:49 -0800 | [diff] [blame] | 27 | |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 28 | "android/soong/response" |
Colin Cross | 38a6193 | 2022-01-27 15:26:49 -0800 | [diff] [blame] | 29 | "android/soong/tools/compliance" |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 32 | var ( |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 33 | failConflicts = fmt.Errorf("conflicts") |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 34 | failNoneRequested = fmt.Errorf("\nNo metadata files requested") |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 35 | failNoLicenses = fmt.Errorf("No licenses") |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 38 | // byError orders conflicts by error string |
| 39 | type byError []compliance.SourceSharePrivacyConflict |
| 40 | |
| 41 | func (l byError) Len() int { return len(l) } |
| 42 | func (l byError) Swap(i, j int) { l[i], l[j] = l[j], l[i] } |
| 43 | func (l byError) Less(i, j int) bool { return l[i].Error() < l[j].Error() } |
| 44 | |
| 45 | func main() { |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 46 | var expandedArgs []string |
| 47 | for _, arg := range os.Args[1:] { |
| 48 | if strings.HasPrefix(arg, "@") { |
| 49 | f, err := os.Open(strings.TrimPrefix(arg, "@")) |
| 50 | if err != nil { |
| 51 | fmt.Fprintln(os.Stderr, err.Error()) |
| 52 | os.Exit(1) |
| 53 | } |
| 54 | |
| 55 | respArgs, err := response.ReadRspFile(f) |
| 56 | f.Close() |
| 57 | if err != nil { |
| 58 | fmt.Fprintln(os.Stderr, err.Error()) |
| 59 | os.Exit(1) |
| 60 | } |
| 61 | expandedArgs = append(expandedArgs, respArgs...) |
| 62 | } else { |
| 63 | expandedArgs = append(expandedArgs, arg) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | flags := flag.NewFlagSet("flags", flag.ExitOnError) |
| 68 | |
| 69 | flags.Usage = func() { |
| 70 | fmt.Fprintf(os.Stderr, `Usage: %s {-o outfile} file.meta_lic {file.meta_lic...} |
| 71 | |
| 72 | Reports on stderr any targets where policy says that the source both |
| 73 | must and must not be shared. The error report indicates the target, the |
| 74 | license condition that has a source privacy policy, and the license |
| 75 | condition that has a source sharing policy. |
| 76 | |
| 77 | Any given target may appear multiple times with different combinations |
| 78 | of conflicting license conditions. |
| 79 | |
| 80 | If all the source code that policy says must be shared may be shared, |
| 81 | outputs "PASS" to stdout and exits with status 0. |
| 82 | |
| 83 | If policy says any source must both be shared and not be shared, |
| 84 | outputs "FAIL" to stdout and exits with status 1. |
| 85 | `, filepath.Base(os.Args[0])) |
| 86 | flags.PrintDefaults() |
| 87 | } |
| 88 | |
| 89 | outputFile := flags.String("o", "-", "Where to write the output. (default stdout)") |
| 90 | |
| 91 | flags.Parse(expandedArgs) |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 92 | |
| 93 | // Must specify at least one root target. |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 94 | if flags.NArg() == 0 { |
| 95 | flags.Usage() |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 96 | os.Exit(2) |
| 97 | } |
| 98 | |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 99 | if len(*outputFile) == 0 { |
| 100 | flags.Usage() |
| 101 | fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n") |
| 102 | os.Exit(2) |
| 103 | } else { |
| 104 | dir, err := filepath.Abs(filepath.Dir(*outputFile)) |
| 105 | if err != nil { |
| 106 | fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err) |
| 107 | os.Exit(1) |
| 108 | } |
| 109 | fi, err := os.Stat(dir) |
| 110 | if err != nil { |
| 111 | fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err) |
| 112 | os.Exit(1) |
| 113 | } |
| 114 | if !fi.IsDir() { |
| 115 | fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile) |
| 116 | os.Exit(1) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | var ofile io.Writer |
| 121 | ofile = os.Stdout |
| 122 | var obuf *bytes.Buffer |
| 123 | if *outputFile != "-" { |
| 124 | obuf = &bytes.Buffer{} |
| 125 | ofile = obuf |
| 126 | } |
| 127 | |
| 128 | err := checkShare(ofile, os.Stderr, compliance.FS, flags.Args()...) |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 129 | if err != nil { |
| 130 | if err != failConflicts { |
| 131 | if err == failNoneRequested { |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 132 | flags.Usage() |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 133 | } |
| 134 | fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 135 | } |
| 136 | os.Exit(1) |
| 137 | } |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 138 | if *outputFile != "-" { |
| 139 | err := os.WriteFile(*outputFile, obuf.Bytes(), 0666) |
| 140 | if err != nil { |
| 141 | fmt.Fprintf(os.Stderr, "could not write output to %q from %q: %s\n", *outputFile, os.Getenv("PWD"), err) |
| 142 | os.Exit(1) |
| 143 | } |
| 144 | } |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 145 | os.Exit(0) |
| 146 | } |
| 147 | |
| 148 | // checkShare implements the checkshare utility. |
Bob Badour | c778e4c | 2022-03-22 13:05:19 -0700 | [diff] [blame] | 149 | func checkShare(stdout, stderr io.Writer, rootFS fs.FS, files ...string) error { |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 150 | |
| 151 | if len(files) < 1 { |
| 152 | return failNoneRequested |
| 153 | } |
| 154 | |
| 155 | // Read the license graph from the license metadata files (*.meta_lic). |
Bob Badour | c778e4c | 2022-03-22 13:05:19 -0700 | [diff] [blame] | 156 | licenseGraph, err := compliance.ReadLicenseGraph(rootFS, stderr, files) |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 157 | if err != nil { |
Bob Badour | 986a839 | 2022-06-03 10:42:27 -0700 | [diff] [blame] | 158 | return fmt.Errorf("Unable to read license metadata file(s) %q from %q: %w\n", files, os.Getenv("PWD"), err) |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 159 | } |
| 160 | if licenseGraph == nil { |
| 161 | return failNoLicenses |
| 162 | } |
| 163 | |
| 164 | // Apply policy to find conflicts and report them to stderr lexicographically ordered. |
| 165 | conflicts := compliance.ConflictingSharedPrivateSource(licenseGraph) |
| 166 | sort.Sort(byError(conflicts)) |
| 167 | for _, conflict := range conflicts { |
| 168 | fmt.Fprintln(stderr, conflict.Error()) |
| 169 | } |
| 170 | |
| 171 | // Indicate pass or fail on stdout. |
| 172 | if len(conflicts) > 0 { |
| 173 | fmt.Fprintln(stdout, "FAIL") |
| 174 | return failConflicts |
| 175 | } |
| 176 | fmt.Fprintln(stdout, "PASS") |
| 177 | return nil |
| 178 | } |