cmNinjaUtilityTargetGenerator.cxx 6.1 KB

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