cmQTWrapCPPCommand.cxx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmProperty.h"
  8. #include "cmRange.h"
  9. #include "cmSourceFile.h"
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. bool cmQTWrapCPPCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (args.size() < 3) {
  16. status.SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. cmMakefile& mf = status.GetMakefile();
  20. // Get the moc executable to run in the custom command.
  21. std::string const& moc_exe = mf.GetRequiredDefinition("QT_MOC_EXECUTABLE");
  22. // Get the variable holding the list of sources.
  23. std::string const& sourceList = args[1];
  24. std::string sourceListValue = mf.GetSafeDefinition(sourceList);
  25. // Create a rule for all sources listed.
  26. for (std::string const& arg : cmMakeRange(args).advance(2)) {
  27. cmSourceFile* curr = mf.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 =
  34. cmStrCat(mf.GetCurrentBinaryDirectory(), "/moc_", srcName, ".cxx");
  35. cmSourceFile* sf = mf.GetOrCreateSource(newName, true);
  36. if (curr) {
  37. cmProp p = curr->GetProperty("ABSTRACT");
  38. sf->SetProperty("ABSTRACT", cmToCStr(p));
  39. }
  40. // Compute the name of the header from which to generate the file.
  41. std::string hname;
  42. if (cmSystemTools::FileIsFullPath(arg)) {
  43. hname = arg;
  44. } else {
  45. if (curr && curr->GetIsGenerated()) {
  46. hname = mf.GetCurrentBinaryDirectory();
  47. } else {
  48. hname = mf.GetCurrentSourceDirectory();
  49. }
  50. hname += "/";
  51. hname += arg;
  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. cmCustomCommandLines commandLines =
  60. cmMakeSingleCommandLine({ moc_exe, "-o", newName, hname });
  61. std::vector<std::string> depends;
  62. depends.push_back(moc_exe);
  63. depends.push_back(hname);
  64. std::string no_main_dependency;
  65. const char* no_working_dir = nullptr;
  66. mf.AddCustomCommandToOutput(newName, depends, no_main_dependency,
  67. commandLines, "Qt Wrapped File",
  68. no_working_dir);
  69. }
  70. }
  71. // Store the final list of source files.
  72. mf.AddDefinition(sourceList, sourceListValue);
  73. return true;
  74. }