-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipelineSteps.cpp
More file actions
1263 lines (1062 loc) · 46.3 KB
/
Copy pathPipelineSteps.cpp
File metadata and controls
1263 lines (1062 loc) · 46.3 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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "runcpp2/PipelineSteps.hpp"
#include "runcpp2/ProfileHelper.hpp"
#include "runcpp2/ConfigParsing.hpp"
#include "runcpp2/DependenciesHelper.hpp"
#include "runcpp2/ParseUtil.hpp"
#include "runcpp2/PlatformUtil.hpp"
#include "runcpp2/Data/BuildTypeHelper.hpp"
#include "System2.h"
#include "ssLogger/ssLog.hpp"
#include "ghc/filesystem.hpp"
#include "dylib.hpp"
#include <queue>
namespace
{
bool RunCompiledScript( const ghc::filesystem::path& executable,
const std::string& scriptPath,
const std::vector<std::string>& runArgs,
int& returnStatus)
{
INTERNAL_RUNCPP2_SAFE_START();
ssLOG_FUNC_INFO();
std::vector<const char*> args;
for(size_t i = 0; i < runArgs.size(); ++i)
args.push_back(runArgs[i].c_str());
System2CommandInfo runCommandInfo = {};
SYSTEM2_RESULT result = System2RunSubprocess( executable.c_str(),
args.data(),
args.size(),
&runCommandInfo);
ssLOG_INFO("Running: " << executable.string());
for(size_t i = 0; i < runArgs.size(); ++i)
ssLOG_INFO("- " << runArgs[i]);
if(result != SYSTEM2_RESULT_SUCCESS)
{
ssLOG_ERROR("System2Run failed with result: " << result);
return false;
}
result = System2GetCommandReturnValueSync(&runCommandInfo, &returnStatus, false);
if(result != SYSTEM2_RESULT_SUCCESS)
{
ssLOG_ERROR("System2GetCommandReturnValueSync failed with result: " << result);
return false;
}
return true;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
}
bool RunCompiledSharedLib( const std::string& scriptPath,
const ghc::filesystem::path& compiledSharedLibPath,
const std::vector<std::string>& runArgs,
int& returnStatus)
{
INTERNAL_RUNCPP2_SAFE_START();
ssLOG_FUNC_INFO();
std::error_code _;
if(!ghc::filesystem::exists(compiledSharedLibPath, _))
{
ssLOG_ERROR("Failed to find shared library: " << compiledSharedLibPath.string());
return false;
}
//Load it
std::unique_ptr<dylib> sharedLib;
try
{
ssLOG_INFO("Trying to run shared library: " << compiledSharedLibPath.string());
//TODO: We might want to use unicode instead for the path
#if defined(_WIN32)
std::string sharedLibDir = compiledSharedLibPath.parent_path().string();
if(SetDllDirectoryA(sharedLibDir.c_str()) == FALSE)
{
std::string lastError = runcpp2::GetWindowsError();
ssLOG_ERROR("Failed to set DLL directory: " << lastError);
return false;
}
#endif
sharedLib = std::unique_ptr<dylib>(new dylib( compiledSharedLibPath.string(),
dylib::no_filename_decorations));
}
catch(std::exception& e)
{
ssLOG_ERROR("Failed to load shared library " << compiledSharedLibPath.string() <<
" with exception: ");
ssLOG_ERROR(e.what());
return false;
}
//Get main as entry point
if(sharedLib->has_symbol("main") == false)
{
ssLOG_ERROR("The shared library does not have a main function");
return false;
}
int (*scriptFullMain)(int, const char**) = nullptr;
int (*scriptMain)() = nullptr;
try
{
scriptFullMain = sharedLib->get_function<int(int, const char**)>("main");
}
catch(const dylib::exception& ex)
{
ssLOG_DEBUG("Failed to get full main function from shared library: " << ex.what());
}
catch(...)
{
ssLOG_ERROR("Failed to get entry point function");
return false;
}
if(scriptFullMain == nullptr)
{
try
{
scriptMain = sharedLib->get_function<int()>("_main");
}
catch(const dylib::exception& ex)
{
ssLOG_DEBUG("Failed to get main function from shared library: " << ex.what());
}
catch(...)
{
ssLOG_ERROR("Failed to get entry point function");
return false;
}
}
if(scriptMain == nullptr && scriptFullMain == nullptr)
{
ssLOG_ERROR("Failed to load function");
return false;
}
//Run the entry point
try
{
if(scriptFullMain != nullptr)
{
std::vector<const char*> runArgsCStr(runArgs.size());
for(size_t i = 0; i < runArgs.size(); ++i)
runArgsCStr.at(i) = &runArgs.at(i).at(0);
returnStatus = scriptFullMain(runArgsCStr.size(), runArgsCStr.data());
}
else if(scriptMain != nullptr)
returnStatus = scriptMain();
}
catch(std::exception& e)
{
ssLOG_ERROR("Failed to run script main with exception: " << e.what());
return true;
}
catch(...)
{
ssLOG_ERROR("Unknown exception caught");
return true;
}
return true;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
}
}
bool runcpp2::CopyFiles(const ghc::filesystem::path& destDir,
const std::vector<std::string>& filePaths,
std::vector<std::string>& outCopiedPaths)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
std::error_code e;
for (const std::string& srcPath : filePaths)
{
ghc::filesystem::path destPath = destDir / ghc::filesystem::path(srcPath).filename();
if(ghc::filesystem::exists(srcPath, e))
{
ghc::filesystem::copy(srcPath,
destPath,
ghc::filesystem::copy_options::update_existing,
e);
if(e)
{
ssLOG_ERROR("Failed to copy file from " << srcPath <<
" to " << destPath.string());
ssLOG_ERROR("Error: " << e.message());
return false;
}
ssLOG_INFO("Copied from " << srcPath << " to " << destPath.string());
outCopiedPaths.push_back(ProcessPath(destPath));
}
else
{
ssLOG_ERROR("File to copy not found: " << srcPath);
return false;
}
}
return true;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
}
runcpp2::PipelineResult
runcpp2::RunProfileCommands(const Data::ProfilesCommands* commands,
const Data::Profile& profile,
const std::string& workingDir,
const std::string& commandType)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
if(commands != nullptr)
{
const std::vector<std::string>* commandSteps =
runcpp2::GetValueFromProfileMap(profile, commands->CommandSteps);
if(commandSteps != nullptr)
{
for(const std::string& cmd : *commandSteps)
{
std::string output;
int returnCode = 0;
if(!runcpp2::RunCommand(cmd, true, workingDir, output, returnCode))
{
ssLOG_ERROR(commandType << " command failed: " << cmd <<
" with return code " << returnCode);
ssLOG_ERROR("Was trying to run: " << cmd);
ssLOG_ERROR("Output: \n" << output);
return PipelineResult::UNEXPECTED_FAILURE;
}
ssLOG_INFO(commandType << " command ran: \n" << cmd);
ssLOG_INFO(commandType << " command output: \n" << output);
}
}
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult runcpp2::ValidateInputs(const std::string& scriptPath,
const std::vector<Data::Profile>& profiles,
ghc::filesystem::path& outAbsoluteScriptPath,
ghc::filesystem::path& outScriptDirectory,
std::string& outScriptName)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
if(profiles.empty())
{
ssLOG_ERROR("No compiler profiles found");
return PipelineResult::EMPTY_PROFILES;
}
//Check if input file exists
std::error_code _;
if(!ghc::filesystem::exists(scriptPath, _))
{
ssLOG_ERROR("File does not exist: " << scriptPath);
return PipelineResult::INVALID_SCRIPT_PATH;
}
if(ghc::filesystem::is_directory(scriptPath, _))
{
ssLOG_ERROR("The input file must not be a directory: " << scriptPath);
return PipelineResult::INVALID_SCRIPT_PATH;
}
outAbsoluteScriptPath = ghc::filesystem::absolute(ghc::filesystem::canonical(scriptPath, _));
outScriptDirectory = outAbsoluteScriptPath.parent_path();
outScriptName = outAbsoluteScriptPath.stem().string();
ssLOG_DEBUG("scriptPath: " << scriptPath);
ssLOG_DEBUG("absoluteScriptPath: " << outAbsoluteScriptPath.string());
ssLOG_DEBUG("scriptDirectory: " << outScriptDirectory.string());
ssLOG_DEBUG("scriptName: " << outScriptName);
ssLOG_DEBUG("is_directory: " << ghc::filesystem::is_directory(outScriptDirectory));
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult
runcpp2::ParseAndValidateScriptInfo(const ghc::filesystem::path& absoluteScriptPath,
const ghc::filesystem::path& scriptDirectory,
const std::string& scriptName,
const bool buildExecutable,
Data::ScriptInfo& outScriptInfo)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
//Check if there's script info as yaml file instead
std::error_code e;
std::string parsableInfo;
std::ifstream inputFile;
ghc::filesystem::path dedicatedYamlLoc =
scriptDirectory / ghc::filesystem::path(scriptName + ".yaml");
if(ghc::filesystem::exists(dedicatedYamlLoc, e))
{
//Record write time for yaml file for watch option
outScriptInfo.LastWriteTime = ghc::filesystem::last_write_time(dedicatedYamlLoc, e);
if(e)
{
ssLOG_ERROR("Failed to get last write time for: " << dedicatedYamlLoc);
return PipelineResult::INVALID_SCRIPT_INFO;
}
inputFile.open(dedicatedYamlLoc);
std::stringstream buffer;
buffer << inputFile.rdbuf();
parsableInfo = buffer.str();
}
else
{
//Record write time for script file for watch option
outScriptInfo.LastWriteTime = ghc::filesystem::last_write_time(absoluteScriptPath, e);
if(e)
{
ssLOG_ERROR("Failed to get last write time for: " << absoluteScriptPath);
return PipelineResult::INVALID_SCRIPT_INFO;
}
inputFile.open(absoluteScriptPath);
if (!inputFile)
{
ssLOG_ERROR("Failed to open file: " << absoluteScriptPath);
return PipelineResult::INVALID_SCRIPT_PATH;
}
std::stringstream buffer;
buffer << inputFile.rdbuf();
std::string source(buffer.str());
if(!GetParsableInfo(source, parsableInfo))
{
ssLOG_ERROR("An error has been encountered when parsing info: " << absoluteScriptPath);
return PipelineResult::INVALID_SCRIPT_INFO;
}
}
//Try to parse the runcpp2 info
if(!ParseScriptInfo(parsableInfo, outScriptInfo))
{
ssLOG_ERROR("Failed to parse info");
ssLOG_ERROR("Content trying to parse: " << "\n" << parsableInfo);
return PipelineResult::INVALID_SCRIPT_INFO;
}
if(!parsableInfo.empty())
{
ssLOG_DEBUG("Parsed script info YAML:");
ssLOG_DEBUG("\n" << outScriptInfo.ToString(""));
}
//Replace build type with internal executable type to trigger recompiling when switching to
//have or not have "--executable" option
if(outScriptInfo.CurrentBuildType == Data::BuildType::EXECUTABLE)
{
outScriptInfo.CurrentBuildType = buildExecutable ?
Data::BuildType::INTERNAL_EXECUTABLE_EXECUTABLE :
Data::BuildType::INTERNAL_EXECUTABLE_SHARED;
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult runcpp2::HandleCleanup( const Data::ScriptInfo& scriptInfo,
const Data::Profile& profile,
const ghc::filesystem::path& scriptDirectory,
const ghc::filesystem::path& buildDir,
const ghc::filesystem::path& absoluteScriptPath,
BuildsManager& buildsManager)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
const Data::ProfilesCommands* cleanupCommands =
runcpp2::GetValueFromPlatformMap(scriptInfo.Cleanup);
if(cleanupCommands != nullptr)
{
const std::vector<std::string>* commands =
runcpp2::GetValueFromProfileMap(profile, cleanupCommands->CommandSteps);
if(commands != nullptr)
{
for(const std::string& cmd : *commands)
{
std::string output;
int returnCode = 0;
if(!runcpp2::RunCommand(cmd, true, scriptDirectory, output, returnCode))
{
ssLOG_ERROR("Cleanup command failed: " << cmd <<
" with return code " << returnCode);
ssLOG_ERROR("Output: \n" << output);
return PipelineResult::UNEXPECTED_FAILURE;
}
ssLOG_INFO("Cleanup command ran: \n" << cmd);
ssLOG_INFO("Cleanup command output: \n" << output);
}
}
}
//Remove build directory
std::error_code e;
if(!ghc::filesystem::remove_all(buildDir, e))
{
ssLOG_ERROR("Failed to remove build directory: " << buildDir);
return PipelineResult::UNEXPECTED_FAILURE;
}
if(!buildsManager.RemoveBuildMapping(absoluteScriptPath))
{
ssLOG_ERROR("Failed to remove build mapping");
return PipelineResult::UNEXPECTED_FAILURE;
}
if(!buildsManager.SaveBuildsMappings())
{
ssLOG_ERROR("Failed to save build mappings");
return PipelineResult::UNEXPECTED_FAILURE;
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult
runcpp2::InitializeBuildDirectory( const ghc::filesystem::path& configDir,
const ghc::filesystem::path& absoluteScriptPath,
bool useLocalBuildDir,
BuildsManager& outBuildsManager,
ghc::filesystem::path& outBuildDir,
IncludeManager& outIncludeManager)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
//Create build directory
ghc::filesystem::path buildDirPath = useLocalBuildDir ?
ghc::filesystem::current_path() / ".runcpp2" :
configDir;
//Create a class that manages build folder
outBuildsManager = BuildsManager(buildDirPath);
if(!outBuildsManager.Initialize())
{
ssLOG_FATAL("Failed to initialize builds manager");
return PipelineResult::INVALID_BUILD_DIR;
}
bool createdBuildDir = false;
bool writeMapping = false;
if(!outBuildsManager.HasBuildMapping(absoluteScriptPath))
writeMapping = true;
if(outBuildsManager.GetBuildMapping(absoluteScriptPath, outBuildDir))
{
if(writeMapping && !outBuildsManager.SaveBuildsMappings())
ssLOG_FATAL("Failed to save builds mappings");
else
createdBuildDir = true;
}
if(!createdBuildDir)
{
ssLOG_FATAL("Failed to create local build directory for: " << absoluteScriptPath);
return PipelineResult::INVALID_BUILD_DIR;
}
outIncludeManager = IncludeManager();
if(!outIncludeManager.Initialize(outBuildDir))
{
ssLOG_FATAL("Failed to initialize include manager");
return PipelineResult::INVALID_BUILD_DIR;
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult
runcpp2::ResolveScriptImports( Data::ScriptInfo& scriptInfo,
const ghc::filesystem::path& scriptPath,
const ghc::filesystem::path& buildDir)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
//Resolve all the script info imports first before evaluating it
if(!ResolveImports(scriptInfo, scriptPath, buildDir))
{
ssLOG_ERROR("Failed to resolve imports");
return PipelineResult::UNEXPECTED_FAILURE;
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult
runcpp2::CheckScriptInfoChanges(const ghc::filesystem::path& buildDir,
const Data::ScriptInfo& scriptInfo,
const Data::Profile& profile,
const ghc::filesystem::path& absoluteScriptPath,
const Data::ScriptInfo* lastScriptInfo,
const int maxThreads,
bool& outAllRecompileNeeded,
bool& outRelinkNeeded,
std::vector<std::string>& outChangedDependencies)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
const ghc::filesystem::path scriptDirectory = absoluteScriptPath.parent_path();
ghc::filesystem::path lastScriptInfoFilePath = buildDir / "LastScriptInfo.yaml";
Data::ScriptInfo lastScriptInfoFromDisk;
std::error_code e;
//Run Setup commands if we don't have previous build
if(!ghc::filesystem::exists(lastScriptInfoFilePath, e))
{
const Data::ProfilesCommands* setupCommands =
runcpp2::GetValueFromPlatformMap(scriptInfo.Setup);
if(setupCommands != nullptr)
{
PipelineResult result = RunProfileCommands( setupCommands,
profile,
scriptDirectory.string(),
"Setup");
if(result != PipelineResult::SUCCESS)
return result;
}
}
//Compare script info in memory or from disk
const Data::ScriptInfo* lastInfo = lastScriptInfo;
if(lastInfo == nullptr && ghc::filesystem::exists(lastScriptInfoFilePath, e))
{
ssLOG_DEBUG("Last script info file exists: " << lastScriptInfoFilePath);
std::ifstream lastScriptInfoFile;
lastScriptInfoFile.open(lastScriptInfoFilePath);
std::stringstream lastScriptInfoBuffer;
lastScriptInfoBuffer << lastScriptInfoFile.rdbuf();
int currentThreadTargetLevel = ssLOG_GET_CURRENT_THREAD_TARGET_LEVEL();
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(ssLOG_LEVEL_NONE);
do
{
if(!ParseScriptInfo(lastScriptInfoBuffer.str(), lastScriptInfoFromDisk))
break;
//Resolve imports for last script info
runcpp2::PipelineResult result = ResolveScriptImports( lastScriptInfoFromDisk,
absoluteScriptPath,
buildDir);
if(result != PipelineResult::SUCCESS)
break;
lastInfo = &lastScriptInfoFromDisk;
}
while(false);
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(currentThreadTargetLevel);
if(lastInfo != nullptr)
ssLOG_INFO("Last script info parsed");
else
ssLOG_INFO("Failed to parse last script info");
}
//Check if the cached script info has changed
if(lastInfo != nullptr)
{
//Relink if there are any changes to the link flags
{
const Data::ProfilesFlagsOverride* lastLinkFlags =
runcpp2::GetValueFromPlatformMap(lastInfo->OverrideLinkFlags);
const Data::ProfilesFlagsOverride* currentLinkFlags =
runcpp2::GetValueFromPlatformMap(scriptInfo.OverrideLinkFlags);
outRelinkNeeded = (lastLinkFlags == nullptr) != (currentLinkFlags == nullptr) ||
(
lastLinkFlags != nullptr &&
currentLinkFlags != nullptr &&
!lastLinkFlags->Equals(*currentLinkFlags)
);
}
outAllRecompileNeeded = scriptInfo.IsAllCompiledCacheInvalidated(*lastInfo);
//Check dependencies
for(int i = 0; i < scriptInfo.Dependencies.size(); ++i)
{
if( lastInfo->Dependencies.size() <= i ||
!scriptInfo.Dependencies.at(i).Equals(lastInfo->Dependencies.at(i)))
{
outChangedDependencies.push_back(scriptInfo.Dependencies.at(i).Name);
}
}
if(outAllRecompileNeeded || outRelinkNeeded)
{
ssLOG_INFO( "Last script info is out of date, " <<
(outAllRecompileNeeded ? "recompiling..." : "relinking..."));
}
}
else
outAllRecompileNeeded = true;
ssLOG_DEBUG("recompileNeeded: " << outAllRecompileNeeded <<
", changedDependencies.size(): " << outChangedDependencies.size() <<
", relinkNeeded: " << outRelinkNeeded);
//Write to file if there's any changes to the current script info
if( !lastInfo ||
outAllRecompileNeeded ||
!outChangedDependencies.empty() ||
outRelinkNeeded ||
!scriptInfo.Equals(*lastInfo))
{
std::ofstream writeOutputFile(lastScriptInfoFilePath);
if(!writeOutputFile)
{
ssLOG_ERROR("Failed to open file: " << lastScriptInfoFilePath);
return PipelineResult::INVALID_BUILD_DIR;
}
writeOutputFile << scriptInfo.ToString("");
ssLOG_DEBUG("Wrote current script info to " << lastScriptInfoFilePath.string());
}
if(!lastInfo)
return PipelineResult::SUCCESS;
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult
runcpp2::ProcessDependencies( Data::ScriptInfo& scriptInfo,
const Data::Profile& profile,
const ghc::filesystem::path& absoluteScriptPath,
const ghc::filesystem::path& buildDir,
const std::unordered_map<CmdOptions, std::string>& currentOptions,
const std::vector<std::string>& changedDependencies,
const int maxThreads,
std::vector<Data::DependencyInfo*>& outAvailableDependencies,
std::vector<std::string>& outGatheredBinariesPaths)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
for(int i = 0; i < scriptInfo.Dependencies.size(); ++i)
{
if(IsDependencyAvailableForThisPlatform(scriptInfo.Dependencies.at(i)))
outAvailableDependencies.push_back(&scriptInfo.Dependencies.at(i));
}
std::vector<std::string> dependenciesLocalCopiesPaths;
std::vector<std::string> dependenciesSourcePaths;
if(!GetDependenciesPaths( outAvailableDependencies,
dependenciesLocalCopiesPaths,
dependenciesSourcePaths,
absoluteScriptPath,
buildDir))
{
ssLOG_ERROR("Failed to get dependencies paths");
return PipelineResult::DEPENDENCIES_FAILED;
}
if(currentOptions.count(CmdOptions::RESET_DEPENDENCIES) > 0 || !changedDependencies.empty())
{
if(currentOptions.count(CmdOptions::BUILD_SOURCE_ONLY) > 0)
{
ssLOG_ERROR("Dependencies settings have changed or being reset explicitly.");
ssLOG_ERROR("Cannot just build source files only without building dependencies");
return PipelineResult::INVALID_OPTION;
}
std::string depsToReset = "all";
if(!changedDependencies.empty())
{
depsToReset = changedDependencies[0];
for(int i = 1; i < changedDependencies.size(); ++i)
depsToReset += "," + changedDependencies[i];
}
if(!CleanupDependencies(profile,
scriptInfo,
outAvailableDependencies,
dependenciesLocalCopiesPaths,
currentOptions.count(CmdOptions::RESET_DEPENDENCIES) > 0 ?
currentOptions.at(CmdOptions::RESET_DEPENDENCIES) :
depsToReset))
{
ssLOG_ERROR("Failed to cleanup dependencies");
return PipelineResult::DEPENDENCIES_FAILED;
}
}
if(currentOptions.count(CmdOptions::RESET_DEPENDENCIES) > 0)
return PipelineResult::SUCCESS;
if(!SetupDependenciesIfNeeded( profile,
buildDir,
scriptInfo,
outAvailableDependencies,
dependenciesLocalCopiesPaths,
dependenciesSourcePaths,
maxThreads))
{
ssLOG_ERROR("Failed to setup script dependencies");
return PipelineResult::DEPENDENCIES_FAILED;
}
//Sync local dependencies before building
if(!SyncLocalDependencies( outAvailableDependencies,
dependenciesSourcePaths,
dependenciesLocalCopiesPaths))
{
ssLOG_ERROR("Failed to sync local dependencies");
return PipelineResult::DEPENDENCIES_FAILED;
}
if(currentOptions.count(CmdOptions::BUILD_SOURCE_ONLY) == 0)
{
if(!BuildDependencies( profile,
scriptInfo,
outAvailableDependencies,
dependenciesLocalCopiesPaths,
maxThreads))
{
ssLOG_ERROR("Failed to build script dependencies. Maybe try resetting dependencies "
"with \"-rd all\" and run again?");
return PipelineResult::DEPENDENCIES_FAILED;
}
}
if(!GatherDependenciesBinaries( outAvailableDependencies,
dependenciesLocalCopiesPaths,
profile,
outGatheredBinariesPaths))
{
ssLOG_ERROR("Failed to gather dependencies binaries");
return PipelineResult::DEPENDENCIES_FAILED;
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
void runcpp2::SeparateDependencyFiles( const Data::FilesTypesInfo& filesTypes,
const std::vector<std::string>& gatheredBinariesPaths,
std::vector<std::string>& outLinkFilesPaths,
std::vector<std::string>& outFilesToCopyPaths)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
std::unordered_set<std::string> linkExtensions;
//Populate the set of link extensions
if(runcpp2::HasValueFromPlatformMap(filesTypes.StaticLinkFile.Extension))
linkExtensions.insert(*runcpp2::GetValueFromPlatformMap(filesTypes.StaticLinkFile.Extension));
if(runcpp2::HasValueFromPlatformMap(filesTypes.SharedLinkFile.Extension))
linkExtensions.insert(*runcpp2::GetValueFromPlatformMap(filesTypes.SharedLinkFile.Extension));
if(runcpp2::HasValueFromPlatformMap(filesTypes.ObjectLinkFile.Extension))
linkExtensions.insert(*runcpp2::GetValueFromPlatformMap(filesTypes.ObjectLinkFile.Extension));
//Separate the gathered files from dependencies into files to link and files to copy
for(int i = 0; i < gatheredBinariesPaths.size(); ++i)
{
ghc::filesystem::path filePath(gatheredBinariesPaths.at(i));
std::string extension = runcpp2::GetFileExtensionWithoutVersion(filePath);
//Check if the file is a link file based on its extension
if(linkExtensions.find(extension) != linkExtensions.end())
{
outLinkFilesPaths.push_back(gatheredBinariesPaths.at(i));
//Special case when SharedLinkFile and SharedLibraryFile share the same extension
if( runcpp2::HasValueFromPlatformMap(filesTypes.SharedLibraryFile.Extension) &&
*runcpp2::GetValueFromPlatformMap(filesTypes.SharedLibraryFile
.Extension) == extension)
{
outFilesToCopyPaths.push_back(gatheredBinariesPaths.at(i));
}
}
else
outFilesToCopyPaths.push_back(gatheredBinariesPaths.at(i));
}
ssLOG_INFO("Files to link:");
for(int i = 0; i < outLinkFilesPaths.size(); ++i)
ssLOG_INFO(" " << outLinkFilesPaths[i]);
ssLOG_INFO("Files to copy:");
for(int i = 0; i < outFilesToCopyPaths.size(); ++i)
ssLOG_INFO(" " << outFilesToCopyPaths[i]);
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(void());
}
runcpp2::PipelineResult runcpp2::HandlePreBuild(const Data::ScriptInfo& scriptInfo,
const Data::Profile& profile,
const ghc::filesystem::path& buildDir)
{
ssLOG_FUNC_INFO();
const Data::ProfilesCommands* preBuildCommands =
runcpp2::GetValueFromPlatformMap(scriptInfo.PreBuild);
return RunProfileCommands(preBuildCommands, profile, buildDir.string(), "PreBuild");
}
runcpp2::PipelineResult runcpp2::HandlePostBuild( const Data::ScriptInfo& scriptInfo,
const Data::Profile& profile,
const ghc::filesystem::path& buildDir)
{
ssLOG_FUNC_INFO();
const Data::ProfilesCommands* postBuildCommands =
GetValueFromPlatformMap(scriptInfo.PostBuild);
return RunProfileCommands(postBuildCommands, profile, buildDir.string(), "PostBuild");
}
runcpp2::PipelineResult
runcpp2::RunCompiledOutput( const ghc::filesystem::path& target,
const ghc::filesystem::path& absoluteScriptPath,
const Data::ScriptInfo& scriptInfo,
const std::vector<std::string>& runArgs,
const std::unordered_map<CmdOptions, std::string>& currentOptions,
int& returnStatus)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
//Skip running if not executable
if( scriptInfo.CurrentBuildType != Data::BuildType::INTERNAL_EXECUTABLE_EXECUTABLE &&
scriptInfo.CurrentBuildType != Data::BuildType::INTERNAL_EXECUTABLE_SHARED)
{
ssLOG_INFO("Skipping run - output is not executable");
return PipelineResult::SUCCESS;
}
std::error_code e;
if(target.empty() || !ghc::filesystem::exists(target, e))
{
ssLOG_ERROR("Failed to find the compiled file to run");
return PipelineResult::COMPILE_LINK_FAILED;
}
//Prepare run arguments
std::vector<std::string> finalRunArgs;
finalRunArgs.push_back(target.string());
if(scriptInfo.PassScriptPath)
finalRunArgs.push_back(absoluteScriptPath);
//Add user provided arguments
for(size_t i = 0; i < runArgs.size(); ++i)
finalRunArgs.push_back(runArgs[i]);
if(scriptInfo.CurrentBuildType == Data::BuildType::INTERNAL_EXECUTABLE_EXECUTABLE)
{
//Running the script with modified args
if(!RunCompiledScript(target, absoluteScriptPath, finalRunArgs, returnStatus))
{
ssLOG_ERROR("Failed to run script");
return PipelineResult::RUN_SCRIPT_FAILED;
}
}
else
{
//Load the shared library and run it with modified args
if(!RunCompiledSharedLib(absoluteScriptPath, target, finalRunArgs, returnStatus))
{
ssLOG_ERROR("Failed to run script");
return PipelineResult::RUN_SCRIPT_FAILED;
}
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
runcpp2::PipelineResult
runcpp2::GetBuiltTargetPaths( const ghc::filesystem::path& buildDir,
const std::string& scriptName,
const Data::Profile& profile,
const std::unordered_map<CmdOptions, std::string>& currentOptions,
const Data::ScriptInfo& scriptInfo,
std::vector<ghc::filesystem::path>& outTargets,
ghc::filesystem::path* outRunnableTarget)
{
ssLOG_FUNC_INFO();
INTERNAL_RUNCPP2_SAFE_START();
std::error_code _;
outTargets.clear();
//Validate executable option against build type
if( currentOptions.count(CmdOptions::EXECUTABLE) > 0 &&
scriptInfo.CurrentBuildType != Data::BuildType::INTERNAL_EXECUTABLE_SHARED &&
scriptInfo.CurrentBuildType != Data::BuildType::INTERNAL_EXECUTABLE_EXECUTABLE)
{
ssLOG_ERROR("Cannot run as executable - script is configured for " <<
Data::BuildTypeToString(scriptInfo.CurrentBuildType) <<
" output. Please remove --executable flag or change build type to Executable");
return PipelineResult::INVALID_OPTION;
}
//Get all target paths
std::vector<bool> isRunnable;
if(!Data::BuildTypeHelper::GetPossibleOutputPaths( buildDir,
scriptName,
profile,
scriptInfo.CurrentBuildType,
outTargets,
isRunnable))
{
ssLOG_ERROR("Extension or prefix not found in compiler profile for build type: " <<
runcpp2::Data::BuildTypeToString(scriptInfo.CurrentBuildType));
return PipelineResult::INVALID_SCRIPT_INFO;
}
//Verify all targets exist
for(const ghc::filesystem::path& target : outTargets)
{
if(!ghc::filesystem::exists(target, _))
{
ssLOG_WARNING("Failed to find the compiled file: " << target.string());
continue;
//return PipelineResult::COMPILE_LINK_FAILED;
}
}
//If requested, find the runnable target
if(outRunnableTarget != nullptr)
{
for(size_t i = 0; i < outTargets.size(); ++i)
{
if(isRunnable.at(i))
{
*outRunnableTarget = outTargets.at(i);
break;
}
}
}
return PipelineResult::SUCCESS;
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
}
bool runcpp2::GatherSourceFiles(const ghc::filesystem::path& absoluteScriptPath,
const Data::ScriptInfo& scriptInfo,
const Data::Profile& currentProfile,
std::vector<ghc::filesystem::path>& outSourcePaths)