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 return ExitCode == 0 && !Log.HasLoggedErrors;
141 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
142 string parameterName)
144 Dictionary <string, object> items = new Dictionary <string, object> ();
146 foreach (ITaskItem item in itemList) {
147 if (!items.ContainsKey (item.ItemSpec))
148 items.Add (item.ItemSpec, null);
157 protected override bool ValidateParameters ()
162 public string[] AdditionalLibPaths {
163 get { return (string[]) Bag ["AdditionalLibPaths"]; }
164 set { Bag ["AdditionalLibPaths"] = value; }
167 public string[] AddModules {
168 get { return (string[]) Bag ["AddModules"]; }
169 set { Bag ["AddModules"] = value; }
172 public int CodePage {
173 get { return GetIntParameterWithDefault ("CodePage", 0); }
174 set { Bag ["CodePage"] = value; }
177 public string DebugType {
178 get { return (string) Bag ["DebugType"]; }
179 set { Bag ["DebugType"] = value; }
182 public string DefineConstants {
183 get { return (string) Bag ["DefineConstants"]; }
184 set { Bag ["DefineConstants"] = value; }
187 public bool DelaySign {
188 get { return GetBoolParameterWithDefault ("DelaySign", false); }
189 set { Bag ["DelaySign"] = value; }
192 public bool EmitDebugInformation {
193 get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
194 set { Bag ["EmitDebugInformation"] = value; }
197 public int FileAlignment {
198 get { return GetIntParameterWithDefault ("FileAlignment", 0); }
199 set { Bag ["FileAlignment"] = value; }
202 protected bool HostCompilerSupportsAllParameters {
204 set { Bag ["HostCompilerSupportsAllParameters"] = value; }
207 public string KeyContainer {
208 get { return (string) Bag ["KeyContainer"]; }
209 set { Bag ["KeyContainer"] = value; }
212 public string KeyFile {
213 get { return (string) Bag ["KeyFile"]; }
214 set { Bag ["KeyFile"] = value; }
217 public ITaskItem[] LinkResources {
218 get { return (ITaskItem[]) Bag ["LinkResources"]; }
219 set { Bag ["LinkResources"] = value; }
222 public string MainEntryPoint {
223 get { return (string) Bag ["MainEntryPoint"]; }
224 set { Bag ["MainEntryPoint"] = value; }
227 public bool NoConfig {
228 get { return GetBoolParameterWithDefault ("NoConfig", false); }
229 set { Bag ["NoConfig"] = value; }
233 get { return GetBoolParameterWithDefault ("NoLogo", false); }
234 set { Bag ["NoLogo"] = value; }
237 public bool Optimize {
238 get { return GetBoolParameterWithDefault ("Optimize", false); }
239 set { Bag ["Optimize"] = value; }
243 public ITaskItem OutputAssembly {
244 get { return (ITaskItem) Bag ["OutputAssembly"]; }
245 set { Bag ["OutputAssembly"] = value; }
248 public ITaskItem[] References {
249 get { return (ITaskItem[]) Bag ["References"]; }
250 set { Bag ["References"] = value; }
253 public ITaskItem[] Resources {
254 get { return (ITaskItem[]) Bag ["Resources"]; }
255 set { Bag ["Resources"] = value; }
258 public ITaskItem[] ResponseFiles {
259 get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
260 set { Bag ["ResponseFiles"] = value; }
263 public ITaskItem[] Sources {
264 get { return (ITaskItem[]) Bag ["Sources"]; }
266 Bag ["Sources"] = value;
267 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
268 Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
272 protected override Encoding StandardOutputEncoding {
273 get { return Console.Error.Encoding; }
276 public string TargetType {
278 if (Bag.Contains ("TargetType")) {
279 string s = (string) Bag ["TargetType"];
284 set { Bag ["TargetType"] = value; }
287 public bool TreatWarningsAsErrors {
288 get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
289 set { Bag ["TreatWarningsAsErrors"] = value; }
292 public bool Utf8Output {
293 get { return GetBoolParameterWithDefault ("Utf8Output", false); }
294 set { Bag ["Utf8Output"] = value; }
297 public string Win32Icon {
298 get { return (string) Bag ["Win32Icon"]; }
299 set { Bag ["Win32Icon"] = value; }
302 public string Win32Resource {
303 get { return (string) Bag ["Win32Resource"]; }
304 set { Bag ["Win32Resource"] = value; }