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++."""
10 def WriteSource(base_name,
14 """Writes C++ header/cc source files for the given map of string variables.
17 base_name: The basename of the file, without the extension.
18 dir_from_src: Path from src to the directory that will contain the file,
19 using forward slashes.
20 output_dir: Directory to output the sources to.
21 global_string_map: Map of variable names to their string values. These
22 variables will be available as globals.
24 copyright = '\n'.join([
25 '// Copyright 2013 The Chromium Authors. All rights reserved.',
26 '// Use of this source code is governed by a BSD-style license that '
28 '// found in the LICENSE file.'])
32 for name in global_string_map.iterkeys():
33 externs += ['extern const char %s[];' % name]
35 temp = '_'.join(dir_from_src.split('/') + [base_name])
36 define = temp.upper() + '_H_'
45 '#endif // ' + define])
48 with open(os.path.join(output_dir, base_name + '.h'), 'w') as f:
53 return line.replace('\\', '\\\\').replace('"', '\\"')
56 for name, contents in global_string_map.iteritems():
58 if '\n' not in contents:
59 lines = [' "%s"' % EscapeLine(contents)]
61 for line in contents.split('\n'):
62 lines += [' "%s\\n"' % EscapeLine(line)]
63 definitions += ['const char %s[] =\n%s;' % (name, '\n'.join(lines))]
68 '#include "%s"' % (dir_from_src + '/' + base_name + '.h'),
70 '\n'.join(definitions)])
73 with open(os.path.join(output_dir, base_name + '.cc'), 'w') as f: