generate_umbrella_header.py (2049B)
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 7 import argparse 8 import datetime 9 import os 10 import re 11 import string 12 13 14 HEADER_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 30 def 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 41 def 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 63 def 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 74 if __name__ == '__main__': 75 Main()