cmQTWrapCPPCommand.cxx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <utility>
  5. #include <cm/memory>
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmExecutionStatus.h"
  9. #include "cmMakefile.h"
  10. #include "cmRange.h"
  11. #include "cmSourceFile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. bool cmQTWrapCPPCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. if (args.size() < 3) {
  18. status.SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. cmMakefile& mf = status.GetMakefile();
  22. // Get the moc executable to run in the custom command.
  23. std::string const& moc_exe = mf.GetRequiredDefinition("QT_MOC_EXECUTABLE");
  24. // Get the variable holding the list of sources.
  25. std::string const& sourceList = args[1];
  26. std::string sourceListValue = mf.GetSafeDefinition(sourceList);
  27. // Create a rule for all sources listed.
  28. for (std::string const& arg : cmMakeRange(args).advance(2)) {
  29. cmSourceFile* curr = mf.GetSource(arg);
  30. // if we should wrap the class
  31. if (!(curr && curr->GetPropertyAsBool("WRAP_EXCLUDE"))) {
  32. // Compute the name of the file to generate.
  33. std::string srcName =
  34. cmSystemTools::GetFilenameWithoutLastExtension(arg);
  35. std::string newName =
  36. cmStrCat(mf.GetCurrentBinaryDirectory(), "/moc_", srcName, ".cxx");
  37. cmSourceFile* sf = mf.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 = mf.GetCurrentBinaryDirectory();
  48. } else {
  49. hname = mf.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. cmCustomCommandLines commandLines =
  61. cmMakeSingleCommandLine({ moc_exe, "-o", newName, hname });
  62. std::vector<std::string> depends;
  63. depends.push_back(moc_exe);
  64. depends.push_back(hname);
  65. std::string no_main_dependency;
  66. auto cc = cm::make_unique<cmCustomCommand>();
  67. cc->SetOutputs(newName);
  68. cc->SetDepends(depends);
  69. cc->SetCommandLines(commandLines);
  70. cc->SetComment("Qt Wrapped File");
  71. mf.AddCustomCommandToOutput(no_main_dependency, std::move(cc));
  72. }
  73. }
  74. // Store the final list of source files.
  75. mf.AddDefinition(sourceList, sourceListValue);
  76. return true;
  77. }