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:portable");
72 commandLine.AppendSwitch ("/debug-");
74 commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
75 commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
76 if (KeyFile != null && !DelaySign)
77 commandLine.AppendSwitch("/publicsign");
79 // FIXME: add ids from metadata
80 if (LinkResources != null)
81 foreach (ITaskItem item in LinkResources)
82 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
85 commandLine.AppendSwitch ("/nologo");
87 if (Bag ["Optimize"] != null)
89 commandLine.AppendSwitch ("/optimize+");
91 commandLine.AppendSwitch ("/optimize-");
93 if (OutputAssembly != null)
94 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
96 if (Resources != null)
97 foreach (ITaskItem item in Resources) {
98 string logical_name = item.GetMetadata ("LogicalName");
99 if (logical_name.Length > 0)
100 commandLine.AppendSwitchIfNotNull ("/resource:",
101 String.Format ("{0},{1}", item.ItemSpec, logical_name));
103 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
107 foreach (ITaskItem item in Sources)
108 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
110 if (TargetType != null)
111 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
112 if (Bag ["TreatWarningsAsErrors"] != null)
113 if (TreatWarningsAsErrors)
114 commandLine.AppendSwitch ("/warnaserror+");
116 commandLine.AppendSwitch ("/warnaserror-");
117 commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
121 protected bool CheckAllReferencesExistOnDisk ()
123 if (Bag ["References"] != null)
124 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
125 if (!File.Exists (item.GetMetadata ("FullPath")))
131 protected void CheckHostObjectSupport (string parameterName,
132 bool resultFromHostObjectSetOperation)
137 protected override bool HandleTaskExecutionErrors ()
139 if (!Log.HasLoggedErrors && ExitCode != 0)
140 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
142 return ExitCode == 0 && !Log.HasLoggedErrors;
146 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
147 string parameterName)
149 Dictionary <string, object> items = new Dictionary <string, object> ();
151 foreach (ITaskItem item in itemList) {
152 if (!items.ContainsKey (item.ItemSpec))
153 items.Add (item.ItemSpec, null);
161 protected internal virtual bool UseAlternateCommandLineToolToExecute ()
164 // If an alternate tool name or tool path was specified in the project file, then that tool is used
165 // rather than the host compiler for integrated development environment (IDE) builds.
167 return !string.IsNullOrEmpty (ToolPath) || ToolName != ToolExe;
171 protected override bool ValidateParameters ()
176 public string[] AdditionalLibPaths {
177 get { return (string[]) Bag ["AdditionalLibPaths"]; }
178 set { Bag ["AdditionalLibPaths"] = value; }
181 public string[] AddModules {
182 get { return (string[]) Bag ["AddModules"]; }
183 set { Bag ["AddModules"] = value; }
186 public int CodePage {
187 get { return GetIntParameterWithDefault ("CodePage", 0); }
188 set { Bag ["CodePage"] = value; }
191 public string DebugType {
192 get { return (string) Bag ["DebugType"]; }
193 set { Bag ["DebugType"] = value; }
196 public string DefineConstants {
197 get { return (string) Bag ["DefineConstants"]; }
198 set { Bag ["DefineConstants"] = value; }
201 public bool DelaySign {
202 get { return GetBoolParameterWithDefault ("DelaySign", false); }
203 set { Bag ["DelaySign"] = value; }
206 public bool EmitDebugInformation {
207 get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
208 set { Bag ["EmitDebugInformation"] = value; }
211 public int FileAlignment {
212 get { return GetIntParameterWithDefault ("FileAlignment", 0); }
213 set { Bag ["FileAlignment"] = value; }
216 protected bool HostCompilerSupportsAllParameters {
218 set { Bag ["HostCompilerSupportsAllParameters"] = value; }
221 public string KeyContainer {
222 get { return (string) Bag ["KeyContainer"]; }
223 set { Bag ["KeyContainer"] = value; }
226 public string KeyFile {
227 get { return (string) Bag ["KeyFile"]; }
228 set { Bag ["KeyFile"] = value; }
231 public ITaskItem[] LinkResources {
232 get { return (ITaskItem[]) Bag ["LinkResources"]; }
233 set { Bag ["LinkResources"] = value; }
236 public string MainEntryPoint {
237 get { return (string) Bag ["MainEntryPoint"]; }
238 set { Bag ["MainEntryPoint"] = value; }
241 public bool NoConfig {
242 get { return GetBoolParameterWithDefault ("NoConfig", false); }
243 set { Bag ["NoConfig"] = value; }
247 get { return GetBoolParameterWithDefault ("NoLogo", false); }
248 set { Bag ["NoLogo"] = value; }
251 public bool Optimize {
252 get { return GetBoolParameterWithDefault ("Optimize", false); }
253 set { Bag ["Optimize"] = value; }
257 public ITaskItem OutputAssembly {
258 get { return (ITaskItem) Bag ["OutputAssembly"]; }
259 set { Bag ["OutputAssembly"] = value; }
262 public ITaskItem[] References {
263 get { return (ITaskItem[]) Bag ["References"]; }
264 set { Bag ["References"] = value; }
267 public ITaskItem[] Resources {
268 get { return (ITaskItem[]) Bag ["Resources"]; }
269 set { Bag ["Resources"] = value; }
272 public ITaskItem[] ResponseFiles {
273 get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
274 set { Bag ["ResponseFiles"] = value; }
277 public ITaskItem[] Sources {
278 get { return (ITaskItem[]) Bag ["Sources"]; }
280 Bag ["Sources"] = value;
281 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
282 Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
286 protected override Encoding StandardOutputEncoding {
287 get { return Console.Error.Encoding; }
290 public string TargetType {
292 if (Bag.Contains ("TargetType")) {
293 string s = (string) Bag ["TargetType"];
294 return s.ToLowerInvariant ();
298 set { Bag ["TargetType"] = value; }
301 public bool TreatWarningsAsErrors {
302 get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
303 set { Bag ["TreatWarningsAsErrors"] = value; }
306 public bool Utf8Output {
307 get { return GetBoolParameterWithDefault ("Utf8Output", false); }
308 set { Bag ["Utf8Output"] = value; }
311 public string Win32Icon {
312 get { return (string) Bag ["Win32Icon"]; }
313 set { Bag ["Win32Icon"] = value; }
316 public string Win32Resource {
317 get { return (string) Bag ["Win32Resource"]; }
318 set { Bag ["Win32Resource"] = value; }