2 // Csc.cs: Task that runs C# compiler
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks.Hosting;
34 using Microsoft.Build.Utilities;
35 using Mono.XBuild.Utilities;
37 namespace Microsoft.Build.Tasks {
38 public class Csc : ManagedCompiler {
44 protected internal override void AddResponseFileCommands (CommandLineBuilderExtension commandLine)
47 //pre-MSBuild 2 targets don't support multi-targeting, so tell compiler to use 2.0 corlib
48 commandLine.AppendSwitch ("/sdk:2");
50 base.AddResponseFileCommands (commandLine);
52 if (AdditionalLibPaths != null && AdditionalLibPaths.Length > 0)
53 commandLine.AppendSwitchIfNotNull ("/lib:", AdditionalLibPaths, ",");
55 if (Bag ["AllowUnsafeBlocks"] != null)
56 if (AllowUnsafeBlocks)
57 commandLine.AppendSwitch ("/unsafe+");
59 commandLine.AppendSwitch ("/unsafe-");
63 if (Bag ["CheckForOverflowUnderflow"] != null)
64 if (CheckForOverflowUnderflow)
65 commandLine.AppendSwitch ("/checked+");
67 commandLine.AppendSwitch ("/checked-");
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));
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, ";");
84 commandLine.AppendSwitchIfNotNull ("/doc:", DocumentationFile);
88 if (GenerateFullPaths)
89 commandLine.AppendSwitch ("/fullpaths");
91 commandLine.AppendSwitchIfNotNull ("/langversion:", LangVersion);
93 commandLine.AppendSwitchIfNotNull ("/main:", MainEntryPoint);
98 commandLine.AppendSwitch ("/nostdlib");
101 commandLine.AppendSwitchIfNotNull ("/platform:", Platform);
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);
110 commandLine.AppendSwitchIfNotNull ("/reference:", item.ItemSpec);
113 if (ResponseFiles != null)
114 foreach (ITaskItem item in ResponseFiles)
115 commandLine.AppendSwitchIfNotNull ("@", item.ItemSpec);
117 if (Bag ["WarningLevel"] != null)
118 commandLine.AppendSwitchIfNotNull ("/warn:", WarningLevel.ToString ());
120 commandLine.AppendSwitchIfNotNull ("/warnaserror+:", WarningsAsErrors);
122 commandLine.AppendSwitchIfNotNull ("/warnaserror-:", WarningsNotAsErrors);
124 if (Win32Resource != null)
125 commandLine.AppendSwitchIfNotNull ("/win32res:", Win32Resource);
129 protected override bool CallHostObjectToExecute ()
131 throw new NotImplementedException ();
134 protected override string GenerateFullPathToTool ()
136 if (!string.IsNullOrEmpty (ToolPath))
137 return Path.Combine (ToolPath, ToolExe);
138 return ToolLocationHelper.GetPathToDotNetFrameworkFile (ToolExe, TargetDotNetFrameworkVersion.VersionLatest);
142 protected override HostObjectInitializationStatus InitializeHostObject ()
144 return HostObjectInitializationStatus.NoActionReturnSuccess;
147 public bool AllowUnsafeBlocks {
148 get { return GetBoolParameterWithDefault ("AllowUnsafeBlocks", false); }
149 set { Bag ["AllowUnsafeBlocks"] = value; }
152 public string BaseAddress {
153 get { return (string) Bag ["BaseAddress"]; }
154 set { Bag ["BaseAddress"] = value; }
157 public bool CheckForOverflowUnderflow {
158 get { return GetBoolParameterWithDefault ("CheckForOverflowUnderflow", false); }
159 set { Bag ["CheckForOverflowUnderflow"] = value; }
162 public string DisabledWarnings {
163 get { return (string) Bag ["DisabledWarnings"]; }
164 set { Bag ["DisabledWarnings"] = value; }
167 public string DocumentationFile {
168 get { return (string) Bag ["DocumentationFile"]; }
169 set { Bag ["DocumentationFile"] = value; }
172 public string ErrorReport {
173 get { return (string) Bag ["ErrorReport"]; }
174 set { Bag ["ErrorReport"] = value; }
177 public bool GenerateFullPaths {
178 get { return GetBoolParameterWithDefault ("GenerateFullPaths", false); }
179 set { Bag ["GenerateFullPaths"] = value; }
182 public string LangVersion {
183 get { return (string) Bag ["LangVersion"]; }
184 set { Bag ["LangVersion"] = value; }
187 public string ModuleAssemblyName {
188 get { return (string) Bag ["ModuleAssemblyName"]; }
189 set { Bag ["ModuleAssemblyName"] = value; }
192 public bool NoStandardLib {
193 get { return GetBoolParameterWithDefault ("NoStandardLib", false); }
194 set { Bag ["NoStandardLib"] = value; }
197 public string PdbFile {
198 get { return (string) Bag ["PdbFile"]; }
199 set { Bag ["PdbFile"] = value; }
202 public string Platform {
203 get { return (string) Bag ["Platform"]; }
204 set { Bag ["Platform"] = value; }
207 protected override string ToolName {
213 public bool UseHostCompilerIfAvailable {
214 get { return GetBoolParameterWithDefault ("UseHostCompilerIfAvailable", false); }
215 set { Bag ["UseHostCompilerIfAvailable"] = value; }
218 public int WarningLevel {
219 get { return GetIntParameterWithDefault ("WarningLevel", 4); }
220 set { Bag ["WarningLevel"] = value; }
223 public string WarningsAsErrors {
224 get { return (string) Bag ["WarningsAsErrors"]; }
225 set { Bag ["WarningsAsErrors"] = value; }
228 public string WarningsNotAsErrors {
229 get { return (string) Bag ["WarningsNotAsErrors"]; }
230 set { Bag ["WarningsNotAsErrors"] = value; }