cmNinjaUtilityTargetGenerator.cxx 5.8 KB

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