461f2f6c648c641961b33bde4cb3fdc0ea8a6d4a
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / ManagedCompiler.cs
1 //
2 // ManagedCompiler.cs: Task for compilers
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 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.IO;
34 using System.Text;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.Tasks {
39         public abstract class ManagedCompiler : ToolTaskExtension {
40         
41                 protected ManagedCompiler ()
42                 {
43                 }
44
45                 [MonoTODO]
46                 protected internal override void AddCommandLineCommands (
47                                                  CommandLineBuilderExtension commandLine)
48                 {
49                         if (NoConfig)
50                                 commandLine.AppendSwitch ("/noconfig");
51                 }
52
53                 [MonoTODO]
54                 protected internal override void AddResponseFileCommands (
55                                                  CommandLineBuilderExtension commandLine)
56                 {
57                         if (AddModules != null && AddModules.Length > 0)
58                                 commandLine.AppendSwitchIfNotNull ("/addmodule:", AddModules, ",");
59                         if (Bag ["CodePage"] != null)
60                                 commandLine.AppendSwitchIfNotNull ("/codepage:", CodePage.ToString ());
61
62                         commandLine.AppendSwitchIfNotNull ("/debug:", DebugType);
63
64                         if (Bag ["DelaySign"] != null)
65                                 if (DelaySign)
66                                         commandLine.AppendSwitch ("/delaysign+");
67                                 else
68                                         commandLine.AppendSwitch ("/delaysign-");
69                         if (Bag ["EmitDebugInformation"] != null)
70                                 if (EmitDebugInformation)
71                                         commandLine.AppendSwitch ("/debug+");
72                                 else
73                                         commandLine.AppendSwitch ("/debug-");
74                         //fileAlignment
75                         commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
76                         commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
77                         // FIXME: add ids from metadata
78                         if (LinkResources != null)
79                                 foreach (ITaskItem item in LinkResources)
80                                                 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
81                         
82                         if (NoLogo)
83                                 commandLine.AppendSwitch ("/nologo");
84
85                         if (Bag ["Optimize"] != null)
86                                 if (Optimize)
87                                         commandLine.AppendSwitch ("/optimize+");
88                                 else
89                                         commandLine.AppendSwitch ("/optimize-");
90
91                         if (OutputAssembly != null)
92                                 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
93                         
94                         if (Resources != null)
95                                 foreach (ITaskItem item in Resources) {
96                                         string logical_name = item.GetMetadata ("LogicalName");
97                                         if (logical_name.Length > 0)
98                                                 commandLine.AppendSwitchIfNotNull ("/resource:",
99                                                                 String.Format ("{0},{1}", item.ItemSpec, logical_name));
100                                         else
101                                                 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
102                                 }
103
104                         if (Sources != null)
105                                 foreach (ITaskItem item in Sources)
106                                                 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
107                         
108                         if (TargetType != null)
109                                 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
110                         if (Bag ["TreatWarningsAsErrors"] != null)
111                                 if (TreatWarningsAsErrors)
112                                         commandLine.AppendSwitch ("/warnaserror+");
113                                 else
114                                         commandLine.AppendSwitch ("/warnaserror-");
115                         commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
116                 }
117
118                 [MonoTODO]
119                 protected bool CheckAllReferencesExistOnDisk ()
120                 {
121                         if (Bag ["References"] != null)
122                                 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
123                                         if (!File.Exists (item.GetMetadata ("FullPath")))
124                                                 return false;
125                         return true;
126                 }
127
128                 [MonoTODO]
129                 protected void CheckHostObjectSupport (string parameterName,
130                                                        bool resultFromHostObjectSetOperation)
131                 {
132                 }
133                 
134                 [MonoTODO]
135                 protected override bool HandleTaskExecutionErrors ()
136                 {
137                         if (!Log.HasLoggedErrors && ExitCode != 0)
138                                 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
139
140                         return ExitCode == 0 && !Log.HasLoggedErrors;
141                 }
142                 
143                 [MonoTODO]
144                 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
145                                                         string parameterName)
146                 {
147                         Dictionary <string, object> items = new Dictionary <string, object> ();
148                         
149                         foreach (ITaskItem item in itemList) {
150                                 if (!items.ContainsKey (item.ItemSpec))
151                                         items.Add (item.ItemSpec, null);
152                                 else
153                                         return false;
154                         }
155                         
156                         return true;
157                 }
158
159                 [MonoTODO]
160                 protected override bool ValidateParameters ()
161                 {
162                         return true;
163                 }
164
165                 public string[] AdditionalLibPaths {
166                         get { return (string[]) Bag ["AdditionalLibPaths"]; }
167                         set { Bag ["AdditionalLibPaths"] = value; }
168                 }
169
170                 public string[] AddModules {
171                         get { return (string[]) Bag ["AddModules"]; }
172                         set { Bag ["AddModules"] = value; }
173                 }
174
175                 public int CodePage {
176                         get { return GetIntParameterWithDefault ("CodePage", 0); }
177                         set { Bag ["CodePage"] = value; }
178                 }
179
180                 public string DebugType {
181                         get { return (string) Bag ["DebugType"]; }
182                         set { Bag ["DebugType"] = value; }
183                 }
184
185                 public string DefineConstants {
186                         get { return (string) Bag ["DefineConstants"]; }
187                         set { Bag ["DefineConstants"] = value; }
188                 }
189
190                 public bool DelaySign {
191                         get { return GetBoolParameterWithDefault ("DelaySign", false); }
192                         set { Bag ["DelaySign"] = value; }
193                 }
194
195                 public bool EmitDebugInformation {
196                         get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
197                         set { Bag ["EmitDebugInformation"] = value; }
198                 }
199
200                 public int FileAlignment {
201                         get { return GetIntParameterWithDefault ("FileAlignment", 0); }
202                         set { Bag ["FileAlignment"] = value; }
203                 }
204
205                 protected bool HostCompilerSupportsAllParameters {
206                         get { return true; }
207                         set { Bag ["HostCompilerSupportsAllParameters"] = value; }
208                 }
209
210                 public string KeyContainer {
211                         get { return (string) Bag ["KeyContainer"]; }
212                         set { Bag ["KeyContainer"] = value; }
213                 }
214
215                 public string KeyFile {
216                         get { return (string) Bag ["KeyFile"]; }
217                         set { Bag ["KeyFile"] = value; }
218                 }
219
220                 public ITaskItem[] LinkResources {
221                         get { return (ITaskItem[]) Bag ["LinkResources"]; }
222                         set { Bag ["LinkResources"] = value; }
223                 }
224
225                 public string MainEntryPoint {
226                         get { return (string) Bag ["MainEntryPoint"]; }
227                         set { Bag ["MainEntryPoint"] = value; }
228                 }
229
230                 public bool NoConfig {
231                         get { return GetBoolParameterWithDefault ("NoConfig", false); }
232                         set { Bag ["NoConfig"] = value; }
233                 }
234
235                 public bool NoLogo {
236                         get { return GetBoolParameterWithDefault ("NoLogo", false); }
237                         set { Bag ["NoLogo"] = value; }
238                 }
239
240                 public bool Optimize {
241                         get { return GetBoolParameterWithDefault ("Optimize", false); }
242                         set { Bag ["Optimize"] = value; }
243                 }
244
245                 [Output]
246                 public ITaskItem OutputAssembly {
247                         get { return (ITaskItem) Bag ["OutputAssembly"]; }
248                         set { Bag ["OutputAssembly"] = value; }
249                 }
250
251                 public ITaskItem[] References {
252                         get { return (ITaskItem[]) Bag ["References"]; }
253                         set { Bag ["References"] = value; }
254                 }
255
256                 public ITaskItem[] Resources {
257                         get { return (ITaskItem[]) Bag ["Resources"]; }
258                         set { Bag ["Resources"] = value; }
259                 }
260
261                 public ITaskItem[] ResponseFiles {
262                         get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
263                         set { Bag ["ResponseFiles"] = value; }
264                 }
265
266                 public ITaskItem[] Sources {
267                         get { return (ITaskItem[]) Bag ["Sources"]; }
268                         set {
269                                 Bag ["Sources"] = value;
270                                 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
271                                         Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
272                         }
273                 }
274
275                 protected override Encoding StandardOutputEncoding {
276                         get { return Console.Error.Encoding; }
277                 }
278
279                 public string TargetType {
280                         get {
281                                 if (Bag.Contains ("TargetType")) {
282                                         string s = (string) Bag ["TargetType"];
283                                         return s.ToLowerInvariant ();
284                                 } else
285                                         return null;
286                         }
287                         set { Bag ["TargetType"] = value; }
288                 }
289
290                 public bool TreatWarningsAsErrors {
291                         get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
292                         set { Bag ["TreatWarningsAsErrors"] = value; }
293                 }
294
295                 public bool Utf8Output {
296                         get { return GetBoolParameterWithDefault ("Utf8Output", false); }
297                         set { Bag ["Utf8Output"] = value; }
298                 }
299
300                 public string Win32Icon {
301                         get { return (string) Bag ["Win32Icon"]; }
302                         set { Bag ["Win32Icon"] = value; }
303                 }
304
305                 public string Win32Resource {
306                         get { return (string) Bag ["Win32Resource"]; }
307                         set { Bag ["Win32Resource"] = value; }
308                 }
309         }
310 }
311
312 #endif