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