cmLocalNinjaGenerator.cxx 12 KB

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