-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruncpp2.hpp
More file actions
984 lines (853 loc) · 42.4 KB
/
Copy pathruncpp2.hpp
File metadata and controls
984 lines (853 loc) · 42.4 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
#ifndef RUNCPP2_RUNCPP2_HPP
#define RUNCPP2_RUNCPP2_HPP
#include "runcpp2/Data/BuildTypeHelper.hpp"
#include "runcpp2/Data/Profile.hpp"
#include "runcpp2/Data/ScriptInfo.hpp"
#include "runcpp2/Data/FileProperties.hpp"
#include "runcpp2/Data/FilesTypesInfo.hpp"
#include "runcpp2/Data/ParseCommon.hpp"
#include "runcpp2/PipelineSteps.hpp"
#include "runcpp2/ProfileHelper.hpp"
#include "runcpp2/CompilingLinking.hpp"
#include "runcpp2/ConfigParsing.hpp"
#include "runcpp2/PlatformUtil.hpp"
#include "runcpp2/BuildsManager.hpp"
#include "runcpp2/IncludeManager.hpp"
#include "ssLogger/ssLog.hpp"
#include "ghc/filesystem.hpp"
#include "DSResult/DSResult.hpp"
#include <string>
#include <vector>
#include <chrono>
#include <stdint.h>
#include <stdlib.h>
#include <system_error>
#include <unordered_map>
//NOTE: #include "runcpp2/LibYamlImpl.cpp" at the end
//Use for SetDllDirectory
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
extern const uint8_t DefaultScriptInfo[];
extern const size_t DefaultScriptInfo_size;
namespace runcpp2 { namespace Data { struct DependencyInfo; } }
namespace
{
DS::Result<bool> HasCompiledCache( const ghc::filesystem::path& scriptDirectory,
const std::vector<ghc::filesystem::path>& sourceFiles,
const ghc::filesystem::path& buildDir,
const runcpp2::Data::Profile& currentProfile,
runcpp2::IncludeManager& includeManager,
std::vector<bool>& outHasCache,
std::vector<ghc::filesystem::path>& outCachedObjectsFiles,
ghc::filesystem::file_time_type& outFinalObjectWriteTime,
ghc::filesystem::file_time_type& outFinalSourceWriteTime,
ghc::filesystem::file_time_type& outFinalIncludeWriteTime)
{
ssLOG_FUNC_INFO();
outHasCache.clear();
outHasCache = std::vector<bool>(sourceFiles.size(), false);
//TODO: Check compile flags
const std::string* rawObjectExt =
runcpp2::GetValueFromPlatformMap(currentProfile.FilesTypes.ObjectLinkFile.Extension);
DS_ASSERT_FALSE(rawObjectExt == nullptr);
const std::string& objectExt = *rawObjectExt;
outFinalObjectWriteTime = ghc::filesystem::file_time_type();
std::error_code e;
for(int i = 0; i < sourceFiles.size(); ++i)
{
ghc::filesystem::path relativeSourcePath =
ghc::filesystem::relative(sourceFiles.at(i), scriptDirectory, e);
if(e)
{
std::string retMsg = DS_STR("Failed to get relative path for ") +
sourceFiles.at(i).string() + "\n";
retMsg += DS_STR("Failed with error: ") + e.message();
return DS_ERROR_MSG(retMsg);
}
ghc::filesystem::path currentObjectFilePath = buildDir /
relativeSourcePath.parent_path() /
relativeSourcePath.stem();
currentObjectFilePath.concat(objectExt);
ssLOG_DEBUG("Trying to use cache: " << sourceFiles.at(i).string());
//Check source file timestamp
ghc::filesystem::file_time_type currentSourceWriteTime =
ghc::filesystem::last_write_time(sourceFiles.at(i), e);
if(currentSourceWriteTime > outFinalSourceWriteTime)
outFinalSourceWriteTime = currentSourceWriteTime;
//Check include record
bool outdatedIncludeRecord = false;
ghc::filesystem::file_time_type currentIncludeWriteTime;
{
std::vector<ghc::filesystem::path> cachedIncludes;
ghc::filesystem::file_time_type recordTime;
if(includeManager.ReadIncludeRecord(sourceFiles.at(i), cachedIncludes, recordTime))
{
if(includeManager.NeedsUpdate(sourceFiles.at(i), cachedIncludes, recordTime))
outdatedIncludeRecord = true;
}
if(outdatedIncludeRecord)
ssLOG_DEBUG("Needs to update include record for " << sourceFiles.at(i).string());
for(int j = 0; j < cachedIncludes.size(); ++j)
{
ghc::filesystem::file_time_type includeWriteTime =
ghc::filesystem::last_write_time(cachedIncludes.at(j), e);
if(includeWriteTime > currentIncludeWriteTime)
currentIncludeWriteTime = includeWriteTime;
}
if(currentIncludeWriteTime > outFinalIncludeWriteTime)
outFinalIncludeWriteTime = currentIncludeWriteTime;
}
//Check object file timestamp
if(ghc::filesystem::exists(currentObjectFilePath, e))
{
ghc::filesystem::file_time_type currentObjectWriteTime =
ghc::filesystem::last_write_time(currentObjectFilePath, e);
bool useCache = currentObjectWriteTime > currentSourceWriteTime &&
currentObjectWriteTime > currentIncludeWriteTime &&
!outdatedIncludeRecord;
ssLOG_DEBUG("currentObjectWriteTime: " <<
currentObjectWriteTime.time_since_epoch().count());
ssLOG_DEBUG("currentSourceWriteTime: " <<
currentSourceWriteTime.time_since_epoch().count());
ssLOG_DEBUG("currentIncludeWriteTime: " <<
currentIncludeWriteTime.time_since_epoch().count());
ssLOG_DEBUG("outdatedIncludeRecord: " << outdatedIncludeRecord);
if(useCache)
{
ssLOG_INFO("Using cache for " << sourceFiles.at(i).string());
outHasCache.at(i) = true;
outCachedObjectsFiles.push_back(currentObjectFilePath);
}
else
ssLOG_INFO("Cache invalidated for " << sourceFiles.at(i).string());
if(currentObjectWriteTime > outFinalObjectWriteTime)
outFinalObjectWriteTime = currentObjectWriteTime;
}
}
return {};
}
bool HasOutputCache( const std::vector<bool>& sourceHasCache,
const ghc::filesystem::path& buildDir,
const runcpp2::Data::Profile& currentProfile,
const runcpp2::Data::ScriptInfo& scriptInfo,
const std::string& scriptName,
const ghc::filesystem::file_time_type& finalBinaryWriteTime,
bool& outOutputCache)
{
for(int i = 0; i < sourceHasCache.size(); ++i)
{
if(!sourceHasCache.at(i))
{
outOutputCache = false;
return true;
}
}
//Check if output is cached
std::error_code e;
std::vector<ghc::filesystem::path> outputPaths;
std::vector<bool> runnable;
if(!runcpp2::Data::BuildTypeHelper::GetPossibleOutputPaths( buildDir,
scriptName,
currentProfile,
scriptInfo.CurrentBuildType,
outputPaths,
runnable))
{
return false;
}
ssLOG_INFO("finalBinaryWriteTime: " << runcpp2::SerializeTimePoint(finalBinaryWriteTime));
int existCount = 0;
for(const ghc::filesystem::path& outputPath : outputPaths)
{
ssLOG_INFO("Trying to use output cache: " << outputPath.string());
if( ghc::filesystem::exists(outputPath, e) &&
ghc::filesystem::file_size(outputPath, e) > 0)
{
++existCount;
ghc::filesystem::file_time_type lastOutputBinary =
ghc::filesystem::last_write_time(outputPath, e);
ssLOG_INFO("lastOutputBinary: " << runcpp2::SerializeTimePoint(lastOutputBinary));
if(lastOutputBinary >= finalBinaryWriteTime)
{
ssLOG_INFO("Using output cache for " << outputPath.string());
continue;
}
else
{
ssLOG_INFO("Object files have more recent write time");
ssLOG_DEBUG("lastOutputBinary: " <<
lastOutputBinary.time_since_epoch().count());
ssLOG_DEBUG("finalBinaryWriteTime: " <<
finalBinaryWriteTime.time_since_epoch().count());
outOutputCache = false;
return true;
}
}
else
ssLOG_INFO(outputPath.string() << " doesn't exist");
}
//TODO: Parsing ExpectedOutputFiles in the profile to see cache is valid or not
//NOTE: We don't know which ones are optionals, at least for now.
// If there's nothing, there's no cache for sure.
// If there's something, it's very likely we have it cached.
// Dumb logic, I know, but it works for now.
if(existCount == 0)
{
outOutputCache = false;
return true;
}
ssLOG_INFO("Using output cache");
outOutputCache = true;
return true;
}
}
namespace runcpp2
{
inline void GetDefaultScriptInfo(std::string& scriptInfo)
{
scriptInfo = std::string( reinterpret_cast<const char*>(DefaultScriptInfo),
DefaultScriptInfo_size);
}
//NOTE: Mainly used for test to reduce spamminig
inline void SetLogLevel(const std::string& logLevel)
{
if(logLevel == "Debug")
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(ssLOG_LEVEL_DEBUG);
else if(logLevel == "Warning")
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(ssLOG_LEVEL_WARNING);
else if(logLevel == "Error")
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(ssLOG_LEVEL_ERROR);
else
ssLOG_ERROR("Invalid log level: " << logLevel);
}
inline DS::Result<void> GetScriptInfoData( const ghc::filesystem::path& scriptPath,
const std::string& rawParameters,
bool executable,
Data::ScriptInfo& outScriptInfo,
ghc::filesystem::path& outAbsoluteScriptPath,
ghc::filesystem::path& outScriptDirectory,
std::string& outScriptName,
std::unordered_map< std::string,
std::string>& outParameterValues)
{
ssLOG_FUNC_INFO();
//Validate inputs and get paths
{
//Check if input file exists
std::error_code _;
if(!ghc::filesystem::exists(scriptPath, _))
return DS_ERROR_MSG("File does not exist: " + scriptPath.string());
if(ghc::filesystem::is_directory(scriptPath, _))
return DS_ERROR_MSG( "The input file must not be a directory: " + scriptPath.string());
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));
}
//Create parameters
std::unordered_map<std::string, std::string> parameterValues;
CreateParameterValues(rawParameters, parameterValues);
//Parse script info
ParseAndValidateScriptInfo( outAbsoluteScriptPath,
outScriptDirectory,
outScriptName,
executable,
parameterValues,
outScriptInfo).DS_TRY();
return {};
}
inline DS::Result<ghc::filesystem::path> GetDefaultBuildDir()
{
ghc::filesystem::path configDir = GetConfigFilePath().DS_TRY();
//Parse and get the config directory
{
std::error_code e;
if(ghc::filesystem::is_directory(configDir, e))
return DS_ERROR_MSG("Unexpected path for config file: " + configDir.string());
configDir = configDir.parent_path();
if(!ghc::filesystem::is_directory(configDir, e))
return DS_ERROR_MSG("Unexpected path for config directory: " + configDir.string());
}
return configDir;
}
struct CoreParams
{
const ghc::filesystem::path& scriptPath;
const std::vector<Data::Profile>& profiles;
const std::string rawParameters;
bool runAsExecutable;
bool buildLocally;
const std::string& configPreferredProfile;
};
inline DS::Result<void> RunCleanup(CoreParams params)
{
ghc::filesystem::path absoluteScriptPath;
ghc::filesystem::path scriptDirectory;
std::string scriptName;
std::unordered_map<std::string, std::string> parameters;
Data::ScriptInfo scriptInfo;
GetScriptInfoData( params.scriptPath,
params.rawParameters,
params.runAsExecutable,
//Output:
scriptInfo,
absoluteScriptPath,
scriptDirectory,
scriptName,
parameters).DS_TRY();
if(params.profiles.empty())
return DS_ERROR_MSG("No compiler profiles found");
int profileIndex = GetPreferredProfileIndex( absoluteScriptPath,
scriptInfo,
params.profiles,
params.configPreferredProfile).DS_TRY();
{
ghc::filesystem::path buildDir = GetDefaultBuildDir().DS_TRY();
BuildsManager buildsManager("/tmp");
IncludeManager includeManager;
InitializeBuildDirectory( buildDir,
absoluteScriptPath,
params.buildLocally,
buildsManager,
buildDir,
includeManager).DS_TRY();
HandleCleanup( scriptInfo,
params.profiles.at(profileIndex),
scriptDirectory,
buildDir,
absoluteScriptPath,
buildsManager).DS_TRY();
}
return {};
}
inline DS::Result<void> RunResetDependencies( CoreParams params,
const std::string& targetDepToReset)
{
ghc::filesystem::path absoluteScriptPath;
ghc::filesystem::path scriptDirectory;
std::string scriptName;
std::unordered_map<std::string, std::string> parameters;
Data::ScriptInfo scriptInfo;
GetScriptInfoData( params.scriptPath,
params.rawParameters,
false, //NOTE: Don't care
//Output:
scriptInfo,
absoluteScriptPath,
scriptDirectory,
scriptName,
parameters).DS_TRY();
if(params.profiles.empty())
return DS_ERROR_MSG("No compiler profiles found");
int profileIndex = GetPreferredProfileIndex( absoluteScriptPath,
scriptInfo,
params.profiles,
params.configPreferredProfile).DS_TRY();
//Parsing the script, setting up dependencies, compiling and linking
std::vector<std::string> filesToCopyPaths;
ghc::filesystem::path buildDir = GetDefaultBuildDir().DS_TRY();
BuildsManager buildsManager("/tmp");
IncludeManager includeManager;
InitializeBuildDirectory( buildDir,
absoluteScriptPath,
params.buildLocally,
buildsManager,
buildDir,
includeManager).DS_TRY();
ResolveDependenciesImports(scriptInfo, scriptDirectory, buildDir, parameters).DS_TRY();
//Process Dependencies
ResetDependencies( scriptInfo,
params.profiles.at(profileIndex),
scriptDirectory,
buildDir,
targetDepToReset).DS_TRY();
return {};
}
inline DS::Result<bool>
CheckSourcesNeedUpdate( CoreParams params,
const std::string rawMaxThreads,
const Data::ScriptInfo* lastScriptInfo,
const ghc::filesystem::file_time_type& prevFinalSourceWriteTime,
const ghc::filesystem::file_time_type& prevFinalIncludeWriteTime)
{
ghc::filesystem::path absoluteScriptPath;
ghc::filesystem::path scriptDirectory;
std::string scriptName;
std::unordered_map<std::string, std::string> parameters;
Data::ScriptInfo scriptInfo;
//TODO: Reduce number of parameters here
GetScriptInfoData( params.scriptPath,
params.rawParameters,
params.runAsExecutable,
//Output:
scriptInfo,
absoluteScriptPath,
scriptDirectory,
scriptName,
parameters).DS_TRY();
//First check if script info file has changed
{
std::error_code e;
ghc::filesystem::path dedicatedYamlLoc =
scriptDirectory / ghc::filesystem::path(scriptName + ".yaml");
ghc::filesystem::file_time_type currentWriteTime;
if(ghc::filesystem::exists(dedicatedYamlLoc, e))
currentWriteTime = ghc::filesystem::last_write_time(dedicatedYamlLoc, e);
else
currentWriteTime = ghc::filesystem::last_write_time(absoluteScriptPath, e);
if(e)
return DS_ERROR_MSG("Failed to get write time for script info" + e.message());
//If script info file is newer than last check, we need to update
if(currentWriteTime > scriptInfo.LastWriteTime)
return true;
}
if(params.profiles.empty())
return DS_ERROR_MSG("No compiler profiles found");
int profileIndex = GetPreferredProfileIndex( absoluteScriptPath,
scriptInfo,
params.profiles,
params.configPreferredProfile).DS_TRY();
//Parsing the script, setting up dependencies, compiling and linking
std::vector<std::string> filesToCopyPaths;
ghc::filesystem::path buildDir = GetDefaultBuildDir().DS_TRY();
BuildsManager buildsManager("/tmp");
IncludeManager includeManager;
InitializeBuildDirectory( buildDir,
absoluteScriptPath,
params.buildLocally,
buildsManager,
buildDir,
includeManager).DS_TRY();
const int maxThreads = rawMaxThreads.empty() ? 8 : strtol( rawMaxThreads.c_str(),
nullptr,
10);
if(maxThreads <= 0)
return DS_ERROR_MSG("Invalid number of threads passed in");
ResolveDependenciesImports(scriptInfo, scriptDirectory, buildDir, parameters).DS_TRY();
//Check if script info has changed if provided and run setup if needed
bool recompileNeeded = false;
bool relinkNeeded = false;
std::vector<std::string> changedDependencies;
CheckScriptInfoChanges( buildDir,
scriptInfo,
params.profiles.at(profileIndex),
scriptDirectory,
lastScriptInfo,
parameters,
recompileNeeded,
relinkNeeded,
changedDependencies).DS_TRY();
if(relinkNeeded)
return true;
std::vector<std::string> gatheredBinariesPaths;
//Process Dependencies
std::vector<Data::DependencyInfo*> availableDependencies;
ProcessDependencies(scriptInfo,
params.profiles.at(profileIndex),
scriptDirectory,
buildDir,
changedDependencies,
false,
maxThreads,
availableDependencies,
gatheredBinariesPaths).DS_TRY();
//Get all the files we are trying to compile
std::vector<ghc::filesystem::path> sourceFiles;
GatherSourceFiles( absoluteScriptPath,
scriptInfo,
params.profiles.at(profileIndex),
sourceFiles).DS_TRY();
//Check if we have already compiled before.
std::vector<bool> sourceHasCache;
std::vector<ghc::filesystem::path> cachedObjectsFiles;
ghc::filesystem::file_time_type finalObjectWriteTime;
ghc::filesystem::file_time_type finalSourceWriteTime;
ghc::filesystem::file_time_type finalIncludeWriteTime;
HasCompiledCache( scriptDirectory,
sourceFiles,
buildDir,
params.profiles.at(profileIndex),
includeManager,
sourceHasCache,
cachedObjectsFiles,
finalObjectWriteTime,
finalSourceWriteTime,
finalIncludeWriteTime).DS_TRY();
if( finalSourceWriteTime > prevFinalSourceWriteTime ||
finalIncludeWriteTime > prevFinalIncludeWriteTime)
{
return true;
}
else
return false;
}
struct RunParams
{
CoreParams Core;
bool rebuild;
bool compileOnly;
bool buildSourceOnly;
bool buildOnly;
const std::vector<std::string>& runArgs;
const std::string rawMaxThreads;
const Data::ScriptInfo* lastScriptInfo;
const ghc::filesystem::path& buildOutputDir;
};
inline DS::Result<int> Run( RunParams runParams,
Data::ScriptInfo& outScriptInfo,
ghc::filesystem::file_time_type& outFinalSourceWriteTime,
ghc::filesystem::file_time_type& outFinalIncludeWriteTime)
{
ssLOG_FUNC_INFO();
ghc::filesystem::path absoluteScriptPath;
ghc::filesystem::path scriptDirectory;
std::string scriptName;
std::unordered_map<std::string, std::string> parameters;
Data::ScriptInfo scriptInfo;
//TODO: Reduce number of parameters here
GetScriptInfoData( runParams.Core.scriptPath,
runParams.Core.rawParameters,
runParams.Core.runAsExecutable,
//Output:
scriptInfo,
absoluteScriptPath,
scriptDirectory,
scriptName,
parameters).DS_TRY();
if(runParams.Core.profiles.empty())
return DS_ERROR_MSG("No compiler profiles found");
int profileIndex = GetPreferredProfileIndex(absoluteScriptPath,
scriptInfo,
runParams.Core.profiles,
runParams.Core.configPreferredProfile).DS_TRY();
//Parsing the script, setting up dependencies, compiling and linking
std::vector<ghc::filesystem::path> filesToCopyPaths;
ghc::filesystem::path buildDir = GetDefaultBuildDir().DS_TRY();
{
BuildsManager buildsManager("/tmp");
IncludeManager includeManager;
InitializeBuildDirectory( buildDir,
absoluteScriptPath,
runParams.Core.buildLocally,
buildsManager,
buildDir,
includeManager).DS_TRY();
const int maxThreads = runParams.rawMaxThreads.empty() ?
8 :
strtol(runParams.rawMaxThreads.c_str(), nullptr, 10);
if(maxThreads <= 0)
return DS_ERROR_MSG("Invalid number of threads passed in");
ResolveDependenciesImports(scriptInfo, scriptDirectory, buildDir, parameters).DS_TRY();
//Check if script info has changed if provided and run setup if needed
bool recompileNeeded = false;
bool relinkNeeded = false;
std::vector<std::string> changedDependencies;
CheckScriptInfoChanges( buildDir,
scriptInfo,
runParams.Core.profiles.at(profileIndex),
scriptDirectory,
runParams.lastScriptInfo,
parameters,
recompileNeeded,
relinkNeeded,
changedDependencies).DS_TRY();
outScriptInfo = scriptInfo;
std::vector<std::string> gatheredBinariesPaths;
//Process Dependencies
std::vector<Data::DependencyInfo*> availableDependencies;
ProcessDependencies(scriptInfo,
runParams.Core.profiles.at(profileIndex),
scriptDirectory,
buildDir,
changedDependencies,
runParams.buildSourceOnly,
maxThreads,
availableDependencies,
gatheredBinariesPaths).DS_TRY();
//Get all the files we are trying to compile
std::vector<ghc::filesystem::path> sourceFiles;
GatherSourceFiles( absoluteScriptPath,
scriptInfo,
runParams.Core.profiles.at(profileIndex),
sourceFiles).DS_TRY();
//Get all include paths
std::vector<ghc::filesystem::path> sourceIncludePaths;
std::vector<ghc::filesystem::path> depIncludePaths;
GatherIncludePaths( scriptDirectory,
scriptInfo,
runParams.Core.profiles.at(profileIndex),
availableDependencies,
sourceIncludePaths,
depIncludePaths).DS_TRY();
//Check if we have already compiled before.
std::vector<bool> sourceHasCache;
std::vector<ghc::filesystem::path> cachedObjectsFiles;
ghc::filesystem::file_time_type finalObjectWriteTime;
if(runParams.rebuild || recompileNeeded)
sourceHasCache = std::vector<bool>(sourceFiles.size(), false);
else
{
HasCompiledCache( scriptDirectory,
sourceFiles,
buildDir,
runParams.Core.profiles.at(profileIndex),
includeManager,
sourceHasCache,
cachedObjectsFiles,
finalObjectWriteTime,
outFinalSourceWriteTime,
outFinalIncludeWriteTime).DS_TRY();
}
runcpp2::SourceIncludeMap sourceIncludeMap;
{
std::vector<ghc::filesystem::path> allIncludePaths = sourceIncludePaths;
allIncludePaths.insert( allIncludePaths.end(),
depIncludePaths.begin(),
depIncludePaths.end());
runcpp2::GatherFilesIncludes( sourceFiles,
sourceHasCache,
allIncludePaths,
sourceIncludeMap).DS_TRY();
}
for(int i = 0; i < sourceFiles.size(); ++i)
{
if(!sourceHasCache.at(i))
{
ssLOG_DEBUG("Updating include record for " << sourceFiles.at(i).string());
if(sourceIncludeMap.count(sourceFiles.at(i)) == 0)
{
ssLOG_WARNING("Includes not gathered for " << sourceFiles.at(i).string());
continue;
}
bool writeResult = includeManager.WriteIncludeRecord
(
sourceFiles.at(i),
sourceIncludeMap.at(sourceFiles.at(i))
);
if(!writeResult)
{
return DS_ERROR_MSG("Failed to write include record for " +
sourceFiles.at(i).string());
}
}
else
{
ssLOG_DEBUG("Include record for " << sourceFiles.at(i).string() <<
" is up to date");
}
}
std::vector<ghc::filesystem::path> depLinkFilesPaths;
SeparateDependencyFiles(runParams.Core.profiles.at(profileIndex).FilesTypes,
gatheredBinariesPaths,
depLinkFilesPaths,
filesToCopyPaths);
//TODO: Allow user to pass the priorities outside
//Set dependencies files to be lower priority
std::vector<int> depBinaryFilesPriorities;
for(int i = 0; i < depLinkFilesPaths.size(); ++i)
depBinaryFilesPriorities.push_back(-100);
//Get finalBinaryWriteTime by combining final object and dependencies write times
std::error_code e;
ghc::filesystem::file_time_type finalBinaryWriteTime = finalObjectWriteTime;
for(int i = 0; i < depLinkFilesPaths.size(); ++i)
{
if(!ghc::filesystem::exists(depLinkFilesPaths.at(i), e))
{
return DS_ERROR_MSG(depLinkFilesPaths.at(i).string() +
" reported as cached but doesn't exist");
}
ghc::filesystem::file_time_type lastWriteTime =
ghc::filesystem::last_write_time(depLinkFilesPaths.at(i), e);
if(lastWriteTime > finalBinaryWriteTime)
finalBinaryWriteTime = lastWriteTime;
}
//Run PreBuild commands before compilation
HandlePreBuild(scriptInfo, runParams.Core.profiles.at(profileIndex), buildDir).DS_TRY();
//Compiling/Linking
bool outputCache = false;
if(!HasOutputCache( sourceHasCache,
buildDir,
runParams.Core.profiles.at(profileIndex),
scriptInfo,
scriptName,
finalBinaryWriteTime,
outputCache))
{
ssLOG_WARNING( "Error detected when trying to use output cache. "
"A cleanup is recommended");
}
if(!outputCache || relinkNeeded)
{
std::vector<ghc::filesystem::path> sourceLinkFilesPaths;
std::vector<int> sourceBinaryFilesPriorities;
for(int i = 0; i < cachedObjectsFiles.size(); ++i)
{
sourceLinkFilesPaths.push_back(cachedObjectsFiles.at(i));
sourceBinaryFilesPriorities.push_back(0);
}
//TODO: Compile and link for watch as well. Load library as well
if(runParams.compileOnly)
{
CompileScriptOnly( buildDir,
scriptDirectory,
sourceFiles,
sourceHasCache,
sourceIncludePaths,
depIncludePaths,
scriptInfo,
runParams.Core.profiles.at(profileIndex),
maxThreads).DS_TRY();
return 0;
}
else
{
CompileAndLinkScript( buildDir,
scriptDirectory,
ghc::filesystem::path(scriptName),
sourceFiles,
sourceHasCache,
sourceIncludePaths,
depIncludePaths,
scriptInfo,
availableDependencies,
runParams.Core.profiles.at(profileIndex),
depLinkFilesPaths,
depBinaryFilesPriorities,
sourceLinkFilesPaths,
sourceBinaryFilesPriorities,
maxThreads)
.DS_TRY_ACT(DS_TMP_ERROR.Message += "\nFailed to compile or link script.";
DS_APPEND_TRACE(DS_TMP_ERROR);
return DS::Error(DS_TMP_ERROR));
}
}
}
//Trigger post build and run the script if needed
int returnStatus = -1;
{
std::vector<ghc::filesystem::path> targets;
ghc::filesystem::path runnableTarget;
GetBuiltTargetPaths(buildDir,
scriptName,
runParams.Core.profiles.at(profileIndex),
runParams.Core.runAsExecutable,
scriptInfo,
targets,
&runnableTarget).DS_TRY();
if(targets.empty())
{
ssLOG_WARNING("No target files found");
return 0;
}
//Copy files to build directory
std::vector<std::string> copiedPaths;
if(!runParams.buildOutputDir.empty())
{
std::error_code e;
if(!ghc::filesystem::exists(runParams.buildOutputDir, e))
{
if(!ghc::filesystem::create_directories(runParams.buildOutputDir, e))
return DS_ERROR_MSG("Failed to create output directory");
}
buildDir = runParams.buildOutputDir;
//filesToCopyPaths.push_back(runnableTarget.string());
for(const ghc::filesystem::path& target : targets)
filesToCopyPaths.push_back(target);
}
CopyFiles(buildDir, filesToCopyPaths, copiedPaths)
.DS_TRY_ACT(DS_TMP_ERROR.Message += "\nFailed to copy binaries before running the "
"script";
DS_APPEND_TRACE(DS_TMP_ERROR);
return DS::Error(DS_TMP_ERROR));
//Run PostBuild commands after successful compilation
HandlePostBuild(scriptInfo,
runParams.Core.profiles.at(profileIndex),
buildDir.string()).DS_TRY();
//Don't run if we are just watching or building
if(runParams.buildOnly)
return 0;
//Run otherwise
ssLOG_INFO("Running script...");
RunCompiledOutput( runnableTarget,
absoluteScriptPath,
scriptInfo,
runParams.runArgs,
returnStatus).DS_TRY();
}
return returnStatus;
}
inline DS::Result<void> DownloadTutorial(char* runcppPath)
{
std::string dummy;
int returnCode = 0;
std::string input;
while(true)
{
input.clear();
ssLOG_BASE( "This will download InteractiveTutorial.cpp from github to current directory. "
"Continue? [Y/n]");
if(!std::getline(std::cin, input))
return DS_ERROR_MSG("IO Error when trying to get cin");
if(!input.empty())
{
if(input == "y" || input == "Y")
break;
else if(input == "n" || input == "N")
{
ssLOG_BASE("Not continuing");
return {};
}
else
ssLOG_BASE("Please only answer with y or n");
}
else
break;
}
std::string targetBranch = RUNCPP2_VERSION;
size_t dashPos = targetBranch.find("-");
if(dashPos != std::string::npos)
targetBranch = targetBranch.substr(0, dashPos);
#ifdef _WIN32
if(!RunCommand( "powershell -Command \""
"Invoke-WebRequest https://github.com/Neko-Box-Coder/runcpp2/raw/"
"refs/tags/" + targetBranch + "/Examples/InteractiveTutorial.cpp "
"-OutFile InteractiveTutorial.cpp\"",
false,
"./",
dummy,
returnCode))
{
return DS_ERROR_MSG("Failed to download tutorial");
}
#else
if(!RunCommand( "curl -L -o InteractiveTutorial.cpp "
"https://github.com/Neko-Box-Coder/runcpp2/raw/refs/tags/" +
targetBranch + "/Examples/InteractiveTutorial.cpp",
false,
"./",
dummy,
returnCode))
{
return DS_ERROR_MSG("Failed to download tutorial");
}
#endif
ssLOG_INFO("targetBranch: " << targetBranch);
ssLOG_BASE("Downloaded InteractiveTutorial.cpp from github.");
ssLOG_BASE("Do `" << runcppPath << " run InteractiveTutorial.cpp to start the tutorial.");
return {};
}
}
#include "runcpp2/LibYamlImpl.cpp"
#endif