cmLocalNinjaGenerator.cxx 16 KB

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