/src/CMake/Source/cmQTWrapCPPCommand.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmQTWrapCPPCommand.h" |
4 | | |
5 | | #include <utility> |
6 | | |
7 | | #include <cm/memory> |
8 | | #include <cm/string_view> |
9 | | |
10 | | #include "cmCustomCommand.h" |
11 | | #include "cmCustomCommandLines.h" |
12 | | #include "cmExecutionStatus.h" |
13 | | #include "cmMakefile.h" |
14 | | #include "cmRange.h" |
15 | | #include "cmSourceFile.h" |
16 | | #include "cmStringAlgorithms.h" |
17 | | #include "cmSystemTools.h" |
18 | | |
19 | | bool cmQTWrapCPPCommand(std::vector<std::string> const& args, |
20 | | cmExecutionStatus& status) |
21 | 0 | { |
22 | 0 | if (args.size() < 3) { |
23 | 0 | status.SetError("called with incorrect number of arguments"); |
24 | 0 | return false; |
25 | 0 | } |
26 | | |
27 | 0 | cmMakefile& mf = status.GetMakefile(); |
28 | | |
29 | | // Get the moc executable to run in the custom command. |
30 | 0 | std::string const& moc_exe = mf.GetRequiredDefinition("QT_MOC_EXECUTABLE"); |
31 | | |
32 | | // Get the variable holding the list of sources. |
33 | 0 | std::string const& sourceList = args[1]; |
34 | 0 | std::string sourceListValue = mf.GetSafeDefinition(sourceList); |
35 | | |
36 | | // Create a rule for all sources listed. |
37 | 0 | for (std::string const& arg : cmMakeRange(args).advance(2)) { |
38 | 0 | cmSourceFile* curr = mf.GetSource(arg); |
39 | | // if we should wrap the class |
40 | 0 | if (!(curr && curr->GetPropertyAsBool("WRAP_EXCLUDE"))) { |
41 | | // Compute the name of the file to generate. |
42 | 0 | std::string srcName = |
43 | 0 | cmSystemTools::GetFilenameWithoutLastExtension(arg); |
44 | 0 | std::string newName = |
45 | 0 | cmStrCat(mf.GetCurrentBinaryDirectory(), "/moc_", srcName, ".cxx"); |
46 | 0 | cmSourceFile* sf = mf.GetOrCreateSource(newName, true); |
47 | 0 | sf->SetSpecialSourceType( |
48 | 0 | cmSourceFile::SpecialSourceType::QtWrapCppSource); |
49 | 0 | if (curr) { |
50 | 0 | sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT")); |
51 | 0 | } |
52 | | |
53 | | // Compute the name of the header from which to generate the file. |
54 | 0 | std::string hname; |
55 | 0 | if (cmSystemTools::FileIsFullPath(arg)) { |
56 | 0 | hname = arg; |
57 | 0 | } else { |
58 | 0 | if (curr && curr->GetIsGenerated()) { |
59 | 0 | hname = mf.GetCurrentBinaryDirectory(); |
60 | 0 | } else { |
61 | 0 | hname = mf.GetCurrentSourceDirectory(); |
62 | 0 | } |
63 | 0 | hname += "/"; |
64 | 0 | hname += arg; |
65 | 0 | } |
66 | | |
67 | | // Append the generated source file to the list. |
68 | 0 | if (!sourceListValue.empty()) { |
69 | 0 | sourceListValue += ";"; |
70 | 0 | } |
71 | 0 | sourceListValue += newName; |
72 | | |
73 | | // Create the custom command to generate the file. |
74 | 0 | cmCustomCommandLines commandLines = |
75 | 0 | cmMakeSingleCommandLine({ moc_exe, "-o", newName, hname }); |
76 | |
|
77 | 0 | std::vector<std::string> depends; |
78 | 0 | depends.push_back(moc_exe); |
79 | 0 | depends.push_back(hname); |
80 | |
|
81 | 0 | auto cc = cm::make_unique<cmCustomCommand>(); |
82 | 0 | cc->SetOutputs(newName); |
83 | 0 | cc->SetDepends(depends); |
84 | 0 | cc->SetCommandLines(commandLines); |
85 | 0 | cc->SetComment("Qt Wrapped File"); |
86 | 0 | mf.AddCustomCommandToOutput(std::move(cc)); |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | | // Store the final list of source files. |
91 | 0 | mf.AddDefinition(sourceList, sourceListValue); |
92 | 0 | return true; |
93 | 0 | } |