cmNinjaNormalTargetGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 "cmNinjaNormalTargetGenerator.h"
  12. #include "cmLocalNinjaGenerator.h"
  13. #include "cmGlobalNinjaGenerator.h"
  14. #include "cmSourceFile.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmMakefile.h"
  17. #include "cmOSXBundleGenerator.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmCustomCommandGenerator.h"
  20. #include <assert.h>
  21. #include <algorithm>
  22. #include <limits>
  23. #ifndef _WIN32
  24. #include <unistd.h>
  25. #endif
  26. cmNinjaNormalTargetGenerator::
  27. cmNinjaNormalTargetGenerator(cmGeneratorTarget* target)
  28. : cmNinjaTargetGenerator(target->Target)
  29. , TargetNameOut()
  30. , TargetNameSO()
  31. , TargetNameReal()
  32. , TargetNameImport()
  33. , TargetNamePDB()
  34. , TargetLinkLanguage("")
  35. {
  36. this->TargetLinkLanguage = target->Target
  37. ->GetLinkerLanguage(this->GetConfigName());
  38. if (target->GetType() == cmTarget::EXECUTABLE)
  39. target->Target->GetExecutableNames(this->TargetNameOut,
  40. this->TargetNameReal,
  41. this->TargetNameImport,
  42. this->TargetNamePDB,
  43. GetLocalGenerator()->GetConfigName());
  44. else
  45. target->Target->GetLibraryNames(this->TargetNameOut,
  46. this->TargetNameSO,
  47. this->TargetNameReal,
  48. this->TargetNameImport,
  49. this->TargetNamePDB,
  50. GetLocalGenerator()->GetConfigName());
  51. if(target->GetType() != cmTarget::OBJECT_LIBRARY)
  52. {
  53. // on Windows the output dir is already needed at compile time
  54. // ensure the directory exists (OutDir test)
  55. EnsureDirectoryExists(target->Target->GetDirectory(this->GetConfigName()));
  56. }
  57. this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
  58. this->GetConfigName());
  59. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  60. }
  61. cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
  62. {
  63. delete this->OSXBundleGenerator;
  64. }
  65. void cmNinjaNormalTargetGenerator::Generate()
  66. {
  67. if (this->TargetLinkLanguage.empty()) {
  68. cmSystemTools::Error("CMake can not determine linker language for "
  69. "target: ",
  70. this->GetTarget()->GetName().c_str());
  71. return;
  72. }
  73. // Write the rules for each language.
  74. this->WriteLanguagesRules();
  75. // Write the build statements
  76. this->WriteObjectBuildStatements();
  77. if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
  78. {
  79. this->WriteObjectLibStatement();
  80. }
  81. else
  82. {
  83. this->WriteLinkRule(false); // write rule without rspfile support
  84. this->WriteLinkRule(true); // write rule with rspfile support
  85. this->WriteLinkStatement();
  86. }
  87. }
  88. void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
  89. {
  90. #ifdef NINJA_GEN_VERBOSE_FILES
  91. cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  92. this->GetRulesFileStream()
  93. << "# Rules for each languages for "
  94. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  95. << " target "
  96. << this->GetTargetName()
  97. << "\n\n";
  98. #endif
  99. // Write rules for languages compiled in this target.
  100. std::set<std::string> languages;
  101. std::vector<cmSourceFile*> sourceFiles;
  102. this->GetTarget()->GetSourceFiles(sourceFiles,
  103. this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  104. for(std::vector<cmSourceFile*>::const_iterator
  105. i = sourceFiles.begin(); i != sourceFiles.end(); ++i)
  106. {
  107. const std::string& lang = (*i)->GetLanguage();
  108. if(!lang.empty())
  109. {
  110. languages.insert(lang);
  111. }
  112. }
  113. for(std::set<std::string>::const_iterator l = languages.begin();
  114. l != languages.end();
  115. ++l)
  116. {
  117. this->WriteLanguageRules(*l);
  118. }
  119. }
  120. const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
  121. {
  122. switch (this->GetTarget()->GetType()) {
  123. case cmTarget::STATIC_LIBRARY:
  124. return "static library";
  125. case cmTarget::SHARED_LIBRARY:
  126. return "shared library";
  127. case cmTarget::MODULE_LIBRARY:
  128. if (this->GetTarget()->IsCFBundleOnApple())
  129. return "CFBundle shared module";
  130. else
  131. return "shared module";
  132. case cmTarget::EXECUTABLE:
  133. return "executable";
  134. default:
  135. return 0;
  136. }
  137. }
  138. std::string
  139. cmNinjaNormalTargetGenerator
  140. ::LanguageLinkerRule() const
  141. {
  142. return this->TargetLinkLanguage
  143. + "_"
  144. + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  145. + "_LINKER";
  146. }
  147. void
  148. cmNinjaNormalTargetGenerator
  149. ::WriteLinkRule(bool useResponseFile)
  150. {
  151. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  152. std::string ruleName = this->LanguageLinkerRule();
  153. if (useResponseFile)
  154. ruleName += "_RSP_FILE";
  155. // Select whether to use a response file for objects.
  156. std::string rspfile;
  157. std::string rspcontent;
  158. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  159. cmLocalGenerator::RuleVariables vars;
  160. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  161. vars.CMTarget = this->GetTarget();
  162. vars.Language = this->TargetLinkLanguage.c_str();
  163. std::string responseFlag;
  164. if (!useResponseFile) {
  165. vars.Objects = "$in";
  166. vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES";
  167. } else {
  168. std::string cmakeVarLang = "CMAKE_";
  169. cmakeVarLang += this->TargetLinkLanguage;
  170. // build response file name
  171. std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
  172. const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar);
  173. if(flag) {
  174. responseFlag = flag;
  175. } else {
  176. responseFlag = "@";
  177. }
  178. rspfile = "$RSP_FILE";
  179. responseFlag += rspfile;
  180. // build response file content
  181. rspcontent = "$in_newline $LINK_PATH $LINK_LIBRARIES";
  182. vars.Objects = responseFlag.c_str();
  183. vars.LinkLibraries = "";
  184. }
  185. vars.ObjectDir = "$OBJECT_DIR";
  186. vars.Target = "$TARGET_FILE";
  187. vars.SONameFlag = "$SONAME_FLAG";
  188. vars.TargetSOName = "$SONAME";
  189. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  190. vars.TargetPDB = "$TARGET_PDB";
  191. // Setup the target version.
  192. std::string targetVersionMajor;
  193. std::string targetVersionMinor;
  194. {
  195. std::ostringstream majorStream;
  196. std::ostringstream minorStream;
  197. int major;
  198. int minor;
  199. this->GetTarget()->GetTargetVersion(major, minor);
  200. majorStream << major;
  201. minorStream << minor;
  202. targetVersionMajor = majorStream.str();
  203. targetVersionMinor = minorStream.str();
  204. }
  205. vars.TargetVersionMajor = targetVersionMajor.c_str();
  206. vars.TargetVersionMinor = targetVersionMinor.c_str();
  207. vars.Flags = "$FLAGS";
  208. vars.LinkFlags = "$LINK_FLAGS";
  209. std::string langFlags;
  210. if (targetType != cmTarget::EXECUTABLE)
  211. {
  212. langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
  213. vars.LanguageCompileFlags = langFlags.c_str();
  214. }
  215. // Rule for linking library/executable.
  216. std::vector<std::string> linkCmds = this->ComputeLinkCmd();
  217. for(std::vector<std::string>::iterator i = linkCmds.begin();
  218. i != linkCmds.end();
  219. ++i)
  220. {
  221. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  222. }
  223. linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
  224. linkCmds.push_back("$POST_BUILD");
  225. std::string linkCmd =
  226. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  227. // Write the linker rule with response file if needed.
  228. std::ostringstream comment;
  229. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  230. << this->GetVisibleTypeName() << ".";
  231. std::ostringstream description;
  232. description << "Linking " << this->TargetLinkLanguage << " "
  233. << this->GetVisibleTypeName() << " $TARGET_FILE";
  234. this->GetGlobalGenerator()->AddRule(ruleName,
  235. linkCmd,
  236. description.str(),
  237. comment.str(),
  238. /*depfile*/ "",
  239. /*deptype*/ "",
  240. rspfile,
  241. rspcontent,
  242. /*restat*/ "$RESTAT",
  243. /*generator*/ false);
  244. }
  245. if (this->TargetNameOut != this->TargetNameReal &&
  246. !this->GetTarget()->IsFrameworkOnApple()) {
  247. std::string cmakeCommand =
  248. this->GetLocalGenerator()->ConvertToOutputFormat(
  249. this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
  250. cmLocalGenerator::SHELL);
  251. if (targetType == cmTarget::EXECUTABLE)
  252. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
  253. cmakeCommand +
  254. " -E cmake_symlink_executable"
  255. " $in $out && $POST_BUILD",
  256. "Creating executable symlink $out",
  257. "Rule for creating "
  258. "executable symlink.",
  259. /*depfile*/ "",
  260. /*deptype*/ "",
  261. /*rspfile*/ "",
  262. /*rspcontent*/ "",
  263. /*restat*/ "",
  264. /*generator*/ false);
  265. else
  266. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
  267. cmakeCommand +
  268. " -E cmake_symlink_library"
  269. " $in $SONAME $out && $POST_BUILD",
  270. "Creating library symlink $out",
  271. "Rule for creating "
  272. "library symlink.",
  273. /*depfile*/ "",
  274. /*deptype*/ "",
  275. /*rspfile*/ "",
  276. /*rspcontent*/ "",
  277. /*restat*/ "",
  278. /*generator*/ false);
  279. }
  280. }
  281. std::vector<std::string>
  282. cmNinjaNormalTargetGenerator
  283. ::ComputeLinkCmd()
  284. {
  285. std::vector<std::string> linkCmds;
  286. cmMakefile* mf = this->GetMakefile();
  287. {
  288. std::string linkCmdVar = this->GetGeneratorTarget()
  289. ->GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName());
  290. const char *linkCmd = mf->GetDefinition(linkCmdVar);
  291. if (linkCmd)
  292. {
  293. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  294. return linkCmds;
  295. }
  296. }
  297. switch (this->GetTarget()->GetType()) {
  298. case cmTarget::STATIC_LIBRARY: {
  299. // We have archive link commands set. First, delete the existing archive.
  300. {
  301. std::string cmakeCommand =
  302. this->GetLocalGenerator()->ConvertToOutputFormat(
  303. mf->GetRequiredDefinition("CMAKE_COMMAND"),
  304. cmLocalGenerator::SHELL);
  305. linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
  306. }
  307. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
  308. {
  309. std::string linkCmdVar = "CMAKE_";
  310. linkCmdVar += this->TargetLinkLanguage;
  311. linkCmdVar += "_ARCHIVE_CREATE";
  312. const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  313. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  314. }
  315. {
  316. std::string linkCmdVar = "CMAKE_";
  317. linkCmdVar += this->TargetLinkLanguage;
  318. linkCmdVar += "_ARCHIVE_FINISH";
  319. const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  320. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  321. }
  322. return linkCmds;
  323. }
  324. case cmTarget::SHARED_LIBRARY:
  325. case cmTarget::MODULE_LIBRARY:
  326. case cmTarget::EXECUTABLE:
  327. break;
  328. default:
  329. assert(0 && "Unexpected target type");
  330. }
  331. return std::vector<std::string>();
  332. }
  333. static int calculateCommandLineLengthLimit(int linkRuleLength)
  334. {
  335. static int const limits[] = {
  336. #ifdef _WIN32
  337. 8000,
  338. #endif
  339. #if defined(__APPLE__) || defined(__HAIKU__) || defined(__linux)
  340. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  341. ((int)sysconf(_SC_ARG_MAX)) - 1000,
  342. #endif
  343. #if defined(__linux)
  344. // #define MAX_ARG_STRLEN (PAGE_SIZE * 32) in Linux's binfmts.h
  345. ((int)sysconf(_SC_PAGESIZE) * 32) - 1000,
  346. #endif
  347. std::numeric_limits<int>::max()
  348. };
  349. size_t const arrSz = cmArraySize(limits);
  350. int const sz = *std::min_element(limits, limits + arrSz);
  351. if (sz == std::numeric_limits<int>::max())
  352. {
  353. return -1;
  354. }
  355. return sz - linkRuleLength;
  356. }
  357. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  358. {
  359. cmTarget& target = *this->GetTarget();
  360. const std::string cfgName = this->GetConfigName();
  361. std::string targetOutput = ConvertToNinjaPath(
  362. target.GetFullPath(cfgName));
  363. std::string targetOutputReal = ConvertToNinjaPath(
  364. target.GetFullPath(cfgName,
  365. /*implib=*/false,
  366. /*realpath=*/true));
  367. std::string targetOutputImplib = ConvertToNinjaPath(
  368. target.GetFullPath(cfgName,
  369. /*implib=*/true));
  370. if (target.IsAppBundleOnApple())
  371. {
  372. // Create the app bundle
  373. std::string outpath = target.GetDirectory(cfgName);
  374. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  375. // Calculate the output path
  376. targetOutput = outpath;
  377. targetOutput += "/";
  378. targetOutput += this->TargetNameOut;
  379. targetOutput = this->ConvertToNinjaPath(targetOutput);
  380. targetOutputReal = outpath;
  381. targetOutputReal += "/";
  382. targetOutputReal += this->TargetNameReal;
  383. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
  384. }
  385. else if (target.IsFrameworkOnApple())
  386. {
  387. // Create the library framework.
  388. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  389. target.GetDirectory(cfgName));
  390. }
  391. else if(target.IsCFBundleOnApple())
  392. {
  393. // Create the core foundation bundle.
  394. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  395. target.GetDirectory(cfgName));
  396. }
  397. // Write comments.
  398. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  399. const cmTarget::TargetType targetType = target.GetType();
  400. this->GetBuildFileStream()
  401. << "# Link build statements for "
  402. << cmTarget::GetTargetTypeName(targetType)
  403. << " target "
  404. << this->GetTargetName()
  405. << "\n\n";
  406. cmNinjaDeps emptyDeps;
  407. cmNinjaVars vars;
  408. // Compute the comment.
  409. std::ostringstream comment;
  410. comment <<
  411. "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
  412. // Compute outputs.
  413. cmNinjaDeps outputs;
  414. outputs.push_back(targetOutputReal);
  415. // Compute specific libraries to link with.
  416. cmNinjaDeps explicitDeps = this->GetObjects();
  417. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  418. cmMakefile* mf = this->GetMakefile();
  419. std::string frameworkPath;
  420. std::string linkPath;
  421. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  422. std::string createRule =
  423. genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
  424. this->GetConfigName());
  425. bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
  426. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  427. vars["TARGET_FILE"] =
  428. localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);
  429. localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
  430. vars["FLAGS"],
  431. vars["LINK_FLAGS"],
  432. frameworkPath,
  433. linkPath,
  434. &genTarget,
  435. useWatcomQuote);
  436. this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
  437. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  438. vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
  439. ::EncodeLiteral(vars["LINK_FLAGS"]);
  440. vars["LINK_PATH"] = frameworkPath + linkPath;
  441. // Compute architecture specific link flags. Yes, these go into a different
  442. // variable for executables, probably due to a mistake made when duplicating
  443. // code between the Makefile executable and library generators.
  444. if (targetType == cmTarget::EXECUTABLE)
  445. {
  446. std::string t = vars["FLAGS"];
  447. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  448. vars["FLAGS"] = t;
  449. }
  450. else
  451. {
  452. std::string t = vars["ARCH_FLAGS"];
  453. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  454. vars["ARCH_FLAGS"] = t;
  455. t = "";
  456. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  457. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  458. }
  459. if (target.HasSOName(cfgName))
  460. {
  461. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  462. vars["SONAME"] = this->TargetNameSO;
  463. if (targetType == cmTarget::SHARED_LIBRARY)
  464. {
  465. std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
  466. if (!install_dir.empty())
  467. {
  468. vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
  469. cmLocalGenerator::NONE,
  470. cmLocalGenerator::SHELL,
  471. false);
  472. }
  473. }
  474. }
  475. if (!this->TargetNameImport.empty())
  476. {
  477. const std::string impLibPath = localGen.ConvertToOutputFormat(
  478. targetOutputImplib,
  479. cmLocalGenerator::SHELL);
  480. vars["TARGET_IMPLIB"] = impLibPath;
  481. EnsureParentDirectoryExists(impLibPath);
  482. if(target.HasImportLibrary())
  483. {
  484. outputs.push_back(targetOutputImplib);
  485. }
  486. }
  487. if (!this->SetMsvcTargetPdbVariable(vars))
  488. {
  489. // It is common to place debug symbols at a specific place,
  490. // so we need a plain target name in the rule available.
  491. std::string prefix;
  492. std::string base;
  493. std::string suffix;
  494. target.GetFullNameComponents(prefix, base, suffix);
  495. std::string dbg_suffix = ".dbg";
  496. // TODO: Where to document?
  497. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  498. {
  499. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  500. }
  501. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  502. }
  503. if (mf->IsOn("CMAKE_COMPILER_IS_MINGW"))
  504. {
  505. const std::string objPath = GetTarget()->GetSupportDirectory();
  506. vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath);
  507. EnsureDirectoryExists(objPath);
  508. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  509. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  510. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  511. std::string& link_path = vars["LINK_PATH"];
  512. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  513. }
  514. const std::vector<cmCustomCommand> *cmdLists[3] = {
  515. &target.GetPreBuildCommands(),
  516. &target.GetPreLinkCommands(),
  517. &target.GetPostBuildCommands()
  518. };
  519. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  520. std::vector<std::string> *cmdLineLists[3] = {
  521. &preLinkCmdLines,
  522. &preLinkCmdLines,
  523. &postBuildCmdLines
  524. };
  525. cmNinjaDeps byproducts;
  526. for (unsigned i = 0; i != 3; ++i)
  527. {
  528. for (std::vector<cmCustomCommand>::const_iterator
  529. ci = cmdLists[i]->begin();
  530. ci != cmdLists[i]->end(); ++ci)
  531. {
  532. cmCustomCommandGenerator ccg(*ci, cfgName, mf);
  533. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  534. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  535. std::transform(ccByproducts.begin(), ccByproducts.end(),
  536. std::back_inserter(byproducts), MapToNinjaPath());
  537. }
  538. }
  539. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  540. // the link commands.
  541. if (!preLinkCmdLines.empty())
  542. {
  543. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  544. mf->GetHomeOutputDirectory(),
  545. cmLocalGenerator::SHELL);
  546. preLinkCmdLines.push_back("cd " + homeOutDir);
  547. }
  548. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  549. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  550. cmNinjaVars symlinkVars;
  551. if (targetOutput == targetOutputReal)
  552. {
  553. vars["POST_BUILD"] = postBuildCmdLine;
  554. }
  555. else
  556. {
  557. vars["POST_BUILD"] = ":";
  558. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  559. }
  560. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  561. int commandLineLengthLimit = 1;
  562. const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  563. if (!mf->IsDefinitionSet(forceRspFile) &&
  564. cmSystemTools::GetEnv(forceRspFile) == 0)
  565. {
  566. commandLineLengthLimit = calculateCommandLineLengthLimit(
  567. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  568. }
  569. const std::string rspfile =
  570. std::string(cmake::GetCMakeFilesDirectoryPostSlash())
  571. + target.GetName() + ".rsp";
  572. // Gather order-only dependencies.
  573. cmNinjaDeps orderOnlyDeps;
  574. this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
  575. orderOnlyDeps);
  576. // Ninja should restat after linking if and only if there are byproducts.
  577. vars["RESTAT"] = byproducts.empty()? "" : "1";
  578. for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
  579. oe = byproducts.end();
  580. oi != oe; ++oi)
  581. {
  582. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  583. outputs.push_back(*oi);
  584. }
  585. // Write the build statement for this target.
  586. globalGen.WriteBuild(this->GetBuildFileStream(),
  587. comment.str(),
  588. this->LanguageLinkerRule(),
  589. outputs,
  590. explicitDeps,
  591. implicitDeps,
  592. orderOnlyDeps,
  593. vars,
  594. rspfile,
  595. commandLineLengthLimit);
  596. if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
  597. {
  598. if (targetType == cmTarget::EXECUTABLE)
  599. {
  600. globalGen.WriteBuild(this->GetBuildFileStream(),
  601. "Create executable symlink " + targetOutput,
  602. "CMAKE_SYMLINK_EXECUTABLE",
  603. cmNinjaDeps(1, targetOutput),
  604. cmNinjaDeps(1, targetOutputReal),
  605. emptyDeps,
  606. emptyDeps,
  607. symlinkVars);
  608. }
  609. else
  610. {
  611. cmNinjaDeps symlinks;
  612. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  613. // If one link has to be created.
  614. if (targetOutputReal == soName || targetOutput == soName)
  615. {
  616. symlinkVars["SONAME"] = soName;
  617. }
  618. else
  619. {
  620. symlinkVars["SONAME"] = "";
  621. symlinks.push_back(soName);
  622. }
  623. symlinks.push_back(targetOutput);
  624. globalGen.WriteBuild(this->GetBuildFileStream(),
  625. "Create library symlink " + targetOutput,
  626. "CMAKE_SYMLINK_LIBRARY",
  627. symlinks,
  628. cmNinjaDeps(1, targetOutputReal),
  629. emptyDeps,
  630. emptyDeps,
  631. symlinkVars);
  632. }
  633. }
  634. // Add aliases for the file name and the target name.
  635. globalGen.AddTargetAlias(this->TargetNameOut, &target);
  636. globalGen.AddTargetAlias(this->GetTargetName(), &target);
  637. }
  638. //----------------------------------------------------------------------------
  639. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  640. {
  641. // Write a phony output that depends on all object files.
  642. cmNinjaDeps outputs;
  643. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  644. cmNinjaDeps depends = this->GetObjects();
  645. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  646. "Object library "
  647. + this->GetTargetName(),
  648. outputs,
  649. depends);
  650. // Add aliases for the target name.
  651. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  652. this->GetTarget());
  653. }