cmNinjaNormalTargetGenerator.cxx 25 KB

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