OLD | NEW |
---|---|
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Runs the Microsoft Message Compiler (mc.exe). This Python adapter is for the | 5 # Runs the Microsoft Message Compiler (mc.exe). This Python adapter is for the |
6 # GN build, which can only run Python and not native binaries. | 6 # GN build, which can only run Python and not native binaries. |
7 # | 7 # |
8 # Usage: message_compiler.py <environment_file> [<args to mc.exe>*] | 8 # Usage: message_compiler.py <environment_file> [<args to mc.exe>*] |
9 | 9 |
10 import os | |
10 import subprocess | 11 import subprocess |
11 import sys | 12 import sys |
12 | 13 |
13 # Read the environment block from the file. This is stored in the format used | 14 def main(): |
14 # by CreateProcess. Drop last 2 NULs, one for list terminator, one for trailing | 15 # Read the environment block from the file. This is stored in the format used |
15 # vs. separator. | 16 # by CreateProcess. Drop last 2 NULs, one for list terminator, one for |
16 env_pairs = open(sys.argv[1]).read()[:-2].split('\0') | 17 # trailing vs. separator. |
17 env_dict = dict([item.split('=', 1) for item in env_pairs]) | 18 env_pairs = open(sys.argv[1]).read()[:-2].split('\0') |
19 env_dict = dict([item.split('=', 1) for item in env_pairs]) | |
18 | 20 |
19 # mc writes to stderr, so this explicitly redirects to stdout and eats it. | 21 # mc writes to stderr, so this explicitly redirects to stdout and eats it. |
20 try: | 22 try: |
21 # This needs shell=True to search the path in env_dict for the mc executable. | 23 # This needs shell=True to search the path in env_dict for the mc |
22 subprocess.check_output(["mc.exe"] + sys.argv[2:], | 24 # executable. |
23 env=env_dict, | 25 rest = sys.argv[2:] |
24 stderr=subprocess.STDOUT, | 26 subprocess.check_output(['mc.exe'] + rest, |
25 shell=True) | 27 env=env_dict, |
26 except subprocess.CalledProcessError as e: | 28 stderr=subprocess.STDOUT, |
27 print e.output | 29 shell=True) |
28 sys.exit(e.returncode) | 30 # We require all source code (in particular, the header generated here) to |
31 # be UTF-8. jinja can output the intermediate .mc file in UTF-8 or UTF-16LE. | |
32 # However, mc.exe only supports Unicode via the -u flag, and it assumes when | |
33 # that is specified that the input is UTF-16LE (and errors out on UTF-8 | |
34 # files, assuming they're ANSI). Even with -u specified and UTF16-LE input, | |
35 # it generates an ANSI header, and includes broken versions of the message | |
36 # text in the comment before the value. To work around this, for any invalid | |
37 # // comment lines, we simply drop the line in the header after building it. | |
Nico
2016/11/22 17:45:21
(did you file an msconnect for getting support for
| |
38 header_dir = None | |
39 input_file = None | |
40 for i, arg in enumerate(rest): | |
41 if arg == '-h' and len(rest) > i + 1: | |
42 assert header_dir == None | |
43 header_dir = rest[i + 1] | |
44 elif arg.endswith('.mc') or arg.endswith('.man'): | |
45 assert input_file == None | |
46 input_file = arg | |
47 if header_dir: | |
48 header_file = os.path.join( | |
49 header_dir, os.path.splitext(os.path.basename(input_file))[0] + '.h') | |
50 header_contents = [] | |
51 with open(header_file, 'rb') as f: | |
52 for line in f.readlines(): | |
53 if line.startswith('//') and '?' in line: | |
54 continue | |
55 header_contents.append(line) | |
56 with open(header_file, 'wb') as f: | |
57 f.write(''.join(header_contents)) | |
58 except subprocess.CalledProcessError as e: | |
59 print e.output | |
60 sys.exit(e.returncode) | |
61 | |
62 if __name__ == '__main__': | |
63 main() | |
OLD | NEW |