aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/MocCmdChecker.cs
blob: 8bc8ec4b2b1b6ae7faa7feee494e48c84b7cca9f (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace QtVsTools.Core
{
    class MocCmdChecker
    {
        private readonly Regex backslashRegEx = new Regex(@"\\+\.?\\+");
        private readonly Regex endRegEx = new Regex(@"\\\.?$");

        private string NormalizePath(string path)
        {
            var s = path.ToLower().Trim();
            s = backslashRegEx.Replace(s, "\\");
            s = endRegEx.Replace(s, string.Empty);
            return s;
        }

        public string NewCmdLine(string cmdLine, string includes, string defines,
            string mocOptions, string mocFile, string newPchParameters,
            string outputFile)
        {
            var inputMocFile = ProjectMacros.Path;
            if (HelperFunctions.IsMocFile(outputFile))
                inputMocFile = mocFile;
            var cmds = SplitIntoCommands(cmdLine);
            var mocPos = MocCommandPosition(cmds);
            if (mocPos < 0)
                return null;

            var mocCmd = cmds[mocPos];
            var defs = ExtractDefines(mocCmd);
            var incs = ExtractIncludes(mocCmd);
            var pchParameters = ExtractPCHOptions(mocCmd);
            var newIncludes = ExtractIncludes(includes);
            var newDefines = ExtractDefines(defines);

            var equal = true;
            if (newDefines.Count == defs.Count) {
                foreach (var s in newDefines) {
                    if (defs.Contains(s)) {
                        defs.Remove(s);
                    } else {
                        equal = false;
                        break;
                    }
                }
            } else {
                equal = false;
            }

            equal = equal && newIncludes.Count == incs.Count;
            if (equal) {
                foreach (var s in newIncludes) {
                    if (incs.Contains(s)) {
                        incs.Remove(s);
                    } else {
                        equal = false;
                        break;
                    }
                }
            }

            equal = equal && pchParameters == newPchParameters;
            if (equal)
                return null;

            var newCmdLine = string.Empty;
            for (var i = 0; i < cmds.Length; ++i) {
                if (i == mocPos) {
                    newCmdLine = newCmdLine + "\"" + Resources.moc4Command + "\" "
                        + mocOptions
                        + " \"" + inputMocFile + "\" -o \"" + outputFile + "\""
                        + " " + defines + " " + includes;
                    if (!string.IsNullOrEmpty(newPchParameters) && !newCmdLine.Contains(newPchParameters))
                        newCmdLine += " " + newPchParameters;
                } else {
                    newCmdLine = newCmdLine + cmds[i];
                }
                if (i < cmds.Length - 1 && !newCmdLine.EndsWith("\r\n", StringComparison.Ordinal))
                    newCmdLine = newCmdLine + "\r\n";
            }
            return newCmdLine;
        }

        private static string[] SplitIntoCommands(string cmdLine)
        {
            var cmds = cmdLine.Split(new[] { "&&", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            var res = new string[cmds.Length];
            for (var i = 0; i < cmds.Length; ++i)
                res[i] = cmds[i].Trim();
            return res;
        }

        private static int MocCommandPosition(string[] cmds)
        {
            var res = -1;
            var reg = new Regex(@"(\S*moc.exe|""\S+:\\\.*moc.exe"")");
            for (var i = 0; i < cmds.Length; ++i) {
                var m = reg.Match(cmds[i]);
                if (m.Success)
                    return i;
            }
            return res;
        }

        private static List<string> ExtractDefines(string cmdLine)
        {
            var reg = new Regex(@"-D(\S+)");
            var col = reg.Matches(cmdLine);
            var lst = new List<string>(col.Count);
            for (var i = 0; i < col.Count; ++i)
                lst.Add(col[i].Groups[1].ToString());
            return lst;
        }

        private List<string> ExtractIncludes(string cmdLine)
        {
            var reg = new Regex(@"-I([^\s""]+)|-I""([^""]+)""");
            var col = reg.Matches(cmdLine);
            var lst = new List<string>(col.Count);
            for (var i = 0; i < col.Count; ++i) {
                var s = col[i].Groups[1].ToString();
                if (s.Length != 0)
                    lst.Add(NormalizePath(s));
                else
                    lst.Add(NormalizePath(col[i].Groups[2].ToString()));
            }
            return lst;
        }

        private static string ExtractPCHOptions(string cmdLine)
        {
            var reg = new Regex(@"""-f(\S+)"" ""-f(\S+)");
            var col = reg.Matches(cmdLine);
            if (col.Count != 1)
                return null;
            return col[0].ToString();
        }
    }
}