xref: /aosp_15_r20/external/cronet/build/config/ios/generate_umbrella_header.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2018 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Generates an umbrella header for an iOS framework."""
6
7import argparse
8import datetime
9import os
10import re
11import string
12
13
14HEADER_TEMPLATE = string.Template('''\
15// Copyright $year The Chromium Authors
16// Use of this source code is governed by a BSD-style license that can be
17// found in the LICENSE file.
18//
19// This file is auto-generated by //build/ios/config/generate_umbrella_header.py
20
21#ifndef $header_guard
22#define $header_guard
23
24$imports
25
26#endif  // $header_guard
27''')
28
29
30def ComputeHeaderGuard(file_path):
31  """Computes the header guard for a file path.
32
33  Args:
34    file_path: The path to convert into an header guard.
35  Returns:
36    The header guard string for the file_path.
37  """
38  return re.sub(r'[.+/\\]', r'_', file_path.upper()) + '_'
39
40
41def WriteUmbrellaHeader(output_path, imported_headers):
42  """Writes the umbrella header.
43
44  Args:
45    output_path: The path to the umbrella header.
46    imported_headers: A list of headers to #import in the umbrella header.
47  """
48  year = datetime.date.today().year
49  header_guard = ComputeHeaderGuard(output_path)
50  imports = '\n'.join([
51      '#import "%s"' % os.path.basename(header)
52          for header in sorted(imported_headers)
53      ])
54  with open(output_path, 'w') as output_file:
55    output_file.write(
56        HEADER_TEMPLATE.safe_substitute({
57            'year': year,
58            'header_guard': header_guard,
59            'imports': imports,
60        }))
61
62
63def Main():
64  parser = argparse.ArgumentParser(description=__doc__)
65  parser.add_argument('--output-path', required=True, type=str,
66                      help='Path to the generated umbrella header.')
67  parser.add_argument('imported_headers', type=str, nargs='+',
68                      help='Headers to #import in the umbrella header.')
69  options = parser.parse_args()
70
71  return WriteUmbrellaHeader(options.output_path, options.imported_headers)
72
73
74if __name__ == '__main__':
75  Main()
76