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