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