Merge branch 'release-3.29'
[kiteware-cmake.git] / Source / cmQTWrapCPPCommand.cxx
blob58cf5142cc6c099b5083ace93842b27e2d67dfe7
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"
5 #include <utility>
7 #include <cm/memory>
9 #include "cmCustomCommand.h"
10 #include "cmCustomCommandLines.h"
11 #include "cmExecutionStatus.h"
12 #include "cmMakefile.h"
13 #include "cmRange.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");
23 return false;
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.
41 std::string srcName =
42 cmSystemTools::GetFilenameWithoutLastExtension(arg);
43 std::string newName =
44 cmStrCat(mf.GetCurrentBinaryDirectory(), "/moc_", srcName, ".cxx");
45 cmSourceFile* sf = mf.GetOrCreateSource(newName, true);
46 if (curr) {
47 sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT"));
50 // Compute the name of the header from which to generate the file.
51 std::string hname;
52 if (cmSystemTools::FileIsFullPath(arg)) {
53 hname = arg;
54 } else {
55 if (curr && curr->GetIsGenerated()) {
56 hname = mf.GetCurrentBinaryDirectory();
57 } else {
58 hname = mf.GetCurrentSourceDirectory();
60 hname += "/";
61 hname += arg;
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);
89 return true;