cmLocalNinjaGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "cmLocalNinjaGenerator.h"
  12. #include "cmCustomCommandGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmGlobalNinjaGenerator.h"
  15. #include "cmNinjaTargetGenerator.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmSourceFile.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include "cmake.h"
  20. #include <assert.h>
  21. cmLocalNinjaGenerator::cmLocalNinjaGenerator()
  22. : cmLocalGenerator()
  23. , ConfigName("")
  24. , HomeRelativeOutputPath("")
  25. {
  26. this->IsMakefileGenerator = true;
  27. #ifdef _WIN32
  28. this->WindowsShell = true;
  29. #endif
  30. this->TargetImplib = "$TARGET_IMPLIB";
  31. }
  32. //----------------------------------------------------------------------------
  33. // Virtual public methods.
  34. cmLocalNinjaGenerator::~cmLocalNinjaGenerator()
  35. {
  36. }
  37. void cmLocalNinjaGenerator::Generate()
  38. {
  39. this->SetConfigName();
  40. this->WriteProcessedMakefile(this->GetBuildFileStream());
  41. #ifdef NINJA_GEN_VERBOSE_FILES
  42. this->WriteProcessedMakefile(this->GetRulesFileStream());
  43. #endif
  44. this->WriteBuildFileTop();
  45. cmTargets& targets = this->GetMakefile()->GetTargets();
  46. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  47. {
  48. cmNinjaTargetGenerator* tg = cmNinjaTargetGenerator::New(&t->second);
  49. if(tg)
  50. {
  51. tg->Generate();
  52. // Add the target to "all" if required.
  53. if (!this->GetGlobalNinjaGenerator()->IsExcluded(
  54. this->GetGlobalNinjaGenerator()->GetLocalGenerators()[0],
  55. t->second))
  56. this->GetGlobalNinjaGenerator()->AddDependencyToAll(&t->second);
  57. delete tg;
  58. }
  59. }
  60. this->WriteCustomCommandBuildStatements();
  61. }
  62. // Implemented in:
  63. // cmLocalUnixMakefileGenerator3.
  64. // Used in:
  65. // Source/cmMakefile.cxx
  66. // Source/cmGlobalGenerator.cxx
  67. void cmLocalNinjaGenerator::Configure()
  68. {
  69. // Compute the path to use when referencing the current output
  70. // directory from the top output directory.
  71. this->HomeRelativeOutputPath =
  72. this->Convert(this->Makefile->GetStartOutputDirectory(), HOME_OUTPUT);
  73. if(this->HomeRelativeOutputPath == ".")
  74. {
  75. this->HomeRelativeOutputPath = "";
  76. }
  77. this->cmLocalGenerator::Configure();
  78. }
  79. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  80. std::string cmLocalNinjaGenerator
  81. ::GetTargetDirectory(cmTarget const& target) const
  82. {
  83. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  84. dir += target.GetName();
  85. #if defined(__VMS)
  86. dir += "_dir";
  87. #else
  88. dir += ".dir";
  89. #endif
  90. return dir;
  91. }
  92. //----------------------------------------------------------------------------
  93. // Non-virtual public methods.
  94. const cmGlobalNinjaGenerator*
  95. cmLocalNinjaGenerator::GetGlobalNinjaGenerator() const
  96. {
  97. return
  98. static_cast<const cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  99. }
  100. cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  101. {
  102. return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  103. }
  104. //----------------------------------------------------------------------------
  105. // Virtual protected methods.
  106. std::string
  107. cmLocalNinjaGenerator::ConvertToLinkReference(std::string const& lib)
  108. {
  109. return this->Convert(lib.c_str(), HOME_OUTPUT, SHELL);
  110. }
  111. std::string
  112. cmLocalNinjaGenerator::ConvertToIncludeReference(std::string const& path)
  113. {
  114. return this->Convert(path.c_str(), HOME_OUTPUT, SHELL);
  115. }
  116. //----------------------------------------------------------------------------
  117. // Private methods.
  118. cmGeneratedFileStream& cmLocalNinjaGenerator::GetBuildFileStream() const
  119. {
  120. return *this->GetGlobalNinjaGenerator()->GetBuildFileStream();
  121. }
  122. cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
  123. {
  124. return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
  125. }
  126. const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
  127. {
  128. return this->GetGlobalGenerator()->GetCMakeInstance();
  129. }
  130. cmake* cmLocalNinjaGenerator::GetCMakeInstance()
  131. {
  132. return this->GetGlobalGenerator()->GetCMakeInstance();
  133. }
  134. bool cmLocalNinjaGenerator::isRootMakefile() const
  135. {
  136. return (strcmp(this->Makefile->GetCurrentDirectory(),
  137. this->GetCMakeInstance()->GetHomeDirectory()) == 0);
  138. }
  139. void cmLocalNinjaGenerator::WriteBuildFileTop()
  140. {
  141. // We do that only once for the top CMakeLists.txt file.
  142. if(!this->isRootMakefile())
  143. return;
  144. // For the build file.
  145. this->WriteProjectHeader(this->GetBuildFileStream());
  146. this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
  147. // For the rule file.
  148. this->WriteProjectHeader(this->GetRulesFileStream());
  149. }
  150. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  151. {
  152. cmGlobalNinjaGenerator::WriteDivider(os);
  153. os
  154. << "# Project: " << this->GetMakefile()->GetProjectName() << std::endl
  155. << "# Configuration: " << this->ConfigName << std::endl
  156. ;
  157. cmGlobalNinjaGenerator::WriteDivider(os);
  158. }
  159. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  160. {
  161. cmGlobalNinjaGenerator::WriteDivider(os);
  162. os
  163. << "# Include auxiliary files.\n"
  164. << "\n"
  165. ;
  166. cmGlobalNinjaGenerator::WriteInclude(os,
  167. cmGlobalNinjaGenerator::NINJA_RULES_FILE,
  168. "Include rules file.");
  169. os << "\n";
  170. }
  171. void cmLocalNinjaGenerator::SetConfigName()
  172. {
  173. // Store the configuration name that will be generated.
  174. if(const char* config =
  175. this->GetMakefile()->GetDefinition("CMAKE_BUILD_TYPE"))
  176. {
  177. // Use the build type given by the user.
  178. this->ConfigName = config;
  179. }
  180. else
  181. {
  182. // No configuration type given.
  183. this->ConfigName = "";
  184. }
  185. }
  186. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  187. {
  188. cmGlobalNinjaGenerator::WriteDivider(os);
  189. os
  190. << "# Write statements declared in CMakeLists.txt:" << std::endl
  191. << "# " << this->Makefile->GetCurrentListFile() << std::endl
  192. ;
  193. if(this->isRootMakefile())
  194. os << "# Which is the root file." << std::endl;
  195. cmGlobalNinjaGenerator::WriteDivider(os);
  196. os << std::endl;
  197. }
  198. std::string cmLocalNinjaGenerator::ConvertToNinjaPath(const char *path)
  199. {
  200. std::string convPath = this->Convert(path, cmLocalGenerator::HOME_OUTPUT);
  201. #ifdef _WIN32
  202. cmSystemTools::ReplaceString(convPath, "/", "\\");
  203. #endif
  204. return convPath;
  205. }
  206. void
  207. cmLocalNinjaGenerator
  208. ::AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs)
  209. {
  210. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  211. }
  212. void
  213. cmLocalNinjaGenerator
  214. ::AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs)
  215. {
  216. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs);
  217. }
  218. void cmLocalNinjaGenerator::AppendCustomCommandDeps(const cmCustomCommand *cc,
  219. cmNinjaDeps &ninjaDeps)
  220. {
  221. const std::vector<std::string> &deps = cc->GetDepends();
  222. for (std::vector<std::string>::const_iterator i = deps.begin();
  223. i != deps.end(); ++i) {
  224. std::string dep;
  225. if (this->GetRealDependency(i->c_str(), this->GetConfigName(), dep))
  226. ninjaDeps.push_back(ConvertToNinjaPath(dep.c_str()));
  227. }
  228. }
  229. std::string cmLocalNinjaGenerator::BuildCommandLine(
  230. const std::vector<std::string> &cmdLines)
  231. {
  232. // If we have no commands but we need to build a command anyway, use ":".
  233. // This happens when building a POST_BUILD value for link targets that
  234. // don't use POST_BUILD.
  235. if (cmdLines.empty())
  236. #ifdef _WIN32
  237. return "cd .";
  238. #else
  239. return ":";
  240. #endif
  241. cmOStringStream cmd;
  242. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  243. li != cmdLines.end(); ++li) {
  244. if (li != cmdLines.begin()) {
  245. cmd << " && ";
  246. #ifdef _WIN32
  247. } else if (cmdLines.size() > 1) {
  248. cmd << "cmd.exe /c ";
  249. #endif
  250. }
  251. cmd << *li;
  252. }
  253. return cmd.str();
  254. }
  255. void cmLocalNinjaGenerator::AppendCustomCommandLines(const cmCustomCommand *cc,
  256. std::vector<std::string> &cmdLines)
  257. {
  258. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this->Makefile);
  259. if (ccg.GetNumberOfCommands() > 0) {
  260. const char* wd = cc->GetWorkingDirectory();
  261. if (!wd)
  262. wd = this->GetMakefile()->GetStartOutputDirectory();
  263. cmOStringStream cdCmd;
  264. cdCmd << "cd " << this->ConvertToOutputFormat(wd, SHELL);
  265. cmdLines.push_back(cdCmd.str());
  266. }
  267. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  268. cmdLines.push_back(this->ConvertToOutputFormat(ccg.GetCommand(i).c_str(),
  269. SHELL));
  270. std::string& cmd = cmdLines.back();
  271. ccg.AppendArguments(i, cmd);
  272. }
  273. }
  274. void
  275. cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  276. cmCustomCommand const *cc, const cmNinjaDeps& orderOnlyDeps)
  277. {
  278. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc))
  279. return;
  280. const std::vector<std::string> &outputs = cc->GetOutputs();
  281. cmNinjaDeps ninjaOutputs(outputs.size()), ninjaDeps;
  282. std::transform(outputs.begin(), outputs.end(),
  283. ninjaOutputs.begin(), MapToNinjaPath());
  284. this->AppendCustomCommandDeps(cc, ninjaDeps);
  285. for (cmNinjaDeps::iterator i = ninjaOutputs.begin(); i != ninjaOutputs.end();
  286. ++i)
  287. this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(*i);
  288. std::vector<std::string> cmdLines;
  289. this->AppendCustomCommandLines(cc, cmdLines);
  290. if (cmdLines.empty()) {
  291. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  292. "Phony custom command for " +
  293. ninjaOutputs[0],
  294. ninjaOutputs,
  295. ninjaDeps,
  296. cmNinjaDeps(),
  297. orderOnlyDeps,
  298. cmNinjaVars());
  299. } else {
  300. this->GetGlobalNinjaGenerator()->WriteCustomCommandBuild(
  301. this->BuildCommandLine(cmdLines),
  302. this->ConstructComment(*cc),
  303. "Custom command for " + ninjaOutputs[0],
  304. ninjaOutputs,
  305. ninjaDeps,
  306. orderOnlyDeps);
  307. }
  308. }
  309. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  310. cmTarget* target)
  311. {
  312. this->CustomCommandTargets[cc].insert(target);
  313. }
  314. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
  315. {
  316. for (CustomCommandTargetMap::iterator i = this->CustomCommandTargets.begin();
  317. i != this->CustomCommandTargets.end(); ++i) {
  318. // A custom command may appear on multiple targets. However, some build
  319. // systems exist where the target dependencies on some of the targets are
  320. // overspecified, leading to a dependency cycle. If we assume all target
  321. // dependencies are a superset of the true target dependencies for this
  322. // custom command, we can take the set intersection of all target
  323. // dependencies to obtain a correct dependency list.
  324. //
  325. // FIXME: This won't work in certain obscure scenarios involving indirect
  326. // dependencies.
  327. std::set<cmTarget*>::iterator j = i->second.begin();
  328. assert(j != i->second.end());
  329. std::vector<std::string> ccTargetDeps;
  330. this->AppendTargetDepends(*j, ccTargetDeps);
  331. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  332. ++j;
  333. for (; j != i->second.end(); ++j) {
  334. std::vector<std::string> jDeps, depsIntersection;
  335. this->AppendTargetDepends(*j, jDeps);
  336. std::sort(jDeps.begin(), jDeps.end());
  337. std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
  338. jDeps.begin(), jDeps.end(),
  339. std::back_inserter(depsIntersection));
  340. ccTargetDeps = depsIntersection;
  341. }
  342. this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
  343. }
  344. }