cmLocalNinjaGenerator.cxx 13 KB

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