Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / cpp_source.py
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.
4
5 """Writes C++ header/cc source files for embedding resources into C++."""
6
7 import os
8
9
10 def WriteSource(base_name,
11                 dir_from_src,
12                 output_dir,
13                 global_string_map):
14   """Writes C++ header/cc source files for the given map of string variables.
15
16   Args:
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.
23   """
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 '
27           'can be',
28       '// found in the LICENSE file.'])
29
30   # Write header file.
31   externs = []
32   for name in global_string_map.iterkeys():
33     externs += ['extern const char %s[];' % name]
34
35   temp = '_'.join(dir_from_src.split('/') + [base_name])
36   define = temp.upper() + '_H_'
37   header = '\n'.join([
38       copyright,
39       '',
40       '#ifndef ' + define,
41       '#define ' + define,
42       '',
43       '\n'.join(externs),
44       '',
45       '#endif  // ' + define])
46   header += '\n'
47
48   with open(os.path.join(output_dir, base_name + '.h'), 'w') as f:
49     f.write(header)
50
51   # Write cc file.
52   def EscapeLine(line):
53     return line.replace('\\', '\\\\').replace('"', '\\"')
54
55   definitions = []
56   for name, contents in global_string_map.iteritems():
57     lines = []
58     if '\n' not in contents:
59       lines = ['    "%s"' % EscapeLine(contents)]
60     else:
61       for line in contents.split('\n'):
62         lines += ['    "%s\\n"' % EscapeLine(line)]
63     definitions += ['const char %s[] =\n%s;' % (name, '\n'.join(lines))]
64
65   cc = '\n'.join([
66       copyright,
67       '',
68       '#include "%s"' % (dir_from_src + '/' + base_name + '.h'),
69       '',
70       '\n'.join(definitions)])
71   cc += '\n'
72
73   with open(os.path.join(output_dir, base_name + '.cc'), 'w') as f:
74     f.write(cc)