cmMakefileUtilityTargetGenerator.cxx 3.8 KB

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