19eec8686542011b04d2515874b9a86450136161
[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
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
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                         if (AddModules != null && AddModules.Length > 0)
57                                 commandLine.AppendSwitchIfNotNull ("/addmodule:", AddModules, ",");
58                         if (Bag ["CodePage"] != null)
59                                 commandLine.AppendSwitchIfNotNull ("/codepage:", CodePage.ToString ());
60
61                         var dtype = DebugType;
62                         if (string.Equals (dtype, "full", StringComparison.OrdinalIgnoreCase) || string.Equals (dtype, "pdbonly", StringComparison.OrdinalIgnoreCase))
63                                 dtype = "portable";
64
65                         commandLine.AppendSwitchIfNotNull ("/debug:", dtype);
66
67                         if (Bag ["DelaySign"] != null)
68                                 if (DelaySign)
69                                         commandLine.AppendSwitch ("/delaysign+");
70                                 else
71                                         commandLine.AppendSwitch ("/delaysign-");
72                         if (Bag ["EmitDebugInformation"] != null)
73                                 if (EmitDebugInformation)
74                                         commandLine.AppendSwitch ("/debug:portable");
75                                 else
76                                         commandLine.AppendSwitch ("/debug-");
77                         //fileAlignment
78                         commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
79                         commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
80                         if (KeyFile != null && !DelaySign)
81                                 commandLine.AppendSwitch("/publicsign");
82
83                         // FIXME: add ids from metadata
84                         if (LinkResources != null)
85                                 foreach (ITaskItem item in LinkResources)
86                                                 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
87                         
88                         if (NoLogo)
89                                 commandLine.AppendSwitch ("/nologo");
90
91                         if (Bag ["Optimize"] != null)
92                                 if (Optimize)
93                                         commandLine.AppendSwitch ("/optimize+");
94                                 else
95                                         commandLine.AppendSwitch ("/optimize-");
96
97                         if (OutputAssembly != null)
98                                 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
99                         
100                         if (Resources != null)
101                                 foreach (ITaskItem item in Resources) {
102                                         string logical_name = item.GetMetadata ("LogicalName");
103                                         if (logical_name.Length > 0)
104                                                 commandLine.AppendSwitchIfNotNull ("/resource:",
105                                                                 String.Format ("{0},{1}", item.ItemSpec, logical_name));
106                                         else
107                                                 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
108                                 }
109
110                         if (Sources != null)
111                                 foreach (ITaskItem item in Sources)
112                                                 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
113                         
114                         if (TargetType != null)
115                                 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
116                         if (Bag ["TreatWarningsAsErrors"] != null)
117                                 if (TreatWarningsAsErrors)
118                                         commandLine.AppendSwitch ("/warnaserror+");
119                                 else
120                                         commandLine.AppendSwitch ("/warnaserror-");
121                         commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
122                 }
123
124                 [MonoTODO]
125                 protected bool CheckAllReferencesExistOnDisk ()
126                 {
127                         if (Bag ["References"] != null)
128                                 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
129                                         if (!File.Exists (item.GetMetadata ("FullPath")))
130                                                 return false;
131                         return true;
132                 }
133
134                 [MonoTODO]
135                 protected void CheckHostObjectSupport (string parameterName,
136                                                        bool resultFromHostObjectSetOperation)
137                 {
138                 }
139                 
140                 [MonoTODO]
141                 protected override bool HandleTaskExecutionErrors ()
142                 {
143                         if (!Log.HasLoggedErrors && ExitCode != 0)
144                                 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
145
146                         return ExitCode == 0 && !Log.HasLoggedErrors;
147                 }
148                 
149                 [MonoTODO]
150                 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
151                                                         string parameterName)
152                 {
153                         Dictionary <string, object> items = new Dictionary <string, object> ();
154                         
155                         foreach (ITaskItem item in itemList) {
156                                 if (!items.ContainsKey (item.ItemSpec))
157                                         items.Add (item.ItemSpec, null);
158                                 else
159                                         return false;
160                         }
161                         
162                         return true;
163                 }
164
165                 protected internal virtual bool UseAlternateCommandLineToolToExecute ()
166                 {
167                         //
168                         // If an alternate tool name or tool path was specified in the project file, then that tool is used 
169                         // rather than the host compiler for integrated development environment (IDE) builds.
170                         //
171                         return !string.IsNullOrEmpty (ToolPath) || ToolName != ToolExe;
172                 }
173
174                 [MonoTODO]
175                 protected override bool ValidateParameters ()
176                 {
177                         return true;
178                 }
179
180                 public string[] AdditionalLibPaths {
181                         get { return (string[]) Bag ["AdditionalLibPaths"]; }
182                         set { Bag ["AdditionalLibPaths"] = value; }
183                 }
184
185                 public string[] AddModules {
186                         get { return (string[]) Bag ["AddModules"]; }
187                         set { Bag ["AddModules"] = value; }
188                 }
189
190                 public int CodePage {
191                         get { return GetIntParameterWithDefault ("CodePage", 0); }
192                         set { Bag ["CodePage"] = value; }
193                 }
194
195                 public string DebugType {
196                         get { return (string) Bag ["DebugType"]; }
197                         set { Bag ["DebugType"] = value; }
198                 }
199
200                 public string DefineConstants {
201                         get { return (string) Bag ["DefineConstants"]; }
202                         set { Bag ["DefineConstants"] = value; }
203                 }
204
205                 public bool DelaySign {
206                         get { return GetBoolParameterWithDefault ("DelaySign", false); }
207                         set { Bag ["DelaySign"] = value; }
208                 }
209
210                 public bool EmitDebugInformation {
211                         get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
212                         set { Bag ["EmitDebugInformation"] = value; }
213                 }
214
215                 public int FileAlignment {
216                         get { return GetIntParameterWithDefault ("FileAlignment", 0); }
217                         set { Bag ["FileAlignment"] = value; }
218                 }
219
220                 protected bool HostCompilerSupportsAllParameters {
221                         get { return true; }
222                         set { Bag ["HostCompilerSupportsAllParameters"] = value; }
223                 }
224
225                 public string KeyContainer {
226                         get { return (string) Bag ["KeyContainer"]; }
227                         set { Bag ["KeyContainer"] = value; }
228                 }
229
230                 public string KeyFile {
231                         get { return (string) Bag ["KeyFile"]; }
232                         set { Bag ["KeyFile"] = value; }
233                 }
234
235                 public ITaskItem[] LinkResources {
236                         get { return (ITaskItem[]) Bag ["LinkResources"]; }
237                         set { Bag ["LinkResources"] = value; }
238                 }
239
240                 public string MainEntryPoint {
241                         get { return (string) Bag ["MainEntryPoint"]; }
242                         set { Bag ["MainEntryPoint"] = value; }
243                 }
244
245                 public bool NoConfig {
246                         get { return GetBoolParameterWithDefault ("NoConfig", false); }
247                         set { Bag ["NoConfig"] = value; }
248                 }
249
250                 public bool NoLogo {
251                         get { return GetBoolParameterWithDefault ("NoLogo", false); }
252                         set { Bag ["NoLogo"] = value; }
253                 }
254
255                 public bool Optimize {
256                         get { return GetBoolParameterWithDefault ("Optimize", false); }
257                         set { Bag ["Optimize"] = value; }
258                 }
259
260                 [Output]
261                 public ITaskItem OutputAssembly {
262                         get { return (ITaskItem) Bag ["OutputAssembly"]; }
263                         set { Bag ["OutputAssembly"] = value; }
264                 }
265
266                 public ITaskItem[] References {
267                         get { return (ITaskItem[]) Bag ["References"]; }
268                         set { Bag ["References"] = value; }
269                 }
270
271                 public ITaskItem[] Resources {
272                         get { return (ITaskItem[]) Bag ["Resources"]; }
273                         set { Bag ["Resources"] = value; }
274                 }
275
276                 public ITaskItem[] ResponseFiles {
277                         get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
278                         set { Bag ["ResponseFiles"] = value; }
279                 }
280
281                 public ITaskItem[] Sources {
282                         get { return (ITaskItem[]) Bag ["Sources"]; }
283                         set {
284                                 Bag ["Sources"] = value;
285                                 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
286                                         Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
287                         }
288                 }
289
290                 protected override Encoding StandardOutputEncoding {
291                         get { return Console.Error.Encoding; }
292                 }
293
294                 public string TargetType {
295                         get {
296                                 if (Bag.Contains ("TargetType")) {
297                                         string s = (string) Bag ["TargetType"];
298                                         return s.ToLowerInvariant ();
299                                 } else
300                                         return null;
301                         }
302                         set { Bag ["TargetType"] = value; }
303                 }
304
305                 public bool TreatWarningsAsErrors {
306                         get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
307                         set { Bag ["TreatWarningsAsErrors"] = value; }
308                 }
309
310                 public bool Utf8Output {
311                         get { return GetBoolParameterWithDefault ("Utf8Output", false); }
312                         set { Bag ["Utf8Output"] = value; }
313                 }
314
315                 public string Win32Icon {
316                         get { return (string) Bag ["Win32Icon"]; }
317                         set { Bag ["Win32Icon"] = value; }
318                 }
319
320                 public string Win32Resource {
321                         get { return (string) Bag ["Win32Resource"]; }
322                         set { Bag ["Win32Resource"] = value; }
323                 }
324         }
325 }
326