aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/MsBuild/MsBuildProject.Build.cs
blob: 94be79a4b9bcf76f7d23e92cfd962ee278f0e69c (plain)
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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TaskStatusCenter;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.VCProjectEngine;

using Task = System.Threading.Tasks.Task;
using Thread = System.Threading.Thread;

namespace QtVsTools.Core.MsBuild
{
    using Options;
    using VisualStudio;
    using static QtVsTools.Common.EnumExt;

    public partial class MsBuildProject
    {
        private struct QueueItem
        {
            public VCProject VcProject;
            public string SolutionPath;
            public UnconfiguredProject UnconfiguredProject;
            public ConfiguredProject ConfiguredProject;
            public Dictionary<string, string> Properties;
            public List<string> Targets;
            public LoggerVerbosity LoggerVerbosity;
        }

        private enum Target
        {
            // Mark project as dirty, but do not request a build
            [String("QtVsTools.QtMsBuild.QtProjectBuild.Target.SetOutdated")] SetOutdated
        }

        private static PunisherQueue<QueueItem> BuildQueue => StaticLazy.Get(() =>
            BuildQueue, () => new PunisherQueue<QueueItem>(
                getItemKey: build => build.ConfiguredProject));

        private static ConcurrentStopwatch RequestTimer => StaticLazy.Get(() =>
            RequestTimer, () => new ConcurrentStopwatch());

        private static JoinableTask BuildDispatcher { get; set; }

        public void StartBuild(
            string configurationName,
            Dictionary<string, string> properties,
            IEnumerable<string> targets,
            LoggerVerbosity verbosity = LoggerVerbosity.Quiet)
        {
            _ = Task.Run(() => StartBuildAsync(configurationName, properties, targets, verbosity));
        }

        public async Task StartBuildAsync(
            string configurationName,
            Dictionary<string, string> properties,
            IEnumerable<string> targets,
            LoggerVerbosity verbosity)
        {
            if (string.IsNullOrEmpty(configurationName))
                throw new ArgumentException("Configuration cannot be null.");

            if (!QtOptionsPage.ProjectTracking)
                return;
            if (QtOptionsPage.BuildDebugInformation) {
                Messages.Print($"{DateTime.Now:HH:mm:ss.FFF} "
                    + $"QtProjectBuild({Thread.CurrentThread.ManagedThreadId}): "
                    + $"Request [{configurationName}] {VcProjectPath}");
            }

            RequestTimer.Restart();
            await Initialized;

            var service = UnconfiguredProject.Services.ProjectConfigurationsService;
            if (service == null)
                return;
            var knownConfigs = await service.GetKnownProjectConfigurationsAsync();

            ConfiguredProject configuredProject = null;
            foreach (var config in knownConfigs) {
                var configProject = await UnconfiguredProject
                    .LoadConfiguredProjectAsync(config);
                if (configProject.ProjectConfiguration.Name != configurationName)
                    continue;
                configuredProject = configProject;
                break;
            }

            if (configuredProject == null)
                throw new ArgumentException($"Unknown configuration '{configurationName}'.");

            BuildQueue.Enqueue(new QueueItem
            {
                VcProject = VcProject,
                SolutionPath = SolutionPath,
                UnconfiguredProject = UnconfiguredProject,
                ConfiguredProject = configuredProject,
                Properties = properties?.ToDictionary(x => x.Key, x => x.Value),
                Targets = targets?.ToList(),
                LoggerVerbosity = verbosity
            });

            if (VsServiceProvider.Instance is AsyncPackage package) {
                StaticThreadSafeInit(() => BuildDispatcher,
                    () => BuildDispatcher = package.JoinableTaskFactory.RunAsync(BuildDispatcherLoopAsync))
                    .FileAndForget("QtVsTools/MsBuildProject/StartBuildAsync");
            } else {
                Messages.Print("Could not run Qt/MSBuild build dispatcher loop.");
            }
        }

        public async Task SetOutdatedAsync(
            string configurationName,
            LoggerVerbosity verbosity = LoggerVerbosity.Quiet)
        {
            await StartBuildAsync(
                configurationName,
                null,
                new[] { Target.SetOutdated.Cast<string>() },
                verbosity);
        }

        private static async Task BuildDispatcherLoopAsync()
        {
            ITaskHandler2 dispatchStatus = null;
            while (!VsShellUtilities.ShutdownToken.IsCancellationRequested) {
                while (BuildQueue.IsEmpty || RequestTimer.ElapsedMilliseconds < 1000) {
                    if (BuildQueue.IsEmpty && dispatchStatus != null) {
                        dispatchStatus.Dismiss();
                        dispatchStatus = null;
                    }
                    await Task.Delay(100, VsShellUtilities.ShutdownToken);
                }
                if (BuildQueue.TryDequeue(out var buildRequest)) {
                    var progressData = new TaskProgressData
                    {
                        ProgressText = "Refreshing IntelliSense data, "
                            + $"{BuildQueue.Count} project(s) remaining...",
                        CanBeCanceled = true
                    };
                    if (dispatchStatus == null) {
                        dispatchStatus = StatusCenter.PreRegister(
                            new TaskHandlerOptions
                            {
                                Title = "Qt VS Tools"
                            },
                            progressData
                        ) as ITaskHandler2;
                        dispatchStatus?.RegisterTask(new Task(() =>
                            throw new InvalidOperationException()));
                    } else {
                        dispatchStatus.Progress.Report(progressData);
                    }
                    await BuildAsync(buildRequest);
                }
                if (BuildQueue.IsEmpty
                    || dispatchStatus?.UserCancellation.IsCancellationRequested == true) {
                    if (dispatchStatus != null) {
                        dispatchStatus.Dismiss();
                        dispatchStatus = null;
                    }
                    BuildQueue.Clear();
                }
            }
        }

        private static async Task<bool> BuildProjectAsync(ProjectWriteLockReleaser writeAccess,
            QueueItem item)
        {
            var msBuildProject = await writeAccess.GetProjectAsync(item.ConfiguredProject);

            if (item.Targets.Any(t => t == Target.SetOutdated.Cast<string>())) {
                msBuildProject.MarkDirty();
                await writeAccess.ReleaseAsync();
                return true;
            }

            var solutionPath = item.SolutionPath;
            var configProps = new Dictionary<string, string>(
                item.ConfiguredProject.ProjectConfiguration.Dimensions.ToImmutableDictionary())
                {
                    { "SolutionPath", solutionPath },
                    { "SolutionFileName", Path.GetFileName(solutionPath) },
                    { "SolutionName", Path.GetFileNameWithoutExtension(solutionPath) },
                    { "SolutionExt", Path.GetExtension(solutionPath) },
                    { "SolutionDir", Path.GetDirectoryName(solutionPath)
                        ?.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar }
                };

            foreach (var property in item.Properties)
                configProps[property.Key] = property.Value;

            var projectInstance = new ProjectInstance(msBuildProject.Xml,
                configProps, null, new ProjectCollection());

            var loggerVerbosity = item.LoggerVerbosity;
            if (QtOptionsPage.BuildDebugInformation)
                loggerVerbosity = QtOptionsPage.BuildLoggerVerbosity;

            var buildParams = new BuildParameters
            {
                Loggers = loggerVerbosity != LoggerVerbosity.Quiet
                        ? new[] { new MsBuildProjectLogger { Verbosity = loggerVerbosity } }
                        : null
            };

            var buildRequest = new BuildRequestData(projectInstance,
                item.Targets.ToArray(),
                hostServices: null,
                flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);

            if (QtOptionsPage.BuildDebugInformation) {
                Messages.Print($"{DateTime.Now:HH:mm:ss.FFF} "
                    + $"QtProjectBuild({Thread.CurrentThread.ManagedThreadId}): "
                    + $"Build [{item.ConfiguredProject.ProjectConfiguration.Name}] "
                    + $"{item.UnconfiguredProject.FullPath}");
                Messages.Print("=== Targets");
                foreach (var target in buildRequest.TargetNames)
                    Messages.Print($"    {target}");
                Messages.Print("=== Properties");
                foreach (var property in item.Properties)
                    Messages.Print($"    {property.Key}={property.Value}");
            }

            BuildResult result = null;
            while (result == null) {
                try {
                    result = BuildManager.DefaultBuildManager.Build(buildParams, buildRequest);
                } catch (InvalidOperationException) {
                    if (QtOptionsPage.BuildDebugInformation) {
                        Messages.Print($"{DateTime.Now:HH:mm:ss.FFF} "
                        + $"QtProjectBuild({Thread.CurrentThread.ManagedThreadId}): "
                        + $"[{item.ConfiguredProject.ProjectConfiguration.Name}] "
                        + "Warning: Another build is in progress; waiting...");
                    }
                    await Task.Delay(3000);
                }
            }

            if (QtOptionsPage.BuildDebugInformation) {
                string resMsg;
                var resInfo = new StringBuilder();
                if (result.OverallResult == BuildResultCode.Success) {
                    resMsg = "Build ok";
                } else {
                    resMsg = "Build FAIL";
                    resInfo.AppendLine("####### Build returned 'Failure' code");
                    if (result.ResultsByTarget != null) {
                        foreach (var tr in result.ResultsByTarget) {
                            var res = tr.Value;
                            if (res.ResultCode != TargetResultCode.Failure)
                                continue;
                            resInfo.AppendFormat("### Target '{0}' FAIL\r\n", tr.Key);
                            if (res.Items is { Length: > 0 }) {
                                resInfo.AppendFormat(
                                    "Items: {0}\r\n", string.Join(", ", res.Items
                                        .Select(it => it.ItemSpec)));
                            }
                            var e = tr.Value?.Exception;
                            if (e != null) {
                                resInfo.AppendFormat(
                                    "Exception: {0}\r\nStacktrace:\r\n{1}\r\n",
                                    e.Message, e.StackTrace);
                            }
                        }
                    }
                }
                Messages.Print($"{DateTime.Now:HH:mm:ss.FFF} "
                    + $"QtProjectBuild({Thread.CurrentThread.ManagedThreadId}): "
                    + $"[{item.ConfiguredProject.ProjectConfiguration.Name}] {resMsg}\r\n{resInfo}");
            }

            var ok = false;
            if (result is { ResultsByTarget: null, OverallResult: BuildResultCode.Success }) {
                Messages.Print($"== {Path.GetFileName(item.UnconfiguredProject.FullPath)}: "
                    + "background build FAILED!");
            } else {
                var checkResults = result.ResultsByTarget
                    .Where(x => item.Targets.Contains(x.Key))
                    .Select(x => x.Value).ToList();
                ok = checkResults.Any()
                    && checkResults.All(x => x.ResultCode == TargetResultCode.Success);
                if (ok)
                    msBuildProject.MarkDirty();
            }
            await writeAccess.ReleaseAsync();
            return ok;
        }

        private static async Task BuildAsync(QueueItem item)
        {
            var path = Path.GetFileNameWithoutExtension(item.UnconfiguredProject.FullPath);

            if (item.LoggerVerbosity != LoggerVerbosity.Quiet) {
                var properties = string.Join("", item.Properties.Select(property =>
                    $"{Environment.NewLine}        {property.Key} = {property.Value}"));
                Messages.Print(
                      $"== {path}: starting build...{Environment.NewLine}"
                    + $"  * Properties: {properties}{Environment.NewLine}"
                    + $"  * Targets: {string.Join(";", item.Targets)}{Environment.NewLine}",
                    clear: !QtOptionsPage.BuildDebugInformation,
                    activate: true);
            }

            var lockService = item.UnconfiguredProject.ProjectService.Services.ProjectLockService;

            var ok = false;
            try {
                var timer = ConcurrentStopwatch.StartNew();
                while (timer.IsRunning) {
                    try {
                        await lockService.WriteLockAsync(
                            async writeAccess =>
                            {
                                ok = await BuildProjectAsync(writeAccess, item);
                            });
                        timer.Stop();
                    } catch (InvalidOperationException) {
                        if (timer.ElapsedMilliseconds >= 5000)
                            throw;
                        await lockService.ReadLockAsync(
                            async readAccess =>
                            {
                                await readAccess.ReleaseAsync();
                            });
                    }
                }

                if (ok) {
                    // Hack: Modify the QtTouchProperty in the project user file to trigger an
                    // update to MOC/UIC/... as well as IntelliSense. Revert it immediately to avoid
                    // changing the project user file.
                    var vcConfigs = item.VcProject.Configurations as IVCCollection;
                    var vcConfig = vcConfigs?.Item(item.ConfiguredProject.ProjectConfiguration.Name)
                        as VCConfiguration;
                    if (vcConfig?.Rules.Item("QtRule10_Settings") is IVCRulePropertyStorage props) {
                        props.SetPropertyValue("QtTouchProperty", " ");
                        props.SetPropertyValue("QtTouchProperty", "");
                    }
                }
            } catch (Exception e) {
                Messages.Print($"{Path.GetFileName(item.UnconfiguredProject.FullPath)}: "
                    + $"background build ERROR: {e.Message}");
            }

            if (item.LoggerVerbosity != LoggerVerbosity.Quiet) {
                Messages.Print(
                    $"{Environment.NewLine}== {path}: build {(ok ? "successful" : "ERROR")}"
                );
            }
        }
    }
}