-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCIDBuild_GCCDriver.cpp
More file actions
601 lines (500 loc) · 18 KB
/
Copy pathCIDBuild_GCCDriver.cpp
File metadata and controls
601 lines (500 loc) · 18 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//
// FILE NAME: CIDBuild_KCCDriver.Cpp
//
// AUTHOR: Will Mason
//
// CREATED: 02/24/99
//
// COPYRIGHT: Charmed Quark Systems, Ltd @ 2019
//
// This software is copyrighted by 'Charmed Quark Systems, Ltd' and
// the author (Dean Roddey.) It is licensed under the MIT Open Source
// license:
//
// https://opensource.org/licenses/MIT
//
// DESCRIPTION:
//
// This file implements the TGCCDriver class.
//
// CAVEATS/GOTCHAS:
//
// 1) Each driver includes its own header directly, so that it does not
// show up to other code. Only the TToolsDriver.Cpp file includes all
// of the concrete driver instantiation headers, and uses conditional
// compilation to include only the one that is required.
//
// LOG:
//
// $_CIDLib_Log_$
//
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include "CIDBuild.hpp"
#include "CIDBuild_GCCDriver.hpp"
#include <stdio.h>
#include <unistd.h>
// ---------------------------------------------------------------------------
// CLASS: TGCCDriver
// PREFIX: tdrv
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// TGCCDriver: Constructors and Destructor
// ---------------------------------------------------------------------------
TGCCDriver::TGCCDriver() :
m_bDebug(false)
{
//
// Get a short cut indicator for whether we are doing a debug or prod
// build, since we use it a lot below.
//
m_bDebug = (facCIDBuild.eBldMode() == tCIDBuild::EBldModes::Develop);
}
TGCCDriver::~TGCCDriver()
{
}
// ---------------------------------------------------------------------------
// TGCCDriver: Protected, inherited methods
// ---------------------------------------------------------------------------
tCIDLib::TBoolean TGCCDriver::bBuild(const TProjectInfo& projiTarget)
{
// Store away the target project for internal use while we are here
m_pprojiTarget = &projiTarget;
//
// Build up the target file name. We take the already computed one
// (minus the path) to avoid duplicating the procedure.
//
tCIDLib::TCard4 c4Name;
tCIDLib::TCard4 c4Ext;
TUtils::FindPathParts(m_pprojiTarget->strOutBin(), c4Name, c4Ext);
m_strTargetFile.CopyAt(m_pprojiTarget->strOutBin(), c4Name);
//
// If it's a shared library then we've got to strip the version
// part for compilation. We'll add it back later.
//
if (m_pprojiTarget->eType() == tCIDBuild::EProjTypes::SharedLib)
{
m_strTargetFile.CapAt(m_strTargetFile.c4Length()
- facCIDBuild.strVersionSuffix().c4Length());
}
//
// Lets do our Cpp compilations. We just run through each of the Cpp
// files, which our parent class has has already set up, and ask if
// each one needs to be compiled. If so, we build it.
//
bool bObjBuilt = bCompileCpps();
//
// NOTE: We change to the main output directory before we link, so all
// of the file names are relative to that. This makes the link line
// a LOT smaller.
//
if (!TUtils::bChangeDir(facCIDBuild.strOutDir()))
{
stdOut << L"Could not change to output directory for link step" << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::Internal;
}
if (bObjBuilt && facCIDBuild.bVerbose())
stdOut << L" Linking because an Obj was rebuilt" << kCIDBuild::EndLn;
//
// Now lets see if the target file is out of date with any of its Obj,
// Lib, Res, etc... files. If so, then we have to build it. If any
// Obj files were built, then don't bother checking since we know that
// we have to link.
//
// If the Force option is on, then of course we have to rebuild and
// there is no reason to check anything.
//
bool bUpdate = (bObjBuilt || facCIDBuild.bForce());
if (!bUpdate)
{
// See if the target exists, if not obviously we have to build
TFindInfo fndiTarget;
if (!TFindInfo::bFindAFile(m_strTargetFile, fndiTarget))
{
bUpdate = true;
if (facCIDBuild.bVerbose())
stdOut << L" Target file not present: " << m_strTargetFile << kCIDBuild::EndLn;
}
//
// If nothing to do yet, then see if any of the Lib or Obj files
// are newer than the target.
//
if (!bUpdate)
{
if (bObjsNewer(fndiTarget) || bLibsNewer(fndiTarget))
bUpdate = true;
}
}
if (bUpdate)
Link();
// It went ok, so return success
return true;
}
//
// A convenience to invoke the debugger
//
tCIDLib::TBoolean TGCCDriver::bInvokeDebugger(const TProjectInfo& projiTarget)
{
return kCIDLib::False;
}
tCIDLib::TVoid TGCCDriver::ResetDebugInfo(const TProjectInfo& projiToReset)
{
}
// ---------------------------------------------------------------------------
// TGCCDriver: Private, non-virtual methods
// ---------------------------------------------------------------------------
tCIDLib::TBoolean TGCCDriver::bCompileCpps()
{
TBldStr strTmp;
//
// First we want to build up the list of command line parameters that
// will be used to compile a Cpp file. For our Visual C++ compiler, the
// order of stuff is not important, so we can just build up the stuff
// that will stay the same.
//
const tCIDBuild::TCh* apszArgs[128];
tCIDLib::TCard4 c4CurArg = 0;
apszArgs[c4CurArg++] = L"g++";
apszArgs[c4CurArg++] = L"-c";
apszArgs[c4CurArg++] = L"-m32";
apszArgs[c4CurArg++] = L"-DPLATFORM_LINUX";
// We are adding some light concepts usage as VC/G++ add them
apszArgs[c4CurArg++] = L"-std=c++17";
if (m_bDebug)
{
apszArgs[c4CurArg++] = L"-g";
apszArgs[c4CurArg++] = L"-D_DEBUG";
}
//
// Set up our CIDLib platform define that gets passed to all code
// to drive conditional compilation.
//
TBldStr strPlatform(L"-D");
strPlatform.Append(kCIDBuild::pszPlatformDir);
apszArgs[c4CurArg++] = strPlatform.pszBuffer();
TBldStr strMajVer(L"-DCID_MAJVER=");
strTmp.Format(facCIDBuild.c4MajVer());
strMajVer.Append(strTmp);
apszArgs[c4CurArg++] = strMajVer.pszBuffer();
TBldStr strMinVer(L"-DCID_MINVER=");
strTmp.Format(facCIDBuild.c4MinVer());
strMinVer.Append(strTmp);
apszArgs[c4CurArg++] = strMinVer.pszBuffer();
TBldStr strRevision(L"-DCID_REVISION=");
strTmp.Format(facCIDBuild.c4Revision());
strRevision.Append(strTmp);
apszArgs[c4CurArg++] = strRevision.pszBuffer();
TBldStr strVerString(L"-DCID_VERSTRING=");
strTmp.Format(facCIDBuild.c4MajVer());
strVerString.Append(strTmp);
strVerString.Append(L".");
strTmp.Format(facCIDBuild.c4MinVer());
strVerString.Append(strTmp);
strVerString.Append(L".");
strTmp.Format(facCIDBuild.c4Revision());
strVerString.Append(strTmp);
apszArgs[c4CurArg++] = strVerString.pszBuffer();
TBldStr strVerSuff(L"-DCID_VERSUFF=");
strVerSuff.Append(facCIDBuild.strVersionSuffix());
apszArgs[c4CurArg++] = strVerSuff.pszBuffer();
// Set the flags for the project type
if (m_pprojiTarget->eType() == tCIDBuild::EProjTypes::SharedLib)
apszArgs[c4CurArg++] = L"-fPIC";
//
// Set up our standard include directories, for public and private
// includes and their respective per-platform subdirectories
//
TBldStr strInclude(L"-I");
strInclude.Append(facCIDBuild.strIncludeDir());
apszArgs[c4CurArg++] = strInclude.pszBuffer();
TBldStr strPPInclude(L"-I");
strPPInclude.Append(facCIDBuild.strPPIncludeDir());
apszArgs[c4CurArg++] = strPPInclude.pszBuffer();
TBldStr strPrivInclude(L"-I");
strPrivInclude.Append(facCIDBuild.strPrivIncludeDir());
apszArgs[c4CurArg++] = strPrivInclude.pszBuffer();
TBldStr strPPPrivInclude(L"-I");
strPPPrivInclude.Append(facCIDBuild.strPPPrivIncludeDir());
apszArgs[c4CurArg++] = strPPPrivInclude.pszBuffer();
//
// If there are any per-project includes, then add them in as well.
//
// NOTE:
// In order to avoid a lot of crap, we just allow these to leak. This
// is ok since this program is always just a one shot anyway and this
// is not a repetitious operation.
//
if (!m_pprojiTarget->listIncludePaths().bEmpty())
{
TBldStr strTmp;
TList<TBldStr>::TCursor cursIncludes(&m_pprojiTarget->listIncludePaths());
cursIncludes.bResetIter();
do
{
strTmp = L"-I";
strTmp.Append(cursIncludes.tCurElement());
apszArgs[c4CurArg++] = strTmp.pszOrphanBuffer();
} while (cursIncludes.bNext());
}
//
// Set up the flags for the runtime library mode, always multithreaded
apszArgs[c4CurArg++] = L"-D_REENTRANT";
//
// If not a pure cpp type project, enable permissive mode?
//
// if (m_pprojiTarget->bPureCpp())
// apszArgs[c4CurArg++] = L"-fpermissive";
//
// Every project has a define which is in the form PROJ_XXX, where XXX
// is the name of the project uppercased.
//
TBldStr strProjDef(L"-DPROJ_");
strProjDef.Append(m_pprojiTarget->strProjectName());
strProjDef.UpperCase();
apszArgs[c4CurArg++] = strProjDef.pszBuffer();
//
// Remember the index at which the file independent parameters stopped
// so that we can just add onto the list what we need below.
//
const tCIDLib::TCard4 c4FirstPerFileArg = c4CurArg;
//
// And now check each Cpp file and see if we have to build it. If so,
// the do it. Keep track of whether we build anything and return that
// status.
//
TList<TDepInfo>::TCursor cursCpps(&listCpps());
if (!cursCpps.bResetIter())
return false;
tCIDLib::TBoolean bBuiltSomething = false;
do
{
// Get a ref to the current element
const TDepInfo& depiCur = cursCpps.tCurElement();
// If this guy does not need to be compiled, check the next one
if (!bMustCompile(*m_pprojiTarget, depiCur))
continue;
// Keep up with the per-file args we add
tCIDLib::TCard4 c4ExtraArgs = c4FirstPerFileArg;
// Setup output
apszArgs[c4ExtraArgs++] = L"-o";
apszArgs[c4ExtraArgs++] = depiCur.strObjFileName().pszBuffer();
// Add the current file as the source file
apszArgs[c4ExtraArgs++] = depiCur.strFileName().pszBuffer();
if (facCIDBuild.bVerbose())
{
for (tCIDLib::TUInt uiInd = 0; uiInd < c4ExtraArgs; uiInd++)
stdOut << apszArgs[uiInd] << L" ";
stdOut << kCIDBuild::EndLn;
}
// Ok, lets do the compilation
tCIDLib::TCard4 c4ExecFlags = kCIDBuild::c4ExecFlag_None;
if (facCIDBuild.bLowPrio())
c4ExecFlags |= kCIDBuild::c4ExecFlag_LowPrio;
tCIDLib::TCard4 c4Result;
if (!TUtils::bExec(apszArgs, c4ExtraArgs, c4Result, c4ExecFlags))
{
stdOut << L"Could not execute the compiler" << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
if (c4Result)
{
stdOut << L"Failed compilation of file: "
<< depiCur.strFileName() << L". Error Code: "
<< c4Result << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
// Make sure we have a line after the output if in verbose mode
if (facCIDBuild.bVerbose())
stdOut << kCIDBuild::EndLn;
// Indicate that we built at least one Obj file
bBuiltSomething = true;
} while (cursCpps.bNext());
return bBuiltSomething;
}
tCIDLib::TBoolean TGCCDriver::bLibsNewer(const TFindInfo& fndiTarget) const
{
// Get a cursor for the libs list
TList<TFindInfo>::TCursor cursLibs(&listLibs());
// If no libs listed, then couldn't be newer
if (!cursLibs.bResetIter())
return false;
do
{
//
// See if this Lib file is newer than the target. If so, then
// return true, since we only have to find one to know that we
// have to relink.
//
if (cursLibs.tCurElement() > fndiTarget)
{
if (facCIDBuild.bVerbose())
{
stdOut << L" Dependent library is newer: "
<< cursLibs.tCurElement().strFileName() << kCIDBuild::EndLn;
}
return true;
}
} while (cursLibs.bNext());
return false;
}
tCIDLib::TBoolean TGCCDriver::bMakeRes()
{
return true;
}
tCIDLib::TBoolean TGCCDriver::bObjsNewer(const TFindInfo& fndiTarget) const
{
// Get a cursor for the Cpp list
TList<TDepInfo>::TCursor cursCpps(&listCpps());
// If no obect files listed, then couldn't be newer
if (!cursCpps.bResetIter())
return false;
TFindInfo fndiTmp;
do
{
// Get a ref to the current element
const TDepInfo& depiCur = cursCpps.tCurElement();
// Try to find the current obj
if (!TFindInfo::bFindAFile(depiCur.strObjFileName(), fndiTmp))
{
stdOut << L"Could not find object file: "
<< depiCur.strObjFileName() << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::NotFound;
}
//
// See if the Obj file is newer than the target. If so, then
// return true, since we only have to find one to know that we
// have to relink.
//
if (fndiTmp > fndiTarget)
{
if (facCIDBuild.bVerbose())
{
stdOut << L" Object file is newer: "
<< depiCur.strObjFileName() << kCIDBuild::EndLn;
}
return true;
}
} while (cursCpps.bNext());
return false;
}
tCIDLib::TVoid TGCCDriver::Link()
{
TList<TBldStr> listParms;
// Set up the standard stuff
listParms.Add(new TBldStr(L"g++"));
// Set up the output file
listParms.Add(new TBldStr(L"-o"));
listParms.Add(new TBldStr(m_strTargetFile));
// And the debug vs. production flags
if (m_bDebug)
{
listParms.Add(new TBldStr(L"-g"));
}
else
{
listParms.Add(new TBldStr(L"-O3"));
}
// 32 bit mode and standard C++ libraries
listParms.Add(new TBldStr(L"-lstdc++"));
listParms.Add(new TBldStr(L"-m32"));
if ((m_pprojiTarget->eType() == tCIDBuild::EProjTypes::Executable)
|| (m_pprojiTarget->eType() == tCIDBuild::EProjTypes::Service))
{
listParms.Add(new TBldStr(L"-lm"));
}
else if (m_pprojiTarget->eType() == tCIDBuild::EProjTypes::SharedLib)
{
listParms.Add(new TBldStr(L"-shared"));
}
// And now add in all of the object files
TList<TDepInfo>::TCursor cursCpps(&listCpps());
if (cursCpps.bResetIter())
{
do
{
// Get a ref to the current element
const TDepInfo& depiCur = cursCpps.tCurElement();
// And add its object file name to the list of args
listParms.Add(new TBldStr(depiCur.strObjFileName()));
} while (cursCpps.bNext());
}
// Multi-threaded support
listParms.Add(new TBldStr(L"-lpthread"));
// Cursor support
listParms.Add(new TBldStr(L"-lncurses"));
// Some special case stuff for CIDKernel
if (m_pprojiTarget->strProjectName() == TBldStr(L"CIDKernel"))
{
listParms.Add(new TBldStr(L"-lreadline"));
listParms.Add(new TBldStr(L"-lhistory"));
listParms.Add(new TBldStr(L"-ldl"));
}
// And now all of the libraries, the system ones then ours
TList<TFindInfo>::TCursor cursLibs(&listLibs());
if (cursLibs.bResetIter())
{
do
{
TBldStr* pstrNew = new TBldStr(cursLibs.tCurElement().strFileName());
listParms.Add(pstrNew);
} while (cursLibs.bNext());
}
TListCursor<TBldStr> cursParms(&listParms);
if (facCIDBuild.bVerbose())
{
cursParms.bResetIter();
do
{
stdOut << *cursParms << L" ";
} while (cursParms.bNext());
stdOut << kCIDBuild::EndLn;
}
// Get an array of points to all of the parameters that we pass into the exec
if (listParms.c4ElemCount() > 128)
{
stdOut << L"Too many linker parameters" << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
tCIDLib::TCard4 c4ParmInd = 0;
const tCIDBuild::TCh* apszArgs[128];
cursParms.bResetIter();
do
{
apszArgs[c4ParmInd++] = cursParms->pszBuffer();
} while (cursParms.bNext());
// And invoke the link
tCIDLib::TCard4 c4ExecFlags = kCIDBuild::c4ExecFlag_None;
if (facCIDBuild.bLowPrio())
c4ExecFlags |= kCIDBuild::c4ExecFlag_LowPrio;
tCIDLib::TCard4 c4Result;
if (!TUtils::bExec(apszArgs, c4ParmInd, c4Result, c4ExecFlags))
{
stdOut << L"Could not execute the linker" << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
if (c4Result)
{
stdOut << L"Link step failed. Error Code:" << c4Result << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
if (m_pprojiTarget->eType() == tCIDBuild::EProjTypes::SharedLib)
{
TBldStr strRealName(m_strTargetFile);
strRealName.Append(facCIDBuild.strVersionSuffix());
tCIDBuild::TSCh* pszRealName = TRawStr::pszTranscode(strRealName.pszBuffer());
TArrayJanitor<tCIDBuild::TSCh> janReal(pszRealName);
tCIDBuild::TSCh* pszTarget = TRawStr::pszTranscode(m_strTargetFile.pszBuffer());
TArrayJanitor<tCIDBuild::TSCh> janTarget(pszTarget);
if (::rename(pszTarget, pszRealName))
{
stdOut << L"Unable to rename target file" << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
if (::symlink(pszRealName, pszTarget))
{
stdOut << L"Unable to create symbolic link" << kCIDBuild::EndLn;
throw tCIDBuild::EErrors::BuildError;
}
}
}