cmFastbuildUtilityTargetGenerator.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "cmFastbuildUtilityTargetGenerator.h"
  4. #include <set>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <cm/memory>
  9. #include "cmFastbuildTargetGenerator.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalFastbuildGenerator.h"
  12. #include "cmListFileCache.h"
  13. #include "cmMakefile.h"
  14. #include "cmStateTypes.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmTarget.h"
  17. #include "cmTargetDepend.h"
  18. cmFastbuildUtilityTargetGenerator::cmFastbuildUtilityTargetGenerator(
  19. cmGeneratorTarget* gt, std::string configParam)
  20. : cmFastbuildTargetGenerator(gt, std::move(configParam))
  21. {
  22. }
  23. void cmFastbuildUtilityTargetGenerator::Generate()
  24. {
  25. std::string targetName = GeneratorTarget->GetName();
  26. if (this->GeneratorTarget->GetType() == cmStateEnums::GLOBAL_TARGET) {
  27. targetName = GetGlobalGenerator()->GetTargetName(GeneratorTarget);
  28. }
  29. FastbuildAliasNode fastbuildTarget;
  30. auto const addUtilDepToTarget = [&fastbuildTarget](std::string depName) {
  31. FastbuildTargetDep dep{ depName };
  32. dep.Type = FastbuildTargetDepType::UTIL;
  33. fastbuildTarget.PreBuildDependencies.emplace(std::move(dep));
  34. };
  35. fastbuildTarget.Name = targetName;
  36. fastbuildTarget.BaseName = targetName;
  37. LogMessage("<-------------->");
  38. LogMessage("Generate Utility target: " + targetName);
  39. LogMessage("Config: " + Config);
  40. for (auto const& dep : TargetDirectDependencies) {
  41. LogMessage("Dep: " + dep->GetName());
  42. }
  43. std::vector<std::string> nonImportedUtils;
  44. for (BT<std::pair<std::string, bool>> const& util :
  45. this->GeneratorTarget->GetUtilities()) {
  46. if (util.Value.first == targetName) {
  47. continue;
  48. }
  49. auto const& utilTargetName =
  50. this->ConvertToFastbuildPath(util.Value.first);
  51. LogMessage("Util: " + utilTargetName);
  52. auto* const target = this->Makefile->FindTargetToUse(utilTargetName);
  53. if (target && target->IsImported()) {
  54. LogMessage("Skipping imported util target: " + utilTargetName);
  55. continue;
  56. }
  57. // Since interface target don't appear in the generated build files,
  58. // transitively propagate their deps (if any).
  59. // Tested in "ExternalProjectSubdir" test.
  60. if (target && target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  61. for (auto const& dep : target->GetUtilities()) {
  62. auto const& depName = this->ConvertToFastbuildPath(dep.Value.first);
  63. LogMessage("Transitively propagating iface dep: " + depName +
  64. ", is cross: " + std::to_string(dep.Value.second));
  65. nonImportedUtils.emplace_back(depName);
  66. addUtilDepToTarget(this->ConvertToFastbuildPath(depName));
  67. }
  68. } else {
  69. nonImportedUtils.emplace_back(utilTargetName);
  70. addUtilDepToTarget(utilTargetName);
  71. }
  72. }
  73. if (this->GetGlobalGenerator()->IsExcluded(this->GetGeneratorTarget())) {
  74. LogMessage(cmStrCat("Excluding ", targetName, " from ALL"));
  75. fastbuildTarget.ExcludeFromAll = true;
  76. }
  77. auto preBuild = GenerateCommands(FastbuildBuildStep::PRE_BUILD);
  78. // Tested in "RunCMake.CPack*" tests.
  79. // Utility target "package" has packaging steps as "POST_BUILD".
  80. for (auto& exec : GenerateCommands(FastbuildBuildStep::POST_BUILD).Nodes) {
  81. addUtilDepToTarget(exec.Name);
  82. for (std::string const& util : nonImportedUtils) {
  83. LogMessage("Adding: util " + util);
  84. exec.PreBuildDependencies.emplace(util);
  85. }
  86. // So POST_BUILD is executed AFTER PRE_BUILD (tested in "CustomCommand"
  87. // test).
  88. for (auto const& pre : preBuild.Nodes) {
  89. LogMessage("Adding: " + pre.Name);
  90. exec.PreBuildDependencies.emplace(pre.Name);
  91. }
  92. this->GetGlobalGenerator()->AddTarget(std::move(exec));
  93. }
  94. for (auto& exec : preBuild.Nodes) {
  95. LogMessage("Adding exec " + exec.Name);
  96. addUtilDepToTarget(exec.Name);
  97. this->GetGlobalGenerator()->AddTarget(std::move(exec));
  98. }
  99. for (auto& exec : GenerateCommands(FastbuildBuildStep::REST).Nodes) {
  100. addUtilDepToTarget(exec.Name);
  101. for (auto const& dep : TargetDirectDependencies) {
  102. LogMessage("Direct dep " + dep->GetName() +
  103. "-all propagating to CC: " + exec.Name);
  104. // All custom commands from within the target must be executed AFTER all
  105. // the target's deps.
  106. exec.PreBuildDependencies.emplace(dep->GetName());
  107. }
  108. this->GetGlobalGenerator()->AddTarget(std::move(exec));
  109. }
  110. if (fastbuildTarget.PreBuildDependencies.empty()) {
  111. if (fastbuildTarget.ExcludeFromAll) {
  112. return;
  113. }
  114. FastbuildTargetDep dep{ FASTBUILD_NOOP_FILE_NAME };
  115. dep.Type = FastbuildTargetDepType::ORDER_ONLY;
  116. fastbuildTarget.PreBuildDependencies.emplace(std::move(dep));
  117. }
  118. fastbuildTarget.Hidden = false;
  119. this->AdditionalCleanFiles();
  120. fastbuildTarget.BasePath = this->GetMakefile()->GetCurrentSourceDirectory();
  121. this->GetGlobalGenerator()->AddIDEProject(fastbuildTarget, Config);
  122. this->GetGlobalGenerator()->AddTarget(std::move(fastbuildTarget));
  123. }