Skip to content

Commit 3950338

Browse files
TriciaCrichtonshs96c
authored andcommitted
Added option to use char and string when generating atoms.cc (#7784)
Previous build structure allowed javascript atoms to be built with char_t and wstring or char and string. This restores that option, with char_t/wstring as the default. Adds utf8 parameter.
1 parent 01b1bbe commit 3950338

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

javascript/chrome-driver/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ closure_lang_file(
109109
name = "header",
110110
out = "atoms.h",
111111
lang = "hdecl",
112+
utf8 = False,
112113
deps = ATOMS,
113114
)
114115

115116
closure_lang_file(
116117
name = "source",
117118
out = "atoms.cc",
118119
lang = "cc",
120+
utf8 = False,
119121
deps = ATOMS,
120122
)
121123

javascript/private/gen_file.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_atom_name(name):
2323
name = os.path.basename(name)
2424
return name.upper()
2525

26-
def write_atom_literal(out, name, contents, lang):
26+
def write_atom_literal(out, name, contents, lang, utf8):
2727
# Escape the contents of the file so it can be stored as a literal.
2828
contents = contents.replace("\\", "\\\\")
2929
contents = contents.replace("\f", "\\f")
@@ -33,7 +33,10 @@ def write_atom_literal(out, name, contents, lang):
3333
contents = contents.replace('"', '\\"')
3434

3535
if "cc" == lang or "hh" == lang:
36+
if utf8:
3637
line_format = " L\"{}\",\n"
38+
else:
39+
line_format = " \"{}\",\n"
3740
elif "java" == lang:
3841
line_format = " .append\(\"{}\")\n"
3942
else:
@@ -42,7 +45,9 @@ def write_atom_literal(out, name, contents, lang):
4245
name = get_atom_name(name)
4346

4447
if "cc" == lang or "hh" == lang:
45-
out.write("const wchar_t* const %s[] = {\n" % name)
48+
string_type = "std::wstring" if utf8 else "std::string"
49+
char_type = "wchar_t" if utf8 else "char"
50+
out.write("const %s* const %s[] = {\n" % (char_type, name))
4651
elif "java" == lang:
4752
out.write(" %s(new StringBuilder()\n" % name)
4853
else:
@@ -67,32 +72,32 @@ def write_atom_literal(out, name, contents, lang):
6772
elif "java" == lang:
6873
out.write(" .toString()),\n")
6974

70-
def generate_header(file_name, out, js_map, just_declare):
75+
def generate_header(file_name, out, js_map, just_declare, utf8):
7176
define_guard = "WEBDRIVER_%s" % os.path.basename(file_name.upper()).replace(".", "_")
72-
77+
include_stddef = "\n#include <stddef.h> // For wchar_t." if utf8 else ""
7378
out.write("""%s
7479
7580
/* AUTO GENERATED - DO NOT EDIT BY HAND */
7681
#ifndef %s
7782
#define %s
78-
79-
#include <stddef.h> // For wchar_t.
83+
%s
8084
#include <string> // For std::(w)string.
8185
8286
namespace webdriver {
8387
namespace atoms {
8488
85-
""" % (_copyright, define_guard, define_guard))
89+
""" % (_copyright, define_guard, define_guard, include_stddef))
8690

91+
string_type = "std::wstring" if utf8 else "std::string"
92+
char_type = "wchar_t" if utf8 else "char"
93+
8794
for (name, file) in js_map.items():
8895
if just_declare:
89-
out.write("extern const wchat_t* const %s[];\n" % name.upper())
96+
out.write("extern const %s* const %s[];\n" % (char_type, name.upper()))
9097
else:
9198
contents = open(file, "r").read()
92-
write_atom_literal(out, name, contents, "hh")
99+
write_atom_literal(out, name, contents, "hh", utf8)
93100

94-
string_type = "std::wstring"
95-
char_type = "wchar_t"
96101
out.write("""
97102
static inline %s asString(const %s* const atom[]) {
98103
%s source;
@@ -108,7 +113,7 @@ def generate_header(file_name, out, js_map, just_declare):
108113
#endif // %s
109114
""" % (string_type, char_type, string_type, define_guard))
110115

111-
def generate_cc_source(out, js_map):
116+
def generate_cc_source(out, js_map, utf8):
112117
out.write("""%s
113118
114119
/* AUTO GENERATED - DO NOT EDIT BY HAND */
@@ -123,7 +128,7 @@ def generate_cc_source(out, js_map):
123128

124129
for (name, file) in js_map.items():
125130
contents = open(file, "r").read()
126-
write_atom_literal(out, name, contents, "cc")
131+
write_atom_literal(out, name, contents, "cc", utf8)
127132

128133
out.write("""
129134
} // namespace atoms
@@ -148,7 +153,7 @@ def generate_java_source(file_name, out, preamble, js_map):
148153

149154
for (name, file) in js_map.items():
150155
contents = open(file, "r").read()
151-
write_atom_literal(out, name, contents, "java")
156+
write_atom_literal(out, name, contents, "java", True)
152157

153158
out.write("""
154159
;
@@ -173,18 +178,19 @@ def main(argv=[]):
173178
lang = argv[1]
174179
file_name = argv[2]
175180
preamble = argv[3]
181+
utf8 = (argv[4] == "true")
176182

177183
js_map = {}
178-
for i in range(4, len(argv), 2):
184+
for i in range(5, len(argv), 2):
179185
js_map[argv[i]] = argv[i + 1]
180186

181187
with open(file_name, "w") as out:
182188
if "cc" == lang:
183-
generate_cc_source(out, js_map)
189+
generate_cc_source(out, js_map, utf8)
184190
elif "hdecl" == lang:
185-
generate_header(file_name, out, js_map, True)
191+
generate_header(file_name, out, js_map, True, utf8)
186192
elif "hh" == lang:
187-
generate_header(file_name, out, js_map, False)
193+
generate_header(file_name, out, js_map, False, utf8)
188194
elif "java" == lang:
189195
generate_java_source(file_name, out, preamble, js_map)
190196
else:

javascript/private/header.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def _closure_lang_file_impl(ctx):
2222
args.add(ctx.attr.lang)
2323
args.add(ctx.outputs.out)
2424
args.add(ctx.attr.preamble)
25+
args.add(ctx.attr.utf8)
2526
for key in sorted(binaries.keys()):
2627
args.add(key)
2728
args.add(binaries[key])
@@ -59,5 +60,10 @@ closure_lang_file = rule(
5960
executable = True,
6061
cfg = "host",
6162
),
63+
"utf8": attr.bool(
64+
doc = "Generate utf8 or not. UTF8 with generate wstring and wchar_t."
65+
+ "If false, genereation with use char and string. Defaults to True",
66+
default = True,
67+
),
6268
},
6369
)

0 commit comments

Comments
 (0)