cmQTWrapCPPCommand.cxx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #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. sf->SetSpecialSourceType(
  39. cmSourceFile::SpecialSourceType::QtWrapCppSource);
  40. if (curr) {
  41. sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT"));
  42. }
  43. // Compute the name of the header from which to generate the file.
  44. std::string hname;
  45. if (cmSystemTools::FileIsFullPath(arg)) {
  46. hname = arg;
  47. } else {
  48. if (curr && curr->GetIsGenerated()) {
  49. hname = mf.GetCurrentBinaryDirectory();
  50. } else {
  51. hname = mf.GetCurrentSourceDirectory();
  52. }
  53. hname += "/";
  54. hname += arg;
  55. }
  56. // Append the generated source file to the list.
  57. if (!sourceListValue.empty()) {
  58. sourceListValue += ";";
  59. }
  60. sourceListValue += newName;
  61. // Create the custom command to generate the file.
  62. cmCustomCommandLines commandLines =
  63. cmMakeSingleCommandLine({ moc_exe, "-o", newName, hname });
  64. std::vector<std::string> depends;
  65. depends.push_back(moc_exe);
  66. depends.push_back(hname);
  67. auto cc = cm::make_unique<cmCustomCommand>();
  68. cc->SetOutputs(newName);
  69. cc->SetDepends(depends);
  70. cc->SetCommandLines(commandLines);
  71. cc->SetComment("Qt Wrapped File");
  72. mf.AddCustomCommandToOutput(std::move(cc));
  73. }
  74. }
  75. // Store the final list of source files.
  76. mf.AddDefinition(sourceList, sourceListValue);
  77. return true;
  78. }