* Makefile: Build the make-map.exe in Mono.Unix.Native; add /nowarn:0618 to
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BatchingImpl.cs
1 //
2 // BatchingImpl.cs: Class that implements BatchingAlgorithm from the wiki.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.IO;
30 using System.Xml;
31
32 namespace Microsoft.Build.BuildEngine {
33         internal class BatchingImpl {
34         
35                 string          inputs;
36                 string          outputs;
37                 Project         project;
38         
39                 public BatchingImpl (Project project, XmlElement targetElement)
40                 {
41                         if (targetElement == null)
42                                 throw new ArgumentNullException ("targetElement");
43                         if (project == null)
44                                 throw new ArgumentNullException ("project");
45                 
46                         this.project = project;
47                         
48                         inputs = targetElement.GetAttribute ("Inputs");
49                         outputs = targetElement.GetAttribute ("Outputs");
50                 }
51                 
52                 public bool BuildNeeded ()
53                 {
54                         // FIXME: change this to ITaskItem instead of string
55                 
56                         Expression inputExpr, outputExpr;
57                         string[] inputFiles, outputFiles;
58                         DateTime oldestInput, youngestOutput;
59                 
60                         if (inputs == String.Empty) {
61                                 return true;
62                         }
63                         if (outputs == String.Empty) {
64                                 return true;
65                         }
66                         
67                         inputExpr = new Expression (project, inputs);
68                         outputExpr = new Expression (project, outputs);
69                         
70                         inputFiles = (string[]) inputExpr.ToArray (typeof (string[]));
71                         outputFiles = (string[]) outputExpr.ToArray (typeof (string[]));
72                         
73                         if (inputFiles == null) {
74                                 return true;
75                         }
76                         if (outputFiles == null) {
77                                 return true;
78                         }
79                         if (inputFiles.Length == 0) {
80                                 return true;
81                         }
82                         if (outputFiles.Length == 0) {
83                                 return true;
84                         }
85                         
86                         if (File.Exists (inputFiles [0])) {
87                                 oldestInput = File.GetLastWriteTime (inputFiles [0]);
88                         } else {
89                                 return true;
90                         }
91                         if (File.Exists (outputFiles [0])) {
92                                 youngestOutput = File.GetLastWriteTime (outputFiles [0]);
93                         } else {
94                                 return true;
95                         }
96                                 
97                         foreach (string file in inputFiles) {
98                                 if (file.Trim () == String.Empty)
99                                         continue;
100                         
101                                 if (File.Exists (file.Trim ())) {
102                                         if (File.GetLastWriteTime (file.Trim ()) > oldestInput)
103                                                 oldestInput = File.GetLastWriteTime (file.Trim ());
104                                 } else {
105                                         return true;
106                                 }
107                         }
108                         foreach (string file in outputFiles) {
109                                 if (file.Trim () == String.Empty)
110                                         continue;
111                         
112                                 if (File.Exists (file.Trim ())) {
113                                         if (File.GetLastWriteTime (file.Trim ()) < youngestOutput)
114                                                 youngestOutput = File.GetLastWriteTime (file.Trim ());
115                                 } else {
116                                         return true;
117                                 }
118                         }
119                         
120                         if (oldestInput > youngestOutput) {
121                                 return true;
122                         }
123                         else {
124                                 return false;
125                         }
126                 }
127                 
128                 public bool BatchTaskElement (TaskElement taskElement)
129                 {
130                         return taskElement.Execute ();
131                 }
132         }
133 }