cmMakefileUtilityTargetGenerator.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "cmMakefileUtilityTargetGenerator.h"
  4. #include <ostream>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "cm_memory.hxx"
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalUnixMakefileGenerator3.h"
  12. #include "cmLocalUnixMakefileGenerator3.h"
  13. #include "cmMakefile.h"
  14. #include "cmOSXBundleGenerator.h"
  15. #include "cmSystemTools.h"
  16. cmMakefileUtilityTargetGenerator::cmMakefileUtilityTargetGenerator(
  17. cmGeneratorTarget* target)
  18. : cmMakefileTargetGenerator(target)
  19. {
  20. this->CustomCommandDriver = OnUtility;
  21. this->OSXBundleGenerator =
  22. cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
  23. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  24. }
  25. cmMakefileUtilityTargetGenerator::~cmMakefileUtilityTargetGenerator() =
  26. default;
  27. void cmMakefileUtilityTargetGenerator::WriteRuleFiles()
  28. {
  29. this->CreateRuleFile();
  30. *this->BuildFileStream << "# Utility rule file for "
  31. << this->GeneratorTarget->GetName() << ".\n\n";
  32. if (!this->NoRuleMessages) {
  33. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")
  34. ? "$(CMAKE_BINARY_DIR)/"
  35. : "");
  36. // Include the progress variables for the target.
  37. *this->BuildFileStream
  38. << "# Include the progress variables for this target.\n"
  39. << this->GlobalGenerator->IncludeDirective << " " << root
  40. << cmSystemTools::ConvertToOutputPath(
  41. this->LocalGenerator->MaybeConvertToRelativePath(
  42. this->LocalGenerator->GetBinaryDirectory(),
  43. this->ProgressFileNameFull))
  44. << "\n\n";
  45. }
  46. // write the custom commands for this target
  47. this->WriteTargetBuildRules();
  48. // Collect the commands and dependencies.
  49. std::vector<std::string> commands;
  50. std::vector<std::string> depends;
  51. // Utility targets store their rules in pre- and post-build commands.
  52. this->LocalGenerator->AppendCustomDepends(
  53. depends, this->GeneratorTarget->GetPreBuildCommands());
  54. this->LocalGenerator->AppendCustomDepends(
  55. depends, this->GeneratorTarget->GetPostBuildCommands());
  56. this->LocalGenerator->AppendCustomCommands(
  57. commands, this->GeneratorTarget->GetPreBuildCommands(),
  58. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  59. // Depend on all custom command outputs for sources
  60. this->DriveCustomCommands(depends);
  61. this->LocalGenerator->AppendCustomCommands(
  62. commands, this->GeneratorTarget->GetPostBuildCommands(),
  63. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  64. // Add dependencies on targets that must be built first.
  65. this->AppendTargetDepends(depends);
  66. // Add a dependency on the rule file itself.
  67. this->LocalGenerator->AppendRuleDepend(depends,
  68. this->BuildFileNameFull.c_str());
  69. // If the rule is empty add the special empty rule dependency needed
  70. // by some make tools.
  71. if (depends.empty() && commands.empty()) {
  72. std::string hack = this->GlobalGenerator->GetEmptyRuleHackDepends();
  73. if (!hack.empty()) {
  74. depends.push_back(std::move(hack));
  75. }
  76. }
  77. // Write the rule.
  78. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  79. this->GeneratorTarget->GetName(),
  80. depends, commands, true);
  81. // Write the main driver rule to build everything in this target.
  82. this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
  83. // Write clean target
  84. this->WriteTargetCleanRules();
  85. // Write the dependency generation rule. This must be done last so
  86. // that multiple output pair information is available.
  87. this->WriteTargetDependRules();
  88. // close the streams
  89. this->CloseFileStreams();
  90. }