xref: /aosp_15_r20/external/bazelbuild-rules_cc/tools/migration/crosstool_query.py (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1*eed53cd4SHONG Yifan"""Script to make automated CROSSTOOL refactorings easier.
2*eed53cd4SHONG Yifan
3*eed53cd4SHONG YifanThis script reads the CROSSTOOL file and allows for querying of its fields.
4*eed53cd4SHONG Yifan"""
5*eed53cd4SHONG Yifan
6*eed53cd4SHONG Yifanfrom absl import app
7*eed53cd4SHONG Yifanfrom absl import flags
8*eed53cd4SHONG Yifanfrom google.protobuf import text_format
9*eed53cd4SHONG Yifanfrom third_party.com.github.bazelbuild.bazel.src.main.protobuf import crosstool_config_pb2
10*eed53cd4SHONG Yifan
11*eed53cd4SHONG Yifanflags.DEFINE_string("crosstool", None, "CROSSTOOL file path to be queried")
12*eed53cd4SHONG Yifanflags.DEFINE_string("identifier", None,
13*eed53cd4SHONG Yifan                    "Toolchain identifier to specify toolchain.")
14*eed53cd4SHONG Yifanflags.DEFINE_string("print_field", None, "Field to be printed to stdout.")
15*eed53cd4SHONG Yifan
16*eed53cd4SHONG Yifan
17*eed53cd4SHONG Yifandef main(unused_argv):
18*eed53cd4SHONG Yifan  crosstool = crosstool_config_pb2.CrosstoolRelease()
19*eed53cd4SHONG Yifan
20*eed53cd4SHONG Yifan  crosstool_filename = flags.FLAGS.crosstool
21*eed53cd4SHONG Yifan  identifier = flags.FLAGS.identifier
22*eed53cd4SHONG Yifan  print_field = flags.FLAGS.print_field
23*eed53cd4SHONG Yifan
24*eed53cd4SHONG Yifan  if not crosstool_filename:
25*eed53cd4SHONG Yifan    raise app.UsageError("ERROR crosstool unspecified")
26*eed53cd4SHONG Yifan  if not identifier:
27*eed53cd4SHONG Yifan    raise app.UsageError("ERROR identifier unspecified")
28*eed53cd4SHONG Yifan
29*eed53cd4SHONG Yifan  if not print_field:
30*eed53cd4SHONG Yifan    raise app.UsageError("ERROR print_field unspecified")
31*eed53cd4SHONG Yifan
32*eed53cd4SHONG Yifan  with open(crosstool_filename, "r") as f:
33*eed53cd4SHONG Yifan    text = f.read()
34*eed53cd4SHONG Yifan    text_format.Merge(text, crosstool)
35*eed53cd4SHONG Yifan
36*eed53cd4SHONG Yifan  toolchain_found = False
37*eed53cd4SHONG Yifan  for toolchain in crosstool.toolchain:
38*eed53cd4SHONG Yifan    if toolchain.toolchain_identifier == identifier:
39*eed53cd4SHONG Yifan      toolchain_found = True
40*eed53cd4SHONG Yifan      if not print_field:
41*eed53cd4SHONG Yifan        continue
42*eed53cd4SHONG Yifan      for field, value in toolchain.ListFields():
43*eed53cd4SHONG Yifan        if print_field == field.name:
44*eed53cd4SHONG Yifan          print value
45*eed53cd4SHONG Yifan
46*eed53cd4SHONG Yifan  if not toolchain_found:
47*eed53cd4SHONG Yifan    print "toolchain_identifier %s not found, valid values are:" % identifier
48*eed53cd4SHONG Yifan    for toolchain in crosstool.toolchain:
49*eed53cd4SHONG Yifan      print "  " + toolchain.toolchain_identifier
50*eed53cd4SHONG Yifan
51*eed53cd4SHONG Yifan
52*eed53cd4SHONG Yifanif __name__ == "__main__":
53*eed53cd4SHONG Yifan  app.run(main)
54