cmNinjaNormalTargetGenerator.cxx 24 KB

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