cmNinjaNormalTargetGenerator.cxx 25 KB

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