-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependenciesHelper.hpp
More file actions
1360 lines (1152 loc) · 56.6 KB
/
Copy pathDependenciesHelper.hpp
File metadata and controls
1360 lines (1152 loc) · 56.6 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
#ifndef RUNCPP2_DEPENDENCIES_SETUP_HELPER_HPP
#define RUNCPP2_DEPENDENCIES_SETUP_HELPER_HPP
#include "runcpp2/Data/DependencyInfo.hpp"
#include "runcpp2/Data/ScriptInfo.hpp"
#include "runcpp2/Data/Profile.hpp"
#include "runcpp2/Data/DependencyLibraryType.hpp"
#include "runcpp2/Data/DependencyLinkProperty.hpp"
#include "runcpp2/Data/DependencySource.hpp"
#include "runcpp2/Data/FileProperties.hpp"
#include "runcpp2/Data/FilesToCopyInfo.hpp"
#include "runcpp2/Data/FilesTypesInfo.hpp"
#include "runcpp2/Data/GitSource.hpp"
#include "runcpp2/Data/LocalSource.hpp"
#include "runcpp2/Data/ParseCommon.hpp"
#include "runcpp2/Data/ProfilesCommands.hpp"
#include "runcpp2/Data/SubmoduleInitType.hpp"
#include "runcpp2/LibYAML_Wrapper.hpp"
#include "runcpp2/PlatformUtil.hpp"
#include "runcpp2/StringUtil.hpp"
#include "runcpp2/DeferUtil.hpp"
#if !defined(NOMINMAX)
#define NOMINMAX 1
#endif
#include "ssLogger/ssLog.hpp"
#include "DSResult/DSResult.hpp"
#include "ghc/filesystem.hpp"
#include "mpark/variant.hpp"
#include <vector>
#include <unordered_set>
#include <future>
#include <chrono>
#include <stddef.h>
#include <cctype>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <system_error>
#include <unordered_map>
namespace
{
DS::Result<void> GetDependencyPath( const runcpp2::Data::DependencyInfo& dependency,
const ghc::filesystem::path& scriptPath,
const ghc::filesystem::path& buildDir,
ghc::filesystem::path& outCopyPath,
ghc::filesystem::path& outSourcePath);
DS::Result<void> PopulateLocalDependency( const runcpp2::Data::DependencyInfo& dependency,
const ghc::filesystem::path& copyPath,
const ghc::filesystem::path& sourcePath,
const ghc::filesystem::path& buildDir,
bool& outPrePopulated);
DS::Result<void>
PopulateLocalDependencies( const std::vector<runcpp2::Data::DependencyInfo*>& dependencies,
const std::vector<std::string>& dependenciesCopiesPaths,
const std::vector<std::string>& dependenciesSourcesPaths,
const ghc::filesystem::path& buildDir,
std::vector<bool>& outPrePopulated);
DS::Result<void>
PopulateAbsoluteIncludePaths( std::vector<runcpp2::Data::DependencyInfo*>& dependencies,
const std::vector<std::string>& dependenciesCopiesPaths);
DS::Result<void>
RunDependenciesSteps( const runcpp2::Data::Profile& profile,
const std::unordered_map< PlatformName,
runcpp2::Data::ProfilesCommands> steps,
const std::string& dependenciesCopiedDirectory,
bool required,
bool redirectIO);
bool GetDependencyBinariesExtensionsToLink( const runcpp2::Data::DependencyInfo& dependencyInfo,
const runcpp2::Data::Profile& profile,
std::vector<std::string>& outExtensionsToLink);
ghc::filesystem::path ResolveSymlink(const ghc::filesystem::path& path, std::error_code& ec);
}
namespace runcpp2
{
inline DS::Result<void>
GetDependenciesPaths( const std::vector<Data::DependencyInfo*>& availableDependencies,
std::vector<std::string>& outCopiesPaths,
std::vector<std::string>& outSourcesPaths,
const ghc::filesystem::path& scriptPath,
const ghc::filesystem::path& buildDir)
{
ssLOG_FUNC_INFO();
for(int i = 0; i < availableDependencies.size(); ++i)
{
ghc::filesystem::path currentCopyPath;
ghc::filesystem::path currentSourcePath;
GetDependencyPath( *availableDependencies.at(i),
scriptPath,
buildDir,
currentCopyPath,
currentSourcePath).DS_TRY();
outCopiesPaths.push_back(currentCopyPath.string());
outSourcesPaths.push_back(currentSourcePath.string());
}
return {};
}
inline bool IsDependencyAvailableForThisPlatform(const Data::DependencyInfo& dependency)
{
std::vector<std::string> platformNames = GetPlatformNames();
for(int i = 0; i < platformNames.size(); ++i)
{
if(dependency.Platforms.find(platformNames.at(i)) != dependency.Platforms.end())
return true;
}
return false;
}
inline DS::Result<void>
CleanupDependencies(const runcpp2::Data::Profile& profile,
const Data::ScriptInfo& scriptInfo,
const std::vector<Data::DependencyInfo*>& availableDependencies,
const std::vector<std::string>& dependenciesLocalCopiesPaths,
const std::string& dependenciesToReset)
{
ssLOG_FUNC_DEBUG();
//If the script info is not populated (i.e. empty script info), don't do anything
if(!scriptInfo.Populated)
return {};
DS_ASSERT_EQ(availableDependencies.size(), dependenciesLocalCopiesPaths.size());
//Split dependency names if not "all"
std::unordered_set<std::string> dependencyNames;
if(dependenciesToReset != "all")
{
std::string currentName;
for(int i = 0; i < dependenciesToReset.size(); ++i)
{
if(dependenciesToReset[i] == ',' || i == dependenciesToReset.size() - 1)
{
if(dependenciesToReset[i] != ',')
currentName += dependenciesToReset[i];
if(!currentName.empty())
{
runcpp2::Trim(currentName);
//Convert to lowercase for case-insensitive comparison
for(int j = 0; j < currentName.size(); ++j)
currentName[j] = std::tolower(currentName[j]);
dependencyNames.insert(currentName);
currentName.clear();
}
}
else
currentName += dependenciesToReset[i];
}
}
for(int i = 0; i < availableDependencies.size(); ++i)
{
//Skip if not in the list of dependencies to reset
if(!dependencyNames.empty())
{
//Convert dependency name to lowercase for comparison
std::string depName = availableDependencies.at(i)->Name;
for(int j = 0; j < depName.size(); ++j)
depName[j] = std::tolower(depName[j]);
if(dependencyNames.count(depName) == 0)
{
ssLOG_DEBUG(availableDependencies.at(i)->Name << " not in list to remove");
continue;
}
}
std::error_code e;
ssLOG_INFO("Running cleanup commands for " << availableDependencies.at(i)->Name);
DS::Result<void> depResult = RunDependenciesSteps( profile,
availableDependencies.at(i)->Cleanup,
dependenciesLocalCopiesPaths.at(i),
false,
false);
if(!depResult.HasValue())
{
depResult.Error().Message += "\nFailed to cleanup dependency " +
availableDependencies.at(i)->Name;
DS_APPEND_TRACE(depResult.Error());
return depResult;
}
//Remove the directory
if( ghc::filesystem::exists(dependenciesLocalCopiesPaths.at(i), e) &&
!ghc::filesystem::remove_all(dependenciesLocalCopiesPaths.at(i), e))
{
return DS_ERROR_MSG("Failed to reset dependency directory: " +
dependenciesLocalCopiesPaths.at(i));
}
ssLOG_DEBUG(availableDependencies.at(i)->Name << " removed");
}
return {};
}
inline DS::Result<void>
SetupDependenciesIfNeeded( const runcpp2::Data::Profile& profile,
const ghc::filesystem::path& buildDir,
const Data::ScriptInfo& scriptInfo,
std::vector<Data::DependencyInfo*>& availableDependencies,
const std::vector<std::string>& dependenciesLocalCopiesPaths,
const std::vector<std::string>& dependenciesSourcePaths,
const int maxThreads)
{
ssLOG_FUNC_INFO();
//If the script info is not populated (i.e. empty script info), don't do anything
if(!scriptInfo.Populated)
return {};
std::vector<bool> prePolulatedDependencies;
//Clone/copy the dependencies if needed
PopulateLocalDependencies( availableDependencies,
dependenciesLocalCopiesPaths,
dependenciesSourcePaths,
buildDir,
prePolulatedDependencies).DS_TRY();
PopulateAbsoluteIncludePaths(availableDependencies, dependenciesLocalCopiesPaths).DS_TRY();
#if RUNCPP2_USE_PARALLEL_FOR_DEP
std::vector<std::future<DS::Result<void>>> actions;
std::vector<bool> finished;
//Cache logs for worker threads
ssLOG_ENABLE_CACHE_OUTPUT_FOR_NEW_THREADS();
int logLevel = ssLOG_GET_CURRENT_THREAD_TARGET_LEVEL();
#endif
//Run setup steps
for(int i = 0; i < availableDependencies.size(); ++i)
{
//Don't run setup if the dependency is already setup in previous runs
if(prePolulatedDependencies.at(i))
{
ssLOG_INFO("Skip running setup commands for " << availableDependencies.at(i)->Name);
continue;
}
#if RUNCPP2_USE_PARALLEL_FOR_DEP
actions.emplace_back
(
std::async
(
std::launch::async,
[
i,
&profile,
&availableDependencies,
&dependenciesLocalCopiesPaths,
logLevel
]() -> DS::Result<void>
{
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(logLevel);
#endif
ssLOG_INFO("Running setup commands for " << availableDependencies.at(i)->Name);
DS::Result<void> depResult =
RunDependenciesSteps( profile,
availableDependencies.at(i)->Setup,
dependenciesLocalCopiesPaths.at(i),
true,
false);
if(!depResult.HasValue())
{
depResult.Error().Message += "\nFailed to setup dependency " +
availableDependencies.at(i)->Name;
DS_APPEND_TRACE(depResult.Error());
return depResult;
}
#if RUNCPP2_USE_PARALLEL_FOR_DEP
return {};
}
)
);
finished.emplace_back(false);
//Evaluate the setup results for each batch
if(actions.size() >= maxThreads || i == availableDependencies.size() - 1)
{
bool needsWaiting = false;
do
{
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(30);
needsWaiting = false;
for(int j = 0; j < actions.size(); ++j)
{
if(finished.at(j))
continue;
if(!actions.at(j).valid())
{
ssLOG_OUTPUT_ALL_CACHE_GROUPED();
return DS_ERROR_MSG("Failed to construct actions for setup");
}
std::future_status actionStatus = actions.at(j).wait_until(deadline);
if(actionStatus == std::future_status::ready)
{
DS::Result<void> actionResult = actions.at(j).get();
if(!actionResult.HasValue())
{
ssLOG_OUTPUT_ALL_CACHE_GROUPED();
actionResult.Error().Message += "\nSetup failed for dependencies";
DS_APPEND_TRACE(actionResult.Error());
return actionResult;
}
finished.at(j) = true;
}
else
{
ssLOG_WARNING("Manual interrupt might be needed");
ssLOG_WARNING("Waited 30 seconds, dependencies setup still going...");
needsWaiting = true;
}
}
}
while(needsWaiting);
actions.clear();
finished.clear();
}
#endif //#if RUNCPP2_USE_PARALLEL_FOR_DEP
}
ssLOG_OUTPUT_ALL_CACHE_GROUPED();
return {};
}
inline DS::Result<void>
BuildDependencies( const runcpp2::Data::Profile& profile,
const Data::ScriptInfo& scriptInfo,
const std::vector<Data::DependencyInfo*>& availableDependencies,
const std::vector<std::string>& dependenciesLocalCopiesPaths,
const int maxThreads)
{
ssLOG_FUNC_INFO();
//If the script info is not populated (i.e. empty script info), don't do anything
if(!scriptInfo.Populated)
return {};
#if RUNCPP2_USE_PARALLEL_FOR_DEP
std::vector<std::future<bool>> actions;
std::vector<bool> finished;
//Cache logs for worker threads
ssLOG_ENABLE_CACHE_OUTPUT_FOR_NEW_THREADS();
int logLevel = ssLOG_GET_CURRENT_THREAD_TARGET_LEVEL();
#endif
//Run build steps
for(int i = 0; i < availableDependencies.size(); ++i)
{
ssLOG_INFO("Running build commands for " << availableDependencies.at(i)->Name);
#if RUNCPP2_USE_PARALLEL_FOR_DEP
actions.emplace_back
(
std::async
(
std::launch::async,
[
i,
&profile,
&availableDependencies,
&dependenciesLocalCopiesPaths,
logLevel
]() -> DS::Result<void>
{
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(logLevel);
#endif
DS::Result<void> depResult =
RunDependenciesSteps( profile,
availableDependencies.at(i)->Build,
dependenciesLocalCopiesPaths.at(i),
true,
false);
if(!depResult.HasValue())
{
depResult.Error().Message += "\nFailed to build dependency " +
availableDependencies.at(i)->Name;
DS_APPEND_TRACE(depResult.Error());
return depResult;
}
#if RUNCPP2_USE_PARALLEL_FOR_DEP
return {};
}
)
);
finished.emplace_back(false);
//Evaluate the build results for each batch
if(actions.size() >= maxThreads || i == availableDependencies.size() - 1)
{
bool needsWaiting = false;
do
{
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(30);
needsWaiting = false;
for(int j = 0; j < actions.size(); ++j)
{
if(finished.at(j))
continue;
if(!actions.at(j).valid())
{
ssLOG_OUTPUT_ALL_CACHE_GROUPED();
return DS_ERROR_MSG("Failed to construct actions for building dependencies");
}
std::future_status actionStatus = actions.at(j).wait_until(deadline);
if(actionStatus == std::future_status::ready)
{
DS::Result<void> actionResult = actions.at(j).get();
if(!actionResult.HasValue())
{
ssLOG_OUTPUT_ALL_CACHE_GROUPED();
actionResult.Error().Message += "\nBuild failed for dependencies";
DS_APPEND_TRACE(actionResult.Error());
return actionResult;
}
finished.at(j) = true;
}
else
{
ssLOG_WARNING("Manual interrupt might be needed");
ssLOG_WARNING("Waited 30 seconds, dependencies build still going...");
needsWaiting = true;
}
}
}
while(needsWaiting);
actions.clear();
finished.clear();
}
#endif ////#if RUNCPP2_USE_PARALLEL_FOR_DEP
}
ssLOG_OUTPUT_ALL_CACHE_GROUPED();
return {};
}
inline DS::Result<void>
GatherDependenciesBinaries( const std::vector<Data::DependencyInfo*>& availableDependencies,
const std::vector<std::string>& dependenciesCopiesPaths,
const Data::Profile& profile,
std::vector<std::string>& outBinariesPaths)
{
ssLOG_FUNC_DEBUG();
std::unordered_set<std::string> binariesPathsSet;
for(int i = 0; i < outBinariesPaths.size(); ++i)
binariesPathsSet.insert(outBinariesPaths[i]);
int minimumDependenciesCopiesCount = 0;
for(int i = 0; i < availableDependencies.size(); ++i)
{
if(availableDependencies.at(i)->LibraryType != runcpp2::Data::DependencyLibraryType::HEADER)
++minimumDependenciesCopiesCount;
}
if(minimumDependenciesCopiesCount > dependenciesCopiesPaths.size())
{
return DS_ERROR_MSG("The amount of available dependencies do not match"
" the amount of dependencies copies paths");
}
int nonLinkFilesCount = 0;
for(int i = 0; i < availableDependencies.size(); ++i)
{
ssLOG_INFO("Evaluating dependency " << availableDependencies.at(i)->Name);
if(runcpp2::HasValueFromPlatformMap(availableDependencies.at(i)->FilesToCopy))
{
const runcpp2::Data::FilesToCopyInfo& filesToCopy =
*runcpp2::GetValueFromPlatformMap(availableDependencies.at(i)->FilesToCopy);
const std::vector<std::string>* filesToGatherForProfile =
runcpp2::GetValueFromProfileMap(profile, filesToCopy.ProfileFiles);
if(filesToGatherForProfile)
{
for(int j = 0; j < filesToGatherForProfile->size(); ++j)
{
ghc::filesystem::path srcPath =
ghc::filesystem::path(dependenciesCopiesPaths.at(i)) /
filesToGatherForProfile->at(j);
std::error_code e;
if(ghc::filesystem::exists(srcPath, e))
{
const std::string processedSrcPath = runcpp2::ProcessPath(srcPath);
outBinariesPaths.push_back(processedSrcPath);
binariesPathsSet.insert(processedSrcPath);
++nonLinkFilesCount;
ssLOG_INFO("Added binary path: " << srcPath.string());
}
else
ssLOG_WARNING("File not found: " << srcPath.string());
}
}
}
std::vector<std::string> extensionsToLink;
//Get all the file extensions to gather
{
DS_ASSERT_TRUE(GetDependencyBinariesExtensionsToLink( *availableDependencies.at(i),
profile,
extensionsToLink));
const std::string* debugSymbolExt =
runcpp2::GetValueFromPlatformMap(profile.FilesTypes.DebugSymbolFile.Extension);
if(debugSymbolExt)
extensionsToLink.push_back(*debugSymbolExt);
}
if(availableDependencies.at(i)->LibraryType == Data::DependencyLibraryType::HEADER)
continue;
//Get the Search path and search library name
using PropertyMap = std::unordered_map<ProfileName, Data::DependencyLinkProperty>;
const PropertyMap& linkProperties = availableDependencies.at(i)->LinkProperties;
if(!runcpp2::HasValueFromPlatformMap(linkProperties))
{
return DS_ERROR_MSG("Link properties for dependency " + availableDependencies.at(i)->Name +
" is missing for the current platform");
}
const Data::DependencyLinkProperty& linkProperty =
*runcpp2::GetValueFromPlatformMap(linkProperties);
const Data::ProfileLinkProperty* profileLinkProperty =
runcpp2::GetValueFromProfileMap(profile, linkProperty.ProfileProperties);
if(!profileLinkProperty)
continue;
for(int searchLibIndex = 0;
searchLibIndex < profileLinkProperty->SearchLibraryNames.size();
++searchLibIndex)
{
for(int searchDirIndex = 0;
searchDirIndex < profileLinkProperty->SearchDirectories.size();
++searchDirIndex)
{
std::string currentSearchLibraryName =
profileLinkProperty->SearchLibraryNames.at(searchLibIndex);
std::string currentSearchDirectory =
profileLinkProperty->SearchDirectories.at(searchDirIndex);
if(!ghc::filesystem::path(currentSearchDirectory).is_absolute())
{
currentSearchDirectory = dependenciesCopiesPaths.at(i) + "/" +
currentSearchDirectory;
}
ssLOG_DEBUG("currentSearchDirectory: " << currentSearchDirectory);
ssLOG_DEBUG("currentSearchLibraryName: " << currentSearchLibraryName);
std::error_code e;
if( !ghc::filesystem::exists(currentSearchDirectory, e) ||
!ghc::filesystem::is_directory(currentSearchDirectory, e))
{
ssLOG_INFO("Invalid search path: " << currentSearchDirectory);
continue;
}
//Iterate each files in the directory we are searching
for(auto it : ghc::filesystem::directory_iterator(currentSearchDirectory, e))
{
if(it.is_directory())
continue;
std::string currentFileName = it.path().filename().string();
std::string currentExtension = runcpp2::GetFileExtensionWithoutVersion(it.path());
ssLOG_DEBUG("currentFileName: " << currentFileName);
ssLOG_DEBUG("currentExtension: " << currentExtension);
//TODO: Make it not case sensitive?
bool nameMatched = false;
if(currentFileName.find(currentSearchLibraryName) != std::string::npos)
nameMatched = true;
for(int excludeIndex = 0;
excludeIndex < profileLinkProperty->ExcludeLibraryNames.size();
++excludeIndex)
{
std::string currentExcludeLibraryName =
profileLinkProperty->ExcludeLibraryNames.at(excludeIndex);
if(currentFileName.find(currentExcludeLibraryName) != std::string::npos)
{
nameMatched = false;
break;
}
}
if(!nameMatched)
continue;
bool extensionMatched = false;
for(int extIndex = 0; extIndex < extensionsToLink.size(); ++extIndex)
{
if(currentExtension == extensionsToLink.at(extIndex))
{
extensionMatched = true;
break;
}
}
if(!extensionMatched)
continue;
//Handle symlink
ghc::filesystem::path resolvedPath = it.path();
{
std::error_code symlink_ec;
resolvedPath = ResolveSymlink(resolvedPath, symlink_ec);
if(symlink_ec)
return DS_ERROR_MSG("Failed to resolve symlink: " + symlink_ec.message());
}
const std::string processedPath = runcpp2::ProcessPath(it.path().string());
const std::string processedResolvedPath =
runcpp2::ProcessPath(resolvedPath.string());
if(binariesPathsSet.count(processedResolvedPath) == 0)
{
ssLOG_INFO("Linking " << processedPath);
outBinariesPaths.push_back(processedPath);
binariesPathsSet.insert(processedResolvedPath);
}
}
}
}
}
//Do a check to see if any dependencies are copied
if(outBinariesPaths.size() - nonLinkFilesCount < minimumDependenciesCopiesCount)
{
ssLOG_WARNING("We could be missing some link files for dependencies");
for(int i = 0; i < outBinariesPaths.size(); ++i)
ssLOG_WARNING("outBinariesPaths[" << i << "]: " << outBinariesPaths.at(i));
}
return {};
}
inline DS::Result<void> HandleImport( Data::DependencyInfo& dependency,
const ghc::filesystem::path& basePath)
{
ssLOG_FUNC_DEBUG();
if(dependency.Source.ImportPath.empty())
return {};
const std::string fullPath = (basePath / dependency.Source.ImportPath).string();
std::error_code ec;
if(!ghc::filesystem::exists(fullPath, ec))
return DS_ERROR_MSG("Import file not found: " + fullPath);
if(ghc::filesystem::is_directory(fullPath))
return DS_ERROR_MSG("Import path is a directory: " + fullPath);
//Parse the YAML file
YAML::ResourceHandle resource;
std::vector<YAML::NodePtr> rootNodes;
std::string content;
{
std::ifstream file(fullPath);
if(!file.is_open())
return DS_ERROR_MSG("Failed to open import file: " + fullPath);
std::stringstream buffer;
buffer << file.rdbuf();
content = buffer.str();
}
rootNodes = YAML::ParseYAML(content, resource).DS_TRY();
DEFER { YAML::FreeYAMLResource(resource); };
DS_ASSERT_FALSE(rootNodes.empty());
//Store the imported sources as copies for traciblity if needed
std::vector<std::shared_ptr<Data::DependencySource>> previouslyImportedSources;
{
std::shared_ptr<Data::DependencySource> currentImportSource =
std::make_shared<Data::DependencySource>(dependency.Source);
previouslyImportedSources = dependency.Source.ImportedSources;
dependency.Source.ImportedSources.clear();
currentImportSource->ImportedSources.clear();
previouslyImportedSources.push_back(currentImportSource);
//Reset the current dependency before we parse the import dependency
dependency = Data::DependencyInfo();
}
for(int i = 0; i < rootNodes.size(); ++i)
{
YAML::ResolveAnchors(rootNodes[i]).DS_TRY();
//Parse the imported dependency
if(!dependency.ParseYAML_Node(rootNodes[i]))
{
//If failed to parse document, fail only if we reach the last one
if(i != rootNodes.size() - 1)
continue;
std::string errMsg = "Failed to parse imported dependency: " + fullPath;
//Print the list of imported sources
for(int j = 0; j < previouslyImportedSources.size(); ++j)
{
errMsg += "Imported source[" + DS_STR(j) + "]: " =
previouslyImportedSources.at(j)->ImportPath.string();
}
return DS_ERROR_MSG(errMsg);
}
dependency.Source.ImportedSources = previouslyImportedSources;
return {};
}
return DS_ERROR_MSG("This should never be reached");
}
inline DS::Result<void> ResolveImports( Data::ScriptInfo& scriptInfo,
const ghc::filesystem::path& scriptPath,
const ghc::filesystem::path& buildDir)
{
ssLOG_FUNC_INFO();
//For each dependency, check if import path exists
for(int i = 0; i < scriptInfo.Dependencies.size(); ++i)
{
Data::DependencyInfo& dependency = scriptInfo.Dependencies.at(i);
//Check if import path exists
Data::DependencySource& source = dependency.Source;
if(source.ImportPath.empty())
continue;
if(!source.ImportPath.is_relative())
return DS_ERROR_MSG("Import path is not relative: " + source.ImportPath.string());
ghc::filesystem::path copyPath;
ghc::filesystem::path sourcePath;
GetDependencyPath(dependency, scriptPath, buildDir, copyPath, sourcePath).DS_TRY();
bool prePopulated = false;
PopulateLocalDependency(dependency, copyPath, sourcePath, buildDir, prePopulated).DS_TRY();
//Parse the import file
HandleImport(dependency, copyPath).DS_TRY();
//Check do we still have import path in the dependency. If so, we need to parse it again
if(!dependency.Source.ImportPath.empty())
--i;
}
return {};
}
inline DS::Result<void> SyncLocalDependency(const Data::DependencyInfo& dependency,
const ghc::filesystem::path& sourcePath,
const ghc::filesystem::path& copyPath)
{
ssLOG_FUNC_DEBUG();
std::error_code ec;
//Only sync if it's a local dependency
const Data::LocalSource* local = mpark::get_if<Data::LocalSource>(&dependency.Source.Source);
if(!local)
{
ssLOG_DEBUG("Not a local dependency, skipping sync");
return {};
}
//Fail if source path doesn't exist
if(!ghc::filesystem::exists(sourcePath, ec))
return DS_ERROR_MSG("Source path does not exist: " + sourcePath.string());
//Create target directory if it doesn't exist
if(!ghc::filesystem::exists(copyPath, ec))
{
if(!ghc::filesystem::create_directory(copyPath, ec))
return DS_ERROR_MSG("Failed to create directory " + copyPath.string() + ": " + ec.message());
}
//Get list of files in source
std::unordered_set<std::string> sourceFiles;
for(const ghc::filesystem::directory_entry& entry :
ghc::filesystem::directory_iterator(sourcePath, ec))
{
sourceFiles.insert(entry.path().filename().string());
}
//First pass: Check existing files in target
for(const ghc::filesystem::directory_entry& entry :
ghc::filesystem::directory_iterator(copyPath, ec))
{
const ghc::filesystem::path& targetPath = entry.path();
const ghc::filesystem::path& srcPath = sourcePath / targetPath.filename();
bool needsUpdate = false;
//Check if this is a symlink
if(ghc::filesystem::is_symlink(targetPath, ec))
{
//Verify if symlink is valid
if(!ghc::filesystem::exists(targetPath, ec))
{
ssLOG_DEBUG("Found invalid symlink, removing: " << targetPath.string());
ghc::filesystem::remove(targetPath, ec);
needsUpdate = true;
}
}
//If file exists in source, check if it needs update
if(ghc::filesystem::exists(srcPath, ec))
{
if(!needsUpdate)
{
ghc::filesystem::file_time_type srcTime =
ghc::filesystem::last_write_time(srcPath, ec);
ghc::filesystem::file_time_type dstTime =
ghc::filesystem::last_write_time(targetPath, ec);
needsUpdate = (srcTime > dstTime);
}
if(needsUpdate)
{
ssLOG_DEBUG("Updating: " << targetPath.string());
ghc::filesystem::remove(targetPath, ec);
switch(local->CopyMode)
{
case Data::LocalCopyMode::Auto:
ghc::filesystem::create_symlink(srcPath, targetPath, ec);
if(ec)
{
ssLOG_DEBUG("Symlink failed: " << ec.message());
ec.clear();
ghc::filesystem::create_hard_link(srcPath, targetPath, ec);
if(ec)
{
ssLOG_DEBUG("Hardlink failed: " << ec.message());
ec.clear();
ghc::filesystem::copy(srcPath, targetPath, ec);
}
}
break;
case Data::LocalCopyMode::Symlink:
ghc::filesystem::create_symlink(srcPath, targetPath, ec);
break;
case Data::LocalCopyMode::Hardlink:
ghc::filesystem::create_hard_link(srcPath, targetPath, ec);
break;
case Data::LocalCopyMode::Copy:
ghc::filesystem::copy(srcPath, targetPath, ec);
break;
}
if(ec)
return DS_ERROR_MSG("Failed to update target: " + ec.message());
}
sourceFiles.erase(targetPath.filename().string());
}
else
{
//File no longer exists in source, remove it
ssLOG_DEBUG("Removing file that no longer exists in source: " << targetPath.string());
ghc::filesystem::remove(targetPath, ec);
}
}
//Second pass: Add any new files from source
for(const std::string& filename : sourceFiles)
{
const ghc::filesystem::path& srcPath = sourcePath / filename;
const ghc::filesystem::path& targetPath = copyPath / filename;
ssLOG_DEBUG("Adding new file: " << targetPath.string());
switch(local->CopyMode)
{
case Data::LocalCopyMode::Auto:
ghc::filesystem::create_symlink(srcPath, targetPath, ec);
if(ec)
{
ssLOG_DEBUG("Symlink failed: " << ec.message());
ec.clear();
ghc::filesystem::create_hard_link(srcPath, targetPath, ec);
if(ec)
{
ssLOG_DEBUG("Hardlink failed: " << ec.message());
ec.clear();
ghc::filesystem::copy(srcPath, targetPath, ec);
}
}
break;
case Data::LocalCopyMode::Symlink:
ghc::filesystem::create_symlink(srcPath, targetPath, ec);
break;
case Data::LocalCopyMode::Hardlink:
ghc::filesystem::create_hard_link(srcPath, targetPath, ec);
break;
case Data::LocalCopyMode::Copy:
ghc::filesystem::copy(srcPath, targetPath, ec);
break;
}
if(ec)
return DS_ERROR_MSG("Failed to add new file: " + ec.message());
}
return {};
}
inline DS::Result<void>
SyncLocalDependencies( const std::vector<Data::DependencyInfo*>& dependencies,
const std::vector<std::string>& dependenciesSourcePaths,
const std::vector<std::string>& dependenciesCopiesPaths)
{
ssLOG_FUNC_DEBUG();
for(size_t i = 0; i < dependencies.size(); ++i)
{
SyncLocalDependency(*dependencies.at(i),
ghc::filesystem::path(dependenciesSourcePaths.at(i)),
ghc::filesystem::path(dependenciesCopiesPaths.at(i))).DS_TRY();
}
return {};
}
}
namespace
{
DS::Result<void> GetDependencyPath( const runcpp2::Data::DependencyInfo& dependency,
const ghc::filesystem::path& scriptPath,
const ghc::filesystem::path& buildDir,
ghc::filesystem::path& outCopyPath,
ghc::filesystem::path& outSourcePath)
{
ssLOG_FUNC_INFO();
ghc::filesystem::path scriptDirectory = scriptPath.parent_path();
const runcpp2::Data::DependencySource& currentSource = dependency.Source;
if(mpark::get_if<runcpp2::Data::GitSource>(¤tSource.Source))
{
const runcpp2::Data::GitSource* git =
mpark::get_if<runcpp2::Data::GitSource>(¤tSource.Source);
size_t lastSlashFoundIndex = git->URL.find_last_of("/");
size_t lastDotGitFoundIndex = git->URL.find_last_of(".git");