Merge branch 'patch-1' of https://github.com/ReubenBond/mono into ReubenBond-patch-1
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / Csc.cs
1 //
2 // Csc.cs: Task that runs C# compiler
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 using System;
29 using System.IO;
30 using Microsoft.Build.Framework;
31 using Microsoft.Build.Tasks.Hosting;
32 using Microsoft.Build.Utilities;
33 using Mono.XBuild.Utilities;
34
35 namespace Microsoft.Build.Tasks {
36         public class Csc : ManagedCompiler {
37         
38                 public Csc ()
39                 {
40                 }
41
42                 protected internal override void AddResponseFileCommands (CommandLineBuilderExtension commandLine)
43                 {
44 #if !NET_4_0
45                         //pre-MSBuild 2 targets don't support multi-targeting, so tell compiler to use 2.0 corlib
46                         commandLine.AppendSwitch ("/sdk:2");
47 #endif
48                         base.AddResponseFileCommands (commandLine);
49
50                         if (AdditionalLibPaths != null && AdditionalLibPaths.Length > 0)
51                                 commandLine.AppendSwitchIfNotNull ("/lib:", AdditionalLibPaths, ",");
52
53                         if (Bag ["AllowUnsafeBlocks"] != null)
54                                 if (AllowUnsafeBlocks)
55                                         commandLine.AppendSwitch ("/unsafe+");
56                                 else
57                                         commandLine.AppendSwitch ("/unsafe-");
58
59                         //baseAddress
60                         
61                         if (Bag ["CheckForOverflowUnderflow"] != null)
62                                 if (CheckForOverflowUnderflow)
63                                         commandLine.AppendSwitch ("/checked+");
64                                 else
65                                         commandLine.AppendSwitch ("/checked-");
66
67                         if (!String.IsNullOrEmpty (DefineConstants)) {
68                                 string [] defines = DefineConstants.Split (new char [] {';', ' '},
69                                                 StringSplitOptions.RemoveEmptyEntries);
70                                 if (defines.Length > 0)
71                                         commandLine.AppendSwitchIfNotNull ("/define:",
72                                                         String.Join (";", defines));
73                         }
74
75                         if (!String.IsNullOrEmpty (DisabledWarnings)) {
76                                 string [] defines = DisabledWarnings.Split (new char [] {';', ' ', ','},
77                                                 StringSplitOptions.RemoveEmptyEntries);
78                                 if (defines.Length > 0)
79                                     commandLine.AppendSwitchIfNotNull ("/nowarn:", defines, ";");
80                         }
81
82                         commandLine.AppendSwitchIfNotNull ("/doc:", DocumentationFile);
83
84                         //errorReport
85
86                         if (GenerateFullPaths)
87                                 commandLine.AppendSwitch ("/fullpaths");
88
89                         commandLine.AppendSwitchIfNotNull ("/langversion:", LangVersion);
90
91                         commandLine.AppendSwitchIfNotNull ("/main:", MainEntryPoint);
92
93                         //moduleAssemblyName
94                         
95                         if (NoStandardLib)
96                                 commandLine.AppendSwitch ("/nostdlib");
97
98                         //platform
99                         commandLine.AppendSwitchIfNotNull ("/platform:", Platform);
100                         //
101                         if (References != null)
102                                 foreach (ITaskItem item in References) {
103                                         string aliases = item.GetMetadata ("Aliases");
104                                         if (!string.IsNullOrEmpty (aliases)) {
105                                                 AddAliasesReference (commandLine, aliases, item.ItemSpec);
106                                         } else {
107                                                 commandLine.AppendSwitchIfNotNull ("/reference:", item.ItemSpec);
108                                         }
109                                 }
110
111                         if (ResponseFiles != null)
112                                 foreach (ITaskItem item in ResponseFiles) 
113                                         commandLine.AppendSwitchIfNotNull ("@", item.ItemSpec);
114
115                         if (Bag ["WarningLevel"] != null)
116                                 commandLine.AppendSwitchIfNotNull ("/warn:", WarningLevel.ToString ());
117
118                         commandLine.AppendSwitchIfNotNull ("/warnaserror+:", WarningsAsErrors);
119
120                         commandLine.AppendSwitchIfNotNull ("/warnaserror-:", WarningsNotAsErrors);
121
122                         if (Win32Resource != null)
123                                 commandLine.AppendSwitchIfNotNull ("/win32res:", Win32Resource);
124                 }
125
126                 static void AddAliasesReference (CommandLineBuilderExtension commandLine, string aliases, string reference)
127                 {
128                         foreach (var alias in aliases.Split (',')) {
129                                 var a = alias.Trim ();
130                                 if (a.Length == null)
131                                         continue;
132
133                                 var r = "/reference:";
134                                 if (!string.Equals (a, "global", StringComparison.OrdinalIgnoreCase))
135                                         r += a + "=";
136
137                                 commandLine.AppendSwitchIfNotNull (r, reference);
138                         }
139                 }
140
141                 [MonoTODO]
142                 protected override bool CallHostObjectToExecute ()
143                 {
144                         throw new NotImplementedException ();
145                 }
146
147                 protected override string GenerateFullPathToTool ()
148                 {
149                         if (!string.IsNullOrEmpty (ToolPath))
150                                 return Path.Combine (ToolPath, ToolExe);
151                         return ToolLocationHelper.GetPathToDotNetFrameworkFile (ToolExe, TargetDotNetFrameworkVersion.VersionLatest);
152                 }
153
154                 [MonoTODO]
155                 protected override HostObjectInitializationStatus InitializeHostObject ()
156                 {
157                         return HostObjectInitializationStatus.NoActionReturnSuccess;
158                 }
159
160                 public bool AllowUnsafeBlocks {
161                         get { return GetBoolParameterWithDefault ("AllowUnsafeBlocks", false); }
162                         set { Bag ["AllowUnsafeBlocks"] = value; }
163                 }
164
165                 public string BaseAddress {
166                         get { return (string) Bag ["BaseAddress"]; }
167                         set { Bag ["BaseAddress"] = value; }
168                 }
169
170                 public bool CheckForOverflowUnderflow {
171                         get { return GetBoolParameterWithDefault ("CheckForOverflowUnderflow", false); }
172                         set { Bag ["CheckForOverflowUnderflow"] = value; }
173                 }
174
175                 public string DisabledWarnings {
176                         get { return (string) Bag ["DisabledWarnings"]; }
177                         set { Bag ["DisabledWarnings"] = value; }
178                 }
179
180                 public string DocumentationFile {
181                         get { return (string) Bag ["DocumentationFile"]; }
182                         set { Bag ["DocumentationFile"] = value; }
183                 }
184
185                 public string ErrorReport {
186                         get { return (string) Bag ["ErrorReport"]; }
187                         set { Bag ["ErrorReport"] = value; }
188                 }
189
190                 public bool GenerateFullPaths {
191                         get { return GetBoolParameterWithDefault ("GenerateFullPaths", false); }
192                         set { Bag ["GenerateFullPaths"] = value; }
193                 }
194
195                 public string LangVersion {
196                         get { return (string) Bag ["LangVersion"]; }
197                         set { Bag ["LangVersion"] = value; }
198                 }
199
200                 public string ModuleAssemblyName {
201                         get { return (string) Bag ["ModuleAssemblyName"]; }
202                         set { Bag ["ModuleAssemblyName"] = value; }
203                 }
204
205                 public bool NoStandardLib {
206                         get { return GetBoolParameterWithDefault ("NoStandardLib", false); }
207                         set { Bag ["NoStandardLib"] = value; }
208                 }
209                 
210                 public string PdbFile {
211                         get { return (string) Bag ["PdbFile"]; }
212                         set { Bag ["PdbFile"] = value; }
213                 }
214
215                 public string Platform {
216                         get { return (string) Bag ["Platform"]; }
217                         set { Bag ["Platform"] = value; }
218                 }
219
220                 protected override string ToolName {
221                         get {
222                                 return "mcs.exe";
223                         }
224                 }
225
226                 public bool UseHostCompilerIfAvailable {
227                         get { return GetBoolParameterWithDefault ("UseHostCompilerIfAvailable", false); }
228                         set { Bag ["UseHostCompilerIfAvailable"] = value; }
229                 }
230
231                 public int WarningLevel {
232                         get { return GetIntParameterWithDefault ("WarningLevel", 4); }
233                         set { Bag ["WarningLevel"] = value; }
234                 }
235
236                 public string WarningsAsErrors {
237                         get { return (string) Bag ["WarningsAsErrors"]; }
238                         set { Bag ["WarningsAsErrors"] = value; }
239                 }
240
241                 public string WarningsNotAsErrors {
242                         get { return (string) Bag ["WarningsNotAsErrors"]; }
243                         set { Bag ["WarningsNotAsErrors"] = value; }
244                 }
245         }
246 }
247