aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/MsBuild/MsBuildProject.cs
blob: 90e1c759773bb8d8a653588d530812bfe3658062 (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
// 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.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TaskStatusCenter;
using Microsoft.VisualStudio.VCProjectEngine;

using Task = System.Threading.Tasks.Task;

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

    /// <summary>
    /// QtProject holds the Qt specific properties for a Visual Studio project.
    /// There exists at most one QtProject perVCProject. Use QtProject.GetOrAdd
    /// to get the QtProject for a VCProject.
    /// </summary>
    public partial class MsBuildProject : Concurrent<MsBuildProject>
    {
        private static LazyFactory StaticLazy { get; } = new();

        private static ConcurrentDictionary<string, MsBuildProject> Instances => StaticLazy.Get(() =>
            Instances, () => new ConcurrentDictionary<string, MsBuildProject>());

        private static IVsTaskStatusCenterService StatusCenter => StaticLazy.Get(() =>
            StatusCenter, VsServiceProvider
            .GetService<SVsTaskStatusCenterService, IVsTaskStatusCenterService>);

        public static MsBuildProject GetOrAdd(VCProject vcProject)
        {
            if (vcProject == null)
                return null;
            lock (StaticCriticalSection) {
                if (MsBuildProjectFormat.GetVersion(vcProject) >= MsBuildProjectFormat.Version.V3) {
                    if (Instances.TryGetValue(vcProject.ProjectFile, out var project))
                        return project;
                    project = new MsBuildProject(vcProject);
                    Instances[vcProject.ProjectFile] = project;
                    if (QtOptionsPage.ProjectTracking) {
                        InitQueue.Enqueue(project);
                        _ = Task.Run(InitDispatcherLoopAsync);
                    }

                    var configs = (vcProject.Configurations as IVCCollection)
                        ?.OfType<VCConfiguration>() ?? Enumerable.Empty<VCConfiguration>();
                    foreach (var config in configs) {
                        if (config.Rules.Item("QtRule10_Settings") is not
                            IVCRulePropertyStorage props) {
                            continue;
                        }
                        var qtInstall = props.GetEvaluatedPropertyValue("QtInstall");
                        if (!QtVersionManager.VersionExists(qtInstall))
                            ShowUpdateQtInstallationMessage(project);
                    }

                    return project;
                }

                if (MsBuildProjectFormat.GetVersion(vcProject) >= MsBuildProjectFormat.Version.V1)
                    ShowUpdateFormatMessage();

                return null; // ignore old or unknown projects
            }
        }

        public static IReadOnlyCollection<MsBuildProject> GetProjects()
        {
            return new List<MsBuildProject>(Instances.Values).AsReadOnly();
        }

        public static void Remove(string projectPath)
        {
            lock (StaticCriticalSection)
                Instances.TryRemove(projectPath, out _);
        }

        public static void Reset()
        {
            lock (StaticCriticalSection) {
                Instances.Clear();
                InitQueue.Clear();
            }
            CloseUpdateFormatMessage();
            CloseProjectFormatUpdated();
        }

        private MsBuildProject(VCProject vcProject)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            VcProject = vcProject;
            VcProjectPath = vcProject.ProjectFile;
            VcProjectDirectory = vcProject.ProjectDirectory;
            Initialized = new EventWaitHandle(false, EventResetMode.ManualReset);
        }

        public VCProject VcProject { get; }
        public string VcProjectPath { get; }
        public string VcProjectDirectory { get; }

        public string SolutionPath { get; set; } = "";
        public bool IsTracked =>
            QtOptionsPage.ProjectTracking && Instances.ContainsKey(VcProjectPath);

        public string QtVersion
        {
            get
            {
                ThreadHelper.ThrowIfNotOnUIThread();
                return GetPropertyValue("QtInstall");
            }
        }

        public MsBuildProjectFormat.Version FormatVersion
        {
            get
            {
                return MsBuildProjectFormat.GetVersion(VcProject);
            }
        }

        public string InstallPath
        {
            get
            {
                ThreadHelper.ThrowIfNotOnUIThread();
                return QtVersionManager.GetInstallPath(this);
            }
        }

        public VersionInformation VersionInfo
        {
            get
            {
                ThreadHelper.ThrowIfNotOnUIThread();
                return VersionInformation.GetOrAddByName(QtVersion);
            }
        }

        public string GetPropertyValue(string propertyName)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            return VcProject.ActiveConfiguration is { } activeConfiguration
                ? activeConfiguration.GetEvaluatedPropertyValue(propertyName)
                : null;
        }

        /// <summary>
        /// Returns the files specified by the file name from a given project as list of VCFile
        /// objects.
        /// </summary>
        /// <param name="fileName">file name (relative path)</param>
        /// <returns></returns>
        public IEnumerable<VCFile> GetFilesFromProject(string fileName)
        {
            var fi = new FileInfo(HelperFunctions.NormalizeRelativeFilePath(fileName));
            foreach (VCFile f in (IVCCollection)VcProject.Files) {
                if (string.Equals(f.Name, fi.Name, IgnoreCase))
                    yield return f;
            }
        }

        public void MarkAsQtPlugin()
        {
            if (VcProject.Configurations is not IVCCollection configurations)
                return;

            foreach (VCConfiguration config in configurations) {
                if (config.Rules.Item("QtRule10_Settings") is IVCRulePropertyStorage rule)
                    rule.SetPropertyValue("QtPlugin", "true");
            }
        }

        public void EnableActiveQtBuildStep(string version, string defFile = null)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            foreach (VCConfiguration config in (IVCCollection)VcProject.Configurations) {
                if (config.Rules.Item("QtRule80_IDC") is IVCRulePropertyStorage rule) {
                    rule.SetPropertyValue("QtIDC", "true");
                    rule.SetPropertyValue("QtIDCVersion", version);
                }

                var linker = (VCLinkerTool)((IVCCollection)config.Tools).Item("VCLinkerTool");
                var librarian = (VCLibrarianTool)((IVCCollection)config.Tools).Item("VCLibrarianTool");

                if (linker != null) {
                    linker.Version = version;
                    linker.ModuleDefinitionFile = defFile ?? VcProject.Name + ".def";
                } else {
                    librarian.ModuleDefinitionFile = defFile ?? VcProject.Name + ".def";
                }
            }
        }

        public bool UsesPrecompiledHeaders()
        {
            if (VcProject.Configurations is not IVCCollection configurations)
                return false;

            const pchOption pchNone = pchOption.pchNone;
            return configurations.Cast<VCConfiguration>()
                .Select(CompilerToolWrapper.Create)
                .All(compiler => (compiler?.GetUsePrecompiledHeader() ?? pchNone) != pchNone);
        }

        public string GetPrecompiledHeaderThrough()
        {
            if (VcProject.Configurations is not IVCCollection configurations)
                return null;

            return configurations.Cast<VCConfiguration>()
                .Select(CompilerToolWrapper.Create)
                .Select(compiler => compiler?.GetPrecompiledHeaderThrough() ?? "")
                .Where(header => !string.IsNullOrEmpty(header))
                .Select(header => header.ToLower())
                .FirstOrDefault();
        }

        public static void SetPCHOption(VCFile vcFile, pchOption option)
        {
            if (vcFile.FileConfigurations is not IVCCollection fileConfigurations)
                return;

            foreach (VCFileConfiguration config in fileConfigurations)
                CompilerToolWrapper.Create(config)?.SetUsePrecompiledHeader(option);
        }

        public void RemoveGeneratedFiles(string fileName)
        {
            var fi = new FileInfo(fileName);
            var lastIndex = fileName.LastIndexOf(fi.Extension, StringComparison.Ordinal);
            var baseName = fi.Name.Remove(lastIndex, fi.Extension.Length);
            string delName = null;
            if (HelperFunctions.IsHeaderFile(fileName))
                delName = "moc_" + baseName + ".cpp";
            else if (HelperFunctions.IsSourceFile(fileName) && !fileName.StartsWith("moc_", IgnoreCase))
                delName = baseName + ".moc";
            else if (HelperFunctions.IsUicFile(fileName))
                delName = "ui_" + baseName + ".h";
            else if (HelperFunctions.IsQrcFile(fileName))
                delName = "qrc_" + baseName + ".cpp";

            if (delName != null) {
                foreach (var vcFile in GetFilesFromProject(delName))
                    vcFile.DeleteAndRemoveFromFilter(FakeFilter.GeneratedFiles());
            }
        }

        public bool SelectInSolutionExplorer()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (VsServiceProvider.Instance is not IServiceProvider provider)
                return false;
            var explorer = VsShellUtilities.GetUIHierarchyWindow(provider,
                VSConstants.StandardToolWindows.SolutionExplorer);
            var hierarchy = VsShellUtilities.GetHierarchy(provider,
                Guid.Parse(VcProject.ProjectGUID)) as IVsUIHierarchy;

            if (explorer == null || hierarchy == null)
                return false;

            explorer.ExpandItem(hierarchy, VSConstants.VSITEMID_ROOT, EXPANDFLAGS.EXPF_SelectItem);
            var ret = explorer.GetItemState(hierarchy, VSConstants.VSITEMID_ROOT,
                (uint)__VSHIERARCHYITEMSTATE.HIS_Selected, out var state);

            return !ErrorHandler.Failed(ret) && state == (uint)__VSHIERARCHYITEMSTATE.HIS_Selected;
        }
    }
}