cmLocalNinjaGenerator.cxx 16 KB

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