cmNinjaUtilityTargetGenerator.cxx 7.0 KB

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