cmNinjaNormalTargetGenerator.cxx 24 KB

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