cmLocalNinjaGenerator.cxx 16 KB

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