cmLocalNinjaGenerator.cxx 16 KB

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