cmNinjaNormalTargetGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. // 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. }
  146. void
  147. cmNinjaNormalTargetGenerator
  148. ::WriteLinkRule(bool useResponseFile)
  149. {
  150. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  151. std::string ruleName = this->LanguageLinkerRule();
  152. if (useResponseFile)
  153. ruleName += "_RSP_FILE";
  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. rspcontent = "$in_newline $LINK_PATH $LINK_LIBRARIES";
  181. vars.Objects = responseFlag.c_str();
  182. vars.LinkLibraries = "";
  183. }
  184. vars.ObjectDir = "$OBJECT_DIR";
  185. // TODO:
  186. // Makefile generator expands <TARGET> to the plain target name
  187. // with suffix. $out expands to a relative path. This difference
  188. // could make trouble when switching to Ninja generator. Maybe
  189. // using TARGET_NAME and RuleVariables::TargetName is a fix.
  190. vars.Target = "$out";
  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. cmOStringStream majorStream;
  200. cmOStringStream 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. cmOStringStream comment;
  233. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  234. << this->GetVisibleTypeName() << ".";
  235. cmOStringStream description;
  236. description << "Linking " << this->TargetLinkLanguage << " "
  237. << this->GetVisibleTypeName() << " $out";
  238. this->GetGlobalGenerator()->AddRule(ruleName,
  239. linkCmd,
  240. description.str(),
  241. comment.str(),
  242. /*depfile*/ "",
  243. /*deptype*/ "",
  244. rspfile,
  245. rspcontent,
  246. /*restat*/ false,
  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*/ false,
  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*/ false,
  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 $out");
  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. #ifdef _WIN32
  340. return 8000 - linkRuleLength;
  341. #elif defined(__linux) || defined(__APPLE__) || defined(__HAIKU__)
  342. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  343. return ((int)sysconf(_SC_ARG_MAX)) - linkRuleLength - 1000;
  344. #else
  345. (void)linkRuleLength;
  346. return -1;
  347. #endif
  348. }
  349. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  350. {
  351. cmTarget& target = *this->GetTarget();
  352. const std::string cfgName = this->GetConfigName();
  353. std::string targetOutput = ConvertToNinjaPath(
  354. target.GetFullPath(cfgName).c_str());
  355. std::string targetOutputReal = ConvertToNinjaPath(
  356. target.GetFullPath(cfgName,
  357. /*implib=*/false,
  358. /*realpath=*/true).c_str());
  359. std::string targetOutputImplib = ConvertToNinjaPath(
  360. target.GetFullPath(cfgName,
  361. /*implib=*/true).c_str());
  362. if (target.IsAppBundleOnApple())
  363. {
  364. // Create the app bundle
  365. std::string outpath = target.GetDirectory(cfgName);
  366. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  367. // Calculate the output path
  368. targetOutput = outpath;
  369. targetOutput += "/";
  370. targetOutput += this->TargetNameOut;
  371. targetOutput = this->ConvertToNinjaPath(targetOutput.c_str());
  372. targetOutputReal = outpath;
  373. targetOutputReal += "/";
  374. targetOutputReal += this->TargetNameReal;
  375. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal.c_str());
  376. }
  377. else if (target.IsFrameworkOnApple())
  378. {
  379. // Create the library framework.
  380. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  381. target.GetDirectory(cfgName));
  382. }
  383. else if(target.IsCFBundleOnApple())
  384. {
  385. // Create the core foundation bundle.
  386. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  387. target.GetDirectory(cfgName));
  388. }
  389. // Write comments.
  390. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  391. const cmTarget::TargetType targetType = target.GetType();
  392. this->GetBuildFileStream()
  393. << "# Link build statements for "
  394. << cmTarget::GetTargetTypeName(targetType)
  395. << " target "
  396. << this->GetTargetName()
  397. << "\n\n";
  398. cmNinjaDeps emptyDeps;
  399. cmNinjaVars vars;
  400. // Compute the comment.
  401. cmOStringStream comment;
  402. comment <<
  403. "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
  404. // Compute outputs.
  405. cmNinjaDeps outputs;
  406. outputs.push_back(targetOutputReal);
  407. // Compute specific libraries to link with.
  408. cmNinjaDeps explicitDeps = this->GetObjects();
  409. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  410. cmMakefile* mf = this->GetMakefile();
  411. std::string frameworkPath;
  412. std::string linkPath;
  413. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  414. std::string createRule =
  415. genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
  416. this->GetConfigName());
  417. bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
  418. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  419. localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
  420. vars["FLAGS"],
  421. vars["LINK_FLAGS"],
  422. frameworkPath,
  423. linkPath,
  424. &genTarget,
  425. useWatcomQuote);
  426. this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
  427. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  428. vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
  429. ::EncodeLiteral(vars["LINK_FLAGS"]);
  430. vars["LINK_PATH"] = frameworkPath + linkPath;
  431. // Compute architecture specific link flags. Yes, these go into a different
  432. // variable for executables, probably due to a mistake made when duplicating
  433. // code between the Makefile executable and library generators.
  434. if (targetType == cmTarget::EXECUTABLE)
  435. {
  436. std::string t = vars["FLAGS"];
  437. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  438. vars["FLAGS"] = t;
  439. }
  440. else
  441. {
  442. std::string t = vars["ARCH_FLAGS"];
  443. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  444. vars["ARCH_FLAGS"] = t;
  445. t = "";
  446. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  447. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  448. }
  449. if (target.HasSOName(cfgName))
  450. {
  451. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  452. vars["SONAME"] = this->TargetNameSO;
  453. if (targetType == cmTarget::SHARED_LIBRARY)
  454. {
  455. std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
  456. if (!install_dir.empty())
  457. {
  458. vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
  459. cmLocalGenerator::NONE,
  460. cmLocalGenerator::SHELL,
  461. false);
  462. }
  463. }
  464. }
  465. if (!this->TargetNameImport.empty())
  466. {
  467. const std::string impLibPath = localGen.ConvertToOutputFormat(
  468. targetOutputImplib,
  469. cmLocalGenerator::SHELL);
  470. vars["TARGET_IMPLIB"] = impLibPath;
  471. EnsureParentDirectoryExists(impLibPath);
  472. }
  473. if (!this->SetMsvcTargetPdbVariable(vars))
  474. {
  475. // It is common to place debug symbols at a specific place,
  476. // so we need a plain target name in the rule available.
  477. std::string prefix;
  478. std::string base;
  479. std::string suffix;
  480. target.GetFullNameComponents(prefix, base, suffix);
  481. std::string dbg_suffix = ".dbg";
  482. // TODO: Where to document?
  483. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  484. {
  485. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  486. }
  487. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  488. }
  489. if (mf->IsOn("CMAKE_COMPILER_IS_MINGW"))
  490. {
  491. const std::string objPath = GetTarget()->GetSupportDirectory();
  492. vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath.c_str());
  493. EnsureDirectoryExists(objPath);
  494. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  495. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  496. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  497. std::string& link_path = vars["LINK_PATH"];
  498. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  499. }
  500. const std::vector<cmCustomCommand> *cmdLists[3] = {
  501. &target.GetPreBuildCommands(),
  502. &target.GetPreLinkCommands(),
  503. &target.GetPostBuildCommands()
  504. };
  505. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  506. std::vector<std::string> *cmdLineLists[3] = {
  507. &preLinkCmdLines,
  508. &preLinkCmdLines,
  509. &postBuildCmdLines
  510. };
  511. for (unsigned i = 0; i != 3; ++i)
  512. {
  513. for (std::vector<cmCustomCommand>::const_iterator
  514. ci = cmdLists[i]->begin();
  515. ci != cmdLists[i]->end(); ++ci)
  516. {
  517. cmCustomCommandGenerator ccg(*ci, cfgName, mf);
  518. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  519. }
  520. }
  521. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  522. // the link commands.
  523. if (!preLinkCmdLines.empty())
  524. {
  525. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  526. mf->GetHomeOutputDirectory(),
  527. cmLocalGenerator::SHELL);
  528. preLinkCmdLines.push_back("cd " + homeOutDir);
  529. }
  530. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  531. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  532. cmNinjaVars symlinkVars;
  533. if (targetOutput == targetOutputReal)
  534. {
  535. vars["POST_BUILD"] = postBuildCmdLine;
  536. }
  537. else
  538. {
  539. vars["POST_BUILD"] = ":";
  540. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  541. }
  542. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  543. int commandLineLengthLimit = 1;
  544. const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  545. if (!mf->IsDefinitionSet(forceRspFile) &&
  546. cmSystemTools::GetEnv(forceRspFile) == 0)
  547. {
  548. commandLineLengthLimit = calculateCommandLineLengthLimit(
  549. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  550. }
  551. const std::string rspfile =
  552. std::string(cmake::GetCMakeFilesDirectoryPostSlash())
  553. + target.GetName() + ".rsp";
  554. // Gather order-only dependencies.
  555. cmNinjaDeps orderOnlyDeps;
  556. this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
  557. orderOnlyDeps);
  558. // Write the build statement for this target.
  559. globalGen.WriteBuild(this->GetBuildFileStream(),
  560. comment.str(),
  561. this->LanguageLinkerRule(),
  562. outputs,
  563. explicitDeps,
  564. implicitDeps,
  565. orderOnlyDeps,
  566. vars,
  567. rspfile,
  568. commandLineLengthLimit);
  569. if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
  570. {
  571. if (targetType == cmTarget::EXECUTABLE)
  572. {
  573. globalGen.WriteBuild(this->GetBuildFileStream(),
  574. "Create executable symlink " + targetOutput,
  575. "CMAKE_SYMLINK_EXECUTABLE",
  576. cmNinjaDeps(1, targetOutput),
  577. cmNinjaDeps(1, targetOutputReal),
  578. emptyDeps,
  579. emptyDeps,
  580. symlinkVars);
  581. }
  582. else
  583. {
  584. cmNinjaDeps symlinks;
  585. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  586. // If one link has to be created.
  587. if (targetOutputReal == soName || targetOutput == soName)
  588. {
  589. symlinkVars["SONAME"] = soName;
  590. }
  591. else
  592. {
  593. symlinkVars["SONAME"] = "";
  594. symlinks.push_back(soName);
  595. }
  596. symlinks.push_back(targetOutput);
  597. globalGen.WriteBuild(this->GetBuildFileStream(),
  598. "Create library symlink " + targetOutput,
  599. "CMAKE_SYMLINK_LIBRARY",
  600. symlinks,
  601. cmNinjaDeps(1, targetOutputReal),
  602. emptyDeps,
  603. emptyDeps,
  604. symlinkVars);
  605. }
  606. }
  607. if (!this->TargetNameImport.empty())
  608. {
  609. // Since using multiple outputs would mess up the $out variable, use an
  610. // alias for the import library.
  611. globalGen.WritePhonyBuild(this->GetBuildFileStream(),
  612. "Alias for import library.",
  613. cmNinjaDeps(1, targetOutputImplib),
  614. cmNinjaDeps(1, targetOutputReal));
  615. }
  616. // Add aliases for the file name and the target name.
  617. globalGen.AddTargetAlias(this->TargetNameOut, &target);
  618. globalGen.AddTargetAlias(this->GetTargetName(), &target);
  619. }
  620. //----------------------------------------------------------------------------
  621. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  622. {
  623. // Write a phony output that depends on all object files.
  624. cmNinjaDeps outputs;
  625. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  626. cmNinjaDeps depends = this->GetObjects();
  627. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  628. "Object library "
  629. + this->GetTargetName(),
  630. outputs,
  631. depends);
  632. // Add aliases for the target name.
  633. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  634. this->GetTarget());
  635. }