xref: /aosp_15_r20/external/cronet/components/cronet/tools/check_combined_proguard_file.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python3
2#
3# Copyright 2023 The Chromium Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import sys
10
11REPOSITORY_ROOT = os.path.abspath(
12    os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))
13
14sys.path.insert(0, REPOSITORY_ROOT)
15import build.android.gyp.util.build_utils as build_utils  # pylint: disable=wrong-import-position
16import components.cronet.tools.utils as cronet_utils  # pylint: disable=wrong-import-position
17
18# Set this environment variable in order to regenerate the golden text
19# files.
20_REBASELINE_CRONET_PROGUARD = os.environ.get('REBASELINE_CRONET_PROGUARD',
21                                             '0') != '0'
22
23def CompareGeneratedWithGolden(generated_file_path, golden_file_path):
24  golden_text = cronet_utils.read_file(golden_file_path)
25  generated_text = cronet_utils.read_file(generated_file_path)
26  if _REBASELINE_CRONET_PROGUARD:
27    if golden_text != generated_text:
28      print('Updated', golden_file_path)
29      with open(golden_file_path, 'w') as f:
30        f.write(generated_text)
31    return None
32
33  if golden_text is None:
34    print(f'Golden file does not exist: {golden_file_path}')
35
36  return cronet_utils.compare_text_and_generate_diff(generated_text,
37                                                     golden_text,
38                                                     golden_file_path)
39
40
41def main():
42  parser = argparse.ArgumentParser()
43  parser.add_argument('--input_generated_file',
44                      type=str,
45                      help="Path to the generated file.")
46  parser.add_argument('--input_golden_file',
47                      type=str,
48                      help='Path to the input golden file.')
49  parser.add_argument('--stamp', type=str, help='Path to touch on success')
50  args = parser.parse_args()
51  text_diff = CompareGeneratedWithGolden(args.input_generated_file,
52                                         args.input_golden_file)
53  if text_diff:
54
55    print(f"""
56  Cronet Proguard golden test failed. To generate it:
57  #######################################################
58  #                                                     #
59  #      Run the command below to generate the file     #
60  #                                                     #
61  #######################################################
62
63  ########### START ###########
64  patch -p1 << 'END_DIFF'
65  {text_diff}
66  END_DIFF
67  ############ END ############
68
69If you wish to build the action locally in generation mode, See the instructions below:
70#####################################################################
71#                                                                   #
72# Run the command below to re-run the action in generation mode     #
73#                                                                   #
74#####################################################################
75./components/cronet/tools/cr_cronet.py -d out/proguard_config gn && \
76REBASELINE_CRONET_PROGUARD=1 autoninja -C out/proguard_config \
77cronet_combined_proguard_flags_golden_test
78
79This will generate a GN output directory with the appropriate GN\
80flags to run the golden_test in generation mode rather than verification mode.
81""")
82    sys.exit(-1)
83  else:
84    build_utils.Touch(args.stamp)
85
86if __name__ == '__main__':
87  sys.exit(main())
88