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
|
/***************************************************************************************************
Copyright (C) 2024 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;
using System.Collections.Generic;
using System.IO;
namespace QtVsTools.Core
{
public class QMakeConf
{
public Hashtable Entries { get; }
public string QMakeSpecDirectory { get; }
public QMakeConf(string qtVersionDir, QMakeQuery qmakeQuery = null)
{
Entries = new Hashtable();
QMakeSpecDirectory = Path.Combine(qtVersionDir, "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)) {
qmakeQuery ??= new QMakeQuery(qtVersionDir);
string qtPrefix = qmakeQuery["QT_INSTALL_PREFIX"];
if (string.IsNullOrEmpty(qtPrefix))
throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_PREFIX");
string qtArchData = qmakeQuery["QT_INSTALL_ARCHDATA"];
if (string.IsNullOrEmpty(qtArchData))
throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_ARCHDATA");
string qmakeXSpec = qmakeQuery["QMAKE_XSPEC"];
if (string.IsNullOrEmpty(qtArchData))
throw new KeyNotFoundException("qmake error: no value for QMAKE_XSPEC");
qmakeConf = Path.Combine(qtPrefix, qtArchData, "mkspecs", qmakeXSpec, "qmake.conf");
if (!File.Exists(qmakeConf)) {
// Check if this is a shadow build of Qt.
qtPrefix = qmakeQuery["QT_INSTALL_PREFIX/src"];
if (string.IsNullOrEmpty(qtPrefix))
throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_PREFIX/src");
qtArchData = qmakeQuery["QT_INSTALL_ARCHDATA/src"];
if (string.IsNullOrEmpty(qtArchData))
throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_ARCHDATA/src");
qmakeConf = Path.Combine(qtPrefix, qtArchData, "mkspecs", qmakeXSpec, "qmake.conf");
}
if (!File.Exists(qmakeConf))
throw new KeyNotFoundException("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);
while (streamReader.ReadLine() is {} line) {
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;
}
}
}
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;
}
}
}
|