cmNinjaUtilityTargetGenerator.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "cmNinjaUtilityTargetGenerator.h"
  4. #include "cmCustomCommand.h"
  5. #include "cmCustomCommandGenerator.h"
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmGlobalNinjaGenerator.h"
  9. #include "cmLocalNinjaGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmNinjaTypes.h"
  12. #include "cmOutputConverter.h"
  13. #include "cmSourceFile.h"
  14. #include "cmState.h"
  15. #include "cmSystemTools.h"
  16. #include "cmake.h"
  17. #include <algorithm>
  18. #include <iterator>
  19. #include <string>
  20. #include <vector>
  21. cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(
  22. cmGeneratorTarget* target)
  23. : cmNinjaTargetGenerator(target)
  24. {
  25. }
  26. cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator()
  27. {
  28. }
  29. void cmNinjaUtilityTargetGenerator::Generate()
  30. {
  31. std::string utilCommandName =
  32. this->GetLocalGenerator()->GetCurrentBinaryDirectory();
  33. utilCommandName += cmake::GetCMakeFilesDirectory();
  34. utilCommandName += "/";
  35. utilCommandName += this->GetTargetName() + ".util";
  36. utilCommandName = this->ConvertToNinjaPath(utilCommandName);
  37. std::vector<std::string> commands;
  38. cmNinjaDeps deps, outputs, util_outputs(1, utilCommandName);
  39. const std::vector<cmCustomCommand>* cmdLists[2] = {
  40. &this->GetGeneratorTarget()->GetPreBuildCommands(),
  41. &this->GetGeneratorTarget()->GetPostBuildCommands()
  42. };
  43. bool uses_terminal = false;
  44. for (unsigned i = 0; i != 2; ++i) {
  45. for (std::vector<cmCustomCommand>::const_iterator ci =
  46. cmdLists[i]->begin();
  47. ci != cmdLists[i]->end(); ++ci) {
  48. cmCustomCommandGenerator ccg(*ci, this->GetConfigName(),
  49. this->GetLocalGenerator());
  50. this->GetLocalGenerator()->AppendCustomCommandDeps(ccg, deps);
  51. this->GetLocalGenerator()->AppendCustomCommandLines(ccg, commands);
  52. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  53. std::transform(ccByproducts.begin(), ccByproducts.end(),
  54. std::back_inserter(util_outputs), MapToNinjaPath());
  55. if (ci->GetUsesTerminal()) {
  56. uses_terminal = true;
  57. }
  58. }
  59. }
  60. std::vector<cmSourceFile*> sources;
  61. std::string config =
  62. this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  63. this->GetGeneratorTarget()->GetSourceFiles(sources, config);
  64. for (std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  65. source != sources.end(); ++source) {
  66. if (cmCustomCommand* cc = (*source)->GetCustomCommand()) {
  67. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  68. this->GetLocalGenerator());
  69. this->GetLocalGenerator()->AddCustomCommandTarget(
  70. cc, this->GetGeneratorTarget());
  71. // Depend on all custom command outputs.
  72. const std::vector<std::string>& ccOutputs = ccg.GetOutputs();
  73. const std::vector<std::string>& ccByproducts = ccg.GetByproducts();
  74. std::transform(ccOutputs.begin(), ccOutputs.end(),
  75. std::back_inserter(deps), MapToNinjaPath());
  76. std::transform(ccByproducts.begin(), ccByproducts.end(),
  77. std::back_inserter(deps), MapToNinjaPath());
  78. }
  79. }
  80. this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
  81. outputs);
  82. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  83. deps);
  84. if (commands.empty()) {
  85. this->GetGlobalGenerator()->WritePhonyBuild(
  86. this->GetBuildFileStream(),
  87. "Utility command for " + this->GetTargetName(), outputs, deps);
  88. } else {
  89. std::string command =
  90. this->GetLocalGenerator()->BuildCommandLine(commands);
  91. const char* echoStr =
  92. this->GetGeneratorTarget()->GetProperty("EchoString");
  93. std::string desc;
  94. if (echoStr) {
  95. desc = echoStr;
  96. } else {
  97. desc = "Running utility command for " + this->GetTargetName();
  98. }
  99. // TODO: fix problematic global targets. For now, search and replace the
  100. // makefile vars.
  101. cmSystemTools::ReplaceString(
  102. command, "$(CMAKE_SOURCE_DIR)",
  103. this->GetLocalGenerator()
  104. ->ConvertToOutputFormat(
  105. this->GetLocalGenerator()->GetSourceDirectory(),
  106. cmOutputConverter::SHELL)
  107. .c_str());
  108. cmSystemTools::ReplaceString(
  109. command, "$(CMAKE_BINARY_DIR)",
  110. this->GetLocalGenerator()
  111. ->ConvertToOutputFormat(
  112. this->GetLocalGenerator()->GetBinaryDirectory(),
  113. cmOutputConverter::SHELL)
  114. .c_str());
  115. cmSystemTools::ReplaceString(command, "$(ARGS)", "");
  116. if (command.find('$') != std::string::npos) {
  117. return;
  118. }
  119. for (cmNinjaDeps::const_iterator oi = util_outputs.begin(),
  120. oe = util_outputs.end();
  121. oi != oe; ++oi) {
  122. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  123. }
  124. this->GetGlobalGenerator()->WriteCustomCommandBuild(
  125. command, desc, "Utility command for " + this->GetTargetName(),
  126. /*depfile*/ "", uses_terminal,
  127. /*restat*/ true, util_outputs, deps);
  128. this->GetGlobalGenerator()->WritePhonyBuild(
  129. this->GetBuildFileStream(), "", outputs,
  130. cmNinjaDeps(1, utilCommandName));
  131. }
  132. // Add an alias for the logical target name regardless of what directory
  133. // contains it. Skip this for GLOBAL_TARGET because they are meant to
  134. // be per-directory and have one at the top-level anyway.
  135. if (this->GetGeneratorTarget()->GetType() != cmStateEnums::GLOBAL_TARGET) {
  136. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  137. this->GetGeneratorTarget());
  138. }
  139. }