cmLocalNinjaGenerator.cxx 17 KB

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