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