cmNinjaUtilityTargetGenerator.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2011 Peter Collingbourne <[email protected]>
  4. Copyright 2011 Nicolas Despres <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmNinjaUtilityTargetGenerator.h"
  12. #include "cmCustomCommand.h"
  13. #include "cmCustomCommandGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmGlobalNinjaGenerator.h"
  17. #include "cmLocalNinjaGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmNinjaTypes.h"
  20. #include "cmOutputConverter.h"
  21. #include "cmSourceFile.h"
  22. #include "cmState.h"
  23. #include "cmSystemTools.h"
  24. #include "cmake.h"
  25. #include <algorithm>
  26. #include <iterator>
  27. #include <string>
  28. #include <vector>
  29. cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(
  30. cmGeneratorTarget* target)
  31. : cmNinjaTargetGenerator(target)
  32. {
  33. }
  34. cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator()
  35. {
  36. }
  37. void cmNinjaUtilityTargetGenerator::Generate()
  38. {
  39. std::string utilCommandName =
  40. this->GetLocalGenerator()->GetCurrentBinaryDirectory();
  41. utilCommandName += cmake::GetCMakeFilesDirectory();
  42. utilCommandName += "/";
  43. utilCommandName += this->GetTargetName() + ".util";
  44. utilCommandName = this->ConvertToNinjaPath(utilCommandName);
  45. std::vector<std::string> commands;
  46. cmNinjaDeps deps, outputs, util_outputs(1, utilCommandName);
  47. const std::vector<cmCustomCommand>* cmdLists[2] = {
  48. &this->GetGeneratorTarget()->GetPreBuildCommands(),
  49. &this->GetGeneratorTarget()->GetPostBuildCommands()
  50. };
  51. bool uses_terminal = false;
  52. for (unsigned i = 0; i != 2; ++i) {
  53. for (std::vector<cmCustomCommand>::const_iterator ci =
  54. cmdLists[i]->begin();
  55. ci != cmdLists[i]->end(); ++ci) {
  56. cmCustomCommandGenerator ccg(*ci, this->GetConfigName(),
  57. this->GetLocalGenerator());
  58. this->GetLocalGenerator()->AppendCustomCommandDeps(ccg, deps);
  59. this->GetLocalGenerator()->AppendCustomCommandLines(ccg, commands);
  60. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  61. std::transform(ccByproducts.begin(), ccByproducts.end(),
  62. std::back_inserter(util_outputs), MapToNinjaPath());
  63. if (ci->GetUsesTerminal()) {
  64. uses_terminal = true;
  65. }
  66. }
  67. }
  68. std::vector<cmSourceFile*> sources;
  69. std::string config =
  70. this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  71. this->GetGeneratorTarget()->GetSourceFiles(sources, config);
  72. for (std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  73. source != sources.end(); ++source) {
  74. if (cmCustomCommand* cc = (*source)->GetCustomCommand()) {
  75. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  76. this->GetLocalGenerator());
  77. this->GetLocalGenerator()->AddCustomCommandTarget(
  78. cc, this->GetGeneratorTarget());
  79. // Depend on all custom command outputs.
  80. const std::vector<std::string>& ccOutputs = ccg.GetOutputs();
  81. const std::vector<std::string>& ccByproducts = ccg.GetByproducts();
  82. std::transform(ccOutputs.begin(), ccOutputs.end(),
  83. std::back_inserter(deps), MapToNinjaPath());
  84. std::transform(ccByproducts.begin(), ccByproducts.end(),
  85. std::back_inserter(deps), MapToNinjaPath());
  86. }
  87. }
  88. this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
  89. outputs);
  90. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  91. deps);
  92. if (commands.empty()) {
  93. this->GetGlobalGenerator()->WritePhonyBuild(
  94. this->GetBuildFileStream(),
  95. "Utility command for " + this->GetTargetName(), outputs, deps);
  96. } else {
  97. std::string command =
  98. this->GetLocalGenerator()->BuildCommandLine(commands);
  99. const char* echoStr =
  100. this->GetGeneratorTarget()->GetProperty("EchoString");
  101. std::string desc;
  102. if (echoStr) {
  103. desc = echoStr;
  104. } else {
  105. desc = "Running utility command for " + this->GetTargetName();
  106. }
  107. // TODO: fix problematic global targets. For now, search and replace the
  108. // makefile vars.
  109. cmSystemTools::ReplaceString(
  110. command, "$(CMAKE_SOURCE_DIR)",
  111. this->GetLocalGenerator()
  112. ->ConvertToOutputFormat(
  113. this->GetLocalGenerator()->GetSourceDirectory(),
  114. cmOutputConverter::SHELL)
  115. .c_str());
  116. cmSystemTools::ReplaceString(
  117. command, "$(CMAKE_BINARY_DIR)",
  118. this->GetLocalGenerator()
  119. ->ConvertToOutputFormat(
  120. this->GetLocalGenerator()->GetBinaryDirectory(),
  121. cmOutputConverter::SHELL)
  122. .c_str());
  123. cmSystemTools::ReplaceString(command, "$(ARGS)", "");
  124. if (command.find('$') != std::string::npos) {
  125. return;
  126. }
  127. for (cmNinjaDeps::const_iterator oi = util_outputs.begin(),
  128. oe = util_outputs.end();
  129. oi != oe; ++oi) {
  130. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  131. }
  132. this->GetGlobalGenerator()->WriteCustomCommandBuild(
  133. command, desc, "Utility command for " + this->GetTargetName(),
  134. /*depfile*/ "", uses_terminal,
  135. /*restat*/ true, util_outputs, deps);
  136. this->GetGlobalGenerator()->WritePhonyBuild(
  137. this->GetBuildFileStream(), "", outputs,
  138. cmNinjaDeps(1, utilCommandName));
  139. }
  140. // Add an alias for the logical target name regardless of what directory
  141. // contains it. Skip this for GLOBAL_TARGET because they are meant to
  142. // be per-directory and have one at the top-level anyway.
  143. if (this->GetGeneratorTarget()->GetType() != cmState::GLOBAL_TARGET) {
  144. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  145. this->GetGeneratorTarget());
  146. }
  147. }