xref: /aosp_15_r20/tools/treble/build/treble_build/cmd/paths.go (revision 105f628577ac4ba0e277a494fbb614ed8c12a994)
1// Copyright 2022 The Android Open Source Project
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	"context"
19	"fmt"
20	"io"
21	"log"
22
23	"tools/treble/build/report/report"
24)
25
26type pathsReport struct {
27	build_target string // Target used to filter build request
28	single       bool   // Get single path
29}
30
31func (p pathsReport) Run(ctx context.Context, rtx *report.Context, rsp *response) error {
32
33	log.Printf("Resolving paths for  %s (single : %v)\n", rsp.Inputs, p.single)
34	rsp.Paths = report.RunPaths(ctx, rtx, p.build_target, p.single, rsp.Inputs)
35
36	//  The path is returned in an array in the form [build_target, path_stop1,...,path_stopN,source_file]
37	//  Choose the closest build target (path_stopN) to the source file to build to reduce the amount that
38	//  is built.
39	const buildPathIndex = 2
40	build_targets := make(map[string]bool)
41	for _, path := range rsp.Paths {
42		// Default to build closest build target
43		if len(path.Paths) > buildPathIndex {
44			build_targets[path.Paths[len(path.Paths)-buildPathIndex]] = true
45		}
46	}
47	for b := range build_targets {
48		rsp.Targets = append(rsp.Targets, b)
49	}
50
51	return nil
52}
53
54func (h *pathsReport) PrintText(w io.Writer, rsp *response, verbose bool) {
55	if len(rsp.Paths) > 0 {
56		fmt.Fprintln(w, "  Paths")
57		for _, p := range rsp.Paths {
58			// Provide path from target to dependency with the
59			// path length, since target and dependency are in the
60			// path subtract them out from length
61			fmt.Fprintf(w, "      %s..(%d)..%-s\n", p.Target, len(p.Paths)-2, p.Dependency)
62		}
63	}
64}
65