-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.cs
More file actions
312 lines (284 loc) · 13.2 KB
/
Builder.cs
File metadata and controls
312 lines (284 loc) · 13.2 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// -clang -msvc
// -debug -ndebug
// -output <output_filepath>
// -force
// -warnings_are_errors
namespace BuildTask
{
internal class Builder
{
bool IsFlag(string arg)
{
return arg[0] == '-' || arg[0] == '/';
}
bool AreArgumentValids(Arguments arguments)
{
bool error = false;
if (!arguments.Compiler.HasValue) { error = true; Log.WriteLine("No compiler specified! Please use -clang or -msvc"); }
return !error;
}
private bool ParseArgumentsForCompiler(CommandLine _commandline, Arguments _args)
{
if (_commandline.IsPresent("clang"))
{
_args.Compiler.Value = ECompiler.Clang6_0;
}
if (_commandline.IsPresent("msvc"))
{
_args.Compiler.Value = ECompiler.MSVC19;
}
if (_commandline.TryGet("compiler", out string compiler_name))
{
switch (compiler_name)
{
case "msvc": _args.Compiler.Value = ECompiler.MSVC19; break;
case "clang": _args.Compiler.Value = ECompiler.Clang6_0; break;
default: Log.WriteLine($@"Unknown compiler ""{compiler_name}""!"); return false;
}
}
if (_args.Compiler.WasCrashed)
{
Log.WriteLine("Compiler defined multiple times!");
return false;
}
return true;
}
private bool ParseArgumentsForOptimizationLevel(CommandLine _commandline, Arguments _args)
{
if (_commandline.IsPresent("debug"))
{
_args.DebugLevel.Value = EDebugLevel.Debug;
}
if (_commandline.IsPresent("ndebug"))
{
_args.DebugLevel.Value = EDebugLevel.NonDebug;
}
if (_commandline.TryGet("optimization", out string optimization_name))
{
switch (optimization_name)
{
case "debug": _args.DebugLevel.Value = EDebugLevel.Debug; break;
case "ndebug": _args.DebugLevel.Value = EDebugLevel.NonDebug; break;
default: Log.WriteLine($@"Unknown optimization level ""{optimization_name}""!"); return false;
}
}
if (_args.DebugLevel.WasCrashed)
{
Log.WriteLine("Optimization level defined multiple times!");
return false;
}
return true;
}
private bool ParseArgumentsForWarningLevel(CommandLine _commandline, Arguments _args)
{
if (_commandline.TryGet("warning_level", out string warning_level_name))
{
if (Enum.TryParse(warning_level_name, true, out EWarningLevel wl))
{
_args.WarningLevel = wl;
return true;
}
else
{
Log.WriteLine($@"Unknown warning level ""{warning_level_name}""!");
return false;
}
}
// no warning_level is not an error
return true;
}
internal int Run(string[] commandline_args)
{
var commandLine = new CommandLine();
commandLine.RegisterFlag("clang", CommandLine.NeedValue.NoValue); // obsolete
commandLine.RegisterFlag("msvc", CommandLine.NeedValue.NoValue); // obsolete
commandLine.RegisterFlag("debug", CommandLine.NeedValue.NoValue);
commandLine.RegisterFlag("ndebug", CommandLine.NeedValue.NoValue);
commandLine.RegisterFlag("force", CommandLine.NeedValue.NoValue);
commandLine.RegisterFlag("warnings_are_errors", CommandLine.NeedValue.NoValue);
commandLine.RegisterFlag("output", CommandLine.NeedValue.OneValue);
commandLine.RegisterFlag("compiler", CommandLine.NeedValue.OneValue);
commandLine.RegisterFlag("warning_level", CommandLine.NeedValue.OneValue);
commandLine.RegisterFlag("blueprint", CommandLine.NeedValue.OneValue);
commandLine.Parse(commandline_args);
Arguments args = new Arguments();
bool arg_ok = true;
arg_ok &= ParseArgumentsForCompiler(commandLine, args);
arg_ok &= ParseArgumentsForOptimizationLevel(commandLine, args);
arg_ok &= ParseArgumentsForWarningLevel(commandLine, args);
// warning as error
args.WarningsAreErrors = commandLine.IsPresent("warnings_are_errors");
// force compilation
args.ForceCompilation = commandLine.IsPresent("force");
string override_outputFilename;
// output
if (commandLine.TryGet("output", out override_outputFilename))
{
override_outputFilename = Path.GetFullPath(override_outputFilename); // override is relative to workspace folder
//Log.WriteLine($@"Override blueprint output filename by ""{ override_outputFilename }"".");
}
if (commandLine.Files.Count() == 0)
{
arg_ok = true;
Log.WriteLine("ERROR: No source filename specified! You must specify at least one source filename.");
}
var missing_files = commandLine.Files.Where(s => !File.Exists(s)).ToArray();
if (missing_files.Length!=0)
{
Log.WriteLine($"ERROR: Missing source files: { string.Join(", ", missing_files.Select(s => $@"""{s}""")) }.");
arg_ok = false;
}
var blueprintProjectContainer = new Blueprint.ProjectContainer();
IEnumerable<Blueprint.Project> project_to_compile = null;
if (commandLine.TryGet("blueprint", out string blueprint_filename))
{
if (blueprintProjectContainer.Import(blueprint_filename))
{
if (commandLine.Files.Count() == 1 && commandLine.Files.First().EndsWith(".blueprint.json"))
{
var project = blueprintProjectContainer.GetProjectByFullpath(Path.GetFullPath(commandLine.Files.First()));
if (project != null)
project_to_compile = new List<Blueprint.Project> { project };
}
else
{
project_to_compile = blueprintProjectContainer.Touch(commandLine.Files);
}
if (project_to_compile.Count() == 0)
{
if (override_outputFilename != null)
{
project_to_compile = new List<Blueprint.Project>
{
new Blueprint.Project
{
Name = "(No Project)",
Sources = commandLine.Files.ToList(),
FullFolderPath = Directory.GetCurrentDirectory(),
Dependencies = new List<string>()
}
};
}
else
{
if (commandLine.Files.Count() > 1)
Log.WriteLine($@"ERROR: No file among { string.Join(", ", commandLine.Files.Select(f => $@"""{f}""")) } belongs to a blueprint and no output defined!");
else
Log.WriteLine($@"ERROR: File ""{ commandLine.Files.First() }"" does not belong to a blueprint and no output defined!");
arg_ok = false;
}
}
}
else
{
Log.WriteLine("ERROR: An error occured while reading blueprints!");
arg_ok = false;
}
}
else
{
Log.WriteLine("ERROR: No blueprint specified! (not supported yet)");
arg_ok = false;
}
arg_ok &= AreArgumentValids(args);
if (!arg_ok)
{
Log.WriteLine("ERROR: Something went wrong!");
return 1;
}
var compilo = Compilers.Factory.Create(args.Compiler.GetValueOrDefault(ECompiler.Clang6_0));
compilo.CppVersion = args.StandardCpp.GetValueOrDefault(ECppVersion.Cpp17);
compilo.WarningLevel = args.WarningLevel.GetValueOrDefault(EWarningLevel.High);
compilo.DebugLevel = args.DebugLevel.GetValueOrDefault(EDebugLevel.NonDebug);
compilo.WarningAsErrors = args.WarningsAreErrors;
Dictionary<string, string> variables = new Dictionary<string, string>
{
{ "compiler_name", compilo.ShortName },
{ "optimization", compilo.DebugLevel==EDebugLevel.Debug ? "d" : "r" },
{ "target", "win64" }
};
int exitCode = 0;
string baseIncludePath = Directory.GetCurrentDirectory();
foreach (var project in project_to_compile)
{
Log.WriteLine($@"Project ""{project.Name}"":");
using (new ScopedWorkingDirectory(project.FullFolderPath))
using (new ScopedLogIndent())
{
string outputFilename = override_outputFilename!=null ? FileUtility.MakeRelative(project.FullFolderPath, override_outputFilename) : project.ResolveOutput(variables);
var sourceFilenames = project.ResolvedSourceRelativePaths.ToList();
//if (!args.ForceCompilation)
//{
// // TODO: should also check the blueprint file date and the dates of the files this project depends on
// if (File.Exists(outputFilename))
// {
// var outputFileDate = File.GetLastWriteTime(outputFilename);
// var mostRecentSourceFileDate = sourceFilenames.Select(sf => File.GetLastWriteTime(sf)).Min();
// if (outputFileDate > mostRecentSourceFileDate)
// {
// Log.WriteLine("No changes detected. Compilation skipped.");
// continue;
// }
// Log.WriteLine($@"output file ""{ outputFilename }"" is old. Compilation needed.");
// }
// else
// {
// Log.WriteLine($@"output file ""{ outputFilename }"" does not exit yet. Compilation needed.");
// }
//}
//else
//{
// Log.WriteLine("Forced compilation.");
//}
compilo.IntermediaryFileFolderName = FileUtility.MakeRelative(project.FullFolderPath, $@"{baseIncludePath}\obj");
compilo.OutputFilepath = outputFilename;
compilo.SourceFilePaths = sourceFilenames;
compilo.LibFilepaths = project.Libs;
compilo.Defines = project.Defines;
if (compilo is Compilers.MSVC)
{
compilo.ExtraFlags = project.MSVCExtra;
}
else if (compilo is Compilers.Clang)
{
compilo.ExtraFlags = project.ClangExtra;
}
List <string> additionalIncludePaths = project.Dependencies.Select(pname => blueprintProjectContainer.GetProject(pname)).Where(pj => pj != null).Select(pj => FileUtility.MakeRelative(project.FullFolderPath, pj.FullFolderPath)).ToList();
additionalIncludePaths.Add(".");
if (project.FullFolderPath != baseIncludePath)
{
string rel = FileUtility.MakeRelative(project.FullFolderPath, baseIncludePath);
additionalIncludePaths.Add( rel );
//Log.WriteLine($@"AdditionalIncludePath: ""{rel}""");
}
compilo.AdditionalIncludePaths = additionalIncludePaths;
exitCode = compilo.Run();
if (exitCode != 0)
{
Log.WriteLine($@"ERROR: Project ""{project.Name}"" compilation failed!");
break;
}
}
}
Log.WriteLine("Build task completed.");
return exitCode;
}
class ScopedLogIndent : IDisposable
{
public ScopedLogIndent()
{
Log.PushIndent();
}
public void Dispose()
{
Log.PopIndent();
}
}
}
}