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/.
12 def relativize(path
, base
=None):
13 # For absolute path in Unix builds, we need relative paths because
14 # Windows programs run via Wine don't like these Unix absolute paths
15 # (they look like command line arguments).
16 if path
.startswith("/"):
17 return os
.path
.relpath(path
, base
)
18 # For Windows absolute paths, we can just use the unmodified path.
19 # And if the path starts with '-', it's a command line argument.
20 if os
.path
.isabs(path
) or path
.startswith("-"):
22 # Remaining case is relative paths, which may be relative to a different
23 # directory (os.getcwd()) than the needed `base`, so we "rebase" it.
24 return os
.path
.relpath(path
, base
)
27 def search_path(paths
, path
):
29 f
= os
.path
.join(p
, path
)
32 raise RuntimeError(f
"Cannot find {path}")
35 # Preprocess all the direct and indirect inputs of midl, and put all the
36 # preprocessed inputs in the given `base` directory. Returns a tuple containing
37 # the path of the main preprocessed input, and the modified flags to use instead
38 # of the flags given as argument.
39 def preprocess(base
, input, flags
):
42 from collections
import deque
44 IMPORT_RE
= re
.compile('import\s*"([^"]+)";')
46 parser
= argparse
.ArgumentParser()
47 parser
.add_argument("-I", action
="append")
48 parser
.add_argument("-D", action
="append")
49 parser
.add_argument("-acf")
50 args
, remainder
= parser
.parse_known_args(flags
)
52 [buildconfig
.substs
["_CXX"]]
53 # Ideally we'd use the real midl version, but querying it adds a
54 # significant overhead to configure. In practice, the version number
55 # doesn't make a difference at the moment.
56 + ["-E", "-D__midl=801"]
57 + [f
"-D{d}" for d
in args
.D
or ()]
58 + [f
"-I{i}" for i
in args
.I
or ()]
60 includes
= ["."] + buildconfig
.substs
["INCLUDE"].split(";") + (args
.I
or [])
62 queue
= deque([input])
64 queue
.append(args
.acf
)
65 output
= os
.path
.join(base
, os
.path
.basename(input))
68 input = queue
.popleft()
71 if os
.path
.basename(input) in seen
:
73 seen
.add(os
.path
.basename(input))
74 input = search_path(includes
, input)
75 # If there is a .acf file corresponding to the .idl we're processing,
76 # we also want to preprocess that file because midl might look for it too.
77 if input.endswith(".idl") and os
.path
.exists(input[:-4] + ".acf"):
78 queue
.append(input[:-4] + ".acf")
79 command
= preprocessor
+ [input]
80 preprocessed
= os
.path
.join(base
, os
.path
.basename(input))
81 subprocess
.run(command
, stdout
=open(preprocessed
, "wb"), check
=True)
82 # Read the resulting file, and search for imports, that we'll want to
84 with
open(preprocessed
, "r") as fh
:
86 if not line
.startswith("import"):
88 m
= IMPORT_RE
.match(line
)
94 # Add -I<base> first in the flags, so that midl resolves imports to the
95 # preprocessed files we created.
96 for i
in [base
] + (args
.I
or []):
97 flags
.extend(["-I", i
])
98 # Add the preprocessed acf file if one was given on the command line.
100 flags
.extend(["-acf", os
.path
.join(base
, os
.path
.basename(args
.acf
))])
101 flags
.extend(remainder
)
105 def midl(out
, input, *flags
):
106 out
.avoid_writing_to_file()
107 midl_flags
= buildconfig
.substs
["MIDL_FLAGS"]
108 base
= os
.path
.dirname(out
.name
) or "."
111 # If the build system is asking to not use the preprocessor to midl,
112 # we need to do the preprocessing ourselves.
113 if "-no_cpp" in midl_flags
:
114 # Normally, we'd use tempfile.TemporaryDirectory, but in this specific
115 # case, we actually want a deterministic directory name, because it's
116 # recorded in the code midl generates.
117 tmpdir
= os
.path
.join(base
, os
.path
.basename(input) + ".tmp")
118 os
.makedirs(tmpdir
, exist_ok
=True)
120 input, flags
= preprocess(tmpdir
, input, flags
)
121 except subprocess
.CalledProcessError
as e
:
123 midl
= buildconfig
.substs
["MIDL"]
124 wine
= buildconfig
.substs
.get("WINE")
125 if midl
.lower().endswith(".exe") and wine
:
126 command
= [wine
, midl
]
129 command
.extend(midl_flags
)
130 command
.extend([relativize(f
, base
) for f
in flags
])
131 command
.append("-Oicf")
132 command
.append(relativize(input, base
))
133 print("Executing:", " ".join(command
))
134 result
= subprocess
.run(command
, cwd
=base
)
135 return result
.returncode
138 shutil
.rmtree(tmpdir
)
141 # midl outputs dlldata to a single dlldata.c file by default. This prevents running
142 # midl in parallel in the same directory for idl files that would generate dlldata.c
143 # because of race conditions updating the file. Instead, we ask midl to create
144 # separate files, and we merge them manually.
145 def merge_dlldata(out
, *inputs
):
146 inputs
= [open(i
) for i
in inputs
]
147 read_a_line
= [True] * len(inputs
)
150 f
.readline() if read_a_line
[n
] else lines
[n
] for n
, f
in enumerate(inputs
)
152 unique_lines
= set(lines
)
153 if len(unique_lines
) == 1:
154 # All the lines are identical
158 read_a_line
= [True] * len(inputs
)
160 len(unique_lines
) == 2
161 and len([l
for l
in unique_lines
if "#define" in l
]) == 1
163 # Most lines are identical. When they aren't, it's typically because some
164 # files have an extra #define that others don't. When that happens, we
165 # print out the #define, and get a new input line from the files that had
166 # a #define on the next iteration. We expect that next line to match what
167 # the other files had on this iteration.
168 # Note: we explicitly don't support the case where there are different
169 # defines across different files, except when there's a different one
170 # for each file, in which case it's handled further below.
171 a
= unique_lines
.pop()
175 out
.write(unique_lines
.pop())
176 read_a_line
= ["#define" in l
for l
in lines
]
177 elif len(unique_lines
) != len(lines
):
178 # If for some reason, we don't get lines that are entirely different
179 # from each other, we have some unexpected input.
181 "Error while merging dlldata. Last lines read: {}".format(lines
),
188 read_a_line
= [True] * len(inputs
)