cmLocalNinjaGenerator.cxx 13 KB

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