-
Notifications
You must be signed in to change notification settings - Fork 1
/
FxCompile.cs
366 lines (327 loc) · 13.2 KB
/
FxCompile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//-----------------------------------------------------------------------
// <copyright file="FxCompile.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using Microsoft.Build.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.Tasks
{
/// <summary>
/// Task to support Fxc.exe
/// </summary>
public class FxCompile : ToolTask
{
/// <summary>
/// Constructor
/// </summary>
public FxCompile()
{
// Because FxCop wants it this way.
}
#region Inputs
/// <summary>
/// Sources to be compiled.
/// </summary>
/// <remarks>Required for task to run.</remarks>
[Required]
public virtual ITaskItem[] Source
{
get { return (ITaskItem[])Bag["Sources"]; }
set { Bag["Sources"] = value; }
}
/// <summary>
/// Gets the collection of parameters used by the derived task class.
/// </summary>
/// <value>Parameter bag.</value>
protected internal Hashtable Bag
{
get
{
return bag;
}
}
private Hashtable bag = new Hashtable();
/// <summary>
/// ShaderType, requires ShaderModel
/// Specifies the type of shader. (/T [type]_[model])
/// </summary>
/// <remarks>Consider using one of these: "NotSet", "Effect", "Vertex", "Pixel", "Geometry", "Hull", "Domain", "Compute", or "Texture".</remarks>
public virtual string ShaderType
{
get
{
return (string)Bag["ShaderType"];
}
set
{
string result = String.Empty;
switch (value.ToLowerInvariant())
{
case "notset":
result = "";
break;
case "effect":
result = "/T fx";
break;
case "vertex":
result = "/T vs";
break;
case "pixel":
result = "/T ps";
break;
case "geometry":
result = "/T gs";
break;
case "hull":
result = "/T hs";
break;
case "domain":
result = "/T ds";
break;
case "compute":
result = "/T cs";
break;
case "texture":
result = "/T tx";
break;
default:
throw new ArgumentException("ShaderType of " + value + @" is an invalid. Consider using one of these: ""NotSet"", ""Effect"", ""Vertex"", ""Pixel"", ""Geometry"", ""Hull"", ""Domain"", ""Compute"", or ""Texture"".");
}
Bag["ShaderType"] = result;
}
}
/// <summary>
/// ShaderModel, requires ShaderType
/// Specifies the shader model. Some shader types can only be used with recent shader models (/T [type]_[model])
/// </summary>
public virtual string ShaderModel
{
get { return (string)Bag["ShaderModel"]; }
set { Bag["ShaderModel"] = value; }
}
/// <summary>
/// AssemblerOutput, requires AssemblerOutputFile
/// Specifies the contents of assembly language output file. (/Fc, /Fx)
/// </summary>
/// <remarks>Consider using one of these: "Assembly Code" or "Assembly Code and Hex".</remarks>
public virtual string AssemblerOutput
{
get
{
return (string)Bag["AssemblerOutput"];
}
set
{
string result = String.Empty;
switch (value.ToLowerInvariant())
{
case "Assembly Code":
result = "/Fc";
break;
case "Assembly Code and Hex":
result = "/Fx";
break;
default:
throw new ArgumentException("AssemblerOutput of " + value + @" is an invalid. Consider using one of these: ""Assembly Code"" or ""Assembly Code and Hex"".");
}
Bag["AssemblerOutput"] = value;
}
}
/// <summary>
/// AssemblerOutputFile, requires AssemblerOutput
/// Specifies file name for assembly code listing file
/// </summary>
public virtual string AssemblerOutputFile
{
get { return (string)Bag["AssemblerOutputFile"]; }
set { Bag["AssemblerOutputFile"] = value; }
}
/// <summary>
/// Specifies a name for the variable name in the header file (/Vn [name])
/// </summary>
public virtual string VariableName
{
get { return (string)Bag["VariableName"]; }
set { Bag["VariableName"] = value; }
}
/// <summary>
/// Specifies a name for header file containing object code. (/Fh [name])
/// </summary>
public virtual string HeaderFileOutput
{
get { return (string)Bag["HeaderFileOutput"]; }
set { Bag["HeaderFileOutput"] = value; }
}
/// <summary>
/// Specifies a name for object file. (/Fo [name])
/// </summary>
public virtual string ObjectFileOutput
{
get { return (string)Bag["ObjectFileOutput"]; }
set { Bag["ObjectFileOutput"] = value; }
}
/// <summary>
/// Defines preprocessing symbols for your source file.
/// </summary>
public virtual string[] PreprocessorDefinitions
{
get { return (string[])Bag["PreprocessorDefinitions"]; }
set { Bag["PreprocessorDefinitions"] = value; }
}
/// <summary>
/// Specifies one or more directories to add to the include path; separate with semi-colons if more than one. (/I[path])
/// </summary>
public virtual string[] AdditionalIncludeDirectories
{
get { return (string[])Bag["AdditionalIncludeDirectories"]; }
set { Bag["AdditionalIncludeDirectories"] = value; }
}
/// <summary>
/// Suppresses the display of the startup banner and information message. (/nologo)
/// </summary>
public virtual bool SuppressStartupBanner
{
get { return GetBoolParameterWithDefault("SuppressStartupBanner", false); }
set { Bag["SuppressStartupBanner"] = value; }
}
/// <summary>
/// Specifies the name of the entry point for the shader (/E[name])
/// </summary>
public virtual string EntryPointName
{
get { return (string)Bag["EntryPointName"]; }
set { Bag["EntryPointName"] = value; }
}
/// <summary>
/// Treats all compiler warnings as errors. For a new project, it may be best to use /WX in all compilations; resolving all warnings will ensure the fewest possible hard-to-find code defects.
/// </summary>
public virtual bool TreatWarningAsError
{
get { return GetBoolParameterWithDefault("TreatWarningAsError", false); }
set { Bag["TreatWarningAsError"] = value; }
}
/// <summary>
/// Disable optimizations. /Od implies /Gfp though output may not be identical to /Od /Gfp.
/// </summary>
public virtual bool DisableOptimizations
{
get { return GetBoolParameterWithDefault("DisableOptimizations", false); }
set { Bag["DisableOptimizations"] = value; }
}
/// <summary>
/// Enable debugging information.
/// </summary>
public virtual bool EnableDebuggingInformation
{
get { return GetBoolParameterWithDefault("EnableDebuggingInformation", false); }
set { Bag["EnableDebuggingInformation"] = value; }
}
/// <summary>
/// Path to Windows SDK
/// </summary>
public string SdkToolsPath
{
get { return (string)Bag["SdkToolsPath"]; }
set { Bag["SdkToolsPath"] = value; }
}
/// <summary>
/// Name to Fxc.exe
/// </summary>
protected override string ToolName
{
get { return "Fxc.exe"; }
}
#endregion
/// <summary>
/// Returns a string with those switches and other information that can't go into a response file and
/// must go directly onto the command line.
/// Called after ValidateParameters and SkipTaskExecution
/// </summary>
/// <returns></returns>
override protected string GenerateCommandLineCommands()
{
CommandLineBuilderExtension commandLineBuilder = new CommandLineBuilderExtension();
AddCommandLineCommands(commandLineBuilder);
return commandLineBuilder.ToString();
}
/// <summary>
/// Returns the command line switch used by the tool executable to specify the response file
/// Will only be called if the task returned a non empty string from GetResponseFileCommands
/// Called after ValidateParameters, SkipTaskExecution and GetResponseFileCommands
/// </summary>
/// <param name="responseFilePath">full path to the temporarily created response file</param>
/// <returns></returns>
override protected string GenerateResponseFileCommands()
{
CommandLineBuilderExtension commandLineBuilder = new CommandLineBuilderExtension();
AddResponseFileCommands(commandLineBuilder);
return commandLineBuilder.ToString();
}
/// <summary>
/// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file.
/// </summary>
/// <param name="commandLine"></param>
protected internal virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
{
}
/// <summary>
/// Add Command Line Commands
/// </summary>
/// <param name="commandLine">CommandLineBuilderExtension</param>
protected internal void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
{
//// Order of these affect the order of the command line
commandLine.AppendSwitchIfNotNull("/I ", AdditionalIncludeDirectories, "");
commandLine.AppendSwitch(SuppressStartupBanner ? "/nologo" : String.Empty);
commandLine.AppendSwitchIfNotNull("/E", EntryPointName);
commandLine.AppendSwitch(TreatWarningAsError ? "/WX" : String.Empty);
// Switch cannot be null
if (ShaderType != null && ShaderModel != null)
{
// shader Model and Type are one switch
commandLine.AppendSwitch(ShaderType + "_" + ShaderModel);
}
commandLine.AppendSwitchIfNotNull("/D ", PreprocessorDefinitions, "");
commandLine.AppendSwitchIfNotNull("/Fh ", HeaderFileOutput);
commandLine.AppendSwitchIfNotNull("/Fo ", ObjectFileOutput);
// Switch cannot be null
if (AssemblerOutput != null)
{
commandLine.AppendSwitchIfNotNull(AssemblerOutput, AssemblerOutputFile);
}
commandLine.AppendSwitchIfNotNull("/Vn ", VariableName);
commandLine.AppendSwitch(DisableOptimizations ? "/Od" : String.Empty);
commandLine.AppendSwitch(EnableDebuggingInformation ? "/Zi" : String.Empty);
commandLine.AppendSwitchIfNotNull("", Source, " ");
}
/// <summary>
/// Fullpath to the fxc.exe
/// </summary>
/// <returns>Fullpath to fxc.exe, if found. Otherwise empty or null.</returns>
protected override string GenerateFullPathToTool()
{
return System.IO.Path.Combine(SdkToolsPath, ToolName);
}
/// <summary>
/// Get a bool parameter and return a default if its not present
/// in the hash table.
/// </summary>
/// <param name="parameters"></param>
/// <param name="parameterName"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
/// <owner>JomoF</owner>
protected internal bool GetBoolParameterWithDefault(string parameterName, bool defaultValue)
{
object obj = bag[parameterName];
return (obj == null) ? defaultValue : (bool) obj;
}
}
}