forked from ClxS/FASTBuild-UE4
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FastBuild.cs
1990 lines (1724 loc) · 81.2 KB
/
FastBuild.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Text.RegularExpressions;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Management;
using System.Text;
namespace UnrealBuildTool
{
public class FASTBuild : ActionExecutor
{
// <----- FastBuild Configuration Settings ----->
// Enable or Disable the *global* usage of FastBuild
static public bool IsEnabled
{
get
{
return true; // <----- Toggle FastBuild on/off <-> (default=on)
}
}
// Change me if you're running into issues with FastBuild taking too long on tiny incremental builds.
static public int MinimumRequiredActionsToEnableThreshold
{
get
{
return 0; // <----- Don't build with FastBuild unless there are more than threshold number of actions to build <-> (default=0)
}
}
// Change me if you're running into issues with FastBuild taking too long on medium sized incremental builds.
static public int MinimumRequiredActionsToEnableDistributionAndCachingThreshold
{
get
{
return 25; // <----- Don't enable FastBuild Distribution and Caching features unless there are more than threshold number of actions to build <-> (default=25)
}
}
static public bool UseSinglePassCompilation
{
get
{
return false; // <----- Should build and link in a single FastBuild .bff step on/off <-> (default=off)
}
}
static private bool AllowCache
{
get
{
return false; // <----- Toggle use of FastBuild Cache on/off <-> (default=on)
}
}
static public bool AllowDistribution
{
get
{
return true; // <----- Toggle FastBuild Distributed Builds on/off <-> (default=on)
}
}
static public bool ContinueOnError
{
get
{
return false; // <----- Should keep building after an error on/off <-> (default=off)
}
}
static public bool EnableMonitorAPIMode
{
get
{
return true; // <----- (-monitor) Toggle FastBuildMonitor Build Visualization Support on/off <-> (default=off)
}
}
static public bool UseIDEMode
{
get
{
return true; // <----- Toggle FastBuild IDE Integration Mode on/off <-> (default=on)
}
}
static public bool ShowBuildSummary
{
get
{
return true; // <----- Show a summary at the end of the build: on/off <-> (default=on)
}
}
static public bool EnableBuildReport
{
get
{
return false; // <----- Output a report at build termination: on/off <-> (default=off) <on->slow>
}
}
static public bool EnableShowCommandLinesInvoked
{
get
{
return false; // <----- (-showcmds) Displays the full command lines passed to external tools as they are invoked: on/off <-> (default=off) <on->slow>
}
}
static public bool EnableShowDefinedTargets
{
get
{
return false; // <----- (-showtargets) Displays the list of targets defined in the bff configuraiton file: on/off <-> (default=off) <on->slow>
}
}
static public bool EnableVerboseDebugging
{
get
{
return false; // <----- Show detailed diagnostic information for debugging: on/off <-> (default=off) <on->slow>
}
}
static public bool EnableInternalDebugging
{
get
{
return false; // <----- Show detailed internal FastBuild.cs diagnostic information for debugging: on/off <-> (default=off) <on->slow>
}
}
static public bool AppendUEGlobalCompilerDefines
{
get
{
return false; // <----- Force appending of UE Global Compiler Defines to .CompilerOptions: on/off <-> (default=off)
}
}
static public bool ForceAppendBaseIncludePaths
{
get
{
return false; // <----- Force appending of .BaseIncludePaths to .CompilerOptions: on/off <-> (default=off)
}
}
// <----- FastBuild Configuration Settings ----->
static private bool? _allowCacheWrite = null;
static public bool EnableCacheGenerationMode
{
get
{
if (!_allowCacheWrite.HasValue)
{
//Add your own way of checking here. I check whether it's running on the CI machine in my case.
// TODO: Check whether running on CI machine, set _allowCacheWrite=true if so.
_allowCacheWrite = false;
}
return _allowCacheWrite.Value;
}
}
static private bool _EnableCache = AllowCache;
static private bool _EnableDistribution = AllowDistribution;
static private bool EnableCache
{
get
{
return (AllowCache && _EnableCache);
}
}
static public bool EnableDistribution
{
get
{
return (AllowDistribution && _EnableDistribution);
}
}
static public void InternalDebugLog( string Format, params object[] Args )
{
if ( EnableInternalDebugging )
{
Log.TraceInformation(Format, Args);
}
}
const string FastBuild_Win8Version = "winv6.3";
//const string FastBuild_Win10Version = "10.0.10240.0";
//const string FastBuild_Win10Version = "10.0.10586.0";
const string FastBuild_Win10Version = "10.0.14393.0";
const string FastBuild_EnvVar = "FASTBUILD_ROOT_DIR";
static public bool GetFastBuildParameters( out string oFastBuildRootPath, out string oSdkDir, out string oSdk10Dir )
{
bool bDoesFastBuildExist = false;
string FastBuildRootPath = Environment.GetEnvironmentVariable( FastBuild_EnvVar );
string ErrorUnassigned = "error-unassigned";
string FastBuildExe = ErrorUnassigned;
string CompilerDir = ErrorUnassigned;
string SdkDir = ErrorUnassigned;
string Sdk10Dir = ErrorUnassigned;
string Sdk10IncludeDir = ErrorUnassigned;
string VSVersion = ErrorUnassigned;
switch (WindowsPlatform.Compiler)
{
case WindowsCompiler.VisualStudio2013:
VSVersion = "13.4";
break;
case WindowsCompiler.VisualStudio2015:
VSVersion = "14.0";
break;
case WindowsCompiler.VisualStudio2017:
VSVersion = "15.0";
break;
case WindowsCompiler.Default:
default:
throw new Exception("Error, unsupported Compiler Version.");
};
if (!String.IsNullOrEmpty(FastBuildRootPath))
{
string ExternalVSCompiler = "External/VS" + VSVersion;
FastBuildExe = Path.Combine(FastBuildRootPath, "FBuild.exe");
CompilerDir = Path.Combine(FastBuildRootPath, ExternalVSCompiler);
SdkDir = Path.Combine(FastBuildRootPath, "External/Windows8.1");
Sdk10Dir = Path.Combine(FastBuildRootPath, "External/Windows10");
Sdk10IncludeDir = Path.Combine(Sdk10Dir, "Include", FastBuild_Win10Version);
bDoesFastBuildExist = File.Exists(FastBuildExe) && Directory.Exists(CompilerDir) && Directory.Exists(SdkDir) && Directory.Exists(Sdk10Dir) && Directory.Exists(Sdk10IncludeDir);
}
if (!bDoesFastBuildExist)
{
Log.TraceInformation("FastBuildRootPath: {0}, was {1}", FastBuildRootPath, (Directory.Exists(FastBuildRootPath)) ? "found" : "not found");
Log.TraceInformation("FastBuildExe: {0}, was {1}", FastBuildExe, (File.Exists(FastBuildExe)) ? "found" : "not found");
Log.TraceInformation("Windows8.1 SDK: {0}, was {1}", SdkDir, (Directory.Exists(SdkDir)) ? "found" : "not found");
Log.TraceInformation("Windows10 Base SDK: {0}, was {1}", Sdk10Dir, (Directory.Exists(Sdk10Dir)) ? "found" : "not found");
Log.TraceInformation("Windows10 SDK Version: {0}, is {1}", Sdk10IncludeDir, (Directory.Exists(Sdk10IncludeDir)) ? "found" : "not found");
Log.TraceInformation("Expected Windows10 SDK Version: {0}, is {1}", FastBuild_Win10Version, (Directory.Exists(Sdk10IncludeDir)) ? "installed" : "not installed");
Log.TraceError("Error, expected Compiler and/or SDK not found in FastBuildRootPath! Please install the missing component (possibly by updating Visual Studio - please ask Layla if you have questions).");
}
oFastBuildRootPath = FastBuildRootPath;
oSdkDir = SdkDir;
oSdk10Dir = Sdk10Dir;
return bDoesFastBuildExist;
}
static public bool DoesFastBuildExist( )
{
string unused_FastBuildRootPath;
string unused_SdkDir;
string unused_Sdk10Dir;
return GetFastBuildParameters(out unused_FastBuildRootPath, out unused_SdkDir, out unused_Sdk10Dir);
}
public static bool ConfigureForBuildParameters( int iNumActionsToPerform )
{
// Refresh these to the current Allow settings
_EnableCache = AllowCache;
_EnableDistribution = AllowDistribution;
if( iNumActionsToPerform < MinimumRequiredActionsToEnableThreshold )
{
return false; // Don't use FastBuild at all for this build
}
else if ( iNumActionsToPerform <= MinimumRequiredActionsToEnableDistributionAndCachingThreshold )
{
// Force off Caching and Distribution for this build
_EnableCache = false;
_EnableDistribution = false;
}
return true; // Do use FastBuild for this build
}
static private string _UEGlobalCompilerDefinitions = null;
public static string GetUEGlobalCompilerDefinitions()
{
if( !AppendUEGlobalCompilerDefines )
{
return "";
}
if (_UEGlobalCompilerDefinitions == null)
{
// TODO: Figure out how to get these from GlobalCompileEnvironment.Config.Definitions
_UEGlobalCompilerDefinitions = "/DUE_BUILD_DEVELOPMENT /DPLATFORM_WINDOWS /DWITH_EDITOR=1 /DWITH_ENGINE=1 /DWITH_UNREAL_DEVELOPER_TOOLS=1 /DWITH_PLUGIN_SUPPORT=1 /DUE_BUILD_MINIMAL=0 /DIS_MONOLITHIC=0 /DIS_PROGRAM=0";
//InBuildTarget.GlobalCompileEnvironment.Config.Definitions.Add("WITH_EDITOR=0");
/* foreach (var define in GlobalCompileEnvironment.Config.Definitions)
{
_UEGlobalCompilerDefinitions += define + " ";
}
*/
}
return _UEGlobalCompilerDefinitions;
}
private struct FastBuildCommon
{
public static string EntryArguments
{
get
{
string FastBuildRootPath = "error-invalid-fast-build-root-path";
string SdkDir = "error-invalid-fast-build-sdk-dir";
string Sdk10Dir = "error-invalid-fast-build-sdk-10-dir";
bool bDoesFastBuildExist = GetFastBuildParameters(out FastBuildRootPath, out SdkDir, out Sdk10Dir);
if (!bDoesFastBuildExist)
{
throw new Exception("Error, expected Compiler and/or SDK not found in FastBuildRootPath! Please install the missing component (possibly by updating Visual Studio - please ask Layla if you have questions).");
}
string EntryArguments = ";-------------------------------------------------------------------------------\r\n";
EntryArguments += "; Windows Platform\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
if (WindowsPlatform.Compiler == WindowsCompiler.VisualStudio2013)
{
string FastBuild_VSBasePath = FastBuildRootPath + "/External/VS13.4/VC";
EntryArguments += ".VSBasePath = '";
EntryArguments += FastBuild_VSBasePath;
EntryArguments += "'\r\n";
}
else if (WindowsPlatform.Compiler == WindowsCompiler.VisualStudio2015)
{
string FastBuild_VSBasePath = FastBuildRootPath + "/External/VS14.0/VC";
EntryArguments += ".VSBasePath = '";
EntryArguments += FastBuild_VSBasePath;
EntryArguments += "'\r\n";
}
else if (WindowsPlatform.Compiler == WindowsCompiler.VisualStudio2017)
{
string FastBuild_VSBasePath = FastBuildRootPath + "/External/VS15.0/VC";
EntryArguments += ".VSBasePath = '";
EntryArguments += FastBuild_VSBasePath;
EntryArguments += "'\r\n";
}
//EntryArguments += ".ClangBasePath = '../Extras/FASTBuild/External/LLVM'\r\n";
string FastBuild_WinSDKBasePath = SdkDir;
EntryArguments += ".WindowsSDKBasePath = '";
EntryArguments += FastBuild_WinSDKBasePath;
EntryArguments += "'\r\n";
EntryArguments += ".Windows8SDKVersion = '";
EntryArguments += FastBuild_Win8Version;
EntryArguments += "'\r\n";
EntryArguments += ".Windows10SDKVersion = '";
EntryArguments += FastBuild_Win10Version;
EntryArguments += "'\r\n";
string FastBuild_WinUMLibraryBasePath = SdkDir + "/lib/$Windows8SDKVersion$/um";
EntryArguments += ".WindowsUMLibraryPath = '";
EntryArguments += FastBuild_WinUMLibraryBasePath;
EntryArguments += "'\r\n";
string FastBuild_WinUCRTBasePath = Sdk10Dir + "/include/$Windows10SDKVersion$/ucrt";
EntryArguments += ".WindowsUCRTBasePath = '";
EntryArguments += FastBuild_WinUCRTBasePath;
EntryArguments += "'\r\n";
string FastBuild_WinUCRTLibraryBasePath = Sdk10Dir + "/lib/$Windows10SDKVersion$/ucrt";
EntryArguments += ".WindowsUCRTLibraryPath = '";
EntryArguments += FastBuild_WinUCRTLibraryBasePath;
EntryArguments += "'\r\n";
//EntryArguments += ".WindowsSDKBasePath = '../Extras/FASTBuild/External/Windows8.1'\r\n";
//EntryArguments += ".OrbisSDK = '../Extras/FASTBuild/External/Orbis'\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
EntryArguments += "; Base (library) includes\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
EntryArguments += ".BaseIncludePaths = ' /I\"$VSBasePath$/include/\"'\r\n";
EntryArguments += " + ' /I\"$VSBasePath$/atlmfc/include/\"'\r\n";
EntryArguments += " + ' /I\"$WindowsSDKBasePath$/include/um/\"'\r\n";
EntryArguments += " + ' /I\"$WindowsSDKBasePath$/include/shared/\"'\r\n";
EntryArguments += " + ' /I\"$WindowsSDKBasePath$/include/winrt/\"'\r\n";
EntryArguments += " + ' /I\"$WindowsUCRTBasePath$/\"'\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
EntryArguments += "; Base (library) directories (x86)\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
EntryArguments += ".BaseLibraryPathsx86 = ' /LIBPATH:\"$VSBasePath$/lib/\"'\r\n";
EntryArguments += " + ' /LIBPATH:\"$WindowsUMLibraryPath$/x86/\"'\r\n";
EntryArguments += " + ' /LIBPATH:\"$WindowsUCRTLibraryPath$/x86/\"'\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
EntryArguments += "; Base (library) directories (x64)\r\n";
EntryArguments += ";-------------------------------------------------------------------------------\r\n";
EntryArguments += ".BaseLibraryPathsx64 = ' /LIBPATH:\"$VSBasePath$/lib/amd64/\"'\r\n";
EntryArguments += " + ' /LIBPATH:\"$WindowsUMLibraryPath$/x64/\"'\r\n";
EntryArguments += " + ' /LIBPATH:\"$WindowsUCRTLibraryPath$/x64/\"'\r\n";
return EntryArguments;
}
}
public static string GetAliasTag(string alias, List<string> targets)
{
string output = "";
int targetCount = targets.Count;
output += "Alias( '" + alias + "' )\r\n";
output += "{\r\n";
output += " .Targets = {";
for (int i = 0; i < targetCount; ++i)
{
output += "'" + targets[i] + "'";
if (i < targetCount - 1)
{
output += ",";
}
}
output += " }\r\n";
output += "}\r\n";
return output;
}
}
private static void DeresponsifyActions(List<Action> actions)
{
FASTBuild.InternalDebugLog("The new post-UE4.13/4.15 UBT stores arguments in .response files - Deresponsifying {0} Actions", actions.Count);
int i = 0;
// UE4.13 started to shove the entire argument into response files. This does not work
// well with FASTBuild so we'll have to undo it.
foreach (var action in actions)
{
FASTBuild.InternalDebugLog("Deresponsifying Action {0}, {1}", i, action.ToString());
//FASTBuild.InternalDebugLog("Deresponsifying Action {0}, {1}", i, action.CommandArguments);
// Not a response file! Copy these args directly into the output args!!!! (?)
if( !(
action.CommandArguments.StartsWith("@\"")
||
action.CommandArguments.StartsWith(" @\"")
))
{
FASTBuild.InternalDebugLog("Deresponsifying Action {0} not required (it is already in argument form).", i++);
continue;
}
if (
(!action.CommandArguments.EndsWith("\""))
||
(action.CommandArguments.Count(f => f == '"') != 2)
)
{
FASTBuild.InternalDebugLog("Deresponsifying Action {0} skipped.", i++);
continue;
}
var file = Regex.Match(action.CommandArguments, "(?<=@\")(.*?)(?=\")");
if (!file.Success)
{
FASTBuild.InternalDebugLog("Deresponsifying Action {0} failed because regex did not match.", i++);
continue;
}
var arg = File.ReadAllText(file.Value);
var newarg = arg.Replace(System.Environment.NewLine, " ");
action.CommandArguments = newarg;
FASTBuild.InternalDebugLog("Deresponsifying Action {0} into: {1}", i++, newarg);
}
}
private static readonly Dictionary<string, Compiler> Compilers = new Dictionary<string, Compiler>();
private static readonly Dictionary<string, Linker> Linkers = new Dictionary<string, Linker>();
private static int AliasBase { get; set; } = 1;
public enum ExecutionResult
{
Unavailable,
TasksFailed,
TasksSucceeded,
}
private enum BuildStep
{
CompileObjects,
Link,
CompileAndLink
}
private enum CompilerType
{
MSVC,
RC,
Clang,
OrbisClang,
OrbisSnarl
}
private enum CompilerPlatform
{
amd64,
x86,
Unknown
}
public enum LinkerType
{
Static,
Dynamic
}
private class Compiler : Linker
{
public Compiler(string exePath)
: base(exePath)
{
LocaliseCompilerPath();
}
public override string InputFileRegex
{
get
{
return "(?<=( \")|(@\"))(.*?)(?=\")";
}
}
public override string OutputFileRegex
{
get
{
if (Type == CompilerType.MSVC || Type == CompilerType.RC)
{
return "(?<=(/Fo \"|/Fo\"))(.*?)(?=\")";
}
else
{
return "(?<=(-o \"|-o\"))(.*?)(?=\")";
}
}
}
public override string PCHOutputRegex
{
get
{
if (Type == CompilerType.MSVC || Type == CompilerType.RC)
{
return "(?<=(/Fp \"|/Fp\"))(.*?)(?=\")";
}
else
{
return "(?<=(/Fp \"|/Fp\"))(.*?)(?=\")";
}
}
}
public string Alias;
public string GetBffArguments(string Arguments, string AdditionalArguments)
{
StringBuilder output = new StringBuilder();
string myUEGlobalCompilerDefinitions = FASTBuild.GetUEGlobalCompilerDefinitions();
output.AppendFormat(" .CompilerOptions\t = '{0}{1} {2}'\r\n", Arguments, AdditionalArguments, myUEGlobalCompilerDefinitions);
FASTBuild.InternalDebugLog("GetBffArguments::ToString(): {0}", output.ToString());
return output.ToString();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Compiler('{0}')\n{{\n", Alias);
sb.AppendFormat("\t.Executable\t\t = '{0}' \n " +
"\t.ExtraFiles\t\t = {{ {1} }}\n",
ExecPath,
string.Join("\n\t\t\t", GetExtraFiles().Select(e => "\t\t'" + e + "'")));
sb.Append("}\n");
FASTBuild.InternalDebugLog("COMPILER TOSTRING: {0}", sb.ToString());
return sb.ToString();
}
private IEnumerable<string> GetExtraFiles()
{
if (Type != CompilerType.MSVC) return Enumerable.Empty<string>();
var output = new List<string>();
string msvcVer = "";
switch(WindowsPlatform.Compiler)
{
case WindowsCompiler.VisualStudio2013:
msvcVer = "120";
break;
case WindowsCompiler.VisualStudio2015:
msvcVer = "140";
break;
case WindowsCompiler.VisualStudio2017:
msvcVer = "150";
break;
};
string msIncludeDir;
string compilerDir = Path.GetDirectoryName(ExecPath);
if (Platform == CompilerPlatform.Unknown)
{
DetectPlatform(compilerDir); // Should already be done by Linker
}
if (Type == CompilerType.MSVC)
{
output.Add(compilerDir + "\\1033\\clui.dll");
if (WindowsPlatform.Compiler == WindowsCompiler.VisualStudio2013)
{
output.Add(compilerDir + "\\c1ast.dll");
output.Add(compilerDir + "\\c1xxast.dll");
}
output.Add(compilerDir + "\\c1xx.dll");
output.Add(compilerDir + "\\c2.dll");
output.Add(compilerDir + "\\c1.dll");
if (compilerDir.Contains("x86_amd64") || compilerDir.Contains("amd64_x86"))
{
msIncludeDir = "$VSBasePath$\\bin"; //We need to include the x86 version of the includes
}
else
{
msIncludeDir = compilerDir;
}
output.Add(compilerDir + "\\msobj" + msvcVer + ".dll");
output.Add(compilerDir + "\\mspdb" + msvcVer + ".dll");
output.Add(compilerDir + "\\mspdbsrv.exe");
output.Add(compilerDir + "\\mspdbcore.dll");
output.Add(compilerDir + "\\mspft" + msvcVer + ".dll");
}
return output;
}
private void LocaliseCompilerPath()
{
string compilerPath = "";
if (ExecPath.Contains("cl.exe"))
{
string[] compilerPathComponents = ExecPath.Replace('\\', '/').Split('/');
int startIndex = Array.FindIndex(compilerPathComponents, row => row == "VC");
if (startIndex > 0)
{
Type = CompilerType.MSVC;
compilerPath = "$VSBasePath$";
for (int i = startIndex + 1; i < compilerPathComponents.Length; ++i)
{
compilerPath += "/" + compilerPathComponents[i];
}
}
ExecPath = compilerPath;
}
else if (ExecPath.Contains("rc.exe"))
{
Type = CompilerType.RC;
string[] compilerPathComponents = ExecPath.Replace('\\', '/').Split('/');
compilerPath = "$WindowsSDKBasePath$";
int startIndex = Array.FindIndex(compilerPathComponents, row => row == "8.1");
if (startIndex > 0)
{
for (int i = startIndex + 1; i < compilerPathComponents.Length; ++i)
{
compilerPath += "/" + compilerPathComponents[i];
}
}
ExecPath = compilerPath;
}
else if (ExecPath.Contains("orbis-clang.exe"))
{
Type = CompilerType.OrbisClang;
}
}
}
private class Linker
{
public Linker(string execPath)
{
ExecPath = execPath;
LocaliseLinkerPath();
DetectPlatform(ExecPath);
}
private void LocaliseLinkerPath()
{
string compilerPath = "";
if (ExecPath.Contains("link.exe") || ExecPath.Contains("lib.exe"))
{
string[] compilerPathComponents = ExecPath.Replace('\\', '/').Split('/');
int startIndex = Array.FindIndex(compilerPathComponents, row => row == "VC");
if (startIndex > 0)
{
Type = CompilerType.MSVC;
compilerPath = "$VSBasePath$";
for (int i = startIndex + 1; i < compilerPathComponents.Length; ++i)
{
compilerPath += "/" + compilerPathComponents[i];
}
}
ExecPath = compilerPath;
}
else if (ExecPath.Contains("orbis-snarl.exe"))
{
Type = CompilerType.OrbisSnarl;
}
else if (ExecPath.Contains("orbis-clang.exe"))
{
Type = CompilerType.OrbisClang;
}
}
public static bool IsKnownLinker(string args)
{
return args.Contains("lib.exe") || args.Contains("link.exe") || args.Contains("orbis-clang.exe") || args.Contains("orbis-snarl.exe");
}
protected void DetectPlatform(string Path)
{
// TODO: PS4 and other platform support
if (Path.Contains("amd64") || Path.Contains("x64"))
{
Platform = CompilerPlatform.amd64;
}
else if (Path.Contains("x86"))
{
Platform = CompilerPlatform.x86;
}
else
{
Platform = CompilerPlatform.Unknown;
Log.TraceInformation("Error, unknown platform type when parsing linker Path: {0}", Path);
throw new Exception("Error, unknown platform type! Fix it");
}
}
public string ExecPath { get; set; }
public CompilerType Type;
public CompilerPlatform Platform = CompilerPlatform.Unknown;
private List<string> _allowedInputTypes;
public virtual List<string> AllowedInputTypes
{
get
{
if (_allowedInputTypes != null) return _allowedInputTypes;
switch (Type)
{
case CompilerType.MSVC:
_allowedInputTypes = new List<string>() { ".response", ".lib", ".obj" };
break;
case CompilerType.OrbisClang:
case CompilerType.OrbisSnarl:
_allowedInputTypes = new List<string>() { ".response", ".a" };
break;
case CompilerType.RC:
case CompilerType.Clang:
default:
break;
};
return _allowedInputTypes;
}
}
private List<string> _allowedOutputTypes;
public virtual List<string> AllowedOutputTypes
{
get
{
if (_allowedOutputTypes != null) return _allowedOutputTypes;
switch( Type )
{
case CompilerType.MSVC:
_allowedOutputTypes = new List<string>() { ".dll", ".lib", ".exe" };
break;
case CompilerType.OrbisClang:
case CompilerType.OrbisSnarl:
_allowedOutputTypes = new List<string>() { ".self", ".a", ".so" };
break;
case CompilerType.RC:
case CompilerType.Clang:
default:
break;
};
return _allowedOutputTypes;
}
}
public virtual string InputFileRegex
{
get
{
return Type == CompilerType.OrbisClang ? "(?<=\")(.*?)(?=\")" : "(?<=@\")(.*?)(?=\")";
}
}
public virtual string OutputFileRegex
{
get
{
switch (Type)
{
case CompilerType.MSVC:
case CompilerType.RC:
return "(?<=(/OUT: \"|/OUT:\"))(.*?)(?=\")";
case CompilerType.Clang:
case CompilerType.OrbisClang:
return "(?<=(-o \"|-o\"))(.*?)(?=\")";
case CompilerType.OrbisSnarl:
return "(?<=\")(.*?.a)(?=\")";
default:
break;
};
return "";
}
}
public virtual string ImportLibraryRegex
{
get
{
if (Type == CompilerType.MSVC || Type == CompilerType.RC)
{
return "(?<=(/IMPLIB: \"|/IMPLIB:\"))(.*?)(?=\")";
}
return "";
}
}
public virtual string PCHOutputRegex
{
get
{
if (Type == CompilerType.MSVC || Type == CompilerType.RC)
{
return "(?<=(/Fp \"|/Fp\"))(.*?)(?=\")";
}
else
{
return "(?<=(/Fp \"|/Fp\"))(.*?)(?=\")";
}
}
}
}
public abstract class FastbuildAction
{
public FastbuildAction()
{
Dependencies = new List<FastbuildAction>();
}
public Action Action { get; set; }
public string NodeType { get; set; }
public int AliasIndex { get; set; }
public List<FastbuildAction> Dependencies { get; set; }
public string Alias
{
get
{
return NodeType + "-" + AliasIndex;
}
}
}
public static List<FastbuildAction> CompilationActions { get; set; }
public static List<FastbuildAction> LinkerActions { get; set; }
public static List<FastbuildAction> AllActions => CompilationActions.Concat(LinkerActions).ToList();
private class ExecAction : FastbuildAction
{
public ExecAction()
{
NodeType = "Exec";
}
public Linker Linker;
public string Arguments { get; set; }
private bool ParseOutputViaRegex(string regex, string Arguments, string FailureString, out string OutputString)
{
// TODO: For some reason, FastBuild is wanting us to transform this to a relative path, relative to UnrealEngine\Engine\Source
// TODO: Figure out how to keep it as an absolute path instead?
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
Match m = r.Match(Arguments);
if ( m.Success )
{
Group g = m.Groups[1]; // Group[0] is the full string we captured, Group[1] should be the string inside quotes.
string CapturedString = g.ToString();
FASTBuild.InternalDebugLog("REGEX TOTAL CAPTURE: {0}", m.Groups[1].ToString());
FASTBuild.InternalDebugLog("REGEX INSIDE CAPTURE: {0}", CapturedString);
var WithoutQuotes = CapturedString.Replace("\"", ""); // Remove double-quotes
const string UEEngine = "UnrealEngine\\Engine";
int UEEngineLocation = WithoutQuotes.IndexOf(UEEngine);
UEEngineLocation += UEEngine.Length;
string AfterEngineString = WithoutQuotes.Substring(UEEngineLocation);
OutputString = "..\\" + AfterEngineString;
return true;
}
OutputString = FailureString;
return false;
}
private bool ParseLinkerOutput( string Arguments, string FailureString, out string OutputString)
{
// /OUT:"C:\Users\layla\Desktop\Insightful\dev\UE4\UnrealEngine\Engine\Binaries\Win64\UE4Editor-Matinee.dll"
return ParseOutputViaRegex("/OUT:(\"[^\"\r\n]*\")", Arguments, FailureString, out OutputString);
}
private bool ParseCompilerOutput( string Arguments, string FailureString, out string OutputString )
{
// /Fo"C:\Users\layla\Desktop\Insightful\dev\UE4\UnrealEngine\Engine\Binaries\Win64\UE4Editor-Matinee.obj"
return ParseOutputViaRegex("/Fo(\"[^\"\r\n]*\")", Arguments, FailureString, out OutputString);
}
public override string ToString()
{
string OutputFile = Alias + "-Output.exe";
string NewOutputFile = "";
bool bParsedCompilerRegexOK = ParseCompilerOutput(Arguments, OutputFile, out NewOutputFile);
if (!bParsedCompilerRegexOK)
{
FASTBuild.InternalDebugLog("Failed to parse Compiler Regex from Arguments.");
bool bParsedLinkerRegexOK = ParseLinkerOutput(Arguments, OutputFile, out NewOutputFile);
if (!bParsedLinkerRegexOK)
{
FASTBuild.InternalDebugLog("Failed to parse Linker Regex from Arguments.");
throw new Exception("Error! Failed to parse either linker or compiler regex from Arguments.");
}
else
{
FASTBuild.InternalDebugLog("Parsed Linker Regex from Arguments: {0}", NewOutputFile);
}
}
else
{
FASTBuild.InternalDebugLog("Parsed Compiler Regex from Arguments: {0}", NewOutputFile);
}
OutputFile = NewOutputFile;
//Carry on here. Need to strip input/output out, or change to not require in/out
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Exec('{0}')\n{{\n", Alias);
sb.AppendFormat("\t.ExecExecutable\t\t = '{0}' \n " +
"\t.ExecArguments\t\t = '{1}'\n" +
"\t.DoNotUseOutput = true\n" +
//"\t.ExecOutput\t\t = '{2}-{0}'\n",
//"\t.ExecOutput\t\t = '{2}-Output.exe'\n",
"\t.ExecOutput\t\t = '{2}'\n",
Linker.ExecPath,
Arguments, OutputFile /*Alias*/);
if (Dependencies.Any())
{
sb.AppendFormat("\t.PreBuildDependencies\t\t= {{ {0} }} \n ",
string.Join("\n\t\t\t", Dependencies.Select(d => "'" + d.Alias + "'").Distinct()));
}
sb.Append("}\n");