cmNinjaNormalTargetGenerator.cxx 25 KB

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