cmNinjaNormalTargetGenerator.cxx 25 KB

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