1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmQTWrapCPPCommand.h"
9 #include "cmCustomCommand.h"
10 #include "cmCustomCommandLines.h"
11 #include "cmExecutionStatus.h"
12 #include "cmMakefile.h"
14 #include "cmSourceFile.h"
15 #include "cmStringAlgorithms.h"
16 #include "cmSystemTools.h"
18 bool cmQTWrapCPPCommand(std::vector
<std::string
> const& args
,
19 cmExecutionStatus
& status
)
21 if (args
.size() < 3) {
22 status
.SetError("called with incorrect number of arguments");
26 cmMakefile
& mf
= status
.GetMakefile();
28 // Get the moc executable to run in the custom command.
29 std::string
const& moc_exe
= mf
.GetRequiredDefinition("QT_MOC_EXECUTABLE");
31 // Get the variable holding the list of sources.
32 std::string
const& sourceList
= args
[1];
33 std::string sourceListValue
= mf
.GetSafeDefinition(sourceList
);
35 // Create a rule for all sources listed.
36 for (std::string
const& arg
: cmMakeRange(args
).advance(2)) {
37 cmSourceFile
* curr
= mf
.GetSource(arg
);
38 // if we should wrap the class
39 if (!(curr
&& curr
->GetPropertyAsBool("WRAP_EXCLUDE"))) {
40 // Compute the name of the file to generate.
42 cmSystemTools::GetFilenameWithoutLastExtension(arg
);
44 cmStrCat(mf
.GetCurrentBinaryDirectory(), "/moc_", srcName
, ".cxx");
45 cmSourceFile
* sf
= mf
.GetOrCreateSource(newName
, true);
47 sf
->SetProperty("ABSTRACT", curr
->GetProperty("ABSTRACT"));
50 // Compute the name of the header from which to generate the file.
52 if (cmSystemTools::FileIsFullPath(arg
)) {
55 if (curr
&& curr
->GetIsGenerated()) {
56 hname
= mf
.GetCurrentBinaryDirectory();
58 hname
= mf
.GetCurrentSourceDirectory();
64 // Append the generated source file to the list.
65 if (!sourceListValue
.empty()) {
66 sourceListValue
+= ";";
68 sourceListValue
+= newName
;
70 // Create the custom command to generate the file.
71 cmCustomCommandLines commandLines
=
72 cmMakeSingleCommandLine({ moc_exe
, "-o", newName
, hname
});
74 std::vector
<std::string
> depends
;
75 depends
.push_back(moc_exe
);
76 depends
.push_back(hname
);
78 auto cc
= cm::make_unique
<cmCustomCommand
>();
79 cc
->SetOutputs(newName
);
80 cc
->SetDepends(depends
);
81 cc
->SetCommandLines(commandLines
);
82 cc
->SetComment("Qt Wrapped File");
83 mf
.AddCustomCommandToOutput(std::move(cc
));
87 // Store the final list of source files.
88 mf
.AddDefinition(sourceList
, sourceListValue
);