cmNinjaNormalTargetGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. // TODO:
  173. // Makefile generator expands <TARGET> to the plain target name
  174. // with suffix. $out expands to a relative path. This difference
  175. // could make trouble when switching to Ninja generator. Maybe
  176. // using TARGET_NAME and RuleVariables::TargetName is a fix.
  177. vars.Target = "$out";
  178. vars.SONameFlag = "$SONAME_FLAG";
  179. vars.TargetSOName = "$SONAME";
  180. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  181. vars.TargetPDB = "$TARGET_PDB";
  182. // Setup the target version.
  183. std::string targetVersionMajor;
  184. std::string targetVersionMinor;
  185. {
  186. cmOStringStream majorStream;
  187. cmOStringStream minorStream;
  188. int major;
  189. int minor;
  190. this->GetTarget()->GetTargetVersion(major, minor);
  191. majorStream << major;
  192. minorStream << minor;
  193. targetVersionMajor = majorStream.str();
  194. targetVersionMinor = minorStream.str();
  195. }
  196. vars.TargetVersionMajor = targetVersionMajor.c_str();
  197. vars.TargetVersionMinor = targetVersionMinor.c_str();
  198. vars.Flags = "$FLAGS";
  199. vars.LinkFlags = "$LINK_FLAGS";
  200. std::string langFlags;
  201. if (targetType != cmTarget::EXECUTABLE)
  202. {
  203. langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
  204. vars.LanguageCompileFlags = langFlags.c_str();
  205. }
  206. // Rule for linking library/executable.
  207. std::vector<std::string> linkCmds = this->ComputeLinkCmd();
  208. for(std::vector<std::string>::iterator i = linkCmds.begin();
  209. i != linkCmds.end();
  210. ++i)
  211. {
  212. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  213. }
  214. linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
  215. linkCmds.push_back("$POST_BUILD");
  216. std::string linkCmd =
  217. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  218. // Write the linker rule with response file if needed.
  219. cmOStringStream comment;
  220. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  221. << this->GetVisibleTypeName() << ".";
  222. cmOStringStream description;
  223. description << "Linking " << this->TargetLinkLanguage << " "
  224. << this->GetVisibleTypeName() << " $out";
  225. this->GetGlobalGenerator()->AddRule(ruleName,
  226. linkCmd,
  227. description.str(),
  228. comment.str(),
  229. /*depfile*/ "",
  230. /*deptype*/ "",
  231. rspfile,
  232. rspcontent,
  233. /*restat*/ false,
  234. /*generator*/ false);
  235. }
  236. if (this->TargetNameOut != this->TargetNameReal &&
  237. !this->GetTarget()->IsFrameworkOnApple()) {
  238. std::string cmakeCommand =
  239. this->GetLocalGenerator()->ConvertToOutputFormat(
  240. this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
  241. cmLocalGenerator::SHELL);
  242. if (targetType == cmTarget::EXECUTABLE)
  243. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
  244. cmakeCommand +
  245. " -E cmake_symlink_executable"
  246. " $in $out && $POST_BUILD",
  247. "Creating executable symlink $out",
  248. "Rule for creating "
  249. "executable symlink.",
  250. /*depfile*/ "",
  251. /*deptype*/ "",
  252. /*rspfile*/ "",
  253. /*rspcontent*/ "",
  254. /*restat*/ false,
  255. /*generator*/ false);
  256. else
  257. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
  258. cmakeCommand +
  259. " -E cmake_symlink_library"
  260. " $in $SONAME $out && $POST_BUILD",
  261. "Creating library symlink $out",
  262. "Rule for creating "
  263. "library symlink.",
  264. /*depfile*/ "",
  265. /*deptype*/ "",
  266. /*rspfile*/ "",
  267. /*rspcontent*/ "",
  268. /*restat*/ false,
  269. /*generator*/ false);
  270. }
  271. }
  272. std::vector<std::string>
  273. cmNinjaNormalTargetGenerator
  274. ::ComputeLinkCmd()
  275. {
  276. std::vector<std::string> linkCmds;
  277. cmMakefile* mf = this->GetMakefile();
  278. {
  279. std::string linkCmdVar = this->GetGeneratorTarget()
  280. ->GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName());
  281. const char *linkCmd = mf->GetDefinition(linkCmdVar);
  282. if (linkCmd)
  283. {
  284. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  285. return linkCmds;
  286. }
  287. }
  288. switch (this->GetTarget()->GetType()) {
  289. case cmTarget::STATIC_LIBRARY: {
  290. // We have archive link commands set. First, delete the existing archive.
  291. {
  292. std::string cmakeCommand =
  293. this->GetLocalGenerator()->ConvertToOutputFormat(
  294. mf->GetRequiredDefinition("CMAKE_COMMAND"),
  295. cmLocalGenerator::SHELL);
  296. linkCmds.push_back(cmakeCommand + " -E remove $out");
  297. }
  298. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
  299. {
  300. std::string linkCmdVar = "CMAKE_";
  301. linkCmdVar += this->TargetLinkLanguage;
  302. linkCmdVar += "_ARCHIVE_CREATE";
  303. const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  304. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  305. }
  306. {
  307. std::string linkCmdVar = "CMAKE_";
  308. linkCmdVar += this->TargetLinkLanguage;
  309. linkCmdVar += "_ARCHIVE_FINISH";
  310. const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  311. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  312. }
  313. return linkCmds;
  314. }
  315. case cmTarget::SHARED_LIBRARY:
  316. case cmTarget::MODULE_LIBRARY:
  317. case cmTarget::EXECUTABLE:
  318. break;
  319. default:
  320. assert(0 && "Unexpected target type");
  321. }
  322. return std::vector<std::string>();
  323. }
  324. static int calculateCommandLineLengthLimit(int linkRuleLength)
  325. {
  326. #ifdef _WIN32
  327. return 8000 - linkRuleLength;
  328. #elif defined(__linux) || defined(__APPLE__) || defined(__HAIKU__)
  329. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  330. return ((int)sysconf(_SC_ARG_MAX)) - linkRuleLength - 1000;
  331. #else
  332. (void)linkRuleLength;
  333. return -1;
  334. #endif
  335. }
  336. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  337. {
  338. cmTarget& target = *this->GetTarget();
  339. const std::string cfgName = this->GetConfigName();
  340. std::string targetOutput = ConvertToNinjaPath(
  341. target.GetFullPath(cfgName));
  342. std::string targetOutputReal = ConvertToNinjaPath(
  343. target.GetFullPath(cfgName,
  344. /*implib=*/false,
  345. /*realpath=*/true));
  346. std::string targetOutputImplib = ConvertToNinjaPath(
  347. target.GetFullPath(cfgName,
  348. /*implib=*/true));
  349. if (target.IsAppBundleOnApple())
  350. {
  351. // Create the app bundle
  352. std::string outpath = target.GetDirectory(cfgName);
  353. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  354. // Calculate the output path
  355. targetOutput = outpath;
  356. targetOutput += "/";
  357. targetOutput += this->TargetNameOut;
  358. targetOutput = this->ConvertToNinjaPath(targetOutput);
  359. targetOutputReal = outpath;
  360. targetOutputReal += "/";
  361. targetOutputReal += this->TargetNameReal;
  362. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
  363. }
  364. else if (target.IsFrameworkOnApple())
  365. {
  366. // Create the library framework.
  367. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  368. target.GetDirectory(cfgName));
  369. }
  370. else if(target.IsCFBundleOnApple())
  371. {
  372. // Create the core foundation bundle.
  373. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  374. target.GetDirectory(cfgName));
  375. }
  376. // Write comments.
  377. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  378. const cmTarget::TargetType targetType = target.GetType();
  379. this->GetBuildFileStream()
  380. << "# Link build statements for "
  381. << cmTarget::GetTargetTypeName(targetType)
  382. << " target "
  383. << this->GetTargetName()
  384. << "\n\n";
  385. cmNinjaDeps emptyDeps;
  386. cmNinjaVars vars;
  387. // Compute the comment.
  388. cmOStringStream comment;
  389. comment <<
  390. "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
  391. // Compute outputs.
  392. cmNinjaDeps outputs;
  393. outputs.push_back(targetOutputReal);
  394. // Compute specific libraries to link with.
  395. cmNinjaDeps explicitDeps = this->GetObjects();
  396. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  397. cmMakefile* mf = this->GetMakefile();
  398. std::string frameworkPath;
  399. std::string linkPath;
  400. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  401. std::string createRule =
  402. genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
  403. this->GetConfigName());
  404. bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
  405. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  406. localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
  407. vars["FLAGS"],
  408. vars["LINK_FLAGS"],
  409. frameworkPath,
  410. linkPath,
  411. &genTarget,
  412. useWatcomQuote);
  413. this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
  414. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  415. vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
  416. ::EncodeLiteral(vars["LINK_FLAGS"]);
  417. vars["LINK_PATH"] = frameworkPath + linkPath;
  418. // Compute architecture specific link flags. Yes, these go into a different
  419. // variable for executables, probably due to a mistake made when duplicating
  420. // code between the Makefile executable and library generators.
  421. if (targetType == cmTarget::EXECUTABLE)
  422. {
  423. std::string t = vars["FLAGS"];
  424. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  425. vars["FLAGS"] = t;
  426. }
  427. else
  428. {
  429. std::string t = vars["ARCH_FLAGS"];
  430. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  431. vars["ARCH_FLAGS"] = t;
  432. t = "";
  433. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  434. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  435. }
  436. if (target.HasSOName(cfgName))
  437. {
  438. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  439. vars["SONAME"] = this->TargetNameSO;
  440. if (targetType == cmTarget::SHARED_LIBRARY)
  441. {
  442. std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
  443. if (!install_dir.empty())
  444. {
  445. vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
  446. cmLocalGenerator::NONE,
  447. cmLocalGenerator::SHELL,
  448. false);
  449. }
  450. }
  451. }
  452. if (!this->TargetNameImport.empty())
  453. {
  454. const std::string impLibPath = localGen.ConvertToOutputFormat(
  455. targetOutputImplib,
  456. cmLocalGenerator::SHELL);
  457. vars["TARGET_IMPLIB"] = impLibPath;
  458. EnsureParentDirectoryExists(impLibPath);
  459. }
  460. if (!this->SetMsvcTargetPdbVariable(vars))
  461. {
  462. // It is common to place debug symbols at a specific place,
  463. // so we need a plain target name in the rule available.
  464. std::string prefix;
  465. std::string base;
  466. std::string suffix;
  467. target.GetFullNameComponents(prefix, base, suffix);
  468. std::string dbg_suffix = ".dbg";
  469. // TODO: Where to document?
  470. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  471. {
  472. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  473. }
  474. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  475. }
  476. if (mf->IsOn("CMAKE_COMPILER_IS_MINGW"))
  477. {
  478. const std::string objPath = GetTarget()->GetSupportDirectory();
  479. vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath);
  480. EnsureDirectoryExists(objPath);
  481. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  482. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  483. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  484. std::string& link_path = vars["LINK_PATH"];
  485. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  486. }
  487. const std::vector<cmCustomCommand> *cmdLists[3] = {
  488. &target.GetPreBuildCommands(),
  489. &target.GetPreLinkCommands(),
  490. &target.GetPostBuildCommands()
  491. };
  492. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  493. std::vector<std::string> *cmdLineLists[3] = {
  494. &preLinkCmdLines,
  495. &preLinkCmdLines,
  496. &postBuildCmdLines
  497. };
  498. for (unsigned i = 0; i != 3; ++i)
  499. {
  500. for (std::vector<cmCustomCommand>::const_iterator
  501. ci = cmdLists[i]->begin();
  502. ci != cmdLists[i]->end(); ++ci)
  503. {
  504. cmCustomCommandGenerator ccg(*ci, cfgName, mf);
  505. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  506. }
  507. }
  508. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  509. // the link commands.
  510. if (!preLinkCmdLines.empty())
  511. {
  512. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  513. mf->GetHomeOutputDirectory(),
  514. cmLocalGenerator::SHELL);
  515. preLinkCmdLines.push_back("cd " + homeOutDir);
  516. }
  517. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  518. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  519. cmNinjaVars symlinkVars;
  520. if (targetOutput == targetOutputReal)
  521. {
  522. vars["POST_BUILD"] = postBuildCmdLine;
  523. }
  524. else
  525. {
  526. vars["POST_BUILD"] = ":";
  527. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  528. }
  529. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  530. int commandLineLengthLimit = 1;
  531. const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  532. if (!mf->IsDefinitionSet(forceRspFile) &&
  533. cmSystemTools::GetEnv(forceRspFile) == 0)
  534. {
  535. commandLineLengthLimit = calculateCommandLineLengthLimit(
  536. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  537. }
  538. const std::string rspfile =
  539. std::string(cmake::GetCMakeFilesDirectoryPostSlash())
  540. + target.GetName() + ".rsp";
  541. // Gather order-only dependencies.
  542. cmNinjaDeps orderOnlyDeps;
  543. this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
  544. orderOnlyDeps);
  545. // Write the build statement for this target.
  546. globalGen.WriteBuild(this->GetBuildFileStream(),
  547. comment.str(),
  548. this->LanguageLinkerRule(),
  549. outputs,
  550. explicitDeps,
  551. implicitDeps,
  552. orderOnlyDeps,
  553. vars,
  554. rspfile,
  555. commandLineLengthLimit);
  556. if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
  557. {
  558. if (targetType == cmTarget::EXECUTABLE)
  559. {
  560. globalGen.WriteBuild(this->GetBuildFileStream(),
  561. "Create executable symlink " + targetOutput,
  562. "CMAKE_SYMLINK_EXECUTABLE",
  563. cmNinjaDeps(1, targetOutput),
  564. cmNinjaDeps(1, targetOutputReal),
  565. emptyDeps,
  566. emptyDeps,
  567. symlinkVars);
  568. }
  569. else
  570. {
  571. cmNinjaDeps symlinks;
  572. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  573. // If one link has to be created.
  574. if (targetOutputReal == soName || targetOutput == soName)
  575. {
  576. symlinkVars["SONAME"] = soName;
  577. }
  578. else
  579. {
  580. symlinkVars["SONAME"] = "";
  581. symlinks.push_back(soName);
  582. }
  583. symlinks.push_back(targetOutput);
  584. globalGen.WriteBuild(this->GetBuildFileStream(),
  585. "Create library symlink " + targetOutput,
  586. "CMAKE_SYMLINK_LIBRARY",
  587. symlinks,
  588. cmNinjaDeps(1, targetOutputReal),
  589. emptyDeps,
  590. emptyDeps,
  591. symlinkVars);
  592. }
  593. }
  594. if (!this->TargetNameImport.empty())
  595. {
  596. // Since using multiple outputs would mess up the $out variable, use an
  597. // alias for the import library.
  598. globalGen.WritePhonyBuild(this->GetBuildFileStream(),
  599. "Alias for import library.",
  600. cmNinjaDeps(1, targetOutputImplib),
  601. cmNinjaDeps(1, targetOutputReal));
  602. }
  603. // Add aliases for the file name and the target name.
  604. globalGen.AddTargetAlias(this->TargetNameOut, &target);
  605. globalGen.AddTargetAlias(this->GetTargetName(), &target);
  606. }
  607. //----------------------------------------------------------------------------
  608. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  609. {
  610. // Write a phony output that depends on all object files.
  611. cmNinjaDeps outputs;
  612. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  613. cmNinjaDeps depends = this->GetObjects();
  614. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  615. "Object library "
  616. + this->GetTargetName(),
  617. outputs,
  618. depends);
  619. // Add aliases for the target name.
  620. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  621. this->GetTarget());
  622. }