cmQTWrapCPPCommand.cxx 3.0 KB

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