xref: /aosp_15_r20/build/make/tools/compliance/cmd/rtrace/rtrace.go (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1*9e94795aSAndroid Build Coastguard Worker// Copyright 2021 Google LLC
2*9e94795aSAndroid Build Coastguard Worker//
3*9e94795aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*9e94795aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*9e94795aSAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*9e94795aSAndroid Build Coastguard Worker//
7*9e94795aSAndroid Build Coastguard Worker//      http://www.apache.org/licenses/LICENSE-2.0
8*9e94795aSAndroid Build Coastguard Worker//
9*9e94795aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*9e94795aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*9e94795aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9e94795aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*9e94795aSAndroid Build Coastguard Worker// limitations under the License.
14*9e94795aSAndroid Build Coastguard Worker
15*9e94795aSAndroid Build Coastguard Workerpackage main
16*9e94795aSAndroid Build Coastguard Worker
17*9e94795aSAndroid Build Coastguard Workerimport (
18*9e94795aSAndroid Build Coastguard Worker	"bytes"
19*9e94795aSAndroid Build Coastguard Worker	"flag"
20*9e94795aSAndroid Build Coastguard Worker	"fmt"
21*9e94795aSAndroid Build Coastguard Worker	"io"
22*9e94795aSAndroid Build Coastguard Worker	"io/fs"
23*9e94795aSAndroid Build Coastguard Worker	"os"
24*9e94795aSAndroid Build Coastguard Worker	"path/filepath"
25*9e94795aSAndroid Build Coastguard Worker	"sort"
26*9e94795aSAndroid Build Coastguard Worker	"strings"
27*9e94795aSAndroid Build Coastguard Worker
28*9e94795aSAndroid Build Coastguard Worker	"android/soong/response"
29*9e94795aSAndroid Build Coastguard Worker	"android/soong/tools/compliance"
30*9e94795aSAndroid Build Coastguard Worker)
31*9e94795aSAndroid Build Coastguard Worker
32*9e94795aSAndroid Build Coastguard Workervar (
33*9e94795aSAndroid Build Coastguard Worker	failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
34*9e94795aSAndroid Build Coastguard Worker	failNoSources     = fmt.Errorf("\nNo projects or metadata files to trace back from")
35*9e94795aSAndroid Build Coastguard Worker	failNoLicenses    = fmt.Errorf("No licenses found")
36*9e94795aSAndroid Build Coastguard Worker)
37*9e94795aSAndroid Build Coastguard Worker
38*9e94795aSAndroid Build Coastguard Workertype context struct {
39*9e94795aSAndroid Build Coastguard Worker	sources     []string
40*9e94795aSAndroid Build Coastguard Worker	stripPrefix []string
41*9e94795aSAndroid Build Coastguard Worker}
42*9e94795aSAndroid Build Coastguard Worker
43*9e94795aSAndroid Build Coastguard Workerfunc (ctx context) strip(installPath string) string {
44*9e94795aSAndroid Build Coastguard Worker	for _, prefix := range ctx.stripPrefix {
45*9e94795aSAndroid Build Coastguard Worker		if strings.HasPrefix(installPath, prefix) {
46*9e94795aSAndroid Build Coastguard Worker			p := strings.TrimPrefix(installPath, prefix)
47*9e94795aSAndroid Build Coastguard Worker			if 0 == len(p) {
48*9e94795aSAndroid Build Coastguard Worker				continue
49*9e94795aSAndroid Build Coastguard Worker			}
50*9e94795aSAndroid Build Coastguard Worker			return p
51*9e94795aSAndroid Build Coastguard Worker		}
52*9e94795aSAndroid Build Coastguard Worker	}
53*9e94795aSAndroid Build Coastguard Worker	return installPath
54*9e94795aSAndroid Build Coastguard Worker}
55*9e94795aSAndroid Build Coastguard Worker
56*9e94795aSAndroid Build Coastguard Worker// newMultiString creates a flag that allows multiple values in an array.
57*9e94795aSAndroid Build Coastguard Workerfunc newMultiString(flags *flag.FlagSet, name, usage string) *multiString {
58*9e94795aSAndroid Build Coastguard Worker	var f multiString
59*9e94795aSAndroid Build Coastguard Worker	flags.Var(&f, name, usage)
60*9e94795aSAndroid Build Coastguard Worker	return &f
61*9e94795aSAndroid Build Coastguard Worker}
62*9e94795aSAndroid Build Coastguard Worker
63*9e94795aSAndroid Build Coastguard Worker// multiString implements the flag `Value` interface for multiple strings.
64*9e94795aSAndroid Build Coastguard Workertype multiString []string
65*9e94795aSAndroid Build Coastguard Worker
66*9e94795aSAndroid Build Coastguard Workerfunc (ms *multiString) String() string     { return strings.Join(*ms, ", ") }
67*9e94795aSAndroid Build Coastguard Workerfunc (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
68*9e94795aSAndroid Build Coastguard Worker
69*9e94795aSAndroid Build Coastguard Workerfunc main() {
70*9e94795aSAndroid Build Coastguard Worker	var expandedArgs []string
71*9e94795aSAndroid Build Coastguard Worker	for _, arg := range os.Args[1:] {
72*9e94795aSAndroid Build Coastguard Worker		if strings.HasPrefix(arg, "@") {
73*9e94795aSAndroid Build Coastguard Worker			f, err := os.Open(strings.TrimPrefix(arg, "@"))
74*9e94795aSAndroid Build Coastguard Worker			if err != nil {
75*9e94795aSAndroid Build Coastguard Worker				fmt.Fprintln(os.Stderr, err.Error())
76*9e94795aSAndroid Build Coastguard Worker				os.Exit(1)
77*9e94795aSAndroid Build Coastguard Worker			}
78*9e94795aSAndroid Build Coastguard Worker
79*9e94795aSAndroid Build Coastguard Worker			respArgs, err := response.ReadRspFile(f)
80*9e94795aSAndroid Build Coastguard Worker			f.Close()
81*9e94795aSAndroid Build Coastguard Worker			if err != nil {
82*9e94795aSAndroid Build Coastguard Worker				fmt.Fprintln(os.Stderr, err.Error())
83*9e94795aSAndroid Build Coastguard Worker				os.Exit(1)
84*9e94795aSAndroid Build Coastguard Worker			}
85*9e94795aSAndroid Build Coastguard Worker			expandedArgs = append(expandedArgs, respArgs...)
86*9e94795aSAndroid Build Coastguard Worker		} else {
87*9e94795aSAndroid Build Coastguard Worker			expandedArgs = append(expandedArgs, arg)
88*9e94795aSAndroid Build Coastguard Worker		}
89*9e94795aSAndroid Build Coastguard Worker	}
90*9e94795aSAndroid Build Coastguard Worker
91*9e94795aSAndroid Build Coastguard Worker	flags := flag.NewFlagSet("flags", flag.ExitOnError)
92*9e94795aSAndroid Build Coastguard Worker
93*9e94795aSAndroid Build Coastguard Worker	flags.Usage = func() {
94*9e94795aSAndroid Build Coastguard Worker		fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
95*9e94795aSAndroid Build Coastguard Worker
96*9e94795aSAndroid Build Coastguard WorkerCalculates the source-sharing requirements in reverse starting at the
97*9e94795aSAndroid Build Coastguard Worker-rtrace projects or metadata files that inherited source-sharing and
98*9e94795aSAndroid Build Coastguard Workerworking back to the targets where the source-sharing requirmements
99*9e94795aSAndroid Build Coastguard Workeroriginate.
100*9e94795aSAndroid Build Coastguard Worker
101*9e94795aSAndroid Build Coastguard WorkerOutputs a space-separated pair where the first field is an originating
102*9e94795aSAndroid Build Coastguard Workertarget with one or more restricted conditions and where the second
103*9e94795aSAndroid Build Coastguard Workerfield is a colon-separated list of the restricted conditions.
104*9e94795aSAndroid Build Coastguard Worker
105*9e94795aSAndroid Build Coastguard WorkerOutputs a count of the originating targets, and if the count is zero,
106*9e94795aSAndroid Build Coastguard Workeroutputs a warning to check the -rtrace projects and/or filenames.
107*9e94795aSAndroid Build Coastguard Worker
108*9e94795aSAndroid Build Coastguard WorkerOptions:
109*9e94795aSAndroid Build Coastguard Worker`, filepath.Base(os.Args[0]))
110*9e94795aSAndroid Build Coastguard Worker		flags.PrintDefaults()
111*9e94795aSAndroid Build Coastguard Worker	}
112*9e94795aSAndroid Build Coastguard Worker
113*9e94795aSAndroid Build Coastguard Worker	outputFile := flags.String("o", "-", "Where to write the output. (default stdout)")
114*9e94795aSAndroid Build Coastguard Worker	sources := newMultiString(flags, "rtrace", "Projects or metadata files to trace back from. (required; multiple allowed)")
115*9e94795aSAndroid Build Coastguard Worker	stripPrefix := newMultiString(flags, "strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
116*9e94795aSAndroid Build Coastguard Worker
117*9e94795aSAndroid Build Coastguard Worker	flags.Parse(expandedArgs)
118*9e94795aSAndroid Build Coastguard Worker
119*9e94795aSAndroid Build Coastguard Worker	// Must specify at least one root target.
120*9e94795aSAndroid Build Coastguard Worker	if flags.NArg() == 0 {
121*9e94795aSAndroid Build Coastguard Worker		flags.Usage()
122*9e94795aSAndroid Build Coastguard Worker		os.Exit(2)
123*9e94795aSAndroid Build Coastguard Worker	}
124*9e94795aSAndroid Build Coastguard Worker
125*9e94795aSAndroid Build Coastguard Worker	if len(*sources) == 0 {
126*9e94795aSAndroid Build Coastguard Worker		flags.Usage()
127*9e94795aSAndroid Build Coastguard Worker		fmt.Fprintf(os.Stderr, "\nMust specify at least 1 --rtrace source.\n")
128*9e94795aSAndroid Build Coastguard Worker		os.Exit(2)
129*9e94795aSAndroid Build Coastguard Worker	}
130*9e94795aSAndroid Build Coastguard Worker
131*9e94795aSAndroid Build Coastguard Worker	if len(*outputFile) == 0 {
132*9e94795aSAndroid Build Coastguard Worker		flags.Usage()
133*9e94795aSAndroid Build Coastguard Worker		fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n")
134*9e94795aSAndroid Build Coastguard Worker		os.Exit(2)
135*9e94795aSAndroid Build Coastguard Worker	} else {
136*9e94795aSAndroid Build Coastguard Worker		dir, err := filepath.Abs(filepath.Dir(*outputFile))
137*9e94795aSAndroid Build Coastguard Worker		if err != nil {
138*9e94795aSAndroid Build Coastguard Worker			fmt.Fprintf(os.Stderr, "cannot determine path to %q: %s\n", *outputFile, err)
139*9e94795aSAndroid Build Coastguard Worker			os.Exit(1)
140*9e94795aSAndroid Build Coastguard Worker		}
141*9e94795aSAndroid Build Coastguard Worker		fi, err := os.Stat(dir)
142*9e94795aSAndroid Build Coastguard Worker		if err != nil {
143*9e94795aSAndroid Build Coastguard Worker			fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %s\n", dir, *outputFile, err)
144*9e94795aSAndroid Build Coastguard Worker			os.Exit(1)
145*9e94795aSAndroid Build Coastguard Worker		}
146*9e94795aSAndroid Build Coastguard Worker		if !fi.IsDir() {
147*9e94795aSAndroid Build Coastguard Worker			fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile)
148*9e94795aSAndroid Build Coastguard Worker			os.Exit(1)
149*9e94795aSAndroid Build Coastguard Worker		}
150*9e94795aSAndroid Build Coastguard Worker	}
151*9e94795aSAndroid Build Coastguard Worker
152*9e94795aSAndroid Build Coastguard Worker	var ofile io.Writer
153*9e94795aSAndroid Build Coastguard Worker	ofile = os.Stdout
154*9e94795aSAndroid Build Coastguard Worker	var obuf *bytes.Buffer
155*9e94795aSAndroid Build Coastguard Worker	if *outputFile != "-" {
156*9e94795aSAndroid Build Coastguard Worker		obuf = &bytes.Buffer{}
157*9e94795aSAndroid Build Coastguard Worker		ofile = obuf
158*9e94795aSAndroid Build Coastguard Worker	}
159*9e94795aSAndroid Build Coastguard Worker
160*9e94795aSAndroid Build Coastguard Worker	ctx := &context{
161*9e94795aSAndroid Build Coastguard Worker		sources:     *sources,
162*9e94795aSAndroid Build Coastguard Worker		stripPrefix: *stripPrefix,
163*9e94795aSAndroid Build Coastguard Worker	}
164*9e94795aSAndroid Build Coastguard Worker	_, err := traceRestricted(ctx, ofile, os.Stderr, compliance.FS, flags.Args()...)
165*9e94795aSAndroid Build Coastguard Worker	if err != nil {
166*9e94795aSAndroid Build Coastguard Worker		if err == failNoneRequested {
167*9e94795aSAndroid Build Coastguard Worker			flags.Usage()
168*9e94795aSAndroid Build Coastguard Worker		}
169*9e94795aSAndroid Build Coastguard Worker		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
170*9e94795aSAndroid Build Coastguard Worker		os.Exit(1)
171*9e94795aSAndroid Build Coastguard Worker	}
172*9e94795aSAndroid Build Coastguard Worker	if *outputFile != "-" {
173*9e94795aSAndroid Build Coastguard Worker		err := os.WriteFile(*outputFile, obuf.Bytes(), 0666)
174*9e94795aSAndroid Build Coastguard Worker		if err != nil {
175*9e94795aSAndroid Build Coastguard Worker			fmt.Fprintf(os.Stderr, "could not write output to %q from %q: %s\n", *outputFile, os.Getenv("PWD"), err)
176*9e94795aSAndroid Build Coastguard Worker			os.Exit(1)
177*9e94795aSAndroid Build Coastguard Worker		}
178*9e94795aSAndroid Build Coastguard Worker	}
179*9e94795aSAndroid Build Coastguard Worker	os.Exit(0)
180*9e94795aSAndroid Build Coastguard Worker}
181*9e94795aSAndroid Build Coastguard Worker
182*9e94795aSAndroid Build Coastguard Worker// traceRestricted implements the rtrace utility.
183*9e94795aSAndroid Build Coastguard Workerfunc traceRestricted(ctx *context, stdout, stderr io.Writer, rootFS fs.FS, files ...string) (*compliance.LicenseGraph, error) {
184*9e94795aSAndroid Build Coastguard Worker	if len(files) < 1 {
185*9e94795aSAndroid Build Coastguard Worker		return nil, failNoneRequested
186*9e94795aSAndroid Build Coastguard Worker	}
187*9e94795aSAndroid Build Coastguard Worker
188*9e94795aSAndroid Build Coastguard Worker	if len(ctx.sources) < 1 {
189*9e94795aSAndroid Build Coastguard Worker		return nil, failNoSources
190*9e94795aSAndroid Build Coastguard Worker	}
191*9e94795aSAndroid Build Coastguard Worker
192*9e94795aSAndroid Build Coastguard Worker	// Read the license graph from the license metadata files (*.meta_lic).
193*9e94795aSAndroid Build Coastguard Worker	licenseGraph, err := compliance.ReadLicenseGraph(rootFS, stderr, files)
194*9e94795aSAndroid Build Coastguard Worker	if err != nil {
195*9e94795aSAndroid Build Coastguard Worker		return nil, fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
196*9e94795aSAndroid Build Coastguard Worker	}
197*9e94795aSAndroid Build Coastguard Worker	if licenseGraph == nil {
198*9e94795aSAndroid Build Coastguard Worker		return nil, failNoLicenses
199*9e94795aSAndroid Build Coastguard Worker	}
200*9e94795aSAndroid Build Coastguard Worker
201*9e94795aSAndroid Build Coastguard Worker	sourceMap := make(map[string]struct{})
202*9e94795aSAndroid Build Coastguard Worker	for _, source := range ctx.sources {
203*9e94795aSAndroid Build Coastguard Worker		sourceMap[source] = struct{}{}
204*9e94795aSAndroid Build Coastguard Worker	}
205*9e94795aSAndroid Build Coastguard Worker
206*9e94795aSAndroid Build Coastguard Worker	compliance.TraceTopDownConditions(licenseGraph, func(tn *compliance.TargetNode) compliance.LicenseConditionSet {
207*9e94795aSAndroid Build Coastguard Worker		if _, isPresent := sourceMap[tn.Name()]; isPresent {
208*9e94795aSAndroid Build Coastguard Worker			return compliance.ImpliesRestricted
209*9e94795aSAndroid Build Coastguard Worker		}
210*9e94795aSAndroid Build Coastguard Worker		for _, project := range tn.Projects() {
211*9e94795aSAndroid Build Coastguard Worker			if _, isPresent := sourceMap[project]; isPresent {
212*9e94795aSAndroid Build Coastguard Worker				return compliance.ImpliesRestricted
213*9e94795aSAndroid Build Coastguard Worker			}
214*9e94795aSAndroid Build Coastguard Worker		}
215*9e94795aSAndroid Build Coastguard Worker		return compliance.NewLicenseConditionSet()
216*9e94795aSAndroid Build Coastguard Worker	})
217*9e94795aSAndroid Build Coastguard Worker
218*9e94795aSAndroid Build Coastguard Worker	// targetOut calculates the string to output for `target` adding `sep`-separated conditions as needed.
219*9e94795aSAndroid Build Coastguard Worker	targetOut := func(target *compliance.TargetNode, sep string) string {
220*9e94795aSAndroid Build Coastguard Worker		tOut := ctx.strip(target.Name())
221*9e94795aSAndroid Build Coastguard Worker		return tOut
222*9e94795aSAndroid Build Coastguard Worker	}
223*9e94795aSAndroid Build Coastguard Worker
224*9e94795aSAndroid Build Coastguard Worker	// outputResolution prints a resolution in the requested format to `stdout`, where one can read
225*9e94795aSAndroid Build Coastguard Worker	// a resolution as `tname` resolves conditions named in `cnames`.
226*9e94795aSAndroid Build Coastguard Worker	// `tname` is the name of the target the resolution traces back to.
227*9e94795aSAndroid Build Coastguard Worker	// `cnames` is the list of conditions to resolve.
228*9e94795aSAndroid Build Coastguard Worker	outputResolution := func(tname string, cnames []string) {
229*9e94795aSAndroid Build Coastguard Worker		// ... one edge per line with names in a colon-separated tuple.
230*9e94795aSAndroid Build Coastguard Worker		fmt.Fprintf(stdout, "%s %s\n", tname, strings.Join(cnames, ":"))
231*9e94795aSAndroid Build Coastguard Worker	}
232*9e94795aSAndroid Build Coastguard Worker
233*9e94795aSAndroid Build Coastguard Worker	// Sort the resolutions by targetname for repeatability/stability.
234*9e94795aSAndroid Build Coastguard Worker	actions := compliance.WalkResolutionsForCondition(licenseGraph, compliance.ImpliesShared).AllActions()
235*9e94795aSAndroid Build Coastguard Worker	targets := make(compliance.TargetNodeList, 0, len(actions))
236*9e94795aSAndroid Build Coastguard Worker	for tn := range actions {
237*9e94795aSAndroid Build Coastguard Worker		if tn.LicenseConditions().MatchesAnySet(compliance.ImpliesRestricted) {
238*9e94795aSAndroid Build Coastguard Worker			targets = append(targets, tn)
239*9e94795aSAndroid Build Coastguard Worker		}
240*9e94795aSAndroid Build Coastguard Worker	}
241*9e94795aSAndroid Build Coastguard Worker	sort.Sort(targets)
242*9e94795aSAndroid Build Coastguard Worker
243*9e94795aSAndroid Build Coastguard Worker	// Output the sorted targets.
244*9e94795aSAndroid Build Coastguard Worker	for _, target := range targets {
245*9e94795aSAndroid Build Coastguard Worker		var tname string
246*9e94795aSAndroid Build Coastguard Worker		tname = targetOut(target, ":")
247*9e94795aSAndroid Build Coastguard Worker
248*9e94795aSAndroid Build Coastguard Worker		// cnames accumulates the list of condition names originating at a single origin that apply to `target`.
249*9e94795aSAndroid Build Coastguard Worker		cnames := target.LicenseConditions().Names()
250*9e94795aSAndroid Build Coastguard Worker
251*9e94795aSAndroid Build Coastguard Worker		// Output 1 line for each attachesTo+actsOn combination.
252*9e94795aSAndroid Build Coastguard Worker		outputResolution(tname, cnames)
253*9e94795aSAndroid Build Coastguard Worker	}
254*9e94795aSAndroid Build Coastguard Worker	fmt.Fprintf(stdout, "restricted conditions trace to %d targets\n", len(targets))
255*9e94795aSAndroid Build Coastguard Worker	if 0 == len(targets) {
256*9e94795aSAndroid Build Coastguard Worker		fmt.Fprintln(stdout, "  (check for typos in project names or metadata files)")
257*9e94795aSAndroid Build Coastguard Worker	}
258*9e94795aSAndroid Build Coastguard Worker	return licenseGraph, nil
259*9e94795aSAndroid Build Coastguard Worker}
260