1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the qmake application of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#ifndef MAKEFILE_H
30#define MAKEFILE_H
31
32#include "option.h"
33#include "project.h"
34#include "makefiledeps.h"
35#include <qtextstream.h>
36#include <qlist.h>
37#include <qhash.h>
38#include <qfileinfo.h>
39#include <functional>
40
41QT_BEGIN_NAMESPACE
42
43#ifdef Q_OS_WIN32
44#define QT_POPEN _popen
45#define QT_POPEN_READ "rb"
46#define QT_PCLOSE _pclose
47#else
48#define QT_POPEN popen
49#define QT_POPEN_READ "r"
50#define QT_PCLOSE pclose
51#endif
52
53struct ReplaceExtraCompilerCacheKey;
54
55class MakefileGenerator : protected QMakeSourceFileInfo
56{
57 QString spec;
58 bool no_io = false;
59 bool resolveDependenciesInFrameworks = false;
60 QHash<QString, bool> init_compiler_already;
61 QString makedir, chkexists;
62 QString fullBuildArgs();
63
64 //internal caches
65 mutable QHash<QString, QMakeLocalFileName> depHeuristicsCache;
66 mutable QHash<QString, QStringList> dependsCache;
67 mutable QHash<ReplaceExtraCompilerCacheKey, QString> extraCompilerVariablesCache;
68
69public:
70 // We can't make it visible to VCFilter in VS2008 except by making it public or directly friending it.
71 enum ReplaceFor { NoShell, LocalShell, TargetShell };
72
73protected:
74 enum TARG_MODE { TARG_UNIX_MODE, TARG_MAC_MODE, TARG_WIN_MODE } target_mode;
75
76 ProStringList createObjectList(const ProStringList &sources);
77
78 //makefile style generator functions
79 void writeObj(QTextStream &, const char *src);
80 void writeInstalls(QTextStream &t, bool noBuild=false);
81 void writeHeader(QTextStream &t);
82 void writeSubDirs(QTextStream &t);
83 void writeMakeQmake(QTextStream &t, bool noDummyQmakeAll = false);
84 void writeExportedVariables(QTextStream &t);
85 void writeExtraVariables(QTextStream &t);
86 void writeExtraTargets(QTextStream &t);
87 QString resolveDependency(const QDir &outDir, const QString &file);
88 void callExtraCompilerDependCommand(const ProString &extraCompiler,
89 const QString &tmp_dep_cmd, const QString &inpf,
90 const QString &tmp_out, bool dep_lines, QStringList *deps,
91 bool existingDepsOnly,
92 bool checkCommandAvailability = false);
93 void writeExtraCompilerTargets(QTextStream &t);
94 void writeExtraCompilerVariables(QTextStream &t);
95 bool writeDummyMakefile(QTextStream &t);
96 virtual bool writeMakefile(QTextStream &t);
97 virtual void writeDefaultVariables(QTextStream &t);
98
99 QString pkgConfigPrefix() const;
100 QString pkgConfigFileName(bool fixify=true);
101 QString pkgConfigFixPath(QString) const;
102 void writePkgConfigFile(); // for pkg-config
103
104 //generating subtarget makefiles
105 struct SubTarget
106 {
107 QString name;
108 QString in_directory, out_directory;
109 QString profile, target, makefile;
110 ProStringList depends;
111 };
112 enum SubTargetFlags {
113 SubTargetInstalls=0x01,
114 SubTargetOrdered=0x02,
115 SubTargetSkipDefaultVariables=0x04,
116 SubTargetSkipDefaultTargets=0x08,
117
118 SubTargetsNoFlags=0x00
119 };
120 QList<MakefileGenerator::SubTarget*> findSubDirsSubTargets() const;
121 void writeSubTargetCall(QTextStream &t,
122 const QString &in_directory, const QString &in, const QString &out_directory, const QString &out,
123 const QString &out_directory_cdin, const QString &makefilein);
124 virtual void writeSubMakeCall(QTextStream &t, const QString &outDirectory_cdin,
125 const QString &makeFileIn);
126 virtual void writeSubTargets(QTextStream &t, QList<SubTarget*> subtargets, int flags);
127 virtual ProStringList extraSubTargetDependencies() { return {}; }
128
129 //extra compiler interface
130 bool verifyExtraCompiler(const ProString &c, const QString &f);
131 virtual QString replaceExtraCompilerVariables(const QString &, const QStringList &, const QStringList &, ReplaceFor forShell);
132 inline QString replaceExtraCompilerVariables(const QString &val, const QString &in, const QString &out, ReplaceFor forShell)
133 { return replaceExtraCompilerVariables(val, QStringList(in), QStringList(out), forShell); }
134
135 //interface to the source file info
136 QMakeLocalFileName fixPathForFile(const QMakeLocalFileName &, bool) override;
137 QMakeLocalFileName findFileForDep(const QMakeLocalFileName &, const QMakeLocalFileName &) override;
138 QFileInfo findFileInfo(const QMakeLocalFileName &) override;
139 QMakeProject *project = nullptr;
140
141 //escape
142 virtual QString escapeFilePath(const QString &path) const = 0;
143 ProString escapeFilePath(const ProString &path) const;
144 template<typename A, typename B>
145 QString escapeFilePath(const QStringBuilder<A, B> &path) const
146 { return escapeFilePath(QString(path)); }
147 QStringList escapeFilePaths(const QStringList &paths) const;
148 ProStringList escapeFilePaths(const ProStringList &paths) const;
149 virtual QString escapeDependencyPath(const QString &path) const;
150 ProString escapeDependencyPath(const ProString &path) const;
151 template<typename A, typename B>
152 QString escapeDependencyPath(const QStringBuilder<A, B> &path) const
153 { return escapeDependencyPath(QString(path)); }
154 QStringList escapeDependencyPaths(const QStringList &paths) const;
155 ProStringList escapeDependencyPaths(const ProStringList &paths) const;
156
157 QStringList finalizeDependencyPaths(const QStringList &paths) const;
158
159 //initialization
160 void verifyCompilers();
161 virtual void init();
162 void initOutPaths();
163 struct Compiler
164 {
165 QString variable_in;
166 enum CompilerFlag {
167 CompilerNoFlags = 0x00,
168 CompilerBuiltin = 0x01,
169 CompilerNoCheckDeps = 0x02,
170 CompilerRemoveNoExist = 0x04,
171 CompilerAddInputsAsMakefileDeps = 0x08
172 };
173 uint flags, type;
174 };
175 friend class QTypeInfo<Compiler>;
176
177 void initCompiler(const Compiler &comp);
178 enum VPATHFlag {
179 VPATH_NoFlag = 0x00,
180 VPATH_WarnMissingFiles = 0x01,
181 VPATH_RemoveMissingFiles = 0x02,
182 VPATH_NoFixify = 0x04
183 };
184 ProStringList findFilesInVPATH(ProStringList l, uchar flags, const QString &var="");
185
186 inline int findExecutable(const QStringList &cmdline)
187 { int ret; canExecute(cmdline, &ret); return ret; }
188 bool canExecute(const QStringList &cmdline, int *argv0) const;
189 inline bool canExecute(const QString &cmdline) const
190 { return canExecute(cmdline.split(' '), nullptr); }
191
192 bool mkdir(const QString &dir) const;
193 QString mkdir_p_asstring(const QString &dir, bool escape=true) const;
194
195 QString specdir();
196
197 //subclasses can use these to query information about how the generator was "run"
198 QString buildArgs(bool withExtra);
199
200 virtual QStringList &findDependencies(const QString &file);
201 virtual bool doDepends() const { return Option::mkfile::do_deps; }
202
203 void filterIncludedFiles(const char *);
204 void processSources() {
205 filterIncludedFiles("SOURCES");
206 filterIncludedFiles("GENERATED_SOURCES");
207 }
208
209 //for installs
210 virtual QString defaultInstall(const QString &);
211 virtual QString installRoot() const;
212
213 //for prl
214 QString prlFileName(bool fixify=true);
215 void writePrlFile();
216 bool processPrlFile(QString &, bool baseOnly);
217 virtual void writePrlFile(QTextStream &);
218
219 //make sure libraries are found
220 virtual bool findLibraries(bool linkPrl, bool mergeLflags);
221
222 //for retrieving values and lists of values
223 virtual QString var(const ProKey &var) const;
224 QString varGlue(const ProKey &var, const QString &before, const QString &glue, const QString &after) const;
225 QString varList(const ProKey &var) const;
226 QString fixFileVarGlue(const ProKey &var, const QString &before, const QString &glue, const QString &after) const;
227 QString fileVarList(const ProKey &var) const;
228 QString fileVarGlue(const ProKey &var, const QString &before, const QString &glue, const QString &after) const;
229 QString fileVar(const ProKey &var) const;
230 QString depVar(const ProKey &var) const;
231 QString val(const ProStringList &varList) const;
232 QString val(const QStringList &varList) const;
233 QString valGlue(const QStringList &varList, const QString &before, const QString &glue, const QString &after) const;
234 QString valGlue(const ProStringList &varList, const QString &before, const QString &glue, const QString &after) const;
235 QString valList(const QStringList &varList) const;
236 QString valList(const ProStringList &varList) const;
237
238 QString filePrefixRoot(const QString &, const QString &);
239
240 enum LibFlagType { LibFlagLib, LibFlagPath, LibFlagFile, LibFlagOther };
241 virtual LibFlagType parseLibFlag(const ProString &flag, ProString *arg);
242 ProStringList fixLibFlags(const ProKey &var);
243 virtual ProString fixLibFlag(const ProString &lib);
244
245public:
246 //file fixification to unify all file names into a single pattern
247 enum FileFixifyType {
248 FileFixifyFromIndir = 0,
249 FileFixifyFromOutdir = 1,
250 FileFixifyToOutDir = 0,
251 FileFixifyToIndir = 2,
252 FileFixifyBackwards = FileFixifyFromOutdir | FileFixifyToIndir,
253 FileFixifyDefault = 0,
254 FileFixifyAbsolute = 4,
255 FileFixifyRelative = 8
256 };
257 Q_DECLARE_FLAGS(FileFixifyTypes, FileFixifyType)
258protected:
259 QString fileFixify(const QString &file, FileFixifyTypes fix = FileFixifyDefault, bool canon = true) const;
260 QStringList fileFixify(const QStringList &files, FileFixifyTypes fix = FileFixifyDefault, bool canon = true) const;
261
262 QString installMetaFile(const ProKey &replace_rule, const QString &src, const QString &dst);
263
264 virtual bool processPrlFileBase(QString &origFile, QStringView origName,
265 QStringView fixedBase, int slashOff);
266 bool processPrlFileCore(QString &origFile, QStringView origName,
267 const QString &fixedFile);
268 QString createResponseFile(const QString &baseName,
269 const ProStringList &objList,
270 const QString &prefix = QString());
271
272public:
273 QMakeProject *projectFile() const;
274 void setProjectFile(QMakeProject *p);
275
276 void setNoIO(bool o);
277 bool noIO() const;
278
279 inline bool exists(QString file) const { return fileInfo(file).exists(); }
280 QFileInfo fileInfo(QString file) const;
281
282 static MakefileGenerator *create(QMakeProject *);
283 virtual bool write();
284 virtual bool writeProjectMakefile();
285 virtual bool supportsMetaBuild() { return true; }
286 virtual bool supportsMergedBuilds() { return false; }
287 virtual bool mergeBuildProject(MakefileGenerator * /*other*/) { return false; }
288 virtual bool openOutput(QFile &, const QString &build) const;
289 bool isWindowsShell() const { return Option::dir_sep == QLatin1String("\\"); }
290 QString shellQuote(const QString &str);
291 virtual ProKey fullTargetVariable() const;
292};
293Q_DECLARE_TYPEINFO(MakefileGenerator::Compiler, Q_MOVABLE_TYPE);
294Q_DECLARE_OPERATORS_FOR_FLAGS(MakefileGenerator::FileFixifyTypes)
295
296inline void MakefileGenerator::setNoIO(bool o)
297{ no_io = o; }
298
299inline bool MakefileGenerator::noIO() const
300{ return no_io; }
301
302inline QString MakefileGenerator::defaultInstall(const QString &)
303{ return QString(""); }
304
305inline QString MakefileGenerator::installRoot() const
306{ return QStringLiteral("$(INSTALL_ROOT)"); }
307
308inline bool MakefileGenerator::findLibraries(bool, bool)
309{ return true; }
310
311struct ReplaceExtraCompilerCacheKey
312{
313 mutable size_t hash;
314 QString var, in, out, pwd;
315 MakefileGenerator::ReplaceFor forShell;
316 ReplaceExtraCompilerCacheKey(const QString &v, const QStringList &i, const QStringList &o, MakefileGenerator::ReplaceFor s);
317 bool operator==(const ReplaceExtraCompilerCacheKey &f) const;
318 inline size_t hashCode() const {
319 if (!hash)
320 hash = (size_t)forShell ^ qHash(var) ^ qHash(in) ^ qHash(out) /*^ qHash(pwd)*/;
321 return hash;
322 }
323};
324inline size_t qHash(const ReplaceExtraCompilerCacheKey &f) { return f.hashCode(); }
325
326QT_END_NAMESPACE
327
328#endif // MAKEFILE_H
329