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/.
9 from argparse
import Action
, ArgumentParser
14 class CPPFlag(Action
):
17 def __call__(self
, parser
, namespace
, values
, option_string
=None):
18 if "windres" in buildconfig
.substs
["RC"].lower():
19 if option_string
== "-U":
21 if option_string
== "-I":
22 option_string
= "--include-dir"
24 self
.all_flags
.extend((option_string
, values
))
28 parser
= ArgumentParser()
30 "-D", action
=CPPFlag
, metavar
="VAR[=VAL]", help="Define a variable"
32 parser
.add_argument("-U", action
=CPPFlag
, metavar
="VAR", help="Undefine a variable")
34 "-I", action
=CPPFlag
, metavar
="DIR", help="Search path for includes"
36 parser
.add_argument("-o", dest
="output", metavar
="OUTPUT", help="Output file")
37 parser
.add_argument("input", help="Input file")
38 args
= parser
.parse_args()
40 is_windres
= "windres" in buildconfig
.substs
["RC"].lower()
42 verbose
= os
.environ
.get("BUILD_VERBOSE_LOG")
44 # llvm-rc doesn't preprocess on its own, so preprocess manually
45 # Theoretically, not windres could be rc.exe, but configure won't use it
46 # unless you really ask for it, and it will still work with preprocessed
50 fd
, path
= tempfile
.mkstemp(suffix
=".rc")
51 command
= buildconfig
.substs
["CXXCPP"] + CPPFlag
.all_flags
52 command
.extend(("-DRC_INVOKED", args
.input))
54 cpu_arch_dict
= {"x86_64": "_AMD64_", "x86": "_X86_", "aarch64": "_ARM64_"}
56 # add a preprocessor #define that specifies the CPU architecture
57 cpu_arch_ppd
= cpu_arch_dict
[buildconfig
.substs
["CPU_ARCH"]]
59 command
.extend(("-D", cpu_arch_ppd
))
62 print("Executing:", " ".join(command
))
63 with os
.fdopen(fd
, "wb") as fh
:
64 retcode
= subprocess
.run(command
, stdout
=fh
).returncode
66 # Rely on the subprocess printing out any relevant error
71 command
= [buildconfig
.substs
["RC"]]
73 command
.extend(("-O", "coff"))
75 # Even though llvm-rc doesn't preprocess, we still need to pass at least
77 command
.extend(CPPFlag
.all_flags
)
81 command
.extend(("-o", args
.output
))
83 # Use win1252 code page for the input.
84 command
.extend(("-c", "1252", "-Fo" + args
.output
))
89 print("Executing:", " ".join(command
))
90 retcode
= subprocess
.run(command
).returncode
92 # Rely on the subprocess printing out any relevant error
95 if path
!= args
.input:
101 if __name__
== "__main__":
102 sys
.exit(generate_res())