1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75

build / config / ios / generate_umbrella_header.py [blame]

# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Generates an umbrella header for an iOS framework."""

import argparse
import datetime
import os
import re
import string


HEADER_TEMPLATE = string.Template('''\
// Copyright $year The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file is auto-generated by //build/ios/config/generate_umbrella_header.py

#ifndef $header_guard
#define $header_guard

$imports

#endif  // $header_guard
''')


def ComputeHeaderGuard(file_path):
  """Computes the header guard for a file path.

  Args:
    file_path: The path to convert into an header guard.
  Returns:
    The header guard string for the file_path.
  """
  return re.sub(r'[.+/\\]', r'_', file_path.upper()) + '_'


def WriteUmbrellaHeader(output_path, imported_headers):
  """Writes the umbrella header.

  Args:
    output_path: The path to the umbrella header.
    imported_headers: A list of headers to #import in the umbrella header.
  """
  year = datetime.date.today().year
  header_guard = ComputeHeaderGuard(output_path)
  imports = '\n'.join([
      '#import "%s"' % os.path.basename(header)
          for header in sorted(imported_headers)
      ])
  with open(output_path, 'w') as output_file:
    output_file.write(
        HEADER_TEMPLATE.safe_substitute({
            'year': year,
            'header_guard': header_guard,
            'imports': imports,
        }))


def Main():
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument('--output-path', required=True, type=str,
                      help='Path to the generated umbrella header.')
  parser.add_argument('imported_headers', type=str, nargs='+',
                      help='Headers to #import in the umbrella header.')
  options = parser.parse_args()

  return WriteUmbrellaHeader(options.output_path, options.imported_headers)


if __name__ == '__main__':
  Main()