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