1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "projectgenerator.h"
5#include "option.h"
6#include <qdatetime.h>
7#include <qdir.h>
8#include <qfile.h>
9#include <qfileinfo.h>
10#include <qregularexpression.h>
11
12QT_BEGIN_NAMESPACE
13
14static QString project_builtin_regx() //calculate the builtin regular expression..
15{
16 QString ret;
17 QStringList builtin_exts;
18 builtin_exts << Option::c_ext << Option::ui_ext << Option::yacc_ext << Option::lex_ext << ".ts" << ".xlf" << ".qrc";
19 builtin_exts += Option::h_ext + Option::cpp_ext;
20 for(int i = 0; i < builtin_exts.size(); ++i) {
21 if(!ret.isEmpty())
22 ret += "; ";
23 ret += QString("*") + builtin_exts[i];
24 }
25 return ret;
26}
27
28void
29ProjectGenerator::init()
30{
31 int file_count = 0;
32 verifyCompilers();
33
34 project->loadSpec();
35 project->evaluateFeatureFile(fileName: "default_pre.prf");
36 project->evaluateFeatureFile(fileName: "default_post.prf");
37 project->evaluateConfigFeatures();
38 project->values(v: "CONFIG").clear();
39 Option::postProcessProject(project);
40
41 ProValueMap &v = project->variables();
42 QString templ = Option::globals->user_template.isEmpty() ? QString("app") : Option::globals->user_template;
43 if (!Option::globals->user_template_prefix.isEmpty())
44 templ.prepend(s: Option::globals->user_template_prefix);
45 v["TEMPLATE_ASSIGN"] += templ;
46
47 //the scary stuff
48 if(project->first(variableName: "TEMPLATE_ASSIGN") != "subdirs") {
49 QString builtin_regex = project_builtin_regx();
50 QStringList dirs = Option::projfile::project_dirs;
51 if(Option::projfile::do_pwd) {
52 if(!v["INCLUDEPATH"].contains(str: "."))
53 v["INCLUDEPATH"] += ".";
54 dirs.prepend(t: qmake_getpwd());
55 }
56
57 for(int i = 0; i < dirs.size(); ++i) {
58 QString dir, regex, pd = dirs.at(i);
59 bool add_depend = false;
60 if(exists(file: pd)) {
61 QFileInfo fi(fileInfo(file: pd));
62 if(fi.isDir()) {
63 dir = pd;
64 add_depend = true;
65 if(dir.right(n: 1) != Option::dir_sep)
66 dir += Option::dir_sep;
67 if (Option::recursive) {
68 QStringList files = QDir(dir).entryList(filters: QDir::Files);
69 for (int i = 0; i < files.size(); i++)
70 dirs.append(t: dir + files[i] + QDir::separator() + builtin_regex);
71 }
72 regex = builtin_regex;
73 } else {
74 QString file = pd;
75 int s = file.lastIndexOf(s: Option::dir_sep);
76 if(s != -1)
77 dir = file.left(n: s+1);
78 if(addFile(file)) {
79 add_depend = true;
80 file_count++;
81 }
82 }
83 } else { //regexp
84 regex = pd;
85 }
86 if(!regex.isEmpty()) {
87 int s = regex.lastIndexOf(s: Option::dir_sep);
88 if(s != -1) {
89 dir = regex.left(n: s+1);
90 regex = regex.right(n: regex.size() - (s+1));
91 }
92 const QDir d(dir);
93 if (Option::recursive) {
94 QStringList entries = d.entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot);
95 for (int i = 0; i < entries.size(); i++)
96 dirs.append(t: dir + entries[i] + QDir::separator() + regex);
97 }
98 QStringList files = d.entryList(nameFilters: QDir::nameFiltersFromString(nameFilter: regex));
99 for(int i = 0; i < (int)files.size(); i++) {
100 QString file = d.absoluteFilePath(fileName: files[i]);
101 if (addFile(file)) {
102 add_depend = true;
103 file_count++;
104 }
105 }
106 }
107 if(add_depend && !dir.isEmpty() && !v["DEPENDPATH"].contains(str: dir, cs: Qt::CaseInsensitive)) {
108 QFileInfo fi(fileInfo(file: dir));
109 if(fi.absoluteFilePath() != qmake_getpwd())
110 v["DEPENDPATH"] += fileFixify(file: dir);
111 }
112 }
113 }
114 if(!file_count) { //shall we try a subdir?
115 QStringList knownDirs = Option::projfile::project_dirs;
116 if(Option::projfile::do_pwd)
117 knownDirs.prepend(t: ".");
118 const QString out_file = fileFixify(file: Option::output.fileName());
119 for(int i = 0; i < knownDirs.size(); ++i) {
120 QString pd = knownDirs.at(i);
121 if(exists(file: pd)) {
122 QString newdir = pd;
123 QFileInfo fi(fileInfo(file: newdir));
124 if(fi.isDir()) {
125 newdir = fileFixify(file: newdir, fix: FileFixifyFromOutdir);
126 ProStringList &subdirs = v["SUBDIRS"];
127 if(exists(file: fi.filePath() + QDir::separator() + fi.fileName() + Option::pro_ext) &&
128 !subdirs.contains(str: newdir, cs: Qt::CaseInsensitive)) {
129 subdirs.append(t: newdir);
130 } else {
131 QStringList profiles = QDir(newdir).entryList(nameFilters: QStringList("*" + Option::pro_ext), filters: QDir::Files);
132 for(int i = 0; i < (int)profiles.size(); i++) {
133 QString nd = newdir;
134 if(nd == ".")
135 nd = "";
136 else if (!nd.isEmpty() && !nd.endsWith(c: QDir::separator()))
137 nd += QDir::separator();
138 nd += profiles[i];
139 fileFixify(file: nd);
140 if (!subdirs.contains(str: nd, cs: Qt::CaseInsensitive) && !out_file.endsWith(s: nd))
141 subdirs.append(t: nd);
142 }
143 }
144 if (Option::recursive) {
145 QStringList dirs = QDir(newdir).entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot);
146 for(int i = 0; i < (int)dirs.size(); i++) {
147 QString nd = fileFixify(file: newdir + QDir::separator() + dirs[i]);
148 if (!knownDirs.contains(str: nd, cs: Qt::CaseInsensitive))
149 knownDirs.append(t: nd);
150 }
151 }
152 }
153 } else { //regexp
154 QString regx = pd, dir;
155 int s = regx.lastIndexOf(s: Option::dir_sep);
156 if(s != -1) {
157 dir = regx.left(n: s+1);
158 regx = regx.right(n: regx.size() - (s+1));
159 }
160 QStringList files = QDir(dir).entryList(nameFilters: QDir::nameFiltersFromString(nameFilter: regx),
161 filters: QDir::Dirs | QDir::NoDotAndDotDot);
162 ProStringList &subdirs = v["SUBDIRS"];
163 for(int i = 0; i < (int)files.size(); i++) {
164 QString newdir(dir + files[i]);
165 QFileInfo fi(fileInfo(file: newdir));
166 {
167 newdir = fileFixify(file: newdir);
168 if(exists(file: fi.filePath() + QDir::separator() + fi.fileName() + Option::pro_ext) &&
169 !subdirs.contains(str: newdir)) {
170 subdirs.append(t: newdir);
171 } else {
172 QStringList profiles = QDir(newdir).entryList(nameFilters: QStringList("*" + Option::pro_ext), filters: QDir::Files);
173 for(int i = 0; i < (int)profiles.size(); i++) {
174 QString nd = newdir + QDir::separator() + files[i];
175 fileFixify(file: nd);
176 if(files[i] != "." && files[i] != ".." && !subdirs.contains(str: nd, cs: Qt::CaseInsensitive)) {
177 if(newdir + files[i] != Option::output_dir + Option::output.fileName())
178 subdirs.append(t: nd);
179 }
180 }
181 }
182 if (Option::recursive && !knownDirs.contains(str: newdir, cs: Qt::CaseInsensitive))
183 knownDirs.append(t: newdir);
184 }
185 }
186 }
187 }
188 v["TEMPLATE_ASSIGN"] = ProStringList("subdirs");
189 return;
190 }
191
192 //setup deplist
193 QList<QMakeLocalFileName> deplist;
194 {
195 const ProStringList &d = v["DEPENDPATH"];
196 for(int i = 0; i < d.size(); ++i)
197 deplist.append(t: QMakeLocalFileName(d[i].toQString()));
198 }
199 setDependencyPaths(deplist);
200
201 ProStringList &h = v["HEADERS"];
202 bool no_qt_files = true;
203 static const char *srcs[] = { "SOURCES", "YACCSOURCES", "LEXSOURCES", "FORMS", nullptr };
204 for (int i = 0; srcs[i]; i++) {
205 const ProStringList &l = v[srcs[i]];
206 QMakeSourceFileInfo::SourceFileType type = QMakeSourceFileInfo::TYPE_C;
207 QMakeSourceFileInfo::addSourceFiles(l, seek: QMakeSourceFileInfo::SEEK_DEPS, type);
208 for(int i = 0; i < l.size(); ++i) {
209 QStringList tmp = QMakeSourceFileInfo::dependencies(file: l.at(i).toQString());
210 if(!tmp.isEmpty()) {
211 for(int dep_it = 0; dep_it < tmp.size(); ++dep_it) {
212 QString dep = tmp[dep_it];
213 dep = fixPathToQmake(file: dep);
214 QString file_dir = dep.section(in_sep: Option::dir_sep, start: 0, end: -2),
215 file_no_path = dep.section(in_sep: Option::dir_sep, start: -1);
216 if(!file_dir.isEmpty()) {
217 for(int inc_it = 0; inc_it < deplist.size(); ++inc_it) {
218 QMakeLocalFileName inc = deplist[inc_it];
219 if(inc.local() == file_dir && !v["INCLUDEPATH"].contains(str: inc.real(), cs: Qt::CaseInsensitive))
220 v["INCLUDEPATH"] += inc.real();
221 }
222 }
223 if (no_qt_files && file_no_path.contains(re: QRegularExpression("^q[a-z_0-9].h$")))
224 no_qt_files = false;
225 QString h_ext;
226 for(int hit = 0; hit < Option::h_ext.size(); ++hit) {
227 if(dep.endsWith(s: Option::h_ext.at(i: hit))) {
228 h_ext = Option::h_ext.at(i: hit);
229 break;
230 }
231 }
232 if(!h_ext.isEmpty()) {
233 for(int cppit = 0; cppit < Option::cpp_ext.size(); ++cppit) {
234 QString src(dep.left(n: dep.size() - h_ext.size()) +
235 Option::cpp_ext.at(i: cppit));
236 if(exists(file: src)) {
237 ProStringList &srcl = v["SOURCES"];
238 if(!srcl.contains(str: src, cs: Qt::CaseInsensitive))
239 srcl.append(t: src);
240 }
241 }
242 } else if(dep.endsWith(s: Option::lex_ext) &&
243 file_no_path.startsWith(s: Option::lex_mod)) {
244 addConfig("lex_included");
245 }
246 if(!h.contains(str: dep, cs: Qt::CaseInsensitive))
247 h += dep;
248 }
249 }
250 }
251 }
252
253 //strip out files that are actually output from internal compilers (ie temporary files)
254 const ProStringList &quc = project->values(v: "QMAKE_EXTRA_COMPILERS");
255 for (ProStringList::ConstIterator it = quc.begin(); it != quc.end(); ++it) {
256 QString tmp_out = project->first(variableName: ProKey(*it + ".output")).toQString();
257 if(tmp_out.isEmpty())
258 continue;
259
260 ProStringList var_out = project->values(v: ProKey(*it + ".variable_out"));
261 bool defaults = var_out.isEmpty();
262 for(int i = 0; i < var_out.size(); ++i) {
263 ProString v = var_out.at(i);
264 if(v.startsWith(sub: "GENERATED_")) {
265 defaults = true;
266 break;
267 }
268 }
269 if(defaults) {
270 var_out << "SOURCES";
271 var_out << "HEADERS";
272 var_out << "FORMS";
273 }
274 const ProStringList &tmp = project->values(v: ProKey(*it + ".input"));
275 for (ProStringList::ConstIterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
276 ProStringList &inputs = project->values(v: (*it2).toKey());
277 for (ProStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
278 QString path = replaceExtraCompilerVariables(val: tmp_out, in: (*input).toQString(), out: QString(), forShell: NoShell);
279 path = fixPathToQmake(file: path).section(asep: '/', astart: -1);
280 for(int i = 0; i < var_out.size(); ++i) {
281 ProString v = var_out.at(i);
282 ProStringList &list = project->values(v: v.toKey());
283 for(int src = 0; src < list.size(); ) {
284 if(list[src] == path || list[src].endsWith(str: "/" + path))
285 list.removeAt(idx: src);
286 else
287 ++src;
288 }
289 }
290 }
291 }
292 }
293}
294
295bool
296ProjectGenerator::writeMakefile(QTextStream &t)
297{
298 t << "######################################################################" << Qt::endl;
299 t << "# Automatically generated by qmake (" QMAKE_VERSION_STR ") " << QDateTime::currentDateTime().toString() << Qt::endl;
300 t << "######################################################################" << Qt::endl << Qt::endl;
301 if (!Option::globals->extra_cmds[QMakeEvalBefore].isEmpty())
302 t << Option::globals->extra_cmds[QMakeEvalBefore] << Qt::endl;
303 t << getWritableVar("TEMPLATE_ASSIGN", fixPath: false);
304 if(project->first(variableName: "TEMPLATE_ASSIGN") == "subdirs") {
305 t << Qt::endl << "# Directories" << "\n"
306 << getWritableVar("SUBDIRS");
307 } else {
308 //figure out target
309 QString ofn = QFileInfo(static_cast<QFile *>(t.device())->fileName()).completeBaseName();
310 if (ofn.isEmpty() || ofn == "-")
311 ofn = "unknown";
312 project->values(v: "TARGET_ASSIGN") = ProStringList(ofn);
313
314 t << getWritableVar("TARGET_ASSIGN")
315 << getWritableVar("CONFIG", fixPath: false)
316 << getWritableVar("CONFIG_REMOVE", fixPath: false)
317 << getWritableVar("INCLUDEPATH") << Qt::endl;
318
319 t << "# You can make your code fail to compile if you use deprecated APIs.\n"
320 "# In order to do so, uncomment the following line.\n"
321 "# Please consult the documentation of the deprecated API in order to know\n"
322 "# how to port your code away from it.\n"
323 "# You can also select to disable deprecated APIs only up to a certain version of Qt.\n"
324 "#DEFINES += QT_DISABLE_DEPRECATED_UP_TO=0x060000 # disables all APIs deprecated in Qt 6.0.0 and earlier\n\n";
325
326 t << "# Input" << "\n";
327 t << getWritableVar("HEADERS")
328 << getWritableVar("FORMS")
329 << getWritableVar("LEXSOURCES")
330 << getWritableVar("YACCSOURCES")
331 << getWritableVar("SOURCES")
332 << getWritableVar("RESOURCES")
333 << getWritableVar("TRANSLATIONS");
334 }
335 if (!Option::globals->extra_cmds[QMakeEvalAfter].isEmpty())
336 t << Option::globals->extra_cmds[QMakeEvalAfter] << Qt::endl;
337 return true;
338}
339
340bool
341ProjectGenerator::addConfig(const QString &cfg, bool add)
342{
343 ProKey where = "CONFIG";
344 if(!add)
345 where = "CONFIG_REMOVE";
346 if (!project->values(v: where).contains(str: cfg)) {
347 project->values(v: where) += cfg;
348 return true;
349 }
350 return false;
351}
352
353bool
354ProjectGenerator::addFile(QString file)
355{
356 file = fileFixify(file, fix: FileFixifyToIndir);
357 QString dir;
358 int s = file.lastIndexOf(s: Option::dir_sep);
359 if(s != -1)
360 dir = file.left(n: s+1);
361 if(file.mid(position: dir.size(), n: Option::h_moc_mod.size()) == Option::h_moc_mod)
362 return false;
363
364 ProKey where;
365 for(int cppit = 0; cppit < Option::cpp_ext.size(); ++cppit) {
366 if(file.endsWith(s: Option::cpp_ext[cppit])) {
367 where = "SOURCES";
368 break;
369 }
370 }
371 if(where.isEmpty()) {
372 for(int hit = 0; hit < Option::h_ext.size(); ++hit)
373 if(file.endsWith(s: Option::h_ext.at(i: hit))) {
374 where = "HEADERS";
375 break;
376 }
377 }
378 if(where.isEmpty()) {
379 for(int cit = 0; cit < Option::c_ext.size(); ++cit) {
380 if(file.endsWith(s: Option::c_ext[cit])) {
381 where = "SOURCES";
382 break;
383 }
384 }
385 }
386 if(where.isEmpty()) {
387 if(file.endsWith(s: Option::ui_ext))
388 where = "FORMS";
389 else if(file.endsWith(s: Option::lex_ext))
390 where = "LEXSOURCES";
391 else if(file.endsWith(s: Option::yacc_ext))
392 where = "YACCSOURCES";
393 else if(file.endsWith(s: ".ts") || file.endsWith(s: ".xlf"))
394 where = "TRANSLATIONS";
395 else if(file.endsWith(s: ".qrc"))
396 where = "RESOURCES";
397 }
398
399 QString newfile = fixPathToQmake(file: fileFixify(file));
400
401 ProStringList &endList = project->values(v: where);
402 if(!endList.contains(str: newfile, cs: Qt::CaseInsensitive)) {
403 endList += newfile;
404 return true;
405 }
406 return false;
407}
408
409QString
410ProjectGenerator::getWritableVar(const char *vk, bool)
411{
412 const ProKey v(vk);
413 ProStringList &vals = project->values(v);
414 if(vals.isEmpty())
415 return "";
416
417 // If values contain spaces, ensure that they are quoted
418 for (ProStringList::iterator it = vals.begin(); it != vals.end(); ++it) {
419 if ((*it).contains(c: ' ') && !(*it).startsWith(c: ' '))
420 *it = "\"" + *it + "\"";
421 }
422
423 QString ret;
424 if(v.endsWith(sub: "_REMOVE"))
425 ret = v.left(len: v.length() - 7) + " -= ";
426 else if(v.endsWith(sub: "_ASSIGN"))
427 ret = v.left(len: v.length() - 7) + " = ";
428 else
429 ret = v + " += ";
430 QString join = vals.join(sep: ' ');
431 if(ret.size() + join.size() > 80) {
432 QString spaces;
433 for(int i = 0; i < ret.size(); i++)
434 spaces += " ";
435 join = vals.join(str: " \\\n" + spaces);
436 }
437 return ret + join + "\n";
438}
439
440bool
441ProjectGenerator::openOutput(QFile &file, const QString &build) const
442{
443 ProString fileName = file.fileName();
444 if (!fileName.endsWith(sub: Option::pro_ext)) {
445 if (fileName.isEmpty())
446 fileName = fileInfo(file: Option::output_dir).fileName();
447 file.setFileName(fileName + Option::pro_ext);
448 }
449 return MakefileGenerator::openOutput(file, build);
450}
451
452
453QString
454ProjectGenerator::fixPathToQmake(const QString &file)
455{
456 QString ret = file;
457 if(Option::dir_sep != QLatin1String("/"))
458 ret.replace(before: Option::dir_sep, after: QLatin1String("/"));
459 return ret;
460}
461
462QT_END_NAMESPACE
463

source code of qtbase/qmake/generators/projectgenerator.cpp