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