-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib-msbuild.cake
492 lines (387 loc) · 20.9 KB
/
lib-msbuild.cake
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#addin "nuget:?package=Cake.Issues&version=5.5.0"
#addin "nuget:?package=Cake.Issues.MsBuild&version=5.5.0"
#addin "nuget:?package=System.Configuration.ConfigurationManager&version=9.0.2"
#tool "nuget:?package=MSBuild.Extension.Pack&version=1.9.1"
//-------------------------------------------------------------
private static void BuildSolution(BuildContext buildContext)
{
var solutionName = buildContext.General.Solution.Name;
var solutionFileName = buildContext.General.Solution.FileName;
buildContext.CakeContext.LogSeparator("Building solution '{0}'", solutionName);
var msBuildSettings = new MSBuildSettings
{
Verbosity = Verbosity.Quiet,
//Verbosity = Verbosity.Diagnostic,
ToolVersion = MSBuildToolVersion.Default,
Configuration = buildContext.General.Solution.ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform,
PlatformTarget = PlatformTarget.MSIL,
NoLogo = true
};
//ConfigureMsBuild(buildContext, msBuildSettings, dependency, "build");
RunMsBuild(buildContext, "Solution", solutionFileName, msBuildSettings, "build");
}
//-------------------------------------------------------------
private static void ConfigureMsBuild(BuildContext buildContext, MSBuildSettings msBuildSettings,
string projectName, string action, bool? allowVsPrerelease = null)
{
var toolPath = GetVisualStudioPath(buildContext, allowVsPrerelease);
if (!string.IsNullOrWhiteSpace(toolPath))
{
buildContext.CakeContext.Information($"Overriding ms build tool path to '{toolPath}'");
msBuildSettings.ToolPath = toolPath;
}
// Note: we need to set OverridableOutputPath because we need to be able to respect
// AppendTargetFrameworkToOutputPath which isn't possible for global properties (which
// are properties passed in using the command line)
var outputDirectory = GetProjectOutputDirectory(buildContext, projectName);
buildContext.CakeContext.Information("Output directory: '{0}'", outputDirectory);
msBuildSettings.WithProperty("OverridableOutputRootPath", buildContext.General.OutputRootDirectory);
// GHK: 2022-05-25: Disabled overriding the (whole) output path since this caused all
// reference projects to be re-build again since this override is used for all projects,
// including project references
//msBuildSettings.WithProperty("OverridableOutputPath", outputDirectory);
msBuildSettings.WithProperty("PackageOutputPath", buildContext.General.OutputRootDirectory);
// Only optimize in release mode
if (!buildContext.General.IsLocalBuild)
{
buildContext.CakeContext.Information("This is NOT a local build, disabling building of project references");
// Don't build project references (should already be built)
msBuildSettings.WithProperty("BuildProjectReferences", "false");
//InjectAssemblySearchPathsInProjectFile(buildContext, projectName, GetProjectFileName(buildContext, projectName));
}
else
{
buildContext.CakeContext.Information("This is a local build, not disabling building of project references");
}
// Continuous integration build
msBuildSettings.ContinuousIntegrationBuild = true;
//msBuildSettings.WithProperty("ContinuousIntegrationBuild", "true");
// No NuGet restore (should already be done)
msBuildSettings.WithProperty("ResolveNuGetPackages", "false");
msBuildSettings.Restore = false;
// Solution info
// msBuildSettings.WithProperty("SolutionFileName", System.IO.Path.GetFileName(buildContext.General.Solution.FileName));
// msBuildSettings.WithProperty("SolutionPath", System.IO.Path.GetFullPath(buildContext.General.Solution.FileName));
// msBuildSettings.WithProperty("SolutionDir", System.IO.Path.GetFullPath(buildContext.General.Solution.Directory));
// msBuildSettings.WithProperty("SolutionName", buildContext.General.Solution.Name);
// msBuildSettings.WithProperty("SolutionExt", ".sln");
// msBuildSettings.WithProperty("DefineExplicitDefaults", "true");
// Disable copyright info
msBuildSettings.NoLogo = true;
// Use as much CPU as possible
msBuildSettings.MaxCpuCount = 0;
// Enable for file logging
if (buildContext.General.EnableMsBuildFileLog)
{
msBuildSettings.AddFileLogger(new MSBuildFileLogger
{
Verbosity = msBuildSettings.Verbosity,
//Verbosity = Verbosity.Diagnostic,
LogFile = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}_log.log", projectName, action))
});
}
// Enable for bin logging
if (buildContext.General.EnableMsBuildBinaryLog)
{
msBuildSettings.BinaryLogger = new MSBuildBinaryLogSettings
{
Enabled = true,
Imports = MSBuildBinaryLogImports.Embed,
FileName = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}_log.binlog", projectName, action))
};
}
}
//-------------------------------------------------------------
private static void ConfigureMsBuildForDotNet(BuildContext buildContext, DotNetMSBuildSettings msBuildSettings,
string projectName, string action, bool? allowVsPrerelease = null)
{
var toolPath = GetVisualStudioPath(buildContext, allowVsPrerelease);
if (!string.IsNullOrWhiteSpace(toolPath))
{
buildContext.CakeContext.Information($"Overriding ms build tool path to '{toolPath}'");
msBuildSettings.ToolPath = toolPath;
}
// Note: we need to set OverridableOutputPath because we need to be able to respect
// AppendTargetFrameworkToOutputPath which isn't possible for global properties (which
// are properties passed in using the command line)
var outputDirectory = GetProjectOutputDirectory(buildContext, projectName);
buildContext.CakeContext.Information("Output directory: '{0}'", outputDirectory);
msBuildSettings.WithProperty("OverridableOutputRootPath", buildContext.General.OutputRootDirectory);
// GHK: 2022-05-25: Disabled overriding the (whole) output path since this caused all
// reference projects to be re-build again since this override is used for all projects,
// including project references
//msBuildSettings.WithProperty("OverridableOutputPath", outputDirectory);
msBuildSettings.WithProperty("PackageOutputPath", buildContext.General.OutputRootDirectory);
// Only optimize in release mode
if (!buildContext.General.IsLocalBuild)
{
buildContext.CakeContext.Information($"This is NOT a local build, disabling building of project references");
// Don't build project references (should already be built)
msBuildSettings.WithProperty("BuildProjectReferences", "false");
//InjectAssemblySearchPathsInProjectFile(buildContext, projectName, GetProjectFileName(buildContext, projectName));
}
else
{
buildContext.CakeContext.Information($"This is a local build, not disabling building of project references");
}
// Continuous integration build
msBuildSettings.ContinuousIntegrationBuild = true;
//msBuildSettings.WithProperty("ContinuousIntegrationBuild", "true");
// No NuGet restore (should already be done)
msBuildSettings.WithProperty("ResolveNuGetPackages", "false");
//msBuildSettings.Restore = false;
// Solution info
// msBuildSettings.WithProperty("SolutionFileName", System.IO.Path.GetFileName(buildContext.General.Solution.FileName));
// msBuildSettings.WithProperty("SolutionPath", System.IO.Path.GetFullPath(buildContext.General.Solution.FileName));
// msBuildSettings.WithProperty("SolutionDir", System.IO.Path.GetFullPath(buildContext.General.Solution.Directory));
// msBuildSettings.WithProperty("SolutionName", buildContext.General.Solution.Name);
// msBuildSettings.WithProperty("SolutionExt", ".sln");
// msBuildSettings.WithProperty("DefineExplicitDefaults", "true");
// Disable copyright info
msBuildSettings.NoLogo = true;
// Use as much CPU as possible
msBuildSettings.MaxCpuCount = 0;
// Enable for file logging
if (buildContext.General.EnableMsBuildFileLog)
{
msBuildSettings.AddFileLogger(new MSBuildFileLoggerSettings
{
Verbosity = msBuildSettings.Verbosity,
//Verbosity = Verbosity.Diagnostic,
LogFile = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}_log.log", projectName, action))
});
}
// Enable for bin logging
if (buildContext.General.EnableMsBuildBinaryLog)
{
msBuildSettings.BinaryLogger = new MSBuildBinaryLoggerSettings
{
Enabled = true,
Imports = MSBuildBinaryLoggerImports.Embed,
FileName = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}.binlog", projectName, action))
};
// Note: this only works for direct .net core msbuild usage, not when this is
// being wrapped in a tool (such as 'dotnet pack')
var binLogArgs = string.Format("-bl:\"{0}\";ProjectImports=Embed",
System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}_log.binlog", projectName, action)));
msBuildSettings.ArgumentCustomization = args => args.Append(binLogArgs);
}
}
//-------------------------------------------------------------
private static void RunMsBuild(BuildContext buildContext, string projectName, string projectFileName, MSBuildSettings msBuildSettings, string action)
{
// IMPORTANT NOTE --- READ <=============================================
//
// Note:
// - Binary logger outputs version 9, but the binlog reader only supports up to 8
// - Xml logger only seems to read warnings
//
// IMPORTANT NOTE --- READ <=============================================
var totalStopwatch = Stopwatch.StartNew();
var buildStopwatch = Stopwatch.StartNew();
// Enforce additional logging for issues
//var logPath = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}_log.binlog", projectName, action));
buildContext.CakeContext.CreateDirectory(buildContext.General.OutputRootDirectory);
var logPath = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, string.Format(@"MsBuild_{0}_{1}_log.xml", projectName, action));
if (buildContext.General.EnableMsBuildXmlLog)
{
msBuildSettings.WithLogger(buildContext.CakeContext.Tools.Resolve("MSBuild.ExtensionPack.Loggers.dll").FullPath,
"XmlFileLogger", $"logfile=\"{logPath}\";verbosity=Detailed;encoding=UTF-8");
}
var failBuild = false;
try
{
// using (buildContext.CakeContext.UseDiagnosticVerbosity())
// {
buildContext.CakeContext.MSBuild(projectFileName, msBuildSettings);
//}
}
catch (System.Exception)
{
// Accept for now, we will throw later
failBuild = true;
}
buildContext.CakeContext.Information(string.Empty);
buildContext.CakeContext.Information($"Done {action}ing project, took '{buildStopwatch.Elapsed}'");
buildContext.CakeContext.Information(string.Empty);
if (buildContext.General.EnableMsBuildXmlLog &&
System.IO.File.Exists(logPath))
{
buildContext.CakeContext.Information($"Investigating potential issues using '{logPath}'");
buildContext.CakeContext.Information(string.Empty);
var investigationStopwatch = Stopwatch.StartNew();
var issuesContext = buildContext.CakeContext.MsBuildIssuesFromFilePath(logPath, buildContext.CakeContext.MsBuildXmlFileLoggerFormat());
//var issuesContext = buildContext.CakeContext.MsBuildIssuesFromFilePath(logPath, buildContext.CakeContext.MsBuildBinaryLogFileFormat());
buildContext.CakeContext.Debug("Created issue context");
var issues = buildContext.CakeContext.ReadIssues(issuesContext, buildContext.General.RootDirectory);
buildContext.CakeContext.Debug($"Found '{issues.Count()}' potential issues");
buildContext.CakeContext.Information(string.Empty);
var loggedIssues = new HashSet<string>();
foreach (var issue in issues)
{
var priority = issue.Priority ?? 0;
var message = $"{issue.AffectedFileRelativePath}({issue.Line},{issue.Column}): {issue.Rule}: {issue.MessageText}";
if (loggedIssues.Contains(message))
{
continue;
}
//buildContext.CakeContext.Information($"[{issue.Priority}] {message}");
if (priority == (int)IssuePriority.Warning)
{
buildContext.CakeContext.Warning($"WARNING: {message}");
loggedIssues.Add(message);
}
else if (priority == (int)IssuePriority.Error)
{
buildContext.CakeContext.Error($"ERROR: {message}");
loggedIssues.Add(message);
failBuild = true;
}
buildContext.CakeContext.Information(string.Empty);
buildContext.CakeContext.Information($"Done investigating project, took '{investigationStopwatch.Elapsed}'");
buildContext.CakeContext.Information(string.Empty);
}
buildContext.CakeContext.Information($"Done investigating project, took '{investigationStopwatch.Elapsed}'");
buildContext.CakeContext.Information($"Total msbuild ({action} + investigation) took '{totalStopwatch.Elapsed}'");
buildContext.CakeContext.Information(string.Empty);
}
if (failBuild)
{
buildContext.CakeContext.Information(string.Empty);
throw new Exception($"{action} failed for project '{projectName}'");
}
}
//-------------------------------------------------------------
private static string FindLatestWindowsKitsDirectory(BuildContext buildContext)
{
// Find highest number with 10.0, e.g. 'C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\makeappx.exe'
var directories = buildContext.CakeContext.GetDirectories($@"{Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)}/Windows Kits/10/bin/10.0.*");
//buildContext.CakeContext.Information($"Found '{directories.Count}' potential directories for MakeAppX.exe");
var directory = directories.LastOrDefault();
if (directory != null)
{
return directory.FullPath;
}
return null;
}
//-------------------------------------------------------------
private static string GetVisualStudioDirectory(BuildContext buildContext, bool? allowVsPrerelease = null)
{
// TODO: Support different editions (e.g. Professional, Enterprise, Community, etc)
// Force 64-bit, even when running as 32-bit process
var programFilesx64 = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
var programFilesx86 = Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%");
var prereleasePaths = new List<KeyValuePair<string, string>>(new []
{
new KeyValuePair<string, string>("Visual Studio 2022 Preview", $@"{programFilesx64}\Microsoft Visual Studio\2022\Preview\"),
new KeyValuePair<string, string>("Visual Studio 2019 Preview", $@"{programFilesx86}\Microsoft Visual Studio\2019\Preview\"),
});
var normalPaths = new List<KeyValuePair<string, string>> (new []
{
new KeyValuePair<string, string>("Visual Studio 2022 Enterprise", $@"{programFilesx64}\Microsoft Visual Studio\2022\Enterprise\"),
new KeyValuePair<string, string>("Visual Studio 2022 Professional", $@"{programFilesx64}\Microsoft Visual Studio\2022\Professional\"),
new KeyValuePair<string, string>("Visual Studio 2022 Community", $@"{programFilesx64}\Microsoft Visual Studio\2022\Community\"),
new KeyValuePair<string, string>("Visual Studio 2019 Enterprise", $@"{programFilesx86}\Microsoft Visual Studio\2019\Enterprise\"),
new KeyValuePair<string, string>("Visual Studio 2019 Professional", $@"{programFilesx86}\Microsoft Visual Studio\2019\Professional\"),
new KeyValuePair<string, string>("Visual Studio 2019 Community", $@"{programFilesx86}\Microsoft Visual Studio\2019\Community\"),
});
// Prerelease paths
if ((allowVsPrerelease ?? true) && buildContext.General.UseVisualStudioPrerelease)
{
buildContext.CakeContext.Debug("Checking for installation of Visual Studio (preview)");
foreach (var prereleasePath in prereleasePaths)
{
if (System.IO.Directory.Exists(prereleasePath.Value))
{
buildContext.CakeContext.Debug($"Found {prereleasePath.Key}");
return prereleasePath.Value;
}
}
}
buildContext.CakeContext.Debug("Checking for installation of Visual Studio (non-preview)");
// Normal paths
foreach (var normalPath in normalPaths)
{
if (System.IO.Directory.Exists(normalPath.Value))
{
buildContext.CakeContext.Debug($"Found {normalPath.Key}");
return normalPath.Value;
}
}
// Fallback in case someone *only* has prerelease
foreach (var prereleasePath in prereleasePaths)
{
if (System.IO.Directory.Exists(prereleasePath.Value))
{
buildContext.CakeContext.Information($"Only Visual Studio preview is available, using {prereleasePath.Key}");
return prereleasePath.Value;
}
}
// Failed
return null;
}
//-------------------------------------------------------------
private static string GetVisualStudioPath(BuildContext buildContext, bool? allowVsPrerelease = null)
{
var potentialPaths = new []
{
@"MSBuild\Current\Bin\msbuild.exe",
@"MSBuild\17.0\Bin\msbuild.exe",
@"MSBuild\16.0\Bin\msbuild.exe",
@"MSBuild\15.0\Bin\msbuild.exe"
};
var directory = GetVisualStudioDirectory(buildContext, allowVsPrerelease);
foreach (var potentialPath in potentialPaths)
{
var pathToCheck = System.IO.Path.Combine(directory, potentialPath);
if (System.IO.File.Exists(pathToCheck))
{
return pathToCheck;
}
}
throw new Exception("Could not find the path to Visual Studio (msbuild.exe)");
}
//-------------------------------------------------------------
private static void InjectAssemblySearchPathsInProjectFile(BuildContext buildContext, string projectName, string projectFileName)
{
try
{
// Allow this project to find any other projects that we have built (since we disabled
// building of project dependencies)
var assemblySearchPaths = new List<string>();
var separator = System.IO.Path.DirectorySeparatorChar.ToString();
foreach (var project in buildContext.AllProjects)
{
var projectOutputDirectory = GetProjectOutputDirectory(buildContext, project);
assemblySearchPaths.Add(projectOutputDirectory);
}
if (assemblySearchPaths.Count == 0)
{
buildContext.CakeContext.Information("No assembly search paths found to inject");
return;
}
// For SourceLink to work, the .csproj should contain something like this:
// <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="all" />
var projectFileContents = System.IO.File.ReadAllText(projectFileName);
if (projectFileContents.Contains("AssemblySearchPaths"))
{
buildContext.CakeContext.Information("Assembly search paths is already added to the project file");
return;
}
buildContext.CakeContext.Information("Injecting assembly search paths into project file");
var xmlDocument = XDocument.Parse(projectFileContents);
var projectElement = xmlDocument.Root;
// Item group with package reference
var propertyGroupElement = new XElement("PropertyGroup");
var assemblySearchPathsElement = new XElement("AssemblySearchPaths");
assemblySearchPathsElement.Value = $"$(AssemblySearchPaths);{string.Join(";", assemblySearchPaths)}";
propertyGroupElement.Add(assemblySearchPathsElement);
projectElement.Add(propertyGroupElement);
xmlDocument.Save(projectFileName);
}
catch (Exception ex)
{
buildContext.CakeContext.Error($"Failed to process assembly search paths for project '{projectFileName}': {ex.Message}");
}
}