cmNinjaNormalTargetGenerator.cxx 24 KB

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