cmLocalNinjaGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. this->WritePools(this->GetRulesFileStream());
  47. const std::string showIncludesPrefix = this->GetMakefile()
  48. ->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  49. if (!showIncludesPrefix.empty())
  50. {
  51. cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
  52. "localized /showIncludes string");
  53. this->GetRulesFileStream()
  54. << "msvc_deps_prefix = " << showIncludesPrefix << "\n\n";
  55. }
  56. }
  57. cmGeneratorTargetsType targets = this->GetMakefile()->GetGeneratorTargets();
  58. for(cmGeneratorTargetsType::iterator t = targets.begin();
  59. t != targets.end(); ++t)
  60. {
  61. if (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::WritePools(std::ostream& os)
  174. {
  175. cmGlobalNinjaGenerator::WriteDivider(os);
  176. const char* jobpools = this->GetCMakeInstance()
  177. ->GetProperty("JOB_POOLS", cmProperty::GLOBAL);
  178. if (jobpools)
  179. {
  180. cmGlobalNinjaGenerator::WriteComment(os,
  181. "Pools defined by global property JOB_POOLS");
  182. std::vector<std::string> pools;
  183. cmSystemTools::ExpandListArgument(jobpools, pools);
  184. for (size_t i = 0; i < pools.size(); ++i)
  185. {
  186. const std::string pool = pools[i];
  187. const std::string::size_type eq = pool.find("=");
  188. unsigned int jobs;
  189. if (eq != std::string::npos &&
  190. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1)
  191. {
  192. os << "pool " << pool.substr(0, eq) << std::endl;
  193. os << " depth = " << jobs << std::endl;
  194. os << std::endl;
  195. }
  196. else
  197. {
  198. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': ",
  199. pool.c_str());
  200. }
  201. }
  202. }
  203. }
  204. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  205. {
  206. cmGlobalNinjaGenerator::WriteDivider(os);
  207. os
  208. << "# Include auxiliary files.\n"
  209. << "\n"
  210. ;
  211. cmGlobalNinjaGenerator::WriteInclude(os,
  212. cmGlobalNinjaGenerator::NINJA_RULES_FILE,
  213. "Include rules file.");
  214. os << "\n";
  215. }
  216. void cmLocalNinjaGenerator::SetConfigName()
  217. {
  218. // Store the configuration name that will be generated.
  219. if(const char* config =
  220. this->GetMakefile()->GetDefinition("CMAKE_BUILD_TYPE"))
  221. {
  222. // Use the build type given by the user.
  223. this->ConfigName = config;
  224. }
  225. else
  226. {
  227. // No configuration type given.
  228. this->ConfigName = "";
  229. }
  230. }
  231. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  232. {
  233. cmGlobalNinjaGenerator::WriteDivider(os);
  234. os
  235. << "# Write statements declared in CMakeLists.txt:" << std::endl
  236. << "# " << this->Makefile->GetCurrentListFile() << std::endl
  237. ;
  238. if(this->isRootMakefile())
  239. os << "# Which is the root file." << std::endl;
  240. cmGlobalNinjaGenerator::WriteDivider(os);
  241. os << std::endl;
  242. }
  243. std::string cmLocalNinjaGenerator::ConvertToNinjaPath(const char *path)
  244. {
  245. std::string convPath = this->Convert(path, cmLocalGenerator::HOME_OUTPUT);
  246. #ifdef _WIN32
  247. cmSystemTools::ReplaceString(convPath, "/", "\\");
  248. #endif
  249. return convPath;
  250. }
  251. void
  252. cmLocalNinjaGenerator
  253. ::AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs)
  254. {
  255. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  256. }
  257. void
  258. cmLocalNinjaGenerator
  259. ::AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs)
  260. {
  261. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs);
  262. }
  263. void cmLocalNinjaGenerator::AppendCustomCommandDeps(const cmCustomCommand *cc,
  264. cmNinjaDeps &ninjaDeps)
  265. {
  266. const std::vector<std::string> &deps = cc->GetDepends();
  267. for (std::vector<std::string>::const_iterator i = deps.begin();
  268. i != deps.end(); ++i) {
  269. std::string dep;
  270. if (this->GetRealDependency(i->c_str(), this->GetConfigName(), dep))
  271. ninjaDeps.push_back(ConvertToNinjaPath(dep.c_str()));
  272. }
  273. }
  274. std::string cmLocalNinjaGenerator::BuildCommandLine(
  275. const std::vector<std::string> &cmdLines)
  276. {
  277. // If we have no commands but we need to build a command anyway, use ":".
  278. // This happens when building a POST_BUILD value for link targets that
  279. // don't use POST_BUILD.
  280. if (cmdLines.empty())
  281. #ifdef _WIN32
  282. return "cd .";
  283. #else
  284. return ":";
  285. #endif
  286. cmOStringStream cmd;
  287. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  288. li != cmdLines.end(); ++li) {
  289. if (li != cmdLines.begin()) {
  290. cmd << " && ";
  291. #ifdef _WIN32
  292. } else if (cmdLines.size() > 1) {
  293. cmd << "cmd.exe /c ";
  294. #endif
  295. }
  296. cmd << *li;
  297. }
  298. return cmd.str();
  299. }
  300. void cmLocalNinjaGenerator::AppendCustomCommandLines(const cmCustomCommand *cc,
  301. std::vector<std::string> &cmdLines)
  302. {
  303. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this->Makefile);
  304. if (ccg.GetNumberOfCommands() > 0) {
  305. const char* wd = cc->GetWorkingDirectory();
  306. if (!wd)
  307. wd = this->GetMakefile()->GetStartOutputDirectory();
  308. cmOStringStream cdCmd;
  309. #ifdef _WIN32
  310. std::string cdStr = "cd /D ";
  311. #else
  312. std::string cdStr = "cd ";
  313. #endif
  314. cdCmd << cdStr << this->ConvertToOutputFormat(wd, SHELL);
  315. cmdLines.push_back(cdCmd.str());
  316. }
  317. std::string launcher = this->MakeCustomLauncher(*cc);
  318. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  319. cmdLines.push_back(launcher +
  320. this->ConvertToOutputFormat(ccg.GetCommand(i).c_str(), SHELL));
  321. std::string& cmd = cmdLines.back();
  322. ccg.AppendArguments(i, cmd);
  323. }
  324. }
  325. void
  326. cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  327. cmCustomCommand const *cc, const cmNinjaDeps& orderOnlyDeps)
  328. {
  329. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc))
  330. return;
  331. const std::vector<std::string> &outputs = cc->GetOutputs();
  332. cmNinjaDeps ninjaOutputs(outputs.size()), ninjaDeps;
  333. std::transform(outputs.begin(), outputs.end(),
  334. ninjaOutputs.begin(), MapToNinjaPath());
  335. this->AppendCustomCommandDeps(cc, ninjaDeps);
  336. for (cmNinjaDeps::iterator i = ninjaOutputs.begin(); i != ninjaOutputs.end();
  337. ++i)
  338. this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(*i);
  339. std::vector<std::string> cmdLines;
  340. this->AppendCustomCommandLines(cc, cmdLines);
  341. if (cmdLines.empty()) {
  342. this->GetGlobalNinjaGenerator()->WritePhonyBuild(
  343. this->GetBuildFileStream(),
  344. "Phony custom command for " +
  345. ninjaOutputs[0],
  346. ninjaOutputs,
  347. ninjaDeps,
  348. cmNinjaDeps(),
  349. orderOnlyDeps,
  350. cmNinjaVars());
  351. } else {
  352. this->GetGlobalNinjaGenerator()->WriteCustomCommandBuild(
  353. this->BuildCommandLine(cmdLines),
  354. this->ConstructComment(*cc),
  355. "Custom command for " + ninjaOutputs[0],
  356. ninjaOutputs,
  357. ninjaDeps,
  358. orderOnlyDeps);
  359. }
  360. }
  361. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  362. cmTarget* target)
  363. {
  364. this->CustomCommandTargets[cc].insert(target);
  365. }
  366. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
  367. {
  368. for (CustomCommandTargetMap::iterator i = this->CustomCommandTargets.begin();
  369. i != this->CustomCommandTargets.end(); ++i) {
  370. // A custom command may appear on multiple targets. However, some build
  371. // systems exist where the target dependencies on some of the targets are
  372. // overspecified, leading to a dependency cycle. If we assume all target
  373. // dependencies are a superset of the true target dependencies for this
  374. // custom command, we can take the set intersection of all target
  375. // dependencies to obtain a correct dependency list.
  376. //
  377. // FIXME: This won't work in certain obscure scenarios involving indirect
  378. // dependencies.
  379. std::set<cmTarget*>::iterator j = i->second.begin();
  380. assert(j != i->second.end());
  381. std::vector<std::string> ccTargetDeps;
  382. this->AppendTargetDepends(*j, ccTargetDeps);
  383. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  384. ++j;
  385. for (; j != i->second.end(); ++j) {
  386. std::vector<std::string> jDeps, depsIntersection;
  387. this->AppendTargetDepends(*j, jDeps);
  388. std::sort(jDeps.begin(), jDeps.end());
  389. std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
  390. jDeps.begin(), jDeps.end(),
  391. std::back_inserter(depsIntersection));
  392. ccTargetDeps = depsIntersection;
  393. }
  394. this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
  395. }
  396. }
  397. std::string cmLocalNinjaGenerator::MakeCustomLauncher(
  398. const cmCustomCommand& cc)
  399. {
  400. const char* property = "RULE_LAUNCH_CUSTOM";
  401. const char* property_value = this->Makefile->GetProperty(property);
  402. if(!property_value || !*property_value)
  403. {
  404. return std::string();
  405. }
  406. // Expand rules in the empty string. It may insert the launcher and
  407. // perform replacements.
  408. RuleVariables vars;
  409. vars.RuleLauncher = property;
  410. std::string output;
  411. const std::vector<std::string>& outputs = cc.GetOutputs();
  412. if(!outputs.empty())
  413. {
  414. RelativeRoot relative_root =
  415. cc.GetWorkingDirectory() ? NONE : START_OUTPUT;
  416. output = this->Convert(outputs[0].c_str(), relative_root, SHELL);
  417. }
  418. vars.Output = output.c_str();
  419. std::string launcher;
  420. this->ExpandRuleVariables(launcher, vars);
  421. if(!launcher.empty())
  422. {
  423. launcher += " ";
  424. }
  425. return launcher;
  426. }