cmQTWrapCPPCommand.cxx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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"
  4. #include "cmCustomCommandLines.h"
  5. #include "cmMakefile.h"
  6. #include "cmRange.h"
  7. #include "cmSourceFile.h"
  8. #include "cmSystemTools.h"
  9. #include <utility>
  10. class cmExecutionStatus;
  11. // cmQTWrapCPPCommand
  12. bool cmQTWrapCPPCommand::InitialPass(std::vector<std::string> const& args,
  13. cmExecutionStatus&)
  14. {
  15. if (args.size() < 3) {
  16. this->SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. // Get the moc executable to run in the custom command.
  20. std::string const& moc_exe =
  21. this->Makefile->GetRequiredDefinition("QT_MOC_EXECUTABLE");
  22. // Get the variable holding the list of sources.
  23. std::string const& sourceList = args[1];
  24. std::string sourceListValue = this->Makefile->GetSafeDefinition(sourceList);
  25. // Create a rule for all sources listed.
  26. for (std::string const& arg : cmMakeRange(args).advance(2)) {
  27. cmSourceFile* curr = this->Makefile->GetSource(arg);
  28. // if we should wrap the class
  29. if (!(curr && curr->GetPropertyAsBool("WRAP_EXCLUDE"))) {
  30. // Compute the name of the file to generate.
  31. std::string srcName =
  32. cmSystemTools::GetFilenameWithoutLastExtension(arg);
  33. std::string newName = this->Makefile->GetCurrentBinaryDirectory();
  34. newName += "/moc_";
  35. newName += srcName;
  36. newName += ".cxx";
  37. cmSourceFile* sf = this->Makefile->GetOrCreateSource(newName, true);
  38. if (curr) {
  39. sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT"));
  40. }
  41. // Compute the name of the header from which to generate the file.
  42. std::string hname;
  43. if (cmSystemTools::FileIsFullPath(arg)) {
  44. hname = arg;
  45. } else {
  46. if (curr && curr->GetIsGenerated()) {
  47. hname = this->Makefile->GetCurrentBinaryDirectory();
  48. } else {
  49. hname = this->Makefile->GetCurrentSourceDirectory();
  50. }
  51. hname += "/";
  52. hname += arg;
  53. }
  54. // Append the generated source file to the list.
  55. if (!sourceListValue.empty()) {
  56. sourceListValue += ";";
  57. }
  58. sourceListValue += newName;
  59. // Create the custom command to generate the file.
  60. cmCustomCommandLine commandLine;
  61. commandLine.push_back(moc_exe);
  62. commandLine.push_back("-o");
  63. commandLine.push_back(newName);
  64. commandLine.push_back(hname);
  65. cmCustomCommandLines commandLines;
  66. commandLines.push_back(std::move(commandLine));
  67. std::vector<std::string> depends;
  68. depends.push_back(moc_exe);
  69. depends.push_back(hname);
  70. std::string no_main_dependency;
  71. const char* no_working_dir = nullptr;
  72. this->Makefile->AddCustomCommandToOutput(
  73. newName, depends, no_main_dependency, commandLines, "Qt Wrapped File",
  74. no_working_dir);
  75. }
  76. }
  77. // Store the final list of source files.
  78. this->Makefile->AddDefinition(sourceList, sourceListValue.c_str());
  79. return true;
  80. }