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++."""
11 def WriteSource(base_name
,
15 """Writes C++ header/cc source files for the given map of string variables.
18 base_name: The basename of the file, without the extension.
19 dir_from_src: Path from src to the directory that will contain the file,
20 using forward slashes.
21 output_dir: Directory to output the sources to.
22 global_string_map: Map of variable names to their string values. These
23 variables will be available as globals.
25 copyright
= '\n'.join([
26 '// Copyright %s The Chromium Authors. All rights reserved.',
27 '// Use of this source code is governed by a BSD-style license that '
29 '// found in the LICENSE file.']) % datetime
.date
.today().year
33 for name
in global_string_map
.iterkeys():
34 externs
+= ['extern const char %s[];' % name
]
36 temp
= '_'.join(dir_from_src
.split('/') + [base_name
])
37 define
= temp
.upper() + '_H_'
46 '#endif // ' + define
])
49 with
open(os
.path
.join(output_dir
, base_name
+ '.h'), 'w') as f
:
54 return line
.replace('\\', '\\\\').replace('"', '\\"')
57 for name
, contents
in global_string_map
.iteritems():
59 if '\n' not in contents
:
60 lines
= [' "%s"' % EscapeLine(contents
)]
62 for line
in contents
.split('\n'):
63 lines
+= [' "%s\\n"' % EscapeLine(line
)]
64 definitions
+= ['const char %s[] =\n%s;' % (name
, '\n'.join(lines
))]
69 '#include "%s"' % (dir_from_src
+ '/' + base_name
+ '.h'),
71 '\n'.join(definitions
)])
74 with
open(os
.path
.join(output_dir
, base_name
+ '.cc'), 'w') as f
: