-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTasks.cs
1036 lines (929 loc) · 37 KB
/
Tasks.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
// **********************************************************************
//
// Copyright (c) 2009-2018 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace IceBuilder.MSBuild
{
public class TaskUtil
{
#if NETSTANDARD2_0
public static readonly bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#else
public static readonly bool isWindows = true;
#endif
public static string MakeRelative(string from, string to)
{
if(!Path.IsPathRooted(from))
{
throw new ArgumentException(string.Format("from: `{0}' must be an absolute path", from));
}
else if(!Path.IsPathRooted(to))
{
return to;
}
string[] firstPathParts =
Path.GetFullPath(from).Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
string[] secondPathParts =
Path.GetFullPath(to).Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
int sameCounter = 0;
while(sameCounter < Math.Min(firstPathParts.Length, secondPathParts.Length) &&
string.Equals(firstPathParts[sameCounter], secondPathParts[sameCounter],
StringComparison.CurrentCultureIgnoreCase))
{
++sameCounter;
}
// Different volumes, relative path not possible.
if(sameCounter == 0)
{
return to;
}
// Pop back up to the common point.
string newPath = "";
for(int i = sameCounter; i < firstPathParts.Length; ++i)
{
newPath += ".." + Path.DirectorySeparatorChar;
}
// Descend to the target.
for(int i = sameCounter; i < secondPathParts.Length; ++i)
{
newPath += secondPathParts[i] + Path.DirectorySeparatorChar;
}
return newPath.TrimEnd(Path.DirectorySeparatorChar);
}
public class StreamReader
{
public string Output
{
get
{
return _output;
}
}
public string Error
{
get
{
return _error;
}
}
public void ouput(object sendingProcess, DataReceivedEventArgs outLine)
{
if(outLine.Data != null)
{
_output += outLine.Data;
}
}
public void error(object sendingProcess, DataReceivedEventArgs outLine)
{
if(outLine.Data != null)
{
_error += outLine.Data;
}
}
private string _output;
private string _error;
}
public static int RunCommand(string workingDir, string command, string args, ref string output, ref string error)
{
Process process = new Process();
process.StartInfo.FileName = command;
process.StartInfo.Arguments = args;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.WorkingDirectory = workingDir;
var streamReader = new StreamReader();
process.OutputDataReceived += new DataReceivedEventHandler(streamReader.ouput);
process.ErrorDataReceived += new DataReceivedEventHandler(streamReader.error);
try
{
process.Start();
//
// When StandardError and StandardOutput are redirected, at least one
// should use asynchronous reads to prevent deadlocks when calling
// process.WaitForExit; the other can be read synchronously using ReadToEnd.
//
// See the Remarks section in the below link:
//
// http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx
//
// Start the asynchronous read of the standard output stream.
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Read Standard error.
process.WaitForExit();
error = streamReader.Error;
output = streamReader.Output;
return process.ExitCode;
}
catch(Exception ex)
{
error = ex.ToString();
return 1;
}
}
}
public abstract class SliceCompilerTask : ToolTask
{
[Required]
public string WorkingDirectory
{
get;
set;
}
[Required]
public string IceHome
{
get;
set;
}
[Required]
public string IceToolsPath
{
get;
set;
}
[Required]
public string OutputDir
{
get;
set;
}
[Required]
public ITaskItem[] Sources
{
get;
set;
}
public string[] IncludeDirectories
{
get;
set;
}
public string[] AdditionalOptions
{
get;
set;
}
[Output]
public ITaskItem[] ComputedSources
{
get;
private set;
}
protected override string GetWorkingDirectory()
{
return WorkingDirectory;
}
protected virtual string GetGeneratedPath(ITaskItem item, string outputDir, string ext)
{
return Path.Combine(outputDir,
Path.GetFileName(Path.ChangeExtension(item.GetMetadata("Identity"), ext)));
}
protected abstract string GeneratedExtensions
{
get;
}
protected virtual Dictionary<string, string> GetOptions()
{
var options = new Dictionary<string, string>();
options["IceHome"] = IceHome;
options["IceToolsPath"] = IceToolsPath;
options["OutputDir"] = OutputDir.TrimEnd('\\');
if(IncludeDirectories != null && IncludeDirectories.Length > 0)
{
options["IncludeDirectories"] = string.Join(";", IncludeDirectories);
}
if(AdditionalOptions != null)
{
options["AdditionalOptions"] = string.Join(";", AdditionalOptions);
}
return options;
}
protected abstract void TraceGenerated();
protected override string GenerateCommandLineCommands()
{
UsageError = false;
CommandLineBuilder builder = new CommandLineBuilder(false);
if(!string.IsNullOrEmpty(OutputDir))
{
builder.AppendSwitch("--output-dir");
builder.AppendFileNameIfNotNull(OutputDir);
}
if(IncludeDirectories != null)
{
foreach(string path in IncludeDirectories)
{
builder.AppendSwitchIfNotNull("-I", path);
}
}
builder.AppendSwitchIfNotNull("-I", Path.Combine(IceHome, "slice"));
if(AdditionalOptions != null)
{
foreach(var option in AdditionalOptions)
{
builder.AppendTextUnquoted(" ");
builder.AppendTextUnquoted(option);
}
}
builder.AppendFileNamesIfNotNull(Sources, " ");
return builder.ToString();
}
protected abstract ITaskItem[] GeneratedItems(ITaskItem source);
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
int status = base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
if(status == 0)
{
TraceGenerated();
//
// Recompue dependencies
//
string output = "";
string error = "";
status = TaskUtil.RunCommand(WorkingDirectory, pathToTool, commandLineCommands + " --depend-xml",
ref output, ref error);
if(status == 0)
{
List<ITaskItem> computed = new List<ITaskItem>();
XmlDocument dependsDoc = new XmlDocument();
dependsDoc.LoadXml(output);
foreach(ITaskItem source in Sources)
{
var inputs = new List<string>();
var depends = dependsDoc.DocumentElement.SelectNodes(
string.Format("/dependencies/source[@name='{0}']/dependsOn",
source.GetMetadata("Identity")));
if(depends != null)
{
foreach(XmlNode depend in depends)
{
inputs.Add(depend.Attributes["name"].Value);
}
}
//
// Save the dependencies for each source to a dependency file
//
// Foo.ice -> $(OutputDir)/SliceCompile.Foo.d
//
var doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("dependencies",
new XElement("source", new XAttribute("name", source.GetMetadata("Identity")),
inputs.Select(path => new XElement("dependsOn", new XAttribute("name", path))),
new XElement("options",
GetOptions().Select(e => new XElement(e.Key, e.Value))))));
doc.Save(Path.Combine(OutputDir,
string.Format("SliceCompile.{0}.d", source.GetMetadata("Filename"))));
//
// Update the Inputs and Outputs metadata of the output sources,
// these info will be use to write the TLog files
//
inputs = inputs.Select(path => Path.GetFullPath(path)).ToList();
inputs.Add(source.GetMetadata("FullPath").ToUpper());
inputs.Add(Path.GetFullPath(pathToTool).ToUpper());
ITaskItem computedSource = new TaskItem(source.ItemSpec);
source.CopyMetadataTo(computedSource);
var outputs = GeneratedItems(source).Select((item) => item.GetMetadata("FullPath").ToUpper());
computedSource.SetMetadata("Outputs", string.Join(";", outputs));
computedSource.SetMetadata("Inputs", string.Join(";", inputs));
computed.Add(computedSource);
}
ComputedSources = computed.ToArray();
}
}
return status;
}
protected override string GenerateFullPathToTool()
{
string path = Path.Combine(IceToolsPath, ToolName);
if(!File.Exists(path))
{
Log.LogError(string.Format("Slice compiler `{0}' not found. Review Ice Home setting", path));
}
return path;
}
protected override void LogToolCommand(string message)
{
Log.LogMessage(MessageImportance.Low, message);
}
private bool UsageError
{
get;
set;
}
protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
if(UsageError)
{
return;
}
int i = singleLine.IndexOf(string.Format("{0}:", ToolName));
if(i != -1)
{
i += (ToolName.Length + 1);
Log.LogError("", "", "", "", 0, 0, 0, 0,
string.Format("{0}: {1}", Path.GetFileName(ToolName), singleLine.Substring(i)));
UsageError = true;
}
else
{
string s = singleLine.Trim();
if(s.StartsWith(WorkingDirectory))
{
s = s.Substring(WorkingDirectory.Length);
}
string file = "";
int line = 0;
string description = "";
//
// Skip the drive letter
//
i = s.IndexOf(":");
if(i <= 1 && s.Length > i + 1)
{
i = s.IndexOf(":", i + 1);
}
if(i != -1)
{
file = Path.GetFullPath(s.Substring(0, i).Trim().Trim('"'));
if(file.IndexOf(WorkingDirectory) != -1)
{
file = file.Substring(WorkingDirectory.Length)
.Trim(Path.DirectorySeparatorChar);
}
if(s.Length > i + 1)
{
s = s.Substring(i + 1);
i = s.IndexOf(":");
if(i != -1)
{
if(int.TryParse(s.Substring(0, i), out line))
{
if(s.Length > i + 1)
{
s = s.Substring(i + 1);
}
}
else
{
s = s.Substring(i);
}
}
description = s.Trim();
description += Environment.NewLine;
}
}
if(description.IndexOf("warning:") == 0)
{
Log.LogWarning("", "", "", file, line - 1, 0, 0, 0, description.Substring("warning:".Length));
}
else if(description.IndexOf("error:") == 0)
{
Log.LogError("", "", "", file, line - 1, 0, 0, 0, description.Substring("error:".Length));
}
else if(!string.IsNullOrEmpty(description))
{
Log.LogError("", "", "", file, line - 1, 0, 0, 0, description);
}
}
}
}
public class Slice2CppTask : SliceCompilerTask
{
public Slice2CppTask()
{
HeaderExt = "h";
SourceExt = "cpp";
}
protected override string ToolName
{
get
{
return "slice2cpp.exe";
}
}
public string HeaderOutputDir
{
get;
set;
}
[Required]
public string HeaderExt
{
get;
set;
}
[Required]
public string SourceExt
{
get;
set;
}
public string BaseDirectoryForGeneratedInclude
{
get;
set;
}
protected override string GeneratedExtensions
{
get
{
return string.Format("{0},{1}", HeaderExt, SourceExt);
}
}
protected override ITaskItem[] GeneratedItems(ITaskItem source)
{
return new ITaskItem[]
{
new TaskItem(GetGeneratedPath(source, OutputDir, SourceExt)),
new TaskItem(GetGeneratedPath(source,
string.IsNullOrEmpty(HeaderOutputDir) ? OutputDir : HeaderOutputDir,
HeaderExt)),
};
}
protected override string GenerateCommandLineCommands()
{
CommandLineBuilder builder = new CommandLineBuilder(false);
builder.AppendSwitch("--header-ext");
builder.AppendFileNameIfNotNull(HeaderExt);
builder.AppendSwitch("--source-ext");
builder.AppendFileNameIfNotNull(SourceExt);
if(!string.IsNullOrEmpty(BaseDirectoryForGeneratedInclude))
{
builder.AppendSwitch("--include-dir");
builder.AppendFileNameIfNotNull(BaseDirectoryForGeneratedInclude);
}
builder.AppendTextUnquoted(" ");
builder.AppendTextUnquoted(base.GenerateCommandLineCommands());
return builder.ToString();
}
protected override Dictionary<string, string> GetOptions()
{
var options = base.GetOptions();
if(!string.IsNullOrEmpty(HeaderOutputDir))
{
options["HeaderOutputDir"] = HeaderOutputDir;
}
if(!string.IsNullOrEmpty(BaseDirectoryForGeneratedInclude))
{
options["BaseDirectoryForGeneratedInclude"] = BaseDirectoryForGeneratedInclude;
}
options["SourceExt"] = SourceExt;
options["HeaderExt"] = HeaderExt;
return options;
}
protected override void TraceGenerated()
{
var headerOutputDir = string.IsNullOrEmpty(HeaderOutputDir) ? OutputDir : HeaderOutputDir;
foreach(ITaskItem source in Sources)
{
var cppSource = GetGeneratedPath(source, OutputDir, SourceExt);
var cppHeader = GetGeneratedPath(source, headerOutputDir, HeaderExt);
Log.LogMessage(MessageImportance.High,
string.Format("Compiling {0} Generating -> {1} and {2}",
source.GetMetadata("Identity"),
TaskUtil.MakeRelative(WorkingDirectory, cppSource),
TaskUtil.MakeRelative(WorkingDirectory, cppHeader)));
}
}
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
int status = base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
if(status == 0)
{
//
// If HeaderOutputDir is set move the generated header to its final location
//
if(!string.IsNullOrEmpty(HeaderOutputDir))
{
foreach(ITaskItem source in Sources)
{
string sourceH = GetGeneratedPath(source, OutputDir, HeaderExt);
string targetH = GetGeneratedPath(source, HeaderOutputDir, HeaderExt);
if(File.Exists(targetH))
{
File.Delete(targetH);
}
File.Move(sourceH, targetH);
}
}
}
return status;
}
}
public class Slice2CSharpTask : SliceCompilerTask
{
protected override string ToolName
{
get
{
return TaskUtil.isWindows ? "slice2cs.exe" : "slice2cs";
}
}
protected override string GeneratedExtensions
{
get
{
return "cs";
}
}
protected override void TraceGenerated()
{
foreach(ITaskItem source in Sources)
{
string message = string.Format("Compiling {0} Generating -> ", source.GetMetadata("Identity"));
message += TaskUtil.MakeRelative(WorkingDirectory, GetGeneratedPath(source, OutputDir, ".cs"));
Log.LogMessage(MessageImportance.High, message);
}
}
protected override ITaskItem[] GeneratedItems(ITaskItem source)
{
return new ITaskItem[]
{
new TaskItem(GetGeneratedPath(source, OutputDir, ".cs"))
};
}
}
public abstract class SliceDependTask : Task
{
[Required]
public ITaskItem[] Sources
{
get;
set;
}
[Required]
public string IceHome
{
get;
set;
}
[Required]
public string IceToolsPath
{
get;
set;
}
[Required]
public string WorkingDirectory
{
get;
set;
}
[Output]
public ITaskItem[] ComputedSources
{
get;
private set;
}
[Output]
public string[] GeneratedCompiledPaths
{
get;
private set;
}
protected abstract string ToolName
{
get;
}
abstract protected ITaskItem[] GeneratedItems(ITaskItem source);
// Same as generated items but only returns the generated items that need to be compiled
// for example it excludes C++ headers
protected abstract ITaskItem[] GeneratedCompiledItems(ITaskItem source);
protected virtual string GetGeneratedPath(ITaskItem item, string outputDir, string ext)
{
return Path.Combine(outputDir,
Path.GetFileName(Path.ChangeExtension(item.GetMetadata("Identity"), ext)));
}
public virtual Dictionary<string, string> GetOptions(ITaskItem item)
{
var options = new Dictionary<string, string>();
options["IceHome"] = IceHome;
options["IceToolsPath"] = IceToolsPath;
options["OutputDir"] = item.GetMetadata("OutputDir").TrimEnd('\\');
var value = item.GetMetadata("IncludeDirectories");
if(!string.IsNullOrEmpty(value))
{
value = value.Trim(';');
if(!string.IsNullOrEmpty(value))
{
options["IncludeDirectories"] = value;
}
}
value = item.GetMetadata("AdditionalOptions");
if(!string.IsNullOrEmpty(value))
{
value = value.Trim(';');
if(!string.IsNullOrEmpty(value))
{
options["AdditionalOptions"] = value;
}
}
return options;
}
public override bool Execute()
{
List<ITaskItem> computed = new List<ITaskItem>();
List<string> generatedCompiledPaths = new List<string>();
foreach(ITaskItem source in Sources)
{
bool skip = true;
Log.LogMessage(MessageImportance.Low,
string.Format("Computing dependencies for {0}", source.GetMetadata("Identity")));
var sourceInfo = new FileInfo(source.GetMetadata("FullPath"));
if(!sourceInfo.Exists)
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because source: {0} doesn't exists",
source.GetMetadata("Identity")));
skip = false;
}
var generatedItems = GeneratedItems(source);
FileInfo generatedInfo = null;
FileInfo dependInfo = null;
generatedCompiledPaths.AddRange(
GeneratedCompiledItems(source).Select(item => item.GetMetadata("FullPath")));
//
// Check if the Slice compiler is older than the source file
//
var sliceCompiler = new FileInfo(Path.Combine(IceToolsPath, ToolName));
if(skip)
{
foreach(ITaskItem item in generatedItems)
{
generatedInfo = new FileInfo(item.GetMetadata("FullPath"));
if(generatedInfo.Exists &&
sliceCompiler.LastWriteTime.ToFileTime() > generatedInfo.LastWriteTime.ToFileTime())
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because target: {0} is older than Slice compiler: {1}",
TaskUtil.MakeRelative(WorkingDirectory, generatedInfo.FullName),
ToolName));
skip = false;
break;
}
}
}
XmlDocument dependsDoc = new XmlDocument();
if(skip)
{
dependInfo = new FileInfo(Path.Combine(WorkingDirectory, source.GetMetadata("OutputDir"),
string.Format("SliceCompile.{0}.d", Path.GetFileNameWithoutExtension(sourceInfo.Name))));
//
// Check that the depdend file exists
//
if(!dependInfo.Exists)
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because depend file: {0} doesn't exists",
TaskUtil.MakeRelative(WorkingDirectory, dependInfo.FullName)));
skip = false;
}
//
// Check that the depend file is older than the corresponding Slice source
//
else if(sourceInfo.LastWriteTime.ToFileTime() > dependInfo.LastWriteTime.ToFileTime())
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because source: {0} is older than depend file {1}",
source.GetMetadata("Identity"),
TaskUtil.MakeRelative(WorkingDirectory, dependInfo.FullName)));
skip = false;
}
else
{
try
{
dependsDoc.Load(dependInfo.FullName);
}
catch(XmlException)
{
try
{
File.Delete(dependInfo.FullName);
}
catch(IOException)
{
}
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because depend file: {0} has some invalid data",
TaskUtil.MakeRelative(WorkingDirectory, dependInfo.FullName)));
skip = false;
}
}
}
if(skip)
{
foreach(ITaskItem item in generatedItems)
{
generatedInfo = new FileInfo(item.GetMetadata("FullPath"));
//
// Check that the generated file exists
//
if(!generatedInfo.Exists)
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because generated: {0} doesn't exists",
TaskUtil.MakeRelative(WorkingDirectory, generatedInfo.FullName)));
skip = false;
break;
}
//
// Check that the generated file is older than the corresponding Slice source
//
else if(sourceInfo.LastWriteTime.ToFileTime() > generatedInfo.LastWriteTime.ToFileTime())
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because source: {0} is older than target {1}",
source.GetMetadata("Identity"),
TaskUtil.MakeRelative(WorkingDirectory, generatedInfo.FullName)));
skip = false;
break;
}
}
}
if(skip)
{
XmlNodeList options = dependsDoc.DocumentElement.SelectNodes(
string.Format("/dependencies/source[@name='{0}']/options/child::node()",
source.GetMetadata("Identity")));
if(options != null)
{
var newOptions = GetOptions(source);
var oldOptions = options.Cast<XmlNode>().Select(node => new
{
node.Name,
node.InnerXml
}).ToDictionary(t => t.Name, t => t.InnerXml);
if(newOptions.Except(oldOptions).Any() || oldOptions.Except(newOptions).Any())
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because source: {0} build options change",
source.GetMetadata("Identity")));
skip = false;
}
}
}
if(skip)
{
XmlNodeList depends = dependsDoc.DocumentElement.SelectNodes(
string.Format("/dependencies/source[@name='{0}']/dependsOn", source.GetMetadata("Identity")));
if(depends != null)
{
var inputs = new List<string>();
foreach(XmlNode depend in depends)
{
string path = depend.Attributes["name"].Value;
FileInfo dependencyInfo = new FileInfo(path);
if(!dependencyInfo.Exists)
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because dependency: {0} doesn't exists",
TaskUtil.MakeRelative(WorkingDirectory, dependencyInfo.FullName)));
skip = false;
break;
}
else if(dependencyInfo.LastWriteTime > generatedInfo.LastWriteTime)
{
Log.LogMessage(MessageImportance.Low,
string.Format("Build required because source: {0} is older than target: {1}",
source.GetMetadata("Identity"),
TaskUtil.MakeRelative(WorkingDirectory, dependencyInfo.FullName)));
skip = false;
break;
}
inputs.Add(Path.GetFullPath(depend.Attributes["name"].Value).ToUpper());
}
inputs.Add(source.GetMetadata("FullPath").ToUpper());
inputs.Add(sliceCompiler.FullName.ToUpper());
var outputs = GeneratedItems(source).Select(item => item.GetMetadata("FullPath").ToUpper());
source.SetMetadata("Outputs", string.Join(";", outputs));
source.SetMetadata("Inputs", string.Join(";", inputs));
}
}
if(skip)
{
string message = string.Format("Skipping {0} -> ", source.GetMetadata("Identity"));
message += generatedItems[0].GetMetadata("Identity");
if(generatedItems.Length > 1)
{
message += " and ";
message += generatedItems[1].GetMetadata("Identity");
message += " are ";
}
else
{
message += " is ";
}
message += "up to date";
Log.LogMessage(MessageImportance.Normal, message);
}
ITaskItem computedSource = new TaskItem(source.ItemSpec);
source.CopyMetadataTo(computedSource);
computedSource.SetMetadata("BuildRequired", skip ? "False" : "True");
computedSource.SetMetadata("OutputDir", computedSource.GetMetadata("OutputDir").TrimEnd('\\'));
computed.Add(computedSource);
}
ComputedSources = computed.ToArray();
GeneratedCompiledPaths = generatedCompiledPaths.ToArray();
return true;
}
}
public class Slice2CppDependTask : SliceDependTask
{
protected override string ToolName
{
get
{
return "slice2cpp.exe";
}
}
public override Dictionary<string, string> GetOptions(ITaskItem item)
{
var options = base.GetOptions(item);
var value = item.GetMetadata("HeaderOutputDir");
if(!string.IsNullOrEmpty(value))
{
options["HeaderOutputDir"] = value;
}
value = item.GetMetadata("BaseDirectoryForGeneratedInclude");
if(!string.IsNullOrEmpty(value))
{
options["BaseDirectoryForGeneratedInclude"] = value;
}
value = item.GetMetadata("HeaderExt");
if(!string.IsNullOrEmpty(value))
{
options["HeaderExt"] = value;
}
value = item.GetMetadata("SourceExt");
if(!string.IsNullOrEmpty(value))
{
options["SourceExt"] = value;
}
return options;
}
protected override ITaskItem[] GeneratedItems(ITaskItem source)
{
var outputDir = source.GetMetadata("OutputDir");
var headerOutputDir = source.GetMetadata("HeaderOutputDir");
if(string.IsNullOrEmpty(headerOutputDir))
{
headerOutputDir = outputDir;
}
var sourceExt = source.GetMetadata("SourceExt");
var headerExt = source.GetMetadata("HeaderExt");
return new ITaskItem[]
{
new TaskItem(GetGeneratedPath(source, outputDir, sourceExt)),
new TaskItem(GetGeneratedPath(source, headerOutputDir, headerExt))
};
}
// Same as generated items but only returns the generated items that need to be compiled
// for example it excludes C++ headers