cmNinjaUtilityTargetGenerator.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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. cmNinjaDeps util_outputs(1, utilCommandName);
  68. bool uses_terminal = false;
  69. {
  70. std::array<std::vector<cmCustomCommand> const*, 2> const cmdLists = {
  71. { &genTarget->GetPreBuildCommands(), &genTarget->GetPostBuildCommands() }
  72. };
  73. for (std::vector<cmCustomCommand> const* cmdList : cmdLists) {
  74. for (cmCustomCommand const& ci : *cmdList) {
  75. cmCustomCommandGenerator ccg(ci, fileConfig, lg);
  76. lg->AppendCustomCommandDeps(ccg, deps, fileConfig);
  77. lg->AppendCustomCommandLines(ccg, commands);
  78. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  79. std::transform(ccByproducts.begin(), ccByproducts.end(),
  80. std::back_inserter(util_outputs),
  81. this->MapToNinjaPath());
  82. if (ci.GetUsesTerminal()) {
  83. uses_terminal = true;
  84. }
  85. }
  86. }
  87. }
  88. {
  89. std::vector<cmSourceFile*> sources;
  90. genTarget->GetSourceFiles(sources, config);
  91. for (cmSourceFile const* source : sources) {
  92. if (cmCustomCommand const* cc = source->GetCustomCommand()) {
  93. cmCustomCommandGenerator ccg(*cc, config, lg);
  94. lg->AddCustomCommandTarget(cc, genTarget);
  95. // Depend on all custom command outputs.
  96. const std::vector<std::string>& ccOutputs = ccg.GetOutputs();
  97. const std::vector<std::string>& ccByproducts = ccg.GetByproducts();
  98. std::transform(ccOutputs.begin(), ccOutputs.end(),
  99. std::back_inserter(deps), this->MapToNinjaPath());
  100. std::transform(ccByproducts.begin(), ccByproducts.end(),
  101. std::back_inserter(deps), this->MapToNinjaPath());
  102. }
  103. }
  104. }
  105. std::string outputConfig;
  106. if (genTarget->Target->IsPerConfig()) {
  107. outputConfig = config;
  108. }
  109. lg->AppendTargetOutputs(genTarget, phonyBuild.Outputs, outputConfig);
  110. if (genTarget->Target->GetType() != cmStateEnums::GLOBAL_TARGET) {
  111. lg->AppendTargetOutputs(genTarget, gg->GetByproductsForCleanTarget(),
  112. config);
  113. std::copy(util_outputs.begin(), util_outputs.end(),
  114. std::back_inserter(gg->GetByproductsForCleanTarget()));
  115. }
  116. lg->AppendTargetDepends(genTarget, deps, config, fileConfig,
  117. DependOnTargetArtifact);
  118. if (commands.empty()) {
  119. phonyBuild.Comment = "Utility command for " + this->GetTargetName();
  120. phonyBuild.ExplicitDeps = std::move(deps);
  121. if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) {
  122. gg->WriteBuild(this->GetImplFileStream(fileConfig), phonyBuild);
  123. } else {
  124. gg->WriteBuild(this->GetCommonFileStream(), phonyBuild);
  125. }
  126. } else {
  127. std::string command =
  128. lg->BuildCommandLine(commands, "utility", this->GeneratorTarget);
  129. std::string desc;
  130. cmProp echoStr = genTarget->GetProperty("EchoString");
  131. if (echoStr) {
  132. desc = *echoStr;
  133. } else {
  134. desc = "Running utility command for " + this->GetTargetName();
  135. }
  136. // TODO: fix problematic global targets. For now, search and replace the
  137. // makefile vars.
  138. cmSystemTools::ReplaceString(
  139. command, "$(CMAKE_SOURCE_DIR)",
  140. lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
  141. cmOutputConverter::SHELL));
  142. cmSystemTools::ReplaceString(
  143. command, "$(CMAKE_BINARY_DIR)",
  144. lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
  145. cmOutputConverter::SHELL));
  146. cmSystemTools::ReplaceString(command, "$(ARGS)", "");
  147. command = gg->ExpandCFGIntDir(command, config);
  148. if (command.find('$') != std::string::npos) {
  149. return;
  150. }
  151. for (std::string const& util_output : util_outputs) {
  152. gg->SeenCustomCommandOutput(util_output);
  153. }
  154. std::string ccConfig;
  155. if (genTarget->Target->IsPerConfig() &&
  156. genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) {
  157. ccConfig = fileConfig;
  158. }
  159. if (config == fileConfig ||
  160. gg->GetPerConfigUtilityTargets().count(genTarget->GetName())) {
  161. gg->WriteCustomCommandBuild(
  162. command, desc, "Utility command for " + this->GetTargetName(),
  163. /*depfile*/ "", /*job_pool*/ "", uses_terminal,
  164. /*restat*/ true, util_outputs, ccConfig, deps);
  165. }
  166. phonyBuild.ExplicitDeps.push_back(utilCommandName);
  167. if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) {
  168. gg->WriteBuild(this->GetImplFileStream(fileConfig), phonyBuild);
  169. } else {
  170. gg->WriteBuild(this->GetCommonFileStream(), phonyBuild);
  171. }
  172. }
  173. // Find ADDITIONAL_CLEAN_FILES
  174. this->AdditionalCleanFiles(config);
  175. // Add an alias for the logical target name regardless of what directory
  176. // contains it. Skip this for GLOBAL_TARGET because they are meant to
  177. // be per-directory and have one at the top-level anyway.
  178. if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) {
  179. gg->AddTargetAlias(this->GetTargetName(), genTarget, config);
  180. }
  181. }