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