aboutsummaryrefslogtreecommitdiffstats
path: root/src/qtprojectlib/QMakeConf.cs
blob: 9c2b90f82ebb42d27482a4bc732877bd3586330f (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
/****************************************************************************
**
** 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;
using System.IO;

namespace QtProjectLib
{
    public class QMakeConf
    {
        public Hashtable Entries { get; private set; }
        public string QMakeSpecDirectory { get; private set; }

        public QMakeConf(VersionInformation versionInfo)
        {
            Entries = new Hashtable();
            QMakeSpecDirectory = Path.Combine(versionInfo.qtDir, "mkspecs", "default");
            var qmakeConf = Path.Combine(QMakeSpecDirectory, "qmake.conf");

            // Starting from Qt5 beta2 there is no more "\\mkspecs\\default" folder available
            // To find location of "qmake.conf" there is a need to run "qmake -query" command
            // This is what happens below.
            if (!File.Exists(qmakeConf)) {
                var qmakeQuery = new QMakeQuery(versionInfo);
                var qmakespecDir = qmakeQuery.query("QMAKE_XSPEC");

                if (qmakeQuery.ErrorValue == 0 && !string.IsNullOrEmpty(qmakespecDir)) {
                    QMakeSpecDirectory = Path.Combine(versionInfo.qtDir, "mkspecs", qmakespecDir);
                    qmakeConf = Path.Combine(QMakeSpecDirectory, "qmake.conf");
                }

                if (qmakeQuery.ErrorValue != 0 || !File.Exists(qmakeConf))
                    throw new QtVSException("qmake.conf expected at " + qmakeConf + " not found");
            }

            ParseFile(qmakeConf);
        }

        private void ParseFile(string filename)
        {
            var fileInfo = new FileInfo(filename);
            if (fileInfo.Exists) {
                var streamReader = new StreamReader(filename);
                var line = streamReader.ReadLine();
                while (line != null) {
                    line = line.Trim();
                    var commentStartIndex = line.IndexOf('#');
                    if (commentStartIndex >= 0)
                        line = line.Remove(commentStartIndex);
                    var pos = line.IndexOf('=');
                    if (pos > 0) {
                        var op = "=";
                        if (line[pos - 1] == '+' || line[pos - 1] == '-')
                            op = line[pos - 1] + "=";

                        var lineKey = line.Substring(0, pos - op.Length + 1).Trim();
                        var lineValue = ExpandVariables(line.Substring(pos + 1).Trim());

                        if (op == "+=") {
                            Entries[lineKey] += " " + lineValue;
                        } else if (op == "-=") {
                            foreach (var remval in lineValue.Split(' ', '\t'))
                                RemoveValue(lineKey, remval);
                        } else {
                            Entries[lineKey] = lineValue;
                        }
                    } else if (line.StartsWith("include", StringComparison.Ordinal)) {
                        pos = line.IndexOf('(');
                        var posEnd = line.LastIndexOf(')');
                        if (pos > 0 && pos < posEnd) {
                            var filenameToInclude = line.Substring(pos + 1, posEnd - pos - 1);
                            var saveCurrentDir = Environment.CurrentDirectory;
                            Environment.CurrentDirectory = fileInfo.Directory.FullName;
                            var fileInfoToInclude = new FileInfo(filenameToInclude);
                            if (fileInfoToInclude.Exists)
                                ParseFile(fileInfoToInclude.FullName);
                            Environment.CurrentDirectory = saveCurrentDir;
                        }
                    }
                    line = streamReader.ReadLine();
                }
                streamReader.Close();

                RemoveValue("QMAKE_LFLAGS_CONSOLE", "@QMAKE_SUBSYSTEM_SUFFIX@");
                RemoveValue("QMAKE_LFLAGS_WINDOWS", "@QMAKE_SUBSYSTEM_SUFFIX@");
            }
        }

        private string ExpandVariables(string value)
        {
            var pos = value.IndexOf("$$", StringComparison.Ordinal);
            while (pos != -1) {
                var startPos = pos + 2;
                var endPos = startPos;

                if (value[startPos] != '[') { // at the moment no handling of qmake internal variables
                    for (; endPos < value.Length; ++endPos) {
                        if ((Char.IsPunctuation(value[endPos]) && value[endPos] != '_')
                            || Char.IsWhiteSpace(value[endPos])) {
                            break;
                        }
                    }
                    if (endPos > startPos) {
                        var varName = value.Substring(startPos, endPos - startPos);
                        var varValue = (Entries[varName] ?? string.Empty).ToString();
                        value = value.Substring(0, pos) + varValue + value.Substring(endPos);
                        endPos = pos + varValue.Length;
                    }
                }

                pos = value.IndexOf("$$", endPos, StringComparison.Ordinal);
            }
            return value;
        }

        private void RemoveValue(string key, string valueToRemove)
        {
            var pos = -1;
            if (!Entries.Contains(key))
                return;

            var value = Entries[key].ToString();
            do {
                pos = value.IndexOf(valueToRemove, StringComparison.Ordinal);
                if (pos >= 0)
                    value = value.Remove(pos, valueToRemove.Length);
            } while (pos >= 0);
            Entries[key] = value;
        }
    }
}