cmMakefileUtilityTargetGenerator.cxx 4.0 KB

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