cmNinjaUtilityTargetGenerator.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "cmNinjaUtilityTargetGenerator.h"
  2. #include "cmCustomCommand.h"
  3. #include "cmGeneratedFileStream.h"
  4. #include "cmGlobalNinjaGenerator.h"
  5. #include "cmMakefile.h"
  6. #include "cmSourceFile.h"
  7. #include "cmTarget.h"
  8. cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(cmTarget *target)
  9. : cmNinjaTargetGenerator(target) {}
  10. cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator() {}
  11. void cmNinjaUtilityTargetGenerator::Generate()
  12. {
  13. std::vector<std::string> commands;
  14. cmNinjaDeps deps, outputs;
  15. const std::vector<cmCustomCommand> *cmdLists[2] = {
  16. &this->GetTarget()->GetPreBuildCommands(),
  17. &this->GetTarget()->GetPostBuildCommands()
  18. };
  19. for (unsigned i = 0; i != 2; ++i) {
  20. for (std::vector<cmCustomCommand>::const_iterator
  21. ci = cmdLists[i]->begin(); ci != cmdLists[i]->end(); ++ci) {
  22. this->GetLocalGenerator()->AppendCustomCommandDeps(&*ci, deps);
  23. this->GetLocalGenerator()->AppendCustomCommandLines(&*ci, commands);
  24. }
  25. }
  26. const std::vector<cmSourceFile*>& sources =
  27. this->GetTarget()->GetSourceFiles();
  28. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  29. source != sources.end(); ++source)
  30. {
  31. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  32. {
  33. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  34. // Depend on all custom command outputs.
  35. const std::vector<std::string>& outputs = cc->GetOutputs();
  36. std::transform(outputs.begin(), outputs.end(),
  37. std::back_inserter(deps), MapToNinjaPath());
  38. }
  39. }
  40. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  41. this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(), deps);
  42. if (commands.empty()) {
  43. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  44. "Utility command for "
  45. + this->GetTargetName(),
  46. outputs,
  47. deps);
  48. } else {
  49. std::string command =
  50. this->GetLocalGenerator()->BuildCommandLine(commands);
  51. const char *echoStr = this->GetTarget()->GetProperty("EchoString");
  52. std::string desc;
  53. if (echoStr)
  54. desc = echoStr;
  55. else
  56. desc = "Running utility command for " + this->GetTargetName();
  57. // TODO: fix problematic global targets. For now, search and replace the
  58. // makefile vars.
  59. cmSystemTools::ReplaceString(command, "$(CMAKE_SOURCE_DIR)",
  60. this->GetTarget()->GetMakefile()->GetHomeDirectory());
  61. cmSystemTools::ReplaceString(command, "$(CMAKE_BINARY_DIR)",
  62. this->GetTarget()->GetMakefile()->GetHomeOutputDirectory());
  63. cmSystemTools::ReplaceString(command, "$(ARGS)", "");
  64. if (command.find('$') != std::string::npos)
  65. return;
  66. std::string utilCommandName = cmake::GetCMakeFilesDirectoryPostSlash();
  67. utilCommandName += this->GetTargetName() + ".util";
  68. this->GetGlobalGenerator()->WriteCustomCommandBuild(
  69. command,
  70. desc,
  71. "Utility command for " + this->GetTargetName(),
  72. cmNinjaDeps(1, utilCommandName),
  73. deps);
  74. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  75. "",
  76. outputs,
  77. cmNinjaDeps(1, utilCommandName),
  78. cmNinjaDeps(),
  79. cmNinjaDeps(),
  80. cmNinjaVars());
  81. }
  82. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  83. this->GetTarget());
  84. }