-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinker.d
193 lines (158 loc) · 5.02 KB
/
Linker.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/+
+ Copyright Andrej Mitrovic 2011.
+ Copyright Tomasz Stachowiak 2009 - 2011.
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+/
module xfbuild.Linker;
import xfbuild.GlobalParams;
import xfbuild.Module;
import xfbuild.Process;
import xfbuild.Misc;
import std.algorithm;
import std.ascii : isAlpha;
import std.array;
import std.exception;
import std.stdio;
/+ {
Regex linkerFileRegex;
}
shared static this() {
//defend\terrain\Generator.obj(Generator)
//linkerFileRegex = Regex(`([a-zA-Z0-9.:_\-\\/]+)\(.*\)`);
}+/
bool isValidObjFileName(string f)
{
foreach (c; f)
{
if (!isAlpha(c) && !(`.:_-\/`.canFind(c)))
{
return false;
}
}
return true;
}
bool link(ref Module[string] modules, string[] mainFiles = null)
{
bool retryCompile;
string[] args;
args ~= globalParams.compilerName;
args ~= globalParams.compilerOptions;
foreach (k; mainFiles)
{
foreach (m; modules)
{
if (m.path == k)
{
if (!m.isHeader)
args ~= m.objFile;
break;
}
}
}
foreach (k, m; modules)
{
if (m.isHeader || canFind(mainFiles, m.path))
continue;
args ~= m.objFile;
}
args ~= "-of" ~ globalParams.outputFile;
if (globalParams.recompileOnUndefinedReference)
{
executeCompilerViaResponseFile(
args[0],
args[1..$],
globalParams.linkerAffinityMask
);
}
else
{
auto process = Process(true, args);
auto output = execute(process);
string currentFile = null;
Module currentModule = null;
foreach (line; output.splitLines)
{
line = strip(line);
if (line.length > 0)
{
writefln("linker: '%s'", line);
}
try
{
auto arr = line.decomposeString(cast(string)null, "(", null, ")");
//if(linkerFileRegex.test(line))
if (arr && isValidObjFileName(arr[1]))
{
//currentFile = linkerFileRegex[1];
currentFile = arr[1];
foreach (m; modules)
if (m.objFile == currentFile)
currentModule = m;
if (!currentModule && globalParams.verbose)
{
writefln("%s doesn't belong to any known module", currentFile);
continue;
}
if (globalParams.verbose)
writefln("linker error in file %s (module %s)", currentFile, currentModule);
}
else if (/*undefinedReferenceRegex.test(line)*/ line.startsWith("Error 42:") && globalParams.recompileOnUndefinedReference)
{
if (globalParams.verbose)
{
if (!currentFile || !currentModule)
{
writeln("no file..?");
//continue; // as i currently recompile every file anyway...
}
/*writefln("undefined reference to %s, will try to recompile %s", undefinedReferenceRegex[1], currentModule);
currentModule.needRecompile = true;
retryCompile = true;*/
writeln("undefined reference, will try the full recompile");
foreach (m; modules)
m.needRecompile = true;
retryCompile = true;
break;
}
}
}
catch (Exception e)
{
if (currentFile && currentModule)
{
writefln("%s", e);
writefln("utf8 exception caught, assuming linker error in file %s", currentModule);
// orly!
foreach (m; modules)
m.needRecompile = true;
retryCompile = true;
break;
}
else
{
throw e;
}
}
}
try
{
checkProcessFail(process);
}
catch (Exception e)
{
if (retryCompile)
{
if (globalParams.verbose)
writeln("ignoring linker error, will try to recompile");
}
else
{
throw e; // rethrow exception since we're not going to retry what we did
}
}
}
globalParams.recompileOnUndefinedReference = false; // avoid infinite loop
return retryCompile;
}