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