cmNinjaNormalTargetGenerator.cxx 24 KB

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