/
Nasm.cs
115 lines (91 loc) · 2.97 KB
/
Nasm.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
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Cosmos.Build.Tasks
{
public class Nasm : ToolTask
{
enum OutputFormatEnum
{
Bin,
ELF
}
#region Task Parameters
[Required]
public string InputFile { get; set; }
[Required]
public string OutputFile { get; set; }
[Required]
public string OutputFormat
{
get => mOutputFormat.ToString();
set => mOutputFormat = (OutputFormatEnum)Enum.Parse(typeof(OutputFormatEnum), value, true);
}
#endregion
private OutputFormatEnum mOutputFormat;
protected override string ToolName => "nasm.exe";
protected override MessageImportance StandardErrorLoggingImportance => MessageImportance.High;
protected override MessageImportance StandardOutputLoggingImportance => MessageImportance.High;
protected override bool ValidateParameters()
{
if (String.IsNullOrWhiteSpace(InputFile))
{
Log.LogError("No input file specified!");
}
else if (!File.Exists(InputFile))
{
Log.LogError($"Input file '{InputFile}' doesn't exist!");
}
if (String.IsNullOrWhiteSpace(OutputFile))
{
Log.LogError($"No output file specified!");
}
return !Log.HasLoggedErrors;
}
protected override string GenerateFullPathToTool()
{
if (String.IsNullOrWhiteSpace(ToolExe))
{
return null;
}
if (String.IsNullOrWhiteSpace(ToolPath))
{
return Path.Combine(Directory.GetCurrentDirectory(), ToolExe);
}
return Path.Combine(Path.GetFullPath(ToolPath), ToolExe);
}
protected override string GenerateCommandLineCommands()
{
var xBuilder = new CommandLineBuilder();
xBuilder.AppendSwitch("-g");
xBuilder.AppendSwitchIfNotNull("-f ", OutputFormat);
xBuilder.AppendSwitchIfNotNull("-o ", OutputFile);
if (mOutputFormat == OutputFormatEnum.ELF)
{
xBuilder.AppendSwitch("-dELF_COMPILATION");
}
else
{
xBuilder.AppendSwitch("-dBIN_COMPILATION");
}
xBuilder.AppendSwitch("-O0");
xBuilder.AppendFileNameIfNotNull(InputFile);
return xBuilder.ToString();
}
public override bool Execute()
{
var xSW = Stopwatch.StartNew();
try
{
return base.Execute();
}
finally
{
xSW.Stop();
Log.LogMessage(MessageImportance.High, "Nasm task took {0}", xSW.Elapsed);
}
}
}
}