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) || string.Equals (dtype, "pdbonly", 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:portable");
76 commandLine.AppendSwitch ("/debug-");
78 commandLine.AppendSwitchIfNotNull ("/keycontainer:", KeyContainer);
79 commandLine.AppendSwitchIfNotNull ("/keyfile:", KeyFile);
80 if (KeyFile != null && !DelaySign)
81 commandLine.AppendSwitch("/publicsign");
83 // FIXME: add ids from metadata
84 if (LinkResources != null)
85 foreach (ITaskItem item in LinkResources)
86 commandLine.AppendSwitchIfNotNull ("/linkresource:", item.ItemSpec);
89 commandLine.AppendSwitch ("/nologo");
91 if (Bag ["Optimize"] != null)
93 commandLine.AppendSwitch ("/optimize+");
95 commandLine.AppendSwitch ("/optimize-");
97 if (OutputAssembly != null)
98 commandLine.AppendSwitchIfNotNull ("/out:", OutputAssembly.ItemSpec);
100 if (Resources != null)
101 foreach (ITaskItem item in Resources) {
102 string logical_name = item.GetMetadata ("LogicalName");
103 if (logical_name.Length > 0)
104 commandLine.AppendSwitchIfNotNull ("/resource:",
105 String.Format ("{0},{1}", item.ItemSpec, logical_name));
107 commandLine.AppendSwitchIfNotNull ("/resource:", item.ItemSpec);
111 foreach (ITaskItem item in Sources)
112 commandLine.AppendFileNameIfNotNull (item.ItemSpec);
114 if (TargetType != null)
115 commandLine.AppendSwitchIfNotNull ("/target:", TargetType);
116 if (Bag ["TreatWarningsAsErrors"] != null)
117 if (TreatWarningsAsErrors)
118 commandLine.AppendSwitch ("/warnaserror+");
120 commandLine.AppendSwitch ("/warnaserror-");
121 commandLine.AppendSwitchIfNotNull ("/win32icon:", Win32Icon);
125 protected bool CheckAllReferencesExistOnDisk ()
127 if (Bag ["References"] != null)
128 foreach (ITaskItem item in (ITaskItem[]) Bag ["References"])
129 if (!File.Exists (item.GetMetadata ("FullPath")))
135 protected void CheckHostObjectSupport (string parameterName,
136 bool resultFromHostObjectSetOperation)
141 protected override bool HandleTaskExecutionErrors ()
143 if (!Log.HasLoggedErrors && ExitCode != 0)
144 Log.LogError ("Compiler crashed with code: {0}.", ExitCode);
146 return ExitCode == 0 && !Log.HasLoggedErrors;
150 protected bool ListHasNoDuplicateItems (ITaskItem [] itemList,
151 string parameterName)
153 Dictionary <string, object> items = new Dictionary <string, object> ();
155 foreach (ITaskItem item in itemList) {
156 if (!items.ContainsKey (item.ItemSpec))
157 items.Add (item.ItemSpec, null);
165 protected internal virtual bool UseAlternateCommandLineToolToExecute ()
168 // If an alternate tool name or tool path was specified in the project file, then that tool is used
169 // rather than the host compiler for integrated development environment (IDE) builds.
171 return !string.IsNullOrEmpty (ToolPath) || ToolName != ToolExe;
175 protected override bool ValidateParameters ()
180 public string[] AdditionalLibPaths {
181 get { return (string[]) Bag ["AdditionalLibPaths"]; }
182 set { Bag ["AdditionalLibPaths"] = value; }
185 public string[] AddModules {
186 get { return (string[]) Bag ["AddModules"]; }
187 set { Bag ["AddModules"] = value; }
190 public int CodePage {
191 get { return GetIntParameterWithDefault ("CodePage", 0); }
192 set { Bag ["CodePage"] = value; }
195 public string DebugType {
196 get { return (string) Bag ["DebugType"]; }
197 set { Bag ["DebugType"] = value; }
200 public string DefineConstants {
201 get { return (string) Bag ["DefineConstants"]; }
202 set { Bag ["DefineConstants"] = value; }
205 public bool DelaySign {
206 get { return GetBoolParameterWithDefault ("DelaySign", false); }
207 set { Bag ["DelaySign"] = value; }
210 public bool EmitDebugInformation {
211 get { return GetBoolParameterWithDefault ("EmitDebugInformation", false); }
212 set { Bag ["EmitDebugInformation"] = value; }
215 public int FileAlignment {
216 get { return GetIntParameterWithDefault ("FileAlignment", 0); }
217 set { Bag ["FileAlignment"] = value; }
220 protected bool HostCompilerSupportsAllParameters {
222 set { Bag ["HostCompilerSupportsAllParameters"] = value; }
225 public string KeyContainer {
226 get { return (string) Bag ["KeyContainer"]; }
227 set { Bag ["KeyContainer"] = value; }
230 public string KeyFile {
231 get { return (string) Bag ["KeyFile"]; }
232 set { Bag ["KeyFile"] = value; }
235 public ITaskItem[] LinkResources {
236 get { return (ITaskItem[]) Bag ["LinkResources"]; }
237 set { Bag ["LinkResources"] = value; }
240 public string MainEntryPoint {
241 get { return (string) Bag ["MainEntryPoint"]; }
242 set { Bag ["MainEntryPoint"] = value; }
245 public bool NoConfig {
246 get { return GetBoolParameterWithDefault ("NoConfig", false); }
247 set { Bag ["NoConfig"] = value; }
251 get { return GetBoolParameterWithDefault ("NoLogo", false); }
252 set { Bag ["NoLogo"] = value; }
255 public bool Optimize {
256 get { return GetBoolParameterWithDefault ("Optimize", false); }
257 set { Bag ["Optimize"] = value; }
261 public ITaskItem OutputAssembly {
262 get { return (ITaskItem) Bag ["OutputAssembly"]; }
263 set { Bag ["OutputAssembly"] = value; }
266 public ITaskItem[] References {
267 get { return (ITaskItem[]) Bag ["References"]; }
268 set { Bag ["References"] = value; }
271 public ITaskItem[] Resources {
272 get { return (ITaskItem[]) Bag ["Resources"]; }
273 set { Bag ["Resources"] = value; }
276 public ITaskItem[] ResponseFiles {
277 get { return (ITaskItem[]) Bag ["ResponseFiles"]; }
278 set { Bag ["ResponseFiles"] = value; }
281 public ITaskItem[] Sources {
282 get { return (ITaskItem[]) Bag ["Sources"]; }
284 Bag ["Sources"] = value;
285 if (Bag ["OutputAssembly"] == null && value != null && value.Length >= 1)
286 Bag ["OutputAssembly"] = new TaskItem (String.Format ("{0}.exe", value [0].ItemSpec));
290 protected override Encoding StandardOutputEncoding {
291 get { return Console.Error.Encoding; }
294 public string TargetType {
296 if (Bag.Contains ("TargetType")) {
297 string s = (string) Bag ["TargetType"];
298 return s.ToLowerInvariant ();
302 set { Bag ["TargetType"] = value; }
305 public bool TreatWarningsAsErrors {
306 get { return GetBoolParameterWithDefault ("TreatWarningsAsErrors", false); }
307 set { Bag ["TreatWarningsAsErrors"] = value; }
310 public bool Utf8Output {
311 get { return GetBoolParameterWithDefault ("Utf8Output", false); }
312 set { Bag ["Utf8Output"] = value; }
315 public string Win32Icon {
316 get { return (string) Bag ["Win32Icon"]; }
317 set { Bag ["Win32Icon"] = value; }
320 public string Win32Resource {
321 get { return (string) Bag ["Win32Resource"]; }
322 set { Bag ["Win32Resource"] = value; }