Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / chromedriver / cpp_source.py
blobc91eca956e81d8410660e0156a83567ce7226172
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Writes C++ header/cc source files for embedding resources into C++."""
7 import datetime
8 import os
9 import sys
12 def WriteSource(base_name,
13 dir_from_src,
14 output_dir,
15 global_string_map):
16 """Writes C++ header/cc source files for the given map of string variables.
18 Args:
19 base_name: The basename of the file, without the extension.
20 dir_from_src: Path from src to the directory that will contain the file,
21 using forward slashes.
22 output_dir: Directory to output the sources to.
23 global_string_map: Map of variable names to their string values. These
24 variables will be available as globals.
25 """
26 copyright = '\n'.join([
27 '// Copyright %s The Chromium Authors. All rights reserved.',
28 '// Use of this source code is governed by a BSD-style license that '
29 'can be',
30 '// found in the LICENSE file.',
31 '',
32 '// This file was generated at (%s) by running:',
33 '// %s']) % (
34 datetime.date.today().year,
35 datetime.datetime.now().isoformat(' '),
36 ' '.join(sys.argv))
38 # Write header file.
39 externs = []
40 for name in global_string_map.iterkeys():
41 externs += ['extern const char %s[];' % name]
43 temp = '_'.join(dir_from_src.split('/') + [base_name])
44 define = temp.upper() + '_H_'
45 header = '\n'.join([
46 copyright,
47 '',
48 '#ifndef ' + define,
49 '#define ' + define,
50 '',
51 '\n'.join(externs),
52 '',
53 '#endif // ' + define])
54 header += '\n'
56 with open(os.path.join(output_dir, base_name + '.h'), 'w') as f:
57 f.write(header)
59 # Write cc file.
60 def EscapeLine(line):
61 return line.replace('\\', '\\\\').replace('"', '\\"')
63 definitions = []
64 for name, contents in global_string_map.iteritems():
65 lines = []
66 if '\n' not in contents:
67 lines = [' "%s"' % EscapeLine(contents)]
68 else:
69 for line in contents.split('\n'):
70 lines += [' "%s\\n"' % EscapeLine(line)]
71 definitions += ['const char %s[] =\n%s;' % (name, '\n'.join(lines))]
73 cc = '\n'.join([
74 copyright,
75 '',
76 '#include "%s"' % (dir_from_src + '/' + base_name + '.h'),
77 '',
78 '\n'.join(definitions)])
79 cc += '\n'
81 with open(os.path.join(output_dir, base_name + '.cc'), 'w') as f:
82 f.write(cc)