cmLocalNinjaGenerator.cxx 16 KB

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