Skip to content

Commit f86b63d

Browse files
committed
Refactor .def lines patching (prepare for .def.in patching)
1 parent 72244a7 commit f86b63d

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

windows/mingw/buildsdk.d

+43-27
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ void runShell(string cmd)
4141
}
4242
}
4343

44+
alias LineTransformer = string delegate(const string line);
45+
string patchLines(string inFile, string outFile, LineTransformer lineTransformer)
46+
{
47+
const lines = std.file.readText(inFile).splitLines;
48+
49+
bool transformed = false;
50+
const newLines = lines.map!((const string line)
51+
{
52+
const newLine = lineTransformer(line);
53+
if (newLine !is line)
54+
transformed = true;
55+
return newLine;
56+
}).array;
57+
58+
if (!transformed)
59+
return inFile;
60+
61+
version (verbose)
62+
writeln(`Patching file "` ~ inFile ~ `" to "` ~ outFile ~ `"`);
63+
64+
std.file.write(outFile, newLines.join("\n"));
65+
return outFile;
66+
}
67+
4468
// Preprocesses a 'foo.def.in' file to 'foo.def'.
4569
void generateDef(string inFile, string outFile)
4670
{
@@ -51,47 +75,39 @@ void generateDef(string inFile, string outFile)
5175

5276
void sanitizeDef(string defFile)
5377
{
54-
const lines = std.file.readText(defFile).splitLines;
55-
56-
bool touched = false;
57-
const newLines = lines.map!((const string line)
78+
patchLines(defFile, defFile, (line)
5879
{
59-
string l = line;
60-
61-
if (l == "LIBRARY vcruntime140_app")
62-
l = `LIBRARY "VCRUNTIME140.dll"`;
80+
if (line == "LIBRARY vcruntime140_app")
81+
return `LIBRARY "vcruntime140.dll"`;
6382

6483
// The MinGW-w64 .def files specify weak external symbols as 'alias == realName'.
65-
if (l.length > 1 && l[0] != ';')
84+
if (line.length > 1 && line[0] != ';')
6685
{
67-
const i = l.indexOf(" == ");
86+
const i = line.indexOf(" == ");
6887
if (i > 0)
6988
{
70-
const weakName = l[0 .. i];
71-
const realName = l[i+4 .. $];
72-
weakSymbols[weakName] = realName;
73-
74-
l = ";" ~ l;
89+
const weakName = line[0 .. i];
90+
const realName = line[i+4 .. $];
91+
92+
if (weakName.indexOf(' ') < 0 && realName.indexOf(' ') < 0)
93+
{
94+
weakSymbols[weakName] = realName;
95+
return ";" ~ line;
96+
}
7597
}
7698
}
7799

78100
// Don't export function 'atexit'; we have our own in msvc_atexit.c.
79-
if (l == "atexit" /* msvcr120 */ || l == "atexit DATA" /* < 120 */)
80-
l = "";
101+
if (line == "atexit")
102+
return "";
81103

82104
// Do export function '__chkstk' (ntdll.dll).
83105
// LLVM emits calls to it to detect stack overflows with '_alloca'.
84-
if (l == ";__chkstk")
85-
l = "__chkstk";
86-
87-
if (l !is line)
88-
touched = true;
89-
90-
return l;
91-
}).array;
106+
if (line == ";__chkstk")
107+
return "__chkstk";
92108

93-
if (touched)
94-
std.file.write(defFile, newLines.join("\n"));
109+
return line;
110+
});
95111
}
96112

97113
void copyDefs(string inDir, string outDir)

0 commit comments

Comments
 (0)