-
Notifications
You must be signed in to change notification settings - Fork 0
/
compileconfig.cpp
229 lines (224 loc) · 6.65 KB
/
compileconfig.cpp
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
#include "compileconfig.h"
#include "ui_compileconfig.h"
#include "global.h"
#include "confighelp.h"
#include <QFileDialog>
#include <QProcess>
#include <QMessageBox>
#include <QJsonArray>
#include <QInputDialog>
QJsonValue CompileConfigure::toJson() const {
QJsonObject obj;
JSON_SET(name);
JSON_SET(gccPath);
JSON_SET(gxxPath);
JSON_SET(gdbPath);
JSON_SET(extraCompile);
JSON_SET(extraLink);
JSON_SET(optimize);
JSON_SET(cstd);
JSON_SET(cxxstd);
JSON_SET(bit);
JSON_SET(warning);
JSON_SET(werror);
JSON_SET(debug);
return obj;
}
void CompileConfigure::fromJson(QJsonValue value) {
QJsonObject obj = value.toObject();
JSON_GET(name);
JSON_GET(gccPath);
JSON_GET(gxxPath);
JSON_GET(gdbPath);
JSON_GET(extraCompile);
JSON_GET(extraLink);
JSON_GET(optimize);
JSON_GET(cstd);
JSON_GET(cxxstd);
JSON_GET(bit);
JSON_GET(warning);
JSON_GET(werror);
JSON_GET(debug);
}
QString CompileConfigure::start(QProcess& proc, const QString& src, Language* language) {
QStringList arg;
QFileInfo si(src);
QString compiler;
arg << src << "-o" << (si.path() + QDir::separator() + si.baseName() + exeSuf);
arg << ("-O" + (QStringList{"0", "1", "2", "3", "fast", "g"}[optimize]));
if (language->test<CLanguage>()) {
arg << ("-std=c" + (QStringList{"90", "99", "11"}[cstd]));
compiler = gccPath;
} else if (language->test<CxxLanguage>()) {
arg << ("-std=c++" + (QStringList{"98", "11", "14", "17"}[cxxstd]));
compiler = gxxPath;
}
arg << ("-m" + (QStringList{"32", "64"}[bit]));
switch (warning) {
case 0:
arg << "-w";
break;
case 2:
arg << "-Wall";
break;
case 3:
arg << "-Wall" << "-Wextra";
break;
default:
;
}
if (werror) {
arg << "-werror";
}
if (debug) {
arg << "-g";
}
arg << extraCompile.split(QRegularExpression(R"(\n)"), QString::SkipEmptyParts);
proc.start(compiler, arg, QIODevice::ReadOnly);
return compiler;
}
CompileConfig::CompileConfig(QList<CompileConfigure> cfg, int cur, QWidget *parent) : QDialog(parent), ui(new Ui::CompileConfig), config(cfg) {
ui->setupUi(this);
connect(ui->ok, &QPushButton::clicked, this, &CompileConfig::accept);
connect(ui->cancel, &QPushButton::clicked, this, &CompileConfig::reject);
connect(ui->configure, QOverload<int>::of(&QComboBox::currentIndexChanged), [&](int idx) {
current = idx;
if (current == -1) {
currentConfig = nullptr;
ui->configure->setEnabled(false);
ui->configDel->setEnabled(false);
ui->configDup->setEnabled(false);
ui->configRen->setEnabled(false);
ui->gccLabel->setEnabled(false);
ui->gccPath->setEnabled(false);
ui->gccBrowse->setEnabled(false);
ui->gdbLabel->setEnabled(false);
ui->gdbPath->setEnabled(false);
ui->gdbBrowse->setEnabled(false);
ui->configTab->setEnabled(false);
} else {
currentConfig = &config[current];
ui->configure->setEnabled(true);
ui->configDel->setEnabled(true);
ui->configDup->setEnabled(true);
ui->configRen->setEnabled(true);
ui->gccLabel->setEnabled(true);
ui->gccPath->setEnabled(true);
ui->gccPath->setText(currentConfig->gccPath);
ui->gccBrowse->setEnabled(true);
ui->gxxLabel->setEnabled(true);
ui->gxxPath->setEnabled(true);
ui->gxxPath->setText(currentConfig->gxxPath);
ui->gxxBrowse->setEnabled(true);
ui->gdbLabel->setEnabled(true);
ui->gdbPath->setEnabled(true);
ui->gdbPath->setText(currentConfig->gdbPath);
ui->gdbBrowse->setEnabled(true);
ui->configTab->setEnabled(true);
ui->extraCompile->clear();
ui->extraCompile->insertPlainText(currentConfig->extraCompile);
ui->extraLink->clear();
ui->extraLink->insertPlainText(currentConfig->extraLink);
ui->optimize->setCurrentIndex(currentConfig->optimize);
ui->cstd->setCurrentIndex(currentConfig->cstd);
ui->cxxstd->setCurrentIndex(currentConfig->cxxstd);
ui->bit->setCurrentIndex(currentConfig->bit);
ui->warning->setCurrentIndex(currentConfig->warning);
ui->werror->setChecked(currentConfig->werror);
ui->debug->setChecked(currentConfig->debug);
}
});
connect(ui->configAdd, &QPushButton::clicked, [&]() {
config.push_back(CompileConfigure());
ui->configure->addItem(config.back().name);
});
connect(ui->configDup, &QPushButton::clicked, [&]() {
config.insert(current, config[current]);
ui->configure->insertItem(current, config[current].name);
});
connect(ui->configDel, &QPushButton::clicked, [&]() {
config.removeAt(current);
ui->configure->removeItem(current);
});
connect(ui->configRen, &QPushButton::clicked, [&]() {
bool ok = false;
QString name = QInputDialog::getText(this, "qdevcpp - 重命名", "输入配置名称:", QLineEdit::Normal, currentConfig->name, &ok);
if (ok) {
currentConfig->name = name;
ui->configure->setItemText(current, name);
}
});
connect(ui->gccBrowse, &QToolButton::clicked, [&]() {
QString path = QFileDialog::getOpenFileName(this, "qdevcpp - 打开gcc",
#ifdef Q_OS_LINUX
"/usr/bin"
#elif defined(Q_OS_WINDOWS)
""
#endif
, "gcc (*gcc" + exeSuf + ")");
if (path != "") {
currentConfig->gccPath = path;
ui->gccPath->setText(path);
}
});
connect(ui->gxxBrowse, &QToolButton::clicked, [&]() {
QString path = QFileDialog::getOpenFileName(this, "qdevcpp - 打开g++",
#ifdef Q_OS_LINUX
"/usr/bin"
#elif defined(Q_OS_WINDOWS)
""
#endif
, "g++ (*g++" + exeSuf + ")");
if (path != "") {
currentConfig->gxxPath = path;
ui->gxxPath->setText(path);
}
});
connect(ui->gdbBrowse, &QToolButton::clicked, [&]() {
QString path = QFileDialog::getOpenFileName(this, "qdevcpp - 打开gdb",
#ifdef Q_OS_LINUX
"/usr/bin"
#elif defined(Q_OS_WINDOWS)
""
#endif
, "gdb (*gdb" + exeSuf + ")");
if (path != "") {
currentConfig->gdbPath = path;
ui->gdbPath->setText(path);
}
});
connect(ui->extraCompile, &QPlainTextEdit::textChanged, [&]() {
currentConfig->extraCompile = ui->extraCompile->toPlainText();
});
connect(ui->extraLink, &QPlainTextEdit::textChanged, [&]() {
currentConfig->extraLink = ui->extraLink->toPlainText();
});
#define CONNECT_ENUM(obj) \
connect(ui->obj, QOverload<int>::of(&QComboBox::currentIndexChanged), [&](int idx) { \
currentConfig->obj = idx; \
})
CONNECT_ENUM(optimize);
CONNECT_ENUM(cstd);
CONNECT_ENUM(cxxstd);
CONNECT_ENUM(bit);
CONNECT_ENUM(warning);
#undef CONNECT_ENUM
#define CONNECT_BOOL(obj) \
connect(ui->obj, &QCheckBox::stateChanged, [&](int state) { \
currentConfig->obj = (state == Qt::Checked); \
})
CONNECT_BOOL(werror);
CONNECT_BOOL(debug);
#undef CONNECT_BOOL
if (config.size()) {
for (const auto& cfg : config) {
ui->configure->addItem(cfg.name);
}
ui->configure->setCurrentIndex(cur);
} else {
current = -1;
}
}
CompileConfig::~CompileConfig() {
delete ui;
}