cmNinjaNormalTargetGenerator.cxx 25 KB

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