cmLocalNinjaGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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->Convert(
  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. return this->Convert(path, forceFullPaths ? cmOutputConverter::FULL
  114. : cmOutputConverter::HOME_OUTPUT,
  115. format);
  116. }
  117. // Private methods.
  118. cmGeneratedFileStream& cmLocalNinjaGenerator::GetBuildFileStream() const
  119. {
  120. return *this->GetGlobalNinjaGenerator()->GetBuildFileStream();
  121. }
  122. cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
  123. {
  124. return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
  125. }
  126. const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
  127. {
  128. return this->GetGlobalGenerator()->GetCMakeInstance();
  129. }
  130. cmake* cmLocalNinjaGenerator::GetCMakeInstance()
  131. {
  132. return this->GetGlobalGenerator()->GetCMakeInstance();
  133. }
  134. void cmLocalNinjaGenerator::WriteBuildFileTop()
  135. {
  136. // For the build file.
  137. this->WriteProjectHeader(this->GetBuildFileStream());
  138. this->WriteNinjaRequiredVersion(this->GetBuildFileStream());
  139. this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
  140. // For the rule file.
  141. this->WriteProjectHeader(this->GetRulesFileStream());
  142. }
  143. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  144. {
  145. cmGlobalNinjaGenerator::WriteDivider(os);
  146. os << "# Project: " << this->GetProjectName() << std::endl
  147. << "# Configuration: " << this->ConfigName << std::endl;
  148. cmGlobalNinjaGenerator::WriteDivider(os);
  149. }
  150. void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
  151. {
  152. // Default required version
  153. std::string requiredVersion =
  154. this->GetGlobalNinjaGenerator()->RequiredNinjaVersion();
  155. // Ninja generator uses the 'console' pool if available (>= 1.5)
  156. if (this->GetGlobalNinjaGenerator()->SupportsConsolePool()) {
  157. requiredVersion =
  158. this->GetGlobalNinjaGenerator()->RequiredNinjaVersionForConsolePool();
  159. }
  160. cmGlobalNinjaGenerator::WriteComment(
  161. os, "Minimal version of Ninja required by this file");
  162. os << "ninja_required_version = " << requiredVersion << std::endl
  163. << std::endl;
  164. }
  165. void cmLocalNinjaGenerator::WritePools(std::ostream& os)
  166. {
  167. cmGlobalNinjaGenerator::WriteDivider(os);
  168. const char* jobpools =
  169. this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
  170. if (jobpools) {
  171. cmGlobalNinjaGenerator::WriteComment(
  172. os, "Pools defined by global property JOB_POOLS");
  173. std::vector<std::string> pools;
  174. cmSystemTools::ExpandListArgument(jobpools, pools);
  175. for (size_t i = 0; i < pools.size(); ++i) {
  176. const std::string pool = pools[i];
  177. const std::string::size_type eq = pool.find('=');
  178. unsigned int jobs;
  179. if (eq != std::string::npos &&
  180. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
  181. os << "pool " << pool.substr(0, eq) << std::endl;
  182. os << " depth = " << jobs << std::endl;
  183. os << std::endl;
  184. } else {
  185. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': ",
  186. pool.c_str());
  187. }
  188. }
  189. }
  190. }
  191. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  192. {
  193. cmGlobalNinjaGenerator::WriteDivider(os);
  194. os << "# Include auxiliary files.\n"
  195. << "\n";
  196. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  197. std::string const ninjaRulesFile =
  198. ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
  199. std::string const rulesFilePath =
  200. ng->EncodeIdent(ng->EncodePath(ninjaRulesFile), os);
  201. cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
  202. "Include rules file.");
  203. os << "\n";
  204. }
  205. void cmLocalNinjaGenerator::ComputeObjectFilenames(
  206. std::map<cmSourceFile const*, std::string>& mapping,
  207. cmGeneratorTarget const* gt)
  208. {
  209. for (std::map<cmSourceFile const*, std::string>::iterator si =
  210. mapping.begin();
  211. si != mapping.end(); ++si) {
  212. cmSourceFile const* sf = si->first;
  213. si->second =
  214. this->GetObjectFileNameWithoutTarget(*sf, gt->ObjectDirectory);
  215. }
  216. }
  217. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  218. {
  219. cmGlobalNinjaGenerator::WriteDivider(os);
  220. os << "# Write statements declared in CMakeLists.txt:" << std::endl
  221. << "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
  222. << std::endl;
  223. if (this->IsRootMakefile()) {
  224. os << "# Which is the root file." << std::endl;
  225. }
  226. cmGlobalNinjaGenerator::WriteDivider(os);
  227. os << std::endl;
  228. }
  229. void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
  230. cmNinjaDeps& outputs)
  231. {
  232. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  233. }
  234. void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
  235. cmNinjaDeps& outputs)
  236. {
  237. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs);
  238. }
  239. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  240. cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps)
  241. {
  242. const std::vector<std::string>& deps = ccg.GetDepends();
  243. for (std::vector<std::string>::const_iterator i = deps.begin();
  244. i != deps.end(); ++i) {
  245. std::string dep;
  246. if (this->GetRealDependency(*i, this->GetConfigName(), dep)) {
  247. ninjaDeps.push_back(
  248. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  249. }
  250. }
  251. }
  252. std::string cmLocalNinjaGenerator::BuildCommandLine(
  253. const std::vector<std::string>& cmdLines)
  254. {
  255. // If we have no commands but we need to build a command anyway, use ":".
  256. // This happens when building a POST_BUILD value for link targets that
  257. // don't use POST_BUILD.
  258. if (cmdLines.empty()) {
  259. #ifdef _WIN32
  260. return "cd .";
  261. #else
  262. return ":";
  263. #endif
  264. }
  265. std::ostringstream cmd;
  266. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  267. li != cmdLines.end(); ++li)
  268. #ifdef _WIN32
  269. {
  270. if (li != cmdLines.begin()) {
  271. cmd << " && ";
  272. } else if (cmdLines.size() > 1) {
  273. cmd << "cmd.exe /C \"";
  274. }
  275. cmd << *li;
  276. }
  277. if (cmdLines.size() > 1) {
  278. cmd << "\"";
  279. }
  280. #else
  281. {
  282. if (li != cmdLines.begin()) {
  283. cmd << " && ";
  284. }
  285. cmd << *li;
  286. }
  287. #endif
  288. return cmd.str();
  289. }
  290. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  291. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  292. {
  293. if (ccg.GetNumberOfCommands() > 0) {
  294. std::string wd = ccg.GetWorkingDirectory();
  295. if (wd.empty()) {
  296. wd = this->GetCurrentBinaryDirectory();
  297. }
  298. std::ostringstream cdCmd;
  299. #ifdef _WIN32
  300. std::string cdStr = "cd /D ";
  301. #else
  302. std::string cdStr = "cd ";
  303. #endif
  304. cdCmd << cdStr
  305. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  306. cmdLines.push_back(cdCmd.str());
  307. }
  308. std::string launcher = this->MakeCustomLauncher(ccg);
  309. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  310. cmdLines.push_back(launcher +
  311. this->ConvertToOutputFormat(ccg.GetCommand(i),
  312. cmOutputConverter::SHELL));
  313. std::string& cmd = cmdLines.back();
  314. ccg.AppendArguments(i, cmd);
  315. }
  316. }
  317. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  318. cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
  319. {
  320. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc)) {
  321. return;
  322. }
  323. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
  324. const std::vector<std::string>& outputs = ccg.GetOutputs();
  325. const std::vector<std::string>& byproducts = ccg.GetByproducts();
  326. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size()), ninjaDeps;
  327. bool symbolic = false;
  328. for (std::vector<std::string>::const_iterator o = outputs.begin();
  329. !symbolic && o != outputs.end(); ++o) {
  330. if (cmSourceFile* sf = this->Makefile->GetSource(*o)) {
  331. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  332. }
  333. }
  334. #if 0
  335. #error TODO: Once CC in an ExternalProject target must provide the \
  336. file of each imported target that has an add_dependencies pointing \
  337. at us. How to know which ExternalProject step actually provides it?
  338. #endif
  339. std::transform(outputs.begin(), outputs.end(), ninjaOutputs.begin(),
  340. this->GetGlobalNinjaGenerator()->MapToNinjaPath());
  341. std::transform(byproducts.begin(), byproducts.end(),
  342. ninjaOutputs.begin() + outputs.size(),
  343. this->GetGlobalNinjaGenerator()->MapToNinjaPath());
  344. this->AppendCustomCommandDeps(ccg, ninjaDeps);
  345. for (cmNinjaDeps::iterator i = ninjaOutputs.begin(); i != ninjaOutputs.end();
  346. ++i) {
  347. this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(*i);
  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->GetUsesTerminal(),
  360. /*restat*/ !symbolic || !byproducts.empty(), ninjaOutputs, ninjaDeps,
  361. orderOnlyDeps);
  362. }
  363. }
  364. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  365. cmGeneratorTarget* target)
  366. {
  367. CustomCommandTargetMap::value_type v(cc, std::set<cmGeneratorTarget*>());
  368. std::pair<CustomCommandTargetMap::iterator, bool> ins =
  369. this->CustomCommandTargets.insert(v);
  370. if (ins.second) {
  371. this->CustomCommands.push_back(cc);
  372. }
  373. ins.first->second.insert(target);
  374. }
  375. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
  376. {
  377. for (std::vector<cmCustomCommand const*>::iterator vi =
  378. this->CustomCommands.begin();
  379. vi != this->CustomCommands.end(); ++vi) {
  380. CustomCommandTargetMap::iterator i = this->CustomCommandTargets.find(*vi);
  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->AppendTargetDepends(*j, ccTargetDeps);
  395. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  396. ++j;
  397. for (; j != i->second.end(); ++j) {
  398. std::vector<std::string> jDeps, depsIntersection;
  399. this->AppendTargetDepends(*j, jDeps);
  400. std::sort(jDeps.begin(), jDeps.end());
  401. std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
  402. jDeps.begin(), jDeps.end(),
  403. std::back_inserter(depsIntersection));
  404. ccTargetDeps = depsIntersection;
  405. }
  406. this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
  407. }
  408. }
  409. std::string cmLocalNinjaGenerator::MakeCustomLauncher(
  410. cmCustomCommandGenerator const& ccg)
  411. {
  412. const char* property = "RULE_LAUNCH_CUSTOM";
  413. const char* property_value = this->Makefile->GetProperty(property);
  414. if (!property_value || !*property_value) {
  415. return std::string();
  416. }
  417. // Expand rules in the empty string. It may insert the launcher and
  418. // perform replacements.
  419. RuleVariables vars;
  420. vars.RuleLauncher = property;
  421. std::string output;
  422. const std::vector<std::string>& outputs = ccg.GetOutputs();
  423. if (!outputs.empty()) {
  424. cmOutputConverter::RelativeRoot relative_root =
  425. ccg.GetWorkingDirectory().empty() ? cmOutputConverter::START_OUTPUT
  426. : cmOutputConverter::NONE;
  427. output =
  428. this->Convert(outputs[0], relative_root, cmOutputConverter::SHELL);
  429. }
  430. vars.Output = output.c_str();
  431. std::string launcher;
  432. this->ExpandRuleVariables(launcher, vars);
  433. if (!launcher.empty()) {
  434. launcher += " ";
  435. }
  436. return launcher;
  437. }