cmNinjaUtilityTargetGenerator.cxx 7.2 KB

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