Bug 1664775 [wpt PR 25515] - Fieldset NG: Fix hit-testing for the rendered legend...
[gecko.git] / config / create_res.py
blobbfa69aafbcf650b3fc684dece71c8a50963eb97f
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from argparse import (
6 Action,
7 ArgumentParser,
9 import os
10 import subprocess
11 import sys
12 import tempfile
14 import buildconfig
17 class CPPFlag(Action):
18 all_flags = []
20 def __call__(self, parser, namespace, values, option_string=None):
21 if 'windres' in buildconfig.substs['RC'].lower():
22 if option_string == '-U':
23 return
24 if option_string == '-I':
25 option_string = '--include-dir'
27 self.all_flags.extend((option_string, values))
30 def generate_res():
31 parser = ArgumentParser()
32 parser.add_argument('-D', action=CPPFlag, metavar='VAR[=VAL]', help='Define a variable')
33 parser.add_argument('-U', action=CPPFlag, metavar='VAR', help='Undefine a variable')
34 parser.add_argument('-I', action=CPPFlag, metavar='DIR', help='Search path for includes')
35 parser.add_argument('-o', dest='output', metavar='OUTPUT', help='Output file')
36 parser.add_argument('input', help='Input file')
37 args = parser.parse_args()
39 is_windres = 'windres' in buildconfig.substs['RC'].lower()
41 verbose = os.environ.get('BUILD_VERBOSE_LOG')
43 # llvm-rc doesn't preprocess on its own, so preprocess manually
44 # Theoretically, not windres could be rc.exe, but configure won't use it
45 # unless you really ask for it, and it will still work with preprocessed
46 # output.
47 try:
48 if not is_windres:
49 fd, path = tempfile.mkstemp(suffix='.rc')
50 command = buildconfig.substs['CXXCPP'] + CPPFlag.all_flags
51 command.extend(('-DRC_INVOKED', args.input))
52 if verbose:
53 print('Executing:', ' '.join(command))
54 with os.fdopen(fd, 'wb') as fh:
55 retcode = subprocess.run(command, stdout=fh).returncode
56 if retcode:
57 # Rely on the subprocess printing out any relevant error
58 return retcode
59 else:
60 path = args.input
62 command = [buildconfig.substs['RC']]
63 if is_windres:
64 command.extend(('-O', 'coff'))
66 # Even though llvm-rc doesn't preprocess, we still need to pass at least
67 # the -I flags.
68 command.extend(CPPFlag.all_flags)
70 if args.output:
71 if is_windres:
72 command.extend(('-o', args.output))
73 else:
74 # Use win1252 code page for the input.
75 command.extend(('-c', '1252', '-Fo' + args.output))
77 command.append(path)
79 if verbose:
80 print('Executing:', ' '.join(command))
81 retcode = subprocess.run(command).returncode
82 if retcode:
83 # Rely on the subprocess printing out any relevant error
84 return retcode
85 finally:
86 if path != args.input:
87 os.remove(path)
89 return 0
92 if __name__ == '__main__':
93 sys.exit(generate_res())