Bug 1720653 [wpt PR 29673] - [Sanitizer API] Move tests to WPT suite., a=testonly
[gecko.git] / config / create_res.py
blob5588fa0a4900dc73d38ba860460aef16fc81df54
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(
33 "-D", action=CPPFlag, metavar="VAR[=VAL]", help="Define a variable"
35 parser.add_argument("-U", action=CPPFlag, metavar="VAR", help="Undefine a variable")
36 parser.add_argument(
37 "-I", action=CPPFlag, metavar="DIR", help="Search path for includes"
39 parser.add_argument("-o", dest="output", metavar="OUTPUT", help="Output file")
40 parser.add_argument("input", help="Input file")
41 args = parser.parse_args()
43 is_windres = "windres" in buildconfig.substs["RC"].lower()
45 verbose = os.environ.get("BUILD_VERBOSE_LOG")
47 # llvm-rc doesn't preprocess on its own, so preprocess manually
48 # Theoretically, not windres could be rc.exe, but configure won't use it
49 # unless you really ask for it, and it will still work with preprocessed
50 # output.
51 try:
52 if not is_windres:
53 fd, path = tempfile.mkstemp(suffix=".rc")
54 command = buildconfig.substs["CXXCPP"] + CPPFlag.all_flags
55 command.extend(("-DRC_INVOKED", args.input))
56 if verbose:
57 print("Executing:", " ".join(command))
58 with os.fdopen(fd, "wb") as fh:
59 retcode = subprocess.run(command, stdout=fh).returncode
60 if retcode:
61 # Rely on the subprocess printing out any relevant error
62 return retcode
63 else:
64 path = args.input
66 command = [buildconfig.substs["RC"]]
67 if is_windres:
68 command.extend(("-O", "coff"))
70 # Even though llvm-rc doesn't preprocess, we still need to pass at least
71 # the -I flags.
72 command.extend(CPPFlag.all_flags)
74 if args.output:
75 if is_windres:
76 command.extend(("-o", args.output))
77 else:
78 # Use win1252 code page for the input.
79 command.extend(("-c", "1252", "-Fo" + args.output))
81 command.append(path)
83 if verbose:
84 print("Executing:", " ".join(command))
85 retcode = subprocess.run(command).returncode
86 if retcode:
87 # Rely on the subprocess printing out any relevant error
88 return retcode
89 finally:
90 if path != args.input:
91 os.remove(path)
93 return 0
96 if __name__ == "__main__":
97 sys.exit(generate_res())