cmNinjaUtilityTargetGenerator.cxx 6.8 KB

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