-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilingLinking.cpp
More file actions
772 lines (656 loc) · 33.1 KB
/
Copy pathCompilingLinking.cpp
File metadata and controls
772 lines (656 loc) · 33.1 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#include "runcpp2/CompilingLinking.hpp"
#include "runcpp2/DependenciesHelper.hpp"
#include "runcpp2/PlatformUtil.hpp"
#include "runcpp2/StringUtil.hpp"
#include "runcpp2/Data/ScriptInfo.hpp"
#include "runcpp2/Data/DependencyLibraryType.hpp"
#include "ssLogger/ssLog.hpp"
#include "System2.h"
#include "ghc/filesystem.hpp"
namespace
{
using OverrideFlags =
std::unordered_map<PlatformName, runcpp2::Data::ProfilesFlagsOverride>;
//TODO: Add option to add/remove flags for each run option (executable, shared, static)
void AppendAndRemoveFlags( const runcpp2::Data::Profile& profile,
const OverrideFlags overrideFlags,
std::string& inOutFlags)
{
ssLOG_FUNC_DEBUG();
if(!runcpp2::HasValueFromPlatformMap(overrideFlags))
{
ssLOG_INFO("No override flags found for current platform");
return;
}
const runcpp2::Data::ProfilesFlagsOverride& currentFlagsOverride =
*runcpp2::GetValueFromPlatformMap(overrideFlags);
const runcpp2::Data::FlagsOverrideInfo* profileFlagsOverride =
runcpp2::GetValueFromProfileMap(profile, currentFlagsOverride.FlagsOverrides);
if(!profileFlagsOverride)
{
ssLOG_INFO("No override flags found for current profile");
return;
}
std::vector<std::string> flagsToRemove;
runcpp2::SplitString(profileFlagsOverride->Remove, " ", flagsToRemove);
for(int i = 0; i < flagsToRemove.size(); ++i)
{
const std::string currentFlagToRemove = flagsToRemove.at(i);
std::size_t foundIndex = inOutFlags.find(currentFlagToRemove);
if(foundIndex != std::string::npos)
{
//Remove the trailing space as well if the flag is in the middle
if(foundIndex + currentFlagToRemove.size() + 1 <= inOutFlags.size())
inOutFlags.erase(foundIndex, currentFlagToRemove.size() + 1);
else
inOutFlags.erase(foundIndex, currentFlagToRemove.size());
continue;
}
ssLOG_WARNING( "Flag to remove not found: " << currentFlagToRemove);
}
runcpp2::TrimRight(inOutFlags);
inOutFlags += " " + profileFlagsOverride->Append;
runcpp2::TrimRight(inOutFlags);
}
bool CompileScript( const ghc::filesystem::path& buildDir,
const ghc::filesystem::path& scriptPath,
const std::vector<ghc::filesystem::path>& sourceFiles,
const runcpp2::Data::ScriptInfo& scriptInfo,
const std::vector<runcpp2::Data::DependencyInfo*>& availableDependencies,
const runcpp2::Data::Profile& profile,
bool compileAsExecutable,
std::vector<ghc::filesystem::path>& outObjectsFilesPaths)
{
ssLOG_FUNC_DEBUG();
outObjectsFilesPaths.clear();
const runcpp2::Data::StageInfo::OutputTypeInfo* currentOutputTypeInfo =
compileAsExecutable ?
runcpp2::GetValueFromPlatformMap(profile.Compiler.OutputTypes.Executable) :
runcpp2::GetValueFromPlatformMap(profile.Compiler.OutputTypes.Shared);
if(currentOutputTypeInfo == nullptr)
{
ssLOG_ERROR("Failed to find current platform for Compiler in OutputTypes");
return false;
}
std::unordered_map<std::string, std::vector<std::string>> substitutionMapTemplate;
substitutionMapTemplate["{Executable}"] = {(*currentOutputTypeInfo).Executable};
//Compile flags
{
std::string compileFlags = (*currentOutputTypeInfo).Flags;
AppendAndRemoveFlags(profile, scriptInfo.OverrideCompileFlags, compileFlags);
substitutionMapTemplate["{CompileFlags}"] = {compileFlags};
}
//Include Directories
{
for(int i = 0; i < availableDependencies.size(); ++i)
{
for(int j = 0; j < availableDependencies.at(i)->AbsoluteIncludePaths.size(); ++j)
{
const std::string& currentIncludePath =
availableDependencies.at(i)->AbsoluteIncludePaths.at(j);
substitutionMapTemplate["{IncludeDirectoryPath}"].push_back(currentIncludePath);
}
}
}
// Add defines
if(runcpp2::HasValueFromPlatformMap(scriptInfo.Defines))
{
const runcpp2::Data::ProfilesDefines& platformDefines =
*runcpp2::GetValueFromPlatformMap(scriptInfo.Defines);
const std::vector<runcpp2::Data::Define>* profileDefines =
runcpp2::GetValueFromProfileMap(profile, platformDefines.Defines);
if(profileDefines)
{
for(int i = 0; i < profileDefines->size(); ++i)
{
const runcpp2::Data::Define& define = profileDefines->at(i);
if(define.HasValue)
{
substitutionMapTemplate["{DefineName}"].push_back(define.Name);
substitutionMapTemplate["{DefineValue}"].push_back(define.Value);
}
else
substitutionMapTemplate["{DefineNameOnly}"].push_back(define.Name);
}
}
}
std::unordered_map<std::string, std::vector<std::string>> substitutionMap;
substitutionMap = substitutionMapTemplate;
for(int i = 0; i < sourceFiles.size(); ++i)
{
std::error_code e;
ghc::filesystem::path currentSource = sourceFiles.at(i);
ghc::filesystem::path relativeSourcePath =
ghc::filesystem::relative(currentSource, scriptPath.parent_path(), e);
if(e)
{
ssLOG_ERROR("Failed to get relative path for " << currentSource);
ssLOG_ERROR("Failed with error: " << e.message());
return false;
}
//TODO: Maybe do ProcessPath on all the .string()?
std::string sourceDirectory = currentSource.parent_path().string();
std::string sourceName = currentSource.stem().string();
std::string sourceExt = currentSource.extension().string();
//Input File
{
substitutionMap["{InputFileName}"] = {sourceName};
substitutionMap["{InputFileExtension}"] = {sourceExt};
substitutionMap["{InputFileDirectory}"] = {sourceDirectory};
substitutionMap["{InputFilePath}"] = {currentSource.string()};
}
//Output File
{
std::string objectFileExt =
*runcpp2::GetValueFromPlatformMap(profile.FilesTypes.ObjectLinkFile.Extension);
if(objectFileExt.empty())
ssLOG_WARNING("Object file extension is empty");
ghc::filesystem::path outputFilePath = buildDir /
relativeSourcePath.parent_path() /
sourceName;
outputFilePath.concat(objectFileExt);
// Create the directory structure if it doesn't exist
ghc::filesystem::create_directories(outputFilePath.parent_path(), e);
if(e)
{
ssLOG_ERROR("Failed to create directory structure for " << outputFilePath);
ssLOG_ERROR("Failed with error: " << e.message());
return false;
}
substitutionMap["{OutputFileName}"] = {sourceName};
substitutionMap["{OutputFileExtension}"] = {objectFileExt};
substitutionMap["{OutputFileDirectory}"] =
{runcpp2::ProcessPath(outputFilePath.parent_path().string())};
substitutionMap["{OutputFilePath}"] =
{runcpp2::ProcessPath(outputFilePath.string())};
outObjectsFilesPaths.push_back(outputFilePath);
}
//Compile the source
{
//Getting PreRun command
std::string preRun =
runcpp2::HasValueFromPlatformMap(profile.Compiler.PreRun) ?
*runcpp2::GetValueFromPlatformMap(profile.Compiler.PreRun) : "";
//Run setup first if any
for(int j = 0; j < currentOutputTypeInfo->Setup.size(); ++j)
{
std::string setupStep = currentOutputTypeInfo->Setup.at(j);
if(!profile.Compiler.PerformSubstituions(substitutionMap, setupStep))
{
ssLOG_ERROR("Failed to substitute \"" << setupStep << "\"");
return false;
}
if(!preRun.empty())
setupStep = preRun + " && " + setupStep;
std::string setupOutput;
int setupResult;
if( !runcpp2::RunCommandAndGetOutput( setupStep,
setupOutput,
setupResult,
buildDir.string()) ||
setupResult != 0)
{
ssLOG_ERROR("Setup command \"" << setupStep << "\" failed");
ssLOG_ERROR("Failed with result " << setupResult);
ssLOG_ERROR("Failed with output: \n" << setupOutput);
return false;
}
}
std::string compileCommand;
//Construct the compile command
{
std::string runPartSubstitutedCommand;
if(!profile.Compiler.ConstructCommand( substitutionMap,
compileAsExecutable,
runPartSubstitutedCommand))
{
return false;
}
if(!preRun.empty())
compileCommand = preRun + " && " + runPartSubstitutedCommand;
else
compileCommand = runPartSubstitutedCommand;
ssLOG_INFO( "running compile command: " << compileCommand <<
" in " << buildDir.string());
std::string commandOutput;
int resultCode = 0;
if( !runcpp2::RunCommandAndGetOutput( compileCommand,
commandOutput,
resultCode,
buildDir.string()) ||
resultCode != 0)
{
ssLOG_ERROR("Compile command failed with result " << resultCode);
ssLOG_ERROR("Compile output: \n" << commandOutput);
return false;
}
else
{
//TODO: Make this configurable
//Attempt to capture warnings
if(commandOutput.find(" warning") != std::string::npos)
ssLOG_WARNING("Warning detected:\n" << commandOutput);
else
ssLOG_INFO("Compile output:\n" << commandOutput);
}
}
//Run cleanup if any
for(int j = 0; j < currentOutputTypeInfo->Cleanup.size(); ++j)
{
std::string cleanupStep = currentOutputTypeInfo->Cleanup.at(j);
if(!profile.Compiler.PerformSubstituions(substitutionMap, cleanupStep))
{
ssLOG_ERROR("Failed to substitute \"" << cleanupStep << "\"");
return false;
}
if(!preRun.empty())
cleanupStep = preRun + " && " + cleanupStep;
std::string cleanupOutput;
int cleanupResult;
if( !runcpp2::RunCommandAndGetOutput( cleanupStep,
cleanupOutput,
cleanupResult,
buildDir.string()) ||
cleanupResult != 0)
{
ssLOG_ERROR("Cleanup command \"" << cleanupStep << "\" failed");
ssLOG_ERROR("Failed with result " << cleanupResult);
ssLOG_ERROR("Failed with output: \n" << cleanupOutput);
return false;
}
}
}
}
return true;
}
bool LinkScript(const ghc::filesystem::path& buildDir,
const std::string& outputName,
const runcpp2::Data::ScriptInfo& scriptInfo,
const std::string& additionalLinkFlags,
const runcpp2::Data::Profile& profile,
const std::vector<ghc::filesystem::path>& objectsFilesPaths,
bool linkAsExecutable,
const std::string& exeExt)
{
ssLOG_FUNC_DEBUG();
const runcpp2::Data::StageInfo::OutputTypeInfo* currentOutputTypeInfo =
linkAsExecutable ?
runcpp2::GetValueFromPlatformMap(profile.Linker.OutputTypes.Executable) :
runcpp2::GetValueFromPlatformMap(profile.Linker.OutputTypes.Shared);
if(currentOutputTypeInfo == nullptr)
{
ssLOG_ERROR("Failed to find current platform for Linker in OutputTypes");
return false;
}
std::unordered_map<std::string, std::vector<std::string>> substitutionMap;
substitutionMap["{Executable}"] = {(*currentOutputTypeInfo).Executable};
//Link Flags
{
std::string linkFlags = (*currentOutputTypeInfo).Flags;
AppendAndRemoveFlags(profile, scriptInfo.OverrideLinkFlags, linkFlags);
if(!additionalLinkFlags.empty())
{
if(linkFlags.empty())
linkFlags = additionalLinkFlags;
else
linkFlags += std::string(" ") + additionalLinkFlags;
}
substitutionMap["{LinkFlags}"] = {linkFlags};
}
//Output File
{
const std::string sharedLibPrefix =
*runcpp2::GetValueFromPlatformMap(profile.FilesTypes.SharedLinkFile.Prefix);
const std::string sharedLibExtension =
*runcpp2::GetValueFromPlatformMap(profile .FilesTypes
.SharedLibraryFile
.Extension);
std::string outputFile = linkAsExecutable ?
outputName + exeExt :
sharedLibPrefix + outputName + sharedLibExtension;
outputFile = runcpp2::ProcessPath( (buildDir / outputFile).string() );
substitutionMap["{OutputFileName}"] = {outputName};
substitutionMap["{OutputFileExtension}"] =
{
(linkAsExecutable ? exeExt : sharedLibExtension)
};
substitutionMap["{OutputFileDirectory}"] = {buildDir.string()};
substitutionMap["{OutputFilePath}"] = {outputFile};
}
//Link Files
{
for(int i = 0; i < objectsFilesPaths.size(); ++i)
{
ssLOG_DEBUG("Trying to link " << objectsFilesPaths.at(i));
using namespace runcpp2;
//Check if this is a file we can link
std::string extension = GetFileExtensionWithoutVersion(objectsFilesPaths.at(i));
Data::DependencyLibraryType currentLinkType = Data::DependencyLibraryType::COUNT;
if(!HasValueFromPlatformMap(profile.FilesTypes.ObjectLinkFile.Extension))
{
ssLOG_WARNING( "profile " << profile.Name << " missing extension for " <<
"object link file");
}
else if(extension ==
*GetValueFromPlatformMap(profile.FilesTypes.ObjectLinkFile.Extension))
{
currentLinkType = Data::DependencyLibraryType::OBJECT;
goto processLinkFile;
}
//TODO: Shared and static link file cannot be differentiated with just
// file extension because they are the same. We should pass the dependency in
// instead
if(!HasValueFromPlatformMap(profile.FilesTypes.SharedLinkFile.Extension))
{
ssLOG_WARNING( "profile " << profile.Name << " missing extension for " <<
"shared link file");
}
else if(extension ==
*GetValueFromPlatformMap(profile.FilesTypes.SharedLinkFile.Extension))
{
currentLinkType = Data::DependencyLibraryType::SHARED;
goto processLinkFile;
}
if(!HasValueFromPlatformMap(profile.FilesTypes.StaticLinkFile.Extension))
{
ssLOG_WARNING( "profile " << profile.Name << " missing extension for " <<
"static link file");
}
else if(extension ==
*GetValueFromPlatformMap(profile.FilesTypes.StaticLinkFile.Extension))
{
currentLinkType = Data::DependencyLibraryType::STATIC;
if(!linkAsExecutable)
{
ssLOG_WARNING( "Trying to link static dependency when script is being " <<
"built as shared. Linking might not work on some platforms.");
//TODO: Maybe revert the default back to executable?
ssLOG_WARNING( "If linking fails, run with -e instead");
}
goto processLinkFile;
}
ssLOG_WARNING("Failing to match current extension: " << extension);
processLinkFile:;
if(currentLinkType == Data::DependencyLibraryType::COUNT)
{
ssLOG_DEBUG("Skip linking " << objectsFilesPaths.at(i));
continue;
}
auto depLinkParsedPath = objectsFilesPaths.at(i);
std::string depLinkDirectory = depLinkParsedPath.parent_path().string();
std::string depLinkName = depLinkParsedPath.stem().string();
std::string depLinkExt = depLinkParsedPath.extension().string();
substitutionMap["{LinkFileName}"].push_back(depLinkName);
substitutionMap["{LinkFileExt}"].push_back(depLinkExt);
substitutionMap["{LinkFileDirectory}"].push_back(depLinkDirectory);
const std::string processedLinkFilePath =
runcpp2::ProcessPath(objectsFilesPaths.at(i));
substitutionMap["{LinkFilePath}"].push_back(processedLinkFilePath);
static_assert( static_cast<int>(Data::DependencyLibraryType::COUNT) == 4,
"Add new type to be processed");
switch(currentLinkType)
{
case Data::DependencyLibraryType::STATIC:
{
substitutionMap["{LinkStaticFileName}"].push_back(depLinkName);
substitutionMap["{LinkStaticFileExt}"].push_back(depLinkExt);
substitutionMap["{LinkStaticFileDirectory}"].push_back(depLinkDirectory);
substitutionMap["{LinkStaticFilePath}"].push_back(processedLinkFilePath);
break;
}
case Data::DependencyLibraryType::SHARED:
{
substitutionMap["{LinkSharedFileName}"].push_back(depLinkName);
substitutionMap["{LinkSharedFileExt}"].push_back(depLinkExt);
substitutionMap["{LinkSharedFileDirectory}"].push_back(depLinkDirectory);
substitutionMap["{LinkSharedFilePath}"].push_back(processedLinkFilePath);
break;
}
case Data::DependencyLibraryType::OBJECT:
{
substitutionMap["{LinkObjectFileName}"].push_back(depLinkName);
substitutionMap["{LinkObjectFileExt}"].push_back(depLinkExt);
substitutionMap["{LinkObjectFileDirectory}"].push_back(depLinkDirectory);
substitutionMap["{LinkObjectFilePath}"].push_back(processedLinkFilePath);
break;
}
case Data::DependencyLibraryType::HEADER:
case Data::DependencyLibraryType::COUNT:
{
ssLOG_WARNING( "Unexpected currentLinkType: " <<
static_cast<int>(currentLinkType) <<
" for " << objectsFilesPaths.at(i));
break;
}
}
}
}
//Link the script
{
//Getting PreRun command
std::string preRun = runcpp2::HasValueFromPlatformMap(profile.Linker.PreRun) ?
*runcpp2::GetValueFromPlatformMap(profile.Linker.PreRun) : "";
//Run setup first if any
for(int i = 0; i < currentOutputTypeInfo->Setup.size(); ++i)
{
std::string setupStep = currentOutputTypeInfo->Setup.at(i);
if(!profile.Linker.PerformSubstituions(substitutionMap, setupStep))
{
ssLOG_ERROR("Failed to substitute \"" << setupStep << "\"");
return false;
}
if(!preRun.empty())
setupStep = preRun + " && " + setupStep;
std::string setupOutput;
int setupResult;
if( !runcpp2::RunCommandAndGetOutput( setupStep,
setupOutput,
setupResult,
buildDir.string()) ||
setupResult != 0)
{
ssLOG_ERROR("Setup command \"" << setupStep << "\" failed");
ssLOG_ERROR("Failed with result " << setupResult);
ssLOG_ERROR("Failed with output: \n" << setupOutput);
return false;
}
}
std::string linkCommand;
//Construct the link command
{
std::string runPartSubstitutedCommand;
if(!profile.Linker.ConstructCommand(substitutionMap,
linkAsExecutable,
runPartSubstitutedCommand))
{
return false;
}
if(!preRun.empty())
linkCommand = preRun + " && " + runPartSubstitutedCommand;
else
linkCommand = runPartSubstitutedCommand;
ssLOG_INFO("running link command: " << linkCommand << " in " << buildDir.string());
std::string linkOutput;
int resultCode = 0;
if( !runcpp2::RunCommandAndGetOutput( linkCommand,
linkOutput,
resultCode,
buildDir.string()) ||
resultCode != 0)
{
ssLOG_ERROR("Link command failed with result " << resultCode);
ssLOG_ERROR("Link output: \n" << linkOutput);
return false;
}
else
ssLOG_INFO("Link output:\n" << linkOutput);
}
//Run cleanup if any
for(int i = 0; i < currentOutputTypeInfo->Cleanup.size(); ++i)
{
std::string cleanupStep = currentOutputTypeInfo->Cleanup.at(i);
if(!profile.Linker.PerformSubstituions(substitutionMap, cleanupStep))
{
ssLOG_ERROR("Failed to substitute \"" << cleanupStep << "\"");
return false;
}
if(!preRun.empty())
cleanupStep = preRun + " && " + cleanupStep;
std::string cleanupOutput;
int cleanupResult;
if( !runcpp2::RunCommandAndGetOutput( cleanupStep,
cleanupOutput,
cleanupResult,
buildDir.string()) ||
cleanupResult != 0)
{
ssLOG_ERROR("Cleanup command \"" << cleanupStep << "\" failed");
ssLOG_ERROR("Failed with result " << cleanupResult);
ssLOG_ERROR("Failed with output: \n" << cleanupOutput);
return false;
}
}
}
return true;
}
bool RunGlobalSteps(const ghc::filesystem::path& buildDir,
const std::unordered_map< PlatformName,
std::vector<std::string>>& platformSteps)
{
if(platformSteps.empty())
return true;
if( !runcpp2::HasValueFromPlatformMap(platformSteps) ||
runcpp2::GetValueFromPlatformMap(platformSteps)->empty())
{
return true;
}
const std::vector<std::string>& steps = *runcpp2::GetValueFromPlatformMap(platformSteps);
for(int i = 0; i < steps.size(); ++i)
{
std::string commandOutput;
int commandResult = 0;
if( !runcpp2::RunCommandAndGetOutput( steps.at(i),
commandOutput,
commandResult,
buildDir.string()) ||
commandResult != 0)
{
ssLOG_ERROR("Command \"" << steps.at(i) << "\" failed");
ssLOG_ERROR("Failed with result " << commandResult);
ssLOG_ERROR("Failed with output: \n" << commandOutput);
return false;
}
}
return true;
}
}
bool runcpp2::CompileScriptOnly(const ghc::filesystem::path& buildDir,
const ghc::filesystem::path& scriptPath,
const std::vector<ghc::filesystem::path>& sourceFiles,
const std::vector<bool>& sourceHasCache,
const Data::ScriptInfo& scriptInfo,
const std::vector<Data::DependencyInfo*>& availableDependencies,
const Data::Profile& profile,
bool buildExecutable)
{
if(!RunGlobalSteps(buildDir, profile.Setup))
{
ssLOG_ERROR("Failed to run profile global setup steps");
return false;
}
std::vector<ghc::filesystem::path> sourceFilesNeededToCompile;
for(int i = 0; i < sourceFiles.size(); ++i)
{
if(!sourceHasCache.at(i))
sourceFilesNeededToCompile.push_back(sourceFiles.at(i));
}
std::vector<ghc::filesystem::path> objectsFilesPaths;
if(!CompileScript( buildDir,
scriptPath,
sourceFilesNeededToCompile,
scriptInfo,
availableDependencies,
profile,
buildExecutable,
objectsFilesPaths))
{
ssLOG_ERROR("CompileScript failed");
return false;
}
return true;
}
bool runcpp2::CompileAndLinkScript( const ghc::filesystem::path& buildDir,
const ghc::filesystem::path& scriptPath,
const std::string& outputName,
const std::vector<ghc::filesystem::path>& sourceFiles,
const std::vector<bool>& sourceHasCache,
const Data::ScriptInfo& scriptInfo,
const std::vector<Data::DependencyInfo*>& availableDependencies,
const Data::Profile& profile,
const std::vector<std::string>& compiledObjectsPaths,
bool buildExecutable,
const std::string exeExt)
{
if(!RunGlobalSteps(buildDir, profile.Setup))
{
ssLOG_ERROR("Failed to run profile global setup steps");
return false;
}
std::vector<ghc::filesystem::path> sourceFilesNeededToCompile;
for(int i = 0; i < sourceFiles.size(); ++i)
{
if(!sourceHasCache.at(i))
sourceFilesNeededToCompile.push_back(sourceFiles.at(i));
}
std::vector<ghc::filesystem::path> objectsFilesPaths;
//Compile source files that don't have cache
if(!CompileScript( buildDir,
scriptPath,
sourceFilesNeededToCompile,
scriptInfo,
availableDependencies,
profile,
buildExecutable,
objectsFilesPaths))
{
ssLOG_ERROR("CompileScript failed");
return false;
}
//Add compiled object files
for(int i = 0; i < compiledObjectsPaths.size(); ++i)
objectsFilesPaths.push_back(compiledObjectsPaths.at(i));
std::string dependenciesLinkFlags;
//Add link flags for the dependencies
for(int i = 0; i < availableDependencies.size(); ++i)
{
if(!runcpp2::HasValueFromPlatformMap(availableDependencies.at(i)->LinkProperties))
continue;
const runcpp2::Data::DependencyLinkProperty& linkProperty =
*runcpp2::GetValueFromPlatformMap(availableDependencies.at(i)->LinkProperties);
const runcpp2::Data::ProfileLinkProperty* profileLinkProperty =
runcpp2::GetValueFromProfileMap(profile, linkProperty.ProfileProperties);
if(!profileLinkProperty)
continue;
for(const std::string& option : profileLinkProperty->AdditionalLinkOptions)
dependenciesLinkFlags += option + " ";
}
runcpp2::TrimRight(dependenciesLinkFlags);
if(!LinkScript( buildDir,
outputName,
scriptInfo,
dependenciesLinkFlags,
profile,
objectsFilesPaths,
buildExecutable,
exeExt))
{
ssLOG_ERROR("LinkScript failed");
return false;
}
if(!RunGlobalSteps(buildDir, profile.Cleanup))
{
ssLOG_ERROR("Failed to run profile global cleanup steps");
return false;
}
return true;
}