xref: /aosp_15_r20/build/soong/cmd/find_input_delta/find_input_delta/main.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 Google Inc. All rights reserved.
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	"flag"
19	"os"
20	"strings"
21
22	fid_lib "android/soong/cmd/find_input_delta/find_input_delta_lib"
23)
24
25func main() {
26	var top string
27	var prior_state_file string
28	var new_state_file string
29	var target string
30	var inputs_file string
31	var template string
32	var inputs []string
33	var inspect bool
34	var err error
35
36	flag.StringVar(&top, "top", ".", "path to top of workspace")
37	flag.StringVar(&prior_state_file, "prior_state", "", "prior internal state file")
38	flag.StringVar(&new_state_file, "new_state", "", "new internal state file")
39	flag.StringVar(&target, "target", "", "name of ninja output file for build action")
40	flag.StringVar(&inputs_file, "inputs_file", "", "file containing list of input files")
41	flag.StringVar(&template, "template", fid_lib.DefaultTemplate, "output template for FileList")
42	flag.BoolVar(&inspect, "inspect", false, "whether to inspect file contents")
43
44	flag.Parse()
45
46	if target == "" {
47		panic("must specify --target")
48	}
49	if prior_state_file == "" {
50		prior_state_file = target + ".pc_state"
51	}
52	if new_state_file == "" {
53		new_state_file = prior_state_file + ".new"
54	}
55
56	if err = os.Chdir(top); err != nil {
57		panic(err)
58	}
59
60	inputs = flag.Args()
61	if inputs_file != "" {
62		data, err := os.ReadFile(inputs_file)
63		if err != nil {
64			panic(err)
65		}
66		inputs = append(inputs, strings.Split(string(data), "\n")...)
67	}
68
69	// Read the prior state
70	prior_state, err := fid_lib.LoadState(prior_state_file, fid_lib.OsFs)
71	if err != nil {
72		panic(err)
73	}
74	// Create the new state
75	new_state, err := fid_lib.CreateState(inputs, inspect, fid_lib.OsFs)
76	if err != nil {
77		panic(err)
78	}
79	if err = fid_lib.WriteState(new_state, new_state_file); err != nil {
80		panic(err)
81	}
82
83	file_list := *fid_lib.CompareInternalState(prior_state, new_state, target)
84
85	if err = file_list.Format(os.Stdout, template); err != nil {
86		panic(err)
87	}
88
89	metrics_file := os.Getenv("SOONG_METRICS_AGGREGATION_FILE")
90	if metrics_file != "" {
91		if err = file_list.SendMetrics(metrics_file); err != nil {
92			panic(err)
93		}
94	}
95}
96