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