/
CppBuilder.cpp
494 lines (454 loc) · 12.1 KB
/
CppBuilder.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "Builders.h"
void PutCompileTime(int time, int count)
{
PutConsole(String().Cat() << count << " file(s) compiled in " << GetPrintTime(time)
<< " " << int(msecs() - time) / count << " msec/file");
}
String CppBuilder::GetTargetExt() const
{
if(!HasFlag("WIN32") && !HasFlag("WINCE"))
return HasFlag("DLL") ? ".so" : "";
else
return HasFlag("DLL") ? ".dll" : ".exe";
}
void CppBuilder::CleanPackage(const String& package, const String& outdir)
{
DeleteFolderDeep(outdir);
}
// POSIX lib files have names in form of libXXXXXX.so.ver.minver(.rel)
// so we can't simply get file extension
String CppBuilder::GetSrcType(String fn) const
{
fn = ToLower(fn);
String ext = GetFileExt(fn);
if(!HasFlag("POSIX") || ext == ".so")
return ext;
int soPos = fn.ReverseFind(".so");
if(soPos < 0)
return ext;
fn = fn.Mid(soPos + 3);
const char *c = ~fn;
while(*c)
{
if(*c != '.' && !IsDigit(*c))
return ext;
c++;
}
return ".so";
}
// from complete lib name/path (libXXX.so.ver.minVer) gets the soname (libXXX.so.ver)
String CppBuilder::GetSoname(String libName) const
{
String soname = GetFileName(libName);
int soLen = soname.GetCount();
int soPos = ToLower(soname).ReverseFind(".so");
if(soPos < 0)
soPos = soLen;
else
soPos += 3;
if(soname.Mid(soPos, 1) == ".")
{
soPos++;
while(soPos < soLen && IsDigit(soname[soPos]))
soPos++;
}
return soname.Left(soPos);
}
// from complete lib name/path (libXXX.so.ver.minVer) gets the link name (libXXX.so)
String CppBuilder::GetSoLinkName(String libName) const
{
String linkName = GetFileName(libName);
int soPos = ToLower(linkName).ReverseFind(".so");
if(soPos < 0)
soPos = linkName.GetCount();
else
soPos += 3;
return linkName.Left(soPos);
}
String CppBuilder::GetSharedLibPath(const String& package) const
{
String outfn;
if(HasFlag("POSIX"))
outfn << "lib";
for(const char *p = package; *p; p++)
outfn.Cat(IsAlNum(*p) || *p == '-' ? *p : '_');
if(!IsNull(version) && !HasFlag("POSIX"))
outfn << version;
outfn << (HasFlag("WIN32") || HasFlag("WINCE") ? ".dll" : ".so");
if(HasFlag("POSIX"))
{
Point p = ExtractVersion();
int ver = IsNull(p.x) ? 1 : p.x;
int minver = IsNull(p.y) ? 0 : p.y;
outfn << '.' << ver << '.' << minver;
}
return CatAnyPath(GetFileFolder(target), outfn);
}
int CppBuilder::AllocSlot()
{
return host->AllocSlot();
}
bool CppBuilder::Run(const char *cmdline, int slot, String key, int blitz_count)
{
return host->Run(CmdX(cmdline), slot, key, blitz_count);
}
bool CppBuilder::Run(const char *cmdline, Stream& out, int slot, String key, int blitz_count)
{
return host->Run(CmdX(cmdline), out, slot, key, blitz_count);
}
bool CppBuilder::Wait()
{
return host->Wait();
}
bool CppBuilder::Wait(int slot)
{
return host->Wait(slot);
}
void CppBuilder::OnFinish(Event<> cb)
{
host->OnFinish(cb);
}
int CasFilter(int c) {
return c == '\n' ? '\n' : c == '\t' ? ' ' : c >= ' ' ? c : 0;
}
int CharFilterEol(int c) {
return c == '\n' || c == '\r' ? c : 0;
}
Vector<String> Cuprep(const String& m, const VectorMap<String, String>& mac,
const Vector<String>& inc) {
String r;
String q = Filter(m, CasFilter);
const char *s = q;
while(*s)
if(*s == '$') {
String v;
s++;
if(*s == '$') {
r.Cat('$');
s++;
}
else
if(*s == '(') {
s++;
bool pinc = false;
if(*s == '!') {
pinc = true;
s++;
}
for(;;) {
if(*s == ')') {
s++;
break;
}
if(*s == '\0')
break;
v.Cat(*s++);
}
if(pinc)
for(int i = 0; i < inc.GetCount(); i++)
if(inc[i].Find(' '))
r << v << '"' << inc[i] << "\" ";
else
r << v << inc[i] << ' ';
else
r.Cat(mac.Get(v, Null));
}
else
r.Cat('$');
}
else
r.Cat(*s++);
return Split(r, CharFilterEol);
}
bool CppBuilder::Cd(const String& cmd) {
if(cmd.GetLength() > 2 && ToLower(cmd.Mid(0, 3)) == "cd ") {
String path = cmd.Mid(3);
#ifdef PLATFOTM_POSIX
chdir(path);
#endif
#ifdef PLATFORM_WIN32
SetCurrentDirectory(path);
#endif
return true;
}
return false;
}
bool CppBuilder::Cp(const String& cmd, const String& package, bool& error) {
if(cmd.GetLength() > 2 && ToLower(cmd.Mid(0, 3)) == "cp ") {
Vector<String> path = Split(cmd.Mid(3), ' ');
if(path.GetCount() == 2) {
String p = GetFileFolder(PackagePath(package));
String p1 = NormalizePath(path[0], p);
String p2 = NormalizePath(path[1], p);
RealizePath(p2);
if(!FileExists(p1)) {
PutConsole("FAILED: " + cmd);
error = true;
}
SaveFile(p2, LoadFile(p1));
}
return true;
}
return false;
}
static void AddPath(VectorMap<String, String>& out, String key, String path)
{
out.Add(key, path);
out.Add(key + "_WIN", WinPath(path));
out.Add(key + "_UNIX", UnixPath(path));
}
void sGatherAllExt(Vector<String>& files, Vector<String>& dirs, const String& pp, const String& p)
{
dirs.Add(p);
FindFile ff(pp + "/" + p + "/*.*");
while(ff) {
String n = Merge("/", p, ff.GetName());
if(ff.IsFile())
files.Add(n);
else
if(ff.IsFolder()) {
sGatherAllExt(files, dirs, pp, n);
}
ff.Next();
}
}
Vector<String> ReadPatterns(CParser& p)
{
Vector<String> out;
while(!p.IsEof() && !p.Char(';')) {
out << ReadValue(p);
p.Char(',');
}
return out;
}
void ExtExclude(CParser& p, Index<String>& x)
{
Vector<String> e = ReadPatterns(p);
Vector<int> remove;
for(int i = 0; i < x.GetCount(); i++)
for(int j = 0; j < e.GetCount(); j++) {
if(PatternMatch(e[j], x[i])) {
remove.Add(i);
break;
}
}
x.Remove(remove);
}
Vector<String> CppBuilder::CustomStep(const String& pf, const String& package_, bool& error)
{
String package = Nvl(package_, mainpackage);
String path = (*pf == '.' && pf[1] != '.') ? target : SourcePath(package, pf);
String file = path;
String ext = ToLower(GetFileExt(pf));
if(ext == ".ext") {
Vector<String> files;
Vector<String> dirs;
sGatherAllExt(files, dirs, GetFileFolder(path), "");
Index<String> pkg_files;
Package pkg;
pkg.Load(PackagePath(package));
for(int i = 0; i < pkg.GetCount(); i++)
pkg_files.Add(pkg[i]);
Index<String> out;
Index<String> include_path;
String f = LoadFile(path);
try {
CParser p(f);
while(!p.IsEof()) {
if(p.Id("files")) {
Vector<String> e = ReadPatterns(p);
for(int i = 0; i < files.GetCount(); i++)
for(int j = 0; j < e.GetCount(); j++) {
String f = files[i];
if(PatternMatch(e[j], f) && pkg_files.Find(f) < 0)
out.FindAdd(f);
}
}
else
if(p.Id("exclude")) {
ExtExclude(p, out);
}
else
if(p.Id("include_path")) {
Vector<String> e = ReadPatterns(p);
for(int j = 0; j < e.GetCount(); j++) {
String ee = e[j];
if(ee.Find('*') >= 0)
for(int i = 0; i < dirs.GetCount(); i++) {
String d = dirs[i];
if(PatternMatch(e[j], d)) {
include_path.FindAdd(d);
}
}
else
include_path.Add(ee);
}
}
else
if(p.Id("exclude_path")) {
ExtExclude(p, include_path);
}
else {
p.PassId("includes");
Vector<String> e = ReadPatterns(p);
for(int i = 0; i < files.GetCount(); i++)
for(int j = 0; j < e.GetCount(); j++) {
String f = files[i];
if(PatternMatch(e[j], f) && pkg_files.Find(f) < 0)
include_path.FindAdd(GetFileFolder(f));
}
}
}
}
catch(CParser::Error) {
PutConsole("Invalid .ext file");
error = true;
return Vector<String>();
}
for(int i = 0; i < include_path.GetCount(); i++)
include.Add(NormalizePath(include_path[i], GetFileFolder(path)));
Vector<String> o;
for(int i = 0; i < out.GetCount(); i++)
o.Add(SourcePath(package, out[i]));
return o;
}
for(int i = 0; i < wspc.GetCount(); i++) {
const Array< ::CustomStep >& mv = wspc.GetPackage(i).custom;
for(int j = 0; j < mv.GetCount(); j++) {
const ::CustomStep& m = mv[j];
if(MatchWhen(m.when, config.GetKeys()) && m.MatchExt(ext)) {
VectorMap<String, String> mac;
AddPath(mac, "PATH", file);
AddPath(mac, "RELPATH", pf);
AddPath(mac, "DIR", GetFileFolder(PackagePath(package)));
AddPath(mac, "FILEDIR", GetFileFolder(file));
AddPath(mac, "PACKAGE", package);
mac.Add("FILE", GetFileName(file));
mac.Add("TITLE", GetFileTitle(file));
AddPath(mac, "EXEPATH", target);
AddPath(mac, "EXEDIR", GetFileFolder(target));
mac.Add("EXEFILE", GetFileName(target));
mac.Add("EXETITLE", GetFileTitle(target));
AddPath(mac, "OUTDIR", outdir);
//BW
AddPath(mac, "OUTDIR", GetFileFolder(target));
AddPath(mac, "OUTFILE", GetFileName(target));
AddPath(mac, "OUTTITLE", GetFileTitle(target));
mac.Add("INCLUDE", Join(include, ";"));
Vector<String> out = Cuprep(m.output, mac, include);
bool dirty = out.IsEmpty();
for(int i = 0; !dirty && i < out.GetCount(); i++)
dirty = (GetFileTime(file) > GetFileTime(out[i]));
if(dirty) {
HdependTimeDirty();
PutConsole(GetFileName(file));
Vector<String> cmd = Cuprep(m.command, mac, include);
String cmdtext;
for(int c = 0; c < cmd.GetCount(); c++) {
PutVerbose(cmd[c]);
if(!Cd(cmd[c]) && !Cp(cmd[c], package, error)) {
String ctext = cmd[c];
const char *cm = ctext;
if(*cm == '?')
cm++;
if(*ctext != '?' && Execute(cm)) {
for(int t = 0; t < out.GetCount(); t++)
DeleteFile(out[t]);
PutConsole("FAILED: " + ctext);
error = true;
return Vector<String>();
}
}
}
}
return out;
}
}
}
Vector<String> out;
out.Add(path);
return out;
}
String CppBuilder::Includes(const char *sep, const String& package, const Package& pkg)
{
String cc;
for(int i = 0; i < include.GetCount(); i++)
cc << sep << GetPathQ(include[i]);
cc << sep << GetPathQ(outdir);
return cc;
}
bool IsSvnDir2(const String& p)
{ // this is a cope of usvn/IsSvnDir to avoid modular issues
if(IsNull(p))
return false;
if(DirectoryExists(AppendFileName(p, ".svn")) || DirectoryExists(AppendFileName(p, "_svn")))
return true;
String path = p;
String path0;
while(path != path0) {
path0 = path;
path = GetFileFolder(path);
if(DirectoryExists(AppendFileName(path, ".svn")))
return true;
}
return false;
}
Vector<String> SvnInfo(const String& package)
{
Vector<String> info;
String d = GetFileFolder(PackagePath(package));
if(IsSvnDir2(d)) {
String v = Sys("svnversion " + d);
if(IsDigit(*v))
info.Add("#define bmSVN_REVISION " + AsCString(TrimBoth(v)));
v = Sys("svn info " + d);
StringStream in(v);
while(!in.IsEof()) {
String l = in.GetLine();
if(l.StartsWith("URL: ")) {
info.Add("#define bmSVN_URL " + AsCString(TrimBoth(l.Mid(5))));
break;
}
}
}
return info;
}
void CppBuilder::SaveBuildInfo(const String& package)
{
String path = AppendFileName(outdir, "build_info.h");
RealizePath(path);
FileOut info(path);
Time t = GetSysTime();
info << "#define bmYEAR " << (int)t.year << "\r\n";
info << "#define bmMONTH " << (int)t.month << "\r\n";
info << "#define bmDAY " << (int)t.day << "\r\n";
info << "#define bmHOUR " << (int)t.hour << "\r\n";
info << "#define bmMINUTE " << (int)t.minute << "\r\n";
info << "#define bmSECOND " << (int)t.second << "\r\n";
info << Format("#define bmTIME Time(%d, %d, %d, %d, %d, %d)\r\n",
(int)t.year, (int)t.month, (int)t.day, (int)t.hour, (int)t.minute, (int)t.second);
info << "#define bmMACHINE " << AsCString(GetComputerName()) << "\r\n";
info << "#define bmUSER " << AsCString(GetUserName()) << "\r\n";
if(package == mainpackage)
info << Join(SvnInfo(package), "\r\n");
}
String CppBuilder::DefinesTargetTime(const char *sep, const String& package, const Package& pkg)
{
String cc;
for(int i = 0; i < config.GetCount(); i++)
cc << sep << "flag" << config[i];
if(main_conf)
cc << sep << "MAIN_CONF";
targettime = GetFileTime(target);
return cc;
}
String CppBuilder::IncludesDefinesTargetTime(const String& package, const Package& pkg)
{
String cc = Includes(" -I", package, pkg);
cc << DefinesTargetTime(" -D", package, pkg);
return cc;
}
bool CppBuilder::HasAnyDebug() const
{
return HasFlag("DEBUG") || HasFlag("DEBUG_MINIMAL") || HasFlag("DEBUG_FULL");
}