cmLocalNinjaGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 "cmState.h"
  20. #include <assert.h>
  21. cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
  22. cmLocalGenerator* parent,
  23. cmState::Snapshot snapshot)
  24. : cmLocalCommonGenerator(gg, parent, snapshot)
  25. , HomeRelativeOutputPath("")
  26. {
  27. this->TargetImplib = "$TARGET_IMPLIB";
  28. }
  29. //----------------------------------------------------------------------------
  30. // Virtual public methods.
  31. cmLocalNinjaGenerator::~cmLocalNinjaGenerator()
  32. {
  33. }
  34. void cmLocalNinjaGenerator::Generate()
  35. {
  36. // Compute the path to use when referencing the current output
  37. // directory from the top output directory.
  38. this->HomeRelativeOutputPath =
  39. this->Convert(this->Makefile->GetCurrentBinaryDirectory(), HOME_OUTPUT);
  40. if(this->HomeRelativeOutputPath == ".")
  41. {
  42. this->HomeRelativeOutputPath = "";
  43. }
  44. this->SetConfigName();
  45. this->WriteProcessedMakefile(this->GetBuildFileStream());
  46. #ifdef NINJA_GEN_VERBOSE_FILES
  47. this->WriteProcessedMakefile(this->GetRulesFileStream());
  48. #endif
  49. // We do that only once for the top CMakeLists.txt file.
  50. if(this->Makefile->IsRootMakefile())
  51. {
  52. this->WriteBuildFileTop();
  53. this->WritePools(this->GetRulesFileStream());
  54. const std::string showIncludesPrefix = this->GetMakefile()
  55. ->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  56. if (!showIncludesPrefix.empty())
  57. {
  58. cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
  59. "localized /showIncludes string");
  60. this->GetRulesFileStream()
  61. << "msvc_deps_prefix = " << showIncludesPrefix << "\n\n";
  62. }
  63. }
  64. cmGeneratorTargetsType targets = this->GetMakefile()->GetGeneratorTargets();
  65. for(cmGeneratorTargetsType::iterator t = targets.begin();
  66. t != targets.end(); ++t)
  67. {
  68. if (t->second->Target->GetType() == cmTarget::INTERFACE_LIBRARY
  69. || t->second->Target->IsImported())
  70. {
  71. continue;
  72. }
  73. cmNinjaTargetGenerator* tg = cmNinjaTargetGenerator::New(t->second);
  74. if(tg)
  75. {
  76. tg->Generate();
  77. // Add the target to "all" if required.
  78. if (!this->GetGlobalNinjaGenerator()->IsExcluded(
  79. this->GetGlobalNinjaGenerator()->GetLocalGenerators()[0],
  80. *t->second->Target))
  81. this->GetGlobalNinjaGenerator()->AddDependencyToAll(t->second->Target);
  82. delete tg;
  83. }
  84. }
  85. this->WriteCustomCommandBuildStatements();
  86. }
  87. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  88. std::string cmLocalNinjaGenerator
  89. ::GetTargetDirectory(cmTarget const& target) const
  90. {
  91. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  92. dir += target.GetName();
  93. #if defined(__VMS)
  94. dir += "_dir";
  95. #else
  96. dir += ".dir";
  97. #endif
  98. return dir;
  99. }
  100. //----------------------------------------------------------------------------
  101. // Non-virtual public methods.
  102. const cmGlobalNinjaGenerator*
  103. cmLocalNinjaGenerator::GetGlobalNinjaGenerator() const
  104. {
  105. return
  106. static_cast<const cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  107. }
  108. cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  109. {
  110. return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  111. }
  112. //----------------------------------------------------------------------------
  113. // Virtual protected methods.
  114. std::string
  115. cmLocalNinjaGenerator::ConvertToLinkReference(std::string const& lib,
  116. OutputFormat format)
  117. {
  118. return this->Convert(lib, HOME_OUTPUT, format);
  119. }
  120. std::string
  121. cmLocalNinjaGenerator::ConvertToIncludeReference(std::string const& path,
  122. OutputFormat format,
  123. bool forceFullPaths)
  124. {
  125. return this->Convert(path, forceFullPaths? FULL : HOME_OUTPUT, format);
  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. void cmLocalNinjaGenerator::WriteBuildFileTop()
  146. {
  147. // For the build file.
  148. this->WriteProjectHeader(this->GetBuildFileStream());
  149. this->WriteNinjaRequiredVersion(this->GetBuildFileStream());
  150. this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
  151. // For the rule file.
  152. this->WriteProjectHeader(this->GetRulesFileStream());
  153. }
  154. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  155. {
  156. cmGlobalNinjaGenerator::WriteDivider(os);
  157. os
  158. << "# Project: " << this->GetMakefile()->GetProjectName() << std::endl
  159. << "# Configuration: " << this->ConfigName << std::endl
  160. ;
  161. cmGlobalNinjaGenerator::WriteDivider(os);
  162. }
  163. void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
  164. {
  165. // Default required version
  166. std::string requiredVersion =
  167. this->GetGlobalNinjaGenerator()->RequiredNinjaVersion();
  168. // Ninja generator uses the 'console' pool if available (>= 1.5)
  169. if(this->GetGlobalNinjaGenerator()->SupportsConsolePool())
  170. {
  171. requiredVersion =
  172. this->GetGlobalNinjaGenerator()->RequiredNinjaVersionForConsolePool();
  173. }
  174. cmGlobalNinjaGenerator::WriteComment(os,
  175. "Minimal version of Ninja required by this file");
  176. os
  177. << "ninja_required_version = "
  178. << requiredVersion
  179. << std::endl << std::endl
  180. ;
  181. }
  182. void cmLocalNinjaGenerator::WritePools(std::ostream& os)
  183. {
  184. cmGlobalNinjaGenerator::WriteDivider(os);
  185. const char* jobpools = this->GetCMakeInstance()->GetState()
  186. ->GetGlobalProperty("JOB_POOLS");
  187. if (jobpools)
  188. {
  189. cmGlobalNinjaGenerator::WriteComment(os,
  190. "Pools defined by global property JOB_POOLS");
  191. std::vector<std::string> pools;
  192. cmSystemTools::ExpandListArgument(jobpools, pools);
  193. for (size_t i = 0; i < pools.size(); ++i)
  194. {
  195. const std::string pool = pools[i];
  196. const std::string::size_type eq = pool.find("=");
  197. unsigned int jobs;
  198. if (eq != std::string::npos &&
  199. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1)
  200. {
  201. os << "pool " << pool.substr(0, eq) << std::endl;
  202. os << " depth = " << jobs << std::endl;
  203. os << std::endl;
  204. }
  205. else
  206. {
  207. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': ",
  208. pool.c_str());
  209. }
  210. }
  211. }
  212. }
  213. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  214. {
  215. cmGlobalNinjaGenerator::WriteDivider(os);
  216. os
  217. << "# Include auxiliary files.\n"
  218. << "\n"
  219. ;
  220. cmGlobalNinjaGenerator::WriteInclude(os,
  221. cmGlobalNinjaGenerator::NINJA_RULES_FILE,
  222. "Include rules file.");
  223. os << "\n";
  224. }
  225. //----------------------------------------------------------------------------
  226. void cmLocalNinjaGenerator::ComputeObjectFilenames(
  227. std::map<cmSourceFile const*, std::string>& mapping,
  228. cmGeneratorTarget const* gt)
  229. {
  230. for(std::map<cmSourceFile const*, std::string>::iterator
  231. si = mapping.begin(); si != mapping.end(); ++si)
  232. {
  233. cmSourceFile const* sf = si->first;
  234. si->second = this->GetObjectFileNameWithoutTarget(*sf,
  235. gt->ObjectDirectory);
  236. }
  237. }
  238. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  239. {
  240. cmGlobalNinjaGenerator::WriteDivider(os);
  241. os
  242. << "# Write statements declared in CMakeLists.txt:" << std::endl
  243. << "# "
  244. << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE") << std::endl;
  245. if(this->Makefile->IsRootMakefile())
  246. os << "# Which is the root file." << std::endl;
  247. cmGlobalNinjaGenerator::WriteDivider(os);
  248. os << std::endl;
  249. }
  250. std::string cmLocalNinjaGenerator::ConvertToNinjaPath(const std::string& path)
  251. {
  252. std::string convPath = this->Convert(path, cmLocalGenerator::HOME_OUTPUT);
  253. #ifdef _WIN32
  254. cmSystemTools::ReplaceString(convPath, "/", "\\");
  255. #endif
  256. return convPath;
  257. }
  258. void
  259. cmLocalNinjaGenerator
  260. ::AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs)
  261. {
  262. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  263. }
  264. void
  265. cmLocalNinjaGenerator
  266. ::AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs)
  267. {
  268. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs);
  269. }
  270. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  271. cmCustomCommandGenerator const& ccg,
  272. cmNinjaDeps &ninjaDeps)
  273. {
  274. const std::vector<std::string> &deps = ccg.GetDepends();
  275. for (std::vector<std::string>::const_iterator i = deps.begin();
  276. i != deps.end(); ++i) {
  277. std::string dep;
  278. if (this->GetRealDependency(*i, this->GetConfigName(), dep))
  279. ninjaDeps.push_back(ConvertToNinjaPath(dep));
  280. }
  281. }
  282. std::string cmLocalNinjaGenerator::BuildCommandLine(
  283. const std::vector<std::string> &cmdLines)
  284. {
  285. // If we have no commands but we need to build a command anyway, use ":".
  286. // This happens when building a POST_BUILD value for link targets that
  287. // don't use POST_BUILD.
  288. if (cmdLines.empty())
  289. #ifdef _WIN32
  290. return "cd .";
  291. #else
  292. return ":";
  293. #endif
  294. std::ostringstream cmd;
  295. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  296. li != cmdLines.end(); ++li)
  297. #ifdef _WIN32
  298. {
  299. if (li != cmdLines.begin())
  300. {
  301. cmd << " && ";
  302. }
  303. else if (cmdLines.size() > 1)
  304. {
  305. cmd << "cmd.exe /C \"";
  306. }
  307. cmd << *li;
  308. }
  309. if (cmdLines.size() > 1)
  310. {
  311. cmd << "\"";
  312. }
  313. #else
  314. {
  315. if (li != cmdLines.begin())
  316. {
  317. cmd << " && ";
  318. }
  319. cmd << *li;
  320. }
  321. #endif
  322. return cmd.str();
  323. }
  324. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  325. cmCustomCommandGenerator const& ccg,
  326. std::vector<std::string> &cmdLines)
  327. {
  328. if (ccg.GetNumberOfCommands() > 0) {
  329. std::string wd = ccg.GetWorkingDirectory();
  330. if (wd.empty())
  331. wd = this->GetMakefile()->GetCurrentBinaryDirectory();
  332. std::ostringstream cdCmd;
  333. #ifdef _WIN32
  334. std::string cdStr = "cd /D ";
  335. #else
  336. std::string cdStr = "cd ";
  337. #endif
  338. cdCmd << cdStr << this->ConvertToOutputFormat(wd, SHELL);
  339. cmdLines.push_back(cdCmd.str());
  340. }
  341. std::string launcher = this->MakeCustomLauncher(ccg);
  342. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  343. cmdLines.push_back(launcher +
  344. this->ConvertToOutputFormat(ccg.GetCommand(i), SHELL));
  345. std::string& cmd = cmdLines.back();
  346. ccg.AppendArguments(i, cmd);
  347. }
  348. }
  349. void
  350. cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  351. cmCustomCommand const *cc, const cmNinjaDeps& orderOnlyDeps)
  352. {
  353. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc))
  354. return;
  355. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
  356. const std::vector<std::string> &outputs = ccg.GetOutputs();
  357. const std::vector<std::string> &byproducts = ccg.GetByproducts();
  358. cmNinjaDeps ninjaOutputs(outputs.size()+byproducts.size()), ninjaDeps;
  359. #if 0
  360. #error TODO: Once CC in an ExternalProject target must provide the \
  361. file of each imported target that has an add_dependencies pointing \
  362. at us. How to know which ExternalProject step actually provides it?
  363. #endif
  364. std::transform(outputs.begin(), outputs.end(),
  365. ninjaOutputs.begin(), MapToNinjaPath());
  366. std::transform(byproducts.begin(), byproducts.end(),
  367. ninjaOutputs.begin() + outputs.size(), MapToNinjaPath());
  368. this->AppendCustomCommandDeps(ccg, ninjaDeps);
  369. for (cmNinjaDeps::iterator i = ninjaOutputs.begin(); i != ninjaOutputs.end();
  370. ++i)
  371. this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(*i);
  372. std::vector<std::string> cmdLines;
  373. this->AppendCustomCommandLines(ccg, cmdLines);
  374. if (cmdLines.empty()) {
  375. this->GetGlobalNinjaGenerator()->WritePhonyBuild(
  376. this->GetBuildFileStream(),
  377. "Phony custom command for " +
  378. ninjaOutputs[0],
  379. ninjaOutputs,
  380. ninjaDeps,
  381. cmNinjaDeps(),
  382. orderOnlyDeps,
  383. cmNinjaVars());
  384. } else {
  385. this->GetGlobalNinjaGenerator()->WriteCustomCommandBuild(
  386. this->BuildCommandLine(cmdLines),
  387. this->ConstructComment(ccg),
  388. "Custom command for " + ninjaOutputs[0],
  389. cc->GetUsesTerminal(),
  390. ninjaOutputs,
  391. ninjaDeps,
  392. orderOnlyDeps);
  393. }
  394. }
  395. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  396. cmTarget* target)
  397. {
  398. this->CustomCommandTargets[cc].insert(target);
  399. }
  400. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
  401. {
  402. for (CustomCommandTargetMap::iterator i = this->CustomCommandTargets.begin();
  403. i != this->CustomCommandTargets.end(); ++i) {
  404. // A custom command may appear on multiple targets. However, some build
  405. // systems exist where the target dependencies on some of the targets are
  406. // overspecified, leading to a dependency cycle. If we assume all target
  407. // dependencies are a superset of the true target dependencies for this
  408. // custom command, we can take the set intersection of all target
  409. // dependencies to obtain a correct dependency list.
  410. //
  411. // FIXME: This won't work in certain obscure scenarios involving indirect
  412. // dependencies.
  413. std::set<cmTarget*>::iterator j = i->second.begin();
  414. assert(j != i->second.end());
  415. std::vector<std::string> ccTargetDeps;
  416. this->AppendTargetDepends(*j, ccTargetDeps);
  417. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  418. ++j;
  419. for (; j != i->second.end(); ++j) {
  420. std::vector<std::string> jDeps, depsIntersection;
  421. this->AppendTargetDepends(*j, jDeps);
  422. std::sort(jDeps.begin(), jDeps.end());
  423. std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
  424. jDeps.begin(), jDeps.end(),
  425. std::back_inserter(depsIntersection));
  426. ccTargetDeps = depsIntersection;
  427. }
  428. this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
  429. }
  430. }
  431. std::string cmLocalNinjaGenerator::MakeCustomLauncher(
  432. cmCustomCommandGenerator const& ccg)
  433. {
  434. const char* property = "RULE_LAUNCH_CUSTOM";
  435. const char* property_value = this->Makefile->GetProperty(property);
  436. if(!property_value || !*property_value)
  437. {
  438. return std::string();
  439. }
  440. // Expand rules in the empty string. It may insert the launcher and
  441. // perform replacements.
  442. RuleVariables vars;
  443. vars.RuleLauncher = property;
  444. std::string output;
  445. const std::vector<std::string>& outputs = ccg.GetOutputs();
  446. if(!outputs.empty())
  447. {
  448. RelativeRoot relative_root =
  449. ccg.GetWorkingDirectory().empty() ? START_OUTPUT : NONE;
  450. output = this->Convert(outputs[0], relative_root, SHELL);
  451. }
  452. vars.Output = output.c_str();
  453. std::string launcher;
  454. this->ExpandRuleVariables(launcher, vars);
  455. if(!launcher.empty())
  456. {
  457. launcher += " ";
  458. }
  459. return launcher;
  460. }