cmQTWrapCPPCommand.cxx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "cmPolicies.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. sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT"));
  38. }
  39. // Compute the name of the header from which to generate the file.
  40. std::string hname;
  41. if (cmSystemTools::FileIsFullPath(arg)) {
  42. hname = arg;
  43. } else {
  44. if (curr && curr->GetIsGenerated()) {
  45. hname = mf.GetCurrentBinaryDirectory();
  46. } else {
  47. hname = mf.GetCurrentSourceDirectory();
  48. }
  49. hname += "/";
  50. hname += arg;
  51. }
  52. // Append the generated source file to the list.
  53. if (!sourceListValue.empty()) {
  54. sourceListValue += ";";
  55. }
  56. sourceListValue += newName;
  57. // Create the custom command to generate the file.
  58. cmCustomCommandLines commandLines =
  59. cmMakeSingleCommandLine({ moc_exe, "-o", newName, hname });
  60. std::vector<std::string> depends;
  61. depends.push_back(moc_exe);
  62. depends.push_back(hname);
  63. std::string no_main_dependency;
  64. const char* no_working_dir = nullptr;
  65. mf.AddCustomCommandToOutput(
  66. newName, depends, no_main_dependency, commandLines, "Qt Wrapped File",
  67. no_working_dir, mf.GetPolicyStatus(cmPolicies::CMP0116));
  68. }
  69. }
  70. // Store the final list of source files.
  71. mf.AddDefinition(sourceList, sourceListValue);
  72. return true;
  73. }