blob: 48d67217c0bc3957bcb7fe22847d60a0d465c410 [file] [log] [blame]
[email protected]fb9d58c2014-03-20 17:43:101#!/usr/bin/env python
2# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Create files with copyright boilerplate and header include guards.
7
8Usage: tools/boilerplate.py path/to/file.{h,cc}
9"""
10
Raul Tambre57e09d62019-09-22 17:18:5211from __future__ import print_function
12
[email protected]fb9d58c2014-03-20 17:43:1013from datetime import date
14import os
15import os.path
16import sys
17
18LINES = [
19 'Copyright %d The Chromium Authors. All rights reserved.' %
20 date.today().year,
21 'Use of this source code is governed by a BSD-style license that can be',
22 'found in the LICENSE file.'
23]
24
25EXTENSIONS_TO_COMMENTS = {
26 'h': '//',
27 'cc': '//',
28 'mm': '//',
29 'js': '//',
sdefresne5feb0b42016-03-15 11:03:4030 'py': '#',
31 'gn': '#',
32 'gni': '#',
Tanmoy Mollik0bf3efe2019-05-10 09:23:1233 'mojom': '//',
34 'typemap': '#',
[email protected]fb9d58c2014-03-20 17:43:1035}
36
37def _GetHeader(filename):
38 _, ext = os.path.splitext(filename)
39 ext = ext[1:]
40 comment = EXTENSIONS_TO_COMMENTS[ext] + ' '
41 return '\n'.join([comment + line for line in LINES])
42
43
44def _CppHeader(filename):
rohitrao786f94382016-11-16 02:47:1545 guard = filename.upper() + '_'
emx359564e2017-04-28 18:17:1546 for char in '/\\.+':
marq070cc782016-11-15 16:19:2547 guard = guard.replace(char, '_')
[email protected]fb9d58c2014-03-20 17:43:1048 return '\n'.join([
49 '',
50 '#ifndef ' + guard,
51 '#define ' + guard,
52 '',
53 '#endif // ' + guard,
54 ''
55 ])
56
57
Jesse McKenna79d284b82020-04-23 14:51:5058def _RemoveCurrentDirectoryPrefix(filename):
59 current_dir_prefixes = [os.curdir + os.sep]
60 if os.altsep is not None:
61 current_dir_prefixes.append(os.curdir + os.altsep)
62 for prefix in current_dir_prefixes:
63 if filename.startswith(prefix):
64 return filename[len(prefix):]
65 return filename
66
67
bnc70c505372016-12-14 16:28:1468def _RemoveTestSuffix(filename):
[email protected]fb9d58c2014-03-20 17:43:1069 base, _ = os.path.splitext(filename)
bnc70c505372016-12-14 16:28:1470 suffixes = [ '_test', '_unittest', '_browsertest' ]
71 for suffix in suffixes:
72 l = len(suffix)
73 if base[-l:] == suffix:
74 return base[:-l]
75 return base
76
sdefresned6844b4a2017-03-07 01:11:2077
78def _IsIOSFile(filename):
79 if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'):
80 return True
81 if 'ios' in filename.split(os.path.sep):
82 return True
83 return False
84
85
emx359564e2017-04-28 18:17:1586def _FilePathSlashesToCpp(filename):
87 return filename.replace('\\', '/')
88
89
bnc70c505372016-12-14 16:28:1490def _CppImplementation(filename):
emx359564e2017-04-28 18:17:1591 return '\n#include "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
92 + '.h"\n'
[email protected]fb9d58c2014-03-20 17:43:1093
94
kkhorimotocc826ad2016-02-11 20:17:4695def _ObjCppImplementation(filename):
sdefresned6844b4a2017-03-07 01:11:2096 implementation = '\n#import "' + _RemoveTestSuffix(filename) + '.h"\n'
97 if not _IsIOSFile(filename):
98 return implementation
99 implementation += '\n'
100 implementation += '#if !defined(__has_feature) || !__has_feature(objc_arc)\n'
101 implementation += '#error "This file requires ARC support."\n'
102 implementation += '#endif\n'
103 return implementation
kkhorimotocc826ad2016-02-11 20:17:46104
105
[email protected]fb9d58c2014-03-20 17:43:10106def _CreateFile(filename):
Jesse McKenna79d284b82020-04-23 14:51:50107 filename = _RemoveCurrentDirectoryPrefix(filename)
108
[email protected]fb9d58c2014-03-20 17:43:10109 contents = _GetHeader(filename) + '\n'
110
111 if filename.endswith('.h'):
112 contents += _CppHeader(filename)
kkhorimotocc826ad2016-02-11 20:17:46113 elif filename.endswith('.cc'):
[email protected]fb9d58c2014-03-20 17:43:10114 contents += _CppImplementation(filename)
kkhorimotocc826ad2016-02-11 20:17:46115 elif filename.endswith('.mm'):
116 contents += _ObjCppImplementation(filename)
[email protected]fb9d58c2014-03-20 17:43:10117
emx359564e2017-04-28 18:17:15118 fd = open(filename, 'wb')
[email protected]fb9d58c2014-03-20 17:43:10119 fd.write(contents)
120 fd.close()
121
122
123def Main():
124 files = sys.argv[1:]
125 if len(files) < 1:
Raul Tambre57e09d62019-09-22 17:18:52126 print(
127 'Usage: boilerplate.py path/to/file.h path/to/file.cc', file=sys.stderr)
[email protected]fb9d58c2014-03-20 17:43:10128 return 1
129
130 # Perform checks first so that the entire operation is atomic.
131 for f in files:
132 _, ext = os.path.splitext(f)
133 if not ext[1:] in EXTENSIONS_TO_COMMENTS:
Raul Tambre57e09d62019-09-22 17:18:52134 print('Unknown file type for %s' % f, file=sys.stderr)
[email protected]fb9d58c2014-03-20 17:43:10135 return 2
136
137 if os.path.exists(f):
Raul Tambre57e09d62019-09-22 17:18:52138 print('A file at path %s already exists' % f, file=sys.stderr)
[email protected]fb9d58c2014-03-20 17:43:10139 return 2
140
141 for f in files:
142 _CreateFile(f)
143
144
145if __name__ == '__main__':
146 sys.exit(Main())