blob: 7f4038b6c5f7d2797c9b53a4fbdd4629d58a78c6 [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 (
Bob Badour6dd00352021-10-01 15:21:58 -070018 "flag"
19 "fmt"
20 "io"
Bob Badourc778e4c2022-03-22 13:05:19 -070021 "io/fs"
Bob Badour6dd00352021-10-01 15:21:58 -070022 "os"
23 "path/filepath"
24 "sort"
Bob Badour103eb0f2022-01-10 13:50:57 -080025 "strings"
Colin Cross38a61932022-01-27 15:26:49 -080026
27 "android/soong/tools/compliance"
Bob Badour6dd00352021-10-01 15:21:58 -070028)
29
30func init() {
31 flag.Usage = func() {
32 fmt.Fprintf(os.Stderr, `Usage: %s file.meta_lic {file.meta_lic...}
33
34Outputs a csv file with 1 project per line in the first field followed
35by target:condition pairs describing why the project must be shared.
36
37Each target is the path to a generated license metadata file for a
38Soong module or Make target, and the license condition is either
39restricted (e.g. GPL) or reciprocal (e.g. MPL).
40`, filepath.Base(os.Args[0]))
41 }
42}
43
44var (
45 failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
Colin Cross35f79c32022-01-27 15:18:52 -080046 failNoLicenses = fmt.Errorf("No licenses found")
Bob Badour6dd00352021-10-01 15:21:58 -070047)
48
49func main() {
50 flag.Parse()
51
52 // Must specify at least one root target.
53 if flag.NArg() == 0 {
54 flag.Usage()
55 os.Exit(2)
56 }
57
Bob Badourc778e4c2022-03-22 13:05:19 -070058 err := listShare(os.Stdout, os.Stderr, compliance.FS, flag.Args()...)
Bob Badour6dd00352021-10-01 15:21:58 -070059 if err != nil {
60 if err == failNoneRequested {
61 flag.Usage()
62 }
63 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
64 os.Exit(1)
65 }
66 os.Exit(0)
67}
68
69// listShare implements the listshare utility.
Bob Badourc778e4c2022-03-22 13:05:19 -070070func listShare(stdout, stderr io.Writer, rootFS fs.FS, files ...string) error {
Bob Badour6dd00352021-10-01 15:21:58 -070071 // Must be at least one root file.
72 if len(files) < 1 {
73 return failNoneRequested
74 }
75
76 // Read the license graph from the license metadata files (*.meta_lic).
Bob Badourc778e4c2022-03-22 13:05:19 -070077 licenseGraph, err := compliance.ReadLicenseGraph(rootFS, stderr, files)
Bob Badour6dd00352021-10-01 15:21:58 -070078 if err != nil {
79 return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
80 }
81 if licenseGraph == nil {
82 return failNoLicenses
83 }
84
85 // shareSource contains all source-sharing resolutions.
86 shareSource := compliance.ResolveSourceSharing(licenseGraph)
87
88 // Group the resolutions by project.
Bob Badour103eb0f2022-01-10 13:50:57 -080089 presolution := make(map[string]compliance.LicenseConditionSet)
Bob Badour6dd00352021-10-01 15:21:58 -070090 for _, target := range shareSource.AttachesTo() {
91 rl := shareSource.Resolutions(target)
92 sort.Sort(rl)
93 for _, r := range rl {
94 for _, p := range r.ActsOn().Projects() {
95 if _, ok := presolution[p]; !ok {
Bob Badour103eb0f2022-01-10 13:50:57 -080096 presolution[p] = r.Resolves()
Bob Badour6dd00352021-10-01 15:21:58 -070097 continue
98 }
Bob Badour103eb0f2022-01-10 13:50:57 -080099 presolution[p] = presolution[p].Union(r.Resolves())
Bob Badour6dd00352021-10-01 15:21:58 -0700100 }
101 }
102 }
103
104 // Sort the projects for repeatability/stability.
105 projects := make([]string, 0, len(presolution))
106 for p := range presolution {
107 projects = append(projects, p)
108 }
109 sort.Strings(projects)
110
111 // Output the sorted projects and the source-sharing license conditions that each project resolves.
112 for _, p := range projects {
Bob Badour103eb0f2022-01-10 13:50:57 -0800113 if presolution[p].IsEmpty() {
114 fmt.Fprintf(stdout, "%s\n", p)
115 } else {
116 fmt.Fprintf(stdout, "%s,%s\n", p, strings.Join(presolution[p].Names(), ","))
Bob Badour6dd00352021-10-01 15:21:58 -0700117 }
Bob Badour6dd00352021-10-01 15:21:58 -0700118 }
119
120 return nil
121}