blob: bba23083003194426aad12528294a30e5067ca2e [file] [log] [blame]
Bob Badour6dd00352021-10-01 15:21:58 -07001// 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 "compliance"
19 "flag"
20 "fmt"
21 "io"
22 "os"
23 "path/filepath"
24 "sort"
25)
26
27func init() {
28 flag.Usage = func() {
29 fmt.Fprintf(os.Stderr, `Usage: %s file.meta_lic {file.meta_lic...}
30
31Outputs a csv file with 1 project per line in the first field followed
32by target:condition pairs describing why the project must be shared.
33
34Each target is the path to a generated license metadata file for a
35Soong module or Make target, and the license condition is either
36restricted (e.g. GPL) or reciprocal (e.g. MPL).
37`, filepath.Base(os.Args[0]))
38 }
39}
40
41var (
42 failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
43 failNoLicenses = fmt.Errorf("No licenses found")
44)
45
46func main() {
47 flag.Parse()
48
49 // Must specify at least one root target.
50 if flag.NArg() == 0 {
51 flag.Usage()
52 os.Exit(2)
53 }
54
55 err := listShare(os.Stdout, os.Stderr, flag.Args()...)
56 if err != nil {
57 if err == failNoneRequested {
58 flag.Usage()
59 }
60 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
61 os.Exit(1)
62 }
63 os.Exit(0)
64}
65
66// listShare implements the listshare utility.
67func listShare(stdout, stderr io.Writer, files ...string) error {
68 // Must be at least one root file.
69 if len(files) < 1 {
70 return failNoneRequested
71 }
72
73 // Read the license graph from the license metadata files (*.meta_lic).
74 licenseGraph, err := compliance.ReadLicenseGraph(os.DirFS("."), stderr, files)
75 if err != nil {
76 return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
77 }
78 if licenseGraph == nil {
79 return failNoLicenses
80 }
81
82 // shareSource contains all source-sharing resolutions.
83 shareSource := compliance.ResolveSourceSharing(licenseGraph)
84
85 // Group the resolutions by project.
86 presolution := make(map[string]*compliance.LicenseConditionSet)
87 for _, target := range shareSource.AttachesTo() {
88 rl := shareSource.Resolutions(target)
89 sort.Sort(rl)
90 for _, r := range rl {
91 for _, p := range r.ActsOn().Projects() {
92 if _, ok := presolution[p]; !ok {
93 presolution[p] = r.Resolves().Copy()
94 continue
95 }
96 presolution[p].AddSet(r.Resolves())
97 }
98 }
99 }
100
101 // Sort the projects for repeatability/stability.
102 projects := make([]string, 0, len(presolution))
103 for p := range presolution {
104 projects = append(projects, p)
105 }
106 sort.Strings(projects)
107
108 // Output the sorted projects and the source-sharing license conditions that each project resolves.
109 for _, p := range projects {
110 fmt.Fprintf(stdout, "%s", p)
111
112 // Sort the conditions for repeatability/stability.
113 conditions := presolution[p].AsList()
114 sort.Sort(conditions)
115
116 // Output the sorted origin:condition pairs.
117 for _, lc := range conditions {
118 fmt.Fprintf(stdout, ",%s:%s", lc.Origin().Name(), lc.Name())
119 }
120 fmt.Fprintf(stdout, "\n")
121 }
122
123 return nil
124}