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.
31 using System.Collections;
32 using System.Collections.Generic;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace Microsoft.Build.Tasks {
39 public abstract class ManagedCompiler : ToolTaskExtension {
41 protected ManagedCompiler ()
46 protected internal override void AddCommandLineCommands (
47 CommandLineBuilderExtension commandLine)
50 commandLine.AppendSwitch ("/noconfig");
54 protected internal override void AddResponseFileCommands (
55 CommandLineBuilderExtension commandLine)
57 if (AddModules != null && AddModules.Length > 0)
58 commandLine.AppendSwitchIfNotNull ("/addmodule:", AddModules, ",");
59 if (Bag ["CodePage"] != null)
60 commandLine.AppendSwitchIfNotNull ("/codepage:", CodePage.ToString ());
62 commandLine.AppendSwitchIfNotNull ("/debug:", DebugType);
64 if (Bag ["DelaySign"] != null)
66 commandLine.AppendSwitch ("/delaysign+");
68 commandLine.AppendSwitch ("/delaysign-");
69 if (Bag ["EmitDebugInformation"] != null)
70 if (EmitDebugInformation)
71 commandLine.AppendSwitch ("/debug+");
73 commandLine.AppendSwitch ("/debug-");
75 commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
76 commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
77 // FIXME: add ids from metadata
78 if (LinkResources != null)
79 foreach (ITaskItem item in LinkResources)
80 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
83 commandLine.AppendSwitch ("/nologo");
85 if (Bag ["Optimize"] != null)
87 commandLine.AppendSwitch ("/optimize+");
89 commandLine.AppendSwitch ("/optimize-");
91 if (OutputAssembly != null)
92 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
94 if (Resources != null)
95 foreach (ITaskItem item in Resources) {
96 string logical_name = item.GetMetadata ("LogicalName");
97 if (logical_name.Length > 0)
98 commandLine.AppendSwitchIfNotNull ("/resource:",
99 String.Format ("{0},{1}", item.ItemSpec, logical_name));
101 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
105 foreach (ITaskItem item in Sources)
106 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
108 if (TargetType != null)
109 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
110 if (Bag ["TreatWarningsAsErrors"] != null)
111 if (TreatWarningsAsErrors)
112 commandLine.AppendSwitch ("/warnaserror+");
114 commandLine.AppendSwitch ("/warnaserror-");
115 commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
119 protected bool CheckAllReferencesExistOnDisk ()
121 if (Bag ["References"] != null)
122 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
123 if (!File.Exists (item.GetMetadata ("FullPath")))
129 protected void CheckHostObjectSupport (string parameterName,
130 bool resultFromHostObjectSetOperation)
135 protected override bool HandleTaskExecutionErrors ()
137 if (!Log.HasLoggedErrors && ExitCode != 0)
138 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
140 return ExitCode == 0 && !Log.HasLoggedErrors;
144 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
145 string parameterName)
147 Dictionary <string, object> items = new Dictionary <string, object> ();
149 foreach (ITaskItem item in itemList) {
150 if (!items.ContainsKey (item.ItemSpec))
151 items.Add (item.ItemSpec, null);
160 protected override bool ValidateParameters ()
165 public string[] AdditionalLibPaths {
166 get { return (string[]) Bag ["AdditionalLibPaths"]; }
167 set { Bag ["AdditionalLibPaths"] = value; }
170 public string[] AddModules {
171 get { return (string[]) Bag ["AddModules"]; }
172 set { Bag ["AddModules"] = value; }
175 public int CodePage {
176 get { return GetIntParameterWithDefault ("CodePage", 0); }
177 set { Bag ["CodePage"] = value; }
180 public string DebugType {
181 get { return (string) Bag ["DebugType"]; }
182 set { Bag ["DebugType"] = value; }
185 public string DefineConstants {
186 get { return (string) Bag ["DefineConstants"]; }
187 set { Bag ["DefineConstants"] = value; }
190 public bool DelaySign {
191 get { return GetBoolParameterWithDefault ("DelaySign", false); }
192 set { Bag ["DelaySign"] = value; }
195 public bool EmitDebugInformation {
196 get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
197 set { Bag ["EmitDebugInformation"] = value; }
200 public int FileAlignment {
201 get { return GetIntParameterWithDefault ("FileAlignment", 0); }
202 set { Bag ["FileAlignment"] = value; }
205 protected bool HostCompilerSupportsAllParameters {
207 set { Bag ["HostCompilerSupportsAllParameters"] = value; }
210 public string KeyContainer {
211 get { return (string) Bag ["KeyContainer"]; }
212 set { Bag ["KeyContainer"] = value; }
215 public string KeyFile {
216 get { return (string) Bag ["KeyFile"]; }
217 set { Bag ["KeyFile"] = value; }
220 public ITaskItem[] LinkResources {
221 get { return (ITaskItem[]) Bag ["LinkResources"]; }
222 set { Bag ["LinkResources"] = value; }
225 public string MainEntryPoint {
226 get { return (string) Bag ["MainEntryPoint"]; }
227 set { Bag ["MainEntryPoint"] = value; }
230 public bool NoConfig {
231 get { return GetBoolParameterWithDefault ("NoConfig", false); }
232 set { Bag ["NoConfig"] = value; }
236 get { return GetBoolParameterWithDefault ("NoLogo", false); }
237 set { Bag ["NoLogo"] = value; }
240 public bool Optimize {
241 get { return GetBoolParameterWithDefault ("Optimize", false); }
242 set { Bag ["Optimize"] = value; }
246 public ITaskItem OutputAssembly {
247 get { return (ITaskItem) Bag ["OutputAssembly"]; }
248 set { Bag ["OutputAssembly"] = value; }
251 public ITaskItem[] References {
252 get { return (ITaskItem[]) Bag ["References"]; }
253 set { Bag ["References"] = value; }
256 public ITaskItem[] Resources {
257 get { return (ITaskItem[]) Bag ["Resources"]; }
258 set { Bag ["Resources"] = value; }
261 public ITaskItem[] ResponseFiles {
262 get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
263 set { Bag ["ResponseFiles"] = value; }
266 public ITaskItem[] Sources {
267 get { return (ITaskItem[]) Bag ["Sources"]; }
269 Bag ["Sources"] = value;
270 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
271 Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
275 protected override Encoding StandardOutputEncoding {
276 get { return Console.Error.Encoding; }
279 public string TargetType {
281 if (Bag.Contains ("TargetType")) {
282 string s = (string) Bag ["TargetType"];
283 return s.ToLowerInvariant ();
287 set { Bag ["TargetType"] = value; }
290 public bool TreatWarningsAsErrors {
291 get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
292 set { Bag ["TreatWarningsAsErrors"] = value; }
295 public bool Utf8Output {
296 get { return GetBoolParameterWithDefault ("Utf8Output", false); }
297 set { Bag ["Utf8Output"] = value; }
300 public string Win32Icon {
301 get { return (string) Bag ["Win32Icon"]; }
302 set { Bag ["Win32Icon"] = value; }
305 public string Win32Resource {
306 get { return (string) Bag ["Win32Resource"]; }
307 set { Bag ["Win32Resource"] = value; }