xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/jdeps/jdeps.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2018 The Bazel Authors. 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
15// jdeps is a command line tool to filter a jdeps proto
16package main
17
18import (
19	"flag"
20	"io/ioutil"
21	"log"
22	"strings"
23
24	dpb "src/tools/jdeps/proto/deps_go_proto"
25	"google.golang.org/protobuf/proto"
26)
27
28var (
29	in     = flag.String("in", "", "Path to input jdeps file")
30	target = flag.String("target", "", "Target suffix to remove from jdeps file")
31	out    = flag.String("out", "", "Path to output jdeps file")
32)
33
34func main() {
35	flag.Parse()
36	if *in == "" || *target == "" || *out == "" {
37		log.Fatal("Missing required flags. Must specify --in --target --out.")
38	}
39
40	bytes, err := ioutil.ReadFile(*in)
41	if err != nil {
42		log.Fatalf("Error reading input jdeps: %v", err)
43	}
44	jdeps := &dpb.Dependencies{}
45	if err = proto.Unmarshal(bytes, jdeps); err != nil {
46		log.Fatalf("Error parsing input jdeps: %v", err)
47	}
48
49	newJdeps := proto.Clone(jdeps).(*dpb.Dependencies)
50	var deps []*dpb.Dependency
51	for _, dep := range jdeps.Dependency {
52		if !strings.HasSuffix(dep.GetPath(), *target) {
53			deps = append(deps, dep)
54		}
55	}
56	newJdeps.Dependency = deps
57
58	bytes, err = proto.Marshal(newJdeps)
59	if err != nil {
60		log.Fatalf("Error serializing output jdeps: %v", err)
61	}
62
63	err = ioutil.WriteFile(*out, bytes, 0644)
64	if err != nil {
65		log.Fatalf("Error writing output jdeps: %v", err)
66	}
67}
68