cmNinjaUtilityTargetGenerator.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "cmStateTypes.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 (cmCustomCommand const& ci : *cmdLists[i]) {
  46. cmCustomCommandGenerator ccg(ci, this->GetConfigName(),
  47. this->GetLocalGenerator());
  48. this->GetLocalGenerator()->AppendCustomCommandDeps(ccg, deps);
  49. this->GetLocalGenerator()->AppendCustomCommandLines(ccg, commands);
  50. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  51. std::transform(ccByproducts.begin(), ccByproducts.end(),
  52. std::back_inserter(util_outputs), MapToNinjaPath());
  53. if (ci.GetUsesTerminal()) {
  54. uses_terminal = true;
  55. }
  56. }
  57. }
  58. std::vector<cmSourceFile*> sources;
  59. std::string config =
  60. this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  61. this->GetGeneratorTarget()->GetSourceFiles(sources, config);
  62. for (cmSourceFile const* source : sources) {
  63. if (cmCustomCommand const* cc = source->GetCustomCommand()) {
  64. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  65. this->GetLocalGenerator());
  66. this->GetLocalGenerator()->AddCustomCommandTarget(
  67. cc, this->GetGeneratorTarget());
  68. // Depend on all custom command outputs.
  69. const std::vector<std::string>& ccOutputs = ccg.GetOutputs();
  70. const std::vector<std::string>& ccByproducts = ccg.GetByproducts();
  71. std::transform(ccOutputs.begin(), ccOutputs.end(),
  72. std::back_inserter(deps), MapToNinjaPath());
  73. std::transform(ccByproducts.begin(), ccByproducts.end(),
  74. std::back_inserter(deps), MapToNinjaPath());
  75. }
  76. }
  77. this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
  78. outputs);
  79. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  80. deps);
  81. if (commands.empty()) {
  82. this->GetGlobalGenerator()->WritePhonyBuild(
  83. this->GetBuildFileStream(),
  84. "Utility command for " + this->GetTargetName(), outputs, deps);
  85. } else {
  86. std::string command =
  87. this->GetLocalGenerator()->BuildCommandLine(commands);
  88. const char* echoStr =
  89. this->GetGeneratorTarget()->GetProperty("EchoString");
  90. std::string desc;
  91. if (echoStr) {
  92. desc = echoStr;
  93. } else {
  94. desc = "Running utility command for " + this->GetTargetName();
  95. }
  96. // TODO: fix problematic global targets. For now, search and replace the
  97. // makefile vars.
  98. cmSystemTools::ReplaceString(
  99. command, "$(CMAKE_SOURCE_DIR)",
  100. this->GetLocalGenerator()
  101. ->ConvertToOutputFormat(
  102. this->GetLocalGenerator()->GetSourceDirectory(),
  103. cmOutputConverter::SHELL)
  104. .c_str());
  105. cmSystemTools::ReplaceString(
  106. command, "$(CMAKE_BINARY_DIR)",
  107. this->GetLocalGenerator()
  108. ->ConvertToOutputFormat(
  109. this->GetLocalGenerator()->GetBinaryDirectory(),
  110. cmOutputConverter::SHELL)
  111. .c_str());
  112. cmSystemTools::ReplaceString(command, "$(ARGS)", "");
  113. if (command.find('$') != std::string::npos) {
  114. return;
  115. }
  116. for (std::string const& util_output : util_outputs) {
  117. this->GetGlobalGenerator()->SeenCustomCommandOutput(util_output);
  118. }
  119. this->GetGlobalGenerator()->WriteCustomCommandBuild(
  120. command, desc, "Utility command for " + this->GetTargetName(),
  121. /*depfile*/ "", uses_terminal,
  122. /*restat*/ true, util_outputs, deps);
  123. this->GetGlobalGenerator()->WritePhonyBuild(
  124. this->GetBuildFileStream(), "", outputs,
  125. cmNinjaDeps(1, utilCommandName));
  126. }
  127. // Add an alias for the logical target name regardless of what directory
  128. // contains it. Skip this for GLOBAL_TARGET because they are meant to
  129. // be per-directory and have one at the top-level anyway.
  130. if (this->GetGeneratorTarget()->GetType() != cmStateEnums::GLOBAL_TARGET) {
  131. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  132. this->GetGeneratorTarget());
  133. }
  134. }