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 var dtype = DebugType;
62 if (string.Equals (dtype, "full", StringComparison.OrdinalIgnoreCase))
65 commandLine.AppendSwitchIfNotNull ("/debug:", dtype);
67 if (Bag ["DelaySign"] != null)
69 commandLine.AppendSwitch ("/delaysign+");
71 commandLine.AppendSwitch ("/delaysign-");
72 if (Bag ["EmitDebugInformation"] != null)
73 if (EmitDebugInformation)
74 commandLine.AppendSwitch ("/debug+");
76 commandLine.AppendSwitch ("/debug-");
78 commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
79 commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
80 // FIXME: add ids from metadata
81 if (LinkResources != null)
82 foreach (ITaskItem item in LinkResources)
83 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
86 commandLine.AppendSwitch ("/nologo");
88 if (Bag ["Optimize"] != null)
90 commandLine.AppendSwitch ("/optimize+");
92 commandLine.AppendSwitch ("/optimize-");
94 if (OutputAssembly != null)
95 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
97 if (Resources != null)
98 foreach (ITaskItem item in Resources) {
99 string logical_name = item.GetMetadata ("LogicalName");
100 if (logical_name.Length > 0)
101 commandLine.AppendSwitchIfNotNull ("/resource:",
102 String.Format ("{0},{1}", item.ItemSpec, logical_name));
104 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
108 foreach (ITaskItem item in Sources)
109 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
111 if (TargetType != null)
112 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
113 if (Bag ["TreatWarningsAsErrors"] != null)
114 if (TreatWarningsAsErrors)
115 commandLine.AppendSwitch ("/warnaserror+");
117 commandLine.AppendSwitch ("/warnaserror-");
118 commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
122 protected bool CheckAllReferencesExistOnDisk ()
124 if (Bag ["References"] != null)
125 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
126 if (!File.Exists (item.GetMetadata ("FullPath")))
132 protected void CheckHostObjectSupport (string parameterName,
133 bool resultFromHostObjectSetOperation)
138 protected override bool HandleTaskExecutionErrors ()
140 if (!Log.HasLoggedErrors && ExitCode != 0)
141 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
143 return ExitCode == 0 && !Log.HasLoggedErrors;
147 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
148 string parameterName)
150 Dictionary <string, object> items = new Dictionary <string, object> ();
152 foreach (ITaskItem item in itemList) {
153 if (!items.ContainsKey (item.ItemSpec))
154 items.Add (item.ItemSpec, null);
162 protected internal virtual bool UseAlternateCommandLineToolToExecute ()
165 // If an alternate tool name or tool path was specified in the project file, then that tool is used
166 // rather than the host compiler for integrated development environment (IDE) builds.
168 return !string.IsNullOrEmpty (ToolPath) || ToolName != ToolExe;
172 protected override bool ValidateParameters ()
177 public string[] AdditionalLibPaths {
178 get { return (string[]) Bag ["AdditionalLibPaths"]; }
179 set { Bag ["AdditionalLibPaths"] = value; }
182 public string[] AddModules {
183 get { return (string[]) Bag ["AddModules"]; }
184 set { Bag ["AddModules"] = value; }
187 public int CodePage {
188 get { return GetIntParameterWithDefault ("CodePage", 0); }
189 set { Bag ["CodePage"] = value; }
192 public string DebugType {
193 get { return (string) Bag ["DebugType"]; }
194 set { Bag ["DebugType"] = value; }
197 public string DefineConstants {
198 get { return (string) Bag ["DefineConstants"]; }
199 set { Bag ["DefineConstants"] = value; }
202 public bool DelaySign {
203 get { return GetBoolParameterWithDefault ("DelaySign", false); }
204 set { Bag ["DelaySign"] = value; }
207 public bool EmitDebugInformation {
208 get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
209 set { Bag ["EmitDebugInformation"] = value; }
212 public int FileAlignment {
213 get { return GetIntParameterWithDefault ("FileAlignment", 0); }
214 set { Bag ["FileAlignment"] = value; }
217 protected bool HostCompilerSupportsAllParameters {
219 set { Bag ["HostCompilerSupportsAllParameters"] = value; }
222 public string KeyContainer {
223 get { return (string) Bag ["KeyContainer"]; }
224 set { Bag ["KeyContainer"] = value; }
227 public string KeyFile {
228 get { return (string) Bag ["KeyFile"]; }
229 set { Bag ["KeyFile"] = value; }
232 public ITaskItem[] LinkResources {
233 get { return (ITaskItem[]) Bag ["LinkResources"]; }
234 set { Bag ["LinkResources"] = value; }
237 public string MainEntryPoint {
238 get { return (string) Bag ["MainEntryPoint"]; }
239 set { Bag ["MainEntryPoint"] = value; }
242 public bool NoConfig {
243 get { return GetBoolParameterWithDefault ("NoConfig", false); }
244 set { Bag ["NoConfig"] = value; }
248 get { return GetBoolParameterWithDefault ("NoLogo", false); }
249 set { Bag ["NoLogo"] = value; }
252 public bool Optimize {
253 get { return GetBoolParameterWithDefault ("Optimize", false); }
254 set { Bag ["Optimize"] = value; }
258 public ITaskItem OutputAssembly {
259 get { return (ITaskItem) Bag ["OutputAssembly"]; }
260 set { Bag ["OutputAssembly"] = value; }
263 public ITaskItem[] References {
264 get { return (ITaskItem[]) Bag ["References"]; }
265 set { Bag ["References"] = value; }
268 public ITaskItem[] Resources {
269 get { return (ITaskItem[]) Bag ["Resources"]; }
270 set { Bag ["Resources"] = value; }
273 public ITaskItem[] ResponseFiles {
274 get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
275 set { Bag ["ResponseFiles"] = value; }
278 public ITaskItem[] Sources {
279 get { return (ITaskItem[]) Bag ["Sources"]; }
281 Bag ["Sources"] = value;
282 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
283 Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
287 protected override Encoding StandardOutputEncoding {
288 get { return Console.Error.Encoding; }
291 public string TargetType {
293 if (Bag.Contains ("TargetType")) {
294 string s = (string) Bag ["TargetType"];
295 return s.ToLowerInvariant ();
299 set { Bag ["TargetType"] = value; }
302 public bool TreatWarningsAsErrors {
303 get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
304 set { Bag ["TreatWarningsAsErrors"] = value; }
307 public bool Utf8Output {
308 get { return GetBoolParameterWithDefault ("Utf8Output", false); }
309 set { Bag ["Utf8Output"] = value; }
312 public string Win32Icon {
313 get { return (string) Bag ["Win32Icon"]; }
314 set { Bag ["Win32Icon"] = value; }
317 public string Win32Resource {
318 get { return (string) Bag ["Win32Resource"]; }
319 set { Bag ["Win32Resource"] = value; }