2 // ManagedCompiler.cs: Task for compilers
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.
30 using System.Collections;
31 using System.Collections.Generic;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
37 namespace Microsoft.Build.Tasks {
38 public abstract class ManagedCompiler : ToolTaskExtension {
40 protected ManagedCompiler ()
45 protected internal override void AddCommandLineCommands (
46 CommandLineBuilderExtension commandLine)
49 commandLine.AppendSwitch ("/noconfig");
53 protected internal override void AddResponseFileCommands (
54 CommandLineBuilderExtension commandLine)
56 if (AddModules != null && AddModules.Length > 0)
57 commandLine.AppendSwitchIfNotNull ("/addmodule:", AddModules, ",");
58 if (Bag ["CodePage"] != null)
59 commandLine.AppendSwitchIfNotNull ("/codepage:", CodePage.ToString ());
61 commandLine.AppendSwitchIfNotNull ("/debug:", DebugType);
63 if (Bag ["DelaySign"] != null)
65 commandLine.AppendSwitch ("/delaysign+");
67 commandLine.AppendSwitch ("/delaysign-");
68 if (Bag ["EmitDebugInformation"] != null)
69 if (EmitDebugInformation)
70 commandLine.AppendSwitch ("/debug+");
72 commandLine.AppendSwitch ("/debug-");
74 commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
75 commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
76 // FIXME: add ids from metadata
77 if (LinkResources != null)
78 foreach (ITaskItem item in LinkResources)
79 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
82 commandLine.AppendSwitch ("/nologo");
84 if (Bag ["Optimize"] != null)
86 commandLine.AppendSwitch ("/optimize+");
88 commandLine.AppendSwitch ("/optimize-");
90 if (OutputAssembly != null)
91 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
93 if (Resources != null)
94 foreach (ITaskItem item in Resources) {
95 string logical_name = item.GetMetadata ("LogicalName");
96 if (logical_name.Length > 0)
97 commandLine.AppendSwitchIfNotNull ("/resource:",
98 String.Format ("{0},{1}", item.ItemSpec, logical_name));
100 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
104 foreach (ITaskItem item in Sources)
105 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
107 if (TargetType != null)
108 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
109 if (Bag ["TreatWarningsAsErrors"] != null)
110 if (TreatWarningsAsErrors)
111 commandLine.AppendSwitch ("/warnaserror+");
113 commandLine.AppendSwitch ("/warnaserror-");
114 commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
118 protected bool CheckAllReferencesExistOnDisk ()
120 if (Bag ["References"] != null)
121 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
122 if (!File.Exists (item.GetMetadata ("FullPath")))
128 protected void CheckHostObjectSupport (string parameterName,
129 bool resultFromHostObjectSetOperation)
134 protected override bool HandleTaskExecutionErrors ()
136 if (!Log.HasLoggedErrors && ExitCode != 0)
137 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
139 return ExitCode == 0 && !Log.HasLoggedErrors;
143 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
144 string parameterName)
146 Dictionary <string, object> items = new Dictionary <string, object> ();
148 foreach (ITaskItem item in itemList) {
149 if (!items.ContainsKey (item.ItemSpec))
150 items.Add (item.ItemSpec, null);
158 protected internal virtual bool UseAlternateCommandLineToolToExecute ()
161 // If an alternate tool name or tool path was specified in the project file, then that tool is used
162 // rather than the host compiler for integrated development environment (IDE) builds.
164 return !string.IsNullOrEmpty (ToolPath) || ToolName != ToolExe;
168 protected override bool ValidateParameters ()
173 public string[] AdditionalLibPaths {
174 get { return (string[]) Bag ["AdditionalLibPaths"]; }
175 set { Bag ["AdditionalLibPaths"] = value; }
178 public string[] AddModules {
179 get { return (string[]) Bag ["AddModules"]; }
180 set { Bag ["AddModules"] = value; }
183 public int CodePage {
184 get { return GetIntParameterWithDefault ("CodePage", 0); }
185 set { Bag ["CodePage"] = value; }
188 public string DebugType {
189 get { return (string) Bag ["DebugType"]; }
190 set { Bag ["DebugType"] = value; }
193 public string DefineConstants {
194 get { return (string) Bag ["DefineConstants"]; }
195 set { Bag ["DefineConstants"] = value; }
198 public bool DelaySign {
199 get { return GetBoolParameterWithDefault ("DelaySign", false); }
200 set { Bag ["DelaySign"] = value; }
203 public bool EmitDebugInformation {
204 get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
205 set { Bag ["EmitDebugInformation"] = value; }
208 public int FileAlignment {
209 get { return GetIntParameterWithDefault ("FileAlignment", 0); }
210 set { Bag ["FileAlignment"] = value; }
213 protected bool HostCompilerSupportsAllParameters {
215 set { Bag ["HostCompilerSupportsAllParameters"] = value; }
218 public string KeyContainer {
219 get { return (string) Bag ["KeyContainer"]; }
220 set { Bag ["KeyContainer"] = value; }
223 public string KeyFile {
224 get { return (string) Bag ["KeyFile"]; }
225 set { Bag ["KeyFile"] = value; }
228 public ITaskItem[] LinkResources {
229 get { return (ITaskItem[]) Bag ["LinkResources"]; }
230 set { Bag ["LinkResources"] = value; }
233 public string MainEntryPoint {
234 get { return (string) Bag ["MainEntryPoint"]; }
235 set { Bag ["MainEntryPoint"] = value; }
238 public bool NoConfig {
239 get { return GetBoolParameterWithDefault ("NoConfig", false); }
240 set { Bag ["NoConfig"] = value; }
244 get { return GetBoolParameterWithDefault ("NoLogo", false); }
245 set { Bag ["NoLogo"] = value; }
248 public bool Optimize {
249 get { return GetBoolParameterWithDefault ("Optimize", false); }
250 set { Bag ["Optimize"] = value; }
254 public ITaskItem OutputAssembly {
255 get { return (ITaskItem) Bag ["OutputAssembly"]; }
256 set { Bag ["OutputAssembly"] = value; }
259 public ITaskItem[] References {
260 get { return (ITaskItem[]) Bag ["References"]; }
261 set { Bag ["References"] = value; }
264 public ITaskItem[] Resources {
265 get { return (ITaskItem[]) Bag ["Resources"]; }
266 set { Bag ["Resources"] = value; }
269 public ITaskItem[] ResponseFiles {
270 get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
271 set { Bag ["ResponseFiles"] = value; }
274 public ITaskItem[] Sources {
275 get { return (ITaskItem[]) Bag ["Sources"]; }
277 Bag ["Sources"] = value;
278 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
279 Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
283 protected override Encoding StandardOutputEncoding {
284 get { return Console.Error.Encoding; }
287 public string TargetType {
289 if (Bag.Contains ("TargetType")) {
290 string s = (string) Bag ["TargetType"];
291 return s.ToLowerInvariant ();
295 set { Bag ["TargetType"] = value; }
298 public bool TreatWarningsAsErrors {
299 get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
300 set { Bag ["TreatWarningsAsErrors"] = value; }
303 public bool Utf8Output {
304 get { return GetBoolParameterWithDefault ("Utf8Output", false); }
305 set { Bag ["Utf8Output"] = value; }
308 public string Win32Icon {
309 get { return (string) Bag ["Win32Icon"]; }
310 set { Bag ["Win32Icon"] = value; }
313 public string Win32Resource {
314 get { return (string) Bag ["Win32Resource"]; }
315 set { Bag ["Win32Resource"] = value; }