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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmQtAutoGenGlobalInitializer.h"
#include "cmQtAutoGen.h"
#include "cmQtAutoGenInitializer.h"
#include "cmAlgorithms.h"
#include "cmCustomCommandLines.h"
#include "cmDuration.h"
#include "cmGeneratorTarget.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmProcessOutput.h"
#include "cmState.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
#include <memory>
#include <utility>
cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
std::vector<cmLocalGenerator*> const& localGenerators)
{
for (cmLocalGenerator* localGen : localGenerators) {
// Detect global autogen and autorcc target names
bool globalAutoGenTarget = false;
bool globalAutoRccTarget = false;
{
cmMakefile* makefile = localGen->GetMakefile();
// Detect global autogen target name
if (cmSystemTools::IsOn(
makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET"))) {
std::string targetName =
makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET_NAME");
if (targetName.empty()) {
targetName = "autogen";
}
GlobalAutoGenTargets_.emplace(localGen, std::move(targetName));
globalAutoGenTarget = true;
}
// Detect global autorcc target name
if (cmSystemTools::IsOn(
makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET"))) {
std::string targetName =
makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET_NAME");
if (targetName.empty()) {
targetName = "autorcc";
}
GlobalAutoRccTargets_.emplace(localGen, std::move(targetName));
globalAutoRccTarget = true;
}
}
// Find targets that require AUTOMOC/UIC/RCC processing
for (cmGeneratorTarget* target : localGen->GetGeneratorTargets()) {
// Process only certain target types
switch (target->GetType()) {
case cmStateEnums::EXECUTABLE:
case cmStateEnums::STATIC_LIBRARY:
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::OBJECT_LIBRARY:
// Process target
break;
default:
// Don't process target
continue;
}
if (target->IsImported()) {
// Don't process target
continue;
}
bool const moc = target->GetPropertyAsBool("AUTOMOC");
bool const uic = target->GetPropertyAsBool("AUTOUIC");
bool const rcc = target->GetPropertyAsBool("AUTORCC");
if (moc || uic || rcc) {
std::string const mocExec =
target->GetSafeProperty("AUTOMOC_EXECUTABLE");
std::string const uicExec =
target->GetSafeProperty("AUTOUIC_EXECUTABLE");
std::string const rccExec =
target->GetSafeProperty("AUTORCC_EXECUTABLE");
// We support Qt4, Qt5 and Qt6
auto qtVersion = cmQtAutoGenInitializer::GetQtVersion(target);
bool const validQt = (qtVersion.first.Major == 4) ||
(qtVersion.first.Major == 5) || (qtVersion.first.Major == 6);
bool const mocAvailable = (validQt || !mocExec.empty());
bool const uicAvailable = (validQt || !uicExec.empty());
bool const rccAvailable = (validQt || !rccExec.empty());
bool const mocIsValid = (moc && mocAvailable);
bool const uicIsValid = (uic && uicAvailable);
bool const rccIsValid = (rcc && rccAvailable);
// Disabled AUTOMOC/UIC/RCC warning
bool const mocDisabled = (moc && !mocAvailable);
bool const uicDisabled = (uic && !uicAvailable);
bool const rccDisabled = (rcc && !rccAvailable);
if (mocDisabled || uicDisabled || rccDisabled) {
std::string msg = "AUTOGEN: No valid Qt version found for target ";
msg += target->GetName();
msg += ". ";
msg += cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled);
msg += " disabled. Consider adding:\n";
{
std::string version = (qtVersion.second == 0)
? std::string("<QTVERSION>")
: std::to_string(qtVersion.second);
std::string comp = uicDisabled ? "Widgets" : "Core";
msg += " find_package(Qt";
msg += version;
msg += " COMPONENTS ";
msg += comp;
msg += ")\n";
}
msg += "to your CMakeLists.txt file.";
target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
}
if (mocIsValid || uicIsValid || rccIsValid) {
// Create autogen target initializer
Initializers_.emplace_back(cm::make_unique<cmQtAutoGenInitializer>(
this, target, qtVersion.first, mocIsValid, uicIsValid, rccIsValid,
globalAutoGenTarget, globalAutoRccTarget));
}
}
}
}
}
cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer() = default;
void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
cmLocalGenerator* localGen, std::string const& name,
std::string const& comment)
{
// Test if the target already exists
if (localGen->FindGeneratorTargetToUse(name) == nullptr) {
cmMakefile* makefile = localGen->GetMakefile();
// Create utility target
cmTarget* target = makefile->AddUtilityCommand(
name, cmMakefile::TargetOrigin::Generator, true,
makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
std::vector<std::string>() /*output*/,
std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
comment.c_str());
localGen->AddGeneratorTarget(new cmGeneratorTarget(target, localGen));
// Set FOLDER property in the target
{
char const* folder =
makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
if (folder != nullptr) {
target->SetProperty("FOLDER", folder);
}
}
}
}
void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
cmLocalGenerator* localGen, std::string const& targetName)
{
auto it = GlobalAutoGenTargets_.find(localGen);
if (it != GlobalAutoGenTargets_.end()) {
cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
if (target != nullptr) {
target->Target->AddUtility(targetName, localGen->GetMakefile());
}
}
}
void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
cmLocalGenerator* localGen, std::string const& targetName)
{
auto it = GlobalAutoRccTargets_.find(localGen);
if (it != GlobalAutoRccTargets_.end()) {
cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
if (target != nullptr) {
target->Target->AddUtility(targetName, localGen->GetMakefile());
}
}
}
bool cmQtAutoGenGlobalInitializer::GetExecutableTestOutput(
std::string const& generator, std::string const& executable,
std::string& error, std::string* output)
{
// Check if we have cached output
{
auto it = this->ExecutableTestOutputs_.find(executable);
if (it != this->ExecutableTestOutputs_.end()) {
// Return output on demand
if (output != nullptr) {
*output = it->second;
}
return true;
}
}
// Check if the executable exists
if (!cmSystemTools::FileExists(executable, true)) {
error = "The \"";
error += generator;
error += "\" executable ";
error += cmQtAutoGen::Quoted(executable);
error += " does not exist.";
return false;
}
// Test the executable
std::string stdOut;
{
std::string stdErr;
std::vector<std::string> command;
command.push_back(executable);
command.emplace_back("-h");
int retVal = 0;
const bool runResult = cmSystemTools::RunSingleCommand(
command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
cmDuration::zero(), cmProcessOutput::Auto);
if (!runResult) {
error = "Test run of \"";
error += generator;
error += "\" executable ";
error += cmQtAutoGen::Quoted(executable) + " failed.\n";
error += cmQtAutoGen::QuotedCommand(command);
error += "\n";
error += stdOut;
error += "\n";
error += stdErr;
return false;
}
}
// Return executable output on demand
if (output != nullptr) {
*output = stdOut;
}
// Register executable and output
this->ExecutableTestOutputs_.emplace(executable, std::move(stdOut));
return true;
}
bool cmQtAutoGenGlobalInitializer::generate()
{
return (InitializeCustomTargets() && SetupCustomTargets());
}
bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
{
// Initialize global autogen targets
{
std::string const comment = "Global AUTOGEN target";
for (auto const& pair : GlobalAutoGenTargets_) {
GetOrCreateGlobalTarget(pair.first, pair.second, comment);
}
}
// Initialize global autorcc targets
{
std::string const comment = "Global AUTORCC target";
for (auto const& pair : GlobalAutoRccTargets_) {
GetOrCreateGlobalTarget(pair.first, pair.second, comment);
}
}
// Initialize per target autogen targets
for (auto& initializer : Initializers_) {
if (!initializer->InitCustomTargets()) {
return false;
}
}
return true;
}
bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
{
for (auto& initializer : Initializers_) {
if (!initializer->SetupCustomTargets()) {
return false;
}
}
return true;
}
|