cmNinjaNormalTargetGenerator.cxx 24 KB

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