aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/QMakeConf.cs
blob: 2dcefe6ffa6e3d07ff02cc761da6d015eb62dc40 (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
// 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.IO;

namespace QtVsTools.Core
{
    public class QMakeConf
    {
        private Dictionary<string, string> Properties { get; } = new();
        public string this[string name] => Properties.TryGetValue(name, out var value) ? value : null;

        public string QMakeSpecDirectory { get; }

        public QMakeConf(QtBuildToolQuery query)
        {
            var qtPrefix = query["QT_INSTALL_PREFIX"];
            if (string.IsNullOrEmpty(qtPrefix))
                throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_PREFIX");
            var qtArchData = query["QT_INSTALL_ARCHDATA"];
            if (string.IsNullOrEmpty(qtArchData))
                throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_ARCHDATA");
            var qmakeXSpec = query["QMAKE_XSPEC"];
            if (string.IsNullOrEmpty(qtArchData))
                throw new KeyNotFoundException("qmake error: no value for QMAKE_XSPEC");
            QMakeSpecDirectory = Path.Combine(qtPrefix, qtArchData, "mkspecs", qmakeXSpec);
            var qmakeConf = Path.Combine(QMakeSpecDirectory, "qmake.conf");
            if (!File.Exists(qmakeConf)) {
                // Check if this is a shadow build of Qt.
                qtPrefix = query["QT_INSTALL_PREFIX/src"];
                if (string.IsNullOrEmpty(qtPrefix))
                    throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_PREFIX/src");
                qtArchData = query["QT_INSTALL_ARCHDATA/src"];
                if (string.IsNullOrEmpty(qtArchData))
                    throw new KeyNotFoundException("qmake error: no value for QT_INSTALL_ARCHDATA/src");
                QMakeSpecDirectory = Path.Combine(qtPrefix, qtArchData, "mkspecs", qmakeXSpec);
                qmakeConf = Path.Combine(QMakeSpecDirectory, "qmake.conf");
            }
            if (!File.Exists(qmakeConf))
                throw new FileNotFoundException("qmake.conf expected at " + qmakeConf + " not found");
            ParseFile(qmakeConf);
        }

        private void ParseFile(string fileName)
        {
            if (!File.Exists(fileName))
                return;
            using 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)
                    ProcessKeyValue(line, pos);
                else if (line.StartsWith("include", StringComparison.Ordinal))
                    ProcessInclude(fileName, line);
            }
            streamReader.Close();
            RemoveValue("QMAKE_LFLAGS_CONSOLE", "@QMAKE_SUBSYSTEM_SUFFIX@");
            RemoveValue("QMAKE_LFLAGS_WINDOWS", "@QMAKE_SUBSYSTEM_SUFFIX@");
        }

        private void ProcessKeyValue(string line, int pos)
        {
            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());

            switch (op) {
            case "+=" when Properties.ContainsKey(lineKey):
                Properties[lineKey] += lineValue;
                break;
            case "-=":
                foreach (var value in lineValue.Split(' ', '\t'))
                    RemoveValue(lineKey, value);
                break;
            default:
                Properties[lineKey] = lineValue;
                break;
            }
        }

        private void ProcessInclude(string fileName, string line)
        {
            var pos = line.IndexOf('(');
            var posEnd = line.LastIndexOf(')');
            if (pos <= 0 || pos >= posEnd)
                return;
            var filenameToInclude = line.Substring(pos + 1, posEnd - pos - 1);
            var saveCurrentDir = Environment.CurrentDirectory;
            Environment.CurrentDirectory = new FileInfo(fileName).Directory?.FullName ?? "";
            var fileInfoToInclude = new FileInfo(filenameToInclude);
            if (fileInfoToInclude.Exists)
                ParseFile(fileInfoToInclude.FullName);
            Environment.CurrentDirectory = saveCurrentDir;
        }

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

                // at the moment no handling of qmake internal variables
                if (value[startPos] != '[') {
                    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);
                        if (!Properties.TryGetValue(varName, out var varValue))
                            varValue = "";
                        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)
        {
            if (!Properties.TryGetValue(key, out var value))
                return;

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